Skip to content

A field's caret blinks only while Hush's window is active, minimised included - #304

Merged
pitis merged 3 commits into
mainfrom
perf/caret-blink-active
Nov 29, 2026
Merged

pitis merged 3 commits into
mainfrom
perf/caret-blink-active

Conversation

@pitis

@pitis pitis commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

A text field's caret now blinks only while Hush's window is active. When the window isn't active, gpui-base's blink is set to steady, so no timer runs and nothing redraws for it. When the window becomes active again, the blink comes back.

What changed

app::blink_while_active sets gpui-base's own caret policy, CursorBlink, through gpui-component's Theme::update: CursorBlink::default() (500 ms) while the window is active, CursorBlink::steady() otherwise. It runs once when the window opens (a window can open inactive) and on every change in the window's activation (Hush::observe). gpui-base's BlinkCursor watches the theme, so a focused field's blink stops right away, without waiting for a frame. On activation, gpui-base's own activation observer and on_focus start it again, now with the blinking setting. gpui-base isn't forked or patched.

The doc comment on drawables::let_go_soon and the name of one of its tests no longer say a caret blinks behind another app. Now it never does. On main it did only in the second case below, a field given the keys while the window was inactive.

Not what the issue expected

The issue's reading (from #296) was that the caret keeps redrawing twice a second whenever Hush is behind another app. That's not what happens on main, by test and by measurement. On the first frame an inactive window draws, GPUI sends a focus event with an empty focus path (Window::draw, current_window_active). gpui-base's on_blur then stops the blink. macOS draws that frame when another app comes to the front, and also when Hush is hidden (⌘H). The blink kept going only where no such frame came:

  • A minimised window draws no frame. The blink went on there: about 2 notifies a second, no frames drawn. On Windows and Linux, closing the window to the tray minimises it (running::hide), so a field that had the keys kept this up for as long as Hush sat in the tray.
  • A field given the keys while the window is inactive. gpui-base's focus starts the blink directly, and no focus event stops it. This happens with a chat opened from the tray or a notification where the system won't bring the window up (running::show: on Windows and Wayland it may only ask for attention). It also happens with a window that opens behind.

Both cases now stay quiet. The effect is small (see the numbers below), because the common case was already quiet.

How the caret looks while the window is inactive is unchanged: it isn't drawn. The issue asks for the caret "shown steady, as native fields do". gpui-base draws the caret only in an active window (show_cursor checks window.is_window_active()), and native fields do the same: AppKit draws no insertion point outside the key window, and Windows and GTK hide the caret when their window loses focus. Showing it would mean forking gpui-base, so it's left as it was. This needs the owner's call if a visible caret behind other apps is really wanted.

Measured

I measured with a release binary (cargo build --release -p hush-desktop --features snapshot) and HUSH_DATA_DIR set to a throwaway folder. The snapshot feature is only there for the demo data. I used a scratch harness that isn't committed: hush --live opens Demo::Chat on screen with the composer focused. It counts frames (Hush::render calls), the composer's notifies, and the process's CPU time (getrusage) over each phase, after one second to settle. The harness moves between phases itself: in front, behind another app (NSApp.deactivate), hidden (cx.hide()), minimised (minimize_window). The window was the default 1000×720 at 2×, on macOS. There were two short runs per build.

Phase main: frames/s, composer notifies/s, CPU this branch
In front (caret blinking) 2.0, 2.0, 0.5–1.4 % 2.0, 2.0, 0.5–1.4 %
Behind another app 0.0, 0.0, 0.01 % 0.0, 0.0, 0.02 %
Hidden (⌘H) 0.0, 0.0, 0.02 % 0.0, 0.0, 0.02 %
Minimised 0.0, 1.9, 0.03 % 0.0, 0.0, 0.01 %

When the window goes inactive, both builds draw exactly one frame (in the settling second), so the change adds no frame. One "after" run counted a single frame in a 10 s behind phase (0.1 frames/s). The second run didn't, and its settling second showed the same one frame as main, so I put it down to noise.

