pax-std 0.39.0

Standard library for Pax, including layouts, drawing primitives, and form controls
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#![allow(unused_imports)]
use crate::*;
use pax_engine::api::{Axis, Event, KeyDown, Numeric, Property, Size};
use pax_engine::*;
use pax_runtime::api::NodeContext;

const DOT_DIAMETER: f64 = 10.0;
const DOT_GAP: f64 = 10.0;
const DOT_PILL_PADDING_X: f64 = 14.0;
const DOT_PILL_THICKNESS: f64 = 26.0;

/// A paged scrolling container with native scroll snapping and optional page dots.
///
/// Each slotted child becomes one page. The carousel lays pages out along `axis`,
/// sizes each page with `page_size`, and binds its scroll position through an
/// internal `Scroller`.
#[pax]
#[engine_import_path("pax_engine")]
#[file("layout/carousel.pax")]
#[custom(Default)]
pub struct Carousel {
    /// Axis along which pages are laid out and snapped.
    pub axis: Property<CarouselAxis>,
    /// Size of each page along the scroll axis (defaults to 100%).
    pub page_size: Property<Size>,
    /// Whether to show page-position dots when there is more than one page.
    pub show_dots: Property<bool>,
    /// Horizontal scroll position, in pixels.
    pub scroll_pos_x: Property<f64>,
    /// Vertical scroll position, in pixels.
    pub scroll_pos_y: Property<f64>,

    // Computed page layouts for the inline template.
    pub _pages: Property<Vec<CarouselCell>>,
    // Computed page width in pixels.
    pub _page_width_px: Property<f64>,
    // Computed page height in pixels.
    pub _page_height_px: Property<f64>,
    // Computed scrollable content width.
    pub _scroll_width: Property<Size>,
    // Computed scrollable content height.
    pub _scroll_height: Property<Size>,
    // Computed horizontal scroll snap anchors.
    pub _snap_positions_x: Property<Vec<Size>>,
    // Computed vertical scroll snap anchors.
    pub _snap_positions_y: Property<Vec<Size>>,
    // Computed number of pages.
    pub _page_count: Property<usize>,
    // Computed active page index.
    pub _active_page: Property<usize>,
    // Computed visibility for dots after accounting for page count.
    pub _show_dots: Property<bool>,
    // Computed dot positions and active states.
    pub _dots: Property<Vec<CarouselDot>>,
    // Computed total dot row/column length.
    pub _dots_width: Property<f64>,
    // Computed dot-pill width.
    pub _dots_pill_width: Property<f64>,
    // Computed dot-pill height.
    pub _dots_pill_height: Property<f64>,
    // Computed dot-pill x position.
    pub _dots_pos_x: Property<Size>,
    // Computed dot-pill y position.
    pub _dots_pos_y: Property<Size>,
}

impl Default for Carousel {
    fn default() -> Self {
        Self {
            axis: Property::new(CarouselAxis::Horizontal),
            page_size: Property::new(Size::Percent(Numeric::F64(100.0))),
            show_dots: Property::new(false),
            scroll_pos_x: Property::new(0.0),
            scroll_pos_y: Property::new(0.0),
            _pages: Property::new(vec![]),
            _page_width_px: Property::new(0.0),
            _page_height_px: Property::new(0.0),
            _scroll_width: Property::new(Size::Pixels(Numeric::F64(0.0))),
            _scroll_height: Property::new(Size::Pixels(Numeric::F64(0.0))),
            _snap_positions_x: Property::new(vec![]),
            _snap_positions_y: Property::new(vec![]),
            _page_count: Property::new(0),
            _active_page: Property::new(0),
            _show_dots: Property::new(false),
            _dots: Property::new(vec![]),
            _dots_width: Property::new(0.0),
            _dots_pill_width: Property::new(0.0),
            _dots_pill_height: Property::new(0.0),
            _dots_pos_x: Property::new(Size::Percent(Numeric::F64(50.0))),
            _dots_pos_y: Property::new(Size::Percent(Numeric::F64(92.0))),
        }
    }
}

