Skip to content

Commit

Permalink
fix: improve service tab keybinding logic
Browse files Browse the repository at this point in the history
Improves useability of service tabs (fixes #43).
Now the `next/previous_preview_tab` actions keep the focus within the
log preview output if the preview output was focused before triggering
the action. This avoids having to _tab around_ to quickly move between
tabs and navigating the output.
  • Loading branch information
isd-project committed Feb 5, 2025
1 parent 827f5da commit 1ffa08d
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions src/isd_tui/isd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,12 @@ async def journalctl_async(


class Preview(RichLog, inherit_bindings=False):
"""
`RichLog` with custom bindings and 'captive' scrolling.
If the output fits onto the presented screen, scrolling does _not_
bubble to the parent.
"""

def __init__(self, navigation_keybindings: NavigationKeybindings, **kwargs):
super().__init__(**kwargs)
for keys, action, description in [
Expand All @@ -1336,6 +1342,18 @@ def __init__(self, navigation_keybindings: NavigationKeybindings, **kwargs):
show=False,
)

@property
def allow_vertical_scroll(self) -> bool:
if self._check_disabled():
return False
return True

@property
def allow_horizontal_scroll(self) -> bool:
if self._check_disabled():
return False
return True


class PreviewArea(Container):
"""
Expand Down Expand Up @@ -1377,9 +1395,23 @@ def watch_units(self, units: List[str]) -> None:
def on_tabbed_content_tab_activated(self, _tab) -> None:
self.update_preview_window()

def focus_preview(self) -> None:
"""
Focuses the current `Preview` output.
Note: If `action_next_tab` is called within the same
update cycle, calling this function will see the _old_
pane. In this instance, you probably want to call it via
`call_after_refresh`.
"""
tabbed_content: TabbedContent = self.query_one(TabbedContent)
cur_pane = tabbed_content.active_pane

if cur_pane:
cur_pane.query_one(Preview).focus()

def action_next_tab(self) -> None:
tabs: Tabs = self.query_one(Tabs)
# may add tabs.has_focus():
tabs.action_next_tab()

def action_previous_tab(self) -> None:
Expand Down Expand Up @@ -1647,11 +1679,22 @@ def system_commands(self):
# FUTURE: Figure out how to work with/handle --global
# FUTURE: Add option to list the most recently failed units.
# -> This should be a global option, as this is a frequent use-case.

def _step_preview_tab(self, *, next: bool):
prev_focus = self.focused
preview_area = self.query_one(PreviewArea)
if next:
preview_area.action_next_tab()
else:
preview_area.action_previous_tab()
if prev_focus and isinstance(prev_focus, Preview):
self.call_after_refresh(preview_area.focus_preview)

def action_next_preview_tab(self) -> None:
self.query_one(PreviewArea).action_next_tab()
self._step_preview_tab(next=True)

def action_previous_preview_tab(self) -> None:
self.query_one(PreviewArea).action_previous_tab()
self._step_preview_tab(next=False)

def action_clear_input(self) -> None:
"""
Expand Down Expand Up @@ -2483,9 +2526,6 @@ def render_field(key, field, level: int = 0) -> str:
# add empty line between top-level keys
if level == 0:
text += "\n"
# HERE: Fix the underlying issue, where an item like
# navigation_keybindings returns text with a prefixed comment
# and then is intended _with_ the comment right here.
return indent(text, " " * level)


Expand Down

0 comments on commit 1ffa08d

Please sign in to comment.