From 210fe76f0c91e9fa272c26f26e91fcb51f014713 Mon Sep 17 00:00:00 2001 From: tsw2k Date: Mon, 16 Nov 2026 11:47:53 +0000 Subject: [PATCH] fix(engine): a drag between narrow columns leaves them as they are A column a file gave below the 6 pt drag minimum was forced up to it by any drag of its border, even one of no distance: columns of 3 pt and 4 pt became 6 pt and 1 pt, and a column of a point or less is one import reads as no width, giving the whole table equal columns. A column already under the minimum is no longer narrowed further, nor widened by a drag with no room for it. Whole-column and selected-row drags both go through share_out. The import part of this change (keeping columns over 1 pt and under 6 pt) moved to craft-libs with RTF and ODT import (storytold/craft-libs#1865). --- crates/engine/src/cmd/table_resize.rs | 12 +++++++---- crates/engine/src/cmd/table_resize/tests.rs | 23 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/cmd/table_resize.rs b/crates/engine/src/cmd/table_resize.rs index 709d4038f5..0870f76314 100644 --- a/crates/engine/src/cmd/table_resize.rs +++ b/crates/engine/src/cmd/table_resize.rs @@ -287,12 +287,16 @@ fn share_out(w: &mut [f32], i: usize, dx: f32, share: Share) -> Option { Share::Proportional if rest > 0.0 && narrowest > 0.0 => Share::Proportional, _ => Share::LeftOnly, }; - let least = MIN_COL - left; + // A column already narrower than `MIN_COL` (a file can give one) is not narrowed further, + // nor widened by a drag that has no room for it: forcing it to `MIN_COL` would take its + // neighbour below a point, which import reads as no width and gives the whole table equal + // columns. + let least = (MIN_COL - left).min(0.0); let d = match share { - Share::Neighbour => dx.clamp(least, (w.get(i + 1).copied().unwrap_or(0.0) - MIN_COL).max(least)), + Share::Neighbour => dx.clamp(least, (w.get(i + 1).copied().unwrap_or(0.0) - MIN_COL).max(0.0)), // As far as the narrowest column on the right, shrunk in proportion, stays `MIN_COL`. - Share::Proportional => dx.clamp(least, (rest * (1.0 - (MIN_COL / narrowest).min(1.0))).max(least)), - Share::LeftOnly => (left + dx).clamp(MIN_COL, MAX_LEN) - left, + Share::Proportional => dx.clamp(least, (rest * (1.0 - (MIN_COL / narrowest).min(1.0))).max(0.0)), + Share::LeftOnly => (left + dx).clamp(MIN_COL.min(left), MAX_LEN.max(left)) - left, }; if let Some(x) = w.get_mut(i) { *x = left + d; diff --git a/crates/engine/src/cmd/table_resize/tests.rs b/crates/engine/src/cmd/table_resize/tests.rs index 5dfb58356f..a7af1247e9 100644 --- a/crates/engine/src/cmd/table_resize/tests.rs +++ b/crates/engine/src/cmd/table_resize/tests.rs @@ -211,6 +211,29 @@ fn ctrl_shares_a_border_drag_among_the_columns_right_of_it() { assert_eq!(share_out(&mut w, 3, 1.0, Share::Proportional), None); } +/// Columns a file gives narrower than the minimum (3 pt and 4 pt) aren't forced to it by a drag +/// between them: that took the right one to 1 pt, which the next import read as no width. +#[test] +fn a_drag_between_narrow_columns_leaves_them_as_they_are() { + for share in [Share::Neighbour, Share::Proportional, Share::LeftOnly] { + for dx in [0.0, -2.0, 2.0] { + let mut w = [3.0, 4.0, 100.0]; + share_out(&mut w, 0, dx, share); + assert!(w.iter().all(|x| *x > 1.0), "{share:?} by {dx}: {w:?}"); + if share == Share::Neighbour { + assert_eq!(w, [3.0, 4.0, 100.0], "by {dx}: no room on either side"); + } + } + } + // A narrow column still widens when its neighbour has room. + let mut w = [3.0, 100.0]; + assert_eq!(share_out(&mut w, 0, 10.0, Share::Neighbour), Some(10.0)); + assert_eq!(w, [13.0, 90.0]); + // Nothing moves for no drag. + let mut w = [3.0, 100.0]; + assert_eq!(share_out(&mut w, 0, 0.0, Share::Neighbour), Some(0.0)); +} + /// The table's left edge, grabbed from just outside it, moves the table in or out: its /// indent changes and the first column makes up the difference, so the other borders stay. /// A centred or right-aligned table keeps its alignment, as in Word: only the first column