From ea84d7f9ddb0e6d186fc47f70f028fba0e9778ae Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Sat, 21 Nov 2026 00:35:15 -0500 Subject: [PATCH] A notification with no sound can flash the title Co-Authored-By: Claude Opus 5.5 --- meshterm/ui/menu.py | 5 ++-- meshterm/ui/tui/frame.py | 56 ++++++++++++++++++++++++++++++++++-- meshterm/ui/tui/session.py | 16 +++++++++++ tests/test_sound_controls.py | 34 ++++++++++++++++++++++ 4 files changed, 107 insertions(+), 4 deletions(-) diff --git a/meshterm/ui/menu.py b/meshterm/ui/menu.py index 584336b..87189ef 100644 --- a/meshterm/ui/menu.py +++ b/meshterm/ui/menu.py @@ -483,9 +483,10 @@ async def run_menu(ctx: AppContext) -> None: session.set_quit_confirm(lambda: _confirm_quit(ctx, session)) # What a held Esc engages while its dialog is open. The app keeps it while it exits. session.set_quiesce(lambda: _off_the_air(ctx)) - # ^S mutes or unmutes the sounds from any screen. The bell theme rings the terminal. + # ^S mutes or unmutes the sounds from any screen. The bell theme rings the terminal, and + # a notification with no sound can flash the title. session.set_sound_toggle(lambda: _toggle_sound(ctx)) - ctx.sound.set_hooks(bell=session.bell) + ctx.sound.set_hooks(bell=session.bell, flash=session.flash) # Where a terminal gives the keys, MeshTerm asks the keyboard whether Esc is still # held. Thus a held Esc quits there too. The emulator reads its keys itself, and the # keyboard of its machine is not the keyboard of the handheld. diff --git a/meshterm/ui/tui/frame.py b/meshterm/ui/tui/frame.py index f92070c..a4f95f8 100644 --- a/meshterm/ui/tui/frame.py +++ b/meshterm/ui/tui/frame.py @@ -10,6 +10,7 @@ past it. from __future__ import annotations import re +import time from collections import OrderedDict from collections.abc import Callable, Sequence @@ -180,6 +181,44 @@ def _breathing_room(body_len: int, budget: int) -> int: return 1 if body_len + 2 <= budget else 0 +#: How long the title flashes for a notification, in seconds (refer to :func:`flash_title`). +FLASH_S = 0.3 + +#: The monotonic time until which the title flashes. +_flash_until = 0.0 + + +def flash_title(now: float | None = None) -> None: + """Flash the title of the frame for :data:`FLASH_S` seconds: the notification flash. + + The title shows in reverse video until the time ends. The caller paints at once, and + again when the time ends (refer to :meth:`~meshterm.ui.tui.session.TuiSession.flash`). + The flash is the way to see a notification where no sound plays: on the PicoCalc, with + the sounds muted, or with the "no sound" theme (refer to :mod:`meshterm.services.sound`). + + Args: + now: The monotonic time now. Tests give it. + """ + global _flash_until + _flash_until = (time.monotonic() if now is None else now) + FLASH_S + + +def flashing(now: float | None = None) -> bool: + """Whether the title flashes now.""" + return (time.monotonic() if now is None else now) < _flash_until + + +def _title_markup(title: str, style: str) -> str: + """The markup of a panel title, in reverse video while it flashes. + + The reverse is a markup tag of its own inside the style tag. Rich does not resolve a + theme name and an attribute in one style string (``reverse title.border``). + """ + if flashing(): + return f"[{style}][reverse]{title}[/reverse][/]" + return f"[{style}]{title}[/]" + + def _panel_box( title: str, visible: list[str], @@ -223,7 +262,7 @@ def _panel_box( subtitle = f"[{hint_style(border)}]{' ยท '.join(atoms)}[/]" if atoms else None return Panel( body, - title=f"[{title_style(border)}]{title}[/]" if title else None, + title=_title_markup(title, title_style(border)) if title else None, subtitle=subtitle, subtitle_align="right" if caption else "center", border_style=border, @@ -367,6 +406,8 @@ def _title_bar( break rule_span = max(0, cols - head_span - tail_span - stat_span) heading = screen.bar_title(title, title_style(border)) if title else Text() + if title and flashing(): + heading.stylize("reverse") if status and label_w > rule_span - 2: # One rule cell on each side, and the remaining cells for the title. The status stays. heading.truncate(max(1, rule_span - 4), overflow="ellipsis") @@ -472,7 +513,18 @@ def compose_base( # Rich sets the padded title into the inner ``cols - 4`` cells of the rule. title = fitted_title(base, cols - 4 - 2) flush = base.flush - key = (cols, rows, title, caption, flush, footer_hint, more_above, more_below, *visible) + key = ( + cols, + rows, + title, + flashing(), + caption, + flush, + footer_hint, + more_above, + more_below, + *visible, + ) if footer_lane is None and _BASE_BOX_CACHE is not None and _BASE_BOX_CACHE[0] == key: body = _BASE_BOX_CACHE[1] else: diff --git a/meshterm/ui/tui/session.py b/meshterm/ui/tui/session.py index 71c914a..978184d 100644 --- a/meshterm/ui/tui/session.py +++ b/meshterm/ui/tui/session.py @@ -836,6 +836,22 @@ class TuiSession: if output is not None: output.bell() + def flash(self) -> None: + """Flash the title of the frame, for a notification that plays no sound. + + The title shows in reverse video for :data:`~meshterm.ui.tui.frame.FLASH_S`. The + session paints now, and again when the time ends, so that the title comes back. + """ + from . import frame + + frame.flash_title() + self.invalidate() + try: + loop = asyncio.get_running_loop() + except RuntimeError: + return + loop.call_later(frame.FLASH_S + 0.02, self.invalidate) + def set_esc_probe(self, down: Callable[[], bool] | None) -> None: """Declare how to ask whether the Esc key is physically down, for the Esc of a terminal. diff --git a/tests/test_sound_controls.py b/tests/test_sound_controls.py index de11e77..e5007e4 100644 --- a/tests/test_sound_controls.py +++ b/tests/test_sound_controls.py @@ -3,6 +3,7 @@ from __future__ import annotations +import re from collections.abc import Iterator from types import SimpleNamespace @@ -116,3 +117,36 @@ def test_ctrl_s_is_the_sound_chord_and_ctrl_e_reveals() -> None: """^S turns the sounds on or off, and ^E shows a concealed value (JP, 2026-11-20).""" assert _CTRL_LETTER_CHORDS["s"] == "sound" assert _CTRL_LETTER_CHORDS["e"] == "reveal" + + +def test_the_title_flashes_in_reverse_video_for_a_short_time() -> None: + """The flash draws the title in reverse video, and the title comes back after FLASH_S.""" + from meshterm.ui.tui import frame + + frame.flash_title(now=100.0) + assert frame.flashing(now=100.1) + assert not frame.flashing(now=100.0 + frame.FLASH_S + 0.01) + frame.flash_title(now=0.0) # long ago: the next test sees no flash + assert frame._title_markup("Nodes", "title.border") == "[title.border]Nodes[/]" + + +def test_the_flash_reaches_the_drawn_title(regular: None) -> None: + """While the flash lasts, the composed frame carries the reverse attribute (SGR 7).""" + import time + + from rich.text import Text + + from meshterm.ui.tui import frame + from meshterm.ui.tui.screen import ScrollScreen + + screen = ScrollScreen(Text("body"), title="Nodes", floating=False) + plain = frame.compose_base(Text(""), screen, screen.footer_hint, 72, 24) + frame.flash_title(now=time.monotonic()) + try: + lit = frame.compose_base(Text(""), screen, screen.footer_hint, 72, 24) + finally: + frame.flash_title(now=0.0) + first = lit.split("\n")[0] + codes = [c.split(";") for c in re.findall(r"\x1b\[([0-9;]*)m", first)] + assert any("7" in c for c in codes) + assert plain.split("\n")[0] != first