Skip to content

Commit 0ba6261

Browse files
committed
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 901db89 commit 0ba6261

File tree

15 files changed

+556
-273
lines changed

15 files changed

+556
-273
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
@@ -754,10 +754,24 @@ pub enum Expression {
754754
/// So this looks like `layout_cache_prop[layout_cache_prop[index] + repeater_index]`
755755
repeater_index: Option<Box<Expression>>,
756756
},
757+
758+
/// Organize a grid layout, i.e. decide what goes where
759+
OrganizeGridLayout(crate::layout::GridLayout),
760+
757761
/// Compute the LayoutInfo for the given layout.
758762
/// The orientation is the orientation of the cache, not the orientation of the layout
759763
ComputeLayoutInfo(crate::layout::Layout, crate::layout::Orientation),
764+
ComputeGridLayoutInfo {
765+
layout_organized_data_prop: NamedReference,
766+
layout: crate::layout::GridLayout,
767+
orientation: crate::layout::Orientation,
768+
},
760769
SolveLayout(crate::layout::Layout, crate::layout::Orientation),
770+
SolveGridLayout {
771+
layout_organized_data_prop: NamedReference,
772+
layout: crate::layout::GridLayout,
773+
orientation: crate::layout::Orientation,
774+
},
761775

762776
MinMax {
763777
ty: Type,
@@ -889,8 +903,11 @@ impl Expression {
889903
// invalid because the expression is unreachable
890904
Expression::ReturnStatement(_) => Type::Invalid,
891905
Expression::LayoutCacheAccess { .. } => Type::LogicalLength,
906+
Expression::OrganizeGridLayout(..) => typeregister::organized_layout_type().into(),
892907
Expression::ComputeLayoutInfo(..) => typeregister::layout_info_type().into(),
908+
Expression::ComputeGridLayoutInfo { .. } => typeregister::layout_info_type().into(),
893909
Expression::SolveLayout(..) => Type::LayoutCache,
910+
Expression::SolveGridLayout { .. } => Type::LayoutCache,
894911
Expression::MinMax { ty, .. } => ty.clone(),
895912
Expression::EmptyComponentFactory => Type::ComponentFactory,
896913
Expression::DebugHook { expression, .. } => expression.ty(),
@@ -987,8 +1004,11 @@ impl Expression {
9871004
Expression::LayoutCacheAccess { repeater_index, .. } => {
9881005
repeater_index.as_deref().map(visitor);
9891006
}
1007+
Expression::OrganizeGridLayout(..) => {}
9901008
Expression::ComputeLayoutInfo(..) => {}
1009+
Expression::ComputeGridLayoutInfo { .. } => {}
9911010
Expression::SolveLayout(..) => {}
1011+
Expression::SolveGridLayout { .. } => {}
9921012
Expression::MinMax { lhs, rhs, .. } => {
9931013
visitor(lhs);
9941014
visitor(rhs);
@@ -1090,8 +1110,11 @@ impl Expression {
10901110
Expression::LayoutCacheAccess { repeater_index, .. } => {
10911111
repeater_index.as_deref_mut().map(visitor);
10921112
}
1113+
Expression::OrganizeGridLayout(..) => {}
10931114
Expression::ComputeLayoutInfo(..) => {}
1115+
Expression::ComputeGridLayoutInfo { .. } => {}
10941116
Expression::SolveLayout(..) => {}
1117+
Expression::SolveGridLayout { .. } => {}
10951118
Expression::MinMax { lhs, rhs, .. } => {
10961119
visitor(lhs);
10971120
visitor(rhs);
@@ -1183,8 +1206,11 @@ impl Expression {
11831206
}
11841207
// TODO: detect constant property within layouts
11851208
Expression::LayoutCacheAccess { .. } => false,
1209+
Expression::OrganizeGridLayout { .. } => false,
11861210
Expression::ComputeLayoutInfo(..) => false,
1211+
Expression::ComputeGridLayoutInfo { .. } => false,
11871212
Expression::SolveLayout(..) => false,
1213+
Expression::SolveGridLayout { .. } => false,
11881214
Expression::MinMax { lhs, rhs, .. } => lhs.is_constant(ga) && rhs.is_constant(ga),
11891215
Expression::EmptyComponentFactory => true,
11901216
Expression::DebugHook { .. } => false,
@@ -1858,8 +1884,11 @@ pub fn pretty_print(f: &mut dyn std::fmt::Write, expression: &Expression) -> std
18581884
if repeater_index.is_some() { " + $index" } else { "" }
18591885
)
18601886
}
1887+
Expression::OrganizeGridLayout(..) => write!(f, "organize_grid_layout(..)"),
18611888
Expression::ComputeLayoutInfo(..) => write!(f, "layout_info(..)"),
1889+
Expression::ComputeGridLayoutInfo { .. } => write!(f, "grid_layout_info(..)"),
18621890
Expression::SolveLayout(..) => write!(f, "solve_layout(..)"),
1891+
Expression::SolveGridLayout { .. } => write!(f, "solve_grid_layout(..)"),
18631892
Expression::MinMax { ty: _, op, lhs, rhs } => {
18641893
match op {
18651894
MinMaxOp::Min => write!(f, "min(")?,

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)