Skip to content

Commit aa39c12

Browse files
committed
πŸ› double-click ux
1 parent 3ab3aff commit aa39c12

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

β€Žbrowsr/widgets/double_click_directory_tree.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
from __future__ import annotations
66

7-
import os
8-
import uuid
9-
from typing import Any
7+
import datetime
8+
from typing import Any, ClassVar
109

1110
from textual import on
1211
from textual.message import Message
@@ -19,19 +18,28 @@ class DoubleClickDirectoryTree(DirectoryTree):
1918
A DirectoryTree that can handle any filesystem.
2019
"""
2120

21+
_double_click_time: ClassVar[datetime.timedelta] = datetime.timedelta(
22+
seconds=0.333333
23+
)
24+
2225
def __init__(self, *args: Any, **kwargs: Any) -> None:
2326
"""
2427
Initialize the DirectoryTree
2528
"""
2629
super().__init__(*args, **kwargs)
27-
self._last_clicked_path: os.PathLike[UPath] = UPath(uuid.uuid4().hex)
30+
self._last_clicked_path: UPath = UPath(
31+
"13041530b3174c569e1895fdfc2676fc57af1e02606059e0d2472d04c1bb360f"
32+
)
33+
self._last_clicked_time = datetime.datetime(
34+
1970, 1, 1, tzinfo=datetime.timezone.utc
35+
)
2836

2937
class DoubleClicked(Message):
3038
"""
3139
A message that is emitted when the directory is changed
3240
"""
3341

34-
def __init__(self, path: os.PathLike[Any]) -> None:
42+
def __init__(self, path: UPath) -> None:
3543
"""
3644
Initialize the message
3745
"""
@@ -69,12 +77,17 @@ def handle_double_click_file(self, message: DirectoryTree.FileSelected) -> None:
6977
message.stop()
7078
self.post_message(self.FileDoubleClicked(path=message.path))
7179

72-
def is_double_click(self, path: os.PathLike[Any]) -> bool:
80+
def is_double_click(self, path: UPath) -> bool:
7381
"""
7482
Check if the path is double clicked
7583
"""
76-
if self._last_clicked_path != path:
77-
self._last_clicked_path = path
84+
click_time = datetime.datetime.now(datetime.timezone.utc)
85+
click_delta = click_time - self._last_clicked_time
86+
self._last_clicked_time = click_time
87+
if self._last_clicked_path == path and click_delta <= self._double_click_time:
88+
return True
89+
elif self._last_clicked_path == path and click_delta > self._double_click_time:
7890
return False
7991
else:
80-
return True
92+
self._last_clicked_path = path
93+
return False

β€Žbrowsr/widgets/universal_directory_tree.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from browsr.widgets.vim import vim_cursor_bindings
1616

1717

18-
class BrowsrDirectoryTree(UniversalDirectoryTree, DoubleClickDirectoryTree):
18+
class BrowsrDirectoryTree(DoubleClickDirectoryTree, UniversalDirectoryTree):
1919
"""
2020
A DirectoryTree that can handle any filesystem.
2121
"""

0 commit comments

Comments
Β (0)