Skip to content

Commit d98e4e8

Browse files
committed
Update PickerGroupBox to only allocate space it uses
Now it is centered using `halign`. This can now be better positioned relative to other widgets.
1 parent c6b646f commit d98e4e8

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

src/picker/group_box/mod.rs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ impl ObjectImpl for PickerGroupBoxInner {
4646
});
4747
SIGNALS.as_ref()
4848
}
49+
50+
fn constructed(&self, widget: &Self::Type) {
51+
self.parent_constructed(widget);
52+
widget.set_halign(gtk::Align::Center);
53+
}
4954
}
5055

5156
impl WidgetImpl for PickerGroupBoxInner {
@@ -76,39 +81,46 @@ impl WidgetImpl for PickerGroupBoxInner {
7681

7782
fn preferred_height_for_width(&self, widget: &Self::Type, width: i32) -> (i32, i32) {
7883
let rows = widget.rows_for_width(width);
79-
let height = rows
80-
.iter()
81-
.map(|row| {
82-
row.iter()
83-
.map(|x| x.widget().preferred_height().1)
84-
.max()
85-
.unwrap_or(0)
86-
})
87-
.sum::<i32>()
88-
+ (rows.len() as i32 - 1) * VSPACING;
89-
84+
let height = total_height_for_rows(&rows);
9085
(height, height)
9186
}
9287

88+
fn adjust_size_allocation(
89+
&self,
90+
obj: &Self::Type,
91+
orientation: gtk::Orientation,
92+
minimum_size: &mut i32,
93+
natural_size: &mut i32,
94+
allocated_pos: &mut i32,
95+
allocated_size: &mut i32,
96+
) {
97+
// For centering to work, adjust natural width to be the portion of
98+
// allocated width that will actually be used after reflowing
99+
// children.
100+
if orientation == gtk::Orientation::Horizontal {
101+
let rows = obj.rows_for_width(*allocated_size);
102+
let total_width = max_width_for_rows(&rows);
103+
*natural_size = (*natural_size).min(total_width);
104+
}
105+
106+
self.parent_adjust_size_allocation(
107+
obj,
108+
orientation,
109+
minimum_size,
110+
natural_size,
111+
allocated_pos,
112+
allocated_size,
113+
);
114+
}
115+
93116
fn size_allocate(&self, obj: &Self::Type, allocation: &gtk::Allocation) {
94117
self.parent_size_allocate(obj, allocation);
95118

96119
let rows = obj.rows_for_width(allocation.width());
97120

98-
let total_width = rows
99-
.iter()
100-
.map(|row| {
101-
row.iter()
102-
.map(|x| x.widget().preferred_width().1)
103-
.sum::<i32>()
104-
+ (row.len() as i32 - 1) * HSPACING
105-
})
106-
.max()
107-
.unwrap_or(0);
108-
109121
let mut y = 0;
110122
for row in rows {
111-
let mut x = (allocation.width() - total_width) / 2;
123+
let mut x = 0;
112124
for group in row {
113125
let height = group.widget().preferred_height().1;
114126
let width = group.widget().preferred_width().1;
@@ -291,3 +303,27 @@ impl PickerGroupBox {
291303
rows
292304
}
293305
}
306+
307+
fn max_width_for_rows(rows: &[&[Box<dyn PickerGroup>]]) -> i32 {
308+
rows.iter()
309+
.map(|row| {
310+
row.iter()
311+
.map(|x| x.widget().preferred_width().1)
312+
.sum::<i32>()
313+
+ (row.len() as i32 - 1) * HSPACING
314+
})
315+
.max()
316+
.unwrap_or(0)
317+
}
318+
319+
fn total_height_for_rows(rows: &[&[Box<dyn PickerGroup>]]) -> i32 {
320+
rows.iter()
321+
.map(|row| {
322+
row.iter()
323+
.map(|x| x.widget().preferred_height().1)
324+
.max()
325+
.unwrap_or(0)
326+
})
327+
.sum::<i32>()
328+
+ (rows.len() as i32 - 1) * VSPACING
329+
}

0 commit comments

Comments
 (0)