/// Direction for carousel paging and scroll snapping.
#[pax]
#[engine_import_path("pax_engine")]
pub enum CarouselAxis {
    /// Pages flow left-to-right.
    #[default]
    Horizontal,
    /// Pages flow top-to-bottom.
    Vertical,
}

// Internal page layout emitted into the inline template.
#[pax]
#[engine_import_path("pax_engine")]
pub struct CarouselCell {
    // Page x offset in pixels.
    pub x_px: f64,
    // Page y offset in pixels.
    pub y_px: f64,
}

// Internal page-dot layout emitted into the inline template.
#[pax]
#[engine_import_path("pax_engine")]
pub struct CarouselDot {
    // Dot x position in pixels.
    pub x_px: f64,
    // Dot y position in pixels.
    pub y_px: f64,
    // Whether this dot represents the active page.
    pub is_active: bool,
}

struct CarouselLayout {
    pages: Vec<CarouselCell>,
    page_width_px: f64,
    page_height_px: f64,
    scroll_width: Size,
    scroll_height: Size,
    snap_positions_x: Vec<Size>,
    snap_positions_y: Vec<Size>,
}

impl Carousel {
    // Wires derived layout properties used by the inline template.
    pub fn on_mount(&mut self, ctx: &NodeContext) {
        let axis = self.axis.clone();
        let page_size = self.page_size.clone();
        let bounds = ctx.bounds_self.clone();
        let projected_children_count = ctx.projected_children_count.clone();
        let show_dots = self.show_dots.clone();
        let scroll_pos_x = self.scroll_pos_x.clone();
        let scroll_pos_y = self.scroll_pos_y.clone();

        let deps = [
            axis.untyped(),
            page_size.untyped(),
            bounds.untyped(),
            projected_children_count.untyped(),
        ];

        let axis_pages = axis.clone();
        let page_size_pages = page_size.clone();
        let bounds_pages = bounds.clone();
        let projected_children_count_pages = projected_children_count.clone();
        self._pages.replace_with(Property::computed(
            move || {
                compute_layout(
                    axis_pages.get(),
                    page_size_pages.get(),
                    bounds_pages.get(),
                    projected_children_count_pages.get(),
                )
                .pages
            },
            &deps,
        ));

        let axis_width = axis.clone();
        let page_size_width = page_size.clone();
        let bounds_width = bounds.clone();
        let projected_children_count_width = projected_children_count.clone();
        self._page_width_px.replace_with(Property::computed(
            move || {
                compute_layout(
                    axis_width.get(),
                    page_size_width.get(),
                    bounds_width.get(),
                    projected_children_count_width.get(),
                )
                .page_width_px
            },
            &deps,
        ));

        let axis_height = axis.clone();
        let page_size_height = page_size.clone();
        let bounds_height = bounds.clone();
        let projected_children_count_height = projected_children_count.clone();
        self._page_height_px.replace_with(Property::computed(
            move || {
                compute_layout(
                    axis_height.get(),
                    page_size_height.get(),
                    bounds_height.get(),
                    projected_children_count_height.get(),
                )
                .page_height_px
            },
            &deps,
        ));

        let axis_scroll_width = axis.clone();
        let page_size_scroll_width = page_size.clone();
        let bounds_scroll_width = bounds.clone();
        let projected_children_count_scroll_width = projected_children_count.clone();
        self._scroll_width.replace_with(Property::computed(
            move || {
                compute_layout(
                    axis_scroll_width.get(),
                    page_size_scroll_width.get(),
                    bounds_scroll_width.get(),
                    projected_children_count_scroll_width.get(),
                )
                .scroll_width
            },
            &deps,
        ));

        let axis_scroll_height = axis.clone();
        let page_size_scroll_height = page_size.clone();
        let bounds_scroll_height = bounds.clone();
        let projected_children_count_scroll_height = projected_children_count.clone();
        self._scroll_height.replace_with(Property::computed(
            move || {
                compute_layout(
                    axis_scroll_height.get(),
                    page_size_scroll_height.get(),
                    bounds_scroll_height.get(),
                    projected_children_count_scroll_height.get(),
                )
                .scroll_height
            },
            &deps,
        ));

        let axis_snap_x = axis.clone();
        let page_size_snap_x = page_size.clone();
        let bounds_snap_x = bounds.clone();
        let projected_children_count_snap_x = projected_children_count.clone();
        self._snap_positions_x.replace_with(Property::computed(
            move || {
                compute_layout(
                    axis_snap_x.get(),
                    page_size_snap_x.get(),
                    bounds_snap_x.get(),
                    projected_children_count_snap_x.get(),
                )
                .snap_positions_x
            },
            &deps,
        ));

        let axis_snap_y = axis.clone();
        let page_size_snap_y = page_size.clone();
        let bounds_snap_y = bounds.clone();
        let projected_children_count_snap_y = projected_children_count.clone();
        self._snap_positions_y.replace_with(Property::computed(
            move || {
                compute_layout(
                    axis_snap_y.get(),
                    page_size_snap_y.get(),
                    bounds_snap_y.get(),
                    projected_children_count_snap_y.get(),
                )
                .snap_positions_y
            },
            &deps,
        ));

        let projected_children_count_count = projected_children_count.clone();
        self._page_count.replace_with(Property::computed(
            move || projected_children_count_count.get(),
            &[projected_children_count.untyped()],
        ));

        let axis_active = axis.clone();
        let page_size_active = page_size.clone();
        let bounds_active = bounds.clone();
        let projected_children_count_active = projected_children_count.clone();
        let scroll_pos_x_active = scroll_pos_x.clone();
        let scroll_pos_y_active = scroll_pos_y.clone();
        self._active_page.replace_with(Property::computed(
            move || {
                active_page_index(
                    axis_active.get(),
                    page_size_active.get(),
                    bounds_active.get(),
                    projected_children_count_active.get(),
                    scroll_pos_x_active.get(),
                    scroll_pos_y_active.get(),
                )
            },
            &[
                axis.untyped(),
                page_size.untyped(),
                bounds.untyped(),
                projected_children_count.untyped(),
                scroll_pos_x.untyped(),
                scroll_pos_y.untyped(),
            ],
        ));

        let show_dots_prop = show_dots.clone();
        let projected_children_count_dots = projected_children_count.clone();
        self._show_dots.replace_with(Property::computed(
            move || show_dots_prop.get() && projected_children_count_dots.get() > 1,
            &[show_dots.untyped(), projected_children_count.untyped()],
        ));

        let axis_dots = axis.clone();
        let show_dots_specs = self._show_dots.clone();
        let active_page_specs = self._active_page.clone();
        let projected_children_count_specs = projected_children_count.clone();
        self._dots.replace_with(Property::computed(
            move || {
                let count = projected_children_count_specs.get();
                if !show_dots_specs.get() || count <= 1 {
                    return vec![];
                }
                let active_index = active_page_specs.get();
                let spacing = DOT_DIAMETER + DOT_GAP;
                let total_width = (count as f64 - 1.0) * spacing + DOT_DIAMETER;
                let start_x = -total_width * 0.5 + DOT_DIAMETER * 0.5;
                let is_vertical = matches!(axis_dots.get(), CarouselAxis::Vertical);
                let mut dots = Vec::with_capacity(count);
                for i in 0..count {
                    let offset = start_x + i as f64 * spacing;
                    dots.push(CarouselDot {
                        x_px: if is_vertical { 0.0 } else { offset },
                        y_px: if is_vertical { offset } else { 0.0 },
                        is_active: i == active_index,
                    });
                }
                dots
            },
            &[
                self._show_dots.untyped(),
                self._active_page.untyped(),
                axis.untyped(),
                projected_children_count.untyped(),
            ],
        ));

        let show_dots_width = self._show_dots.clone();
        let projected_children_count_width = projected_children_count.clone();
        self._dots_width.replace_with(Property::computed(
            move || {
                let count = projected_children_count_width.get();
                if !show_dots_width.get() || count <= 1 {
                    return 0.0;
                }
                (count as f64 - 1.0) * (DOT_DIAMETER + DOT_GAP) + DOT_DIAMETER
            },
            &[
                self._show_dots.untyped(),
                projected_children_count.untyped(),
            ],
        ));

        let axis_pill_width = axis.clone();
        let dots_width_prop = self._dots_width.clone();
        self._dots_pill_width.replace_with(Property::computed(
            move || {
                if matches!(axis_pill_width.get(), CarouselAxis::Vertical) {
                    DOT_PILL_THICKNESS
                } else {
                    dots_width_prop.get() + DOT_PILL_PADDING_X * 2.0
                }
            },
            &[axis.untyped(), self._dots_width.untyped()],
        ));

        let axis_pill_height = axis.clone();
        let dots_height_prop = self._dots_width.clone();
        self._dots_pill_height.replace_with(Property::computed(
            move || {
                if matches!(axis_pill_height.get(), CarouselAxis::Vertical) {
                    dots_height_prop.get() + DOT_PILL_PADDING_X * 2.0
                } else {
                    DOT_PILL_THICKNESS
                }
            },
            &[axis.untyped(), self._dots_width.untyped()],
        ));

        let axis_pos = axis.clone();
        let axis_pos_dep = axis.clone();
        self._dots_pos_x.replace_with(Property::computed(
            move || {
                if matches!(axis_pos.get(), CarouselAxis::Vertical) {
                    Size::Percent(Numeric::F64(6.0))
                } else {
                    Size::Percent(Numeric::F64(50.0))
                }
            },
            &[axis_pos_dep.untyped()],
        ));

        let axis_pos_y = axis.clone();
        let axis_pos_y_dep = axis.clone();
        self._dots_pos_y.replace_with(Property::computed(
            move || {
                if matches!(axis_pos_y.get(), CarouselAxis::Vertical) {
                    Size::Percent(Numeric::F64(50.0))
                } else {
                    Size::Percent(Numeric::F64(92.0))
                }
            },
            &[axis_pos_y_dep.untyped()],
        ));
    }

