forge
Explore
Log in

Don't you dare go Hollow

Overlay title overlap, one row per action

Browse files at this commit

d598c98594e06ecde1a6ce78862fc96ed3f2e4a0Ahmet Kaan GümüşParent: cad8d5a
  • fix
    • overlay title overlap, one row per action
Show full message
🐛 fix(keys): overlay title overlap, one row per action

Title sat in a fixed-height slot; wrapped text painted over the
first row. Title and status line are now auto-height flex_none
children, rows use min_h. Rows merged to one per action showing
the full chord set; a rebind replaces all of the action's chords
(multi-chord stays possible via the config file). Rows grew to
the 24px hit minimum. In-card notice moved to the status line
under the title.
crates/app/src/keymap.rsmodified+68 -26
@@ -168,7 +168,7 @@ pub struct Binding {
168 }168 }
169 169
170 /// Every binding in force, in display order. No chord appears twice -- that is170 /// Every binding in force, in display order. No chord appears twice -- that is
171-/// the invariant [`Keymap::rebind`] and [`parse`] both defend, and it is what171+/// the invariant [`Keymap::rebind_action`] and [`parse`] both defend, and it is what
172 /// lets [`Keymap::lookup`] answer with the first match.172 /// lets [`Keymap::lookup`] answer with the first match.
173 #[derive(Clone, PartialEq, Eq, Debug)]173 #[derive(Clone, PartialEq, Eq, Debug)]
174 pub struct Keymap {174 pub struct Keymap {
@@ -229,30 +229,37 @@ impl Keymap {
229 }229 }
230 }230 }
231 231
232- /// The bindings themselves, in display order. The index into this slice is232+ /// Every binding, in display order.
233- /// what [`Keymap::rebind`] takes.
234 pub fn entries(&self) -> &[Binding] {233 pub fn entries(&self) -> &[Binding] {
235 &self.bindings234 &self.bindings
236 }235 }
237 236
238- /// Puts `chord` on the binding at `index`. `Err` names the action that237+ /// Makes `chord` the whole of what reaches `action`: its other chords go, so
239- /// already holds the chord and nothing is changed: two actions on one stroke238+ /// what the editor shows for an action is what a rebind replaced. Two
240- /// would make the second unreachable, and silently stealing it is worse than239+ /// strokes for one action stay expressible, but only in the file -- the
241- /// refusing. Rebinding a binding to the chord it already has is not a240+ /// parser takes as many lines per action as it is given.
242- /// conflict with itself.
243 ///241 ///
244- /// `index` comes from [`Keymap::entries`], whose length never changes;242+ /// `Err` names the action that already holds the chord and nothing changes:
245- /// anything else is a bug and panics like the slice index it is.243+ /// two actions on one stroke would make the second unreachable, and silently
246- pub fn rebind(&mut self, index: usize, chord: Chord) -> Result<(), ActionId> {244+ /// stealing it is worse than refusing. A chord the action already holds is
245+ /// not a conflict with itself -- it collapses the action onto that one.
246+ pub fn rebind_action(&mut self, action: ActionId, chord: Chord) -> Result<(), ActionId> {
247 if let Some(held) = self247 if let Some(held) = self
248 .bindings248 .bindings
249 .iter()249 .iter()
250- .enumerate()250+ .find(|b| b.action != action && b.chord == chord)
251- .find(|(i, b)| *i != index && b.chord == chord)
252 {251 {
253- return Err(held.1.action);252+ return Err(held.action);
254 }253 }
255- self.bindings[index].chord = chord;254+ self.bindings.retain(|b| b.action != action);
255+ // Back into its own place in the display order rather than onto the end,
256+ // so the file and the editor keep reading in the order of `ALL`.
257+ let at = self
258+ .bindings
259+ .iter()
260+ .position(|b| rank(b.action) > rank(action))
261+ .unwrap_or(self.bindings.len());
262+ self.bindings.insert(at, Binding { action, chord });
256 Ok(())263 Ok(())
257 }264 }
258 265
@@ -331,6 +338,15 @@ impl Keymap {
331 }338 }
332 }339 }
333 340
341+/// Where an action sits in the display order, which is the order of
342+/// [`ActionId::ALL`] and the order the file is written in.
343+fn rank(action: ActionId) -> usize {
344+ ActionId::ALL
345+ .iter()
346+ .position(|a| *a == action)
347+ .unwrap_or(ActionId::ALL.len())
348+}
349+
334 /// The path rule, with the environment handed in so it can be checked. An350 /// The path rule, with the environment handed in so it can be checked. An
335 /// `XDG_CONFIG_HOME` that is empty is one the spec says to ignore; with no351 /// `XDG_CONFIG_HOME` that is empty is one the spec says to ignore; with no
336 /// `HOME` either there is nowhere but here, and a relative path is still better352 /// `HOME` either there is nowhere but here, and a relative path is still better
@@ -493,20 +509,44 @@ mod tests {
493 }509 }
494 510
495 #[test]511 #[test]
496- fn rebinding_refuses_to_steal_a_stroke() {512+ fn rebinding_refuses_to_steal_another_actions_stroke() {
497 let mut k = Keymap::defaults();513 let mut k = Keymap::defaults();
498- // Index 1 is Export on `e`; `space` is Play's.514+ assert_eq!(
499- assert_eq!(k.rebind(1, chord("space", false)), Err(ActionId::Play));515+ k.rebind_action(ActionId::Export, chord("space", false)),
516+ Err(ActionId::Play)
517+ );
518+ // Refused means unchanged, on both sides of the conflict.
500 assert_eq!(k.lookup("e", false), Some(ActionId::Export));519 assert_eq!(k.lookup("e", false), Some(ActionId::Export));
501- // Its own chord is not a conflict with itself.520+ assert_eq!(k.lookup("space", false), Some(ActionId::Play));
502- assert_eq!(k.rebind(1, chord("e", false)), Ok(()));521+ // A free stroke takes, and the old one stops meaning anything.
503- // A free stroke moves, and the old one stops meaning anything.522+ assert_eq!(k.rebind_action(ActionId::Export, chord("w", true)), Ok(()));
504- assert_eq!(k.rebind(1, chord("w", true)), Ok(()));
505 assert_eq!(k.lookup("w", true), Some(ActionId::Export));523 assert_eq!(k.lookup("w", true), Some(ActionId::Export));
506 assert_eq!(k.lookup("e", false), None);524 assert_eq!(k.lookup("e", false), None);
507- // Even the action's own second stroke is a conflict: it would leave one525+ assert_eq!(k.display(ActionId::Export), "ctrl+w");
508- // of the two bindings unreachable.526+ }
509- assert_eq!(k.rebind(6, chord("delete", false)), Err(ActionId::Delete));527+
528+ #[test]
529+ fn a_rebound_action_keeps_only_the_new_stroke() {
530+ let mut k = Keymap::defaults();
531+ assert_eq!(k.display(ActionId::Delete), "x or delete");
532+ // One stroke replaces the whole set: what the row showed is what went.
533+ assert_eq!(k.rebind_action(ActionId::Delete, chord("d", true)), Ok(()));
534+ assert_eq!(k.display(ActionId::Delete), "ctrl+d");
535+ assert_eq!(k.lookup("x", false), None);
536+ assert_eq!(k.lookup("delete", false), None);
537+ assert_eq!(k.lookup("d", true), Some(ActionId::Delete));
538+ // One of its own chords is not a conflict with itself -- it collapses
539+ // the action onto that one.
540+ assert_eq!(k.rebind_action(ActionId::Undo, chord("z", true)), Ok(()));
541+ assert_eq!(k.display(ActionId::Undo), "ctrl+z");
542+ assert_eq!(k.lookup("z", false), None);
543+ // The action keeps its place in the display order, and the file keeps
544+ // reading in that order too.
545+ let order: Vec<_> = k.entries().iter().map(|b| b.action).collect();
546+ assert_eq!(order, ActionId::ALL.to_vec());
547+ assert_eq!(emit(&k), emit(&parse(&emit(&k)).unwrap()));
548+ // Every action is now single-bound, so the file is one line each.
549+ assert_eq!(k.entries().len(), ActionId::ALL.len());
510 }550 }
511 551
512 #[test]552 #[test]
@@ -533,7 +573,9 @@ mod tests {
533 let dir = std::env::temp_dir().join(format!("edith-keymap-{}", std::process::id()));573 let dir = std::env::temp_dir().join(format!("edith-keymap-{}", std::process::id()));
534 let path = dir.join("edith").join("keybindings");574 let path = dir.join("edith").join("keybindings");
535 let mut written = Keymap::defaults();575 let mut written = Keymap::defaults();
536- written.rebind(1, chord("w", true)).unwrap();576+ written
577+ .rebind_action(ActionId::Export, chord("w", true))
578+ .unwrap();
537 // The config directory does not exist yet -- the first save makes it.579 // The config directory does not exist yet -- the first save makes it.
538 written.save_to(&path).unwrap();580 written.save_to(&path).unwrap();
539 let (read, notice) = Keymap::load_from(&path);581 let (read, notice) = Keymap::load_from(&path);
crates/app/src/main.rsmodified+63 -39
@@ -55,9 +55,10 @@ const RULER_HIT_H: f32 = HIT_MIN;
55 /// Wide enough for `HH:MM:SS:FF / HH:MM:SS:FF`, and fixed so changing digits55 /// Wide enough for `HH:MM:SS:FF / HH:MM:SS:FF`, and fixed so changing digits
56 /// cannot push the layout around.56 /// cannot push the layout around.
57 const TIME_W: f32 = 200.;57 const TIME_W: f32 = 200.;
58-/// The keybindings card: 11 rows and a title inside a 360 px tall window.58+/// The keybindings card: a row per action, a title and a status line, inside a
59-const KEYS_W: f32 = 300.;59+/// 360 px tall window. The rows are click targets, so `HIT_MIN` binds them too.
60-const KEYS_ROW_H: f32 = 20.;60+const KEYS_W: f32 = 320.;
61+const KEYS_ROW_H: f32 = HIT_MIN;
61 62
62 /// The one key name this file still spells out, and gpui's spelling of it: it63 /// The one key name this file still spells out, and gpui's spelling of it: it
63 /// is the way out of a capture and out of the overlay, and both have to work64 /// is the way out of a capture and out of the overlay, and both have to work
@@ -118,9 +119,9 @@ struct Player {
118 /// pointer: a stroke or a click meant for a row must not also cut the119 /// pointer: a stroke or a click meant for a row must not also cut the
119 /// timeline.120 /// timeline.
120 keys_open: bool,121 keys_open: bool,
121- /// The row waiting for a stroke, as an index into `keymap.entries()`. The122+ /// The action whose row is waiting for a stroke. The next key that is
122- /// next key that is neither escape nor a lone modifier *is* that binding.123+ /// neither escape nor a lone modifier becomes the whole of what reaches it.
123- rebinding: Option<usize>,124+ rebinding: Option<ActionId>,
124 /// What the last file action had to say. Holds its own bar above the panel125 /// What the last file action had to say. Holds its own bar above the panel
125 /// until it is answered -- any key retires it, so does a click on it -- so a126 /// until it is answered -- any key retires it, so does a click on it -- so a
126 /// failure is read in full instead of blinking past.127 /// failure is read in full instead of blinking past.
@@ -370,17 +371,19 @@ impl Player {
370 cx.notify();371 cx.notify();
371 }372 }
372 373
373- /// The stroke a waiting row was after. A chord another action already holds374+ /// The stroke a waiting row was after: it becomes the whole of what reaches
374- /// is refused by the keymap and the row keeps waiting, so the next stroke is375+ /// that action, which is what the row was showing. A chord another action
375- /// another try rather than a lost one. A binding that took holds either way:376+ /// already holds is refused by the keymap and the row keeps waiting, so the
377+ /// next stroke is another try rather than a lost one. A binding that took
378+ /// holds either way:
376 /// what a failed write costs is only the next run, which is what the notice379 /// what a failed write costs is only the next run, which is what the notice
377 /// is for.380 /// is for.
378- fn capture(&mut self, index: usize, key: &str, ctrl: bool) {381+ fn capture(&mut self, action: ActionId, key: &str, ctrl: bool) {
379 let chord = keymap::Chord {382 let chord = keymap::Chord {
380 key: key.to_string(),383 key: key.to_string(),
381 ctrl,384 ctrl,
382 };385 };
383- let text = match self.keymap.rebind(index, chord.clone()) {386+ let text = match self.keymap.rebind_action(action, chord.clone()) {
384 Ok(()) => {387 Ok(()) => {
385 self.rebinding = None;388 self.rebinding = None;
386 match self.keymap.save() {389 match self.keymap.save() {
@@ -532,11 +535,11 @@ impl Render for Player {
532 // data: it means the binding and nothing else, which is why this535 // data: it means the binding and nothing else, which is why this
533 // answers before the export guard and before the keymap is536 // answers before the export guard and before the keymap is
534 // consulted at all.537 // consulted at all.
535- if let Some(index) = this.rebinding {538+ if let Some(action) = this.rebinding {
536 if key == ESCAPE {539 if key == ESCAPE {
537 this.rebinding = None;540 this.rebinding = None;
538 } else if !is_bare_modifier(key) {541 } else if !is_bare_modifier(key) {
539- this.capture(index, key, event.keystroke.modifiers.control);542+ this.capture(action, key, event.keystroke.modifiers.control);
540 }543 }
541 cx.notify();544 cx.notify();
542 return;545 return;
@@ -920,18 +923,20 @@ impl Player {
920 ctrl: false,923 ctrl: false,
921 }924 }
922 .pretty();925 .pretty();
923- let rows: Vec<_> = self926+ // One row per action, not per binding: an action with two strokes reads
924- .keymap927+ // as one line ("x or delete"), and a rebind replaces that whole set.
925- .entries()928+ let rows: Vec<_> = ActionId::ALL
926- .iter()929+ .into_iter()
927 .enumerate()930 .enumerate()
928- .map(|(i, binding)| {931+ .map(|(i, action)| {
929- let capturing = self.rebinding == Some(i);932+ let capturing = self.rebinding == Some(action);
930 let out = out.clone();933 let out = out.clone();
931 div()934 div()
932 .id(("bind", i))935 .id(("bind", i))
933 .flex()936 .flex()
934- .h(px(KEYS_ROW_H))937+ // The floor, not the height: a row that needed two lines
938+ // would otherwise paint over the one under it.
939+ .min_h(px(KEYS_ROW_H))
935 .items_center()940 .items_center()
936 .justify_between()941 .justify_between()
937 .gap(px(12.))942 .gap(px(12.))
@@ -941,16 +946,16 @@ impl Player {
941 .hover(|s| s.bg(rgb(HOVER)))946 .hover(|s| s.bg(rgb(HOVER)))
942 .when(capturing, |d| d.bg(rgb(SELECTED)))947 .when(capturing, |d| d.bg(rgb(SELECTED)))
943 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {948 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
944- this.rebinding = Some(i);949+ this.rebinding = Some(action);
945 cx.notify();950 cx.notify();
946 }))951 }))
947- .child(binding.action.label())952+ .child(action.label())
948 .child(if capturing {953 .child(if capturing {
949 div()954 div()
950 .text_color(rgb(INK_DIM))955 .text_color(rgb(INK_DIM))
951 .child(format!("press a key — {out} cancels"))956 .child(format!("press a key — {out} cancels"))
952 } else {957 } else {
953- div().child(binding.chord.pretty())958+ div().child(self.keymap.display(action))
954 })959 })
955 })960 })
956 .collect();961 .collect();
@@ -976,22 +981,31 @@ impl Player {
976 .p(px(12.))981 .p(px(12.))
977 .rounded(px(6.))982 .rounded(px(6.))
978 .bg(rgb(SURFACE))983 .bg(rgb(SURFACE))
984+ // Title and instruction are two children, never one
985+ // wrapping line: a fixed-height slot whose text wrapped
986+ // painted its second line over the first row.
987+ .child(div().flex_none().px(px(6.)).child("Keybindings"))
988+ // One status line, under the title where the eye starts.
989+ // A refusal takes it over -- it is the more urgent of the
990+ // two, and the notice bar it would otherwise appear in is
991+ // under the scrim.
992+ //
993+ // ponytail: a refusal long enough to wrap to three lines
994+ // pushes the card past a 360 px tall window. The upgrade
995+ // path is a scrolling row list, not a shorter message.
979 .child(996 .child(
980 div()997 div()
981- .h(px(KEYS_ROW_H))998+ .flex_none()
982- .px(px(6.))
983- .child("Keybindings — click a row, then press a key"),
984- )
985- .children(rows)
986- // A refusal belongs where the click was: the notice bar
987- // itself is under the scrim.
988- .children(self.notice.clone().map(|notice| {
989- div()
990 .px(px(6.))999 .px(px(6.))
991 .text_size(px(11.))1000 .text_size(px(11.))
992 .text_color(rgb(INK_DIM))1001 .text_color(rgb(INK_DIM))
993- .child(notice)1002+ .child(
994- })),1003+ self.notice
1004+ .clone()
1005+ .unwrap_or_else(|| "click a row, then press a key".into()),
1006+ ),
1007+ )
1008+ .children(rows),
995 ),1009 ),
996 )1010 )
997 }1011 }
@@ -1467,12 +1481,22 @@ mod tests {
1467 1481
1468 #[test]1482 #[test]
1469 fn the_keybindings_card_fits_the_smallest_window() {1483 fn the_keybindings_card_fits_the_smallest_window() {
1470- // A title line plus a row per default binding, inside the 640x360 the1484+ // A row per action -- not per binding, which is why the count is
1471- // rest of the layout is already sized for.1485+ // `ALL` -- under a title and a status line, inside the 640x360 the rest
1472- let lines = keymap::Keymap::defaults().entries().len() as f32 + 1.;1486+ // of the layout is already sized for.
1473- // Rows, their 2 px gaps, the card's 12 px padding top and bottom.1487+ let rows = keymap::ActionId::ALL.len() as f32;
1474- assert!(lines * (KEYS_ROW_H + 2.) + 24. <= 360., "card too tall");1488+ let title = 17.; // 13 px text on its own line
1489+ let status = 28.; // 11 px text, two lines: a refusal wraps
1490+ let gaps = (rows + 1.) * 2.;
1491+ let padding = 24.;
1492+ assert!(
1493+ title + status + rows * KEYS_ROW_H + gaps + padding <= 360.,
1494+ "card too tall"
1495+ );
1475 assert!(KEYS_W <= 640., "card too wide");1496 assert!(KEYS_W <= 640., "card too wide");
1497+ // The rows are clickable, so WCAG 2.5.8 binds them like every other
1498+ // target in this window.
1499+ assert!(KEYS_ROW_H >= HIT_MIN);
1476 }1500 }
1477 1501
1478 #[test]1502 #[test]