From c3907359ad02428819484a2730387ac55857aecf Mon Sep 17 00:00:00 2001 From: Justin Flannery Date: Wed, 20 Mar 2024 14:31:30 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20double-click=20ux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browsr/widgets/double_click_directory_tree.py | 31 +++++++++++++------ browsr/widgets/universal_directory_tree.py | 2 +- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/browsr/widgets/double_click_directory_tree.py b/browsr/widgets/double_click_directory_tree.py index 054b2c4..878b892 100644 --- a/browsr/widgets/double_click_directory_tree.py +++ b/browsr/widgets/double_click_directory_tree.py @@ -4,9 +4,8 @@ from __future__ import annotations -import os -import uuid -from typing import Any +import datetime +from typing import Any, ClassVar from textual import on from textual.message import Message @@ -19,19 +18,28 @@ class DoubleClickDirectoryTree(DirectoryTree): A DirectoryTree that can handle any filesystem. """ + _double_click_time: ClassVar[datetime.timedelta] = datetime.timedelta( + seconds=0.333333 + ) + def __init__(self, *args: Any, **kwargs: Any) -> None: """ Initialize the DirectoryTree """ super().__init__(*args, **kwargs) - self._last_clicked_path: os.PathLike[UPath] = UPath(uuid.uuid4().hex) + self._last_clicked_path: UPath = UPath( + "13041530b3174c569e1895fdfc2676fc57af1e02606059e0d2472d04c1bb360f" + ) + self._last_clicked_time = datetime.datetime( + 1970, 1, 1, tzinfo=datetime.timezone.utc + ) class DoubleClicked(Message): """ A message that is emitted when the directory is changed """ - def __init__(self, path: os.PathLike[Any]) -> None: + def __init__(self, path: UPath) -> None: """ Initialize the message """ @@ -69,12 +77,17 @@ def handle_double_click_file(self, message: DirectoryTree.FileSelected) -> None: message.stop() self.post_message(self.FileDoubleClicked(path=message.path)) - def is_double_click(self, path: os.PathLike[Any]) -> bool: + def is_double_click(self, path: UPath) -> bool: """ Check if the path is double clicked """ - if self._last_clicked_path != path: - self._last_clicked_path = path + click_time = datetime.datetime.now(datetime.timezone.utc) + click_delta = click_time - self._last_clicked_time + self._last_clicked_time = click_time + if self._last_clicked_path == path and click_delta <= self._double_click_time: + return True + elif self._last_clicked_path == path and click_delta > self._double_click_time: return False else: - return True + self._last_clicked_path = path + return False diff --git a/browsr/widgets/universal_directory_tree.py b/browsr/widgets/universal_directory_tree.py index c391b30..ab96380 100644 --- a/browsr/widgets/universal_directory_tree.py +++ b/browsr/widgets/universal_directory_tree.py @@ -15,7 +15,7 @@ from browsr.widgets.vim import vim_cursor_bindings -class BrowsrDirectoryTree(UniversalDirectoryTree, DoubleClickDirectoryTree): +class BrowsrDirectoryTree(DoubleClickDirectoryTree, UniversalDirectoryTree): """ A DirectoryTree that can handle any filesystem. """