Skip to main content

pax_std/layout/
table.rs

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