From 415844ef5d817f961590fab5e47a524940e8b6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Aug 2026 19:49:56 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(ui):=20keyboard=20focus=20v1?= =?UTF-8?q?=20=E2=80=94=20Tab=20cycles=20dock/bench/clip,=201px=20STROKE?= =?UTF-8?q?=5FFOCUS=20ring,=20all=20other=20keys=20bubble=20to=20the=20roo?= =?UTF-8?q?t=20keymap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/app/src/main.rs | 9 ++++ crates/app/src/tests/view.rs | 42 ++++++++++++++++ crates/app/src/ui/dock_stance.rs | 44 +++++++++++++++-- crates/app/src/ui/stance.rs | 82 ++++++++++++++++++++++++++++++-- 4 files changed, 170 insertions(+), 7 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index e6c458c..7169851 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -769,6 +769,12 @@ struct Player { /// Wall clock of the first displayed frame -- the real-speed measurement. started: Option, focus: FocusHandle, + /// The three focus-ring surfaces (`ui::stance::Surface`) Tab/Shift-Tab + /// cycles between once one of them holds focus -- `focus` above stays + /// the fallback none of these keys ever leave. + focus_dock: FocusHandle, + focus_bench: FocusHandle, + focus_inspector: FocusHandle, } #[cfg(test)] @@ -1050,6 +1056,9 @@ fn main() { dropped: 0, started: None, focus: cx.focus_handle(), + focus_dock: cx.focus_handle(), + focus_bench: cx.focus_handle(), + focus_inspector: cx.focus_handle(), }); // Nothing else takes focus, and without it the key listener // above is never reached. diff --git a/crates/app/src/tests/view.rs b/crates/app/src/tests/view.rs index d34f3e4..13d1826 100644 --- a/crates/app/src/tests/view.rs +++ b/crates/app/src/tests/view.rs @@ -891,3 +891,45 @@ fn the_zoom_button_says_how_much_is_on_the_bed() { assert_eq!(secs_label(1. / 60.), "0.02s"); assert_eq!(secs_label(4.5), "4.5s"); } + +/// Keyboard focus v1 (`ui::stance::{Surface, next_surface, is_focus_cycle_key}`): +/// Tab walks dock -> bench -> inspector -> dock, wrapping; Shift-Tab walks +/// it backward. No gpui window harness exists in this crate to drive a real +/// `KeyDownEvent` through -- this exercises the exact pure function every +/// surface's `on_key_down` calls, which is where the cycle order actually +/// lives. +#[test] +fn tab_cycles_dock_bench_inspector_and_wraps() { + use crate::ui::stance::{Surface, next_surface}; + + assert_eq!(next_surface(Surface::Dock, false), Surface::Bench); + assert_eq!(next_surface(Surface::Bench, false), Surface::Inspector); + assert_eq!(next_surface(Surface::Inspector, false), Surface::Dock); + + // Shift-Tab is the exact reverse at every step. + assert_eq!(next_surface(Surface::Dock, true), Surface::Inspector); + assert_eq!(next_surface(Surface::Inspector, true), Surface::Bench); + assert_eq!(next_surface(Surface::Bench, true), Surface::Dock); +} + +/// The root fallback guarantee (main.rs:1054's "every keybind hangs off the +/// root FocusHandle"): a surface's own `on_key_down` answers Tab/Shift-Tab +/// and nothing else -- `cx.stop_propagation()` is only ever reached from +/// that one branch (`ui::stance::is_focus_cycle_key`), so every other key, +/// still-pressed while a surface is focused, is left un-stopped and bubbles +/// up the dispatch tree to the root's own `on_key_down` +/// (gpui-0.2.2 `window.rs`'s `dispatch_key_down_up_event`, `Bubble` phase, +/// walks the focused node's ancestors and stops only at +/// `cx.stop_propagation()` -- verified by reading that function, not +/// assumed). This asserts the gate itself: only "tab" is ever intercepted, +/// so a keybind like space (play/pause) or "s" (split) is provably never +/// swallowed by a focused surface. +#[test] +fn only_tab_is_intercepted_by_a_focused_surface_everything_else_reaches_root() { + use crate::ui::stance::is_focus_cycle_key; + + assert!(is_focus_cycle_key("tab")); + for key in ["space", "escape", "s", "c", "left", "right", "enter", "delete", "?"] { + assert!(!is_focus_cycle_key(key), "{key} must bubble to the root"); + } +} diff --git a/crates/app/src/ui/dock_stance.rs b/crates/app/src/ui/dock_stance.rs index 4c00277..1537494 100644 --- a/crates/app/src/ui/dock_stance.rs +++ b/crates/app/src/ui/dock_stance.rs @@ -12,6 +12,7 @@ //! [`clip_middle`]...), only the anatomy around them is new. use crate::ui::hitmap; +use crate::ui::stance::{Surface, is_focus_cycle_key, next_surface}; use crate::ui::type_scale::{self, Typeset, head, label, mono}; use crate::*; use gpui::FontWeight; @@ -817,7 +818,12 @@ fn subtitle_palette(player: &Player, cx: &mut Context) -> Option) -> impl IntoElement { +fn sources_tab( + player: &Player, + window: &mut Window, + cx: &mut Context, +) -> impl IntoElement { + let focused = player.focus_dock.is_focused(window); let sources = player .session .as_ref() @@ -862,6 +868,11 @@ fn sources_tab(player: &Player, cx: &mut Context) -> impl IntoElement { let filter_text: SharedString = player.dock_filter.clone().into(); div() .id("dock-sources") + .track_focus(&player.focus_dock) + .on_key_down(cx.listener(move |this, event: &KeyDownEvent, window, cx| { + cycle_on_key_down(Surface::Dock)(this, event, window, cx) + })) + .when(focused, |d| d.border_1().border_color(rgb(STROKE_FOCUS()))) .flex_1() .min_h(px(0.)) .flex() @@ -1077,8 +1088,10 @@ fn clip_tab( player: &Player, width: f32, window_size: Size, + window: &mut Window, cx: &mut Context, ) -> impl IntoElement { + let focused = player.focus_inspector.is_focused(window); // The room actually given here, not the window's: `eq_card_w`/ // `card_max_w` are asked "how wide may I draw" and answered with the // *whole viewport's* width when handed `window_size` verbatim -- but this @@ -1099,6 +1112,11 @@ fn clip_tab( && !player.subtitle_style_open; div() .id("dock-clip") + .track_focus(&player.focus_inspector) + .on_key_down(cx.listener(move |this, event: &KeyDownEvent, window, cx| { + cycle_on_key_down(Surface::Inspector)(this, event, window, cx) + })) + .when(focused, |d| d.border_1().border_color(rgb(STROKE_FOCUS()))) .flex_1() .min_h(px(0.)) .flex() @@ -1257,6 +1275,7 @@ pub(crate) fn render( player: &Player, width: f32, window_size: Size, + window: &mut Window, cx: &mut Context, ) -> impl IntoElement { let src_active = player.dock_src_active; @@ -1279,7 +1298,26 @@ pub(crate) fn render( .child(dock_tab("dock-tab-clip", "CLIP", !src_active, cx)), ) .child(match src_active { - true => sources_tab(player, cx).into_any_element(), - false => clip_tab(player, width, window_size, cx).into_any_element(), + true => sources_tab(player, window, cx).into_any_element(), + false => clip_tab(player, width, window_size, window, cx).into_any_element(), }) } + +/// The Tab/Shift-Tab handler shared by the dock's two tabs (`sources_tab`'s +/// "dock/library" and `clip_tab`'s "inspector") -- only [`is_focus_cycle_key`] +/// is ever answered here; every other key is left un-stopped so it bubbles +/// to the room's root handler (`ui::stance::render`'s `on_key_down`), which +/// is the one every other keybind, Tab included when nothing in the ring +/// has focus yet, still hangs off. +fn cycle_on_key_down( + surface: Surface, +) -> impl Fn(&mut Player, &KeyDownEvent, &mut Window, &mut Context) + 'static { + move |this, event, window, cx| { + if is_focus_cycle_key(event.keystroke.key.as_str()) { + let next = next_surface(surface, event.keystroke.modifiers.shift); + window.focus(this.focus_handle(next)); + cx.stop_propagation(); + cx.notify(); + } + } +} diff --git a/crates/app/src/ui/stance.rs b/crates/app/src/ui/stance.rs index f509be9..40afdd6 100644 --- a/crates/app/src/ui/stance.rs +++ b/crates/app/src/ui/stance.rs @@ -17,6 +17,58 @@ use crate::ui::timeband_stance; use crate::ui::type_scale::{self, Typeset}; use crate::*; +/// Keyboard focus v1: the three regions Tab/Shift-Tab cycle a painted ring +/// through -- the bench, the dock's Sources tab (library) and the dock's +/// Clip tab (inspector), the darkroom's own stand-ins for "timeline", +/// "library" and "inspector" (there is no separate inspector *region* in +/// this tree -- MOCK-SPEC's Clip tab, `dock_stance::clip_tab`, is it). +/// `Player::focus` (the root handle every keybind hangs off) is not a +/// member: Tab already means "select the clip under the playhead" +/// (`ActionId::Select`, `keymap.rs`) at the root, unchanged here, and only +/// starts meaning "enter the ring" once one of these three already holds +/// focus. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum Surface { + Dock, + Bench, + Inspector, +} + +/// Fixed cycle order: dock, bench, inspector. +const SURFACE_CYCLE: [Surface; 3] = [Surface::Dock, Surface::Bench, Surface::Inspector]; + +/// Tab (`backward` false) or Shift-Tab (`backward` true) from `current`, +/// wrapping at either end of [`SURFACE_CYCLE`]. +pub(crate) fn next_surface(current: Surface, backward: bool) -> Surface { + let i = SURFACE_CYCLE.iter().position(|&s| s == current).unwrap(); + let n = SURFACE_CYCLE.len(); + SURFACE_CYCLE[if backward { (i + n - 1) % n } else { (i + 1) % n }] +} + +/// The one key a surface's own `on_key_down` answers. Everything else is +/// left alone on purpose, unhandled and un-stopped, so it bubbles to the +/// root's fallback handler exactly as an unfocused key already does +/// (`window.rs`'s key dispatch walks the focused node's ancestors in +/// `Bubble` phase and stops only at `cx.stop_propagation()` -- the three +/// surface handlers call that solely from this branch, gpui-0.2.2 +/// `window.rs:3897-3906`). +pub(crate) fn is_focus_cycle_key(key: &str) -> bool { + key == "tab" +} + +impl Player { + /// The `FocusHandle` a [`Surface`] paints its ring on and Tab/Shift-Tab + /// moves to next -- one door, so `next_surface`'s answer and the handle + /// `window.focus` is given cannot name two different surfaces. + pub(crate) fn focus_handle(&self, surface: Surface) -> &FocusHandle { + match surface { + Surface::Dock => &self.focus_dock, + Surface::Bench => &self.focus_bench, + Surface::Inspector => &self.focus_inspector, + } + } +} + /// Left rail, full height (DESIGN §5). pub(crate) const SPINE_W: f32 = 56.; /// Fixed strip under the screen: timecode, transport, cut readout, contact @@ -533,9 +585,24 @@ fn time_band(player: &mut Player, position: f64, cx: &mut Context) -> im /// Height comes from [`Player::split_px`] now, not the fixed [`BENCH_H`]: /// the seam [`crate::ui::stance::render`] mounts above this region is what /// answers "ui fields are not stretchable" for the bench. -fn bench(player: &mut Player, bench_h: f32, cx: &mut Context) -> impl IntoElement { +fn bench( + player: &mut Player, + bench_h: f32, + window: &mut Window, + cx: &mut Context, +) -> impl IntoElement { + let focused = player.focus_bench.is_focused(window); div() .id("stance-bench") + .track_focus(&player.focus_bench) + .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| { + if is_focus_cycle_key(event.keystroke.key.as_str()) { + let next = next_surface(Surface::Bench, event.keystroke.modifiers.shift); + window.focus(this.focus_handle(next)); + cx.stop_propagation(); + cx.notify(); + } + })) .flex_none() .h(px(bench_h)) // §4: lanes take 0 radius. @@ -543,6 +610,12 @@ fn bench(player: &mut Player, bench_h: f32, cx: &mut Context) -> impl In .bg(rgb(DARK_CANVAS())) .border_t_1() .border_color(rgba(DARK_SEAM())) + // The ring (DESIGN §4's "1px, lamp-adjacent" convention -- the same + // `.border_1().border_color(...)` a picked bench clip already uses): + // painted on this outer frame, never on a row, and only while this + // surface itself -- not a picked clip inside it -- holds the focus + // ring Tab/Shift-Tab moves ([`next_surface`]). + .when(focused, |d| d.border_1().border_color(rgb(STROKE_FOCUS()))) .flex() .flex_col() .px(px(12.)) @@ -739,6 +812,7 @@ fn dock( player: &Player, dock_w: f32, window_size: Size, + window: &mut Window, cx: &mut Context, ) -> impl IntoElement { div() @@ -751,7 +825,7 @@ fn dock( .border_color(rgba(DARK_SEAM())) .flex() .flex_col() - .child(dock_stance::render(player, dock_w, window_size, cx)) + .child(dock_stance::render(player, dock_w, window_size, window, cx)) } /// A zero-size element whose only job is `window.on_mouse_event`'s door: @@ -982,7 +1056,7 @@ pub(crate) fn render( .child(screen(player, position, window, cx)) .child(time_band(player, position, cx)) .child(divider(Split::Bench, cx)) - .child(bench(player, bench_h, cx)) + .child(bench(player, bench_h, window, cx)) .child(ledger(player, position)) .when_some(player.notices.back().cloned(), |el, n| { el.child(notice_plate(n, bench_h)) @@ -1033,7 +1107,7 @@ pub(crate) fn render( }), ) .child(divider(Split::Dock, cx)) - .child(dock(player, dock_w, window_size, cx)) + .child(dock(player, dock_w, window_size, window, cx)) // The library row menu mounts on the ROOT, not inside // `stance-centre` like the clip menu beside it: it is opened from a // dock row, so its anchor is a window x past the centre column's own