Skip to main content

pax_std/forms/
dialogs.rs

1#![allow(dead_code)]
2
3#[allow(unused)]
4use crate::*;
5use pax_engine::api::*;
6use pax_engine::*;
7
8/// A confirmation dialog, a modal dialog that asks the user to confirm an action with "Yes" or "No" buttons.
9#[pax]
10#[engine_import_path("pax_engine")]
11#[inlined(
12    if self.open {
13        <Group x=50% y=50% width=300px height=170px>
14            <Button width=80px height=30px anchor_y=100% anchor_x=100% y={100%-20px} x={100% -80px -20px -20px} label="Yes" @button_click=handle_yes/>
15            <Button width=80px height=30px anchor_y=100% anchor_x=100% y={100% -20px} x={100% -20px} label="No" @button_click=handle_no/>
16            <Text x=20px y=20px height=15px width=100px text=text/>
17            <Rectangle fill=rgb(20, 20, 20)
18            corner_radius=15.0/>
19        </Group>
20        <Rectangle fill=rgba(0, 0, 0, 70)/>
21    }
22)]
23#[custom(Default)]
24pub struct ConfirmationDialog {
25    /// Prompt text shown in the dialog.
26    pub text: Property<String>,
27    /// Whether the dialog is visible.
28    pub open: Property<bool>,
29    // Private signal toggled when the user confirms.
30    pub _signal: Property<bool>,
31}
32
33impl Default for ConfirmationDialog {
34    fn default() -> Self {
35        Self {
36            text: Property::new("Are you sure?".to_owned()),
37            open: Property::default(),
38            _signal: Property::default(),
39        }
40    }
41}
42
43impl ConfirmationDialog {
44    // Handles the affirmative button and emits the confirmation signal.
45    pub fn handle_yes(&mut self, _ctx: &NodeContext, _event: Event<ButtonClick>) {
46        self.open.set(false);
47        self._signal.set(true);
48    }
49
50    // Handles the negative button by closing the dialog.
51    pub fn handle_no(&mut self, _ctx: &NodeContext, _event: Event<ButtonClick>) {
52        self.open.set(false);
53    }
54}