diff --git a/frogmouth/screens/main.py b/frogmouth/screens/main.py index f6f1a43..9e2ea64 100644 --- a/frogmouth/screens/main.py +++ b/frogmouth/screens/main.py @@ -111,7 +111,7 @@ def compose(self) -> ComposeResult: yield Viewer(classes="focusable") yield Footer() - def visit(self, location: Path | URL, remember: bool = True) -> None: + def visit(self, location: Path | URL, remember: bool = True, focus:bool = True) -> None: """Visit the given location. Args: @@ -122,7 +122,7 @@ def visit(self, location: Path | URL, remember: bool = True) -> None: # locally in the filesystem or out on the web... if maybe_markdown(location): # ...attempt to visit it in the viewer. - self.query_one(Viewer).visit(location, remember) + self.query_one(Viewer).visit(location, remember, focus) elif isinstance(location, Path): # So, it's not Markdown, but it *is* a Path of some sort. If the # resource seems to exist... @@ -303,7 +303,7 @@ def on_local_files_goto(self, event: LocalFiles.Goto) -> None: Args: event: The local file visit request event. """ - self.visit(event.location) + self.visit(event.location , focus=event.focus_viewer) def on_history_goto(self, event: History.Goto) -> None: """Handle a request to go to a location from history. diff --git a/frogmouth/widgets/navigation_panes/local_files.py b/frogmouth/widgets/navigation_panes/local_files.py index 19f71cc..d979a59 100644 --- a/frogmouth/widgets/navigation_panes/local_files.py +++ b/frogmouth/widgets/navigation_panes/local_files.py @@ -4,11 +4,13 @@ from pathlib import Path from typing import Iterable +import os from httpx import URL from textual.app import ComposeResult from textual.message import Message from textual.widgets import DirectoryTree +from textual.types import DirEntry from ...utility import maybe_markdown from .navigation_pane import NavigationPane @@ -84,7 +86,7 @@ def set_focus_within(self) -> None: class Goto(Message): """Message that requests the viewer goes to a given location.""" - def __init__(self, location: Path | URL) -> None: + def __init__(self, location: Path | URL, focus_viewer:bool) -> None: """Initialise the history goto message. Args: @@ -93,6 +95,8 @@ def __init__(self, location: Path | URL) -> None: super().__init__() self.location = location """The location to go to.""" + self.focus_viewer = focus_viewer + """Viewer has to be focused or not""" def on_directory_tree_file_selected( self, event: DirectoryTree.FileSelected @@ -103,4 +107,18 @@ def on_directory_tree_file_selected( event: The direct tree selection event. """ event.stop() - self.post_message(self.Goto(Path(event.path))) + self.post_message(self.Goto(Path(event.path) , True)) + + def on_tree_node_highlighted(self, event: DirectoryTree.NodeHighlighted): + """Handle node highlight in directory tree. + + Args: + event: The directory node highlight event. + """ + + event.stop() + if not event.node.data: + return + pathEntry: DirEntry = event.node.data + + return os.path.isfile(pathEntry.path) and self.post_message(self.Goto(Path(pathEntry.path), False)) \ No newline at end of file diff --git a/frogmouth/widgets/viewer.py b/frogmouth/widgets/viewer.py index c83b936..276cd90 100644 --- a/frogmouth/widgets/viewer.py +++ b/frogmouth/widgets/viewer.py @@ -254,7 +254,7 @@ async def _remote_load(self, location: URL, remember: bool = True) -> None: # to the user. Let's be nice... open_url(str(location)) - def visit(self, location: Path | URL, remember: bool = True) -> None: + def visit(self, location: Path | URL, remember: bool = True, focus:bool=True) -> None: """Visit a location. Args: @@ -263,6 +263,7 @@ def visit(self, location: Path | URL, remember: bool = True) -> None: """ # Based on the type of the location, load up the content. if isinstance(location, Path): + self.can_focus = focus self._local_load(location.expanduser().resolve(), remember) elif isinstance(location, URL): self._remote_load(location, remember)