From 8aba979d0bc4434c230bb45feb2354ba6bd00e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 10 Aug 2026 07:45:56 +0300 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor(ui):=20single-sou?= =?UTF-8?q?rce=20shortcut=20registry=20feeds=20handler=20and=20keys=20over?= =?UTF-8?q?lay?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The keys menu listed only the bindable actions, so the strokes the modal cards read for themselves -- escape out of a card, a digit or backspace in the export card -- worked and were nowhere on it. They join the registry as Fixed entries, every action gains a Category, and the overlay draws both halves grouped by heading. no_stroke_is_missing_from_the_keys_menu reads the key handler's own source: every key it compares against must be a binding or a Fixed entry, so a new shortcut that nobody listed fails the test instead of shipping invisible. --- crates/app/src/keymap.rs | 77 ++++++++++++++++++ crates/app/src/main.rs | 167 +++++++++++++++++++++++++++++++-------- 2 files changed, 210 insertions(+), 34 deletions(-) diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index a282198..e94e483 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -118,8 +118,85 @@ impl ActionId { fn from_name(name: &str) -> Option { Self::ALL.into_iter().find(|a| a.name() == name) } + + /// Which heading the keys menu files it under. Every action has one, so a + /// new action is listed the moment it exists -- there is no second list to + /// forget. + pub fn category(self) -> Category { + match self { + ActionId::Play => Category::Playback, + ActionId::Copy | ActionId::Paste | ActionId::Delete | ActionId::Lift => Category::Clips, + ActionId::Cut | ActionId::Regroup | ActionId::Undo => Category::Editing, + ActionId::ToggleMute | ActionId::VolumeUp | ActionId::VolumeDown => Category::Audio, + ActionId::Save | ActionId::Export | ActionId::CancelExport => Category::File, + } + } +} + +/// A heading in the keys menu, and the order the headings come in. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Category { + Playback, + Editing, + Clips, + Audio, + File, + View, +} + +impl Category { + pub const ALL: [Category; 6] = [ + Category::Playback, + Category::Editing, + Category::Clips, + Category::Audio, + Category::File, + Category::View, + ]; + + pub fn label(self) -> &'static str { + match self { + Category::Playback => "Playback", + Category::Editing => "Editing", + Category::Clips => "Clips", + Category::Audio => "Audio", + Category::File => "File", + Category::View => "View", + } + } +} + +/// A stroke that works but nobody may rebind: the modal cards read the keyboard +/// themselves, and what closes a card must not be a thing a user can take away. +/// Listed here anyway, because the keys menu shows every stroke that works -- +/// `no_stroke_is_missing_from_the_keys_menu` reads the key handler's own source +/// and fails on any key that reaches neither this table nor a binding. +pub struct Fixed { + /// As a person reads it, which for a single key is [`Chord::pretty`]'s + /// spelling and for a family of keys is the family (`0–9`). + pub chord: &'static str, + pub label: &'static str, + pub category: Category, } +pub const FIXED: [Fixed; 3] = [ + Fixed { + chord: "esc", + label: "Close this card, or cancel a capture", + category: Category::View, + }, + Fixed { + chord: "0–9", + label: "Type a custom export bitrate", + category: Category::File, + }, + Fixed { + chord: "backspace", + label: "Erase a bitrate digit", + category: Category::File, + }, +]; + /// A stroke as the key handler sees it: gpui's key name plus the one modifier /// this editor binds. Nothing else is a chord here -- shift and alt are part of /// the key name gpui reports, and a second modifier would need the file format diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 9206398..3db415d 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -1638,42 +1638,84 @@ impl Player { ctrl: false, } .pretty(); - // One row per action, not per binding: an action with two strokes reads - // as one line ("x or delete"), and a rebind replaces that whole set. - let rows: Vec<_> = ActionId::ALL - .into_iter() - .enumerate() - .map(|(i, action)| { - let capturing = self.rebinding == Some(action); - let out = out.clone(); - div() - .id(("bind", i)) - .flex() - // The floor, not the height: a row that needed two lines - // would otherwise paint over the one under it. - .min_h(px(KEYS_ROW_H)) - .items_center() - .justify_between() - .gap(px(12.)) - .px(px(6.)) - .rounded(px(3.)) - .cursor_pointer() - .hover(|s| s.bg(rgb(HOVER))) - .when(capturing, |d| d.bg(rgb(SELECTED))) - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - this.rebinding = Some(action); - cx.notify(); - })) - .child(action.label()) - .child(if capturing { + // Every stroke that works, under its heading, and both halves of the + // list come from the registry: one row per action -- an action with two + // strokes reads as one line ("x or delete") and a rebind replaces that + // whole set -- then the strokes the modal cards answer to, which are + // shown but not offered, because nothing may unbind the way out. + let mut rows: Vec = Vec::new(); + for category in keymap::Category::ALL { + let actions = ActionId::ALL + .into_iter() + .enumerate() + .filter(|(_, a)| a.category() == category); + let fixed = keymap::FIXED.iter().filter(|f| f.category == category); + let mut headed = false; + let mut head = |rows: &mut Vec| { + if !std::mem::replace(&mut headed, true) { + rows.push( div() + .flex_none() + .px(px(6.)) + .pt(px(4.)) + .text_size(px(11.)) .text_color(rgb(INK_DIM)) - .child(format!("press a key — {out} cancels")) - } else { - div().child(self.keymap.display(action)) - }) - }) - .collect(); + .child(category.label()) + .into_any_element(), + ); + } + }; + for (i, action) in actions { + head(&mut rows); + let capturing = self.rebinding == Some(action); + let out = out.clone(); + rows.push( + div() + .id(("bind", i)) + .flex() + // The floor, not the height: a row that needed two lines + // would otherwise paint over the one under it. + .min_h(px(KEYS_ROW_H)) + .items_center() + .justify_between() + .gap(px(12.)) + .px(px(6.)) + .rounded(px(3.)) + .cursor_pointer() + .hover(|s| s.bg(rgb(HOVER))) + .when(capturing, |d| d.bg(rgb(SELECTED))) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.rebinding = Some(action); + cx.notify(); + })) + .child(action.label()) + .child(if capturing { + div() + .text_color(rgb(INK_DIM)) + .child(format!("press a key — {out} cancels")) + } else { + div().child(self.keymap.display(action)) + }) + .into_any_element(), + ); + } + for f in fixed { + head(&mut rows); + rows.push( + div() + .flex() + .min_h(px(KEYS_ROW_H)) + .items_center() + .justify_between() + .gap(px(12.)) + .px(px(6.)) + .child(f.label) + // Dim, and no hover: this one is not a row you can click. + .child(div().text_color(rgb(INK_DIM)).child(f.chord)) + .into_any_element(), + ); + } + } Some( div() .absolute() @@ -2902,6 +2944,63 @@ mod tests { assert!(!is_project(p("a.edith.mp4"))); } + /// The keys menu is the registry drawn, so a *bindable* stroke cannot go + /// missing from it by construction. The strokes the modal cards read for + /// themselves are the ones that could: this reads the key handler's own + /// source and fails on any key it answers to that the menu never mentions. + #[test] + fn no_stroke_is_missing_from_the_keys_menu() { + use keymap::{ActionId, Keymap}; + // Everything above the test module -- the handler, and the helpers it + // asks. The tests below compare keys too, and are not shortcuts. + let handler = include_str!("main.rs") + .split("#[cfg(test)]") + .next() + .unwrap(); + let keymap = Keymap::defaults(); + let listed = |key: &str| { + let pretty = keymap::Chord { + key: key.to_string(), + ctrl: false, + } + .pretty(); + keymap.lookup(key, false).is_some() || keymap::FIXED.iter().any(|f| f.chord == pretty) + }; + let mut compared = 0; + for (at, needle) in handler.match_indices("key == ") { + let rest = &handler[at + needle.len()..]; + let key = match rest.strip_prefix('"') { + Some(literal) => { + literal[..literal.find('"').expect("unterminated key")].to_string() + } + // A named constant: the escape the cards get out by. + None if rest.starts_with("ESCAPE") => super::ESCAPE.to_string(), + None => panic!("a key compared against something this cannot read: {rest:.20}"), + }; + assert!( + listed(&key), + "the handler answers to {key:?} and the keys menu never says so" + ); + compared += 1; + } + // The scan is only a guard while it still finds the comparisons: a + // rewrite that spells them differently must come back here. + assert!( + compared >= 4, + "the key comparisons moved; this scan is blind" + ); + // The one branch that is not a comparison -- any digit is a bitrate. + assert!(handler.contains("key.parse::()")); + assert!(keymap::FIXED.iter().any(|f| f.chord == "0–9")); + // And every entry of both halves lands under a heading the menu draws. + for action in ActionId::ALL { + assert!(keymap::Category::ALL.contains(&action.category())); + } + for fixed in &keymap::FIXED { + assert!(keymap::Category::ALL.contains(&fixed.category)); + } + } + #[test] fn clip_boxes_split_the_lane_by_duration() { // 1 s + 3 s of a 4 s timeline: a quarter and three quarters.