Skip to main content

pax_std/layout/
table.rs

1#[allow(unused)]
2use crate::*;
3use pax_engine::api::*;
4use pax_engine::*;
5
6const PIXEL_ALIGN_FACTOR: f64 = 1.0;
7
8/// A simple grid container for positioning children by rows and columns.
9///
10/// `Table` establishes row/column counts in local store. Child components such
11/// as `Row`, `Col`, `Cell`, and `Span` read that context to size and position
12/// their own slotted content.
13#[pax]
14#[engine_import_path("pax_engine")]
15#[inlined(
16for i in 0..self._slots {
17    slot(i)
18}
19<Rectangle fill=GREEN/>
20@settings {
21    @mount: on_mount,
22}
23)]
24pub struct Table {
25    /// Number of table rows.
26    pub rows: Property<usize>,
27    /// Number of table columns.
28    pub columns: Property<usize>,
29    // Slot count mirrored from `NodeContext`.
30    pub _slots: Property<usize>,
31}
32
33// Local table geometry shared with row/column child components.
34pub struct TableContext {
35    rows: Property<usize>,
36    columns: Property<usize>,
37}
38
39impl Store for TableContext {}
40
41impl Table {
42    // Publishes row/column counts for child layout helpers.
43    pub fn on_mount(&mut self, ctx: &NodeContext) {
44        ctx.push_local_store(TableContext {
45            rows: self.rows.clone(),
46            columns: self.columns.clone(),
47        });
48        let projected_children = ctx.projected_children_count.clone();
49        let deps = [projected_children.untyped()];
50        self._slots
51            .replace_with(Property::computed(move || projected_children.get(), &deps));
52    }
53}
54
55/// Selects one row from a parent `Table`.
56#[pax]
57#[engine_import_path("pax_engine")]
58#[inlined(
59<Group anchor_y=0% y={self._y_pos} height={self._height} width=100%>
60    for i in 0..self._slots {
61        slot(i)
62    }
63</Group>
64@settings {
65    @mount: on_mount,
66}
67)]
68pub struct Row {
69    /// Zero-based row index.
70    pub y: Property<usize>,
71    // Computed y position.
72    pub _y_pos: Property<Size>,
73    // Computed row height.
74    pub _height: Property<Size>,
75    // Slot count mirrored from `NodeContext`.
76    pub _slots: Property<usize>,
77}
78
79impl Row {
80    // Sizes and positions this row from the parent table context.
81    pub fn on_mount(&mut self, ctx: &NodeContext) {
82        ctx.peek_local_store(|table_ctx: &mut TableContext| {
83            let rows = table_ctx.rows.clone();
84            let deps = [rows.untyped()];
85            self._height.replace_with(Property::computed(
86                move || Size::Percent((PIXEL_ALIGN_FACTOR * 100.0 / rows.get() as f64).into()),
87                &deps,
88            ));
89            let rows = table_ctx.rows.clone();
90            let y = self.y.clone();
91            let deps = [rows.untyped(), y.untyped()];
92            self._y_pos.replace_with(Property::computed(
93                move || {
94                    Size::Percent(
95                        (PIXEL_ALIGN_FACTOR * y.get() as f64 * 100.0 / rows.get() as f64).into(),
96                    )
97                },
98                &deps,
99            ));
100        })
101        .expect("rows can not exist outside a table");
102        let projected_children = ctx.projected_children_count.clone();
103        let deps = [projected_children.untyped()];
104        self._slots
105            .replace_with(Property::computed(move || projected_children.get(), &deps));
106    }
107}
108
109/// Selects one column from a parent `Table`.
110#[pax]
111#[engine_import_path("pax_engine")]
112#[inlined(
113<Group anchor_x=0% x={self._x_pos} width={self._width} height=100%>
114    for i in 0..self._slots {
115        slot(i)
116    }
117</Group>
118@settings {
119    @mount: on_mount,
120}
121)]
122pub struct Col {
123    /// Zero-based column index.
124    pub x: Property<usize>,
125    // Computed x position.
126    pub _x_pos: Property<Size>,
127    // Computed column width.
128    pub _width: Property<Size>,
129    // Slot count mirrored from `NodeContext`.
130    pub _slots: Property<usize>,
131}
132
133impl Col {
134    // Sizes and positions this column from the parent table context.
135    pub fn on_mount(&mut self, ctx: &NodeContext) {
136        ctx.peek_local_store(|table_ctx: &mut TableContext| {
137            let columns = table_ctx.columns.clone();
138            let deps = [columns.untyped()];
139            self._width.replace_with(Property::computed(
140                move || Size::Percent((PIXEL_ALIGN_FACTOR * 100.0 / columns.get() as f64).into()),
141                &deps,
142            ));
143            let columns = table_ctx.columns.clone();
144            let x = self.x.clone();
145            let deps = [columns.untyped(), x.untyped()];
146            self._x_pos.replace_with(Property::computed(
147                move || {
148                    Size::Percent(
149                        (PIXEL_ALIGN_FACTOR * x.get() as f64 * 100.0 / columns.get() as f64).into(),
150                    )
151                },
152                &deps,
153            ));
154        })
155        .expect("columns can not exist outside a table");
156        let projected_children = ctx.projected_children_count.clone();
157        let deps = [projected_children.untyped()];
158        self._slots
159            .replace_with(Property::computed(move || projected_children.get(), &deps));
160    }
161}
162
163/// Selects a rectangular region from a parent `Table`.
164#[pax]
165#[engine_import_path("pax_engine")]
166#[inlined(
167<Group
168    anchor_x=0%
169    x={self._x_pos}
170    width={self._width}
171    anchor_y=0%
172    y={self._y_pos}
173    height={self._height}>
174
175    for i in 0..self._slots {
176        slot(i)
177    }
178</Group>
179@settings {
180    @mount: on_mount,
181}
182)]
183pub struct Span {
184    /// Zero-based column index.
185    pub x: Property<usize>,
186    /// Zero-based row index.
187    pub y: Property<usize>,
188    /// Number of columns to span.
189    pub w: Property<usize>,
190    /// Number of rows to span.
191    pub h: Property<usize>,
192
193    // Computed x position.
194    pub _x_pos: Property<Size>,
195    // Computed y position.
196    pub _y_pos: Property<Size>,
197    // Computed span width.
198    pub _width: Property<Size>,
199    // Computed span height.
200    pub _height: Property<Size>,
201    // Slot count mirrored from `NodeContext`.
202    pub _slots: Property<usize>,
203}
204
205impl Span {
206    // Sizes and positions this span from the parent table context.
207    pub fn on_mount(&mut self, ctx: &NodeContext) {
208        ctx.peek_local_store(|table_ctx: &mut TableContext| {
209            let rows = table_ctx.rows.clone();
210            let h = self.h.clone();
211            let deps = [rows.untyped(), h.untyped()];
212            self._height.replace_with(Property::computed(
213                move || {
214                    Size::Percent(
215                        (PIXEL_ALIGN_FACTOR * h.get() as f64 * 100.0 / rows.get() as f64).into(),
216                    )
217                },
218                &deps,
219            ));
220            let rows = table_ctx.rows.clone();
221            let y = self.y.clone();
222            let deps = [rows.untyped(), y.untyped()];
223            self._y_pos.replace_with(Property::computed(
224                move || {
225                    Size::Percent(
226                        (PIXEL_ALIGN_FACTOR * y.get() as f64 * 100.0 / rows.get() as f64).into(),
227                    )
228                },
229                &deps,
230            ));
231            let columns = table_ctx.columns.clone();
232            let w = self.w.clone();
233            let deps = [columns.untyped(), w.untyped()];
234            self._width.replace_with(Property::computed(
235                move || {
236                    Size::Percent(
237                        (PIXEL_ALIGN_FACTOR * w.get() as f64 * 100.0 / columns.get() as f64).into(),
238                    )
239                },
240                &deps,
241            ));
242            let columns = table_ctx.columns.clone();
243            let x = self.x.clone();
244            let deps = [columns.untyped(), x.untyped()];
245            self._x_pos.replace_with(Property::computed(
246                move || {
247                    Size::Percent(
248                        (PIXEL_ALIGN_FACTOR * x.get() as f64 * 100.0 / columns.get() as f64).into(),
249                    )
250                },
251                &deps,
252            ));
253        })
254        .expect("columns can not exist outside a table");
255        let projected_children = ctx.projected_children_count.clone();
256        let deps = [projected_children.untyped()];
257        self._slots
258            .replace_with(Property::computed(move || projected_children.get(), &deps));
259    }
260}
261
262/// Selects one cell from a parent `Table`.
263#[pax]
264#[engine_import_path("pax_engine")]
265#[inlined(
266<Group
267    anchor_x=0%
268    x={self._x_pos}
269    width={self._width}
270    anchor_y=0%
271    y={self._y_pos}
272    height={self._height}>
273
274    for i in 0..self._slots {
275        slot(i)
276    }
277</Group>
278@settings {
279    @mount: on_mount,
280}
281)]
282pub struct Cell {
283    /// Zero-based column index.
284    pub x: Property<usize>,
285    /// Zero-based row index.
286    pub y: Property<usize>,
287
288    // Computed x position.
289    pub _x_pos: Property<Size>,
290    // Computed y position.
291    pub _y_pos: Property<Size>,
292    // Computed cell width.
293    pub _width: Property<Size>,
294    // Computed cell height.
295    pub _height: Property<Size>,
296    // Slot count mirrored from `NodeContext`.
297    pub _slots: Property<usize>,
298}
299
300impl Cell {
301    // Sizes and positions this cell from the parent table context.
302    pub fn on_mount(&mut self, ctx: &NodeContext) {
303        ctx.peek_local_store(|table_ctx: &mut TableContext| {
304            let rows = table_ctx.rows.clone();
305            let deps = [rows.untyped()];
306            self._height.replace_with(Property::computed(
307                move || Size::Percent((PIXEL_ALIGN_FACTOR * 100.0 / rows.get() as f64).into()),
308                &deps,
309            ));
310            let rows = table_ctx.rows.clone();
311            let y = self.y.clone();
312            let deps = [rows.untyped(), y.untyped()];
313            self._y_pos.replace_with(Property::computed(
314                move || {
315                    Size::Percent(
316                        (PIXEL_ALIGN_FACTOR * y.get() as f64 * 100.0 / rows.get() as f64).into(),
317                    )
318                },
319                &deps,
320            ));
321            let columns = table_ctx.columns.clone();
322            let deps = [columns.untyped()];
323            self._width.replace_with(Property::computed(
324                move || Size::Percent((PIXEL_ALIGN_FACTOR * 100.0 / columns.get() as f64).into()),
325                &deps,
326            ));
327            let columns = table_ctx.columns.clone();
328            let x = self.x.clone();
329            let deps = [columns.untyped(), x.untyped()];
330            self._x_pos.replace_with(Property::computed(
331                move || {
332                    Size::Percent(
333                        (PIXEL_ALIGN_FACTOR * x.get() as f64 * 100.0 / columns.get() as f64).into(),
334                    )
335                },
336                &deps,
337            ));
338        })
339        .expect("columns can not exist outside a table");
340        let projected_children = ctx.projected_children_count.clone();
341        let deps = [projected_children.untyped()];
342        self._slots
343            .replace_with(Property::computed(move || projected_children.get(), &deps));
344    }
345}