From 5992a2f3039e33e0ce7aefd13aa9c0e788e3306d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 6 Dec 2026 12:33:25 +0100 Subject: [PATCH] Simplify Alt+Down key guard and add tests for surviving mutants --- crates/thing-core/src/input/keys.rs | 28 ++++---- crates/thing-core/src/input/session.rs | 1 - crates/thing-core/tests/chooser.rs | 97 +++++++++++++++++++++++--- 3 files changed, 101 insertions(+), 25 deletions(-) diff --git a/crates/thing-core/src/input/keys.rs b/crates/thing-core/src/input/keys.rs index 09c5b10..ceb8161 100644 --- a/crates/thing-core/src/input/keys.rs +++ b/crates/thing-core/src/input/keys.rs @@ -26,13 +26,24 @@ pub(super) fn key( owners: &mut Owners<'_>, now: Timestamp, ) -> Vec { - let allow_alt_down = modifiers.alt && !modifiers.ctrl && !modifiers.meta && !modifiers.shift && key == Key::Down; - if modifiers.ctrl || modifiers.meta || (modifiers.alt && !allow_alt_down) { + if modifiers.ctrl || modifiers.meta { + return Vec::new(); + } + if modifiers.alt { + if !modifiers.shift + && key == Key::Down + && session.is_none() + && !repeat + && let Some(focused) = focus.node().cloned() + && matches!(seen.tree.index().get(&focused).copied(), Some(Node::Select(_))) + { + return chooser::open(session, focused, seen, owners); + } return Vec::new(); } let index = seen.tree.index(); let floor = hit::modal_floor(seen.laid, &index); - if key == Key::Escape && !modifiers.alt { + if key == Key::Escape { if session.is_some() { if !repeat { *session = None; @@ -48,15 +59,12 @@ pub(super) fn key( } return Vec::new(); } - if key == Key::Tab && !modifiers.alt { + if key == Key::Tab { *session = None; focus.step(modifiers.shift, seen.laid); return Vec::new(); } if session.is_some() { - if modifiers.alt { - return Vec::new(); - } return chooser::key(session, key, repeat, seen, owners, now); } let Some(focused) = focus.node().cloned() else { @@ -65,12 +73,6 @@ pub(super) fn key( let Some(node) = index.get(&focused).copied() else { return Vec::new(); }; - if modifiers.alt { - if matches!(node, Node::Select(_)) && !repeat { - return chooser::open(session, focused, seen, owners); - } - return Vec::new(); - } match node { Node::Button(_) if matches!(key, Key::Enter | Key::Char(' ')) => { if repeat { diff --git a/crates/thing-core/src/input/session.rs b/crates/thing-core/src/input/session.rs index 951e577..d63f5a8 100644 --- a/crates/thing-core/src/input/session.rs +++ b/crates/thing-core/src/input/session.rs @@ -91,7 +91,6 @@ impl SessionOverlay { (row_id, Row { value, label }) }) .collect(); - let selected = selected.filter(|&i| i < rows.len()); let highlight = selected.unwrap_or(0); let reveal = !rows.is_empty(); Self { owner, kind: SessionKind::Select, rows, highlight, selected, reveal } diff --git a/crates/thing-core/tests/chooser.rs b/crates/thing-core/tests/chooser.rs index 99a1972..ff5ebfb 100644 --- a/crates/thing-core/tests/chooser.rs +++ b/crates/thing-core/tests/chooser.rs @@ -328,21 +328,37 @@ fn keys_open_move_highlight_choose_and_close_without_activating() { let mut host = Host::new(MemState::new(), &doc, mode); // Focus "pick". host.frame(vec![tab(false)]); - // Repeat must not open. - let rep = host.frame(vec![key_repeat(Key::Enter)]); - assert!(surface_fills(&rep).is_empty(), "{mode:?}"); + // Non-trigger keys, repeat, and disallowed modifier combos must not open. + for bad in [ + key_repeat(Key::Enter), + key(Key::Left), + key(Key::Up), + key(Key::Char('a')), + InputEvent::Key { key: Key::Enter, modifiers: Modifiers { alt: true, ..mods() }, repeat: false }, + InputEvent::Key { key: Key::Up, modifiers: Modifiers { alt: true, ..mods() }, repeat: false }, + InputEvent::Key { key: Key::Down, modifiers: Modifiers { alt: true, shift: true, ..mods() }, repeat: false }, + InputEvent::Key { key: Key::Down, modifiers: Modifiers { alt: true, ctrl: true, ..mods() }, repeat: false }, + InputEvent::Key { key: Key::Down, modifiers: Modifiers { alt: true, meta: true, ..mods() }, repeat: false }, + ] { + let rep = host.frame(vec![bad.clone()]); + assert!(surface_fills(&rep).is_empty(), "{mode:?} {bad:?}"); + } // Open with each trigger key. let opened = host.frame(vec![open_key.clone()]); assert_eq!(surface_fills(&opened).len(), 1, "{mode:?} {open_key:?}"); assert!(opened.outbound.is_empty(), "{mode:?}"); + // Alt+Down while already open stays inside chooser scope and leaves it open. + let alt_open = host.frame(vec![alt_down()]); + assert_eq!(surface_fills(&alt_open).len(), 1, "{mode:?}"); + assert!(alt_open.outbound.is_empty(), "{mode:?}"); // Escape closes without choosing. let esc = host.frame(vec![key(Key::Escape)]); assert!(surface_fills(&esc).is_empty(), "{mode:?}"); assert!(esc.outbound.is_empty(), "{mode:?}"); - // Re-open, move Down, End, Home, Down, and Space to choose "Beta". + // Re-open, move End, Home, Down, Down, Up -> Space chooses "Beta". host.frame(vec![open_key.clone()]); let moved_end = host.frame(vec![key(Key::End)]); let end_sel = selection_rects(&moved_end)[0]; @@ -350,7 +366,7 @@ fn keys_open_move_highlight_choose_and_close_without_activating() { let home_sel = selection_rects(&moved_home)[0]; assert!(end_sel.y > home_sel.y, "{mode:?}: End moves below Home"); - host.frame(vec![key(Key::Down)]); + host.frame(vec![key(Key::Down), key(Key::Down), key(Key::Up)]); // Repeat Enter must not choose. let rep_choose = host.frame(vec![key_repeat(Key::Enter)]); assert_eq!(surface_fills(&rep_choose).len(), 1, "{mode:?}: repeat Enter does not choose"); @@ -423,16 +439,75 @@ fn scrollable_chooser_reveals_highlight_on_keys_and_alt_down_does_not_leak() { let opt14 = texts_end.iter().find(|(_, _, t)| t == "Opt 14").expect("Opt 14 drawn"); assert!(opt14.1 >= list_rect.y && opt14.1 < list_rect.y + list_rect.h, "{mode:?}: Opt 14 revealed in {list_rect:?}: {opt14:?}"); - // PageUp moves back by one visible page and keeps highlight inside list_rect. + // PageUp moves back by one visible page (> 1 row) and keeps highlight inside list_rect. let page_up = host.frame(vec![key(Key::PageUp)]); let sel_after_pgup = selection_rects(&page_up)[0]; assert!(sel_after_pgup.y >= list_rect.y && sel_after_pgup.y + sel_after_pgup.h <= list_rect.y + list_rect.h, "{mode:?}"); + let pgup_chosen = host.frame(vec![key(Key::Enter)]); + let InputValue::Choice(ChoiceValue { value: pgup_val }) = &pgup_chosen.outbound[0].captured[0].value else { + panic!("expected choice"); + }; + let pgup_idx: usize = pgup_val.strip_prefix('v').unwrap().parse().unwrap(); + let expected_page = match mode.0 { + Metrics::Pixels => 6, + Metrics::Cells => 5, + }; + assert_eq!(pgup_idx, 14 - expected_page, "{mode:?}: PageUp from 14 by {expected_page}"); - // PageDown returns toward the end; Up/Down move smoothly and Enter chooses current highlight. - host.frame(vec![key(Key::PageDown), key(Key::Up)]); - let chosen = host.frame(vec![key(Key::Enter)]); - assert_eq!(chosen.outbound.len(), 1, "{mode:?}"); - assert_ne!(chosen.outbound[0].captured[0].value, InputValue::Choice(ChoiceValue { value: "v0".into() }), "{mode:?}"); + // Now v(14 - expected_page) is drafted; opening again immediately reveals that pre-selected + // row, and Home -> Down -> PageDown lands at 1 + expected_page. + let reopened = host.frame(vec![key(Key::Enter)]); + let expected_label = format!("Opt {pgup_idx:02} *"); + let pre_row = drawn_texts(&reopened).into_iter().find(|(_, _, t)| t == &expected_label).expect("pre-selected revealed"); + assert!(pre_row.1 >= list_rect.y && pre_row.1 < list_rect.y + list_rect.h, "{mode:?}: {pre_row:?}"); + + host.frame(vec![key(Key::Home), key(Key::Down), key(Key::PageDown)]); + let pgdn_chosen = host.frame(vec![key(Key::Enter)]); + let expected_pgdn = format!("v{}", 1 + expected_page); + assert_eq!(pgdn_chosen.outbound[0].captured[0].value, InputValue::Choice(ChoiceValue { value: expected_pgdn }), "{mode:?}"); + } +} + +#[test] +fn overflow_boundary_in_pixels_and_cells_respects_room_below() { + let make_doc = |count: usize, with_spacer: bool| { + let opts: Vec<_> = (0..count).map(|i| (format!("v{i}"), format!("Opt {i}"))).collect(); + let opt_refs: Vec<_> = opts.iter().map(|(v, l)| (v.as_str(), l.as_str())).collect(); + let mut children = Vec::new(); + if with_spacer { + children.push(fixed("spacer", 160.0, 80.0)); + } + children.push(select_node("pick", opt_refs, "choose")); + surface(column("root", None, Overflow::Visible, children), vec![decl("choose", &["pick"])]) + }; + let has_clip = |out: &FrameOutput| out.commands.iter().any(|c| matches!(c, RenderCommand::ClipPush { .. })); + let has_thumb = |out: &FrameOutput| { + let thumb = Theme::std().color("thing.scrollbar.thumb").unwrap(); + out.commands.iter().any(|c| matches!(c, RenderCommand::FillRect { color, .. } if *color == thumb)) + }; + + // Pixels: viewport height 160, 80px spacer above pick => room_below = 80px. + // 4 rows = 66px <= 80px (no scroll); 6 rows = 98px > 80px (scrolls; 160+80 would not). + for (count, expect_scroll) in [(4, false), (6, true)] { + let doc = make_doc(count, true); + let mut host = Host::new(MemState::new(), &doc, MODES[0]); + host.vp = (400.0, 160.0); + host.frame(vec![]); + let opened = host.frame(vec![tab(false), key(Key::Enter)]); + assert_eq!(has_thumb(&opened), expect_scroll, "pixels count={count}"); + assert_eq!(has_clip(&opened), expect_scroll, "pixels clip count={count}"); + } + + // Cells: viewport height 5 cells (80px), pick at y=0 => room = 5 cells. + // 3 rows + 2 border = 5 cells (exact fit, no clip/scroll); 4 rows + 2 border = 6 cells > 5 (scrolls). + for (count, expect_scroll) in [(3, false), (4, true)] { + let doc = make_doc(count, false); + let mut host = Host::new(MemState::new(), &doc, MODES[2]); + host.vp = (40.0, 5.0); + host.frame(vec![]); + let opened = host.frame(vec![tab(false), key(Key::Enter)]); + assert_eq!(has_thumb(&opened), expect_scroll, "cells thumb count={count}"); + assert_eq!(has_clip(&opened), expect_scroll, "cells clip count={count}"); } }