vizia_core/views/radio.rs
1use crate::prelude::*;
2
3/// A radio button used to display and toggle boolean state.
4///
5/// Clicking on the radio button with the left mouse button triggers the `on_select` callback.
6///
7/// # Examples
8///
9/// ## Basic radio button
10///
11/// The radio button takes a boolean signal (for example `Signal<bool>`).
12///
13/// ```
14/// # use vizia_core::prelude::*;
15/// #
16/// #
17/// # struct AppData {
18/// # value: bool,
19/// # }
20/// #
21/// # impl Model for AppData {}
22/// #
23/// # let cx = &mut Context::default();
24/// #
25/// # let value = Signal::new(false);
26/// #
27/// RadioButton::new(cx, value);
28/// ```
29///
30/// ## Radio button with an action
31///
32/// A radio button can be used to trigger a callback when selected. Usually this updates
33/// the underlying boolean state.
34///
35/// ```
36/// # use vizia_core::prelude::*;
37/// #
38/// #
39/// # struct AppData {
40/// # value: bool,
41/// # }
42/// #
43/// # impl Model for AppData {}
44/// #
45/// # enum AppEvent {
46/// # ToggleValue,
47/// # }
48/// #
49/// # let cx = &mut Context::default();
50/// #
51/// # let value = Signal::new(false);
52/// #
53/// RadioButton::new(cx, value).on_select(|cx| cx.emit(AppEvent::ToggleValue));
54/// ```
55///
56/// ## Radio button with a label
57///
58/// A radio button is usually used with a label next to it describing what state the radio button
59/// controls or what the radio button does when pressed. This can be done, for example, by
60/// wrapping the radio button in an [`HStack`](crate::prelude::HStack) and adding a [`Label`](crate::prelude::Label)
61/// to it.
62///
63/// ```
64/// # use vizia_core::prelude::*;
65/// #
66/// #
67/// # struct AppData {
68/// # value: bool,
69/// # }
70/// #
71/// # impl Model for AppData {}
72/// #
73/// # let cx = &mut Context::default();
74/// #
75/// # let value = Signal::new(false);
76/// #
77/// HStack::new(cx, |cx| {
78/// RadioButton::new(cx, value);
79/// Label::new(cx, "Press me");
80/// });
81/// ```
82pub struct RadioButton {
83 on_select: Option<Box<dyn Fn(&mut EventContext)>>,
84}
85
86impl RadioButton {
87 /// Creates a new [RadioButton] view.
88 pub fn new(cx: &mut Context, checked: impl Res<bool>) -> Handle<Self> {
89 Self { on_select: None }
90 .build(cx, |cx| {
91 Element::new(cx).class("inner").hoverable(false);
92 })
93 .checked(checked)
94 .navigable(true)
95 .checkable(true)
96 .role(Role::RadioButton)
97 }
98}
99
100impl View for RadioButton {
101 fn element(&self) -> Option<&'static str> {
102 Some("radiobutton")
103 }
104
105 fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
106 event.map(|window_event, meta| match window_event {
107 WindowEvent::Press { mouse } => {
108 let over = if *mouse { cx.mouse.left.pressed } else { cx.focused() };
109 if over == cx.current()
110 && meta.target == cx.current()
111 && !cx.is_disabled()
112 && let Some(callback) = &self.on_select
113 {
114 (callback)(cx);
115 }
116 }
117
118 WindowEvent::ActionRequest(request) => match request.action {
119 Action::Click => {
120 if let Some(callback) = &self.on_select {
121 (callback)(cx);
122 }
123 }
124
125 _ => {}
126 },
127
128 _ => {}
129 });
130 }
131}
132
133impl Handle<'_, RadioButton> {
134 /// Set the callback triggered when the radio button is selected.
135 ///
136 /// # Examples
137 ///
138 /// ```
139 /// # use vizia_core::prelude::*;
140 /// #
141 /// #
142 /// # struct AppData {
143 /// # value: bool,
144 /// # }
145 /// #
146 /// # impl Model for AppData {}
147 /// #
148 /// # enum AppEvent {
149 /// # ToggleValue,
150 /// # }
151 /// #
152 /// # let cx = &mut Context::default();
153 /// #
154 /// # let value = Signal::new(false);
155 /// #
156 /// RadioButton::new(cx, value)
157 /// .on_select(|cx| cx.emit(AppEvent::ToggleValue));
158 /// ```
159 pub fn on_select<F>(self, callback: F) -> Self
160 where
161 F: 'static + Fn(&mut EventContext),
162 {
163 self.modify(|radiobutton| radiobutton.on_select = Some(Box::new(callback)))
164 }
165}