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
use crate::icons::ICON_CHECK;
use crate::prelude::*;
/// A checkbox used to display and toggle a boolean state.
///
/// Pressing the checkbox triggers the [`on_toggle`](Checkbox::on_toggle) callback.
///
/// # Examples
///
/// ## Basic checkbox
///
/// The checkbox must bound to some boolean data.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// #
/// Checkbox::new(cx, AppData::value);
/// ```
///
/// ## Checkbox with an action
///
/// A checkbox can be used to trigger a callback when toggled. Usually this is emitting an
/// event responsible for changing the data the checkbox is bound to.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # enum AppEvent {
/// # ToggleValue,
/// # }
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// #
/// Checkbox::new(cx, AppData::value).on_toggle(|cx| cx.emit(AppEvent::ToggleValue));
/// ```
///
/// ## Checkbox with a label
///
/// A checkbox is usually used with a label next to it describing what data the checkbox
/// is bound to or what the checkbox does when pressed. This can be done, for example, by
/// wrapping the checkbox in an [`HStack`](crate::prelude::HStack) and adding a [`Label`](crate::prelude::Label)
/// to it.
///
/// The Label can be used to trigger the checkbox by assigning the checkbox an id name and using it with the `describing` modifier on the label.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// #
/// HStack::new(cx, |cx| {
/// Checkbox::new(cx, AppData::value).id("check1");
/// Label::new(cx, "Press me").describing("check1");
/// });
/// ```
///
/// ## Custom checkbox
///
/// The `with_icons` constructor can be used to create a checkbox with custom icons for both checked and unchecked states.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # enum AppEvent {
/// # ToggleValue,
/// # }
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// # use vizia_core::icons::ICON_X;
///
/// Checkbox::with_icons(cx, AppData::value, None, Some(ICON_X))
/// .on_toggle(|cx| cx.emit(AppEvent::ToggleValue));
/// ```
pub struct Checkbox {
on_toggle: Option<Box<dyn Fn(&mut EventContext)>>,
}
impl Checkbox {
/// Creates a new checkbox.
///
/// # Examples
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// #
/// Checkbox::new(cx, AppData::value);
/// ```
pub fn new(cx: &mut Context, checked: impl Lens<Target = bool>) -> Handle<Self> {
Self { on_toggle: None }
.build(cx, |cx| {
Binding::new(cx, checked, |cx, checked| {
if checked.get(cx) {
Svg::new(cx, ICON_CHECK);
}
})
})
.checked(checked)
.role(Role::CheckBox)
.default_action_verb(DefaultActionVerb::Click)
.navigable(true)
}
/// Creates a new checkbox with custom icons for both checked and unchecked states.
///
/// # Examples
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # enum AppEvent {
/// # ToggleValue,
/// # }
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// # use vizia_core::icons::ICON_X;
///
/// Checkbox::with_icons(cx, AppData::value, None, Some(ICON_X))
/// .on_toggle(|cx| cx.emit(AppEvent::ToggleValue));
/// ```
pub fn with_icons<T>(
cx: &mut Context,
checked: impl Lens<Target = bool>,
icon_default: Option<impl Res<T> + 'static + Clone>,
icon_checked: Option<impl Res<T> + 'static + Clone>,
) -> Handle<Self>
where
T: AsRef<[u8]> + 'static,
{
Self { on_toggle: None }
.build(cx, |cx| {
Binding::new(cx, checked, move |cx, checked| {
let icon_default = icon_default.clone();
let icon_checked = icon_checked.clone();
if checked.get(cx) {
if let Some(icon) = icon_checked {
Svg::new(cx, icon);
}
} else if let Some(icon) = icon_default {
Svg::new(cx, icon);
}
})
})
.checked(checked)
.role(Role::CheckBox)
.default_action_verb(DefaultActionVerb::Click)
.navigable(true)
}
pub fn intermediate(
cx: &mut Context,
checked: impl Lens<Target = bool>,
intermediate: impl Lens<Target = bool>,
) -> Handle<Self> {
Self { on_toggle: None }
.build(cx, |_| {})
.bind(checked, move |handle, c| {
handle.bind(intermediate, move |handle, i| {
if c.get(&handle) {
handle.text(ICON_CHECK).toggle_class("intermediate", false);
} else if i.get(&handle) {
handle.text("-").toggle_class("intermediate", true);
} else {
handle.text("").toggle_class("intermediate", false);
}
});
})
.checked(checked)
.navigable(true)
}
}
impl Handle<'_, Checkbox> {
/// Set the callback triggered when the checkbox is pressed.
///
/// # Examples
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # enum AppEvent {
/// # ToggleValue,
/// # }
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// #
/// Checkbox::new(cx, AppData::value).on_toggle(|cx| cx.emit(AppEvent::ToggleValue));
/// ```
pub fn on_toggle<F>(self, callback: F) -> Self
where
F: 'static + Fn(&mut EventContext),
{
self.modify(|checkbox| checkbox.on_toggle = Some(Box::new(callback)))
}
}
impl View for Checkbox {
fn element(&self) -> Option<&'static str> {
Some("checkbox")
}
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, meta| match window_event {
WindowEvent::PressDown { mouse: _ } => {
if meta.target == cx.current {
cx.focus();
}
}
WindowEvent::Press { mouse: _ } => {
if meta.target == cx.current {
if let Some(callback) = &self.on_toggle {
(callback)(cx);
}
}
}
WindowEvent::ActionRequest(action) => match action.action {
Action::Default => {
if let Some(callback) = &self.on_toggle {
(callback)(cx);
}
}
_ => {}
},
_ => {}
});
}
}