Skip to content

Commit f01b9de

Browse files
authored
core: Fix constraints for empty layout
- Respect the padding even for empty layouts - When there is a non-default alignment, layouts shouldn't have a max size: Fix the case for empty layout - Remove max constraint in the orthogonal direction of an empty layout
1 parent 6b9b9d9 commit f01b9de

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

internal/core/layout.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -624,10 +624,16 @@ pub fn box_layout_info(
624624
alignment: LayoutAlignment,
625625
) -> LayoutInfo {
626626
let count = cells.len();
627+
let is_stretch = alignment == LayoutAlignment::Stretch;
627628
if count < 1 {
628-
return LayoutInfo { max: 0 as _, ..LayoutInfo::default() };
629+
let mut info = LayoutInfo::default();
630+
info.min = padding.begin + padding.end;
631+
info.preferred = info.min;
632+
if is_stretch {
633+
info.max = info.min;
634+
}
635+
return info;
629636
};
630-
let is_stretch = alignment == LayoutAlignment::Stretch;
631637
let extra_w = padding.begin + padding.end + spacing * (count - 1) as Coord;
632638
let min = cells.iter().map(|c| c.constraint.min).sum::<Coord>() + extra_w;
633639
let max = if is_stretch {
@@ -641,12 +647,7 @@ pub fn box_layout_info(
641647
}
642648

643649
pub fn box_layout_info_ortho(cells: Slice<BoxLayoutCellData>, padding: &Padding) -> LayoutInfo {
644-
let count = cells.len();
645-
if count < 1 {
646-
return LayoutInfo { max: 0 as _, ..LayoutInfo::default() };
647-
};
648650
let extra_w = padding.begin + padding.end;
649-
650651
let mut fold =
651652
cells.iter().fold(LayoutInfo { stretch: f32::MAX, ..Default::default() }, |a, b| {
652653
a.merge(&b.constraint)

tests/cases/crashes/layout_deleted_item.slint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export TestCase := Window {
1313
property<[int]> model: [1];
1414
VerticalLayout {
1515
under := TouchArea {
16-
HorizontalLayout {
16+
VerticalLayout {
1717
for value in model: TouchArea {
1818
horizontal-stretch: 5;
1919
vertical-stretch: 5;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright © SixtyFPS GmbH <[email protected]>
2+
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3+
4+
export component TestCase inherits Window {
5+
width: 300phx;
6+
height: 300phx;
7+
8+
empty1 := Rectangle { HorizontalLayout { } }
9+
empty2 := Rectangle { VerticalLayout { spacing: 80px; padding: 2px; } }
10+
empty3 := Rectangle { HorizontalLayout { alignment: center; } }
11+
empty4 := Rectangle { VerticalLayout { alignment: end; padding-left: 10phx; padding-top: 50phx; } }
12+
empty5 := Rectangle {
13+
VerticalLayout {
14+
alignment: end; padding-left: 10phx; padding-top: 50phx;
15+
for _ in 0 : Text { text: "Hello World!"; }
16+
}
17+
}
18+
empty6 := Rectangle {
19+
HorizontalLayout {
20+
if false: Rectangle { background: red; min-height: 10px; min-width: 10px; }
21+
padding-right: 80phx; padding-bottom: 70phx;
22+
}
23+
}
24+
25+
out property <bool> t1: empty1.min-height == 0 && empty1.min-width == 0 && empty1.preferred-height == 0 && empty1.preferred-width == 0 && empty1.max-height > 10000px && empty1.max-width == 0;
26+
out property <bool> t2: empty2.min-height == 4px && empty2.min-width == 4px && empty2.preferred-height == 4px && empty2.preferred-width == 4px && empty2.max-height == 4px && empty2.max-width > 100000px;
27+
out property <bool> t3: empty3.min-height == 0 && empty3.min-width == 0 && empty3.preferred-height == 0 && empty3.preferred-width == 0 && empty3.max-height > 10000px && empty3.max-width > 10000px;
28+
out property <bool> t4: empty4.min-height == 50phx && empty4.min-width == 10phx && empty4.preferred-height == 50phx && empty4.preferred-width == 10phx && empty4.max-height > 10000px && empty4.max-width > 10000px;
29+
out property <bool> t5: empty5.min-height == 50phx && empty5.min-width == 10phx && empty5.preferred-height == 50phx && empty5.preferred-width == 10phx && empty5.max-height > 10000px && empty5.max-width > 10000px;
30+
out property <bool> t6: empty6.min-height == 70phx && empty6.min-width == 80phx && empty6.preferred-height == 70phx && empty6.preferred-width == 80phx && empty6.max-height > 10000px && empty6.max-width == 80phx;
31+
out property <bool> test: t1 && t2 && t3 && t4 && t5 && t6;
32+
}
33+
34+
/*
35+
36+
```cpp
37+
auto handle = TestCase::create();
38+
const TestCase &instance = *handle;
39+
assert(instance.get_test());
40+
```
41+
42+
43+
```rust
44+
let instance = TestCase::new().unwrap();
45+
assert!(instance.get_test());
46+
```
47+
48+
```js
49+
var instance = new slint.TestCase();
50+
assert(instance.test);
51+
```
52+
53+
*/

0 commit comments

Comments
 (0)