Skip to main content

pax_std/forms/
toast.rs

1#![allow(dead_code)]
2
3use crate::*;
4#[allow(unused)]
5use pax_engine::api::LocalProperty;
6use pax_engine::api::*;
7use pax_engine::*;
8
9const IN_OUT_TIME: u64 = 10;
10
11/// A user interface "toast", a notification banner that appears for a short time and then disappears again.
12#[pax]
13#[engine_import_path("pax_engine")]
14#[inlined(
15    if self.shown {
16        <Group x=100% y={y_pos} width=400px height={height}>
17            <Group x=50% y=50% width={100%-20px} height={100%-20px}>
18                <Text x=40px height=100% width=70% text=message align={TextAlignHorizontal::Left}/>
19                <Button width=70px height=30px x=90% y=50% label="Undo" @button_click=handle_trigger/>
20                <Rectangle fill=rgb(20, 20, 20)
21                corner_radius=15.0/>
22            </Group>
23        </Group>
24    }
25
26    @settings {
27        @mount: on_mount
28        @pre_render: pre_render
29    }
30)]
31pub struct Toast {
32    /// Whether the toast is currently shown.
33    pub shown: Property<bool>,
34    /// The message to display in the toast.
35    pub message: Property<String>,
36    /// The height of the toast.
37    pub height: Property<Size>,
38    /// The y position of the toast.
39    pub y_pos: Property<Size>,
40
41    // Private signal toggled when the action button is triggered.
42    pub _signal: Property<bool>,
43    // Private listener that starts show/hide animation when `message` changes.
44    pub _on_message_changed: Property<bool>,
45}
46
47impl Toast {
48    // Initializes toast positioning and wires message-driven animation.
49    pub fn on_mount(&mut self, _ctx: &NodeContext) {
50        self.height.set(Size::Pixels(80.into()));
51        self.y_pos.set(Size::default() + self.height.get());
52        let message = self.message.local();
53        let deps = [message.untyped()];
54        let y_pos = self.y_pos.local();
55        let height = self.height.local();
56        self.shown.set(true);
57        self._on_message_changed
58            .replace_with(LocalProperty::computed(
59                move || {
60                    if message.get() != "" {
61                        // show for the animation period
62                        // in
63                        set_px_offset(&y_pos, Size::ZERO(), IN_OUT_TIME);
64                        // stay
65                        set_px_offset_later(&y_pos, Size::ZERO(), 300);
66                        // out
67                        set_px_offset_later(&y_pos, height.get(), IN_OUT_TIME);
68                    }
69                    false
70                },
71                &deps,
72            ));
73    }
74
75    // Forces the message-change listener to run.
76    pub fn pre_render(&mut self, _ctx: &NodeContext) {
77        self._on_message_changed.get();
78    }
79
80    // Hides the toast and emits its action signal.
81    pub fn handle_trigger(&mut self, _ctx: &NodeContext, _event: Event<ButtonClick>) {
82        set_px_offset(&self.y_pos, self.height.get(), IN_OUT_TIME);
83        self._signal.set(true);
84    }
85}
86
87fn set_px_offset(y_pos: &impl PropertyBinding<Size>, offset: Size, time: u64) {
88    y_pos
89        .local()
90        .ease_to(Size::default() + offset, time, EasingCurve::InQuad);
91}
92
93fn set_px_offset_later(y_pos: &impl PropertyBinding<Size>, offset: Size, time: u64) {
94    y_pos
95        .local()
96        .ease_to_later(Size::default() + offset, time, EasingCurve::OutQuad);
97}