fltk_float/grid/builder/
stripe.rs

1use std::borrow::Borrow;
2
3use fltk::prelude::GroupExt;
4
5use crate::grid::{CellAlign, Stripe, StripeCell, StripeProperties};
6use crate::WrapperFactory;
7
8use super::{GridBuilder, StripeKind};
9
10pub struct StripeBuilder<'l, G: GroupExt + Clone, F: Borrow<WrapperFactory>> {
11    owner: &'l mut GridBuilder<G, F>,
12    kind: StripeKind,
13    props: StripeProperties,
14    group_idx: Option<usize>,
15    default_align: CellAlign,
16}
17
18impl<'l, G: GroupExt + Clone, F: Borrow<WrapperFactory>> StripeBuilder<'l, G, F> {
19    pub(super) fn new(
20        owner: &'l mut GridBuilder<G, F>,
21        kind: StripeKind,
22        group_idx: Option<usize>,
23    ) -> Self {
24        let default_align = match kind {
25            StripeKind::Row => CellAlign::Center,
26            StripeKind::Column => CellAlign::Stretch,
27        };
28        Self {
29            owner,
30            kind,
31            props: StripeProperties {
32                stretch: 0,
33                min_size: 0,
34            },
35            group_idx,
36            default_align,
37        }
38    }
39
40    pub fn with_stretch(mut self, stretch: u8) -> Self {
41        self.props.stretch = stretch;
42        self
43    }
44
45    pub fn with_min_size(mut self, min_size: i32) -> Self {
46        self.props.min_size = std::cmp::max(0, min_size);
47        self
48    }
49
50    pub fn with_default_align(mut self, align: CellAlign) -> Self {
51        self.default_align = align;
52        self
53    }
54
55    pub fn add(self) {
56        self.add_to_owner(1);
57    }
58
59    pub fn batch(self, count: usize) {
60        self.add_to_owner(count);
61    }
62
63    fn add_to_owner(self, count: usize) {
64        let (stripes, default_aligns, perpendicular) = match self.kind {
65            StripeKind::Row => (
66                &mut self.owner.props.rows,
67                &mut self.owner.default_row_align,
68                &mut self.owner.props.cols,
69            ),
70            StripeKind::Column => (
71                &mut self.owner.props.cols,
72                &mut self.owner.default_col_align,
73                &mut self.owner.props.rows,
74            ),
75        };
76        for _ in 0..count {
77            let group_idx = self.group_idx.unwrap_or_else(|| {
78                let idx = self.owner.props.groups.len();
79                self.owner.props.groups.push(self.props);
80                idx
81            });
82            stripes.push(Stripe {
83                cells: vec![StripeCell::Free; perpendicular.len()],
84                group_idx,
85            });
86            default_aligns.push(self.default_align);
87            for perp in perpendicular.iter_mut() {
88                perp.cells.push(StripeCell::Free);
89            }
90        }
91    }
92}