use crate::{Borderable, Color, Colorable, FontSize, Labelable, Scalar};
use crate::position::{self, Align};
use crate::text;
use crate::widget;
#[derive(Clone, WidgetCommon_)]
pub struct Toggle<'a> {
#[carbide(common_builder)]
common: widget::CommonBuilder,
value: bool,
maybe_label: Option<&'a str>,
style: Style,
pub enabled: bool,
}
#[derive(Copy, Clone, Debug, Default, PartialEq, WidgetStyle_)]
pub struct Style {
#[carbide(default = "theme.shape_color")]
pub color: Option<Color>,
#[carbide(default = "theme.border_width")]
pub border: Option<Scalar>,
#[carbide(default = "theme.border_color")]
pub border_color: Option<Color>,
#[carbide(default = "theme.label_color")]
pub label_color: Option<Color>,
#[carbide(default = "theme.font_size_medium")]
pub label_font_size: Option<FontSize>,
#[carbide(default = "theme.font_id")]
pub label_font_id: Option<Option<text::font::Id>>,
#[carbide(default = "position::Relative::Align(Align::Middle)")]
pub label_x: Option<position::Relative>,
#[carbide(default = "position::Relative::Align(Align::Middle)")]
pub label_y: Option<position::Relative>,
}
widget_ids! {
struct Ids {
rectangle,
label,
}
}
pub struct State {
ids: Ids,
}
#[derive(Clone, Debug)]
#[allow(missing_copy_implementations)]
pub struct TimesClicked {
state: bool,
count: u16,
}
impl Iterator for TimesClicked {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
if self.count > 0 {
self.count -= 1;
self.state = !self.state;
Some(self.state)
} else {
None
}
}
}
impl<'a> Toggle<'a> {
pub fn new(value: bool) -> Toggle<'a> {
Toggle {
common: widget::CommonBuilder::default(),
style: Style::default(),
maybe_label: None,
value: value,
enabled: true,
}
}
pub fn label_font_id(mut self, font_id: text::font::Id) -> Self {
self.style.label_font_id = Some(Some(font_id));
self
}
pub fn label_x(mut self, x: position::Relative) -> Self {
self.style.label_x = Some(x);
self
}
pub fn label_y(mut self, y: position::Relative) -> Self {
self.style.label_y = Some(y);
self
}
builder_methods!{
pub enabled { enabled = bool }
}
}
impl<'a> Colorable for Toggle<'a> {
builder_method!(color { style.color = Some(Color) });
}
impl<'a> Borderable for Toggle<'a> {
builder_methods!{
border { style.border = Some(Scalar) }
border_color { style.border_color = Some(Color) }
}
}
impl<'a> Labelable<'a> for Toggle<'a> {
builder_methods!{
label { maybe_label = Some(&'a str) }
label_color { style.label_color = Some(Color) }
label_font_size { style.label_font_size = Some(FontSize) }
}
}