Skip to content

Commit 9915b96

Browse files
authored
Stylo integration (#693)
* Make return type of grid track list functions generic * Initial taffy_stylo crate (flexbox only) * Use blitz branch of stylo * Implement grid type conversions in taffy_stylo * Fix clippy lints * Use stylo main * Module rename / reorganisation
1 parent 902c322 commit 9915b96

File tree

10 files changed

+1033
-52
lines changed

10 files changed

+1033
-52
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,5 @@ members = [
101101
"scripts/format-fixtures",
102102
"scripts/import-yoga-tests",
103103
"benches",
104+
"taffy_stylo",
104105
]

src/compute/grid/explicit_grid.rs

Lines changed: 121 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ use num_traits::float::FloatCore;
1515
/// Compute the number of rows and columns in the explicit grid
1616
pub(crate) fn compute_explicit_grid_size_in_axis(
1717
style: &impl GridContainerStyle,
18+
template: &[TrackSizingFunction],
1819
inner_container_size: Size<Option<f32>>,
1920
axis: AbsoluteAxis,
2021
) -> u16 {
21-
// Load the grid-template-rows or grid-template-columns definition (depending on the axis)
22-
let template = style.grid_template_tracks(axis);
23-
2422
// If template contains no tracks, then there are trivially zero explicit tracks
2523
if template.is_empty() {
2624
return 0;
@@ -294,8 +292,18 @@ mod test {
294292
fn explicit_grid_sizing_no_repeats() {
295293
let grid_style = (600.0, 600.0, 2, 4).into_grid();
296294
let preferred_size = grid_style.size.map(|s| s.into_option());
297-
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
298-
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
295+
let width = compute_explicit_grid_size_in_axis(
296+
&grid_style,
297+
&grid_style.grid_template_columns,
298+
preferred_size,
299+
AbsoluteAxis::Horizontal,
300+
);
301+
let height = compute_explicit_grid_size_in_axis(
302+
&grid_style,
303+
&grid_style.grid_template_rows,
304+
preferred_size,
305+
AbsoluteAxis::Vertical,
306+
);
299307
assert_eq!(width, 2);
300308
assert_eq!(height, 4);
301309
}
@@ -311,8 +319,18 @@ mod test {
311319
..Default::default()
312320
};
313321
let preferred_size = grid_style.size.map(|s| s.into_option());
314-
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
315-
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
322+
let width = compute_explicit_grid_size_in_axis(
323+
&grid_style,
324+
&grid_style.grid_template_columns,
325+
preferred_size,
326+
AbsoluteAxis::Horizontal,
327+
);
328+
let height = compute_explicit_grid_size_in_axis(
329+
&grid_style,
330+
&grid_style.grid_template_rows,
331+
preferred_size,
332+
AbsoluteAxis::Vertical,
333+
);
316334
assert_eq!(width, 3);
317335
assert_eq!(height, 4);
318336
}
@@ -328,8 +346,18 @@ mod test {
328346
..Default::default()
329347
};
330348
let preferred_size = grid_style.size.map(|s| s.into_option());
331-
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
332-
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
349+
let width = compute_explicit_grid_size_in_axis(
350+
&grid_style,
351+
&grid_style.grid_template_columns,
352+
preferred_size,
353+
AbsoluteAxis::Horizontal,
354+
);
355+
let height = compute_explicit_grid_size_in_axis(
356+
&grid_style,
357+
&grid_style.grid_template_rows,
358+
preferred_size,
359+
AbsoluteAxis::Vertical,
360+
);
333361
assert_eq!(width, 3);
334362
assert_eq!(height, 4);
335363
}
@@ -345,8 +373,18 @@ mod test {
345373
..Default::default()
346374
};
347375
let inner_container_size = Size { width: Some(120.0), height: Some(80.0) };
348-
let width = compute_explicit_grid_size_in_axis(&grid_style, inner_container_size, AbsoluteAxis::Horizontal);
349-
let height = compute_explicit_grid_size_in_axis(&grid_style, inner_container_size, AbsoluteAxis::Vertical);
376+
let width = compute_explicit_grid_size_in_axis(
377+
&grid_style,
378+
&grid_style.grid_template_columns,
379+
inner_container_size,
380+
AbsoluteAxis::Horizontal,
381+
);
382+
let height = compute_explicit_grid_size_in_axis(
383+
&grid_style,
384+
&grid_style.grid_template_rows,
385+
inner_container_size,
386+
AbsoluteAxis::Vertical,
387+
);
350388
assert_eq!(width, 3);
351389
assert_eq!(height, 4);
352390
}
@@ -362,8 +400,18 @@ mod test {
362400
..Default::default()
363401
};
364402
let inner_container_size = Size { width: Some(140.0), height: Some(90.0) };
365-
let width = compute_explicit_grid_size_in_axis(&grid_style, inner_container_size, AbsoluteAxis::Horizontal);
366-
let height = compute_explicit_grid_size_in_axis(&grid_style, inner_container_size, AbsoluteAxis::Vertical);
403+
let width = compute_explicit_grid_size_in_axis(
404+
&grid_style,
405+
&grid_style.grid_template_columns,
406+
inner_container_size,
407+
AbsoluteAxis::Horizontal,
408+
);
409+
let height = compute_explicit_grid_size_in_axis(
410+
&grid_style,
411+
&grid_style.grid_template_rows,
412+
inner_container_size,
413+
AbsoluteAxis::Vertical,
414+
);
367415
assert_eq!(width, 4);
368416
assert_eq!(height, 5);
369417
}
@@ -379,8 +427,18 @@ mod test {
379427
..Default::default()
380428
};
381429
let preferred_size = grid_style.size.map(|s| s.into_option());
382-
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
383-
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
430+
let width = compute_explicit_grid_size_in_axis(
431+
&grid_style,
432+
&grid_style.grid_template_columns,
433+
preferred_size,
434+
AbsoluteAxis::Horizontal,
435+
);
436+
let height = compute_explicit_grid_size_in_axis(
437+
&grid_style,
438+
&grid_style.grid_template_rows,
439+
preferred_size,
440+
AbsoluteAxis::Vertical,
441+
);
384442
assert_eq!(width, 4); // 2 repetitions * 2 repeated tracks = 4 tracks in total
385443
assert_eq!(height, 6); // 3 repetitions * 2 repeated tracks = 4 tracks in total
386444
}
@@ -397,8 +455,18 @@ mod test {
397455
..Default::default()
398456
};
399457
let preferred_size = grid_style.size.map(|s| s.into_option());
400-
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
401-
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
458+
let width = compute_explicit_grid_size_in_axis(
459+
&grid_style,
460+
&grid_style.grid_template_columns,
461+
preferred_size,
462+
AbsoluteAxis::Horizontal,
463+
);
464+
let height = compute_explicit_grid_size_in_axis(
465+
&grid_style,
466+
&grid_style.grid_template_rows,
467+
preferred_size,
468+
AbsoluteAxis::Vertical,
469+
);
402470
assert_eq!(width, 2); // 2 tracks + 1 gap
403471
assert_eq!(height, 3); // 3 tracks + 2 gaps
404472
}
@@ -414,8 +482,18 @@ mod test {
414482
..Default::default()
415483
};
416484
let preferred_size = grid_style.size.map(|s| s.into_option());
417-
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
418-
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
485+
let width = compute_explicit_grid_size_in_axis(
486+
&grid_style,
487+
&grid_style.grid_template_columns,
488+
preferred_size,
489+
AbsoluteAxis::Horizontal,
490+
);
491+
let height = compute_explicit_grid_size_in_axis(
492+
&grid_style,
493+
&grid_style.grid_template_rows,
494+
preferred_size,
495+
AbsoluteAxis::Vertical,
496+
);
419497
assert_eq!(width, 3);
420498
assert_eq!(height, 1);
421499
}
@@ -432,8 +510,18 @@ mod test {
432510
..Default::default()
433511
};
434512
let preferred_size = grid_style.size.map(|s| s.into_option());
435-
let width = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Horizontal);
436-
let height = compute_explicit_grid_size_in_axis(&grid_style, preferred_size, AbsoluteAxis::Vertical);
513+
let width = compute_explicit_grid_size_in_axis(
514+
&grid_style,
515+
&grid_style.grid_template_columns,
516+
preferred_size,
517+
AbsoluteAxis::Horizontal,
518+
);
519+
let height = compute_explicit_grid_size_in_axis(
520+
&grid_style,
521+
&grid_style.grid_template_rows,
522+
preferred_size,
523+
AbsoluteAxis::Vertical,
524+
);
437525
assert_eq!(width, 3); // 3 tracks + 2 gaps
438526
assert_eq!(height, 2); // 2 tracks + 1 gap
439527
}
@@ -450,8 +538,18 @@ mod test {
450538
..Default::default()
451539
};
452540
let inner_container_size = Size { width: Some(100.0), height: Some(80.0) };
453-
let width = compute_explicit_grid_size_in_axis(&grid_style, inner_container_size, AbsoluteAxis::Horizontal);
454-
let height = compute_explicit_grid_size_in_axis(&grid_style, inner_container_size, AbsoluteAxis::Vertical);
541+
let width = compute_explicit_grid_size_in_axis(
542+
&grid_style,
543+
&grid_style.grid_template_columns,
544+
inner_container_size,
545+
AbsoluteAxis::Horizontal,
546+
);
547+
let height = compute_explicit_grid_size_in_axis(
548+
&grid_style,
549+
&grid_style.grid_template_rows,
550+
inner_container_size,
551+
AbsoluteAxis::Vertical,
552+
);
455553
assert_eq!(width, 5); // 40px horizontal padding
456554
assert_eq!(height, 4); // 20px vertical padding
457555
}

