Skip to content

Commit

Permalink
improved activation/deactivation for input panel
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Dec 9, 2024
1 parent 927fdea commit d68eb04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/inspect_ai/_display/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,30 @@ def set_title(self, title: str) -> None:
tab.label = Text.from_markup(title)

def activate(self) -> None:
# show the tab
tabs = self.app.query_one(TabbedContent)
tabs.show_tab(self.tab_id)

# focus the first focuable child (this seems to be necessary
# to get textual to reliably make the switch). after that, focus
# the tabs control so the user can switch back w/ the keyboard
tab_pane = self.app.query_one(f"#{self.tab_id}")
panel = cast(InputPanel, tab_pane.children[0])
for child in panel.children:
if child.focusable:
child.focus()
self.app.query_one(ContentTabs).focus()
break

def deactivate(self) -> None:
tabs = self.app.query_one(TabbedContent)
if tabs.active == self.tab_id:
self.app.activate_tasks_tab()

def close(self) -> None:
self.app.activate_tasks_tab()
tabs = self.app.query_one(TabbedContent)
tabs.remove_pane(self.tab_id)
self.app.activate_tasks_tab()


class TextualTaskScreen(TaskScreen, Generic[TR]):
Expand Down
16 changes: 16 additions & 0 deletions src/inspect_ai/util/_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ async def input_panel(title: str, panel: type[TP]) -> TP:
'title' running at once. Therefore, if the panel doesn't exist it
is created, otherwise a reference to the existing panel is returned.
Examples:
Create/activate an input panel (the panel will remain after
the scope exits -- see below for open/close semantics)
```python
panel = await input_panel("Custom", CustomPanel)
panel.activate()
```
Activate and close an input panel using a context manager:
```python
async with await input_panel("Custom", CustomPanel) as panel:
...
```
Args:
title (str): Input panel title.
panel (type[TP]): Type of panel widget (must derive from `InputPanel`)
Expand Down

0 comments on commit d68eb04

Please sign in to comment.