2021-02-08 18:37:25 +11:00
|
|
|
use cacao::layout::{Layout, LayoutConstraint};
|
|
|
|
use cacao::switch::Switch;
|
2022-07-16 00:14:02 +10:00
|
|
|
use cacao::text::Label;
|
|
|
|
use cacao::view::View;
|
2021-02-08 18:37:25 +11:00
|
|
|
|
|
|
|
/// A reusable widget for a toggle; this is effectively a standard checkbox/label combination for
|
|
|
|
/// toggling a boolean value.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ToggleOptionView {
|
|
|
|
pub view: View,
|
|
|
|
pub switch: Switch,
|
|
|
|
pub title: Label,
|
2022-08-27 00:40:31 +10:00
|
|
|
pub subtitle: Label
|
2021-02-08 18:37:25 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ToggleOptionView {
|
|
|
|
/// Creates and returns a stock toggle view.
|
|
|
|
fn default() -> Self {
|
|
|
|
let view = View::new();
|
|
|
|
|
|
|
|
let switch = Switch::new("");
|
|
|
|
view.add_subview(&switch);
|
|
|
|
|
|
|
|
let title = Label::new();
|
|
|
|
view.add_subview(&title);
|
2022-07-11 01:15:29 +10:00
|
|
|
|
2021-02-08 18:37:25 +11:00
|
|
|
let subtitle = Label::new();
|
|
|
|
view.add_subview(&subtitle);
|
|
|
|
|
|
|
|
LayoutConstraint::activate(&[
|
|
|
|
switch.top.constraint_equal_to(&view.top),
|
|
|
|
switch.leading.constraint_equal_to(&view.leading),
|
|
|
|
switch.width.constraint_equal_to_constant(24.),
|
|
|
|
title.top.constraint_equal_to(&view.top),
|
|
|
|
title.leading.constraint_equal_to(&switch.trailing),
|
|
|
|
title.trailing.constraint_equal_to(&view.trailing),
|
|
|
|
subtitle.top.constraint_equal_to(&title.bottom),
|
|
|
|
subtitle.leading.constraint_equal_to(&switch.trailing),
|
|
|
|
subtitle.trailing.constraint_equal_to(&view.trailing),
|
2022-08-27 00:40:31 +10:00
|
|
|
subtitle.bottom.constraint_equal_to(&view.bottom)
|
2021-02-08 18:37:25 +11:00
|
|
|
]);
|
|
|
|
|
|
|
|
ToggleOptionView {
|
|
|
|
view,
|
|
|
|
switch,
|
|
|
|
title,
|
2022-08-27 00:40:31 +10:00
|
|
|
subtitle
|
2021-02-08 18:37:25 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToggleOptionView {
|
|
|
|
/// Configures the widget. The handler will be fired on each state change of the checkbox; you
|
|
|
|
/// can toggle your settings and such there.
|
|
|
|
pub fn configure<F>(&mut self, text: &str, subtitle: &str, state: bool, handler: F)
|
|
|
|
where
|
2022-08-27 00:40:31 +10:00
|
|
|
F: Fn() + Send + Sync + 'static
|
2021-02-08 18:37:25 +11:00
|
|
|
{
|
|
|
|
self.title.set_text(text);
|
|
|
|
self.subtitle.set_text(subtitle);
|
|
|
|
self.switch.set_action(handler);
|
|
|
|
self.switch.set_checked(state);
|
|
|
|
}
|
|
|
|
}
|