add type-hints on cfd_file_operations, cfd_app_config, animated_icon

This commit is contained in:
2025-08-10 12:04:34 +02:00
parent 5a41d6b1fd
commit 30c2c3b901
3 changed files with 686 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ import os
import shutil
import tkinter as tk
from tkinter import ttk
from typing import Optional, Any, TYPE_CHECKING
try:
import send2trash
@@ -12,11 +13,14 @@ except ImportError:
from shared_libs.message import MessageDialog
from cfd_app_config import LocaleStrings, _
if TYPE_CHECKING:
from custom_file_dialog import CustomFileDialog
class FileOperationsManager:
"""Manages file operations like delete, create, and rename."""
def __init__(self, dialog):
def __init__(self, dialog: 'CustomFileDialog') -> None:
"""
Initializes the FileOperationsManager.
@@ -25,7 +29,7 @@ class FileOperationsManager:
"""
self.dialog = dialog
def delete_selected_item(self, event=None):
def delete_selected_item(self, event: Optional[tk.Event] = None) -> None:
"""
Deletes the selected item or moves it to the trash.
@@ -77,15 +81,15 @@ class FileOperationsManager:
message_type="error"
).show()
def create_new_folder(self):
def create_new_folder(self) -> None:
"""Creates a new folder in the current directory."""
self._create_new_item(is_folder=True)
def create_new_file(self):
def create_new_file(self) -> None:
"""Creates a new empty file in the current directory."""
self._create_new_item(is_folder=False)
def _create_new_item(self, is_folder):
def _create_new_item(self, is_folder: bool) -> None:
"""
Internal helper to create a new file or folder.
@@ -108,7 +112,7 @@ class FileOperationsManager:
self.dialog.widget_manager.search_status_label.config(
text=f"{LocaleStrings.FILE['error_creating']}: {e}")
def _get_unique_name(self, base_name):
def _get_unique_name(self, base_name: str) -> str:
"""
Generates a unique name for a file or folder.
@@ -129,7 +133,7 @@ class FileOperationsManager:
new_name = f"{name} {counter}{ext}"
return new_name
def _copy_to_clipboard(self, data):
def _copy_to_clipboard(self, data: str) -> None:
"""
Copies the given data to the system clipboard.
@@ -141,7 +145,7 @@ class FileOperationsManager:
self.dialog.widget_manager.search_status_label.config(
text=f"'{self.dialog.shorten_text(data, 50)}' {LocaleStrings.FILE['copied_to_clipboard']}")
def _show_context_menu(self, event, item_path):
def _show_context_menu(self, event: tk.Event, item_path: str) -> str:
"""
Displays a context menu for the selected item.
@@ -173,7 +177,7 @@ class FileOperationsManager:
self.dialog.context_menu.tk_popup(event.x_root, event.y_root)
return "break"
def _open_file_location_from_context(self, file_path):
def _open_file_location_from_context(self, file_path: str) -> None:
"""
Navigates to the location of the given file path.
@@ -192,7 +196,7 @@ class FileOperationsManager:
self.dialog.navigation_manager.navigate_to(directory)
self.dialog.after(100, lambda: self.dialog.view_manager._select_file_in_view(filename))
def on_rename_request(self, event, item_path=None, item_frame=None):
def on_rename_request(self, event: tk.Event, item_path: Optional[str] = None, item_frame: Optional[tk.Widget] = None) -> None:
"""
Handles the initial request to rename an item.
@@ -215,7 +219,7 @@ class FileOperationsManager:
if item_path and item_frame:
self.start_rename(item_frame, item_path)
def start_rename(self, item_widget, item_path):
def start_rename(self, item_widget: Any, item_path: str) -> None:
"""
Starts the renaming UI for an item.
@@ -231,7 +235,7 @@ class FileOperationsManager:
else: # list view
self._start_rename_list_view(item_widget) # item_widget is item_id
def _start_rename_icon_view(self, item_frame, item_path):
def _start_rename_icon_view(self, item_frame: ttk.Frame, item_path: str) -> None:
"""
Initiates the in-place rename UI for an item in icon view.
@@ -250,7 +254,7 @@ class FileOperationsManager:
entry.select_range(0, tk.END)
entry.focus_set()
def finish_rename(event):
def finish_rename(event: tk.Event) -> None:
new_name = entry.get()
new_path = os.path.join(self.dialog.current_dir, new_name)
if new_name and new_path != item_path:
@@ -270,14 +274,14 @@ class FileOperationsManager:
else:
self.dialog.view_manager.populate_files(item_to_select=os.path.basename(item_path))
def cancel_rename(event):
def cancel_rename(event: tk.Event) -> None:
self.dialog.view_manager.populate_files()
entry.bind("<Return>", finish_rename)
entry.bind("<FocusOut>", finish_rename)
entry.bind("<Escape>", cancel_rename)
def _start_rename_list_view(self, item_id):
def _start_rename_list_view(self, item_id: str) -> None:
"""
Initiates the in-place rename UI for an item in list view.
@@ -304,7 +308,7 @@ class FileOperationsManager:
entry.select_range(0, tk.END)
entry.focus_set()
def finish_rename(event):
def finish_rename(event: tk.Event) -> None:
new_name = entry.get()
old_path = os.path.join(self.dialog.current_dir, item_text)
new_path = os.path.join(self.dialog.current_dir, new_name)
@@ -326,7 +330,7 @@ class FileOperationsManager:
self.dialog.view_manager.populate_files(item_to_select=item_text)
entry.destroy()
def cancel_rename(event):
def cancel_rename(event: tk.Event) -> None:
entry.destroy()
entry.bind("<Return>", finish_rename)