Skip to content

Commit b13c725

Browse files
authored
feat(custom): add halign and valign options to box widget (JakeStanger#988)
1 parent e4c0a1b commit b13c725

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

docs/modules/Custom.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ A container to place nested widgets inside.
4747
|---------------|------------------------------------------------------------|----------------|-------------------------------------------------------------------|
4848
| `orientation` | `'horizontal'` or `'vertical'` (shorthand: `'h'` or `'v'`) | `'horizontal'` | Whether child widgets should be horizontally or vertically added. |
4949
| `widgets` | `(Module or Widget)[]` | `[]` | List of modules/widgets to add to this box. |
50+
| `halign` | `'start'` or `'center'` or `'end'` or `'fill'` | `'fill'` | The horizontal alignment of the box within its parent container. |
51+
| `valign` | `'start'` or `'center'` or `'end'` or `'fill'` | `'fill'` | The vertical alignment of the box within its parent container. |
5052

5153
#### Label
5254

src/modules/custom/box.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ use crate::modules::custom::WidgetConfig;
55
use gtk::prelude::*;
66
use serde::Deserialize;
77

8+
#[derive(Debug, Deserialize, Clone)]
9+
#[serde(rename_all = "lowercase")]
10+
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
11+
pub enum ModuleAlignment {
12+
/// Align widget to the start (left for horizontal, top for vertical).
13+
Start,
14+
/// Align widget to the center.
15+
Center,
16+
/// Align widget to the end (right for horizontal, bottom for vertical).
17+
End,
18+
/// Stretch widget to fill available space.
19+
Fill,
20+
}
21+
22+
impl From<ModuleAlignment> for gtk::Align {
23+
fn from(align: ModuleAlignment) -> Self {
24+
match align {
25+
ModuleAlignment::Start => gtk::Align::Start,
26+
ModuleAlignment::Center => gtk::Align::Center,
27+
ModuleAlignment::End => gtk::Align::End,
28+
ModuleAlignment::Fill => gtk::Align::Fill,
29+
}
30+
}
31+
}
32+
833
#[derive(Debug, Deserialize, Clone)]
934
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
1035
pub struct BoxWidget {
@@ -21,10 +46,21 @@ pub struct BoxWidget {
2146
/// Whether child widgets should be horizontally or vertically added.
2247
///
2348
/// **Valid options**: `horizontal`, `vertical`, `h`, `v`
24-
/// <br />
2549
/// **Default**: `horizontal`
2650
orientation: Option<ModuleOrientation>,
2751

52+
/// Horizontal alignment of the box relative to its parent.
53+
///
54+
/// **Valid options**: `start`, `center`, `end`, `fill`
55+
/// **Default**: `fill`
56+
halign: Option<ModuleAlignment>,
57+
58+
/// Vertical alignment of the box relative to its parent.
59+
///
60+
/// **Valid options**: `start`, `center`, `end`, `fill`
61+
/// **Default**: `fill`
62+
valign: Option<ModuleAlignment>,
63+
2864
/// Modules and widgets to add to this box.
2965
///
3066
/// **Default**: `null`
@@ -47,6 +83,12 @@ impl CustomWidget for BoxWidget {
4783
}
4884
}
4985

86+
let horizontal_alignment = self.halign.unwrap_or(ModuleAlignment::Fill);
87+
let vertical_alignment = self.valign.unwrap_or(ModuleAlignment::Fill);
88+
89+
container.set_halign(horizontal_alignment.into());
90+
container.set_valign(vertical_alignment.into());
91+
5092
container
5193
}
5294
}

0 commit comments

Comments
 (0)