@@ -46,6 +46,11 @@ impl ObjectImpl for PickerGroupBoxInner {
46
46
} ) ;
47
47
SIGNALS . as_ref ( )
48
48
}
49
+
50
+ fn constructed ( & self , widget : & Self :: Type ) {
51
+ self . parent_constructed ( widget) ;
52
+ widget. set_halign ( gtk:: Align :: Center ) ;
53
+ }
49
54
}
50
55
51
56
impl WidgetImpl for PickerGroupBoxInner {
@@ -76,39 +81,46 @@ impl WidgetImpl for PickerGroupBoxInner {
76
81
77
82
fn preferred_height_for_width ( & self , widget : & Self :: Type , width : i32 ) -> ( i32 , i32 ) {
78
83
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) ;
90
85
( height, height)
91
86
}
92
87
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
+
93
116
fn size_allocate ( & self , obj : & Self :: Type , allocation : & gtk:: Allocation ) {
94
117
self . parent_size_allocate ( obj, allocation) ;
95
118
96
119
let rows = obj. rows_for_width ( allocation. width ( ) ) ;
97
120
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
-
109
121
let mut y = 0 ;
110
122
for row in rows {
111
- let mut x = ( allocation . width ( ) - total_width ) / 2 ;
123
+ let mut x = 0 ;
112
124
for group in row {
113
125
let height = group. widget ( ) . preferred_height ( ) . 1 ;
114
126
let width = group. widget ( ) . preferred_width ( ) . 1 ;
@@ -291,3 +303,27 @@ impl PickerGroupBox {
291
303
rows
292
304
}
293
305
}
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