use std::sync::Arc;
use super::{
button::ButtonSize,
names,
part_style::Length,
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
shared::{fading, glyph, one_line, pressable},
theme::{Themed, current_theme},
};
use crate::{
app::theme::{ControlSize, Density, IconSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
core::Signal,
node::Element,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TogglePart {
Root,
Label,
Icon,
}
names!(TogglePart {
Root => "root",
Label => "label",
Icon => "icon",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ToggleState {
On,
Disabled,
IconOnly,
}
names!(ToggleState {
On => "on",
Disabled => "disabled",
IconOnly => "icon_only",
});
pub type ToggleSize = ButtonSize;
pub type ToggleRenderer = Arc<dyn Fn(ToggleView) -> Element + Send + Sync>;
pub struct Toggle {
label: Arc<str>,
size: Name,
icon: Option<Name>,
leading: Option<Element>,
icon_only: bool,
on: bool,
disabled: bool,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
on_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
focuses: Option<crate::core::ElementRef>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Toggle {
pub fn new(label: &str) -> Self {
Self {
label: Arc::from(label),
size: ButtonSize::default().into(),
icon: None,
leading: None,
icon_only: false,
on: false,
disabled: false,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
on_change: Vec::new(),
focuses: None,
id: None,
key: None,
}
}
pub fn focuses(mut self, target: &crate::core::ElementRef) -> Self {
self.focuses = Some(target.clone());
self
}
pub fn size(mut self, size: impl Into<Name>) -> Self {
self.size = size.into();
self
}
pub fn icon(mut self, name: impl Into<Name>) -> Self {
self.icon = Some(name.into());
self
}
pub fn leading(mut self, element: impl Into<Element>) -> Self {
self.leading = Some(element.into());
self
}
pub fn icon_only(mut self, icon_only: bool) -> Self {
self.icon_only = icon_only;
self
}
pub fn on(mut self, on: bool) -> Self {
self.on = on;
self
}
pub fn bind(self, on: Signal<bool>) -> Self {
let set = on.clone();
self.on(on.get()).on_change(move |next| set.set(next))
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
pub fn on_change(mut self, handler: impl Fn(bool) + Send + Sync + 'static) -> Self {
self.on_change.push(Arc::new(handler));
self
}
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
pub fn force(mut self, interaction: Option<Interaction>) -> Self {
self.forced = interaction;
self
}
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
pub(crate) fn command(&self) -> super::toolbar::ToolbarCommand {
let (on, handlers) = (self.on, self.on_change.clone());
super::toolbar::ToolbarCommand {
label: self.label.clone(),
icon: self.icon,
icon_only: self.icon_only,
disabled: self.disabled,
checked: Some(on),
run: Arc::new(move || {
for handler in &handlers {
handler(!on);
}
}),
id: self.id.clone(),
key: None,
}
}
pub fn build(mut self, theme: &Theme) -> Element {
self.icon_only = self.icon_only || self.label.is_empty() && self.icon.is_some();
let case = Case {
variant: None,
size: Some(self.size),
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(ToggleState::On, self.on)
.state(ToggleState::Disabled, self.disabled)
.state(ToggleState::IconOnly, self.icon_only);
let view = ToggleView {
label: self.label,
size: self.size,
icon: self.icon,
leading: self.leading,
icon_only: self.icon_only,
on: self.on,
disabled: self.disabled,
recipe: theme
.resolve::<Toggle>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
forced: self.forced,
on_change: self.on_change,
focuses: self.focuses,
id: self.id,
key: self.key,
};
match theme.renderer::<Toggle>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<Toggle> for Element {
fn from(toggle: Toggle) -> Self {
toggle.build(¤t_theme())
}
}
pub struct ToggleView {
pub label: Arc<str>,
pub size: Name,
pub icon: Option<Name>,
pub leading: Option<Element>,
pub icon_only: bool,
pub on: bool,
pub disabled: bool,
pub recipe: ResolvedRecipe,
forced: Option<Interaction>,
on_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
focuses: Option<crate::core::ElementRef>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl ToggleView {
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part).forced(self.forced)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
pub fn root(&self) -> crate::components::Button {
let mut root = pressable(&self.part(TogglePart::Root), self.disabled);
if !self.icon_only {
root = root
.flex_shrink(1.0)
.shrink_limit(crate::layout::layout_kind::ShrinkLimit::Content)
.max_width(crate::node::dimension::Dimension::Pct(100.0));
}
let (handlers, next) = (self.on_change.clone(), !self.on);
if !handlers.is_empty() {
root = root.on_click(move |_| {
for handler in &handlers {
handler(next);
}
});
}
if self.icon_only && !self.label.is_empty() {
root = root.describe("a11y_name", self.label.clone());
}
if let Some(target) = &self.focuses {
root = root.focuses(target);
}
if let Some(id) = &self.id {
root = root.id(id.clone());
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
root
}
pub fn render(mut self) -> Element {
let mut root = self.root();
if let Some(leading) = self.leading.take() {
root = root.child(leading);
}
if let Some(name) = self.icon {
root = root.child(glyph(&self.part(TogglePart::Icon), name, false));
}
if !self.icon_only {
let part = self.part(TogglePart::Label);
root = root.child(one_line(&part, &self.label));
}
root.into()
}
}
impl crate::ui::Styled for Toggle {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for Toggle {
const NAME: &'static str = "toggle";
type Renderer = ToggleRenderer;
fn parts() -> crate::ui::Declared {
TogglePart::declared()
}
fn states() -> crate::ui::Declared {
ToggleState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::*;
use TogglePart::{Icon, Label, Root};
let picked_out = |s: super::PartStyle| s.background(Highlight).foreground(HighlightForeground);
let recipe = Recipe::new()
.base(Root, |s| {
fading(
s.height(ControlSize::Md)
.padding_x(10.0)
.gap(8.0)
.radius(RadiusSize::Md)
.foreground(Foreground),
)
})
.base(Label, |s| s.typography(TypographyStyle::Button))
.base(Icon, |s| s.square(IconSize::Md))
.hovered(Root, picked_out)
.pressed(Root, |s| s.veil(StatePressed))
.focus_visible(Root, |s| s.focus_ring())
.size(ButtonSize::Xs, Root, |s| s.height(Density::Compact).padding_x(8.0))
.size(ButtonSize::Xs, Label, |s| s.font_size(12.0))
.size(ButtonSize::Sm, Root, |s| s.height(ControlSize::Sm).padding_x(8.0))
.size(ButtonSize::Lg, Root, |s| s.height(ControlSize::Lg).padding_x(12.0))
.size(ButtonSize::Xl, Root, |s| s.height(Density::Comfortable).padding_x(14.0))
.size(ButtonSize::Xl, Label, |s| s.font_size(15.0))
.state(ToggleState::On, Root, picked_out)
.when(Selector::new().state(ToggleState::On).hovered(), Root, |s| {
s.background(SecondaryHover)
})
.state(ToggleState::Disabled, Root, |s| s.opacity(OpacityLevel::Disabled))
.state(ToggleState::IconOnly, Root, |s| s.padding_x(0.0).width(ControlSize::Md));
let sized = |recipe: Recipe, size: ButtonSize, icon: Length, side: Length| {
recipe.size(size, Icon, |s| s.square(icon)).when(
Selector::new().size(size).state(ToggleState::IconOnly),
Root,
|s| s.width(side),
)
};
let recipe = sized(recipe, ButtonSize::Xs, IconSize::Sm.into(), Density::Compact.into());
let recipe = sized(recipe, ButtonSize::Sm, IconSize::Md.into(), ControlSize::Sm.into());
let recipe = sized(recipe, ButtonSize::Lg, Length::Px(18.0), ControlSize::Lg.into());
sized(recipe, ButtonSize::Xl, IconSize::Lg.into(), Density::Comfortable.into())
}
}