Skip to content

Commit a808cfa

Browse files
thankyouverycoolawesomekling
authored andcommitted
LibGUI+Applications: Govern Splitter resizing by opportunistic growth
This patch replaces the concept of fixed resizees with opportunistic ones which use the new SpecialDimension::OpportunisticGrow UISize. This lets us simplify splitter resize code and take advantage of the layout system's automatic calculations for minimum size and expansion. Functionally the same as before, but fixes Splitter's unintended ability to grow window size.
1 parent 6f2a304 commit a808cfa

File tree

5 files changed

+29
-57
lines changed

5 files changed

+29
-57
lines changed

Userland/Applications/FileManager/FileManagerWindow.gml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@
4343

4444
@GUI::HorizontalSplitter {
4545
name: "splitter"
46-
first_resizee_minimum_size: 80
4746

4847
@GUI::TreeView {
4948
name: "tree_view"
50-
fixed_width: 175
49+
preferred_width: 175
5150
}
5251
}
5352

Userland/Applications/FontEditor/FontEditorWindow.gml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
}
6363

6464
@GUI::HorizontalSplitter {
65-
fixed_resizee: "Second"
65+
opportunistic_resizee: "First"
6666

6767
@GUI::Widget {
6868
layout: @GUI::VerticalBoxLayout {}
@@ -217,7 +217,7 @@
217217

218218
@GUI::Widget {
219219
name: "unicode_block_container"
220-
fixed_width: 175
220+
preferred_width: 175
221221
layout: @GUI::VerticalBoxLayout {}
222222

223223
@GUI::TextBox {

Userland/Applications/Help/HelpWindow.gml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
@GUI::TabWidget {
1919
name: "tab_widget"
20-
fixed_width: 200
20+
preferred_width: 200
2121
container_margins: [6]
2222

2323
@GUI::TreeView {

Userland/Libraries/LibGUI/Splitter.cpp

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ namespace GUI {
2020
Splitter::Splitter(Orientation orientation)
2121
: m_orientation(orientation)
2222
{
23-
REGISTER_INT_PROPERTY("first_resizee_minimum_size", first_resizee_minimum_size, set_first_resizee_minimum_size);
24-
REGISTER_INT_PROPERTY("second_resizee_minimum_size", second_resizee_minimum_size, set_second_resizee_minimum_size);
25-
REGISTER_ENUM_PROPERTY("fixed_resizee", fixed_resizee, set_fixed_resizee, FixedResizee,
26-
{ FixedResizee::First, "First" },
27-
{ FixedResizee::Second, "Second" });
23+
REGISTER_ENUM_PROPERTY("opportunistic_resizee", opportunisitic_resizee, set_opportunisitic_resizee, OpportunisticResizee,
24+
{ OpportunisticResizee::First, "First" },
25+
{ OpportunisticResizee::Second, "Second" });
2826

2927
set_background_role(ColorRole::Button);
3028
set_layout<BoxLayout>(orientation);
@@ -197,7 +195,6 @@ void Splitter::recompute_grabbables()
197195
set_hovered_grabbable(&m_grabbables[old_hovered_index.value()]);
198196
}
199197

200-
// FIXME: Respect the child widgets min and max sizes
201198
void Splitter::mousemove_event(MouseEvent& event)
202199
{
203200
auto* grabbable = grabbable_at(event.position());
@@ -206,50 +203,33 @@ void Splitter::mousemove_event(MouseEvent& event)
206203
override_cursor(grabbable != nullptr);
207204
return;
208205
}
209-
auto delta = event.position() - m_resize_origin;
210206
if (!m_first_resizee || !m_second_resizee) {
211-
// One or both of the resizees were deleted during an ongoing resize, screw this.
212207
m_resizing = false;
213208
return;
214209
}
215-
auto new_first_resizee_size = m_first_resizee_start_size;
216-
auto new_second_resizee_size = m_second_resizee_start_size;
217210

218-
new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + delta.primary_offset_for_orientation(m_orientation));
219-
new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - delta.primary_offset_for_orientation(m_orientation));
220-
221-
if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < m_first_resizee_minimum_size) {
222-
int correction = m_first_resizee_minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
223-
new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
224-
new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
225-
}
226-
if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < m_second_resizee_minimum_size) {
227-
int correction = m_second_resizee_minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
228-
new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
229-
new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
230-
}
211+
auto delta = (event.position() - m_resize_origin).primary_offset_for_orientation(m_orientation);
212+
auto new_first_resizee_size = clamp(m_first_resizee_start_size.primary_size_for_orientation(m_orientation) + delta, 0, m_first_resizee_max_size);
213+
auto new_second_resizee_size = clamp(m_second_resizee_start_size.primary_size_for_orientation(m_orientation) - delta, 0, m_second_resizee_max_size);
231214

232215
if (m_orientation == Orientation::Horizontal) {
233-
if (fixed_resizee() == FixedResizee::First) {
234-
m_first_resizee->set_fixed_width(new_first_resizee_size.width());
235-
m_second_resizee->set_min_width(SpecialDimension::Shrink);
236-
m_second_resizee->set_max_width(SpecialDimension::Grow);
216+
if (opportunisitic_resizee() == OpportunisticResizee::First) {
217+
m_first_resizee->set_preferred_width(SpecialDimension::OpportunisticGrow);
218+
m_second_resizee->set_preferred_width(new_second_resizee_size);
237219
} else {
238-
VERIFY(fixed_resizee() == FixedResizee::Second);
239-
m_second_resizee->set_fixed_width(new_second_resizee_size.width());
240-
m_first_resizee->set_min_width(SpecialDimension::Shrink);
241-
m_first_resizee->set_max_width(SpecialDimension::Grow);
220+
VERIFY(opportunisitic_resizee() == OpportunisticResizee::Second);
221+
m_second_resizee->set_preferred_width(SpecialDimension::OpportunisticGrow);
222+
m_first_resizee->set_preferred_width(new_first_resizee_size);
242223
}
243224
} else {
244-
if (fixed_resizee() == FixedResizee::First) {
245-
m_first_resizee->set_fixed_height(new_first_resizee_size.height());
246-
m_second_resizee->set_min_height(SpecialDimension::Shrink);
247-
m_second_resizee->set_max_height(SpecialDimension::Grow);
225+
if (opportunisitic_resizee() == OpportunisticResizee::First) {
226+
m_first_resizee->set_preferred_height(SpecialDimension::OpportunisticGrow);
227+
m_second_resizee->set_preferred_height(new_second_resizee_size);
228+
248229
} else {
249-
VERIFY(fixed_resizee() == FixedResizee::Second);
250-
m_second_resizee->set_fixed_height(new_second_resizee_size.height());
251-
m_first_resizee->set_min_height(SpecialDimension::Shrink);
252-
m_first_resizee->set_max_height(SpecialDimension::Grow);
230+
VERIFY(opportunisitic_resizee() == OpportunisticResizee::Second);
231+
m_second_resizee->set_preferred_height(SpecialDimension::OpportunisticGrow);
232+
m_first_resizee->set_preferred_height(new_first_resizee_size);
253233
}
254234
}
255235

@@ -272,13 +252,13 @@ void Splitter::custom_layout()
272252
if (m_last_child_count > child_widgets.size()) {
273253
bool has_child_to_fill_space = false;
274254
for (auto& child : child_widgets) {
275-
if (child.max_size().primary_size_for_orientation(m_orientation).is_grow()) {
255+
if (child.preferred_size().primary_size_for_orientation(m_orientation).is_opportunistic_grow()) {
276256
has_child_to_fill_space = true;
277257
break;
278258
}
279259
}
280260
if (!has_child_to_fill_space)
281-
child_widgets.last().set_preferred_size(SpecialDimension::Grow);
261+
child_widgets.last().set_preferred_size(SpecialDimension::OpportunisticGrow);
282262
}
283263
}
284264

Userland/Libraries/LibGUI/Splitter.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@ class Splitter : public Widget {
1515
C_OBJECT(Splitter);
1616

1717
public:
18-
enum class FixedResizee {
18+
enum class OpportunisticResizee {
1919
First,
2020
Second
2121
};
2222

2323
virtual ~Splitter() override = default;
2424

25-
int first_resizee_minimum_size() { return m_first_resizee_minimum_size; }
26-
void set_first_resizee_minimum_size(int minimum_size) { m_first_resizee_minimum_size = minimum_size; }
27-
int second_resizee_minimum_size() { return m_second_resizee_minimum_size; }
28-
void set_second_resizee_minimum_size(int minimum_size) { m_second_resizee_minimum_size = minimum_size; }
29-
3025
protected:
3126
explicit Splitter(Gfx::Orientation);
3227

@@ -40,8 +35,8 @@ class Splitter : public Widget {
4035
virtual void did_layout() override;
4136
virtual void custom_layout() override;
4237

43-
FixedResizee fixed_resizee() const { return m_fixed_resizee; }
44-
void set_fixed_resizee(FixedResizee resizee) { m_fixed_resizee = resizee; }
38+
OpportunisticResizee opportunisitic_resizee() const { return m_opportunistic_resizee; }
39+
void set_opportunisitic_resizee(OpportunisticResizee resizee) { m_opportunistic_resizee = resizee; }
4540

4641
private:
4742
void override_cursor(bool do_override);
@@ -55,9 +50,7 @@ class Splitter : public Widget {
5550
WeakPtr<Widget> m_second_resizee;
5651
Gfx::IntSize m_first_resizee_start_size;
5752
Gfx::IntSize m_second_resizee_start_size;
58-
int m_first_resizee_minimum_size { 0 };
59-
int m_second_resizee_minimum_size { 0 };
60-
FixedResizee m_fixed_resizee { FixedResizee::First };
53+
OpportunisticResizee m_opportunistic_resizee { OpportunisticResizee::Second };
6154
size_t m_last_child_count { 0 };
6255
int m_first_resizee_max_size { 0 };
6356
int m_second_resizee_max_size { 0 };

0 commit comments

Comments
 (0)