i-slint-compiler 1.18.0

Internal Slint Compiler Library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

//! Datastructures used to represent layouts in the compiler

use crate::diagnostics::{BuildDiagnostics, DiagnosticLevel, Spanned};
use crate::expression_tree::*;
use crate::langtype::{ElementType, PropertyLookupMode, PropertyLookupResult, Type};
use crate::object_tree::{Component, ElementRc};

use smol_str::{SmolStr, ToSmolStr};

use std::cell::RefCell;
use std::rc::{Rc, Weak};

/// Number of slots a cell occupies in a box layout cache: position and size.
pub const BOX_LAYOUT_CACHE_ENTRIES_PER_CELL: usize = 2;

#[derive(Clone, Debug, Copy, Eq, PartialEq)]
pub enum Orientation {
    Horizontal,
    Vertical,
}

impl Orientation {
    pub fn orthogonal(self) -> Self {
        match self {
            Orientation::Horizontal => Orientation::Vertical,
            Orientation::Vertical => Orientation::Horizontal,
        }
    }
}

#[derive(Clone, Debug, Copy, Eq, PartialEq, Default)]
pub enum FlexboxLayoutDirection {
    /// Items are laid out in rows (horizontal primary axis)
    #[default]
    Row,
    /// Items are laid out in rows in reverse order (horizontal primary axis, right to left)
    RowReverse,
    /// Items are laid out in columns (vertical primary axis)
    Column,
    /// Items are laid out in columns in reverse order (vertical primary axis, bottom to top)
    ColumnReverse,
}

/// Relationship between a queried orientation and a FlexboxLayout's direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlexboxAxisRelation {
    /// The queried orientation is the main axis (e.g., Horizontal for a Row flex)
    MainAxis,
    /// The queried orientation is the cross axis (e.g., Vertical for a Row flex)
    CrossAxis,
    /// The flex direction is not known at compile time
    Unknown,
}

#[derive(Clone, Debug, derive_more::From)]
pub enum Layout {
    GridLayout(GridLayout),
    BoxLayout(BoxLayout),
    FlexboxLayout(FlexboxLayout),
}

impl Layout {
    /// Call the visitor for each NamedReference stored in the layout
    pub fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        match self {
            Layout::GridLayout(grid) => grid.visit_named_references(visitor),
            Layout::BoxLayout(l) => l.visit_named_references(visitor),
            Layout::FlexboxLayout(l) => l.visit_named_references(visitor),
        }
    }
}

/// An Item in the layout tree
#[derive(Debug, Default, Clone)]
pub struct LayoutItem {
    pub element: ElementRc,
    pub constraints: LayoutConstraints,
    /// The `cross-axis-self-alignment` property, if set.
    /// Used by box layouts and FlexboxLayout; always `None` in a GridLayout.
    pub cross_axis_self_alignment: Option<NamedReference>,
    /// The `layout-order` property, if set.
    /// Used by box layouts and FlexboxLayout; always `None` in a GridLayout.
    pub layout_order: Option<NamedReference>,
}

/// A child within a repeated Row in a GridLayout.
/// Can be either a static item or a nested repeater (`for y in model: ...`).
#[derive(Debug, Clone)]
pub enum RowChildTemplate {
    Static(LayoutItem),
    Repeated {
        item: LayoutItem,
        /// The repeated element (the `for y in ...` element inside the Row)
        repeated_element: ElementRc,
    },
}

impl RowChildTemplate {
    pub fn layout_item(&self) -> &LayoutItem {
        match self {
            RowChildTemplate::Static(item) => item,
            RowChildTemplate::Repeated { item, .. } => item,
        }
    }

    pub fn layout_item_mut(&mut self) -> &mut LayoutItem {
        match self {
            RowChildTemplate::Static(item) => item,
            RowChildTemplate::Repeated { item, .. } => item,
        }
    }

    pub fn repeated_element(&self) -> Option<&ElementRc> {
        match self {
            RowChildTemplate::Static(_) => None,
            RowChildTemplate::Repeated { repeated_element, .. } => Some(repeated_element),
        }
    }

    pub fn is_repeated(&self) -> bool {
        self.repeated_element().is_some()
    }
}

