Skip to main content

waft_winui/controls/
toggle_split_button.rs

1//! ToggleSplitButton.cpp: toggle before primary activation, with the shared SplitButton template.
2
3use super::split_button::SplitButtonPresenter;
4use crate::{Flyout, SplitButton};
5use std::{fmt, rc::Rc};
6use waft_foundation::{App, Listener, Ref};
7use waft_widgets::*;
8
9/// Reports the requested checked value before the primary Click callback.
10pub type ToggleSplitButtonCheckedHandler = Rc<dyn Fn(&mut App, bool)>;
11
12/// A toggle action with a separate flyout button that does not change the checked value.
13#[derive(Clone)]
14pub struct ToggleSplitButton {
15    /// Current application-owned checked value.
16    pub is_checked: bool,
17
18    /// Applies the checked value requested by primary activation.
19    pub is_checked_changed: ToggleSplitButtonCheckedHandler,
20
21    /// Content displayed by the primary button.
22    pub content: WidgetRef,
23
24    /// Optional primary Click event, raised after the checked-value callback.
25    pub click: Option<Listener>,
26
27    /// Flyout opened by the secondary button, F4 or Alt+Down.
28    pub flyout: Option<Ref<Flyout>>,
29
30    /// Enables both actions and keyboard focus.
31    pub is_enabled: bool,
32
33    /// Includes this control in sequential keyboard traversal.
34    pub is_tab_stop: bool,
35
36    /// Optional caller-owned focus identity for the whole control.
37    pub focus_node: Option<FocusNodeRef>,
38
39    /// Identity across parent rebuilds.
40    pub key: Option<KeyRef>,
41}
42
43impl fmt::Debug for ToggleSplitButton {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.debug_struct("ToggleSplitButton")
46            .field("is_checked", &self.is_checked)
47            .field("is_enabled", &self.is_enabled)
48            .field("flyout", &self.flyout)
49            .finish_non_exhaustive()
50    }
51}
52
53impl ToggleSplitButton {
54    /// Creates a toggle split button using the kit's owner-managed checked-value convention.
55    #[cfg_attr(debug_assertions, track_caller)]
56    pub fn new<K>(
57        content: impl IntoWidget<K>,
58        is_checked: bool,
59        is_checked_changed: impl Fn(&mut App, bool) + 'static,
60    ) -> Self {
61        Self {
62            content: content.into_widget(),
63            is_checked,
64            is_checked_changed: Rc::new(is_checked_changed),
65            click: None,
66            flyout: None,
67            is_enabled: true,
68            is_tab_stop: true,
69            focus_node: None,
70            key: None,
71        }
72    }
73
74    /// Creates a toggle split button with a text label.
75    pub fn text(
76        text: impl Into<String>,
77        is_checked: bool,
78        is_checked_changed: impl Fn(&mut App, bool) + 'static,
79    ) -> Self {
80        Self::new(Text::new(text), is_checked, is_checked_changed)
81    }
82
83    /// Sets the Click callback that follows the checked-value callback.
84    pub fn click(mut self, click: Listener) -> Self {
85        self.click = Some(click);
86        self
87    }
88
89    /// Sets the flyout without changing its ownership.
90    pub fn flyout(mut self, flyout: Ref<Flyout>) -> Self {
91        self.flyout = Some(flyout);
92        self
93    }
94
95    /// Enables or disables both actions.
96    pub fn is_enabled(mut self, value: bool) -> Self {
97        self.is_enabled = value;
98        self
99    }
100
101    /// Sets whether Tab visits the control.
102    pub fn is_tab_stop(mut self, value: bool) -> Self {
103        self.is_tab_stop = value;
104        self
105    }
106
107    /// Supplies a caller-owned focus node.
108    pub fn focus_node(mut self, value: FocusNodeRef) -> Self {
109        self.focus_node = Some(value);
110        self
111    }
112
113    /// Sets the widget's identity.
114    pub fn key(mut self, value: KeyRef) -> Self {
115        self.key = Some(value);
116        self
117    }
118}
119
120impl StatelessWidget for ToggleSplitButton {
121    fn key(&self) -> Option<&KeyRef> {
122        self.key.as_ref()
123    }
124
125    fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
126        let next = !self.is_checked;
127        let changed = self.is_checked_changed.clone();
128        let click = self.click.clone();
129        let button = SplitButton {
130            content: self.content.clone(),
131            click: Listener::new(move |app| {
132                changed(app, next);
133                if let Some(click) = &click {
134                    click.call(app);
135                }
136            }),
137            flyout: self.flyout.clone(),
138            is_enabled: self.is_enabled,
139            is_tab_stop: self.is_tab_stop,
140            focus_node: self.focus_node.clone(),
141            key: None,
142        };
143        SplitButtonPresenter {
144            button,
145            is_checked: self.is_checked,
146        }
147        .into_widget()
148    }
149}