diff --git a/docs/modules/Custom.md b/docs/modules/Custom.md index ca9c430..561d191 100644 --- a/docs/modules/Custom.md +++ b/docs/modules/Custom.md @@ -47,6 +47,8 @@ A container to place nested widgets inside. |---------------|------------------------------------------------------------|----------------|-------------------------------------------------------------------| | `orientation` | `'horizontal'` or `'vertical'` (shorthand: `'h'` or `'v'`) | `'horizontal'` | Whether child widgets should be horizontally or vertically added. | | `widgets` | `(Module or Widget)[]` | `[]` | List of modules/widgets to add to this box. | +| `halign` | `'start'` or `'center'` or `'end'` or `'fill'` | `'fill'` | The horizontal alignment of the box within its parent container. | +| `valign` | `'start'` or `'center'` or `'end'` or `'fill'` | `'fill'` | The vertical alignment of the box within its parent container. | #### Label diff --git a/src/modules/custom/box.rs b/src/modules/custom/box.rs index 2c4275f..e683d7b 100644 --- a/src/modules/custom/box.rs +++ b/src/modules/custom/box.rs @@ -5,6 +5,31 @@ use crate::modules::custom::WidgetConfig; use gtk::prelude::*; use serde::Deserialize; +#[derive(Debug, Deserialize, Clone)] +#[serde(rename_all = "lowercase")] +#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] +pub enum ModuleAlignment { + /// Align widget to the start (left for horizontal, top for vertical). + Start, + /// Align widget to the center. + Center, + /// Align widget to the end (right for horizontal, bottom for vertical). + End, + /// Stretch widget to fill available space. + Fill, +} + +impl From for gtk::Align { + fn from(align: ModuleAlignment) -> Self { + match align { + ModuleAlignment::Start => gtk::Align::Start, + ModuleAlignment::Center => gtk::Align::Center, + ModuleAlignment::End => gtk::Align::End, + ModuleAlignment::Fill => gtk::Align::Fill, + } + } +} + #[derive(Debug, Deserialize, Clone)] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] pub struct BoxWidget { @@ -21,10 +46,21 @@ pub struct BoxWidget { /// Whether child widgets should be horizontally or vertically added. /// /// **Valid options**: `horizontal`, `vertical`, `h`, `v` - ///
/// **Default**: `horizontal` orientation: Option, + /// Horizontal alignment of the box relative to its parent. + /// + /// **Valid options**: `start`, `center`, `end`, `fill` + /// **Default**: `fill` + halign: Option, + + /// Vertical alignment of the box relative to its parent. + /// + /// **Valid options**: `start`, `center`, `end`, `fill` + /// **Default**: `fill` + valign: Option, + /// Modules and widgets to add to this box. /// /// **Default**: `null` @@ -47,6 +83,12 @@ impl CustomWidget for BoxWidget { } } + let horizontal_alignment = self.halign.unwrap_or(ModuleAlignment::Fill); + let vertical_alignment = self.valign.unwrap_or(ModuleAlignment::Fill); + + container.set_halign(horizontal_alignment.into()); + container.set_valign(vertical_alignment.into()); + container } }