Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions libs/code/deepagents_code/widgets/theme_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class ThemeSelectorScreen(ModalScreen[str | None]):

Displays available themes in an `OptionList`. Navigating the option list
applies a live preview by swapping the app theme. Returns the selected
theme name on Enter, or `None` on Esc (restoring the original theme).
theme name on Enter, or `None` on Esc. Esc normally restores the original
theme, but if the user set a per-terminal default with `t` this session,
Esc keeps that theme active instead of reverting.
"""

BINDINGS: ClassVar[list[BindingType]] = [
Expand All @@ -47,7 +49,8 @@ class ThemeSelectorScreen(ModalScreen[str | None]):
]
"""Key bindings for the selector.

Esc dismisses and restores the original theme. Arrow keys and Enter are
Esc dismisses, restoring the original theme unless `t` set a per-terminal
default this session (in which case that theme is kept). Arrow keys and Enter are
handled natively by the embedded `OptionList`; Tab / Shift+Tab are bound
here to advance the option list cursor for consistency with other
selector screens (where Tab cycles focus across multiple widgets).
Expand Down Expand Up @@ -109,6 +112,7 @@ def __init__(self, current_theme: str, terminal_default: str | None = None) -> N
self._current_theme = current_theme
self._original_theme = current_theme
self._terminal_default = terminal_default
self._session_terminal_default: str | None = None
self._show_keys = False

def _sync_terminal_background(self) -> None:
Expand Down Expand Up @@ -218,8 +222,17 @@ def on_option_list_option_selected(self, event: OptionList.OptionSelected) -> No
self.dismiss(None)

def action_cancel(self) -> None:
"""Restore the original theme and dismiss."""
self.app.theme = self._original_theme
"""Dismiss, keeping a terminal default set this session or restoring.

Pressing `t` saves a per-terminal default and is treated as a
deliberate choice, so Esc keeps that theme active instead of
reverting. `dismiss(None)` skips the global `[ui].theme` write — the
per-terminal mapping was already persisted by `action_set_for_terminal`.
Without a `t` press, Esc restores the original theme as before.
"""
keep = self._session_terminal_default
target = keep if keep is not None else self._original_theme
self.app.theme = target
self._sync_terminal_background()
self.dismiss(None)

