diff --git a/custom_file_dialog/custom_file_dialog.py b/custom_file_dialog/custom_file_dialog.py index d1fc007..7a7d075 100644 --- a/custom_file_dialog/custom_file_dialog.py +++ b/custom_file_dialog/custom_file_dialog.py @@ -4,7 +4,7 @@ import tkinter as tk import subprocess import json import threading -from typing import Optional, List, Tuple, Dict +from typing import Optional, List, Tuple, Dict, Union from shared_libs.common_tools import IconManager, Tooltip, LxTools from .cfd_app_config import CfdConfigManager, LocaleStrings from .cfd_ui_setup import StyleManager, WidgetManager @@ -33,7 +33,7 @@ class CustomFileDialog(tk.Toplevel): def __init__(self, parent: tk.Widget, initial_dir: Optional[str] = None, filetypes: Optional[List[Tuple[str, str]]] = None, - dialog_mode: str = "open", title: str = LocaleStrings.CFD["title"]): + mode: str = "open", title: str = LocaleStrings.CFD["title"]): """ Initializes the CustomFileDialog. @@ -41,13 +41,16 @@ class CustomFileDialog(tk.Toplevel): parent: The parent widget. initial_dir: The initial directory to display. filetypes: A list of filetype tuples, e.g., [('Text files', '*.txt')]. - dialog_mode: The dialog mode, either "open" or "save". + mode: The dialog mode. Can be "open" (select single file), + "save" (select single file for saving), + "multi" (select multiple files/directories), + or "dir" (select a single directory). title: The title of the dialog window. """ super().__init__(parent) self.my_tool_tip: Optional[Tooltip] = None - self.dialog_mode: str = dialog_mode + self.dialog_mode: str = mode self.load_settings() @@ -65,7 +68,7 @@ class CustomFileDialog(tk.Toplevel): self.transient(parent) self.grab_set() - self.selected_file: Optional[str] = None + self.result: Optional[Union[str, List[str]]] = None self.current_dir: str = os.path.abspath( initial_dir) if initial_dir else os.path.expanduser("~") self.filetypes: List[Tuple[str, str]] = filetypes if filetypes else [ @@ -361,7 +364,8 @@ class CustomFileDialog(tk.Toplevel): self.widget_manager.devices_scrollbar.grid_remove() def toggle_recursive_search(self) -> None: - """Toggles the recursive search option on or off.""" + """ + Toggles the recursive search option on or off.""" self.widget_manager.recursive_search.set( not self.widget_manager.recursive_search.get()) if self.widget_manager.recursive_search.get(): @@ -408,29 +412,31 @@ class CustomFileDialog(tk.Toplevel): def on_open(self) -> None: """Handles the 'Open' action, closing the dialog if a file is selected.""" - if self.selected_file and os.path.isfile(self.selected_file): + if self.result and isinstance(self.result, str) and os.path.isfile(self.result): self.destroy() def on_save(self) -> None: """Handles the 'Save' action, setting the selected file and closing the dialog.""" file_name = self.widget_manager.filename_entry.get() if file_name: - self.selected_file = os.path.join(self.current_dir, file_name) + self.result = os.path.join(self.current_dir, file_name) self.destroy() def on_cancel(self) -> None: """Handles the 'Cancel' action, clearing the selection and closing the dialog.""" - self.selected_file = None + self.result = None self.destroy() - def get_selected_file(self) -> Optional[str]: + def get_result(self) -> Optional[Union[str, List[str]]]: """ - Returns the path of the selected file. + Returns the result of the dialog. Returns: - The selected file path, or None if no file was selected. + - A string containing a single path for modes 'open', 'save', 'dir'. + - A list of strings for mode 'multi'. + - None if the dialog was cancelled. """ - return self.selected_file + return self.result def update_action_buttons_state(self) -> None: """Updates the state of action buttons (e.g., 'New Folder') based on directory permissions.""" @@ -560,6 +566,4 @@ class CustomFileDialog(tk.Toplevel): except Exception as e: print(f"Error getting mounted devices: {e}") - return devices - - + return devices \ No newline at end of file