Skip to content

Commit 8ae28ad

Browse files
dfaure-kdabogoffart
authored andcommitted
GridLayout: introduce an Organize step to compute row/col
This allows to store the result into a different cache property than the one that comes out of solve_grid_layout, which fixes a binding loop when the text of a widget depends on its row or col value.
1 parent aef30ac commit 8ae28ad

File tree

16 files changed

+557
-274
lines changed

16 files changed

+557
-274
lines changed

api/cpp/include/slint.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,32 @@ inline SharedVector<float> solve_box_layout(const cbindgen_private::BoxLayoutDat
123123
return result;
124124
}
125125

126-
inline SharedVector<float> solve_grid_layout(const cbindgen_private::GridLayoutData &data,
127-
cbindgen_private::Orientation orientation)
126+
inline SharedVector<float>
127+
organize_grid_layout(cbindgen_private::Slice<cbindgen_private::GridLayoutInputData> input_data)
128128
{
129129
SharedVector<float> result;
130-
cbindgen_private::slint_solve_grid_layout(&data, orientation, &result);
130+
cbindgen_private::slint_organize_grid_layout(input_data, &result);
131+
return result;
132+
}
133+
134+
inline SharedVector<float>
135+
solve_grid_layout(const cbindgen_private::GridLayoutData &data,
136+
cbindgen_private::Slice<cbindgen_private::LayoutInfo> constraints,
137+
cbindgen_private::Orientation orientation)
138+
{
139+
SharedVector<float> result;
140+
cbindgen_private::slint_solve_grid_layout(&data, constraints, orientation, &result);
131141
return result;
132142
}
133143

134144
inline cbindgen_private::LayoutInfo
135-
grid_layout_info(cbindgen_private::Slice<cbindgen_private::GridLayoutCellData> cells, float spacing,
136-
const cbindgen_private::Padding &padding, cbindgen_private::Orientation o)
145+
grid_layout_info(const cbindgen_private::GridLayoutOrganizedData &organized_data,
146+
cbindgen_private::Slice<cbindgen_private::LayoutInfo> constraints, float spacing,
147+
const cbindgen_private::Padding &padding,
148+
cbindgen_private::Orientation orientation)
137149
{
138-
return cbindgen_private::slint_grid_layout_info(cells, spacing, &padding, o);
150+
return cbindgen_private::slint_grid_layout_info(&organized_data, constraints, spacing, &padding,
151+
orientation);
139152
}
140153

141154
inline cbindgen_private::LayoutInfo

internal/compiler/expression_tree.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,24 @@ pub enum Expression {
752752
/// So this looks like `layout_cache_prop[layout_cache_prop[index] + repeater_index]`
753753
repeater_index: Option<Box<Expression>>,
754754
},
755+
756+
/// Organize a grid layout, i.e. decide what goes where
757+
OrganizeGridLayout(crate::layout::GridLayout),
758+
755759
/// Compute the LayoutInfo for the given layout.
756760
/// The orientation is the orientation of the cache, not the orientation of the layout
757761
ComputeLayoutInfo(crate::layout::Layout, crate::layout::Orientation),
762+
ComputeGridLayoutInfo {
763+
layout_organized_data_prop: NamedReference,
764+
layout: crate::layout::GridLayout,
765+
orientation: crate::layout::Orientation,
766+
},
758767
SolveLayout(crate::layout::Layout, crate::layout::Orientation),
768+
SolveGridLayout {
769+
layout_organized_data_prop: NamedReference,
770+
layout: crate::layout::GridLayout,
771+
orientation: crate::layout::Orientation,
772+
},
759773

