forge
Explore
Log in

Don't you dare go Hollow

Single-source shortcut registry feeds handler and keys overlay

Browse files at this commit

8aba979d0bc4434c230bb45feb2354ba6bd00e8fAhmet Kaan GümüşParent: 771cd4a
  • refactor
    • single-source shortcut registry feeds handler and keys overlay
Show full message
♻️ refactor(ui): single-source shortcut registry feeds handler and keys overlay

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.rsmodified+77 -0
@@ -118,8 +118,85 @@ impl ActionId {
118118 fn from_name(name: &str) -> Option<Self> {
119119 Self::ALL.into_iter().find(|a| a.name() == name)
120120 }
121+
122+ /// Which heading the keys menu files it under. Every action has one, so a
123+ /// new action is listed the moment it exists -- there is no second list to
124+ /// forget.
125+ pub fn category(self) -> Category {
126+ match self {
127+ ActionId::Play => Category::Playback,
128+ ActionId::Copy | ActionId::Paste | ActionId::Delete | ActionId::Lift => Category::Clips,
129+ ActionId::Cut | ActionId::Regroup | ActionId::Undo => Category::Editing,
130+ ActionId::ToggleMute | ActionId::VolumeUp | ActionId::VolumeDown => Category::Audio,
131+ ActionId::Save | ActionId::Export | ActionId::CancelExport => Category::File,
132+ }
133+ }
134+}
135+
136+/// A heading in the keys menu, and the order the headings come in.
137+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
138+pub enum Category {
139+ Playback,
140+ Editing,
141+ Clips,
142+ Audio,
143+ File,
144+ View,
145+}
146+
147+impl Category {
148+ pub const ALL: [Category; 6] = [
149+ Category::Playback,
150+ Category::Editing,
151+ Category::Clips,
152+ Category::Audio,
153+ Category::File,
154+ Category::View,
155+ ];
156+
157+ pub fn label(self) -> &'static str {
158+ match self {
159+ Category::Playback => "Playback",
160+ Category::Editing => "Editing",
161+ Category::Clips => "Clips",
162+ Category::Audio => "Audio",
163+ Category::File => "File",
164+ Category::View => "View",
165+ }
166+ }
167+}
168+
169+/// A stroke that works but nobody may rebind: the modal cards read the keyboard
170+/// themselves, and what closes a card must not be a thing a user can take away.
171+/// Listed here anyway, because the keys menu shows every stroke that works --
172+/// `no_stroke_is_missing_from_the_keys_menu` reads the key handler's own source
173+/// and fails on any key that reaches neither this table nor a binding.
174+pub struct Fixed {
175+ /// As a person reads it, which for a single key is [`Chord::pretty`]'s
176+ /// spelling and for a family of keys is the family (`0–9`).
177+ pub chord: &'static str,
178+ pub label: &'static str,
179+ pub category: Category,
121180 }
122181
182+pub const FIXED: [Fixed; 3] = [
183+ Fixed {
184+ chord: "esc",
185+ label: "Close this card, or cancel a capture",
186+ category: Category::View,
187+ },
188+ Fixed {
189+ chord: "0–9",
190+ label: "Type a custom export bitrate",
191+ category: Category::File,
192+ },
193+ Fixed {
194+ chord: "backspace",
195+ label: "Erase a bitrate digit",
196+ category: Category::File,
197+ },
198+];
199+
123200 /// A stroke as the key handler sees it: gpui's key name plus the one modifier
124201 /// this editor binds. Nothing else is a chord here -- shift and alt are part of
125202 /// the key name gpui reports, and a second modifier would need the file format
crates/app/src/main.rsmodified+133 -34
@@ -1638,42 +1638,84 @@ impl Player {
16381638 ctrl: false,
16391639 }
16401640 .pretty();
1641- // One row per action, not per binding: an action with two strokes reads
1642- // as one line ("x or delete"), and a rebind replaces that whole set.
1643- let rows: Vec<_> = ActionId::ALL
1644- .into_iter()
1645- .enumerate()
1646- .map(|(i, action)| {
1647- let capturing = self.rebinding == Some(action);
1648- let out = out.clone();
1649- div()
1650- .id(("bind", i))
1651- .flex()
1652- // The floor, not the height: a row that needed two lines
1653- // would otherwise paint over the one under it.
1654- .min_h(px(KEYS_ROW_H))
1655- .items_center()
1656- .justify_between()
1657- .gap(px(12.))
1658- .px(px(6.))
1659- .rounded(px(3.))
1660- .cursor_pointer()
1661- .hover(|s| s.bg(rgb(HOVER)))
1662- .when(capturing, |d| d.bg(rgb(SELECTED)))
1663- .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
1664- this.rebinding = Some(action);
1665- cx.notify();
1666- }))
1667- .child(action.label())
1668- .child(if capturing {
1641+ // Every stroke that works, under its heading, and both halves of the
1642+ // list come from the registry: one row per action -- an action with two
1643+ // strokes reads as one line ("x or delete") and a rebind replaces that
1644+ // whole set -- then the strokes the modal cards answer to, which are
1645+ // shown but not offered, because nothing may unbind the way out.
1646+ let mut rows: Vec<AnyElement> = Vec::new();
1647+ for category in keymap::Category::ALL {
1648+ let actions = ActionId::ALL
1649+ .into_iter()
1650+ .enumerate()
1651+ .filter(|(_, a)| a.category() == category);
1652+ let fixed = keymap::FIXED.iter().filter(|f| f.category == category);
1653+ let mut headed = false;
1654+ let mut head = |rows: &mut Vec<AnyElement>| {
1655+ if !std::mem::replace(&mut headed, true) {
1656+ rows.push(
16691657 div()
1658+ .flex_none()
1659+ .px(px(6.))
1660+ .pt(px(4.))
1661+ .text_size(px(11.))
16701662 .text_color(rgb(INK_DIM))
1671- .child(format!("press a key — {out} cancels"))
1672- } else {
1673- div().child(self.keymap.display(action))
1674- })
1675- })
1676- .collect();
1663+ .child(category.label())
1664+ .into_any_element(),
1665+ );
1666+ }
1667+ };
1668+ for (i, action) in actions {
1669+ head(&mut rows);
1670+ let capturing = self.rebinding == Some(action);
1671+ let out = out.clone();
1672+ rows.push(
1673+ div()
1674+ .id(("bind", i))
1675+ .flex()
1676+ // The floor, not the height: a row that needed two lines
1677+ // would otherwise paint over the one under it.
1678+ .min_h(px(KEYS_ROW_H))
1679+ .items_center()
1680+ .justify_between()
1681+ .gap(px(12.))
1682+ .px(px(6.))
1683+ .rounded(px(3.))
1684+ .cursor_pointer()
1685+ .hover(|s| s.bg(rgb(HOVER)))
1686+ .when(capturing, |d| d.bg(rgb(SELECTED)))
1687+ .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
1688+ this.rebinding = Some(action);
1689+ cx.notify();
1690+ }))
1691+ .child(action.label())
1692+ .child(if capturing {
1693+ div()
1694+ .text_color(rgb(INK_DIM))
1695+ .child(format!("press a key — {out} cancels"))
1696+ } else {
1697+ div().child(self.keymap.display(action))
1698+ })
1699+ .into_any_element(),
1700+ );
1701+ }
1702+ for f in fixed {
1703+ head(&mut rows);
1704+ rows.push(
1705+ div()
1706+ .flex()
1707+ .min_h(px(KEYS_ROW_H))
1708+ .items_center()
1709+ .justify_between()
1710+ .gap(px(12.))
1711+ .px(px(6.))
1712+ .child(f.label)
1713+ // Dim, and no hover: this one is not a row you can click.
1714+ .child(div().text_color(rgb(INK_DIM)).child(f.chord))
1715+ .into_any_element(),
1716+ );
1717+ }
1718+ }
16771719 Some(
16781720 div()
16791721 .absolute()
@@ -2902,6 +2944,63 @@ mod tests {
29022944 assert!(!is_project(p("a.edith.mp4")));
29032945 }
29042946
2947+ /// The keys menu is the registry drawn, so a *bindable* stroke cannot go
2948+ /// missing from it by construction. The strokes the modal cards read for
2949+ /// themselves are the ones that could: this reads the key handler's own
2950+ /// source and fails on any key it answers to that the menu never mentions.
2951+ #[test]
2952+ fn no_stroke_is_missing_from_the_keys_menu() {
2953+ use keymap::{ActionId, Keymap};
2954+ // Everything above the test module -- the handler, and the helpers it
2955+ // asks. The tests below compare keys too, and are not shortcuts.
2956+ let handler = include_str!("main.rs")
2957+ .split("#[cfg(test)]")
2958+ .next()
2959+ .unwrap();
2960+ let keymap = Keymap::defaults();
2961+ let listed = |key: &str| {
2962+ let pretty = keymap::Chord {
2963+ key: key.to_string(),
2964+ ctrl: false,
2965+ }
2966+ .pretty();
2967+ keymap.lookup(key, false).is_some() || keymap::FIXED.iter().any(|f| f.chord == pretty)
2968+ };
2969+ let mut compared = 0;
2970+ for (at, needle) in handler.match_indices("key == ") {
2971+ let rest = &handler[at + needle.len()..];
2972+ let key = match rest.strip_prefix('"') {
2973+ Some(literal) => {
2974+ literal[..literal.find('"').expect("unterminated key")].to_string()
2975+ }
2976+ // A named constant: the escape the cards get out by.
2977+ None if rest.starts_with("ESCAPE") => super::ESCAPE.to_string(),
2978+ None => panic!("a key compared against something this cannot read: {rest:.20}"),
2979+ };
2980+ assert!(
2981+ listed(&key),
2982+ "the handler answers to {key:?} and the keys menu never says so"
2983+ );
2984+ compared += 1;
2985+ }
2986+ // The scan is only a guard while it still finds the comparisons: a
2987+ // rewrite that spells them differently must come back here.
2988+ assert!(
2989+ compared >= 4,
2990+ "the key comparisons moved; this scan is blind"
2991+ );
2992+ // The one branch that is not a comparison -- any digit is a bitrate.
2993+ assert!(handler.contains("key.parse::<u32>()"));
2994+ assert!(keymap::FIXED.iter().any(|f| f.chord == "0–9"));
2995+ // And every entry of both halves lands under a heading the menu draws.
2996+ for action in ActionId::ALL {
2997+ assert!(keymap::Category::ALL.contains(&action.category()));
2998+ }
2999+ for fixed in &keymap::FIXED {
3000+ assert!(keymap::Category::ALL.contains(&fixed.category));
3001+ }
3002+ }
3003+
29053004 #[test]
29063005 fn clip_boxes_split_the_lane_by_duration() {
29073006 // 1 s + 3 s of a 4 s timeline: a quarter and three quarters.