    // Keyboard paging for arrow-key navigation.
    pub fn key_down(&mut self, ctx: &NodeContext, args: Event<KeyDown>) {
        let key = args.keyboard.key.as_str();
        let axis = self.axis.get();
        let delta = match axis {
            CarouselAxis::Horizontal => match key {
                "ArrowLeft" => -1,
                "ArrowRight" => 1,
                _ => 0,
            },
            CarouselAxis::Vertical => match key {
                "ArrowUp" => -1,
                "ArrowDown" => 1,
                _ => 0,
            },
        };
        if delta == 0 {
            return;
        }
        let page_count = ctx.projected_children_count.get();
        if page_count <= 1 {
            return;
        }
        let bounds = ctx.bounds_self.get();
        let page_extent_px = page_extent_px(axis.clone(), self.page_size.get(), bounds, page_count);
        if page_extent_px <= 0.0 {
            return;
        }
        let current_scroll = match axis {
            CarouselAxis::Horizontal => self.scroll_pos_x.get(),
            CarouselAxis::Vertical => self.scroll_pos_y.get(),
        };
        let current_index = (current_scroll / page_extent_px).round() as isize;
        let mut next_index = current_index + delta as isize;
        if next_index < 0 {
            next_index = 0;
        }
        let last_index = page_count.saturating_sub(1) as isize;
        if next_index > last_index {
            next_index = last_index;
        }
        let target_scroll = page_extent_px * next_index as f64;
        match axis {
            CarouselAxis::Horizontal => self.scroll_pos_x.set(target_scroll),
            CarouselAxis::Vertical => self.scroll_pos_y.set(target_scroll),
        }
    }
}

