Skip to content

Commit

Permalink
Update directory_tree.py
Browse files Browse the repository at this point in the history
Fixed double click to work properly
  • Loading branch information
celltoolz authored Jun 21, 2024
1 parent fb0534b commit f165183
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions porcupine/plugins/directory_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,13 @@ def __init__(self, master: tkinter.Misc) -> None:

# Needs after_idle because selection hasn't updated when binding runs
self.bind("<Button-1>", self._on_click, add=True)
self.bind("<Double-1>", self._on_double_click, add=True)

self.bind("<<TreeviewOpen>>", self.open_file_or_dir, add=True)
self.bind("<<ThemeChanged>>", self._config_tags, add=True)
self.column("#0", minwidth=500) # allow scrolling sideways
self._config_tags()

self._last_click_time = 0 # Very long time since previous click, no double click
self._last_click_item: str | None = None

self._project_num_counter = 0
self.contextmenu = tkinter.Menu(tearoff=False)

Expand All @@ -118,6 +116,16 @@ def set_the_selection_correctly(self, id: str) -> None:
self.selection_set(id)
self.focus(id)

def _on_double_click(self, event: tkinter.Event[DirectoryTree]) -> str | None:
self.tk.call("focus", self)
item = self.identify_row(event.y)
if not item:
return None
self.set_the_selection_correctly(item)
if item.startswith("file"):
self.open_file_or_dir()
return "break"

def _on_click(self, event: tkinter.Event[DirectoryTree]) -> str | None:
self.tk.call("focus", self)

Expand All @@ -126,27 +134,15 @@ def _on_click(self, event: tkinter.Event[DirectoryTree]) -> str | None:
if not item:
return None

# Couldn't get <Double-Button-1> to work, so I wrote a program to
# measure max time between double clicks. It's 500ms on my system.
double_click = event.time - self._last_click_time < 500 and self._last_click_item == item

self.set_the_selection_correctly(item)
if item.startswith("file:"):
if double_click:

little_arrow_clicked = self.identify_element(event.x, event.y) == "Treeitem.indicator"

if little_arrow_clicked:
self.item(item, open=(not self.item(item, "open")))
if self.item(item, "open"):
self.open_file_or_dir()
else:
little_arrow_clicked = self.identify_element(event.x, event.y) == "Treeitem.indicator"
if double_click or little_arrow_clicked:
self.item(item, open=(not self.item(item, "open")))
if self.item(item, "open"):
self.open_file_or_dir()

self._last_click_item = item
if double_click:
# Prevent getting two double clicks with 3 quick subsequent clicks
self._last_click_time = 0
else:
self._last_click_time = event.time

return "break"

def _config_tags(self, junk: object = None) -> None:
Expand Down

0 comments on commit f165183

Please sign in to comment.