Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -52,6 +54,9 @@ pub struct ActionSheet {
pub message: Option<String>,
pub actions: Vec<SheetAction>,
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<usize>,
}

impl ActionSheet {
Expand All @@ -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<Option<usize>> {
let h = self.hot?;
match self.actions.get(h) {
Some(a) if a.enabled => Some(Some(a.id)),
Some(_) => None,
None => Some(None),
}
}

Expand All @@ -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));
}
}
52 changes: 42 additions & 10 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,17 @@ 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();
w.menu = Some((menu, Rc::new(move |i| (pick.borrow_mut())(i))));
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();
Expand All @@ -199,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<usize>) {
let mut l = self.layer;
let choose = l.write().actions.take().map(|(_, c)| c);
Expand Down Expand Up @@ -268,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;
}
Expand Down Expand Up @@ -639,11 +668,13 @@ fn ActionsView(unit: Unit, sheet: Option<ActionSheet>, 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| {
Expand All @@ -657,16 +688,17 @@ fn ActionsView(unit: Unit, sheet: Option<ActionSheet>, 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;
Expand Down
53 changes: 39 additions & 14 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -42,6 +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
/// 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: 24.0,
char: 9.7,
min_width: 160.0,
margin: 0.0,
overlap: 9.7,
};
}

Expand All @@ -50,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)]
Expand Down Expand Up @@ -276,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],
Expand Down Expand Up @@ -318,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
Expand Down Expand Up @@ -488,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.
Expand Down
28 changes: 28 additions & 0 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -87,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: 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; }
.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;
Expand Down Expand Up @@ -410,5 +429,14 @@ 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))));
// 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
))));
}
}
Loading
Loading