fltk_float/grid/builder/
cell.rs

1use std::borrow::Borrow;
2use std::rc::Rc;
3
4use fltk::prelude::GroupExt;
5
6use crate::grid::{Cell, CellAlign, CellProperties, Padding, StripeCell};
7use crate::{IntoWidget, LayoutElement, WrapperFactory};
8
9use super::GridBuilder;
10
11pub struct CellBuilder<'l, G: GroupExt + Clone, F: Borrow<WrapperFactory>> {
12    owner: &'l mut GridBuilder<G, F>,
13    props: CellProperties,
14}
15
16impl<'l, G: GroupExt + Clone, F: Borrow<WrapperFactory>> CellBuilder<'l, G, F> {
17    pub(super) fn new(
18        owner: &'l mut GridBuilder<G, F>,
19        row: usize,
20        col: usize,
21        row_span: usize,
22        col_span: usize,
23    ) -> Self {
24        let padding = owner.default_cell_padding;
25        let horz_align = owner.default_col_align[col];
26        let vert_align = owner.default_row_align[row];
27        Self {
28            owner,
29            props: CellProperties {
30                row,
31                col,
32                row_span,
33                col_span,
34                padding,
35                horz_align,
36                vert_align,
37            },
38        }
39    }
40
41    pub fn with_left_padding(mut self, padding: i32) -> Self {
42        self.props.padding.left = padding;
43        self
44    }
45
46    pub fn with_top_padding(mut self, padding: i32) -> Self {
47        self.props.padding.top = padding;
48        self
49    }
50
51    pub fn with_right_padding(mut self, padding: i32) -> Self {
52        self.props.padding.right = padding;
53        self
54    }
55
56    pub fn with_bottom_padding(mut self, padding: i32) -> Self {
57        self.props.padding.bottom = padding;
58        self
59    }
60
61    pub fn with_padding(mut self, left: i32, top: i32, right: i32, bottom: i32) -> Self {
62        self.props.padding = Padding {
63            left,
64            top,
65            right,
66            bottom,
67        };
68        self
69    }
70
71    pub fn with_horz_align(mut self, align: CellAlign) -> Self {
72        self.props.horz_align = align;
73        self
74    }
75
76    pub fn with_vert_align(mut self, align: CellAlign) -> Self {
77        self.props.vert_align = align;
78        self
79    }
80
81    pub fn skip(self) {
82        let top = self.props.row;
83        let bottom = top + self.props.row_span;
84        let left = self.props.col;
85        let right = left + self.props.col_span;
86        for row in top..bottom {
87            for col in left..right {
88                self.owner.props.rows[row].cells[col] = StripeCell::Skipped;
89                self.owner.props.cols[col].cells[row] = StripeCell::Skipped;
90            }
91        }
92    }
93
94    pub fn add<E: LayoutElement + 'static>(self, element: E) {
95        self.add_shared(Rc::new(element));
96    }
97
98    pub fn add_shared(self, element: Rc<dyn LayoutElement>) {
99        self.owner.add_cell(Cell {
100            element,
101            min_size: Default::default(),
102            props: self.props,
103        });
104    }
105
106    pub fn wrap<W: IntoWidget + 'static>(self, widget: W) -> W {
107        let element = self.owner.factory.borrow().wrap(widget.clone());
108        self.add_shared(element);
109        widget
110    }
111}