src/compute/grid/mod.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! This module is a partial implementation of the CSS Grid Level 1 specification
22
//! <https://www.w3.org/TR/css-grid-1>
3+
use core::borrow::Borrow;
4+
35
use crate::geometry::{AbsoluteAxis, AbstractAxis, InBothAbsAxis};
46
use crate::geometry::{Line, Point, Rect, Size};
57
use crate::style::{AlignItems, AlignSelf, AvailableSpace, Overflow, Position};
@@ -89,6 +91,13 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
8991
let align_items = style.align_items();
9092
let justify_items = style.justify_items();
9193

94+
// Note: we avoid accessing the grid rows/columns methods more than once as this can
95+
// cause an expensive-ish computation
96+
let grid_template_columms = style.grid_template_columns();
97+
let grid_template_rows = style.grid_template_rows();
98+
let grid_auto_columms = style.grid_auto_columns();
99+
let grid_auto_rows = style.grid_auto_rows();
100+
92101
let constrained_available_space = known_dimensions
93102
.or(preferred_size)
94103
.map(|size| size.map(AvailableSpace::Definite))
@@ -137,10 +146,18 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
137146
.maybe_sub(content_box_inset.sum_axes());
138147

139148
// Exactly compute the number of rows and columns in the explicit grid.
140-
let explicit_col_count =
141-
compute_explicit_grid_size_in_axis(&style, auto_fit_container_size, AbsoluteAxis::Horizontal);
142-
let explicit_row_count =
143-
compute_explicit_grid_size_in_axis(&style, auto_fit_container_size, AbsoluteAxis::Vertical);
149+
let explicit_col_count = compute_explicit_grid_size_in_axis(
150+
&style,
151+
grid_template_columms.borrow(),
152+
auto_fit_container_size,
153+
AbsoluteAxis::Horizontal,
154+
);
155+
let explicit_row_count = compute_explicit_grid_size_in_axis(
156+
&style,
157+
grid_template_rows.borrow(),
158+
auto_fit_container_size,
159+
AbsoluteAxis::Vertical,
160+
);
144161