760774
MinMax {
761775
ty: Type,
@@ -883,8 +897,11 @@ impl Expression {
883897
// invalid because the expression is unreachable
884898
Expression::ReturnStatement(_) => Type::Invalid,
885899
Expression::LayoutCacheAccess { .. } => Type::LogicalLength,
900+
Expression::OrganizeGridLayout(..) => typeregister::organized_layout_type().into(),
886901
Expression::ComputeLayoutInfo(..) => typeregister::layout_info_type().into(),
902+
Expression::ComputeGridLayoutInfo { .. } => typeregister::layout_info_type().into(),
887903
Expression::SolveLayout(..) => Type::LayoutCache,
904+
Expression::SolveGridLayout { .. } => Type::LayoutCache,
888905
Expression::MinMax { ty, .. } => ty.clone(),
889906
Expression::EmptyComponentFactory => Type::ComponentFactory,
890907
Expression::DebugHook { expression, .. } => expression.ty(),
@@ -982,8 +999,11 @@ impl Expression {
982999
Expression::LayoutCacheAccess { repeater_index, .. } => {
9831000
repeater_index.as_deref().map(visitor);
9841001
}
1002+
Expression::OrganizeGridLayout(..) => {}
9851003
Expression::ComputeLayoutInfo(..) => {}
1004+
Expression::ComputeGridLayoutInfo { .. } => {}
9861005
Expression::SolveLayout(..) => {}
1006+
Expression::SolveGridLayout { .. } => {}
9871007
Expression::MinMax { lhs, rhs, .. } => {
9881008
visitor(lhs);
9891009
visitor(rhs);
@@ -1086,8 +1106,11 @@ impl Expression {
10861106
Expression::LayoutCacheAccess { repeater_index, .. } => {
10871107
repeater_index.as_deref_mut().map(visitor);
10881108
}
1109+
Expression::OrganizeGridLayout(..) => {}
10891110
Expression::ComputeLayoutInfo(..) => {}
1111+
Expression::ComputeGridLayoutInfo { .. } => {}
10901112
Expression::SolveLayout(..) => {}
1113+
Expression::SolveGridLayout { .. } => {}
10911114
Expression::MinMax { lhs, rhs, .. } => {
10921115
visitor(lhs);
10931116
visitor(rhs);
@@ -1180,8 +1203,11 @@ impl Expression {
11801203
}
11811204
// TODO: detect constant property within layouts
11821205
Expression::LayoutCacheAccess { .. } => false,
1206+
Expression::OrganizeGridLayout { .. } => false,
11831207
Expression::ComputeLayoutInfo(..) => false,
1208+
Expression::ComputeGridLayoutInfo { .. } => false,
11841209
Expression::SolveLayout(..) => false,
1210+
Expression::SolveGridLayout { .. } => false,
11851211
Expression::MinMax { lhs, rhs, .. } => lhs.is_constant(ga) && rhs.is_constant(ga),
11861212
Expression::EmptyComponentFactory => true,
11871213
Expression::DebugHook { .. } => false,
@@ -1856,8 +1882,11 @@ pub fn pretty_print(f: &mut dyn std::fmt::Write, expression: &Expression) -> std
18561882
if repeater_index.is_some() { " + $index" } else { "" }
18571883
)
18581884
}
1885+
Expression::OrganizeGridLayout(..) => write!(f, "organize_grid_layout(..)"),
18591886
Expression::ComputeLayoutInfo(..) => write!(f, "layout_info(..)"),
1887+
Expression::ComputeGridLayoutInfo { .. } => write!(f, "grid_layout_info(..)"),
18601888
Expression::SolveLayout(..) => write!(f, "solve_layout(..)"),
1889+
Expression::SolveGridLayout { .. } => write!(f, "solve_grid_layout(..)"),
18611890
Expression::MinMax { ty: _, op, lhs, rhs } => {
18621891
match op {
18631892
MinMaxOp::Min => write!(f, "min(")?,

internal/compiler/langtype.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ pub enum BuiltinPrivateStruct {
644644
PropertyAnimation,
645645
GridLayoutCellData,
646646
GridLayoutData,
647+
GridLayoutInputData,
647648
BoxLayoutData,
648649
BoxLayoutCellData,
649650
Padding,
@@ -661,7 +662,7 @@ pub enum BuiltinPrivateStruct {
661662

662663
impl BuiltinPrivateStruct {
663664
pub fn is_layout_data(&self) -> bool {
664-
matches!(self, Self::GridLayoutData | Self::BoxLayoutData)
665+
matches!(self, Self::GridLayoutInputData | Self::GridLayoutData | Self::BoxLayoutData)
665666
}
666667
pub fn slint_name(&self) -> Option<SmolStr> {
667668
match self {

internal/compiler/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ pub struct GridLayout {
465465
}
466466

467467
impl GridLayout {
468-
fn visit_named_references(&mut self, visitor: &mut impl FnMut(&mut NamedReference)) {
468+
pub fn visit_named_references(&mut self, visitor: &mut impl FnMut(&mut NamedReference)) {
469469
for cell in &mut self.elems {
470470
cell.item.constraints.visit_named_references(visitor);
471471
}

0 commit comments

Comments
 (0)