commit 44

This commit is contained in:
2025-08-03 01:39:21 +02:00
parent b1394e0f62
commit 1ca1264101
2 changed files with 92 additions and 12 deletions

View File

@@ -411,6 +411,50 @@ class CustomFileDialog(tk.Toplevel):
search_tree.bind("<Double-1>", on_search_double_click)
# Context menu
def show_context_menu(event):
iid = search_tree.identify_row(event.y)
if not iid:
return "break"
search_tree.selection_set(iid)
item = search_tree.item(iid)
filename = item['text'].strip()
directory = item['values'][0]
full_path = os.path.join(directory, filename)
self._show_context_menu(event, full_path)
return "break"
search_tree.bind("<ButtonRelease-3>", show_context_menu)
def _open_file_location(self, search_tree):
selection = search_tree.selection()
if not selection:
return
item = search_tree.item(selection[0])
filename = item['text'].strip()
directory = item['values'][0]
# Exit search mode and navigate to the directory
self.toggle_search_mode() # To restore normal view
self.navigate_to(directory)
# Select the file in the list
self.after(100, lambda: self._select_file_in_view(filename))
def _select_file_in_view(self, filename):
if self.view_mode.get() == "list":
for item_id in self.tree.get_children():
if self.tree.item(item_id, "text").strip() == filename:
self.tree.selection_set(item_id)
self.tree.focus(item_id)
self.tree.see(item_id)
break
else: # icon view
# This is more complex as items are in a grid. A simple selection is not straightforward.
# For now, we just navigate to the folder.
pass
def _unbind_mouse_wheel_events(self):
# Unbind all mouse wheel events from the root window
self.unbind_all("<MouseWheel>")
@@ -542,6 +586,7 @@ class CustomFileDialog(tk.Toplevel):
p=path: self.on_item_double_click(p))
widget.bind("<Button-1>", lambda e, p=path,
f=item_frame: self.on_item_select(p, f))
widget.bind("<ButtonRelease-3>", lambda e, p=path: self._show_context_menu(e, p))
widget.bind("<MouseWheel>", _on_mouse_wheel)
widget.bind("<Button-4>", _on_mouse_wheel)
widget.bind("<Button-5>", _on_mouse_wheel)
@@ -592,6 +637,7 @@ class CustomFileDialog(tk.Toplevel):
self.tree.bind("<Double-1>", self.on_list_double_click)
self.tree.bind("<<TreeviewSelect>>", self.on_list_select)
self.tree.bind("<F2>", self.on_rename_request)
self.tree.bind("<ButtonRelease-3>", self.on_list_context_menu)
items, error, warning = self._get_sorted_items()
if warning:
@@ -661,6 +707,16 @@ class CustomFileDialog(tk.Toplevel):
self.widget_manager.filename_entry.delete(0, tk.END)
self.widget_manager.filename_entry.insert(0, item_text)
def on_list_context_menu(self, event):
iid = self.tree.identify_row(event.y)
if not iid:
return "break"
self.tree.selection_set(iid)
item_text = self.tree.item(iid, "text").strip()
item_path = os.path.join(self.current_dir, item_text)
self._show_context_menu(event, item_path)
return "break"
def on_rename_request(self, event, item_path=None, item_frame=None):
if self.view_mode.get() == "list":
if not self.tree.selection():
@@ -673,16 +729,9 @@ class CustomFileDialog(tk.Toplevel):
if item_path and item_frame:
self.start_rename(item_frame, item_path)
def _handle_unsupported_file(self, path):
if path.lower().endswith('.svg'):
self.widget_manager.status_bar.config(
text="SVG-Dateien werden nicht unterstützt.")
return True
return False
def on_item_double_click(self, path):
if self._handle_unsupported_file(path):
return
if os.path.isdir(path):
self.navigate_to(path)
else:
@@ -695,8 +744,6 @@ class CustomFileDialog(tk.Toplevel):
item_id = self.tree.selection()[0]
item_text = self.tree.item(item_id, 'text').strip()
path = os.path.join(self.current_dir, item_text)
if self._handle_unsupported_file(path):
return
if os.path.isdir(path):
self.navigate_to(path)
else:
@@ -783,8 +830,6 @@ class CustomFileDialog(tk.Toplevel):
def on_open(self):
if self.selected_file and os.path.isfile(self.selected_file):
if self._handle_unsupported_file(self.selected_file):
return
self.destroy()
def on_save(self):
@@ -830,6 +875,41 @@ class CustomFileDialog(tk.Toplevel):
new_name = f"{name} {counter}{ext}"
return new_name
def _copy_to_clipboard(self, data):
self.clipboard_clear()
self.clipboard_append(data)
self.widget_manager.status_bar.config(text=f"'{self.shorten_text(data, 50)}' in Zwischenablage kopiert.")
def _show_context_menu(self, event, item_path):
if not item_path:
return "break"
# Destroy any existing context menu
if hasattr(self, 'context_menu') and self.context_menu.winfo_exists():
self.context_menu.destroy()
self.context_menu = tk.Menu(self, tearoff=0, background=self.style_manager.header, foreground=self.style_manager.color_foreground, activebackground=self.style_manager.selection_color, activeforeground=self.style_manager.color_foreground, relief='flat', borderwidth=0)
self.context_menu.add_command(label="Dateiname in Zwischenablage", command=lambda: self._copy_to_clipboard(os.path.basename(item_path)))
self.context_menu.add_command(label="Pfad in Zwischenablage", command=lambda: self._copy_to_clipboard(item_path))
if self.search_mode:
self.context_menu.add_separator()
self.context_menu.add_command(label="Speicherort öffnen", command=lambda: self._open_file_location_from_context(item_path))
self.context_menu.tk_popup(event.x_root, event.y_root)
return "break"
def _open_file_location_from_context(self, file_path):
directory = os.path.dirname(file_path)
filename = os.path.basename(file_path)
if self.search_mode:
self.toggle_search_mode()
self.navigate_to(directory)
self.after(100, lambda: self._select_file_in_view(filename))
def start_rename(self, item_widget, item_path):
if self.view_mode.get() == "icons":
self._start_rename_icon_view(item_widget, item_path)