From 43ccb39ba3f1ee35126866daa906f5f486d98371 Mon Sep 17 00:00:00 2001 From: Harrison Booth Date: Thu, 15 Oct 2026 04:17:26 -0500 Subject: [PATCH 01/10] Keyboard: F8 Extend Selection and Shift+F8 Add or Remove Selection F8 toggles Extend Selection: arrows, Page Up/Down, Ctrl+arrows, Home and Ctrl+Home/End extend from the anchor as if Shift were held, and a click extends like Shift+click. Shift+F8 toggles Add or Remove Selection: arrows move the active cell and keep the selected areas, Shift+arrows grow a new area from there, and a click adds an area or removes a selected one, like Ctrl+click. The status bar names the mode (translated in every catalog). The key again, Esc or editing a cell ends it. UI-only command ui.selectionMode sets or reports the mode for agents. Co-Authored-By: Claude Opus 5.5 --- ROADMAP.md | 4 +- crates/ui-egui/src/control.rs | 5 + crates/ui-egui/src/grid.rs | 130 ++++++++++++++++-- crates/ui-egui/src/i18n.rs | 2 + crates/ui-egui/src/i18n/cs.rs | 2 + crates/ui-egui/src/i18n/de.rs | 2 + crates/ui-egui/src/i18n/es.rs | 2 + crates/ui-egui/src/i18n/fr.rs | 2 + crates/ui-egui/src/i18n/ko.rs | 2 + crates/ui-egui/src/i18n/pt.rs | 2 + crates/ui-egui/src/i18n/ru.rs | 2 + crates/ui-egui/src/i18n/sr.rs | 2 + crates/ui-egui/src/i18n/zh.rs | 2 + crates/ui-egui/src/lib.rs | 14 ++ crates/ui-egui/src/tabs.rs | 5 + crates/ui-egui/tests/keyboard_shortcuts.rs | 149 ++++++++++++++++++++- docs/ui-parity.md | 8 +- 17 files changed, 321 insertions(+), 14 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 070552d07e..097e4c9f67 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-12 · **Change:** minor (rows auto-fit oversized fonts on open and whole-row/column formatting) · **Target:** Microsoft Excel (Microsoft 365) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-15 · **Change:** minor (F8 / Shift+F8 selection modes) · **Target:** Microsoft Excel (Microsoft 365) GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web. @@ -132,6 +132,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | What landed | |---|---| +| 2026-10-15 | Keyboard: F8 Extend Selection and Shift+F8 Add or Remove Selection: arrows and clicks extend from the anchor or build multi-area selections without holding Shift or Ctrl, the status bar names the mode, and Esc, the key again or editing a cell ends it | | 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) | | 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) | | 2026-10-12 | Number formats: a fraction denominator with 65,536 zeros no longer crashes; format codes are capped at 255 characters like Excel (longer codes display as General, `TEXT` gives `#VALUE!`) (#807) | @@ -193,6 +194,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | Date | Change | Summary | |---|---|---| +| 2026-10-15 | minor | Progress log: F8 and Shift+F8 selection modes | | 2026-10-12 | minor | Rows auto-fit oversized fonts on open and whole-row/column formatting (#833); progress log row | | 2026-10-12 | minor | Progress log: multi-area row/column delete (#758) | | 2026-10-12 | trivial | Progress log: XLSX chart sheets kept on save (#727) | diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 2ea5cfae01..6035323cf3 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -184,6 +184,11 @@ pub fn handle(app: &mut SheetApp, ctx: &egui::Context, method: &str, p: &Json) - ("options.autoComplete", "Enable AutoComplete for cell values", "{on?: bool} (toggles without it) → {on}"), ("view.interfaceScale", "Interface Size", "{percent: 75–200 | \"auto\"} (100 = auto; clamped)"), ("ui.dialog", "Open Dialog", ""), + ( + "ui.selectionMode", + "Extend or Add to Selection", + "{mode?: \"extend\" (F8) | \"add\" (Shift+F8) | \"off\"} → {mode}; {} reports the mode", + ), ("ui.backstage", "File (Backstage view)", "{page?: home|new|open|info|saveAs|print|export|options|close}"), ( "ui.printSettings", diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 97b787ef33..b5a536a6fc 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -149,6 +149,65 @@ pub struct GridState { pub hscroll: Option, /// Ctrl+wheel zoom in progress (see [`WheelZoom`]). pub wheel_zoom: Option, + /// F8 Extend Selection or Shift+F8 Add or Remove Selection mode (see [`SelectionMode`]). + pub selection_mode: SelectionMode, + /// Add mode: the last area is the cell an arrow key moved to, not yet extended, so the next + /// arrow moves it again instead of keeping it. + pub add_pending: bool, +} + +/// Excel's keyboard selection modes, shown in the status bar. In Extend mode (F8) arrow keys and +/// clicks extend from the anchor as if Shift were held. In Add mode (Shift+F8) arrow keys move the +/// active cell without dropping the selected areas, Shift+arrows grow a new area from there, and a +/// click adds an area (or removes one already selected) as Ctrl+click does. Esc, the same key +/// again, or editing a cell turns the mode off. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum SelectionMode { + #[default] + Off, + Extend, + Add, +} + +impl SelectionMode { + /// The id the `ui.selectionMode` command takes and returns. + pub fn id(self) -> &'static str { + match self { + Self::Off => "off", + Self::Extend => "extend", + Self::Add => "add", + } + } + + pub fn parse(id: &str) -> Option { + match id.trim().to_ascii_lowercase().as_str() { + "off" | "none" => Some(Self::Off), + "extend" => Some(Self::Extend), + "add" => Some(Self::Add), + _ => None, + } + } + + /// The status bar text (an i18n catalog key), when the mode shows one. + pub fn status_text(self) -> Option<&'static str> { + match self { + Self::Off => None, + Self::Extend => Some("Extend Selection"), + Self::Add => Some("Add or Remove Selection"), + } + } +} + +impl GridState { + /// Turns `mode` on, or off when it is already on (F8 / Shift+F8); the other mode ends. + pub fn toggle_selection_mode(&mut self, mode: SelectionMode) { + self.set_selection_mode(if self.selection_mode == mode { SelectionMode::Off } else { mode }); + } + + pub fn set_selection_mode(&mut self, mode: SelectionMode) { + self.selection_mode = mode; + self.add_pending = false; + } } /// Ctrl+wheel zoom for one (doc uid, sheet). egui eases a notch in over several frames and @@ -1784,6 +1843,14 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & let primary_clicked = resp.clicked_by(egui::PointerButton::Primary); let pointer = ctx.input(|i| i.pointer.clone()); let mods = ctx.input(|i| i.modifiers); + // Selecting with the pointer: Extend mode (F8) clicks as if Shift were held, Add mode + // (Shift+F8) as if Ctrl were. + let mut sel_mods = mods; + match app.grid.selection_mode { + SelectionMode::Extend => sel_mods.shift = true, + SelectionMode::Add => sel_mods.command = true, + SelectionMode::Off => {} + } // Hit-testing works in logical coordinates (see `Geo::rtl`). let pos = pointer.interact_pos().or(pointer.hover_pos()).map(|p| geo.mirror_pos(p)); let in_cells = pos.is_some_and(|p| geo.in_cells(p)); @@ -2020,7 +2087,7 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & return; } if resp.drag_started() - && !mods.any() + && !sel_mods.any() && sel.ranges.len() == 1 && sel.current().is_full_cols() && sel.current().contains(CellRef::new(0, c)) @@ -2028,9 +2095,9 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & app.grid.drag = Drag::MoveCols { source: sel.current(), before: None }; return; } - let range = if mods.shift { RangeRef::cols(sel.anchor.col.min(c), sel.anchor.col.max(c)) } else { RangeRef::cols(c, c) }; - select_ranges(app, range, mods.command, CellRef::new(geo.row_at(sh, geo.cells.top() + 1.0), c)); - app.grid.drag = Drag::Cols(if mods.shift { sel.anchor.col } else { c }); + let range = if sel_mods.shift { RangeRef::cols(sel.anchor.col.min(c), sel.anchor.col.max(c)) } else { RangeRef::cols(c, c) }; + select_ranges(app, range, sel_mods.command, CellRef::new(geo.row_at(sh, geo.cells.top() + 1.0), c)); + app.grid.drag = Drag::Cols(if sel_mods.shift { sel.anchor.col } else { c }); return; } if in_row_header { @@ -2042,9 +2109,9 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & if point_header(app, wb, sh, RangeRef::rows(r, r)) { return; } - let range = if mods.shift { RangeRef::rows(sel.anchor.row.min(r), sel.anchor.row.max(r)) } else { RangeRef::rows(r, r) }; - select_ranges(app, range, mods.command, CellRef::new(r, geo.col_at(sh, geo.cells.left() + 1.0))); - app.grid.drag = Drag::Rows(if mods.shift { sel.anchor.row } else { r }); + let range = if sel_mods.shift { RangeRef::rows(sel.anchor.row.min(r), sel.anchor.row.max(r)) } else { RangeRef::rows(r, r) }; + select_ranges(app, range, sel_mods.command, CellRef::new(r, geo.col_at(sh, geo.cells.left() + 1.0))); + app.grid.drag = Drag::Rows(if sel_mods.shift { sel.anchor.row } else { r }); return; } if !in_cells { @@ -2108,12 +2175,12 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & return; } app.grid.last_click = Some((c, now)); - if mods.shift { + if sel_mods.shift { let _ = app.session.run("selection.set", json!({"range": RangeRef::new(sel.anchor, c).a1(), "active": sel.active.a1()})); if let Some(d) = app.session.active_mut() { d.selection.anchor = sel.anchor; } - } else if mods.command { + } else if sel_mods.command { select_ranges(app, RangeRef::cell(c), true, c); } else { let _ = app.session.run("selection.set", json!({"cell": c.a1()})); @@ -2553,7 +2620,37 @@ fn toggled_ranges(mut ranges: Vec, clicked: String, add: bool) -> Vec>().join(","); + let _ = app.session.run("selection.set", json!({"range": range, "active": active.a1()})); + app.grid.add_pending = !kept; +} + fn select_ranges(app: &mut SheetApp, r: RangeRef, add: bool, active: CellRef) { + // A clicked area is kept: the next Add mode arrow starts a new one. + app.grid.add_pending = false; let current: Vec = if add { app.session.active().map(|d| d.selection.ranges.iter().map(|x| x.a1()).collect()).unwrap_or_default() } else { vec![] }; let clicked = r.a1(); @@ -2609,12 +2706,22 @@ fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: } egui::Event::Key { key, physical_key, pressed: true, modifiers: m, .. } => { use egui::Key; + let navigation = matches!( + key, + Key::ArrowUp | Key::ArrowDown | Key::ArrowLeft | Key::ArrowRight | Key::PageUp | Key::PageDown | Key::Home | Key::End + ); + // Extend mode (F8): navigation keys extend from the anchor as if Shift were held. + let m = if navigation && app.grid.selection_mode == SelectionMode::Extend { egui::Modifiers { shift: true, ..m } } else { m }; if crate::keymap::dispatch(app, ctx, key, physical_key, m, geo, sh) { continue; } let cmd = m.command; let shift = m.shift; let mv = |app: &mut SheetApp, dr: i64, dc: i64| { + if app.grid.selection_mode == SelectionMode::Add { + add_mode_move(app, dr, dc, shift, cmd); + return; + } let _ = app.session.run("selection.move", json!({"dr": dr, "dc": dc, "extend": shift, "jump": cmd})); app.grid.ensure_visible = true; app.grid.reveal_moving_end = shift; @@ -2673,8 +2780,13 @@ fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: Key::Escape => { app.session.clipboard = None; app.session.format_painter = None; + app.grid.set_selection_mode(SelectionMode::Off); } Key::Space if !cmd && !shift && is_checkbox(sh, app) => app.run_or_alert("cell.toggleCheckbox", json!({})), + // F8 Extend Selection, Shift+F8 Add or Remove Selection: each toggles its mode. + Key::F8 if !m.alt && !cmd => { + app.grid.toggle_selection_mode(if shift { SelectionMode::Add } else { SelectionMode::Extend }); + } Key::F8 if m.alt => { app.open_dialog("macros", json!({})); } diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 061698df9b..72c0042dfc 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -457,6 +457,8 @@ const JAPANESE: &[(&str, &str)] = &[ ("Point", "ポイント"), ("Calculate", "計算"), ("Select destination and press Enter or choose Paste", "貼り付け先を選び、Enter キーを押すか [貼り付け] を選びます"), + ("Extend Selection", "選択範囲の拡張"), + ("Add or Remove Selection", "選択範囲の追加または削除"), ("General", "標準"), ("Number", "数値"), ("Currency", "通貨"), diff --git a/crates/ui-egui/src/i18n/cs.rs b/crates/ui-egui/src/i18n/cs.rs index 7ed8372c7d..581038d078 100644 --- a/crates/ui-egui/src/i18n/cs.rs +++ b/crates/ui-egui/src/i18n/cs.rs @@ -385,6 +385,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Výběr odkazu", "Calculate" => "Přepočítat", "Select destination and press Enter or choose Paste" => "Vyberte cíl a stiskněte Enter nebo zvolte Vložit", + "Extend Selection" => "Rozšířit výběr", + "Add or Remove Selection" => "Přidat nebo odebrat výběr", "General" => "Obecný", "Number" => "Číslo", "Currency" => "Měna", diff --git a/crates/ui-egui/src/i18n/de.rs b/crates/ui-egui/src/i18n/de.rs index c20c7d1b36..77a9314189 100644 --- a/crates/ui-egui/src/i18n/de.rs +++ b/crates/ui-egui/src/i18n/de.rs @@ -383,6 +383,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Zeigen", "Calculate" => "Berechnen", "Select destination and press Enter or choose Paste" => "Ziel auswählen und die Eingabetaste drücken oder „Einfügen“ wählen", + "Extend Selection" => "Markierung erweitern", + "Add or Remove Selection" => "Markierung hinzufügen oder entfernen", "General" => "Standard", "Number" => "Zahl", "Currency" => "Währung", diff --git a/crates/ui-egui/src/i18n/es.rs b/crates/ui-egui/src/i18n/es.rs index 4ce231bcad..07ffcfc1c2 100644 --- a/crates/ui-egui/src/i18n/es.rs +++ b/crates/ui-egui/src/i18n/es.rs @@ -382,6 +382,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Señalar", "Calculate" => "Calcular", "Select destination and press Enter or choose Paste" => "Seleccione el destino y presione Entrar o elija Pegar", + "Extend Selection" => "Extender selección", + "Add or Remove Selection" => "Agregar o quitar selección", "General" => "General", "Number" => "Número", "Currency" => "Moneda", diff --git a/crates/ui-egui/src/i18n/fr.rs b/crates/ui-egui/src/i18n/fr.rs index 5d20ab0ca2..616db6fc28 100644 --- a/crates/ui-egui/src/i18n/fr.rs +++ b/crates/ui-egui/src/i18n/fr.rs @@ -284,6 +284,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Point", "Calculate" => "Calculer", "Select destination and press Enter or choose Paste" => "Sélectionnez la destination et appuyez sur Entrée ou choisissez Coller", + "Extend Selection" => "Étendre la sélection", + "Add or Remove Selection" => "Ajouter ou supprimer la sélection", "General" => "Standard", "Number" => "Nombre", "Currency" => "Monétaire", diff --git a/crates/ui-egui/src/i18n/ko.rs b/crates/ui-egui/src/i18n/ko.rs index 1902ad3015..17b1d0ba06 100644 --- a/crates/ui-egui/src/i18n/ko.rs +++ b/crates/ui-egui/src/i18n/ko.rs @@ -273,6 +273,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "가리키기", "Calculate" => "계산", "Select destination and press Enter or choose Paste" => "대상을 선택하고 Enter 키를 누르거나 [붙여넣기]를 선택하세요", + "Extend Selection" => "선택 영역 확장", + "Add or Remove Selection" => "선택 영역 추가 또는 제거", "General" => "일반", "Number" => "숫자", "Currency" => "통화", diff --git a/crates/ui-egui/src/i18n/pt.rs b/crates/ui-egui/src/i18n/pt.rs index fb9236cdb6..9adf19256d 100644 --- a/crates/ui-egui/src/i18n/pt.rs +++ b/crates/ui-egui/src/i18n/pt.rs @@ -274,6 +274,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Apontar", "Calculate" => "Calcular", "Select destination and press Enter or choose Paste" => "Selecione o destino e pressione Enter ou escolha Colar", + "Extend Selection" => "Estender Seleção", + "Add or Remove Selection" => "Adicionar ou Remover Seleção", "General" => "Geral", "Number" => "Número", "Currency" => "Moeda", diff --git a/crates/ui-egui/src/i18n/ru.rs b/crates/ui-egui/src/i18n/ru.rs index 765f6bb326..1669676b7d 100644 --- a/crates/ui-egui/src/i18n/ru.rs +++ b/crates/ui-egui/src/i18n/ru.rs @@ -273,6 +273,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Указание", "Calculate" => "Вычисление", "Select destination and press Enter or choose Paste" => "Выберите место вставки и нажмите Enter или выберите «Вставить»", + "Extend Selection" => "Расширить выделение", + "Add or Remove Selection" => "Добавить или удалить выделение", "General" => "Общий", "Number" => "Числовой", "Currency" => "Денежный", diff --git a/crates/ui-egui/src/i18n/sr.rs b/crates/ui-egui/src/i18n/sr.rs index cbf18d4c46..80e1acd214 100644 --- a/crates/ui-egui/src/i18n/sr.rs +++ b/crates/ui-egui/src/i18n/sr.rs @@ -276,6 +276,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Pokaži", "Calculate" => "Izračunavanje", "Select destination and press Enter or choose Paste" => "Izaberi odredište i pritisni Enter ili izaberi Nalepi", + "Extend Selection" => "Proširi izbor", + "Add or Remove Selection" => "Dodaj ili ukloni izbor", "General" => "Opšte", "Number" => "Broj", "Currency" => "Valuta", diff --git a/crates/ui-egui/src/i18n/zh.rs b/crates/ui-egui/src/i18n/zh.rs index 00175925e6..516ea4ee72 100644 --- a/crates/ui-egui/src/i18n/zh.rs +++ b/crates/ui-egui/src/i18n/zh.rs @@ -273,6 +273,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "指向", "Calculate" => "计算", "Select destination and press Enter or choose Paste" => "选择目标位置并按 Enter,或选择“粘贴”", + "Extend Selection" => "扩展式选定", + "Add or Remove Selection" => "添加或删除所选内容", "General" => "常规", "Number" => "数值", "Currency" => "货币", diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index f96cdac3c0..ce2cea4820 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -663,6 +663,18 @@ impl SheetApp { self.open_dialog(name, p.clone()); Ok(json!({"dialog": self.dialog.as_ref().map(|d| d.name())})) } + "ui.selectionMode" => { + // `{"mode": "extend"}` (F8), `"add"` (Shift+F8) or `"off"`; `{}` reports the mode. + match p.get("mode").and_then(Json::as_str) { + Some(id) => match grid::SelectionMode::parse(id) { + Some(mode) => self.grid.set_selection_mode(mode), + None => return Some(Err(format!("unknown selection mode {id:?} (use \"extend\", \"add\" or \"off\")"))), + }, + None if p.get("mode").is_some_and(|v| !v.is_null()) => return Some(Err("`mode` must be \"extend\", \"add\" or \"off\"".into())), + None => {} + } + Ok(json!({"mode": self.grid.selection_mode.id()})) + } "ui.message.dismiss" => { self.message = None; Ok(Json::Null) @@ -867,6 +879,8 @@ impl SheetApp { self.message = Some(("Text Box".into(), clean_error(&e))); return; } + // Editing a cell ends Extend and Add mode, as in Excel. + self.grid.set_selection_mode(grid::SelectionMode::Off); let Some(d) = self.session.active() else { return }; let Some(sh) = d.wb.active() else { return }; let at = sh.merge_at(d.selection.active).map(|m| m.start).unwrap_or(d.selection.active); diff --git a/crates/ui-egui/src/tabs.rs b/crates/ui-egui/src/tabs.rs index b77524bb96..89b021c176 100644 --- a/crates/ui-egui/src/tabs.rs +++ b/crates/ui-egui/src/tabs.rs @@ -237,6 +237,11 @@ pub fn status_bar(app: &mut SheetApp, ui: &mut Ui) { gridcraft_engine::Mode::Point => "Point", }; ui.label(egui::RichText::new(app.ui.language.tr(mode)).font(theme::ui_font(12.5)).color(t.text_dim)); + // F8 / Shift+F8 selection mode. + if let Some(text) = app.grid.selection_mode.status_text() { + ui.add_space(12.0); + ui.label(egui::RichText::new(app.ui.language.tr(text)).font(theme::ui_font(12.5)).color(t.text_dim)); + } if let Some(d) = app.session.active() { if d.wb.vba_project.is_some() { ui.add_space(12.0); diff --git a/crates/ui-egui/tests/keyboard_shortcuts.rs b/crates/ui-egui/tests/keyboard_shortcuts.rs index c0a1307b3d..3f39850324 100644 --- a/crates/ui-egui/tests/keyboard_shortcuts.rs +++ b/crates/ui-egui/tests/keyboard_shortcuts.rs @@ -1,10 +1,12 @@ //! Excel keyboard shortcuts (crate::keymap) dispatched through real egui key events. -use egui::{Event, Key, Modifiers}; +use egui::{Event, Key, Modifiers, PointerButton}; +use egui_kittest::kittest::Queryable as _; use gridcraft_engine::Session; use gridcraft_engine::core::CellRef; use gridcraft_engine::model::BorderStyle; use gridcraft_ui_egui::SheetApp; +use gridcraft_ui_egui::grid::{Geo, SelectionMode}; use gridcraft_ui_egui::keymap::{Shortcut, lookup}; use serde_json::json; @@ -86,6 +88,151 @@ fn copy_formula_from_above_and_function_keys() { assert_eq!(h.state().session.active().unwrap().wb.sheets[0].zoom, 110); } +fn selection_a1(h: &Harness) -> String { + h.state().session.active().unwrap().selection.a1() +} + +fn active_a1(h: &Harness) -> String { + h.state().session.active().unwrap().selection.active.a1() +} + +fn mode(h: &Harness) -> SelectionMode { + h.state().grid.selection_mode +} + +#[test] +fn f8_extend_selection_extends_from_the_anchor_until_turned_off() { + let mut h = harness(book("A1")); + assert!(h.query_by_label("Extend Selection").is_none()); + key(&mut h, Key::F8, Modifiers::NONE); + assert_eq!(mode(&h), SelectionMode::Extend); + assert!(h.query_by_label("Extend Selection").is_some(), "the status bar shows the mode"); + key(&mut h, Key::ArrowRight, Modifiers::NONE); + key(&mut h, Key::ArrowDown, Modifiers::NONE); + assert_eq!(selection_a1(&h), "A1:B2"); + assert_eq!(active_a1(&h), "A1", "the active cell stays on the anchor"); + // Ctrl+arrow extends to the edge of the data, as Ctrl+Shift+arrow does. + key(&mut h, Key::ArrowDown, CTRL); + assert_eq!(selection_a1(&h), "A1:B3"); + // F8 again ends the mode: arrows move again. + key(&mut h, Key::F8, Modifiers::NONE); + assert_eq!(mode(&h), SelectionMode::Off); + assert!(h.query_by_label("Extend Selection").is_none()); + key(&mut h, Key::ArrowRight, Modifiers::NONE); + assert_eq!(selection_a1(&h), "B1"); + // Esc ends it too, and keeps the extended selection. + key(&mut h, Key::F8, Modifiers::NONE); + key(&mut h, Key::ArrowRight, Modifiers::NONE); + key(&mut h, Key::Escape, Modifiers::NONE); + assert_eq!(mode(&h), SelectionMode::Off); + assert_eq!(selection_a1(&h), "B1:C1"); + key(&mut h, Key::ArrowDown, Modifiers::NONE); + assert_eq!(selection_a1(&h), "B2"); +} + +#[test] +fn shift_f8_add_to_selection_builds_areas_with_arrow_keys() { + let mut h = harness(book("A1:A2")); + key(&mut h, Key::F8, Modifiers::SHIFT); + assert_eq!(mode(&h), SelectionMode::Add); + assert!(h.query_by_label("Add or Remove Selection").is_some(), "the status bar shows the mode"); + assert!(h.query_by_label("Extend Selection").is_none()); + // Arrows move the active cell and keep A1:A2; the next arrow moves the new cell again. + key(&mut h, Key::ArrowRight, Modifiers::NONE); + assert_eq!(selection_a1(&h), "A1:A2,B1"); + key(&mut h, Key::ArrowRight, Modifiers::NONE); + assert_eq!(selection_a1(&h), "A1:A2,C1"); + assert_eq!(active_a1(&h), "C1"); + // Shift+arrow grows that area from it and keeps it. + key(&mut h, Key::ArrowRight, Modifiers::SHIFT); + assert_eq!(selection_a1(&h), "A1:A2,C1:D1"); + assert_eq!(active_a1(&h), "C1"); + // The next arrow starts a third area. + key(&mut h, Key::ArrowDown, Modifiers::NONE); + key(&mut h, Key::ArrowDown, Modifiers::NONE); + assert_eq!(selection_a1(&h), "A1:A2,C1:D1,C3"); + // F8 switches to Extend mode, and Shift+F8 twice ends Add mode. + key(&mut h, Key::F8, Modifiers::NONE); + assert_eq!(mode(&h), SelectionMode::Extend); + key(&mut h, Key::F8, Modifiers::SHIFT); + assert_eq!(mode(&h), SelectionMode::Add); + key(&mut h, Key::F8, Modifiers::SHIFT); + assert_eq!(mode(&h), SelectionMode::Off); + assert!(h.query_by_label("Add or Remove Selection").is_none()); + key(&mut h, Key::ArrowRight, Modifiers::NONE); + assert_eq!(selection_a1(&h), "D3", "with the mode off an arrow drops the areas"); +} + +#[test] +fn editing_a_cell_ends_the_selection_mode() { + let mut h = harness(book("A1")); + key(&mut h, Key::F8, Modifiers::NONE); + key(&mut h, Key::F2, Modifiers::NONE); + assert!(h.state().editor.is_some()); + assert_eq!(mode(&h), SelectionMode::Off); + key(&mut h, Key::Escape, Modifiers::NONE); + key(&mut h, Key::F8, Modifiers::SHIFT); + h.input_mut().events.push(Event::Text("7".into())); + h.run_steps(2); + assert!(h.state().editor.is_some(), "typing starts an edit"); + assert_eq!(mode(&h), SelectionMode::Off); +} + +fn click(h: &mut Harness, a1: &str) { + let at = { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll).cell_rect(sheet, CellRef::parse(a1).unwrap()).center() + }; + h.input_mut().events.push(Event::PointerMoved(at)); + h.run_steps(1); + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.run_steps(1); + } + h.run_steps(1); +} + +#[test] +fn clicks_extend_or_add_in_the_selection_modes() { + let mut h = harness(book("B2")); + key(&mut h, Key::F8, Modifiers::NONE); + click(&mut h, "D4"); + assert_eq!(selection_a1(&h), "B2:D4", "in Extend mode a click extends from the anchor"); + assert_eq!(mode(&h), SelectionMode::Extend, "the mode stays on"); + key(&mut h, Key::F8, Modifiers::SHIFT); + click(&mut h, "F6"); + assert_eq!(selection_a1(&h), "B2:D4,F6", "in Add mode a click adds an area"); + // (Not a double-click: that would edit the cell.) + h.state_mut().grid.last_click = None; + click(&mut h, "F6"); + assert_eq!(selection_a1(&h), "B2:D4", "and a click on a selected area removes it"); + // An arrow after a click keeps the clicked areas. + click(&mut h, "H8"); + key(&mut h, Key::ArrowDown, Modifiers::NONE); + assert_eq!(selection_a1(&h), "B2:D4,H8,H9"); + // Moving back onto the kept H8 and off again keeps it. + key(&mut h, Key::ArrowUp, Modifiers::NONE); + assert_eq!(selection_a1(&h), "B2:D4,H8"); + key(&mut h, Key::ArrowDown, Modifiers::NONE); + assert_eq!(selection_a1(&h), "B2:D4,H8,H9"); +} + +#[test] +fn selection_mode_command_sets_and_reports_without_a_dialog() { + let mut h = harness(book("A1")); + assert_eq!(h.state_mut().run("ui.selectionMode", json!({})), Ok(json!({"mode": "off"}))); + assert_eq!(h.state_mut().run("ui.selectionMode", json!({"mode": "extend"})), Ok(json!({"mode": "extend"}))); + h.run_steps(2); + assert!(h.query_by_label("Extend Selection").is_some()); + assert_eq!(h.state_mut().run("ui.selectionMode", json!({"mode": "add"})), Ok(json!({"mode": "add"}))); + assert!(h.state_mut().run("ui.selectionMode", json!({"mode": "sideways"})).is_err()); + assert!(h.state_mut().run("ui.selectionMode", json!({"mode": 3})).is_err()); + assert_eq!(mode(&h), SelectionMode::Add, "a bad mode changes nothing"); + assert_eq!(h.state_mut().run("ui.selectionMode", json!({"mode": "off"})), Ok(json!({"mode": "off"}))); + assert!(h.state().dialog.is_none()); +} + #[test] fn windows_and_mac_variants() { let cmd = CTRL; diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 22e221e698..cb4ee67988 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -1,6 +1,6 @@ # UI parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-13 · **Change:** minor (horizontal scrollbar in the sheet tab bar, #840) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-15 · **Change:** minor (F8 Extend Selection and Shift+F8 Add or Remove Selection) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Interaction, controls and feel, against Excel. Part of [target-app-parity.md](target-app-parity.md). **UI/UX fidelity: ~55% ready, 50–80 h** (estimated). Ribbon command coverage is measured @@ -75,7 +75,7 @@ this logic, with one undo step. | Shortcuts bound to commands | 200+ documented shortcuts per platform | audited below (#110): shortcuts whose feature exists are bound, with the exceptions listed; the rest wait for their features | | Ctrl+arrow, Ctrl+Shift+arrow, Ctrl+Home/End, Ctrl+Shift+Home/End, Page Up/Down, Alt+Page | yes | done | | Ctrl+1, Ctrl+; and Ctrl+Shift+; , Ctrl+D/R, Ctrl+E, Ctrl+T/L, Ctrl+K, Ctrl+Shift+L, Ctrl+Shift+~!@#$%^&_ | yes | done | -| F2, F4, F5, F7, F9 family, Shift+F2/F3/F10/F11, F12 | yes | done; F11 chart sheet, F8 extend mode, F3 Paste Name, F4 Repeat missing | +| F2, F4, F5, F7, F9 family, Shift+F2/F3/F10/F11, F12 | yes | done; F8 / Shift+F8 extend and add modes with their status bar text; F11 chart sheet, F3 Paste Name, F4 Repeat missing | | Mac-specific (Cmd variants, Control variants, Fn keys) | yes | done where a feature exists (`keymap.rs` picks the Mac variant) | | Sequential Alt access keys (Alt, H, N, …) on every ribbon tab | yes | 364-sequence keytip table for all tabs: per-control badges, prefix waiting, dropdowns open for editing (type/arrows/Enter), pending bindings for not-yet-built commands; QAT/Backstage excluded | | Requests | — | #110 (more shortcuts and hot keys), #33 (sequential Alt; initial support landed in #43) | @@ -123,7 +123,8 @@ otherwise. Number-format and border keys match the physical key (Ctrl+Shift+4, w | Shift+F3, Ctrl+F3, Ctrl+Shift+F3 | Shift+F3, Cmd+F3 (Define Name), Cmd+Shift+F3 | Insert Function, Name Manager, Create from Selection | **added** | | F5, Shift+F5 (Mac), F7, Shift+F9, Ctrl+Alt+F9, F12, Ctrl+F4 | same | Go To, Find, Spelling, calculate sheet / all, Save As, close | **added** | | Shift+F10, Alt+Shift+F10, Ctrl+F1 | Shift+F10, Cmd+Option+R | context menu, Error Checking, ribbon | **added** | -| F1, F3, F4 (repeat), F6 / Shift+F6, F8 / Shift+F8, F11, End mode | same | help, Paste Name, Repeat, pane cycling, extend/add mode, chart sheet | missing (no such feature yet) | +| F8, Shift+F8 | same | Extend Selection (arrows, Page keys, Ctrl+arrows, Home and clicks extend from the anchor) and Add or Remove Selection (arrows move the active cell and keep the areas, Shift+arrows grow a new area, a click adds or removes an area); the status bar names the mode; the key again, Esc or editing a cell ends it (`ui.selectionMode`) | **added** | +| F1, F3, F4 (repeat), F6 / Shift+F6, F11, End mode | same | help, Paste Name, Repeat, pane cycling, chart sheet | missing (no such feature yet) | | Ctrl+6, Ctrl+8, Ctrl+Q, Ctrl+Shift+A, Ctrl+A after a function name, Ctrl+Shift+U, Ctrl+Alt+P | same | objects, outline symbols, Quick Analysis, argument names, Function Arguments, formula bar height, tooltips | missing | | Ctrl+Shift+Page Up / Down, Ctrl+Alt+5, Ctrl+Shift+F1, Ctrl+F5–F10, Alt+F8 / F11 / F12 | same | group sheets, shape cycling, full screen, window keys, macros, VBA, Power Query | missing | | — | Cmd+= (calculate), Cmd+T (references while editing; F4 works), Shift+F7, Cmd+Shift+L | Mac-only extras | missing (Cmd+= is egui's zoom key; no thesaurus or Modify Cell Style) | @@ -160,6 +161,7 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. ## Revision history | Date | Change | Summary | +| 2026-10-15 | minor | F8 Extend Selection and Shift+F8 Add or Remove Selection modes, shown in the status bar (`ui.selectionMode`) | |---|---|---| | 2026-10-12 | minor | The File view's recent list tells when each workbook was last changed, read off the UI thread | | 2026-10-13 | minor | Horizontal scrollbar in the sheet tab bar: drag, track click pages, frozen columns excluded from the page (#840) | From a6bedd5153a3b2787eee35b5769e58cab2ab8efc Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:41:30 -0500 Subject: [PATCH 02/10] Docs: keep the UI parity revision table well-formed --- docs/ui-parity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 75b7903dd9..d51d556b82 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -174,8 +174,8 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. ## Revision history -| Date | Change | Summary | | 2026-10-15 | minor | F8 Extend Selection and Shift+F8 Add or Remove Selection modes, shown in the status bar (`ui.selectionMode`) | +| Date | Change | Summary | |---|---|---| | 2026-10-23 | minor | Frozen panes: Page Up, Ctrl+Up and Shift variants that move from the scrolled rows into the frozen ones bring the scrolled rows back to the top, so the rows in between are seen again (Page Up from far down used to end on row 1 with the view still a page down); Ctrl+Left and Alt+Page Up do the same for frozen columns. Other ways of selecting a frozen cell (click, Find, Go To, Undo, formula pointing) leave the view where it is | | 2026-10-23 | minor | After a left-drag of the fill handle an Auto Fill Options button sits below the filled cells; its menu redoes the fill as Copy Cells, Fill Series, Fill Formatting Only or Fill Without Formatting (undoes the drag's fill, then `edit.autoFill` in that mode). It goes away when the workbook changes or the selection moves (#1403) | From a4309846b2f7d44b08385027c47dbdd0e9788eb0 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:58:55 -0500 Subject: [PATCH 03/10] Keyboard: Add-mode header drags keep earlier areas; Shift-click ends a pending area --- crates/ui-egui/src/grid.rs | 23 +++++++++++++++++++---- docs/ui-parity.md | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 415bb40805..ab478b5875 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -185,6 +185,8 @@ pub struct GridState { /// Add mode: the last area is the cell an arrow key moved to, not yet extended, so the next /// arrow moves it again instead of keeping it. pub add_pending: bool, + /// Add mode header drag: the areas kept from before the drag, which the dragged range joins. + pub header_drag_base: Vec, } /// Excel's keyboard selection modes, shown in the status bar. In Extend mode (F8) arrow keys and @@ -2369,6 +2371,7 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & return; } let range = if sel_mods.shift { RangeRef::cols(sel.anchor.col.min(c), sel.anchor.col.max(c)) } else { RangeRef::cols(c, c) }; + app.grid.header_drag_base = add_mode_base(app); select_ranges(app, range, sel_mods.command, CellRef::new(geo.row_at(sh, geo.cells.top() + 1.0), c)); app.grid.drag = Drag::Cols(if sel_mods.shift { sel.anchor.col } else { c }); return; @@ -2383,6 +2386,7 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & return; } let range = if sel_mods.shift { RangeRef::rows(sel.anchor.row.min(r), sel.anchor.row.max(r)) } else { RangeRef::rows(r, r) }; + app.grid.header_drag_base = add_mode_base(app); select_ranges(app, range, sel_mods.command, CellRef::new(r, geo.col_at(sh, geo.cells.left() + 1.0))); app.grid.drag = Drag::Rows(if sel_mods.shift { sel.anchor.row } else { r }); return; @@ -2475,6 +2479,7 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & } app.grid.last_click = Some((c, now)); if sel_mods.shift { + app.grid.add_pending = false; let _ = app.session.run("selection.set", json!({"range": RangeRef::new(sel.anchor, c).a1(), "active": sel.active.a1()})); if let Some(d) = app.session.active_mut() { d.selection.anchor = sel.anchor; @@ -2549,14 +2554,16 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & } Drag::Rows(anchor) => { let r = geo.row_at(sh, p.y); - let _ = - app.session.run("selection.set", json!({"range": RangeRef::rows(anchor.min(r), anchor.max(r)).a1(), "active": sel.active.a1()})); + let range = RangeRef::rows(anchor.min(r), anchor.max(r)).a1(); + let ranges = app.grid.header_drag_base.iter().map(String::as_str).chain([range.as_str()]).collect::>().join(","); + let _ = app.session.run("selection.set", json!({"range": ranges, "active": sel.active.a1()})); scroll_by(app, vec2(0.0, auto.y)); } Drag::Cols(anchor) => { let c = geo.col_at(sh, p.x); - let _ = - app.session.run("selection.set", json!({"range": RangeRef::cols(anchor.min(c), anchor.max(c)).a1(), "active": sel.active.a1()})); + let range = RangeRef::cols(anchor.min(c), anchor.max(c)).a1(); + let ranges = app.grid.header_drag_base.iter().map(String::as_str).chain([range.as_str()]).collect::>().join(","); + let _ = app.session.run("selection.set", json!({"range": ranges, "active": sel.active.a1()})); scroll_by(app, vec2(auto.x, 0.0)); } Drag::MoveCols { source, .. } => { @@ -3047,6 +3054,14 @@ fn add_mode_move(app: &mut SheetApp, dr: i64, dc: i64, shift: bool, jump: bool) app.grid.add_pending = !kept; } +/// The active sheet's areas in Add mode, as `selection.set` takes them; empty in other modes. +fn add_mode_base(app: &SheetApp) -> Vec { + if app.grid.selection_mode != SelectionMode::Add { + return Vec::new(); + } + app.session.active().map(|d| d.selection.ranges.iter().map(|x| x.a1()).collect()).unwrap_or_default() +} + fn select_ranges(app: &mut SheetApp, r: RangeRef, add: bool, active: CellRef) { // A clicked area is kept: the next Add mode arrow starts a new one. app.grid.add_pending = false; diff --git a/docs/ui-parity.md b/docs/ui-parity.md index d51d556b82..f62a30d5be 100644 --- a/docs/ui-parity.md +++ b/docs/ui-parity.md @@ -174,9 +174,9 @@ scrollbar bugs 2–4 · pixel/feel pass against observed Excel 7–12. ## Revision history -| 2026-10-15 | minor | F8 Extend Selection and Shift+F8 Add or Remove Selection modes, shown in the status bar (`ui.selectionMode`) | | Date | Change | Summary | |---|---|---| +| 2026-10-15 | minor | F8 Extend Selection and Shift+F8 Add or Remove Selection modes, shown in the status bar (`ui.selectionMode`) | | 2026-10-23 | minor | Frozen panes: Page Up, Ctrl+Up and Shift variants that move from the scrolled rows into the frozen ones bring the scrolled rows back to the top, so the rows in between are seen again (Page Up from far down used to end on row 1 with the view still a page down); Ctrl+Left and Alt+Page Up do the same for frozen columns. Other ways of selecting a frozen cell (click, Find, Go To, Undo, formula pointing) leave the view where it is | | 2026-10-23 | minor | After a left-drag of the fill handle an Auto Fill Options button sits below the filled cells; its menu redoes the fill as Copy Cells, Fill Series, Fill Formatting Only or Fill Without Formatting (undoes the drag's fill, then `edit.autoFill` in that mode). It goes away when the workbook changes or the selection moves (#1403) | | 2026-10-22 | minor | Show Outline Symbols (`view.showOutlineSymbols`, Ctrl+8): a sheet can hide the outline's level buttons and brackets while keeping its outline, as Excel saves with `showOutlineSymbols="0"` (read and written); the grid then gives the bands no room | From 41a1a3f8bc8611be25f853e70350fecb97e87a35 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Sat, 24 Oct 2026 15:21:08 -0500 Subject: [PATCH 04/10] Keyboard: Add-mode Shift-click keeps earlier areas; Dutch status labels --- crates/ui-egui/src/grid.rs | 8 +++++++- crates/ui-egui/src/i18n/nl.rs | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 81d3b19298..4d13eee49b 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -2492,8 +2492,14 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & } app.grid.last_click = Some((c, now)); if sel_mods.shift { + // Add mode keeps the areas already selected and drops only the provisional area it made itself. + let mut ranges = add_mode_base(app); + if app.grid.add_pending { + ranges.pop(); + } app.grid.add_pending = false; - let _ = app.session.run("selection.set", json!({"range": RangeRef::new(sel.anchor, c).a1(), "active": sel.active.a1()})); + ranges.push(RangeRef::new(sel.anchor, c).a1()); + let _ = app.session.run("selection.set", json!({"range": ranges.join(","), "active": sel.active.a1()})); if let Some(d) = app.session.active_mut() { d.selection.anchor = sel.anchor; } diff --git a/crates/ui-egui/src/i18n/nl.rs b/crates/ui-egui/src/i18n/nl.rs index c1f952357f..1f35904ddb 100644 --- a/crates/ui-egui/src/i18n/nl.rs +++ b/crates/ui-egui/src/i18n/nl.rs @@ -278,6 +278,8 @@ pub(super) fn translate(text: &str) -> Option<&'static str> { "Point" => "Aanwijzen", "Calculate" => "Berekenen", "Select destination and press Enter or choose Paste" => "Selecteer de bestemming en druk op Enter of kies Plakken", + "Extend Selection" => "Selectie uitbreiden", + "Add or Remove Selection" => "Selectie toevoegen of verwijderen", "General" => "Algemeen", "Number" => "Getal", "Currency" => "Valuta", From bf9531954df1cbbe55c5a61f7af1ddd2ab8bed11 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Sat, 24 Oct 2026 15:25:47 -0500 Subject: [PATCH 05/10] Keyboard: test that an Add-mode header drag keeps the earlier areas --- crates/ui-egui/tests/it/keyboard_shortcuts.rs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/ui-egui/tests/it/keyboard_shortcuts.rs b/crates/ui-egui/tests/it/keyboard_shortcuts.rs index 2c8a863fee..6a75c5f5ac 100644 --- a/crates/ui-egui/tests/it/keyboard_shortcuts.rs +++ b/crates/ui-egui/tests/it/keyboard_shortcuts.rs @@ -218,6 +218,38 @@ fn clicks_extend_or_add_in_the_selection_modes() { assert_eq!(selection_a1(&h), "B2:D4,H8,H9"); } +/// A point in the column header band, above `col`'s cells. +fn column_header_point(h: &Harness, col: &str) -> egui::Pos2 { + let app = h.state(); + let sheet = app.session.active().unwrap().wb.active().unwrap(); + let geo = Geo::new(sheet, app.grid.rect.unwrap(), app.view().scroll); + let x = geo.cell_rect(sheet, CellRef::parse(&format!("{col}1")).unwrap()).center().x; + egui::pos2(x, geo.head_top() + 4.0) +} + +fn drag(h: &mut Harness, from: egui::Pos2, to: egui::Pos2) { + h.input_mut().events.push(Event::PointerMoved(from)); + h.run_steps(1); + h.input_mut().events.push(Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.run_steps(1); + h.input_mut().events.push(Event::PointerMoved(to)); + h.run_steps(2); + h.input_mut().events.push(Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(2); +} + +#[test] +fn add_mode_header_drag_keeps_the_earlier_areas() { + let mut h = harness(book("B2")); + key(&mut h, Key::F8, Modifiers::SHIFT); + click(&mut h, "F6"); + assert_eq!(selection_a1(&h), "B2,F6"); + h.state_mut().grid.last_click = None; + let (from, to) = (column_header_point(&h, "H"), column_header_point(&h, "J")); + drag(&mut h, from, to); + assert_eq!(selection_a1(&h), "B2,F6,H:J", "a header drag in Add mode adds its columns and keeps the areas already selected"); +} + #[test] fn selection_mode_command_sets_and_reports_without_a_dialog() { let mut h = harness(book("A1")); From 8471ccace5a2a05b6a312b13021cbe0589ed248e Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Sat, 24 Oct 2026 15:33:04 -0500 Subject: [PATCH 06/10] Keyboard: Add mode keeps areas for Home and End; a click inside a larger area removes it --- crates/ui-egui/src/grid.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index 4d13eee49b..fc4fb8176d 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -3089,10 +3089,15 @@ fn add_mode_base(app: &SheetApp) -> Vec { fn select_ranges(app: &mut SheetApp, r: RangeRef, add: bool, active: CellRef) { // A clicked area is kept: the next Add mode arrow starts a new one. app.grid.add_pending = false; - let current: Vec = - if add { app.session.active().map(|d| d.selection.ranges.iter().map(|x| x.a1()).collect()).unwrap_or_default() } else { vec![] }; + let current: Vec = if add { app.session.active().map(|d| d.selection.ranges.clone()).unwrap_or_default() } else { vec![] }; let clicked = r.a1(); - let ranges = toggled_ranges(current, clicked.clone(), add); + // A click on a cell inside a larger kept area removes that area, as a click on a single-cell area does. + let inside = + (add && current.len() > 1 && r.start == r.end).then(|| current.iter().position(|x| x.a1() != clicked && x.contains(r.start))).flatten(); + let ranges: Vec = match inside { + Some(i) => current.iter().enumerate().filter(|(j, _)| *j != i).map(|(_, x)| x.a1()).collect(), + None => toggled_ranges(current.iter().map(|x| x.a1()).collect(), clicked.clone(), add), + }; // A removed area takes the active cell with it; `selection.set` then puts it at the start of the last area. let params = if ranges.contains(&clicked) { json!({"range": ranges.join(","), "active": active.a1()}) } else { json!({"range": ranges.join(",")}) }; @@ -3191,6 +3196,16 @@ fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: Key::PageUp if !cmd => mv(app, -page, 0), Key::PageDown => app.run_or_alert("sheet.next", json!({})), Key::PageUp => app.run_or_alert("sheet.previous", json!({})), + // Add mode: Home and End move the active cell as arrows do, keeping the selected areas. + Key::Home | Key::End if app.grid.selection_mode == SelectionMode::Add && (cmd || key == Key::Home) => { + let a = app.session.active().map(|d| d.selection.active).unwrap_or_default(); + let to = match key { + Key::Home if cmd => CellRef::new(sh.freeze.map(|f| f.0).unwrap_or(0), sh.freeze.map(|f| f.1).unwrap_or(0)), + Key::End => sh.used_range().map(|u| u.end).unwrap_or_default(), + _ => CellRef::new(a.row, 0), + }; + add_mode_move(app, i64::from(to.row) - i64::from(a.row), i64::from(to.col) - i64::from(a.col), false, false); + } Key::Home if cmd => { let r = sh.freeze.map(|f| f.0).unwrap_or(0); let c = sh.freeze.map(|f| f.1).unwrap_or(0); From 9b7c5ca1f604d7162c44700670686d4096a38572 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Sat, 24 Oct 2026 15:41:13 -0500 Subject: [PATCH 07/10] Keyboard: Add mode drops only its own provisional area; selection commands go through app.run --- crates/ui-egui/src/grid.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index fc4fb8176d..6aff68e5fa 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -184,7 +184,7 @@ pub struct GridState { pub selection_mode: SelectionMode, /// Add mode: the last area is the cell an arrow key moved to, not yet extended, so the next /// arrow moves it again instead of keeping it. - pub add_pending: bool, + pub add_pending: Option, /// Add mode header drag: the areas kept from before the drag, which the dragged range joins. pub header_drag_base: Vec, } @@ -239,7 +239,7 @@ impl GridState { pub fn set_selection_mode(&mut self, mode: SelectionMode) { self.selection_mode = mode; - self.add_pending = false; + self.add_pending = None; } } @@ -2494,10 +2494,11 @@ fn interact(app: &mut SheetApp, ui: &mut egui::Ui, resp: &egui::Response, geo: & if sel_mods.shift { // Add mode keeps the areas already selected and drops only the provisional area it made itself. let mut ranges = add_mode_base(app); - if app.grid.add_pending { + if let Some(p) = app.grid.add_pending.take() + && ranges.last() == Some(&p.a1()) + { ranges.pop(); } - app.grid.add_pending = false; ranges.push(RangeRef::new(sel.anchor, c).a1()); let _ = app.session.run("selection.set", json!({"range": ranges.join(","), "active": sel.active.a1()})); if let Some(d) = app.session.active_mut() { @@ -3058,24 +3059,27 @@ fn add_mode_move(app: &mut SheetApp, dr: i64, dc: i64, shift: bool, jump: bool) app.grid.ensure_visible = true; app.grid.reveal_moving_end = shift; if shift { - let _ = app.session.run("selection.move", json!({"dr": dr, "dc": dc, "extend": true, "jump": jump})); - app.grid.add_pending = false; + let _ = app.run("selection.move", json!({"dr": dr, "dc": dc, "extend": true, "jump": jump})); + app.grid.add_pending = None; return; } let Some(mut keep) = app.session.active().map(|d| d.selection.ranges.clone()) else { return }; - if app.grid.add_pending { + // Only the provisional area this mode made is dropped, and only while it is still the last area. + if let Some(p) = app.grid.add_pending.take() + && keep.last() == Some(&p) + { keep.pop(); } // The engine works out where the cell lands (hidden rows, merged cells, Ctrl+arrow jumps). - let _ = app.session.run("selection.move", json!({"dr": dr, "dc": dc, "extend": false, "jump": jump})); + let _ = app.run("selection.move", json!({"dr": dr, "dc": dc, "extend": false, "jump": jump})); let Some((area, active)) = app.session.active().map(|d| (d.selection.current(), d.selection.active)) else { return }; // Landing on an area already kept leaves it kept: the next arrow doesn't drop it. let kept = keep.contains(&area); keep.retain(|r| *r != area); keep.push(area); let range = keep.iter().map(|r| r.a1()).collect::>().join(","); - let _ = app.session.run("selection.set", json!({"range": range, "active": active.a1()})); - app.grid.add_pending = !kept; + let _ = app.run("selection.set", json!({"range": range, "active": active.a1()})); + app.grid.add_pending = (!kept).then_some(area); } /// The active sheet's areas in Add mode, as `selection.set` takes them; empty in other modes. @@ -3088,7 +3092,7 @@ fn add_mode_base(app: &SheetApp) -> Vec { fn select_ranges(app: &mut SheetApp, r: RangeRef, add: bool, active: CellRef) { // A clicked area is kept: the next Add mode arrow starts a new one. - app.grid.add_pending = false; + app.grid.add_pending = None; let current: Vec = if add { app.session.active().map(|d| d.selection.ranges.clone()).unwrap_or_default() } else { vec![] }; let clicked = r.a1(); // A click on a cell inside a larger kept area removes that area, as a click on a single-cell area does. From 4efd8fc0dbd435da0204bc4b682b323de11bedf2 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:47:41 -0500 Subject: [PATCH 08/10] Qualify selection mode references --- crates/ui-egui/src/app/commands.rs | 2 +- crates/ui-egui/src/app/editing.rs | 2 +- crates/ui-egui/src/grid/selection.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index f05cd08f7e..99e70fa753 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -228,7 +228,7 @@ impl SheetApp { "ui.selectionMode" => { // `{"mode": "extend"}` (F8), `"add"` (Shift+F8) or `"off"`; `{}` reports the mode. match p.get("mode").and_then(Json::as_str) { - Some(id) => match grid::SelectionMode::parse(id) { + Some(id) => match crate::grid::SelectionMode::parse(id) { Some(mode) => self.grid.set_selection_mode(mode), None => return Some(Err(format!("unknown selection mode {id:?} (use \"extend\", \"add\" or \"off\")"))), }, diff --git a/crates/ui-egui/src/app/editing.rs b/crates/ui-egui/src/app/editing.rs index 9d9e3dd77b..2ba978e544 100644 --- a/crates/ui-egui/src/app/editing.rs +++ b/crates/ui-egui/src/app/editing.rs @@ -13,7 +13,7 @@ impl SheetApp { return; } // Editing a cell ends Extend and Add mode, as in Excel. - self.grid.set_selection_mode(grid::SelectionMode::Off); + self.grid.set_selection_mode(crate::grid::SelectionMode::Off); let Some(d) = self.session.active() else { return }; let Some(sh) = d.wb.active() else { return }; let at = sh.merge_at(d.selection.active).map(|m| m.start).unwrap_or(d.selection.active); diff --git a/crates/ui-egui/src/grid/selection.rs b/crates/ui-egui/src/grid/selection.rs index e57694c4d0..17995ca2c2 100644 --- a/crates/ui-egui/src/grid/selection.rs +++ b/crates/ui-egui/src/grid/selection.rs @@ -3,7 +3,7 @@ use super::geo::Geo; use super::interact::{hidden_col_edge, hidden_row_edge}; #[cfg(test)] use super::show::hidden_around; -use super::{CellRef, Color32, Painter, RangeRef, Rect, Sheet, SheetApp, Stroke, StrokeKind, Tokens, json, pos2, vec2}; +use super::{CellRef, Color32, Painter, RangeRef, Rect, SelectionMode, Sheet, SheetApp, Stroke, StrokeKind, Tokens, json, pos2, vec2}; /// Where the active cell is on screen: the whole merged cell when it is part of one. fn active_rect(geo: &Geo, sh: &Sheet, sel: &gridcraft_engine::Selection) -> Rect { From 3b8daf900cc02d6498f5674b540c9f4a785957d3 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:56:19 -0500 Subject: [PATCH 09/10] Split grid keyboard focus guard --- crates/ui-egui/src/grid/keyboard.rs | 57 ++++++++++++++++------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/crates/ui-egui/src/grid/keyboard.rs b/crates/ui-egui/src/grid/keyboard.rs index 18864fb452..b47811061b 100644 --- a/crates/ui-egui/src/grid/keyboard.rs +++ b/crates/ui-egui/src/grid/keyboard.rs @@ -6,33 +6,9 @@ use super::{CellRef, Drag, SelectionMode, Sheet, SheetApp, json}; /// Grid keyboard handling when no text field has focus. pub(super) fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: &Geo, sh: &Sheet) { - // Focus left on an editor that no longer exists goes back to the grid. - let stale = [egui::Id::unique("gridcraft.cell_editor"), egui::Id::unique("gridcraft.formula_bar"), crate::text_box::editor_id()]; - let editing = app.editor.is_some() || app.text_box_editor.is_some(); - if !editing && ctx.memory(|m| m.focused()).is_some_and(|f| stale.contains(&f)) { - resp.request_focus(); - } - let other_focus = ctx.memory(|m| m.focused()).is_some_and(|f| f != resp.id && (editing || !stale.contains(&f))); - // An open filter menu owns the keyboard even when a click inside it left focus on nothing (#1516). - // So do the fill menu (its Esc closes it, not the copy) and the right-drag that opens it (an - // arrow key would move the selection the menu fills from). - if editing - || app.dialog.is_some() - || other_focus - || app.message.is_some() - || app.grid.filter_menu.is_some() - || app.grid.fill_menu.is_some() - || matches!(app.grid.drag, Drag::Fill { menu: true, .. }) - { + if !claim_grid_keyboard(app, ctx, resp) { return; } - if ctx.memory(|m| m.focused()).is_none() { - resp.request_focus(); - } - // The grid owns arrows, Tab and Escape (egui would otherwise move focus between widgets). - ctx.memory_mut(|m| { - m.set_focus_lock_filter(resp.id, egui::EventFilter { tab: true, horizontal_arrows: true, vertical_arrows: true, escape: true }) - }); let events = ctx.input(|i| i.events.clone()); // A shortcut key that also types a character (Shift+Space sends Text(" ")) must not start an edit. let shortcut_pressed = events.iter().any(|e| { @@ -180,3 +156,34 @@ pub(super) fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Res } } } + +fn claim_grid_keyboard(app: &SheetApp, ctx: &egui::Context, resp: &egui::Response) -> bool { + // Focus left on an editor that no longer exists goes back to the grid. + let stale = [egui::Id::unique("gridcraft.cell_editor"), egui::Id::unique("gridcraft.formula_bar"), crate::text_box::editor_id()]; + let editing = app.editor.is_some() || app.text_box_editor.is_some(); + if !editing && ctx.memory(|m| m.focused()).is_some_and(|f| stale.contains(&f)) { + resp.request_focus(); + } + let other_focus = ctx.memory(|m| m.focused()).is_some_and(|f| f != resp.id && (editing || !stale.contains(&f))); + // An open filter menu owns the keyboard even when a click inside it left focus on nothing (#1516). + // So do the fill menu (its Esc closes it, not the copy) and the right-drag that opens it (an + // arrow key would move the selection the menu fills from). + if editing + || app.dialog.is_some() + || other_focus + || app.message.is_some() + || app.grid.filter_menu.is_some() + || app.grid.fill_menu.is_some() + || matches!(app.grid.drag, Drag::Fill { menu: true, .. }) + { + return false; + } + if ctx.memory(|m| m.focused()).is_none() { + resp.request_focus(); + } + // The grid owns arrows, Tab and Escape (egui would otherwise move focus between widgets). + ctx.memory_mut(|m| { + m.set_focus_lock_filter(resp.id, egui::EventFilter { tab: true, horizontal_arrows: true, vertical_arrows: true, escape: true }) + }); + true +} From a276e2f21d5ce1e85a4cad6da30e756eb8088b07 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:04:36 -0500 Subject: [PATCH 10/10] Allow keyboard dispatch complexity --- crates/ui-egui/src/grid/keyboard.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ui-egui/src/grid/keyboard.rs b/crates/ui-egui/src/grid/keyboard.rs index b47811061b..fb9aaeacf4 100644 --- a/crates/ui-egui/src/grid/keyboard.rs +++ b/crates/ui-egui/src/grid/keyboard.rs @@ -5,6 +5,7 @@ use super::selection::add_mode_move; use super::{CellRef, Drag, SelectionMode, Sheet, SheetApp, json}; /// Grid keyboard handling when no text field has focus. +#[allow(clippy::cognitive_complexity)] // Keyboard input dispatch keeps related key behavior together. pub(super) fn keyboard(app: &mut SheetApp, ctx: &egui::Context, resp: &egui::Response, geo: &Geo, sh: &Sheet) { if !claim_grid_keyboard(app, ctx, resp) { return;