forge
Explore
Log in

Don't you dare go Hollow

Keyboard focus v1 — Tab cycles dock/bench/clip, 1px STROKE_FOCUS ring, all other keys bubble to the root keymap

Browse files at this commit

415844ef5d817f961590fab5e47a524940e8b6e2Ahmet Kaan GümüşParent: 59c625d
  • feat
    • keyboard focus v1 — Tab cycles dock/bench/clip, 1px STROKE_FOCUS ring, all other keys bubble to the root keymap
Show full message
✨ feat(ui): keyboard focus v1 — Tab cycles dock/bench/clip, 1px STROKE_FOCUS ring, all other keys bubble to the root keymap
crates/app/src/main.rsmodified+9 -0
@@ -769,6 +769,12 @@ struct Player {
769 /// Wall clock of the first displayed frame -- the real-speed measurement.769 /// Wall clock of the first displayed frame -- the real-speed measurement.
770 started: Option<Instant>,770 started: Option<Instant>,
771 focus: FocusHandle,771 focus: FocusHandle,
772+ /// The three focus-ring surfaces (`ui::stance::Surface`) Tab/Shift-Tab
773+ /// cycles between once one of them holds focus -- `focus` above stays
774+ /// the fallback none of these keys ever leave.
775+ focus_dock: FocusHandle,
776+ focus_bench: FocusHandle,
777+ focus_inspector: FocusHandle,
772 }778 }
773 779
774 #[cfg(test)]780 #[cfg(test)]
@@ -1050,6 +1056,9 @@ fn main() {
1050 dropped: 0,1056 dropped: 0,
1051 started: None,1057 started: None,
1052 focus: cx.focus_handle(),1058 focus: cx.focus_handle(),
1059+ focus_dock: cx.focus_handle(),
1060+ focus_bench: cx.focus_handle(),
1061+ focus_inspector: cx.focus_handle(),
1053 });1062 });
1054 // Nothing else takes focus, and without it the key listener1063 // Nothing else takes focus, and without it the key listener
1055 // above is never reached.1064 // above is never reached.
crates/app/src/tests/view.rsmodified+42 -0
@@ -891,3 +891,45 @@ fn the_zoom_button_says_how_much_is_on_the_bed() {
891 assert_eq!(secs_label(1. / 60.), "0.02s");891 assert_eq!(secs_label(1. / 60.), "0.02s");
892 assert_eq!(secs_label(4.5), "4.5s");892 assert_eq!(secs_label(4.5), "4.5s");
893 }893 }
894+
895+/// Keyboard focus v1 (`ui::stance::{Surface, next_surface, is_focus_cycle_key}`):
896+/// Tab walks dock -> bench -> inspector -> dock, wrapping; Shift-Tab walks
897+/// it backward. No gpui window harness exists in this crate to drive a real
898+/// `KeyDownEvent` through -- this exercises the exact pure function every
899+/// surface's `on_key_down` calls, which is where the cycle order actually
900+/// lives.
901+#[test]
902+fn tab_cycles_dock_bench_inspector_and_wraps() {
903+ use crate::ui::stance::{Surface, next_surface};
904+
905+ assert_eq!(next_surface(Surface::Dock, false), Surface::Bench);
906+ assert_eq!(next_surface(Surface::Bench, false), Surface::Inspector);
907+ assert_eq!(next_surface(Surface::Inspector, false), Surface::Dock);
908+
909+ // Shift-Tab is the exact reverse at every step.
910+ assert_eq!(next_surface(Surface::Dock, true), Surface::Inspector);
911+ assert_eq!(next_surface(Surface::Inspector, true), Surface::Bench);
912+ assert_eq!(next_surface(Surface::Bench, true), Surface::Dock);
913+}
914+
915+/// The root fallback guarantee (main.rs:1054's "every keybind hangs off the
916+/// root FocusHandle"): a surface's own `on_key_down` answers Tab/Shift-Tab
917+/// and nothing else -- `cx.stop_propagation()` is only ever reached from
918+/// that one branch (`ui::stance::is_focus_cycle_key`), so every other key,
919+/// still-pressed while a surface is focused, is left un-stopped and bubbles
920+/// up the dispatch tree to the root's own `on_key_down`
921+/// (gpui-0.2.2 `window.rs`'s `dispatch_key_down_up_event`, `Bubble` phase,
922+/// walks the focused node's ancestors and stops only at
923+/// `cx.stop_propagation()` -- verified by reading that function, not
924+/// assumed). This asserts the gate itself: only "tab" is ever intercepted,
925+/// so a keybind like space (play/pause) or "s" (split) is provably never
926+/// swallowed by a focused surface.
927+#[test]
928+fn only_tab_is_intercepted_by_a_focused_surface_everything_else_reaches_root() {
929+ use crate::ui::stance::is_focus_cycle_key;
930+
931+ assert!(is_focus_cycle_key("tab"));
932+ for key in ["space", "escape", "s", "c", "left", "right", "enter", "delete", "?"] {
933+ assert!(!is_focus_cycle_key(key), "{key} must bubble to the root");
934+ }
935+}
crates/app/src/ui/dock_stance.rsmodified+41 -3
@@ -12,6 +12,7 @@
12 //! [`clip_middle`]...), only the anatomy around them is new.12 //! [`clip_middle`]...), only the anatomy around them is new.
13 13
14 use crate::ui::hitmap;14 use crate::ui::hitmap;
15+use crate::ui::stance::{Surface, is_focus_cycle_key, next_surface};
15 use crate::ui::type_scale::{self, Typeset, head, label, mono};16 use crate::ui::type_scale::{self, Typeset, head, label, mono};
16 use crate::*;17 use crate::*;
17 use gpui::FontWeight;18 use gpui::FontWeight;
@@ -817,7 +818,12 @@ fn subtitle_palette(player: &Player, cx: &mut Context<Player>) -> Option<AnyElem
817 /// filter, sort chips, rows, IMPORT, hint -- none of it the legacy818 /// filter, sort chips, rows, IMPORT, hint -- none of it the legacy
818 /// Media/Audio/Text panel `library.rs` draws. That panel's row facts819 /// Media/Audio/Text panel `library.rs` draws. That panel's row facts
819 /// ([`library_rows`]) are still what every row here is built from.820 /// ([`library_rows`]) are still what every row here is built from.
820-fn sources_tab(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {821+fn sources_tab(
822+ player: &Player,
823+ window: &mut Window,
824+ cx: &mut Context<Player>,
825+) -> impl IntoElement {
826+ let focused = player.focus_dock.is_focused(window);
821 let sources = player827 let sources = player
822 .session828 .session
823 .as_ref()829 .as_ref()
@@ -862,6 +868,11 @@ fn sources_tab(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
862 let filter_text: SharedString = player.dock_filter.clone().into();868 let filter_text: SharedString = player.dock_filter.clone().into();
863 div()869 div()
864 .id("dock-sources")870 .id("dock-sources")
871+ .track_focus(&player.focus_dock)
872+ .on_key_down(cx.listener(move |this, event: &KeyDownEvent, window, cx| {
873+ cycle_on_key_down(Surface::Dock)(this, event, window, cx)
874+ }))
875+ .when(focused, |d| d.border_1().border_color(rgb(STROKE_FOCUS())))
865 .flex_1()876 .flex_1()
866 .min_h(px(0.))877 .min_h(px(0.))
867 .flex()878 .flex()
@@ -1077,8 +1088,10 @@ fn clip_tab(
1077 player: &Player,1088 player: &Player,
1078 width: f32,1089 width: f32,
1079 window_size: Size<Pixels>,1090 window_size: Size<Pixels>,
1091+ window: &mut Window,
1080 cx: &mut Context<Player>,1092 cx: &mut Context<Player>,
1081 ) -> impl IntoElement {1093 ) -> impl IntoElement {
1094+ let focused = player.focus_inspector.is_focused(window);
1082 // The room actually given here, not the window's: `eq_card_w`/1095 // The room actually given here, not the window's: `eq_card_w`/
1083 // `card_max_w` are asked "how wide may I draw" and answered with the1096 // `card_max_w` are asked "how wide may I draw" and answered with the
1084 // *whole viewport's* width when handed `window_size` verbatim -- but this1097 // *whole viewport's* width when handed `window_size` verbatim -- but this
@@ -1099,6 +1112,11 @@ fn clip_tab(
1099 && !player.subtitle_style_open;1112 && !player.subtitle_style_open;
1100 div()1113 div()
1101 .id("dock-clip")1114 .id("dock-clip")
1115+ .track_focus(&player.focus_inspector)
1116+ .on_key_down(cx.listener(move |this, event: &KeyDownEvent, window, cx| {
1117+ cycle_on_key_down(Surface::Inspector)(this, event, window, cx)
1118+ }))
1119+ .when(focused, |d| d.border_1().border_color(rgb(STROKE_FOCUS())))
1102 .flex_1()1120 .flex_1()
1103 .min_h(px(0.))1121 .min_h(px(0.))
1104 .flex()1122 .flex()
@@ -1257,6 +1275,7 @@ pub(crate) fn render(
1257 player: &Player,1275 player: &Player,
1258 width: f32,1276 width: f32,
1259 window_size: Size<Pixels>,1277 window_size: Size<Pixels>,
1278+ window: &mut Window,
1260 cx: &mut Context<Player>,1279 cx: &mut Context<Player>,
1261 ) -> impl IntoElement {1280 ) -> impl IntoElement {
1262 let src_active = player.dock_src_active;1281 let src_active = player.dock_src_active;
@@ -1279,7 +1298,26 @@ pub(crate) fn render(
1279 .child(dock_tab("dock-tab-clip", "CLIP", !src_active, cx)),1298 .child(dock_tab("dock-tab-clip", "CLIP", !src_active, cx)),
1280 )1299 )
1281 .child(match src_active {1300 .child(match src_active {
1282- true => sources_tab(player, cx).into_any_element(),1301+ true => sources_tab(player, window, cx).into_any_element(),
1283- false => clip_tab(player, width, window_size, cx).into_any_element(),1302+ false => clip_tab(player, width, window_size, window, cx).into_any_element(),
1284 })1303 })
1285 }1304 }
1305+
1306+/// The Tab/Shift-Tab handler shared by the dock's two tabs (`sources_tab`'s
1307+/// "dock/library" and `clip_tab`'s "inspector") -- only [`is_focus_cycle_key`]
1308+/// is ever answered here; every other key is left un-stopped so it bubbles
1309+/// to the room's root handler (`ui::stance::render`'s `on_key_down`), which
1310+/// is the one every other keybind, Tab included when nothing in the ring
1311+/// has focus yet, still hangs off.
1312+fn cycle_on_key_down(
1313+ surface: Surface,
1314+) -> impl Fn(&mut Player, &KeyDownEvent, &mut Window, &mut Context<Player>) + 'static {
1315+ move |this, event, window, cx| {
1316+ if is_focus_cycle_key(event.keystroke.key.as_str()) {
1317+ let next = next_surface(surface, event.keystroke.modifiers.shift);
1318+ window.focus(this.focus_handle(next));
1319+ cx.stop_propagation();
1320+ cx.notify();
1321+ }
1322+ }
1323+}
crates/app/src/ui/stance.rsmodified+78 -4
@@ -17,6 +17,58 @@ use crate::ui::timeband_stance;
17 use crate::ui::type_scale::{self, Typeset};17 use crate::ui::type_scale::{self, Typeset};
18 use crate::*;18 use crate::*;
19 19
20+/// Keyboard focus v1: the three regions Tab/Shift-Tab cycle a painted ring
21+/// through -- the bench, the dock's Sources tab (library) and the dock's
22+/// Clip tab (inspector), the darkroom's own stand-ins for "timeline",
23+/// "library" and "inspector" (there is no separate inspector *region* in
24+/// this tree -- MOCK-SPEC's Clip tab, `dock_stance::clip_tab`, is it).
25+/// `Player::focus` (the root handle every keybind hangs off) is not a
26+/// member: Tab already means "select the clip under the playhead"
27+/// (`ActionId::Select`, `keymap.rs`) at the root, unchanged here, and only
28+/// starts meaning "enter the ring" once one of these three already holds
29+/// focus.
30+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
31+pub(crate) enum Surface {
32+ Dock,
33+ Bench,
34+ Inspector,
35+}
36+
37+/// Fixed cycle order: dock, bench, inspector.
38+const SURFACE_CYCLE: [Surface; 3] = [Surface::Dock, Surface::Bench, Surface::Inspector];
39+
40+/// Tab (`backward` false) or Shift-Tab (`backward` true) from `current`,
41+/// wrapping at either end of [`SURFACE_CYCLE`].
42+pub(crate) fn next_surface(current: Surface, backward: bool) -> Surface {
43+ let i = SURFACE_CYCLE.iter().position(|&s| s == current).unwrap();
44+ let n = SURFACE_CYCLE.len();
45+ SURFACE_CYCLE[if backward { (i + n - 1) % n } else { (i + 1) % n }]
46+}
47+
48+/// The one key a surface's own `on_key_down` answers. Everything else is
49+/// left alone on purpose, unhandled and un-stopped, so it bubbles to the
50+/// root's fallback handler exactly as an unfocused key already does
51+/// (`window.rs`'s key dispatch walks the focused node's ancestors in
52+/// `Bubble` phase and stops only at `cx.stop_propagation()` -- the three
53+/// surface handlers call that solely from this branch, gpui-0.2.2
54+/// `window.rs:3897-3906`).
55+pub(crate) fn is_focus_cycle_key(key: &str) -> bool {
56+ key == "tab"
57+}
58+
59+impl Player {
60+ /// The `FocusHandle` a [`Surface`] paints its ring on and Tab/Shift-Tab
61+ /// moves to next -- one door, so `next_surface`'s answer and the handle
62+ /// `window.focus` is given cannot name two different surfaces.
63+ pub(crate) fn focus_handle(&self, surface: Surface) -> &FocusHandle {
64+ match surface {
65+ Surface::Dock => &self.focus_dock,
66+ Surface::Bench => &self.focus_bench,
67+ Surface::Inspector => &self.focus_inspector,
68+ }
69+ }
70+}
71+
20 /// Left rail, full height (DESIGN §5).72 /// Left rail, full height (DESIGN §5).
21 pub(crate) const SPINE_W: f32 = 56.;73 pub(crate) const SPINE_W: f32 = 56.;
22 /// Fixed strip under the screen: timecode, transport, cut readout, contact74 /// 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<Player>) -> im
533 /// Height comes from [`Player::split_px`] now, not the fixed [`BENCH_H`]:585 /// Height comes from [`Player::split_px`] now, not the fixed [`BENCH_H`]:
534 /// the seam [`crate::ui::stance::render`] mounts above this region is what586 /// the seam [`crate::ui::stance::render`] mounts above this region is what
535 /// answers "ui fields are not stretchable" for the bench.587 /// answers "ui fields are not stretchable" for the bench.
536-fn bench(player: &mut Player, bench_h: f32, cx: &mut Context<Player>) -> impl IntoElement {588+fn bench(
589+ player: &mut Player,
590+ bench_h: f32,
591+ window: &mut Window,
592+ cx: &mut Context<Player>,
593+) -> impl IntoElement {
594+ let focused = player.focus_bench.is_focused(window);
537 div()595 div()
538 .id("stance-bench")596 .id("stance-bench")
597+ .track_focus(&player.focus_bench)
598+ .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| {
599+ if is_focus_cycle_key(event.keystroke.key.as_str()) {
600+ let next = next_surface(Surface::Bench, event.keystroke.modifiers.shift);
601+ window.focus(this.focus_handle(next));
602+ cx.stop_propagation();
603+ cx.notify();
604+ }
605+ }))
539 .flex_none()606 .flex_none()
540 .h(px(bench_h))607 .h(px(bench_h))
541 // §4: lanes take 0 radius.608 // §4: lanes take 0 radius.
@@ -543,6 +610,12 @@ fn bench(player: &mut Player, bench_h: f32, cx: &mut Context<Player>) -> impl In
543 .bg(rgb(DARK_CANVAS()))610 .bg(rgb(DARK_CANVAS()))
544 .border_t_1()611 .border_t_1()
545 .border_color(rgba(DARK_SEAM()))612 .border_color(rgba(DARK_SEAM()))
613+ // The ring (DESIGN §4's "1px, lamp-adjacent" convention -- the same
614+ // `.border_1().border_color(...)` a picked bench clip already uses):
615+ // painted on this outer frame, never on a row, and only while this
616+ // surface itself -- not a picked clip inside it -- holds the focus
617+ // ring Tab/Shift-Tab moves ([`next_surface`]).
618+ .when(focused, |d| d.border_1().border_color(rgb(STROKE_FOCUS())))
546 .flex()619 .flex()
547 .flex_col()620 .flex_col()
548 .px(px(12.))621 .px(px(12.))
@@ -739,6 +812,7 @@ fn dock(
739 player: &Player,812 player: &Player,
740 dock_w: f32,813 dock_w: f32,
741 window_size: Size<Pixels>,814 window_size: Size<Pixels>,
815+ window: &mut Window,
742 cx: &mut Context<Player>,816 cx: &mut Context<Player>,
743 ) -> impl IntoElement {817 ) -> impl IntoElement {
744 div()818 div()
@@ -751,7 +825,7 @@ fn dock(
751 .border_color(rgba(DARK_SEAM()))825 .border_color(rgba(DARK_SEAM()))
752 .flex()826 .flex()
753 .flex_col()827 .flex_col()
754- .child(dock_stance::render(player, dock_w, window_size, cx))828+ .child(dock_stance::render(player, dock_w, window_size, window, cx))
755 }829 }
756 830
757 /// A zero-size element whose only job is `window.on_mouse_event`'s door:831 /// A zero-size element whose only job is `window.on_mouse_event`'s door:
@@ -982,7 +1056,7 @@ pub(crate) fn render(
982 .child(screen(player, position, window, cx))1056 .child(screen(player, position, window, cx))
983 .child(time_band(player, position, cx))1057 .child(time_band(player, position, cx))
984 .child(divider(Split::Bench, cx))1058 .child(divider(Split::Bench, cx))
985- .child(bench(player, bench_h, cx))1059+ .child(bench(player, bench_h, window, cx))
986 .child(ledger(player, position))1060 .child(ledger(player, position))
987 .when_some(player.notices.back().cloned(), |el, n| {1061 .when_some(player.notices.back().cloned(), |el, n| {
988 el.child(notice_plate(n, bench_h))1062 el.child(notice_plate(n, bench_h))
@@ -1033,7 +1107,7 @@ pub(crate) fn render(
1033 }),1107 }),
1034 )1108 )
1035 .child(divider(Split::Dock, cx))1109 .child(divider(Split::Dock, cx))
1036- .child(dock(player, dock_w, window_size, cx))1110+ .child(dock(player, dock_w, window_size, window, cx))
1037 // The library row menu mounts on the ROOT, not inside1111 // The library row menu mounts on the ROOT, not inside
1038 // `stance-centre` like the clip menu beside it: it is opened from a1112 // `stance-centre` like the clip menu beside it: it is opened from a
1039 // dock row, so its anchor is a window x past the centre column's own1113 // dock row, so its anchor is a window x past the centre column's own