fn page_extent_px(
    axis: CarouselAxis,
    page_size: Size,
    bounds: (f64, f64),
    page_count: usize,
) -> f64 {
    let axis_extent = match axis {
        CarouselAxis::Horizontal => bounds.0,
        CarouselAxis::Vertical => bounds.1,
    };
    if page_count <= 1 {
        return axis_extent;
    }
    match axis {
        CarouselAxis::Horizontal => page_size.evaluate(bounds, Axis::X),
        CarouselAxis::Vertical => page_size.evaluate(bounds, Axis::Y),
    }
}

fn active_page_index(
    axis: CarouselAxis,
    page_size: Size,
    bounds: (f64, f64),
    page_count: usize,
    scroll_pos_x: f64,
    scroll_pos_y: f64,
) -> usize {
    if page_count <= 1 {
        return 0;
    }
    let page_extent = page_extent_px(axis.clone(), page_size, bounds, page_count);
    if page_extent <= 0.0 {
        return 0;
    }
    let scroll_pos = match axis {
        CarouselAxis::Horizontal => scroll_pos_x,
        CarouselAxis::Vertical => scroll_pos_y,
    };
    let mut idx = (scroll_pos / page_extent).round() as isize;
    if idx < 0 {
        idx = 0;
    }
    let max_idx = page_count.saturating_sub(1) as isize;
    if idx > max_idx {
        idx = max_idx;
    }
    idx as usize
}