Expand Down Expand Up @@ -293,6 +306,9 @@ async def _persist() -> None:
timeout=6,
)
return
# Esc should only preserve a previewed theme after the terminal
Comment thread
robin-agent[bot] marked this conversation as resolved.
Resolved
# default has actually been saved.
self._session_terminal_default = name
if status.message is not None:
self.app.notify(
status.message,
Expand Down
137 changes: 137 additions & 0 deletions libs/code/tests/unit_tests/test_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,29 @@ def _register_lc_theme(app: object) -> None:
app_any.theme = "langchain"


def _register_lc_light_theme(app: object) -> None:
"""Register the light LangChain theme for preview tests."""
from textual.theme import Theme as TextualTheme

c = LIGHT_COLORS
app.register_theme( # ty: ignore
TextualTheme(
name="langchain-light",
primary=c.primary,
secondary=c.secondary,
accent=c.accent,
foreground=c.foreground,
background=c.background,
surface=c.surface,
panel=c.panel,
warning=c.warning,
error=c.error,
success=c.success,
dark=False,
)
)


class TestThemeSelectorScreen:
"""ThemeSelectorScreen widget tests."""

Expand Down Expand Up @@ -2277,6 +2300,7 @@ async def test_t_notifies_on_save_failure(
silent no-op.
"""
from textual.app import App
from textual.widgets import OptionList

from deepagents_code.app import _ConfigWriteResult
from deepagents_code.widgets.theme_selector import ThemeSelectorScreen
Expand All @@ -2298,6 +2322,7 @@ async def test_t_notifies_on_save_failure(
app = App()
async with app.run_test() as pilot:
_register_lc_theme(app)
_register_lc_light_theme(app)

def _capture(
message: str, *, severity: str = "information", **_kwargs: object
Expand All @@ -2309,13 +2334,23 @@ def _capture(
screen = ThemeSelectorScreen(current_theme="langchain")
app.push_screen(screen)
await pilot.pause()

target_key = "langchain-light"
target_index = list(theme.get_registry()).index(target_key)
option_list = screen.query_one("#theme-options", OptionList)
option_list.highlighted = target_index
await pilot.pause()

await pilot.press("t")
await app.workers.wait_for_complete()
await pilot.pause()
await pilot.press("escape")
await pilot.pause()

assert any(
sev == "error" and "terminal mapping" in msg for sev, msg in notifications
)
assert app.theme == "langchain"

async def test_t_notifies_on_save_exception(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
Expand All @@ -2326,6 +2361,7 @@ async def test_t_notifies_on_save_exception(
name (e.g., `OSError`) tells the user where to look.
"""
from textual.app import App
from textual.widgets import OptionList

from deepagents_code.widgets.theme_selector import ThemeSelectorScreen

Expand All @@ -2346,6 +2382,7 @@ def _boom(*_args: object, **_kwargs: object) -> None:
app = App()
async with app.run_test() as pilot:
_register_lc_theme(app)
_register_lc_light_theme(app)

def _capture(
message: str, *, severity: str = "information", **_kwargs: object
Expand All @@ -2357,11 +2394,21 @@ def _capture(
screen = ThemeSelectorScreen(current_theme="langchain")
app.push_screen(screen)
await pilot.pause()

target_key = "langchain-light"
target_index = list(theme.get_registry()).index(target_key)
option_list = screen.query_one("#theme-options", OptionList)
option_list.highlighted = target_index
await pilot.pause()

await pilot.press("t")
await app.workers.wait_for_complete()
await pilot.pause()
await pilot.press("escape")
await pilot.pause()

assert any(sev == "error" and "OSError" in msg for sev, msg in notifications)
assert app.theme == "langchain"

async def test_t_write_skips_rerender_after_screen_unmount(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
Expand Down Expand Up @@ -2542,3 +2589,93 @@ async def test_n_toggles_between_labels_and_registry_keys(self) -> None:
str(option_list.get_option_at_index(other_index).prompt) == other_label
)
assert option_list.highlighted == other_index

async def test_escape_keeps_theme_set_for_terminal_this_session(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Esc keeps a theme set with `t` instead of reverting to the original.

Pressing `t` is a deliberate choice, so Esc should preserve it rather
than rolling back to the theme that was active when the picker opened.
"""
from textual.app import App
from textual.theme import Theme as TextualTheme
from textual.widgets import OptionList

from deepagents_code.widgets.theme_selector import ThemeSelectorScreen

config = tmp_path / "config.toml"
monkeypatch.setattr("deepagents_code.model_config.DEFAULT_CONFIG_PATH", config)
monkeypatch.setenv("TERM_PROGRAM", "Apple_Terminal")

results: list[str | None] = []

app = App()
async with app.run_test() as pilot:
_register_lc_theme(app)
c = theme.LIGHT_COLORS
app.register_theme(
TextualTheme(
name="langchain-light",
primary=c.primary,
secondary=c.secondary,
accent=c.accent,
foreground=c.foreground,
background=c.background,
surface=c.surface,
panel=c.panel,
warning=c.warning,
error=c.error,
success=c.success,
dark=False,
)
)

def on_result(result: str | None) -> None:
results.append(result)

screen = ThemeSelectorScreen(current_theme="langchain")
app.push_screen(screen, on_result)
await pilot.pause()

target_key = "langchain-light"
target_index = list(theme.get_registry()).index(target_key)

option_list = screen.query_one("#theme-options", OptionList)
option_list.highlighted = target_index
await pilot.pause()
await pilot.press("t")
await app.workers.wait_for_complete()
await pilot.pause()

await pilot.press("escape")
await pilot.pause()

assert app.theme == target_key
assert results == [None]

async def test_escape_restores_original_when_no_terminal_default_set(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Without a `t` press, Esc still reverts the previewed theme."""
from textual.app import App

from deepagents_code.widgets.theme_selector import ThemeSelectorScreen

config = tmp_path / "config.toml"
monkeypatch.setattr("deepagents_code.model_config.DEFAULT_CONFIG_PATH", config)
monkeypatch.setenv("TERM_PROGRAM", "Apple_Terminal")

app = App()
async with app.run_test() as pilot:
_register_lc_theme(app)
screen = ThemeSelectorScreen(current_theme="langchain")
app.push_screen(screen)
await pilot.pause()

await pilot.press("down")
await pilot.pause()
await pilot.press("escape")
await pilot.pause()

assert app.theme == "langchain"
Loading