refactor(cfd): Bereitet den Dialog für neue Modi (multi, dir) vor

This commit is contained in:
2025-08-13 23:39:11 +02:00
parent 16937faf91
commit 2c2163b936

View File

@@ -4,7 +4,7 @@ import tkinter as tk
import subprocess import subprocess
import json import json
import threading 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 shared_libs.common_tools import IconManager, Tooltip, LxTools
from .cfd_app_config import CfdConfigManager, LocaleStrings from .cfd_app_config import CfdConfigManager, LocaleStrings
from .cfd_ui_setup import StyleManager, WidgetManager 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, def __init__(self, parent: tk.Widget, initial_dir: Optional[str] = None,
filetypes: Optional[List[Tuple[str, 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. Initializes the CustomFileDialog.
@@ -41,13 +41,16 @@ class CustomFileDialog(tk.Toplevel):
parent: The parent widget. parent: The parent widget.
initial_dir: The initial directory to display. initial_dir: The initial directory to display.
filetypes: A list of filetype tuples, e.g., [('Text files', '*.txt')]. 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. title: The title of the dialog window.
""" """
super().__init__(parent) super().__init__(parent)
self.my_tool_tip: Optional[Tooltip] = None self.my_tool_tip: Optional[Tooltip] = None
self.dialog_mode: str = dialog_mode self.dialog_mode: str = mode
self.load_settings() self.load_settings()
@@ -65,7 +68,7 @@ class CustomFileDialog(tk.Toplevel):
self.transient(parent) self.transient(parent)
self.grab_set() self.grab_set()
self.selected_file: Optional[str] = None self.result: Optional[Union[str, List[str]]] = None
self.current_dir: str = os.path.abspath( self.current_dir: str = os.path.abspath(
initial_dir) if initial_dir else os.path.expanduser("~") initial_dir) if initial_dir else os.path.expanduser("~")
self.filetypes: List[Tuple[str, str]] = filetypes if filetypes else [ 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() self.widget_manager.devices_scrollbar.grid_remove()
def toggle_recursive_search(self) -> None: 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( self.widget_manager.recursive_search.set(
not self.widget_manager.recursive_search.get()) not self.widget_manager.recursive_search.get())
if 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: def on_open(self) -> None:
"""Handles the 'Open' action, closing the dialog if a file is selected.""" """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() self.destroy()
def on_save(self) -> None: def on_save(self) -> None:
"""Handles the 'Save' action, setting the selected file and closing the dialog.""" """Handles the 'Save' action, setting the selected file and closing the dialog."""
file_name = self.widget_manager.filename_entry.get() file_name = self.widget_manager.filename_entry.get()
if file_name: 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() self.destroy()
def on_cancel(self) -> None: def on_cancel(self) -> None:
"""Handles the 'Cancel' action, clearing the selection and closing the dialog.""" """Handles the 'Cancel' action, clearing the selection and closing the dialog."""
self.selected_file = None self.result = None
self.destroy() 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: 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: def update_action_buttons_state(self) -> None:
"""Updates the state of action buttons (e.g., 'New Folder') based on directory permissions.""" """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: except Exception as e:
print(f"Error getting mounted devices: {e}") print(f"Error getting mounted devices: {e}")
return devices return devices