Tests

  • compose::window_tests::the_caret_blinks_only_while_the_window_is_in_front: the composer gets the keys while the window is behind, then the window is activated, deactivated and activated again. After the deactivate step it gives the composer the keys again while the window is behind. It counts the composer's notifies over 3 s in each state: 0 behind, at least 5 in front. It failed before the change at the first step ("given the keys behind, it doesn't blink": 6 notifies). The "given the keys behind again" step guards the steady setting on deactivation: with the setting applied only on activation, that step fails (6 notifies). The deactivate step on its own passed on main as well: GPUI's test windows always draw a frame, so a minimised window can't be reproduced in a test (the test platform can't minimise either). The minimised case is covered by the measurement above.
  • Gate: make check passes on 526f8f1 (origin/main merged in): cargo fmt --check, both clippy -D warnings runs, and cargo test --features hush-desktop/snapshot (404 + 553 passed, 4 ignored).
  • Snapshots: I rendered all 92 demo screens with both builds (--snapshot). The only differences are the demo's clock times, because the runs were a minute apart. I looked at chat-light from both builds: same layout and styling. Snapshot windows are inactive, so neither build ever drew a caret there.

Closes #297

gpui-base stops a field's blink on the first frame an inactive window
draws, but a minimised window draws none, so the blink went on there
(twice a second, nothing drawn), and a field given the keys while the
window was inactive blinked behind. The window's activation now sets
gpui-base's blink policy: blinking while active, steady otherwise.
The test gives the composer the keys again while the window is behind,
which blinks unless deactivation set the steady setting. The drawables
test's name no longer says a caret blinks behind another app, and the
setting's comment says who stops the blink and that it is the app's.
@pitis

pitis commented Nov 29, 2026

Copy link
Copy Markdown
Owner Author

Review (pr-review-toolkit: code, tests, comments) and what was done

There were no Critical findings. Fixes are in a794660; origin/main is merged in (526f8f1).

Important

  1. Tests: nothing guarded the steady setting on deactivation. This is the half that fixes a minimised window. The test's deactivate step passes on main too, because test windows draw a frame and GPUI's blur stops the blink. Fixed: after the deactivate step, the test gives the composer the keys again while the window is behind. The keys are still there, so no focus event is sent, and gpui-base starts the blink under whatever setting is in place. I checked it: with the setting applied only on activation, this step fails (6 notifies, expected 0).
  2. Comments: a drawables test name said a caret blinks behind another app (a_window_another_app_is_in_front_of_lets_go_though_its_caret_blinks). Fixed: it is now ..._lets_go_though_it_keeps_drawing, matching the doc comment this PR already corrected.
  3. Comments: the PR body said a caret blinked behind another app "neither before nor after". That was wrong for a field given the keys while the window was inactive, which did blink on main. Fixed in the PR body.

Suggestions taken

  • The blink_while_active comment now says who does what: GPUI blurs the field on the inactive window's first frame, and that stops gpui-base's blink. "Starts blinking" became "would blink on", and the comment notes that the setting is the app's and follows this window, since Hush has one window.
  • In the test, "redraws" became "notifies" (that's what it counts). The test asserts that the window starts inactive, and the garbled comment sentence is rewritten.
  • PR body: on activation the blink restarts through gpui-base's own activation observer and on_focus, not only through the theme.

Declined

  • Assert Theme::global(cx).cursor_blink.is_steady() after deactivating (code review, suggestion). The behavioural step in Important 1 guards the same thing without testing the setting's field.

Noted, not changed

  • With the screen locked, the window stays active, so the caret keeps blinking there, as before. The issue doesn't list this case.
  • The issue asks for the caret "shown steady" behind other apps. gpui-base draws no caret in an inactive window, as native fields do, and that is unchanged. Showing one would need a gpui-base change, so it's the owner's call.

Gate on 526f8f1 (origin/main merged in): make check passes: cargo fmt --check, both clippy -D warnings runs, cargo test --features hush-desktop/snapshot (404 + 553 passed, 4 ignored).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The composer's caret blinks only while Hush is the active app

1 participant