From 32b25b773f995708414f6c2f4542cd42a0809fb2 Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Wed, 21 Oct 2026 15:17:56 -0600 Subject: [PATCH 1/5] feat(theme): a cell density, for a renderer that paints into a terminal's cells Density::Cell makes every row a menu or a dialog has one cell high (24px, Metrics::CELL and menu::Sizes::CELL, kept in step with the stylesheet by the existing test), with square corners and no shadows. A menu's padding leaves a row above and below its items for the border. The host sets Theme::font to its monospace face. Theme gains a `cell` field, so a Theme written out field by field needs it added. --- src/layer.rs | 1 + src/menu.rs | 11 +++++++++++ src/style.rs | 16 ++++++++++++++++ src/theme.rs | 24 ++++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/src/layer.rs b/src/layer.rs index 39f6ea3..3ad84d0 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -165,6 +165,7 @@ impl Unit { let sizes = match *self.density.peek() { crate::Density::Regular => Sizes::REGULAR, crate::Density::Touch => Sizes::TOUCH, + crate::Density::Cell => Sizes::CELL, }; let menu = Menu::open(entries, at, above, viewport, sizes); let mut w = l.write(); diff --git a/src/menu.rs b/src/menu.rs index 980f3ae..6c96779 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -43,6 +43,17 @@ impl Sizes { char: 9.0, min_width: 200.0, }; + + /// For a terminal's cells: an item and a separator a row each, and a + /// padding that with the 1px border makes a row above and below, where + /// the border is drawn. `char` is a 16px monospace face's advance. + pub const CELL: Sizes = Sizes { + item: 24.0, + separator: 24.0, + pad: 23.0, + char: 9.7, + min_width: 160.0, + }; } const MAX_WIDTH: f64 = 380.0; diff --git a/src/style.rs b/src/style.rs index a4920b7..7d591f5 100644 --- a/src/style.rs +++ b/src/style.rs @@ -46,6 +46,12 @@ pub const CSS: &str = r#" .unit-root[data-density="touch"] .unit-item { height: 40px; line-height: 40px; border-radius: 8px; } .unit-root[data-density="touch"] .unit-tick { width: 28px; } .unit-root[data-density="touch"] .unit-sep { height: 13px; padding: 6px 12px; } +.unit-root[data-density="cell"] { line-height: 24px; } +.unit-root[data-density="cell"] .unit-menu { padding: 23px 0; font-size: inherit; border-radius: 0; box-shadow: none; } +.unit-root[data-density="cell"] .unit-item { height: 24px; line-height: 24px; margin: 0; padding: 0 10px 0 0; border-radius: 0; } +.unit-root[data-density="cell"] .unit-tick { width: 20px; } +.unit-root[data-density="cell"] .unit-key { padding-left: 20px; } +.unit-root[data-density="cell"] .unit-sep { height: 24px; padding: 11px 10px; } /* The overlay's dialog */ .unit-dialog-place { position: absolute; left: 0; top: 0; right: 0; bottom: 0; @@ -58,6 +64,11 @@ pub const CSS: &str = r#" .unit-dialog-message { margin-bottom: 10px; } .unit-dialog .unit-input { margin: 4px 0 14px; } .unit-dialog-buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; } +.unit-root[data-density="cell"] .unit-dialog { padding: 23px 20px; box-shadow: none; } +.unit-root[data-density="cell"] .unit-dialog-title { margin-bottom: 0; } +.unit-root[data-density="cell"] .unit-dialog-message { margin: 24px 0 0; } +.unit-root[data-density="cell"] .unit-dialog .unit-input { margin: 24px 0 0; } +.unit-root[data-density="cell"] .unit-dialog-buttons { gap: 20px; margin-top: 24px; } .unit-field { line-height: var(--unit-control-height); white-space: pre; overflow: hidden; } .unit-field.all { background: var(--unit-selection); } .unit-caret { display: inline-block; width: 1px; height: 16px; vertical-align: middle; @@ -410,5 +421,10 @@ mod tests { assert!(CSS.contains(&format!(".unit-item {{ height: {}px", t.item))); assert!(CSS.contains(&format!(".unit-sep {{ height: {}px", t.separator))); assert!(CSS.contains(&format!(".unit-menu {{ padding: {}px 0", t.pad))); + let c = Sizes::CELL; + let cell = |rule: &str| format!(".unit-root[data-density=\"cell\"] {rule}"); + assert!(CSS.contains(&cell(&format!(".unit-item {{ height: {}px", c.item)))); + assert!(CSS.contains(&cell(&format!(".unit-sep {{ height: {}px", c.separator)))); + assert!(CSS.contains(&cell(&format!(".unit-menu {{ padding: {}px 0", c.pad)))); } } diff --git a/src/theme.rs b/src/theme.rs index f1f265a..c2ee926 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -187,6 +187,17 @@ impl Metrics { radius_large: 14.0, }; + /// For a terminal's cells ([`Density::Cell`]): a control a row high, + /// text a monospace face's 16px, a gap of about one cell's width, and + /// square corners. + pub const CELL: Metrics = Metrics { + control: 24.0, + font_size: 16.0, + gap: 10.0, + radius: 0.0, + radius_large: 0.0, + }; + fn entries(&self) -> [(&'static str, f64); 5] { [ ("control-height", self.control), @@ -227,6 +238,13 @@ pub enum Density { Regular, /// For a finger. Touch, + /// For a terminal's character cells: a renderer that paints the page + /// into a grid (leaf's terminal host) rather than pixels. Every row a + /// menu or a dialog has is one cell high — [`Metrics::CELL`]'s control, + /// 24px — with no rounded corners or shadows, which cells cannot draw. + /// The host gives the theme its monospace [`Theme::font`], whose advance + /// is its cell. + Cell, } impl Density { @@ -235,6 +253,7 @@ impl Density { match self { Density::Regular => "regular", Density::Touch => "touch", + Density::Cell => "cell", } } } @@ -246,6 +265,7 @@ pub struct Theme { pub dark: Palette, pub regular: Metrics, pub touch: Metrics, + pub cell: Metrics, /// The font stack. pub font: &'static str, /// The headings' font stack: a heading, a card's and a dialog's title. @@ -263,6 +283,7 @@ impl Default for Theme { dark: Palette::DARK, regular: Metrics::REGULAR, touch: Metrics::TOUCH, + cell: Metrics::CELL, font: SYSTEM_FONT, heading_font: SYSTEM_FONT, } @@ -302,6 +323,9 @@ impl Theme { s.push_str(".unit-root[data-density=\"touch\"] {\n"); metrics(&mut s, &self.touch); s.push_str("}\n"); + s.push_str(".unit-root[data-density=\"cell\"] {\n"); + metrics(&mut s, &self.cell); + s.push_str("}\n"); s } } From b15d9eee2aa557f5ebd4db891e5e1462eba9d6ec Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Wed, 21 Oct 2026 15:23:39 -0600 Subject: [PATCH 2/5] feat(menu): a menu's margin and submenu overlap are its density's Sizes gains margin and overlap, 4px each for the pointer and touch densities as before. The cell density keeps to the grid: no margin, a submenu overlapping its parent by one cell, and a pad that counts the 1px border, so a level is exactly as tall as its rows. Sizes gains two fields, so a Sizes written out field by field needs them added. --- src/menu.rs | 48 +++++++++++++++++++++++++++++++----------------- src/style.rs | 6 +++++- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/menu.rs b/src/menu.rs index 6c96779..d66d479 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -24,6 +24,10 @@ pub struct Sizes { /// An average character of the menus' face, for estimating width. pub char: f64, pub min_width: f64, + /// How far a level is kept off the viewport's edges. + pub margin: f64, + /// How far a submenu overlaps its parent, as the system's do. + pub overlap: f64, } impl Sizes { @@ -33,6 +37,8 @@ impl Sizes { pad: 5.0, char: 7.0, min_width: 160.0, + margin: 4.0, + overlap: 4.0, }; /// For a finger: rows tall enough to tap. @@ -42,17 +48,24 @@ impl Sizes { pad: 6.0, char: 9.0, min_width: 200.0, + margin: 4.0, + overlap: 4.0, }; /// For a terminal's cells: an item and a separator a row each, and a - /// padding that with the 1px border makes a row above and below, where - /// the border is drawn. `char` is a 16px monospace face's advance. + /// row above and below the items, where the border is drawn — `pad` is + /// the stylesheet's 23px padding and the 1px border together, so a level + /// is exactly as tall as its rows. `char` is a 16px monospace face's + /// advance. A level keeps to the grid: no margin off the edges, and a + /// submenu overlapping its parent by one cell, so they share a border. pub const CELL: Sizes = Sizes { item: 24.0, separator: 24.0, - pad: 23.0, + pad: 24.0, char: 9.7, min_width: 160.0, + margin: 0.0, + overlap: 9.7, }; } @@ -61,10 +74,6 @@ const MAX_WIDTH: f64 = 380.0; const GUTTERS: f64 = 44.0; /// An item's icon and the gap after it: `.unit-item .unit-svg`. const ICON: f64 = 22.0; -/// How far a submenu overlaps its parent, as the system's do. -const OVERLAP: f64 = 4.0; -/// A level kept this far off the viewport's edges. -const MARGIN: f64 = 4.0; /// One thing a menu shows. #[derive(Clone, Debug, PartialEq)] @@ -287,11 +296,12 @@ impl Menu { ) -> Self { let mut level = Level::new(entries, sizes); let (w, h) = (level.width, level.height()); - level.left = at.0.min(viewport.0 - w - MARGIN).max(MARGIN); - level.top = if at.1 + h > viewport.1 - MARGIN && at.1 - above - h > MARGIN { + let margin = sizes.margin; + level.left = at.0.min(viewport.0 - w - margin).max(margin); + level.top = if at.1 + h > viewport.1 - margin && at.1 - above - h > margin { at.1 - above - h } else { - at.1.min(viewport.1 - h - MARGIN).max(MARGIN) + at.1.min(viewport.1 - h - margin).max(margin) }; Self { levels: vec![level], @@ -329,17 +339,18 @@ impl Menu { }; let mut level = Level::new(entries.clone(), parent.sizes); let (w, h) = (level.width, level.height()); - let right = parent.left + parent.width - OVERLAP; - level.left = if right + w > self.viewport.0 - MARGIN { + let z = parent.sizes; + let right = parent.left + parent.width - z.overlap; + level.left = if right + w > self.viewport.0 - z.margin { // No room to the right: to the left of the parent instead. - (parent.left - w + OVERLAP).max(MARGIN) + (parent.left - w + z.overlap).max(z.margin) } else { right }; // Its first item level with the parent's. - level.top = (parent.top + parent.entry_top(i) - parent.sizes.pad) - .min(self.viewport.1 - h - MARGIN) - .max(MARGIN); + level.top = (parent.top + parent.entry_top(i) - z.pad) + .min(self.viewport.1 - h - z.margin) + .max(z.margin); self.levels.truncate(depth + 1); self.levels.push(level); true @@ -499,7 +510,10 @@ mod tests { m.hover(0, 3); assert_eq!(m.levels.len(), 2); let (parent, sub) = (&m.levels[0], &m.levels[1]); - assert_eq!(sub.left, parent.left + parent.width - OVERLAP); + assert_eq!( + sub.left, + parent.left + parent.width - Sizes::REGULAR.overlap + ); assert_eq!(sub.top, parent.top + parent.entry_top(3) - parent.sizes.pad); assert_eq!(m.release(1, 1), Outcome::Pick(11)); // Back to the parent: the submenu closes. diff --git a/src/style.rs b/src/style.rs index 7d591f5..4ca1cee 100644 --- a/src/style.rs +++ b/src/style.rs @@ -425,6 +425,10 @@ mod tests { let cell = |rule: &str| format!(".unit-root[data-density=\"cell\"] {rule}"); assert!(CSS.contains(&cell(&format!(".unit-item {{ height: {}px", c.item)))); assert!(CSS.contains(&cell(&format!(".unit-sep {{ height: {}px", c.separator)))); - assert!(CSS.contains(&cell(&format!(".unit-menu {{ padding: {}px 0", c.pad)))); + // The cell density's pad is its padding and its 1px border. + assert!(CSS.contains(&cell(&format!( + ".unit-menu {{ padding: {}px 0", + c.pad - 1.0 + )))); } } From 1e082ca0fe0d374482fe0e9c963dacc2acaf1813 Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Wed, 21 Oct 2026 15:25:30 -0600 Subject: [PATCH 3/5] feat(menu): Unit::hot_first is public, for a host that opens a menu from a key --- src/layer.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/layer.rs b/src/layer.rs index 3ad84d0..f5ea1e3 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -173,8 +173,9 @@ impl Unit { w.owner = (owner != 0).then_some(owner); } - /// Highlight the open menu's first item, as a menu opened by a key does. - pub(crate) fn hot_first(&self) { + /// Highlight the open menu's first item, as a menu opened by a key does: + /// for a host that opens [`Unit::open_menu`] from a key of its own. + pub fn hot_first(&self) { let mut l = self.layer; if let Some((m, _)) = l.write().menu.as_mut() { m.hot_first(); From b70dc039feaaa4084e33e18034d79493c7f4571d Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Wed, 21 Oct 2026 15:31:57 -0600 Subject: [PATCH 4/5] feat(actions): choose an action sheet's action from the keyboard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ↑ and ↓ (or Tab and ⇧Tab) move a highlight through the sheet's actions that can be chosen and its Cancel, round from the end; Return or Space chooses the one highlighted. Until a key moves it nothing is highlighted, so Return on a sheet just opened still does nothing. The cell density centres the sheet and gives each action one row. ActionSheet gains a `hot` field, so a sheet written out field by field needs it added. Behavioural-change: an open action sheet acts on ↑, ↓, Tab, Return and Space, which it used to swallow. --- src/actions.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++- src/layer.rs | 46 ++++++++++++++++++++++++++------ src/style.rs | 8 ++++++ 3 files changed, 116 insertions(+), 9 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index 2bd137e..6cd9db2 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -2,7 +2,9 @@ //! bottom of the page with Cancel apart under them, for what a long press or //! a "⋯" offers. It is drawn by the overlay (`layer.rs`), in the tree from //! the first frame like the overlay's menus and dialog, and like them takes -//! every key while it is open: Escape cancels it. +//! every key while it is open: Escape cancels it, ↑ and ↓ (or Tab) move a +//! highlight through the actions and Cancel, and Return or Space chooses the +//! one highlighted — so a sheet can be answered from a keyboard. /// One action an [`ActionSheet`] offers: its `id` is what the sheet's pick /// handler is given. @@ -52,6 +54,9 @@ pub struct ActionSheet { pub message: Option, pub actions: Vec, pub cancel: String, + /// The row the keys have highlighted: an action's index, or + /// `actions.len()` for Cancel. None until a key moves it. + pub hot: Option, } impl ActionSheet { @@ -61,6 +66,41 @@ impl ActionSheet { message: None, actions, cancel: "Cancel".into(), + hot: None, + } + } + + /// Move the highlight down (or up) to the next action that can be + /// chosen, or to Cancel, round from the end to the start. + pub fn step(&mut self, down: bool) { + let rows = self.actions.len() + 1; + let usable = |i: usize| i == self.actions.len() || self.actions[i].enabled; + let mut i = match (self.hot, down) { + (None, true) => rows - 1, + (None, false) => 0, + (Some(h), _) => h, + }; + for _ in 0..rows { + i = if down { + (i + 1) % rows + } else { + (i + rows - 1) % rows + }; + if usable(i) { + self.hot = Some(i); + return; + } + } + } + + /// What Return chooses: `Some(Some(id))` for a highlighted action, + /// `Some(None)` for Cancel, `None` with nothing highlighted. + pub fn chosen(&self) -> Option> { + let h = self.hot?; + match self.actions.get(h) { + Some(a) if a.enabled => Some(Some(a.id)), + Some(_) => None, + None => Some(None), } } @@ -79,3 +119,32 @@ impl ActionSheet { self } } + +#[cfg(test)] +mod tests { + use super::*; + + fn sheet() -> ActionSheet { + ActionSheet::new(vec![ + SheetAction::new(7, "Save"), + SheetAction::new(8, "Off").enabled(false), + SheetAction::new(9, "Don't Save").destructive(), + ]) + } + + #[test] + fn the_keys_step_past_what_cannot_be_chosen_and_round_to_cancel() { + let mut s = sheet(); + assert_eq!(s.chosen(), None); + s.step(true); + assert_eq!(s.chosen(), Some(Some(7))); + s.step(true); + assert_eq!(s.chosen(), Some(Some(9))); + s.step(true); + assert_eq!(s.chosen(), Some(None)); + s.step(true); + assert_eq!(s.chosen(), Some(Some(7))); + s.step(false); + assert_eq!(s.chosen(), Some(None)); + } +} diff --git a/src/layer.rs b/src/layer.rs index f5ea1e3..7afb170 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -201,6 +201,26 @@ impl Unit { w.actions = Some((sheet, Rc::new(move |a| (choose.borrow_mut())(a)))); } + fn step_actions(&self, down: bool) { + let mut l = self.layer; + if let Some((s, _)) = l.write().actions.as_mut() { + s.step(down); + } + } + + /// Choose what the keys have highlighted, if anything. + fn choose_hot(&self) { + let chosen = self + .layer + .peek() + .actions + .as_ref() + .and_then(|(s, _)| s.chosen()); + if let Some(c) = chosen { + self.choose(c); + } + } + fn choose(&self, action: Option) { let mut l = self.layer; let choose = l.write().actions.take().map(|(_, c)| c); @@ -270,8 +290,15 @@ impl Unit { return self.settle(outcome); } if l.peek().actions.is_some() { - if matches!(key, Key::Named(NamedKey::Escape)) { - self.choose(None); + let shift = mods.contains(Modifiers::SHIFT); + match key { + Key::Named(NamedKey::Escape) => self.choose(None), + Key::Named(NamedKey::ArrowDown) => self.step_actions(true), + Key::Named(NamedKey::ArrowUp) => self.step_actions(false), + Key::Named(NamedKey::Tab) => self.step_actions(!shift), + Key::Named(NamedKey::Enter) => self.choose_hot(), + Key::Character(c) if c == " " => self.choose_hot(), + _ => {} } return true; } @@ -641,11 +668,13 @@ fn ActionsView(unit: Unit, sheet: Option, bottom: f64) -> Element { } } } - for a in s.actions.iter().cloned() { - {action_view(unit, a)} + for (i, a) in s.actions.iter().cloned().enumerate() { + {action_view(unit, a, s.hot == Some(i))} } } - button { class: "unit-actions-cancel", r#type: "button", + button { + class: if s.hot == Some(s.actions.len()) { "unit-actions-cancel hot" } else { "unit-actions-cancel" }, + r#type: "button", "aria-label": "{s.cancel}", onpointerdown: move |e: PointerEvent| e.stop_propagation(), onclick: move |e: MouseEvent| { @@ -659,16 +688,17 @@ fn ActionsView(unit: Unit, sheet: Option, bottom: f64) -> Element { } } -fn action_view(unit: Unit, a: SheetAction) -> Element { +fn action_view(unit: Unit, a: SheetAction, hot: bool) -> Element { let ink = match () { _ if !a.enabled => Ink::Dim, _ if a.destructive => Ink::Danger, _ => Ink::Accent, }; let class = format!( - "unit-action{}{}", + "unit-action{}{}{}", if a.destructive { " danger" } else { "" }, - if a.enabled { "" } else { " off" } + if a.enabled { "" } else { " off" }, + if hot { " hot" } else { "" } ); let id = a.id; let enabled = a.enabled; diff --git a/src/style.rs b/src/style.rs index 4ca1cee..1ecfe47 100644 --- a/src/style.rs +++ b/src/style.rs @@ -98,6 +98,14 @@ pub const CSS: &str = r#" .unit-action:hover, .unit-actions-cancel:hover { background: var(--unit-control-hover); } .unit-actions-cancel { font-weight: 700; background: var(--unit-surface); border: 1px solid var(--unit-border); border-radius: var(--unit-radius-large); } +.unit-action.hot, .unit-actions-cancel.hot { background: var(--unit-accent); color: var(--unit-accent-text); } +.unit-action.danger.hot { background: var(--unit-danger); color: var(--unit-accent-text); } +.unit-root[data-density="cell"] .unit-actions-place { justify-content: center; padding: 24px 20px; } +.unit-root[data-density="cell"] .unit-actions { gap: 24px; } +.unit-root[data-density="cell"] .unit-actions-group { border-radius: 0; padding-bottom: 23px; } +.unit-root[data-density="cell"] .unit-actions-head { padding: 23px 20px 24px; font-size: inherit; border-bottom: 0; } +.unit-root[data-density="cell"] .unit-action, .unit-root[data-density="cell"] .unit-actions-cancel { + min-height: 24px; height: 24px; border-top: 0; border-radius: 0; } /* Toasts */ .unit-toasts { position: absolute; left: 0; right: 0; bottom: 16px; display: flex; From e8ff6d2a1bee4616c686dfbb29eb682f017f665a Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Wed, 21 Oct 2026 15:33:08 -0600 Subject: [PATCH 5/5] fix(actions): a cell-density sheet stands three rows down, on the grid --- src/style.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/style.rs b/src/style.rs index 1ecfe47..78a33d4 100644 --- a/src/style.rs +++ b/src/style.rs @@ -100,7 +100,7 @@ pub const CSS: &str = r#" border: 1px solid var(--unit-border); border-radius: var(--unit-radius-large); } .unit-action.hot, .unit-actions-cancel.hot { background: var(--unit-accent); color: var(--unit-accent-text); } .unit-action.danger.hot { background: var(--unit-danger); color: var(--unit-accent-text); } -.unit-root[data-density="cell"] .unit-actions-place { justify-content: center; padding: 24px 20px; } +.unit-root[data-density="cell"] .unit-actions-place { justify-content: flex-start; padding: 72px 20px 24px; } .unit-root[data-density="cell"] .unit-actions { gap: 24px; } .unit-root[data-density="cell"] .unit-actions-group { border-radius: 0; padding-bottom: 23px; } .unit-root[data-density="cell"] .unit-actions-head { padding: 23px 20px 24px; font-size: inherit; border-bottom: 0; }