impl LayoutItem {
    pub fn rect(&self) -> LayoutRect {
        let p = |unresolved_name: &str| {
            let PropertyLookupResult { resolved_name, property_type, .. } = self
                .element
                .borrow()
                .lookup_property(unresolved_name, PropertyLookupMode::ComponentLocal);
            if property_type == Type::LogicalLength {
                Some(NamedReference::new(&self.element, resolved_name.to_smolstr()))
            } else {
                None
            }
        };
        LayoutRect {
            x_reference: p("x"),
            y_reference: p("y"),
            width_reference: if !self.constraints.fixed_width { p("width") } else { None },
            height_reference: if !self.constraints.fixed_height { p("height") } else { None },
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct LayoutRect {
    pub width_reference: Option<NamedReference>,
    pub height_reference: Option<NamedReference>,
    pub x_reference: Option<NamedReference>,
    pub y_reference: Option<NamedReference>,
}

impl LayoutRect {
    pub fn install_on_element(element: &ElementRc) -> Self {
        let install_prop =
            |name: &'static str| Some(NamedReference::new(element, SmolStr::new_static(name)));

        Self {
            x_reference: install_prop("x"),
            y_reference: install_prop("y"),
            width_reference: install_prop("width"),
            height_reference: install_prop("height"),
        }
    }

    fn visit_named_references(&mut self, mut visitor: &mut dyn FnMut(&mut NamedReference)) {
        self.width_reference.as_mut().map(&mut visitor);
        self.height_reference.as_mut().map(&mut visitor);
        self.x_reference.as_mut().map(&mut visitor);
        self.y_reference.as_mut().map(&mut visitor);
    }

    pub fn size_reference(&self, orientation: Orientation) -> Option<&NamedReference> {
        match orientation {
            Orientation::Horizontal => self.width_reference.as_ref(),
            Orientation::Vertical => self.height_reference.as_ref(),
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct LayoutConstraints {
    pub min_width: Option<NamedReference>,
    pub max_width: Option<NamedReference>,
    pub min_height: Option<NamedReference>,
    pub max_height: Option<NamedReference>,
    pub preferred_width: Option<NamedReference>,
    pub preferred_height: Option<NamedReference>,
    pub horizontal_stretch: Option<NamedReference>,
    pub vertical_stretch: Option<NamedReference>,
    pub fixed_width: bool,
    pub fixed_height: bool,
    /// For each constraint, whether it is set directly on the element (an
    /// override) rather than inherited from a base component. Inherited layout
    /// constraints are already baked into an element's own `layoutinfo-*`, so a
    /// parent layout that measured the cell through its layout-info must not
    /// re-apply them (double-count / height-for-width loop); locally-set ones
    /// must be applied. See [`Self::to_apply`].
    pub local: LayoutConstraintLocality,
}

/// Which [`LayoutConstraints`] are set directly on the element (depth 0) rather
/// than inherited from a base component.
#[derive(Debug, Default, Clone)]
pub struct LayoutConstraintLocality {
    pub min_width: bool,
    pub max_width: bool,
    pub min_height: bool,
    pub max_height: bool,
    pub preferred_width: bool,
    pub preferred_height: bool,
    pub horizontal_stretch: bool,
    pub vertical_stretch: bool,
}

/// The [`LayoutConstraints`] fields along one orientation.
pub struct OrientationConstraints<'a> {
    pub min: &'a Option<NamedReference>,
    pub max: &'a Option<NamedReference>,
    pub preferred: &'a Option<NamedReference>,
    pub stretch: &'a Option<NamedReference>,
    /// The size is set by an explicit `width`/`height` binding.
    pub fixed: bool,
}

impl LayoutConstraints {
    /// Build the constraints for the given element.
    ///
    /// When `diag` is `Some`, a redundant size constraint (e.g. both `width` and `min-width`) is
    /// reported at the given level; pass `None` to compute the constraints without reporting (e.g.
    /// when another pass owns that diagnostic).
    pub fn new(
        element: &ElementRc,
        mut diag: Option<(&mut BuildDiagnostics, DiagnosticLevel)>,
    ) -> Self {
        let mut constraints = Self {
            min_width: binding_reference(element, "min-width"),
            max_width: binding_reference(element, "max-width"),
            min_height: binding_reference(element, "min-height"),
            max_height: binding_reference(element, "max-height"),
            preferred_width: binding_reference(element, "preferred-width"),
            preferred_height: binding_reference(element, "preferred-height"),
            horizontal_stretch: binding_reference(element, "horizontal-stretch"),
            vertical_stretch: binding_reference(element, "vertical-stretch"),
            fixed_width: false,
            fixed_height: false,
            local: LayoutConstraintLocality {
                // min/max-{width,height} may be derived from a local fixed
                // `width`/`height` binding (see below), which is just as local
                // an override as an explicit min/max constraint.
                min_width: is_local_binding(element, "min-width")
                    || is_local_binding(element, "width"),
                max_width: is_local_binding(element, "max-width")
                    || is_local_binding(element, "width"),
                min_height: is_local_binding(element, "min-height")
                    || is_local_binding(element, "height"),
                max_height: is_local_binding(element, "max-height")
                    || is_local_binding(element, "height"),
                preferred_width: is_local_binding(element, "preferred-width"),
                preferred_height: is_local_binding(element, "preferred-height"),
                horizontal_stretch: is_local_binding(element, "horizontal-stretch"),
                vertical_stretch: is_local_binding(element, "vertical-stretch"),
            },
        };
        let mut apply_size_constraint =
            |prop: &'static str,
             binding: &BindingExpression,
             enclosing1: &Weak<Component>,
             depth,
             op: &mut Option<NamedReference>| {
                if let Some(other_prop) = op {
                    find_binding(
                        &other_prop.element(),
                        other_prop.name(),
                        |old, enclosing2, d2| {
                            if let Some((diag, level)) = &mut diag
                                && Weak::ptr_eq(enclosing1, enclosing2)
                                && old.priority.saturating_add(d2)
                                    <= binding.priority.saturating_add(depth)
                            {
                                diag.push_diagnostic_with_span(
                                    format!(
                                        "Cannot specify both '{prop}' and '{}'",
                                        other_prop.name()
                                    ),
                                    binding.to_source_location(),
                                    *level,
                                );
                            }
                        },
                    );
                }
                *op = Some(NamedReference::new(element, SmolStr::new_static(prop)))
            };
        find_binding(element, "height", |s, enclosing, depth| {
            constraints.fixed_height = true;
            apply_size_constraint("height", s, enclosing, depth, &mut constraints.min_height);
            apply_size_constraint("height", s, enclosing, depth, &mut constraints.max_height);
        });
        find_binding(element, "width", |s, enclosing, depth| {
            constraints.fixed_width = true;
            if s.expression.ty() == Type::Percent {
                apply_size_constraint("width", s, enclosing, depth, &mut constraints.min_width);
            } else {
                apply_size_constraint("width", s, enclosing, depth, &mut constraints.min_width);
                apply_size_constraint("width", s, enclosing, depth, &mut constraints.max_width);
            }
        });

        constraints
    }

    pub fn has_explicit_restrictions(&self, orientation: Orientation) -> bool {
        match orientation {
            Orientation::Horizontal => {
                self.min_width.is_some()
                    || self.max_width.is_some()
                    || self.preferred_width.is_some()
                    || self.horizontal_stretch.is_some()
            }
            Orientation::Vertical => {
                self.min_height.is_some()
                    || self.max_height.is_some()
                    || self.preferred_height.is_some()
                    || self.vertical_stretch.is_some()
            }
        }
    }

    /// The constraints a parent layout should apply on top of a cell's measured
    /// layout-info for `orientation`. Native items (whose layout-info doesn't
    /// merge their constraints) keep everything. For elements whose `layoutinfo-*`
    /// already includes their intrinsic constraints, only locally-set overrides
    /// are kept — inherited constraints are already in the measured value, and
    /// re-reading them unconstrained can reintroduce a height-for-width loop.
    pub fn to_apply(&self, element: &ElementRc, orientation: Orientation) -> Self {
        if !element.borrow().layout_info_includes_own_constraints(orientation) {
            return self.clone();
        }
        let mut c = self.clone();
        match orientation {
            Orientation::Horizontal => {
                if !self.local.min_width {
                    c.min_width = None;
                }
                if !self.local.max_width {
                    c.max_width = None;
                }
                if !self.local.preferred_width {
                    c.preferred_width = None;
                }
                if !self.local.horizontal_stretch {
                    c.horizontal_stretch = None;
                }
            }
            Orientation::Vertical => {
                if !self.local.min_height {
                    c.min_height = None;
                }
                if !self.local.max_height {
                    c.max_height = None;
                }
                if !self.local.preferred_height {
                    c.preferred_height = None;
                }
                if !self.local.vertical_stretch {
                    c.vertical_stretch = None;
                }
            }
        }
        c
    }

    pub fn for_orientation(&self, orientation: Orientation) -> OrientationConstraints<'_> {
        match orientation {
            Orientation::Horizontal => OrientationConstraints {
                min: &self.min_width,
                max: &self.max_width,
                preferred: &self.preferred_width,
                stretch: &self.horizontal_stretch,
                fixed: self.fixed_width,
            },
            Orientation::Vertical => OrientationConstraints {
                min: &self.min_height,
                max: &self.max_height,
                preferred: &self.preferred_height,
                stretch: &self.vertical_stretch,
                fixed: self.fixed_height,
            },
        }
    }

    // Iterate over the constraint with a reference to a property, and the corresponding member in the i_slint_core::layout::LayoutInfo struct
    pub fn for_each_restrictions(
        &self,
        orientation: Orientation,
    ) -> impl Iterator<Item = (&NamedReference, &'static str)> {
        let c = self.for_orientation(orientation);
        std::iter::empty()
            .chain(c.min.as_ref().map(|x| {
                if Expression::PropertyReference(x.clone()).ty() != Type::Percent {
                    (x, "min")
                } else {
                    (x, "min_percent")
                }
            }))
            .chain(c.max.as_ref().map(|x| {
                if Expression::PropertyReference(x.clone()).ty() != Type::Percent {
                    (x, "max")
                } else {
                    (x, "max_percent")
                }
            }))
            .chain(c.preferred.as_ref().map(|x| (x, "preferred")))
            .chain(c.stretch.as_ref().map(|x| (x, "stretch")))
    }

    pub fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        if let Some(e) = self.max_width.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.min_width.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.max_height.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.min_height.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.preferred_width.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.preferred_height.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.horizontal_stretch.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.vertical_stretch.as_mut() {
            visitor(&mut *e);
        }
    }
}

#[derive(Debug, Clone)]
pub enum RowColExpr {
    Named(NamedReference),
    Literal(u16),
    Auto,
}

#[derive(Debug, Clone)]
pub struct GridLayoutCell {
    pub new_row: bool,
    pub col_expr: RowColExpr,
    pub row_expr: RowColExpr,
    pub colspan_expr: RowColExpr,
    pub rowspan_expr: RowColExpr,
    pub child_items: Option<Vec<RowChildTemplate>>, // for repeated rows
}

impl GridLayoutCell {
    pub fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        if let RowColExpr::Named(ref mut e) = self.col_expr {
            visitor(e);
        }
        if let RowColExpr::Named(ref mut e) = self.row_expr {
            visitor(e);
        }
        if let RowColExpr::Named(ref mut e) = self.colspan_expr {
            visitor(e);
        }
        if let RowColExpr::Named(ref mut e) = self.rowspan_expr {
            visitor(e);
        }
        if let Some(children) = &mut self.child_items {
            for child in children {
                child.layout_item_mut().constraints.visit_named_references(visitor);
            }
        }
    }
}

/// An element in a GridLayout
#[derive(Debug, Clone)]
pub struct GridLayoutElement {
    /// `Rc<RefCell<GridLayoutCell>>` because shared with the repeated component's element
    pub cell: Rc<RefCell<GridLayoutCell>>,
    pub item: LayoutItem,
}

impl GridLayoutElement {
    pub fn span(&self, orientation: Orientation) -> RowColExpr {
        let cell = self.cell.borrow();
        match orientation {
            Orientation::Horizontal => cell.colspan_expr.clone(),
            Orientation::Vertical => cell.rowspan_expr.clone(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Padding {
    pub left: Option<NamedReference>,
    pub right: Option<NamedReference>,
    pub top: Option<NamedReference>,
    pub bottom: Option<NamedReference>,
}

impl Padding {
    fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        if let Some(e) = self.left.as_mut() {
            visitor(&mut *e)
        }
        if let Some(e) = self.right.as_mut() {
            visitor(&mut *e)
        }
        if let Some(e) = self.top.as_mut() {
            visitor(&mut *e)
        }
        if let Some(e) = self.bottom.as_mut() {
            visitor(&mut *e)
        }
    }

    // Return reference to the begin and end padding for a given orientation
    pub fn begin_end(&self, o: Orientation) -> (Option<&NamedReference>, Option<&NamedReference>) {
        match o {
            Orientation::Horizontal => (self.left.as_ref(), self.right.as_ref()),
            Orientation::Vertical => (self.top.as_ref(), self.bottom.as_ref()),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Spacing {
    pub horizontal: Option<NamedReference>,
    pub vertical: Option<NamedReference>,
}

impl Spacing {
    fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        if let Some(e) = self.horizontal.as_mut() {
            visitor(&mut *e);
        }
        if let Some(e) = self.vertical.as_mut() {
            visitor(&mut *e);
        }
    }

    pub fn orientation(&self, o: Orientation) -> Option<&NamedReference> {
        match o {
            Orientation::Horizontal => self.horizontal.as_ref(),
            Orientation::Vertical => self.vertical.as_ref(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct LayoutGeometry {
    pub rect: LayoutRect,
    pub spacing: Spacing,
    pub alignment: Option<NamedReference>,
    pub padding: Padding,
}

impl LayoutGeometry {
    pub fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        self.rect.visit_named_references(visitor);
        if let Some(e) = self.alignment.as_mut() {
            visitor(&mut *e)
        }
        self.spacing.visit_named_references(visitor);
        self.padding.visit_named_references(visitor);
    }

    pub fn new(layout_element: &ElementRc) -> Self {
        let spacing = || binding_reference(layout_element, "spacing");
        init_fake_property(layout_element, "spacing-horizontal", spacing);
        init_fake_property(layout_element, "spacing-vertical", spacing);

        let alignment = binding_reference(layout_element, "alignment");

        let padding = || binding_reference(layout_element, "padding");
        init_fake_property(layout_element, "padding-left", padding);
        init_fake_property(layout_element, "padding-right", padding);
        init_fake_property(layout_element, "padding-top", padding);
        init_fake_property(layout_element, "padding-bottom", padding);

        let padding = Padding {
            left: binding_reference(layout_element, "padding-left").or_else(padding),
            right: binding_reference(layout_element, "padding-right").or_else(padding),
            top: binding_reference(layout_element, "padding-top").or_else(padding),
            bottom: binding_reference(layout_element, "padding-bottom").or_else(padding),
        };

        let spacing = Spacing {
            horizontal: binding_reference(layout_element, "spacing-horizontal").or_else(spacing),
            vertical: binding_reference(layout_element, "spacing-vertical").or_else(spacing),
        };

        let rect = LayoutRect::install_on_element(layout_element);

        Self { rect, spacing, padding, alignment }
    }
}

/// If this element or any of the parent has a binding to the property, call the functor with that binding, and the depth.
/// Return None if the binding does not exist in any of the sub component, or Some with the result of the functor otherwise
pub(crate) fn find_binding<R>(
    element: &ElementRc,
    name: &str,
    f: impl FnOnce(&BindingExpression, &Weak<Component>, i32) -> R,
) -> Option<R> {
    let mut element = element.clone();
    let mut depth = 0;
    loop {
        if let Some(b) = element.borrow().binding(name)
            && b.has_binding()
        {
            return Some(f(&b, &element.borrow().enclosing_component, depth));
        }
        let e = match &element.borrow().base_type {
            ElementType::Component(base) => base.root_element.clone(),
            _ => return None,
        };
        element = e;
        depth += 1;
    }
}

/// Return a named reference to a property if a binding is set on that property
pub fn binding_reference(element: &ElementRc, name: &'static str) -> Option<NamedReference> {
    find_binding(element, name, |_, _, _| NamedReference::new(element, SmolStr::new_static(name)))
}

/// Whether `name`'s binding is set directly on `element` (depth 0) rather than
/// inherited from a base component. Must be evaluated while the binding is still
/// present (i.e. when building [`LayoutConstraints`]); later passes may move it.
fn is_local_binding(element: &ElementRc, name: &str) -> bool {
    find_binding(element, name, |_, _, depth| depth == 0) == Some(true)
}

fn init_fake_property(
    grid_layout_element: &ElementRc,
    name: &str,
    lazy_default: impl Fn() -> Option<NamedReference>,
) {
    if grid_layout_element.borrow().property_declarations.contains_key(name)
        && grid_layout_element.borrow().binding(name).is_none()
        && let Some(e) = lazy_default()
    {
        if e.name() == name && Rc::ptr_eq(&e.element(), grid_layout_element) {
            // Don't reference self
            return;
        }
        grid_layout_element
            .borrow_mut()
            .set_binding(name.into(), Expression::PropertyReference(e).into());
    }
}

/// Internal representation of a grid layout
#[derive(Debug, Clone)]
pub struct GridLayout {
    /// All the elements which will be laid out within that element.
    pub elems: Vec<GridLayoutElement>,

    pub geometry: LayoutGeometry,

    /// When this GridLayout is actually the layout of a Dialog, then the cells start with all the buttons,
    /// and this variable contains their roles. The string is actually one of the values from the i_slint_core::layout::DialogButtonRole
    pub dialog_button_roles: Option<Vec<SmolStr>>,

    /// Whether any of the row/column expressions use 'auto'
    pub uses_auto: bool,
}

impl GridLayout {
    /// Clone each element's cell into a new Rc, breaking any Rc sharing with the original.
    pub fn clone_cells(&mut self) {
        for e in &mut self.elems {
            let cloned = Rc::new(RefCell::new(e.cell.borrow().clone()));
            e.cell = cloned;
        }
    }

    pub fn visit_rowcol_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        for elem in &mut self.elems {
            let mut cell = elem.cell.borrow_mut();
            if let RowColExpr::Named(ref mut e) = cell.col_expr {
                visitor(e);
            }
            if let RowColExpr::Named(ref mut e) = cell.row_expr {
                visitor(e);
            }
            if let RowColExpr::Named(ref mut e) = cell.colspan_expr {
                visitor(e);
            }
            if let RowColExpr::Named(ref mut e) = cell.rowspan_expr {
                visitor(e);
            }
        }
    }

    pub fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        self.visit_rowcol_named_references(visitor);
        for layout_elem in &mut self.elems {
            layout_elem.item.constraints.visit_named_references(visitor);
            if let Some(child_items) = &mut layout_elem.cell.borrow_mut().child_items {
                for child in child_items {
                    child.layout_item_mut().constraints.visit_named_references(visitor);
                }
            }
        }
        self.geometry.visit_named_references(visitor);
    }
}

/// Internal representation of a BoxLayout
#[derive(Debug, Clone)]
pub struct BoxLayout {
    /// Whether this is a HorizontalLayout or a VerticalLayout
    pub orientation: Orientation,
    pub elems: Vec<LayoutItem>,
    pub geometry: LayoutGeometry,
    /// The `cross-axis-alignment` property, if set.
    pub cross_alignment: Option<NamedReference>,
}

impl BoxLayout {
    pub fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        for cell in &mut self.elems {
            cell.constraints.visit_named_references(visitor);
            if let Some(e) = cell.cross_axis_self_alignment.as_mut() {
                visitor(&mut *e);
            }
            if let Some(e) = cell.layout_order.as_mut() {
                visitor(&mut *e);
            }
        }
        self.geometry.visit_named_references(visitor);
        if let Some(e) = self.cross_alignment.as_mut() {
            visitor(&mut *e);
        }
    }
}

/// Internal representation of a FlexboxLayout (row or column direction with wrapping)
#[derive(Debug, Clone)]
pub struct FlexboxLayout {
    pub elems: Vec<LayoutItem>,
    pub geometry: LayoutGeometry,
    pub direction: Option<NamedReference>,
    pub cross_axis_line_alignment: Option<NamedReference>,
    pub cross_axis_alignment: Option<NamedReference>,
    pub flex_wrap: Option<NamedReference>,
}

impl FlexboxLayout {
    /// If `elem` is a (lowered, inline) FlexboxLayout, return its layout
    /// description. The struct is embedded in the synthesized
    /// `layoutinfo-{h,v}` / `layout-cache` bindings on the element.
    pub fn from_element(elem: &ElementRc) -> Option<FlexboxLayout> {
        use crate::expression_tree::Expression;
        // The `layoutinfo-{h,v}` property's binding (on this element or its
        // base component's root) holds a `ComputeFlexboxLayoutInfo` with the
        // layout when the element is a FlexboxLayout.
        let nr = {
            let eb = elem.borrow();
            eb.effective_layout_info_prop(Orientation::Vertical)
                .or_else(|| eb.effective_layout_info_prop(Orientation::Horizontal))
                .cloned()
        }?;
        let target = nr.element();
        let target = target.borrow();
        let binding = target.binding(nr.name())?;
        match binding.value_expression() {
            Expression::ComputeFlexboxLayoutInfo { layout, .. } => Some(layout.clone()),
            _ => None,
        }
    }

    /// Try to determine the flex direction at compile time from a constant binding.
    /// Returns None if the direction is set at runtime.
    fn compile_time_direction(&self) -> Option<FlexboxLayoutDirection> {
        match self.direction.as_ref() {
            None => Some(FlexboxLayoutDirection::Row),
            Some(nr) => nr.element().borrow().binding(nr.name()).and_then(|binding| {
                if let crate::expression_tree::Expression::EnumerationValue(ev) =
                    binding.value_expression()
                {
                    match ev.enumeration.values[ev.value].as_str() {
                        "row" => Some(FlexboxLayoutDirection::Row),
                        "row-reverse" => Some(FlexboxLayoutDirection::RowReverse),
                        "column" => Some(FlexboxLayoutDirection::Column),
                        "column-reverse" => Some(FlexboxLayoutDirection::ColumnReverse),
                        _ => None,
                    }
                } else {
                    None
                }
            }),
        }
    }

    /// Determine the relationship between a queried orientation and this flex's direction.
    pub fn axis_relation(&self, orientation: Orientation) -> FlexboxAxisRelation {
        match self.compile_time_direction() {
            None => FlexboxAxisRelation::Unknown,
            Some(dir) => {
                let is_main = matches!(
                    (dir, orientation),
                    (
                        FlexboxLayoutDirection::Row | FlexboxLayoutDirection::RowReverse,
                        Orientation::Horizontal
                    ) | (
                        FlexboxLayoutDirection::Column | FlexboxLayoutDirection::ColumnReverse,
                        Orientation::Vertical
                    )
                );
                if is_main { FlexboxAxisRelation::MainAxis } else { FlexboxAxisRelation::CrossAxis }
            }
        }
    }

    pub fn visit_named_references(&mut self, visitor: &mut dyn FnMut(&mut NamedReference)) {
        for cell in &mut self.elems {
            cell.constraints.visit_named_references(visitor);
            if let Some(e) = cell.cross_axis_self_alignment.as_mut() {
                visitor(&mut *e)
            }
            if let Some(e) = cell.layout_order.as_mut() {
                visitor(&mut *e)
            }
        }
        self.geometry.visit_named_references(visitor);
        if let Some(e) = self.direction.as_mut() {
            visitor(&mut *e)
        }
        if let Some(e) = self.cross_axis_line_alignment.as_mut() {
            visitor(&mut *e)
        }
        if let Some(e) = self.cross_axis_alignment.as_mut() {
            visitor(&mut *e)
        }
        if let Some(e) = self.flex_wrap.as_mut() {
            visitor(&mut *e)
        }
    }
}

/// Whether the builtin — or the native class it resolves to after the
/// `resolve_native_classes` pass — has no intrinsic size (Rectangle, Empty,
/// TouchArea, etc.): its layout info is the static default, never
/// height-for-width.
fn has_no_intrinsic_size(base: &ElementType) -> bool {
    let name = match base {
        ElementType::Builtin(b) => b.name.as_str(),
        ElementType::Native(n) => n.class_name.as_str(),
        _ => return false,
    };
    matches!(
        name,
        "Rectangle"
            | "BasicBorderRectangle"
            | "BorderRectangle"
            | "Empty"
            | "TouchArea"
            | "FocusScope"
            | "Opacity"
            | "Layer"
            | "BoxShadow"
            | "Clip"
    )
}

/// Controls whether `implicit_layout_info_call` returns layout info for builtins
/// that don't have an intrinsic size (Rectangle, Empty, TouchArea, etc.).
#[derive(Clone, Copy, PartialEq)]
pub enum BuiltinFilter {
    /// Return layout info for all builtins (existing behavior).
    All,
    /// Skip builtins whose `default_size_binding` is not `ImplicitSize`.
    SkipNonImplicit,
}

/// Get the implicit layout info of a particular element.
/// When `constraint` is `Some`, it's passed as the `cross_axis_constraint`
/// parameter to `Item::layout_info` for height-for-width support.
pub fn implicit_layout_info_call(
    elem: &ElementRc,
    orientation: Orientation,
    filter: BuiltinFilter,
    constraint: Option<Expression>,
) -> Option<Expression> {
    let mut elem_it = elem.clone();
    // The instance decides, not the base it walks down to: a base cannot see a
    // height the instance sets. Loop-invariant, so read it once.
    let height_settled = elem.borrow().height_is_literal;
    loop {
        return match &elem_it.clone().borrow().base_type {
            ElementType::Component(base_comp) => {
                // Flexbox supplies a width constraint to break its
                // h/v cache cycle; call the base component's parametrized
                // layout-info function when present.
                let parametrized_nr =
                    constraint.as_ref().filter(|_| orientation == Orientation::Vertical).and_then(
                        |_| base_comp.root_element.borrow().layout_info_v_with_constraint.clone(),
                    );
                if let Some(nr) = parametrized_nr
                    && let Some(c) = &constraint
                {
                    debug_assert!(Rc::ptr_eq(&nr.element(), &base_comp.root_element));
                    return Some(Expression::FunctionCall {
                        function: crate::expression_tree::Callable::Function(NamedReference::new(
                            elem,
                            nr.name().clone(),
                        )),
                        arguments: vec![c.clone()],
                        source_location: None,
                    });
                }
                let base_prop = elem_it.borrow().base_layout_info_prop(orientation, height_settled);
                match base_prop {
                    Some(nr) => {
                        // We cannot take nr as is because it is relative to the elem's component. We therefore need to
                        // use `elem` as an element for the PropertyReference, not `root` within the base of elem
                        debug_assert!(Rc::ptr_eq(&nr.element(), &base_comp.root_element));
                        Some(Expression::PropertyReference(NamedReference::new(
                            elem,
                            nr.name().clone(),
                        )))
                    }
                    None => {
                        elem_it = base_comp.root_element.clone();
                        continue;
                    }
                }
            }
            base @ (ElementType::Builtin(_) | ElementType::Native(_))
                if has_no_intrinsic_size(base) =>
            {
                if filter == BuiltinFilter::SkipNonImplicit {
                    return None;
                }
                // hard-code the value for rectangle because many rectangle end up optimized away and we
                // don't want to depend on the element.
                Some(Expression::Struct {
                    ty: crate::typeregister::layout_info_type(),
                    values: [("min", 0.), ("max", f32::MAX), ("preferred", 0.)]
                        .iter()
                        .map(|(s, v)| {
                            (SmolStr::new_static(s), Expression::NumberLiteral(*v as _, Unit::Px))
                        })
                        .chain(
                            [("min_percent", 0.), ("max_percent", 100.), ("stretch", 1.)]
                                .iter()
                                .map(|(s, v)| {
                                    (
                                        SmolStr::new_static(s),
                                        Expression::NumberLiteral(*v, Unit::None),
                                    )
                                }),
                        )
                        .collect(),
                })
            }
            ElementType::Builtin(base_type)
                if filter == BuiltinFilter::SkipNonImplicit
                    && base_type.default_size_binding
                        != crate::langtype::DefaultSizeBinding::ImplicitSize =>
            {
                None
            }
            _ => Some(Expression::FunctionCall {
                function: BuiltinFunction::ImplicitLayoutInfo(orientation).into(),
                arguments: vec![
                    Expression::ElementReference(Rc::downgrade(elem)),
                    constraint.unwrap_or(Expression::NumberLiteral(-1., Unit::None)),
                ],
                source_location: None,
            }),
        };
    }
}

/// The stretch factor of elements based on text or image items, which never
/// stretch: their `layout_info` always reports stretch 0, and a layoutinfo
/// property synthesized later can only merge it with smaller values.
pub fn static_native_stretch(elem: &ElementRc) -> Option<Expression> {
    elem.borrow()
        .builtin_type()
        .filter(|b| matches!(b.name.as_str(), "Text" | "StyledText" | "TextInput" | "Image"))
        .map(|_| Expression::NumberLiteral(0., Unit::None))
}

/// Create a new property based on the name. (it might get a different name if that property exist)
pub fn create_new_prop(elem: &ElementRc, tentative_name: SmolStr, ty: Type) -> NamedReference {
    let mut e = elem.borrow_mut();
    let name = if e.lookup_property(&tentative_name, PropertyLookupMode::InternalName).is_valid() {
        e.unique_member_name(&tentative_name)
    } else {
        tentative_name
    };
    e.property_declarations.insert(name.clone(), ty.into());
    drop(e);
    NamedReference::new(elem, name)
}

/// Return true if this type is a layout that has constraints
pub fn is_layout(base_type: &ElementType) -> bool {
    match base_type {
        ElementType::Component(c) => is_layout(&c.root_element.borrow().base_type),
        ElementType::Builtin(be) => {
            matches!(
                be.name.as_str(),
                "GridLayout" | "HorizontalLayout" | "VerticalLayout" | "FlexboxLayout"
            )
        }
        _ => false,
    }
}