145162
// 3. Implicit Grid: Estimate Track Counts
146163
// Estimate the number of rows and columns in the implicit grid (= the entire grid)
@@ -181,20 +198,24 @@ pub fn compute_grid_layout(tree: &mut impl LayoutGridContainer, node: NodeId, in
181198
initialize_grid_tracks(
182199
&mut columns,
183200
final_col_counts,
184-
style.grid_template_columns(),
185-
style.grid_auto_columns(),
201+
grid_template_columms.borrow(),
202+
grid_auto_columms.borrow(),
186203
style.gap().width,
187204
|column_index| cell_occupancy_matrix.column_is_occupied(column_index),
188205
);
189206
initialize_grid_tracks(
190207
&mut rows,
191208
final_row_counts,
192-
style.grid_template_rows(),
193-
style.grid_auto_rows(),
209+
grid_template_rows.borrow(),
210+
grid_auto_rows.borrow(),
194211
style.gap().height,
195212
|row_index| cell_occupancy_matrix.row_is_occupied(row_index),
196213
);
197214

215+
drop(grid_template_rows);
216+
drop(grid_template_columms);
217+
drop(grid_auto_rows);
218+
drop(grid_auto_columms);
198219
drop(style);
199220

200221
// 6. Track Sizing

src/style/grid.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,33 @@ use crate::compute::grid::{GridCoordinate, GridLine, OriginZeroLine};
44
use crate::geometry::{AbsoluteAxis, AbstractAxis, Line, MinMax, Size};
55
use crate::style_helpers::*;
66
use crate::util::sys::GridTrackVec;
7+
use core::borrow::Borrow;
78
use core::cmp::{max, min};
89
use core::convert::Infallible;
910

1011
/// The set of styles required for a CSS Grid container
1112
pub trait GridContainerStyle: CoreStyle {
13+
/// The type returned by grid_template_rows and grid_template_columns
14+
type TemplateTrackList<'a>: Borrow<[TrackSizingFunction]>
15+
where
16+
Self: 'a;
17+
/// The type returned by grid_auto_rows and grid_auto_columns
18+
type AutoTrackList<'a>: Borrow<[NonRepeatedTrackSizingFunction]>
19+
where
20+
Self: 'a;
21+
22+
// FIXME: re-add default implemenations for grid_{template,auto}_{rows,columns} once the
23+
// associated_type_defaults feature (https://github.com/rust-lang/rust/issues/29661) is stabilised.
24+
1225
/// Defines the track sizing functions (heights) of the grid rows
13-
#[inline(always)]
14-
fn grid_template_rows(&self) -> &[TrackSizingFunction] {
15-
&[]
16-
}
26+
fn grid_template_rows(&self) -> Self::TemplateTrackList<'_>;
1727
/// Defines the track sizing functions (widths) of the grid columns
18-
#[inline(always)]
19-
fn grid_template_columns(&self) -> &[TrackSizingFunction] {
20-
&[]
21-
}
28+
fn grid_template_columns(&self) -> Self::TemplateTrackList<'_>;
2229
/// Defines the size of implicitly created rows
23-
#[inline(always)]
24-
fn grid_auto_rows(&self) -> &[NonRepeatedTrackSizingFunction] {
25-
&[]
26-
}
30+
fn grid_auto_rows(&self) -> Self::AutoTrackList<'_>;
2731
/// Defined the size of implicitly created columns
28-
#[inline(always)]
29-
fn grid_auto_columns(&self) -> &[NonRepeatedTrackSizingFunction] {
30-
&[]
31-
}
32+
fn grid_auto_columns(&self) -> Self::AutoTrackList<'_>;
33+
3234
/// Controls how items get placed into the grid for auto-placed items
3335
#[inline(always)]
3436
fn grid_auto_flow(&self) -> GridAutoFlow {
@@ -66,7 +68,7 @@ pub trait GridContainerStyle: CoreStyle {
6668

6769
/// Get a grid item's row or column placement depending on the axis passed
6870
#[inline(always)]
69-
fn grid_template_tracks(&self, axis: AbsoluteAxis) -> &[TrackSizingFunction] {
71+
fn grid_template_tracks(&self, axis: AbsoluteAxis) -> Self::TemplateTrackList<'_> {
7072
match axis {
7173
AbsoluteAxis::Horizontal => self.grid_template_columns(),
7274
AbsoluteAxis::Vertical => self.grid_template_rows(),

0 commit comments

Comments
 (0)