diff --git a/common/src/app/common/geom/shapes/grid_layout/layout_data.cljc b/common/src/app/common/geom/shapes/grid_layout/layout_data.cljc index d089faff2d..a259ddf9a2 100644 --- a/common/src/app/common/geom/shapes/grid_layout/layout_data.cljc +++ b/common/src/app/common/geom/shapes/grid_layout/layout_data.cljc @@ -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 diff --git a/common/test/common_tests/geom_grid_layout_test.cljc b/common/test/common_tests/geom_grid_layout_test.cljc index 54b3c7fedc..a78facf3b7 100644 --- a/common/test/common_tests/geom_grid_layout_test.cljc +++ b/common/test/common_tests/geom_grid_layout_test.cljc @@ -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)))) diff --git a/render-wasm/src/shapes/modifiers/grid_layout.rs b/render-wasm/src/shapes/modifiers/grid_layout.rs index 02ae3947b0..3e8d0cddfe 100644 --- a/render-wasm/src/shapes/modifiers/grid_layout.rs +++ b/render-wasm/src/shapes/modifiers/grid_layout.rs @@ -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 { +/// 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 { 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);