✨ Size percent tracks by content in a grid that hugs

A grid that hugs its content along an axis took its percent tracks
there as percentages of its current size, which the hug then changed,
so the grid settled only over several reflows, in both engines. Along an
axis the grid hugs, percent tracks are now sized by their content, as
CSS does for a container without a definite size, and the grid settles
in one reflow.

Benchmark (lower quantile, ms):

| world                              | baseline |     now |    mean | ratio |
|------------------------------------+----------+---------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  22.420 |  23.267 |  0.64 |
| edit-through-a-chain-of-dependents |   68.931 |  42.715 |  52.079 |  0.62 |
| library-update-into-copies         |   83.083 |  59.152 |  66.682 |  0.71 |
| main-edit-among-many-shapes        |    4.837 |   3.469 |   3.887 |  0.72 |
| main-edit-into-10-copies           |    1.79  |   1.119 |   1.416 |  0.63 |
| main-edit-into-100-copies          |   15.415 |  10.790 |  12.449 |  0.70 |
| main-edit-into-1000-copies         |  153.086 | 108.911 | 129.565 |  0.71 |
| variant-switch-into-copies         |   32.052 |  23.968 |  26.842 |  0.75 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-14 00:58:27 +01:00
parent 10f573b327
commit 1ed798df25
3 changed files with 56 additions and 6 deletions

View File

@ -437,12 +437,19 @@
children)
;; Initialize tracks
;; a grid that hugs its content along an axis sizes its percent tracks
;; there by their content, as their percentages would depend on its size
hugged
(fn [tracks hug?]
(cond->> tracks
hug? (mapv #(cond-> % (= :percent (:type %)) (assoc :type :auto :value nil)))))
column-tracks
(->> layout-grid-columns
(->> (hugged layout-grid-columns auto-width?)
(mapv (partial calculate-initial-track-size bound-width)))
row-tracks
(->> layout-grid-rows
(->> (hugged layout-grid-rows auto-height?)
(mapv (partial calculate-initial-track-size bound-height)))
;; Go through cells to adjust auto sizes for span=1. Base is the max of its children

View File

@ -489,3 +489,25 @@
;; (100 - 40 + 7 - 3) / 2 and (30 - 56 + 5 - 0) / 2
(t/is (mth/close? 32.0 (:x corner) 0.01))
(t/is (mth/close? -10.5 (:y corner) 0.01))))
(t/deftest grid-that-hugs-sizes-its-percent-tracks-by-content
;; percentages of a size the grid takes from them would never settle
(let [child-id (uuid/next)
cell-id (uuid/next)
frame (-> (make-grid-frame :layout-grid-columns [{:type :percent :value 50.0}]
:layout-grid-rows [{:type :fixed :value 30.0}]
:layout-item-h-sizing :auto
:width 200 :height 30)
(assoc :shapes [child-id]
:layout-grid-cells {cell-id {:id cell-id :row 1 :column 1 :row-span 1 :column-span 1
:position :manual :align-self :auto :justify-self :auto
:shapes [child-id]}}))
child (cts/setup-shape {:id child-id :type :rect :x 0 :y 0 :width 40 :height 20
:parent-id (:id frame) :frame-id (:id frame)})
objects {uuid/zero (cts/setup-shape {:id uuid/zero :type :frame :x 0 :y 0 :width 1000 :height 1000
:shapes [(:id frame)]})
(:id frame) (assoc frame :parent-id uuid/zero :frame-id uuid/zero)
child-id child}
tree (gm/set-objects-modifiers {(:id frame) {:modifiers (ctm/reflow-modifiers)}} objects)
resized (gsh/transform-shape (get objects (:id frame)) (get-in tree [(:id frame) :modifiers]))]
(t/is (mth/close? 40.0 (:width (:selrect resized)) 0.01))))

View File

@ -67,7 +67,7 @@ pub fn calculate_tracks(
&grid_data.rows
};
let mut tracks = init_tracks(grid_tracks, layout_size);
let mut tracks = init_tracks(grid_tracks, layout_size, auto_layout);
set_auto_base_size(is_column, &mut tracks, cells, shapes, bounds);
set_auto_multi_span(is_column, &mut tracks, cells, shapes, bounds);
set_flex_multi_span(is_column, layout_data, &mut tracks, cells, shapes, bounds);
@ -83,17 +83,25 @@ pub fn calculate_tracks(
tracks
}
fn init_tracks(track: &[GridTrack], size: f32) -> Vec<TrackData> {
/// The tracks of one axis at their initial sizes. A grid that hugs its
/// content along the axis sizes its percent tracks by their content, as
/// their percentages would depend on its own size.
fn init_tracks(track: &[GridTrack], size: f32, auto_layout: bool) -> Vec<TrackData> {
track
.iter()
.map(|t| {
let (size, max_size) = match t.track_type {
let track_type = if auto_layout && t.track_type == GridTrackType::Percent {
GridTrackType::Auto
} else {
t.track_type
};
let (size, max_size) = match track_type {
GridTrackType::Fixed => (t.value, t.value),
GridTrackType::Percent => (size * t.value / 100.0, size * t.value / 100.0),
_ => (MIN_SIZE, MAX_SIZE),
};
TrackData {
track_type: t.track_type,
track_type,
value: t.value,
size,
max_size,
@ -1008,6 +1016,19 @@ mod tests {
}
}
#[test]
fn test_init_tracks_sizes_percent_tracks_by_content_while_hugging() {
let tracks = vec![GridTrack {
track_type: GridTrackType::Percent,
value: 50.0,
}];
let fixed = init_tracks(&tracks, 200.0, false);
assert_eq!(fixed[0].size, 100.0);
let hugging = init_tracks(&tracks, 200.0, true);
assert!(hugging[0].track_type == GridTrackType::Auto);
assert_eq!(hugging[0].size, MIN_SIZE);
}
#[test]
fn test_cell_offset_places_the_margin_box() {
assert_eq!(cell_offset(CellAlignment::Start, 30.0, 56.0, 5.0, 3.0), 5.0);