@@ -44,6 +44,70 @@ BoxLayout::BoxLayout(Orientation orientation)
44
44
" orientation" , [this ] { return m_orientation == Gfx::Orientation::Vertical ? " Vertical" : " Horizontal" ; }, nullptr );
45
45
}
46
46
47
+ Gfx::IntSize BoxLayout::preferred_size () const
48
+ {
49
+ Gfx::IntSize size;
50
+ size.set_primary_size_for_orientation (orientation (), preferred_primary_size ());
51
+ size.set_secondary_size_for_orientation (orientation (), preferred_secondary_size ());
52
+ return size;
53
+ }
54
+
55
+ int BoxLayout::preferred_primary_size () const
56
+ {
57
+ int size = 0 ;
58
+
59
+ for (auto & entry : m_entries) {
60
+ if (!entry.widget )
61
+ continue ;
62
+ int min_size = entry.widget ->min_size ().primary_size_for_orientation (orientation ());
63
+ int max_size = entry.widget ->max_size ().primary_size_for_orientation (orientation ());
64
+ int preferred_primary_size = -1 ;
65
+ if (entry.widget ->is_shrink_to_fit () && entry.widget ->layout ()) {
66
+ preferred_primary_size = entry.widget ->layout ()->preferred_size ().primary_size_for_orientation (orientation ());
67
+ }
68
+ int item_size = max (0 , preferred_primary_size);
69
+ item_size = max (min_size, item_size);
70
+ item_size = min (max_size, item_size);
71
+ size += item_size + spacing ();
72
+ }
73
+ if (size > 0 )
74
+ size -= spacing ();
75
+
76
+ if (orientation () == Gfx::Orientation::Horizontal)
77
+ size += margins ().left () + margins ().right ();
78
+ else
79
+ size += margins ().top () + margins ().bottom ();
80
+
81
+ if (!size)
82
+ return -1 ;
83
+ return size;
84
+ }
85
+
86
+ int BoxLayout::preferred_secondary_size () const
87
+ {
88
+ int size = 0 ;
89
+ for (auto & entry : m_entries) {
90
+ if (!entry.widget )
91
+ continue ;
92
+ int min_size = entry.widget ->min_size ().secondary_size_for_orientation (orientation ());
93
+ int preferred_secondary_size = -1 ;
94
+ if (entry.widget ->is_shrink_to_fit () && entry.widget ->layout ()) {
95
+ preferred_secondary_size = entry.widget ->layout ()->preferred_size ().secondary_size_for_orientation (orientation ());
96
+ size = max (size, preferred_secondary_size);
97
+ }
98
+ size = max (min_size, size);
99
+ }
100
+
101
+ if (orientation () == Gfx::Orientation::Horizontal)
102
+ size += margins ().top () + margins ().bottom ();
103
+ else
104
+ size += margins ().left () + margins ().right ();
105
+
106
+ if (!size)
107
+ return -1 ;
108
+ return size;
109
+ }
110
+
47
111
void BoxLayout::run (Widget& widget)
48
112
{
49
113
if (m_entries.is_empty ())
@@ -71,6 +135,12 @@ void BoxLayout::run(Widget& widget)
71
135
continue ;
72
136
auto min_size = entry.widget ->min_size ();
73
137
auto max_size = entry.widget ->max_size ();
138
+
139
+ if (entry.widget ->is_shrink_to_fit () && entry.widget ->layout ()) {
140
+ auto preferred_size = entry.widget ->layout ()->preferred_size ();
141
+ min_size = max_size = preferred_size;
142
+ }
143
+
74
144
items.append (Item { entry.widget .ptr (), min_size.primary_size_for_orientation (orientation ()), max_size.primary_size_for_orientation (orientation ()) });
75
145
}
76
146
0 commit comments