Repository navigation
[CTX-1808] feat(platform): per-window focus tracking and graceful per-window close (bitty#1765 slice 3) - #3138
Conversation
…-window close bitty#1765 slice 3. The window registry becomes the single owner of the focused window and of what one close event means for the instance. Per-window focus (A): - `WindowRegistry::focused` is a separate state from event attribution: `active` follows the last handled event, focus moves only on `Focused` edges, so a background window's key press, resize or occlusion edge can never claim focus. - `focus_edge` keeps that rule in one place instead of re-deriving it in the event handler; the app forwards every window event through it. - focus is validated by identity: `focus`/`blur` refuse an identity this process never registered, and `blur` only clears when it names the window currently focused, so a late blur from the previously focused window cannot clear a newer focus. - creation and destruction never leave a focus hole: a new row takes focus only when nothing was focused, and removing the focused window hands focus to a surviving row (same deterministic rule as attribution). - `TerminalApp::focused_window()` is the target a window-scoped command resolves to (focused, else the first registered, else the single-window identity), so the single-window case is unchanged. Graceful per-window close (B): - `WindowRegistry::close_decision` states the consequence of one close: `WindowOnly` while another window survives, `LastWindow` for the last row, `Unknown` for an identity the registry does not own (headless run). - a secondary window's `Closed` event now runs the same bookkeeping as `CloseRequested` (release that window's surface, drop its row, clear a deferred attach), so a destroyed window no longer leaves a stale row. - closing a secondary window releases only its surface and row; focus and attribution re-home to a survivor and the instance keeps running. - closing the last window still takes the existing save/shutdown/exit path. - the first window of a multi-window instance refuses to close: promoting a survivor to the primary role needs a runtime surface seam that does not exist yet, so the request is declined loudly instead of terminating an instance the user still has a window in (see the PR for the exact remainder).
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 29 minutes. View limit details
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Xuepoo
left a comment
There was a problem hiding this comment.
Independent review (CTX-1808, commit baf3a416): NEEDS-FIX
Verified (passing)
- Per-window focus tracking (
crates/bitty-terminal/src/window_registry.rs,terminal_app.rs):focus_edgerestricts focus changes toFocused(true)/Focused(false);focus/blurreject unknown IDs and prevent stale blurs from clearing newer focus;insertonly sets focus whenself.focused.is_none();removedeterministically re-homes focus to the first survivor. - Graceful per-window close:
CloseDecision(Unknown,WindowOnly,LastWindow) cleanly separates pre-registry exit, multi-window close, and last-window exit. SecondaryCloseRequestedandClosedshareis_window_closeteardown (release_window_surface,windows.remove,deferred_window_surfaces.retain) without mutating survivor rows/surfaces. PrimaryCloseRequestedwithWindowOnlydeclines loudly while survivor promotion is unimplemented. - Single-window invariance & scope:
TerminalApp::focused_window()preservesself.window.idfallback; 3-file diff, no hardcoded host paths/secrets, no unrelated edits. - Local checks:
cargo fmt --check: passcargo clippy -p bitty-terminal --offline -- -D warnings: passcargo test -p bitty-terminal --offline --bin bitty -- window_registry focus_sensitive: 18 passed, 0 failedcargo test -p bitty-terminal --offline --features input-vm-test-support --bin bitty -- window_registry focus_sensitive: 21 passed, 0 failed
Blocking finding
just doc-check/ CIQuality gates (1)(Check rustdoc) failure:
RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --lockedfails with 3rustdoc::broken-intra-doc-linkserrors incrates/bitty-terminal/src/window_registry.rs:- Line 33:
[Self::active] - Line 35:
[Self::focused] - Line 43:
[Self::close_decision]
Selfis not in scope in module-level//!documentation; replace those links with[WindowRegistry::active],[WindowRegistry::focused], and[WindowRegistry::close_decision], then re-runjust doc-check.
- Line 33:
Non-blocking PR hygiene
- PR #3138 currently has
milestone: null(missingv0.1.0) and lacks thePriority: ... | Area: ... | Labels: ... | Milestone: ... | Task: CTX-1808body line required bybitty/AGENTS.md.
Xuepoo
left a comment
There was a problem hiding this comment.
Independent re-review (CTX-1808, commit 2b02d310): APPROVE
- Verified
crates/bitty-terminal/src/window_registry.rsqualifiesWindowRegistry::active,WindowRegistry::focused, andWindowRegistry::close_decisionin the module-level documentation. - Verified
just doc-check(RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --locked) exits0with zero warnings/errors. - Verified
cargo test -p bitty-terminal --offline --bin bitty -- window_registrypasses (17 passed, 0 failed). - Verified PR #3138 carries milestone
v0.1.0, labelsfeat,P0,area:platform, and the requiredPriority: P0 | Area: platform | ... | Task: CTX-1808metadata line.
Priority: P0 | Area: platform | Labels: feat, P0, area:platform | Milestone: v0.1.0 | RFC: OQ-none | Task: CTX-1808
Related #1765
Summary
Slice 3 of bitty#1765. Slices 1 and 2 gave the process a window registry
(#3133) and per-window presentation surfaces with second-window paint fan-out
(#3134). This slice makes the registry the single owner of which window is
focused and of what one close event means for the instance, and wires
both into the event path without a second registry and without touching the
single-window behaviour.
No new window machinery, no new platform seam, no new crate edge. Three files,
one commit, headless only.
Per-window state model
Attribution and focus are two states, not one:
active(slice 1)focused(this slice)Focused(true)/Focused(false)onlyfocus_edge(kind)is the one place that decides which events count as focus;a key press, a resize, an occlusion edge or a close request never moves focus.
focus(id)/blur(id)validate the identity first. A focus edge for anidentity this process never admitted changes nothing, and
blurclears onlywhen it names the window currently focused — focus edges cross in flight (the
platform reports the new window focused before the old one blurred), so a late
blur cannot clear a newer focus.
nothing was focused (there is no platform seam to ask for focus, so creation
never steals it), and removing the focused window hands focus to a surviving
row — the same deterministic first-survivor rule slice 1 uses for
active.TerminalApp::focused_window()is the target a window-scoped command resolvesto: focused, else the first registered window, else the single-window identity.
With one window this is byte-identical to today (
self.window.id).Close is one decision, answered by the registry:
CloseDecisionUnknownWindowOnlyLastWindowPer-window close bookkeeping:
Closedevent now runs the same teardown asCloseRequested(release its surface, drop its row, clear a deferred attach),so a window the platform destroyed no longer leaves a stale registry row;
Headless tests
Registry (
crates/bitty-terminal/src/window_registry.rs, constructible windowpayload — a
WindowHandlecannot exist without a display server, which is whythe row semantics live here):
only_focus_events_move_window_focus— the focus/attribution split, enumeratedover resize, scale, redraw, cursor-enter/leave, close, closed, occlusion.
a_registered_window_starts_focused_only_when_nothing_was_focused— creationdoes not steal focus; a registry with no focus takes the new window.
focus_edges_move_focus_and_unknown_identities_are_refuseda_stale_blur_from_the_previously_focused_window_cannot_clear_a_newer_focusclosing_the_focused_window_rehomes_focus_to_a_survivor— no focus hole, nodangling identity, survivor surface state intact, survivor becomes
LastWindow.closing_an_unfocused_window_leaves_focus_on_its_windowclosing_the_last_window_leaves_no_focus_and_no_registry_rowclose_decision_separates_window_only_last_window_and_unknownApp (
crates/bitty-terminal/src/tests.rs):a_focus_sensitive_command_targets_the_only_window_of_a_single_window_instance— the single-window target is unchanged and resolving a target never fabricates
a row.
window_registry_command::a_close_request_for_an_unregistered_window_still_takes_the_exit_path(feature
input-vm-test-support, driven through the realAppHandlerwith therecording context) — a close the registry does not own still exits the instance
and leaves no row or focus behind.
Gates
cargo fmt --checkcargo clippy --workspace --all-targets --offline -- -D warningscargo test -p bitty-terminal --offline --bin bittycargo test -p bitty-terminal --offline --features input-vm-test-support --bin bitty -- window_registry focus_sensitivecargo +1.85 check --workspace --tests --offline(MSRV, no let-chains)RUSTFLAGS='-D warnings' cargo check -p bitty-terminal --tests --target x86_64-pc-windows-gnu --offlinerustfmt +stable --edition 2024 --checkon the three touched filesMutation evidence (each hunk mutated in a throwaway copy outside the worktree,
with its own
CARGO_TARGET_DIR, then restored — the lane worktree was nevermutated):
removeclosing_the_focused_window_rehomes_focus_to_a_survivor,closing_the_last_window_leaves_no_focus_and_no_registry_rowblurclear unconditionallya_stale_blur_from_the_previously_focused_window_cannot_clear_a_newer_focus,focus_edges_move_focus_and_unknown_identities_are_refusedfocus_edgeanswerSome(true)for every eventonly_focus_events_move_window_focusclose_decisionalways answerLastWindowclose_decision_separates_window_only_last_window_and_unknownPre-existing, not introduced here:
cargo clippy -p bitty-terminal --all-targets --features input-vm-test-support -D warningsreports fourdrop_non_droperrors at
tests.rs:1372/1391/1414/1433onorigin/mainas well. That featurecombination is not a repository gate (
just check-windowruns nextest for thosetests), so this PR leaves it alone rather than widen scope.
Reference behaviour
A new window is a peer of the focused one and focus follows the compositor's
Focusededges (ghostty/kitty/wezterm default: the platform focuses the windowit created). Deliberate deviation: Bitty has no
focus_windowplatform seam, sothe registry never claims a window is focused on creation — it only records the
edge the OS sends.
Xon a secondary window closes only that window, askitty @ close-window/ a wezterm per-window close does.What remains in #1765
is already
WindowOnlyfor it, but the app cannot complete it: the primaryswap chain, presenter and GPU context are process-wide, so promoting a
survivor to the primary role needs a
bitty-runtimeseam (release the primarysurface without dropping the per-window fleet, then rebuild the primary
surface for the survivor on the existing device) that does not exist yet.
Until it lands the request is declined loudly (
warning:+ stderr linenaming the window) instead of terminating an instance the user still has a
window in — deliberately not a half-wired teardown.
Closedfrom the primary while survivors exist still takes the exit path:it cannot be declined (the window is already gone) and there is no survivor
promotion yet. It is unreachable from the graceful flow because (1) declines
the request first.
core.window.liststill reports a single hardcoded window(
{"windows":[{"id":"w:1"}]}inctl/apply.rs). Threading registry stateinto
ctl::applyis the command/CLI half of the issue's "Actions & CLI"bullet (launching new windows in a running instance), not this slice.
process-global, so a window's close has only its presentation surface and its
registry row to tear down. When per-window layout lands, that teardown joins
the
WindowOnlypath.WindowHandle::focus) if background window creation isever added; today focus is only ever reported, never requested.
contained and dropped (slice 1 behaviour): the layout tree, PTY and IME
session are process-global, so typing in a second window goes nowhere yet.
This slice records which window is focused; routing input to the focused
window needs the per-window layout ownership the same promotion seam unlocks.
Summary by CodeRabbit