salvation/widgets/
column.rs

1use anyhow::{Context, Result};
2use salvation_macros::impl_with;
3
4use crate::{
5    impl_widget_common,
6    layout::{Alignment, LayoutItemOptions},
7};
8
9use super::{Widget, WidgetCommon};
10
11pub struct Column {
12    // TODO: add layout options
13    common: WidgetCommon,
14}
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
17pub struct Options {
18    pub alignment: Option<Alignment>,
19    // TODO: alignment, priority, stretch, etc.
20}
21
22#[impl_with]
23impl Column {
24    #[allow(clippy::new_without_default)]
25    pub fn new() -> Self {
26        Self {
27            common: WidgetCommon::new(),
28        }
29    }
30
31    pub fn add_child(&mut self, widget: Box<dyn Widget>) {
32        let row = self.common.children.len();
33        self.common
34            .add_child(widget, LayoutItemOptions::from_pos_in_grid(0, row as i32));
35        self.common.update();
36    }
37
38    pub fn set_options(&mut self, index: usize, options: Options) -> Result<()> {
39        let mut all_options = self
40            .common
41            .children
42            .get(index)
43            .context("invalid child index")?
44            .options
45            .clone();
46        all_options.x.alignment = options.alignment;
47        self.common.set_child_options(index, all_options)
48    }
49
50    pub fn and_options(mut self, options: Options) -> Self {
51        let index = self.common.children.len();
52        self.set_options(index, options)
53            .expect("should not fail with correct index");
54        self
55    }
56}
57
58impl Widget for Column {
59    impl_widget_common!();
60}