1use crate::Value;
8use crate::eval::{EvalContext, eval_expression};
9use i_slint_compiler::llr::lower_layout_expression::MEASURE_KNOWN_W_LOCAL;
10use i_slint_compiler::llr::{BoxMeasureCell, Expression, FlexboxMeasureCell};
11use i_slint_core::SharedVector;
12use i_slint_core::layout::{
13 BoxLayoutData, FlexboxLayoutData, FlexboxLayoutItemInfo, GridLayoutData, GridLayoutInputData,
14 LayoutInfo, LayoutItemInfo, Padding,
15};
16use i_slint_core::model::Model;
17use i_slint_core::slice::Slice;
18
19fn to_f32(v: &Value) -> f32 {
22 match v {
23 Value::Number(n) => *n as f32,
24 _ => 0.,
25 }
26}
27
28fn to_padding(v: &Value) -> Padding {
29 let Value::Struct(s) = v else { return Padding::default() };
30 let f = |k| match s.get_field(k) {
31 Some(Value::Number(n)) => *n as f32,
32 _ => 0.,
33 };
34 Padding { begin: f("begin"), end: f("end") }
35}
36
37fn to_enum<T: std::str::FromStr + Default>(v: &Value) -> T {
38 match v {
39 Value::EnumerationValue(_, n) => n.parse().unwrap_or_default(),
40 _ => T::default(),
41 }
42}
43
44fn to_cells(v: &Value) -> Vec<LayoutItemInfo> {
45 let Value::Model(m) = v else { return Vec::new() };
46 (0..m.row_count())
47 .filter_map(|i| {
48 let Value::Struct(s) = m.row_data(i)? else { return None };
49 let c = s.get_field("constraint")?;
50 Some(LayoutItemInfo {
51 constraint: c.clone().try_into().unwrap_or_default(),
52 cross_axis_self_alignment: s
54 .get_field("cross-axis-self-alignment")
55 .map(to_enum)
56 .unwrap_or_default(),
57 layout_order: match s.get_field("layout-order") {
59 Some(Value::Number(n)) => *n as i32,
60 _ => 0,
61 },
62 })
63 })
64 .collect()
65}
66
67pub(crate) fn flexbox_item_info_from_struct(s: &crate::api::Struct) -> FlexboxLayoutItemInfo {
72 let constraint: LayoutInfo =
73 s.get_field("constraint").cloned().and_then(|v| v.try_into().ok()).unwrap_or_default();
74 let props = match s.get_field("props") {
75 Some(Value::Struct(p)) => flex_props_from_struct(p),
76 _ => Default::default(),
77 };
78 FlexboxLayoutItemInfo { constraint, props }
79}
80
81pub(crate) fn flex_props_from_struct(
84 s: &crate::api::Struct,
85) -> i_slint_core::layout::FlexItemProps {
86 i_slint_core::layout::FlexItemProps {
87 cross_axis_self_alignment: s
88 .get_field("cross-axis-self-alignment")
89 .map(to_enum)
90 .unwrap_or_default(),
91 layout_order: match s.get_field("layout-order") {
92 Some(Value::Number(n)) => *n as i32,
93 _ => 0,
94 },
95 }
96}
97
98fn to_flex_props(v: &Value) -> Vec<i_slint_core::layout::FlexItemProps> {
99 let Value::Model(m) = v else { return Vec::new() };
100 (0..m.row_count())
101 .filter_map(|i| {
102 let Value::Struct(s) = m.row_data(i)? else { return None };
103 Some(flex_props_from_struct(&s))
104 })
105 .collect()
106}
107
108fn to_u32_vec(v: &Value) -> Vec<u32> {
109 let Value::Model(m) = v else { return Vec::new() };
110 (0..m.row_count())
111 .filter_map(|i| match m.row_data(i)? {
112 Value::Number(n) => Some(n as u32),
113 _ => None,
114 })
115 .collect()
116}
117
118fn to_grid_input_data(v: &Value) -> Vec<GridLayoutInputData> {
119 let Value::Model(m) = v else { return Vec::new() };
120 (0..m.row_count())
121 .filter_map(|i| {
122 let Value::Struct(s) = m.row_data(i)? else { return None };
123 let f = |k: &str| match s.get_field(k) {
124 Some(Value::Number(n)) => *n as f32,
125 _ => 0.,
126 };
127 Some(GridLayoutInputData {
128 new_row: matches!(s.get_field("new-row"), Some(Value::Bool(true))),
129 col: f("col"),
130 row: f("row"),
131 colspan: f("colspan"),
132 rowspan: f("rowspan"),
133 })
134 })
135 .collect()
136}
137
138fn to_array_of_u16(v: &Value) -> SharedVector<u16> {
139 match v {
140 Value::ArrayOfU16(v) => v.clone(),
141 _ => Default::default(),
142 }
143}
144
145fn to_dialog_roles(v: &Value) -> Vec<i_slint_core::items::DialogButtonRole> {
146 let Value::Model(m) = v else { return Vec::new() };
147 (0..m.row_count())
148 .filter_map(|i| match m.row_data(i)? {
149 Value::EnumerationValue(_, n) => n.parse().ok(),
150 _ => None,
151 })
152 .collect()
153}
154
155fn sf32(s: &crate::api::Struct, k: &str) -> f32 {
156 match s.get_field(k) {
157 Some(Value::Number(n)) => *n as f32,
158 _ => 0.,
159 }
160}
161
162pub(crate) fn call_extra_builtin(
165 ctx: &mut EvalContext,
166 name: &str,
167 arguments: &[Expression],
168) -> Value {
169 let a: Vec<Value> = arguments.iter().map(|e| eval_expression(ctx, e)).collect();
170
171 match name {
172 "box_layout_info" => {
173 let c = to_cells(&a[0]);
174 i_slint_core::layout::box_layout_info(
175 Slice::from_slice(&c),
176 to_f32(&a[1]),
177 &to_padding(&a[2]),
178 to_enum(&a[3]),
179 )
180 .into()
181 }
182 "box_layout_info_ortho" => {
183 let c = to_cells(&a[0]);
184 i_slint_core::layout::box_layout_info_ortho(Slice::from_slice(&c), &to_padding(&a[1]))
185 .into()
186 }
187 "organize_dialog_button_layout" => {
188 let input = to_grid_input_data(&a[0]);
189 let roles = to_dialog_roles(&a[1]);
190 Value::ArrayOfU16(i_slint_core::layout::organize_dialog_button_layout(
191 Slice::from_slice(&input),
192 Slice::from_slice(&roles),
193 ))
194 }
195 "organize_grid_layout" => {
196 let (input, ri, rs) = (to_grid_input_data(&a[0]), to_u32_vec(&a[1]), to_u32_vec(&a[2]));
197 Value::ArrayOfU16(i_slint_core::layout::organize_grid_layout(
198 Slice::from_slice(&input),
199 Slice::from_slice(&ri),
200 Slice::from_slice(&rs),
201 ))
202 }
203 "grid_layout_info" => {
204 let (c, ri, rs) = (to_cells(&a[1]), to_u32_vec(&a[2]), to_u32_vec(&a[3]));
205 i_slint_core::layout::grid_layout_info(
206 to_array_of_u16(&a[0]),
207 Slice::from_slice(&c),
208 Slice::from_slice(&ri),
209 Slice::from_slice(&rs),
210 to_f32(&a[4]),
211 &to_padding(&a[5]),
212 to_enum(&a[6]),
213 )
214 .into()
215 }
216 "solve_grid_layout" => {
217 let (c, ri, rs) = (to_cells(&a[1]), to_u32_vec(&a[3]), to_u32_vec(&a[4]));
218 let Value::Struct(s) = &a[0] else { return Value::LayoutCache(Default::default()) };
219 Value::LayoutCache(i_slint_core::layout::solve_grid_layout(
220 &GridLayoutData {
221 size: sf32(s, "size"),
222 spacing: sf32(s, "spacing"),
223 padding: s.get_field("padding").map(to_padding).unwrap_or_default(),
224 organized_data: s
225 .get_field("organized-data")
226 .map(to_array_of_u16)
227 .unwrap_or_default(),
228 },
229 Slice::from_slice(&c),
230 to_enum(&a[2]),
231 Slice::from_slice(&ri),
232 Slice::from_slice(&rs),
233 ))
234 }
235 "solve_box_layout" => {
236 let ri = to_u32_vec(&a[1]);
237 let Value::Struct(s) = &a[0] else { return Value::LayoutCache(Default::default()) };
238 let cells = s.get_field("cells").map(to_cells).unwrap_or_default();
239 Value::LayoutCache(i_slint_core::layout::solve_box_layout(
240 &BoxLayoutData {
241 size: sf32(s, "size"),
242 spacing: sf32(s, "spacing"),
243 padding: s.get_field("padding").map(to_padding).unwrap_or_default(),
244 alignment: s.get_field("alignment").map(to_enum).unwrap_or_default(),
245 cells: Slice::from_slice(&cells),
246 },
247 Slice::from_slice(&ri),
248 ))
249 }
250 "solve_box_layout_ortho" => {
251 let ri = to_u32_vec(&a[1]);
252 let Value::Struct(s) = &a[0] else { return Value::LayoutCache(Default::default()) };
253 let cells = s.get_field("cells").map(to_cells).unwrap_or_default();
254 Value::LayoutCache(i_slint_core::layout::solve_box_layout_ortho(
255 &i_slint_core::layout::BoxLayoutOrthoData {
256 size: sf32(s, "size"),
257 padding: s.get_field("padding").map(to_padding).unwrap_or_default(),
258 cross_axis_alignment: s
259 .get_field("cross-axis-alignment")
260 .map(to_enum)
261 .unwrap_or_default(),
262 cells: Slice::from_slice(&cells),
263 },
264 Slice::from_slice(&ri),
265 ))
266 }
267 "solve_flexbox_layout" => {
268 let ri = to_u32_vec(&a[1]);
269 let Value::Struct(s) = &a[0] else { return Value::LayoutCache(Default::default()) };
270 let (ch, cv) = (
271 s.get_field("cells-h").map(to_cells).unwrap_or_default(),
272 s.get_field("cells-v").map(to_cells).unwrap_or_default(),
273 );
274 let fp = s.get_field("flex-props").map(to_flex_props).unwrap_or_default();
275 Value::LayoutCache(i_slint_core::layout::solve_flexbox_layout(
276 &FlexboxLayoutData {
277 width: sf32(s, "width"),
278 height: sf32(s, "height"),
279 spacing_h: sf32(s, "spacing_h"),
280 spacing_v: sf32(s, "spacing_v"),
281 padding_h: s.get_field("padding-h").map(to_padding).unwrap_or_default(),
282 padding_v: s.get_field("padding-v").map(to_padding).unwrap_or_default(),
283 alignment: s.get_field("alignment").map(to_enum).unwrap_or_default(),
284 direction: s.get_field("direction").map(to_enum).unwrap_or_default(),
285 cross_axis_line_alignment: s
286 .get_field("cross-axis-line-alignment")
287 .map(to_enum)
288 .unwrap_or_default(),
289 cross_axis_alignment: s
290 .get_field("cross-axis-alignment")
291 .map(to_enum)
292 .unwrap_or_default(),
293 flex_wrap: s.get_field("flex-wrap").map(to_enum).unwrap_or_default(),
294 cells_h: Slice::from_slice(&ch),
295 cells_v: Slice::from_slice(&cv),
296 flex_props: Slice::from_slice(&fp),
297 },
298 Slice::from_slice(&ri),
299 ))
300 }
301 "flexbox_layout_info_main_axis" => {
302 let cells = to_cells(&a[0]);
303 i_slint_core::layout::flexbox_layout_info_main_axis(
304 Slice::from_slice(&cells),
305 to_f32(&a[1]),
306 &to_padding(&a[2]),
307 to_enum(&a[3]),
308 )
309 .into()
310 }
311 "flexbox_layout_unwrapped_main" => {
312 let cells = to_cells(&a[0]);
313 Value::Number(i_slint_core::layout::flexbox_layout_unwrapped_main(
314 Slice::from_slice(&cells),
315 to_f32(&a[1]),
316 &to_padding(&a[2]),
317 ) as f64)
318 }
319 "flexbox_layout_info_cross_axis" => {
320 let (ch, cv) = (to_cells(&a[0]), to_cells(&a[1]));
321 let fp = to_flex_props(&a[2]);
322 i_slint_core::layout::flexbox_layout_info_cross_axis(
323 Slice::from_slice(&ch),
324 Slice::from_slice(&cv),
325 Slice::from_slice(&fp),
326 to_f32(&a[3]),
327 to_f32(&a[4]),
328 &to_padding(&a[5]),
329 &to_padding(&a[6]),
330 to_enum(&a[7]),
331 to_enum(&a[8]),
332 to_enum(&a[9]),
333 to_f32(&a[10]),
334 )
335 .into()
336 }
337 other => unimplemented!("ExtraBuiltinFunctionCall `{other}`"),
338 }
339}
340
341fn eval_info(ctx: &mut EvalContext, e: &Expression) -> LayoutInfo {
342 eval_expression(ctx, e).try_into().unwrap_or_default()
343}
344
345enum FlatCell<'a> {
348 Static {
349 v_info: &'a Expression,
350 },
351 Repeated(vtable::VRc<i_slint_core::item_tree::ItemTreeVTable, crate::instance::Instance>),
352 Fixed,
354}
355
356fn flatten_measure_cells<'a>(
360 ctx: &mut EvalContext,
361 measure_cells: &'a [FlexboxMeasureCell],
362) -> Vec<FlatCell<'a>> {
363 let mut flat: Vec<FlatCell> = Vec::with_capacity(measure_cells.len());
364 for item in measure_cells {
365 match item {
366 FlexboxMeasureCell::Static { v_info } => flat.push(FlatCell::Static { v_info }),
367 FlexboxMeasureCell::Repeated(repeater) => {
368 if let Some(current) = ctx.current.as_ref() {
369 let rep = ¤t.repeaters[repeater.repeater_index];
370 rep.track_instance_changes();
371 flat.extend(rep.instances_vec().into_iter().map(FlatCell::Repeated));
372 }
373 }
374 FlexboxMeasureCell::Fixed => flat.push(FlatCell::Fixed),
375 }
376 }
377 flat
378}
379
380fn measure_flexbox_cell(
386 ctx: &mut EvalContext,
387 flat: &[FlatCell],
388 index: usize,
389 w: f32,
390 h: f32,
391) -> (f32, f32) {
392 let Some(cell) = flat.get(index) else { return (w, h) };
393 match cell {
394 FlatCell::Static { v_info } => {
395 let prev = ctx.locals.insert(MEASURE_KNOWN_W_LOCAL.into(), Value::Number(w as f64));
396 let info = eval_info(ctx, v_info);
397 crate::eval::restore_local(ctx, MEASURE_KNOWN_W_LOCAL, prev);
398 (w, info.preferred_bounded())
399 }
400 FlatCell::Repeated(instance) => (
401 w,
402 instance
403 .as_pin_ref()
404 .flexbox_layout_item_info_at_cross_width(w)
405 .constraint
406 .preferred_bounded(),
407 ),
408 FlatCell::Fixed => (w, h),
409 }
410}
411
412pub(crate) fn solve_flexbox_layout_with_measure(ctx: &mut EvalContext, expr: &Expression) -> Value {
414 let Expression::SolveFlexboxLayoutWithMeasure { data, repeater_indices, measure_cells } = expr
415 else {
416 return Value::Void;
417 };
418 let ri = to_u32_vec(&eval_expression(ctx, repeater_indices));
419 let data = eval_expression(ctx, data);
420 let Value::Struct(s) = &data else { return Value::LayoutCache(Default::default()) };
421 let (ch, cv) = (
422 s.get_field("cells-h").map(to_cells).unwrap_or_default(),
423 s.get_field("cells-v").map(to_cells).unwrap_or_default(),
424 );
425 let fp = s.get_field("flex-props").map(to_flex_props).unwrap_or_default();
426
427 let flat = flatten_measure_cells(ctx, measure_cells);
428 let mut measure = |index: usize, w: f32, h: f32| measure_flexbox_cell(ctx, &flat, index, w, h);
429
430 Value::LayoutCache(i_slint_core::layout::solve_flexbox_layout_with_measure(
431 &FlexboxLayoutData {
432 width: sf32(s, "width"),
433 height: sf32(s, "height"),
434 spacing_h: sf32(s, "spacing_h"),
435 spacing_v: sf32(s, "spacing_v"),
436 padding_h: s.get_field("padding-h").map(to_padding).unwrap_or_default(),
437 padding_v: s.get_field("padding-v").map(to_padding).unwrap_or_default(),
438 alignment: s.get_field("alignment").map(to_enum).unwrap_or_default(),
439 direction: s.get_field("direction").map(to_enum).unwrap_or_default(),
440 cross_axis_line_alignment: s
441 .get_field("cross-axis-line-alignment")
442 .map(to_enum)
443 .unwrap_or_default(),
444 cross_axis_alignment: s
445 .get_field("cross-axis-alignment")
446 .map(to_enum)
447 .unwrap_or_default(),
448 flex_wrap: s.get_field("flex-wrap").map(to_enum).unwrap_or_default(),
449 cells_h: Slice::from_slice(&ch),
450 cells_v: Slice::from_slice(&cv),
451 flex_props: Slice::from_slice(&fp),
452 },
453 Slice::from_slice(&ri),
454 Some(&mut measure),
455 ))
456}
457
458pub(crate) fn box_layout_info_ortho_with_measure(
463 ctx: &mut EvalContext,
464 expr: &Expression,
465) -> Value {
466 use i_slint_core::model::RepeatedItemTree;
467 let Expression::BoxLayoutInfoOrthoWithMeasure { solve_data, padding_ortho, measure_cells } =
468 expr
469 else {
470 return Value::Void;
471 };
472 let data = eval_expression(ctx, solve_data);
473 let Value::Struct(s) = &data else { return LayoutInfo::default().into() };
474 let cells = s.get_field("cells").map(to_cells).unwrap_or_default();
475 let solved = i_slint_core::layout::solve_box_layout(
476 &BoxLayoutData {
477 size: sf32(s, "size"),
478 spacing: sf32(s, "spacing"),
479 padding: s.get_field("padding").map(to_padding).unwrap_or_default(),
480 alignment: s.get_field("alignment").map(to_enum).unwrap_or_default(),
481 cells: Slice::from_slice(&cells),
482 },
483 Slice::from_slice(&[]),
484 );
485 let solved_size = |cursor: usize| solved.as_slice().get(cursor * 2 + 1).copied().unwrap_or(0.);
486 let mut out_cells: Vec<LayoutItemInfo> = Vec::with_capacity(cells.len());
487 let mut cursor = 0usize;
488 for cell in measure_cells {
489 match cell {
490 BoxMeasureCell::Static { info } => {
491 let prev = ctx.locals.insert(
492 MEASURE_KNOWN_W_LOCAL.into(),
493 Value::Number(solved_size(cursor) as f64),
494 );
495 let constraint = eval_info(ctx, info);
496 crate::eval::restore_local(ctx, MEASURE_KNOWN_W_LOCAL, prev);
497 out_cells.push(LayoutItemInfo { constraint, ..Default::default() });
498 cursor += 1;
499 }
500 BoxMeasureCell::Repeated(repeater) => {
501 let Some(current) = ctx.current.as_ref() else {
502 debug_assert!(false, "measure pass evaluated without a current instance");
506 return LayoutInfo::default().into();
507 };
508 let rep = ¤t.repeaters[repeater.repeater_index];
509 rep.track_instance_changes();
510 for instance in rep.instances_vec() {
511 out_cells.push(
512 instance.as_pin_ref().layout_item_info_at_cross_width(solved_size(cursor)),
513 );
514 cursor += 1;
515 }
516 }
517 }
518 }
519 i_slint_core::layout::box_layout_info_ortho(
520 Slice::from_slice(&out_cells),
521 &to_padding(&eval_expression(ctx, padding_ortho)),
522 )
523 .into()
524}
525
526pub(crate) fn flexbox_layout_info_cross_axis_with_measure(
531 ctx: &mut EvalContext,
532 expr: &Expression,
533) -> Value {
534 let Expression::FlexboxLayoutInfoCrossAxisWithMeasure { arguments, measure_cells } = expr
535 else {
536 return Value::Void;
537 };
538 let a: Vec<Value> = arguments.iter().map(|e| eval_expression(ctx, e)).collect();
539 let (ch, cv) = (to_cells(&a[0]), to_cells(&a[1]));
540 let fp = to_flex_props(&a[2]);
541 let flat = flatten_measure_cells(ctx, measure_cells);
542 let mut measure = |index: usize, w: f32, h: f32| measure_flexbox_cell(ctx, &flat, index, w, h);
543 i_slint_core::layout::flexbox_layout_info_cross_axis_with_measure(
544 Slice::from_slice(&ch),
545 Slice::from_slice(&cv),
546 Slice::from_slice(&fp),
547 to_f32(&a[3]),
548 to_f32(&a[4]),
549 &to_padding(&a[5]),
550 &to_padding(&a[6]),
551 to_enum(&a[7]),
552 to_enum(&a[8]),
553 to_enum(&a[9]),
554 to_f32(&a[10]),
555 Some(&mut measure),
556 )
557 .into()
558}