From 5e7d2c9943bc521c1e4580a9a488262b063bf73b Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 04:35:28 +0000 Subject: [PATCH] CellGrid header press: Shift extends the current range from the anchor's line, Cmd/Ctrl adds the whole line as a range --- crates/behavior/src/selection/cells.rs | 32 ++++++++++++++++++++++++ crates/table/src/ui/grid.rs | 8 +++--- crates/table/src/ui/grid/input.rs | 34 ++++++++++++++++++-------- crates/table/tests/it/grid.rs | 29 ++++++++++++++++++++++ 4 files changed, 89 insertions(+), 14 deletions(-) diff --git a/crates/behavior/src/selection/cells.rs b/crates/behavior/src/selection/cells.rs index 54d4b73d4..9762d2f09 100644 --- a/crates/behavior/src/selection/cells.rs +++ b/crates/behavior/src/selection/cells.rs @@ -523,6 +523,38 @@ impl CellSelection { true } + /// Cmd+click (Ctrl+click) on a row or column header: `range` joins as a new range after the others, covered over + /// merges unless it is whole lines ([`CellSpace::cover`]); the active cell and the anchor at its top-left. A drag + /// from there extends it with [`extend_current`](Self::extend_current). Ends a Tab run. + pub fn add_range(&mut self, range: CellRange, space: &CellSpace<'_, K>) -> bool { + let Some(placed) = Placed::of(self, space) else { + return self.select_range(range, space); + }; + let Some(range) = space.place_range(&range) else { + return false; + }; + let range = space.cover(range); + let active = range.top_left(); + let mut ranges = placed.ranges; + ranges.push(range); + self.store(Placed { current: ranges.len() - 1, ranges, active, anchor: active, tab_run: None }, space) + } + + /// Shift+click on a header and a drag over the headers: the current range becomes `range`, covered as + /// [`select_range`](Self::select_range) covers it; the other ranges, the active cell and the anchor stay. On the + /// empty selection it selects `range`. Ends a Tab run. + pub fn extend_current(&mut self, range: CellRange, space: &CellSpace<'_, K>) -> bool { + let Some(mut placed) = Placed::of(self, space) else { + return self.select_range(range, space); + }; + let Some(range) = space.place_range(&range) else { + return false; + }; + placed.ranges[placed.current] = space.cover(range); + placed.tab_run = None; + self.store(placed, space) + } + /// Cmd+click (Ctrl+click) on any cell: a cell a range holds is taken out of the selection (and joins it again /// when it was taken out); any other cell joins as a new range ([`add`](Self::add)). It places /// the ranges, so it also takes out a cell between the rows of a range; the widget reports the result as diff --git a/crates/table/src/ui/grid.rs b/crates/table/src/ui/grid.rs index 2513be5c6..2100a888e 100644 --- a/crates/table/src/ui/grid.rs +++ b/crates/table/src/ui/grid.rs @@ -139,10 +139,10 @@ pub struct CellGridStyle { /// a fill handle and copy marks. /// /// - Pointer: click selects, drag extends with auto-scroll, Shift+click extends from the anchor, Cmd/Ctrl+click adds a -/// range on an unselected cell and removes a selected one; a header click selects the line, a drag over headers -/// several; the corner selects all. A header edge drags a resize, a double-click autofits (or unhides a hidden -/// line); the fill handle on the current range's corner drags a [`GridEdit::Fill`], the range's border a -/// [`GridEdit::MoveCells`] (Esc cancels either). +/// range on an unselected cell and removes a selected one; a header click selects the line, Shift+click extends the +/// current range to it, Cmd/Ctrl+click adds it as a range, a drag over headers several; the corner selects all. A +/// header edge drags a resize, a double-click autofits (or unhides a hidden line); the fill handle on the current +/// range's corner drags a [`GridEdit::Fill`], the range's border a [`GridEdit::MoveCells`] (Esc cancels either). /// - Keys: arrows move (Shift extends; Cmd/Ctrl jumps to [`GridModel::data_edge`]), Page Up and Down a screen, Home the /// row start, Cmd/Ctrl+Home the first cell after the frozen panes, Cmd/Ctrl+End [`GridModel::last_used`], Enter and /// Tab advance inside a selection (Shift back), Cmd/Ctrl+A selects all, Cmd/Ctrl+Space the column, Shift+Space the diff --git a/crates/table/src/ui/grid/input.rs b/crates/table/src/ui/grid/input.rs index f34f30cf0..58331b25e 100644 --- a/crates/table/src/ui/grid/input.rs +++ b/crates/table/src/ui/grid/input.rs @@ -63,8 +63,9 @@ impl Preview { /// The gesture a drag on one of the grid's parts runs. #[derive(Clone, Debug, PartialEq)] enum Grab { - /// Selecting whole columns (`rows` false) or rows from `anchor`. - Lines { rows: bool, anchor: u32 }, + /// Selecting whole columns (`rows` false) or rows from `anchor`; `extend` when the drag moves the current range's + /// far edge (after Shift or Cmd) instead of replacing the selection. + Lines { rows: bool, anchor: u32, extend: bool }, /// Resizing the line `line` of a column (`rows` false) or row axis from `from` points at 100 %. Resize { rows: bool, line: u32, from: f32, id: GestureId, last: f32 }, /// Growing `source` by the fill handle; `menu` for a secondary drag. @@ -600,8 +601,9 @@ impl CellGrid<'_> { (edits, swapped) } - /// A press on a column (`rows` false) or row header: selects the line, a drag selects several, a secondary release - /// asks for the header's menu. + /// A press on a column (`rows` false) or row header: selects the line, Shift+press extends the current range to it + /// from the anchor's line, Cmd+press adds it as a new range, a drag selects several, a secondary release asks for + /// the header's menu. #[allow(clippy::too_many_arguments)] fn line_press( edits: &mut Vec, @@ -616,23 +618,35 @@ impl CellGrid<'_> { let range = |a: u32, b: u32| if rows { Some(space.whole_rows(a.min(b), a.max(b))) } else { space.whole_columns(a.min(b), a.max(b)) }; match press.phase { PressPhase::Down { start, .. } if start.button == PointerButton::Primary => { - *grab = Some(Grab::Lines { rows, anchor: line }); - if let Some(range) = range(line, line) { - if press.mods.cmd { - selection.add(CellAt::new(range.first, range.columns.start), space); + // Shift extends the current range from the anchor's line; Cmd adds the line as a new range (Excel). + let held = selection.anchor_cell().copied().map(|cell| if rows { cell.row } else { cell.column }); + let (anchor, extend) = match held { + Some(held) if press.mods.shift => (held, true), + _ => (line, press.mods.cmd), + }; + *grab = Some(Grab::Lines { rows, anchor, extend }); + if let Some(range) = range(anchor, line) { + if press.mods.shift { + selection.extend_current(range, space); + } else if press.mods.cmd { + selection.add_range(range, space); } else { selection.select_range(range, space); } } } PressPhase::Drag { pos, .. } => { - if let Some(Grab::Lines { rows: dragging, anchor }) = grab + if let Some(Grab::Lines { rows: dragging, anchor, extend }) = grab && *dragging == rows { let at = geometry.mirror_pos(pos); let to = if rows { geometry.row_at(at.y) } else { geometry.column_at(at.x) }; if let Some(range) = range(*anchor, to) { - selection.select_range(range, space); + if *extend { + selection.extend_current(range, space); + } else { + selection.select_range(range, space); + } } } } diff --git a/crates/table/tests/it/grid.rs b/crates/table/tests/it/grid.rs index d451196b3..74230e696 100644 --- a/crates/table/tests/it/grid.rs +++ b/crates/table/tests/it/grid.rs @@ -105,6 +105,12 @@ impl Rig { self.fixture.part_rect(id, part).expect("the part was sensed") } + /// A click at the centre of `part` with `modifiers` held. + fn click_part(&mut self, part: impl Into, modifiers: Modifiers) { + let at = self.part_rect(part).center(); + self.fixture.hold(modifiers).click_at(at).hold(Modifiers::NONE).idle(2); + } + fn drag_part(&mut self, part: impl Into, by: Vec2) { let from = self.part_rect(part).center(); self.fixture.drag_at(from, by); @@ -232,3 +238,26 @@ fn alt_or_ctrl_enter_opens_the_active_cell() { assert_eq!(rig.active(), Some(CellAt::new(0, 0)), "{modifiers:?}+Enter does not advance"); } } + +/// Shift+click on a column header extends the current range from the anchor's column; Cmd+click adds the whole +/// column as a new range. +#[trial] +fn header_shift_click_extends_and_cmd_click_adds_a_whole_line() { + let mut rig = rig(None, a1(), 1.0); + let column = |at: u64| PartKey::new("column_header").at(at); + rig.click_part(column(1), Modifiers::NONE); + rig.click_part(column(3), Modifiers::SHIFT); + let whole = (0..=SHEET_ROWS - 1, 1..=3); + { + let state = rig.state.borrow(); + let ranges = state.selection.ranges(); + assert_eq!(ranges.len(), 1, "Shift extends the one range: {ranges:?}"); + assert_eq!((ranges[0].rows(), ranges[0].column_span()), whole, "columns B to D, whole"); + } + rig.click_part(column(5), Modifiers::COMMAND); + let state = rig.state.borrow(); + let ranges = state.selection.ranges(); + assert_eq!(ranges.len(), 2, "Cmd adds a range: {ranges:?}"); + assert_eq!((ranges[0].rows(), ranges[0].column_span()), whole, "the first range stays"); + assert_eq!((ranges[1].rows(), ranges[1].column_span()), (0..=SHEET_ROWS - 1, 5..=5), "the new range is all of column F"); +}