fn compute_layout(
    axis: CarouselAxis,
    page_size: Size,
    bounds: (f64, f64),
    page_count: usize,
) -> CarouselLayout {
    let axis_extent_px = match axis {
        CarouselAxis::Horizontal => bounds.0,
        CarouselAxis::Vertical => bounds.1,
    };
    let cross_extent_px = match axis {
        CarouselAxis::Horizontal => bounds.1,
        CarouselAxis::Vertical => bounds.0,
    };
    let page_extent_px = if page_count <= 1 {
        axis_extent_px
    } else {
        match axis {
            CarouselAxis::Horizontal => page_size.evaluate(bounds, Axis::X),
            CarouselAxis::Vertical => page_size.evaluate(bounds, Axis::Y),
        }
    };
    let scroll_pages = page_count.max(1) as f64;
    let total_extent_px = page_extent_px * scroll_pages;

    let (scroll_width_px, scroll_height_px) = match axis {
        CarouselAxis::Horizontal => (total_extent_px, cross_extent_px),
        CarouselAxis::Vertical => (cross_extent_px, total_extent_px),
    };
    let (page_width_px, page_height_px) = match axis {
        CarouselAxis::Horizontal => (page_extent_px, cross_extent_px),
        CarouselAxis::Vertical => (cross_extent_px, page_extent_px),
    };

    let mut pages = Vec::with_capacity(page_count);
    let mut snap_positions_x = vec![];
    let mut snap_positions_y = vec![];

    if page_count > 1 {
        for i in 0..page_count {
            let offset_px = page_extent_px * (i as f64);
            match axis {
                CarouselAxis::Horizontal => {
                    pages.push(CarouselCell {
                        x_px: offset_px,
                        y_px: 0.0,
                    });
                    snap_positions_x.push(Size::Pixels(Numeric::F64(offset_px)));
                }
                CarouselAxis::Vertical => {
                    pages.push(CarouselCell {
                        x_px: 0.0,
                        y_px: offset_px,
                    });
                    snap_positions_y.push(Size::Pixels(Numeric::F64(offset_px)));
                }
            }
        }
    } else if page_count == 1 {
        pages.push(CarouselCell {
            x_px: 0.0,
            y_px: 0.0,
        });
    }

    CarouselLayout {
        pages,
        page_width_px,
        page_height_px,
        scroll_width: Size::Pixels(Numeric::F64(scroll_width_px)),
        scroll_height: Size::Pixels(Numeric::F64(scroll_height_px)),
        snap_positions_x,
        snap_positions_y,
    }
}