use std::sync::Arc;
use super::{
icons, names,
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe, Selector},
shared::{fading, glyph, pressable},
theme::{Themed, current_theme},
};
use crate::{
app::theme::{OpacityLevel, PaletteColor, RadiusSize, Theme},
components,
node::{CursorIcon, Element, Outline, color::Color},
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum PlayButtonSize {
Sm,
#[default]
Md,
Lg,
Xl,
}
names!(PlayButtonSize {
Sm => "sm",
Md => "md",
Lg => "lg",
Xl => "xl",
});
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum PlayButtonVariant {
#[default]
Primary,
Overlay,
}
names!(PlayButtonVariant {
Primary => "primary",
Overlay => "overlay",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PlayButtonPart {
Root,
Icon,
}
names!(PlayButtonPart {
Root => "root",
Icon => "icon",
});
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PlayButtonState {
Playing,
Replay,
Loading,
Disabled,
}
names!(PlayButtonState {
Playing => "playing",
Replay => "replay",
Loading => "loading",
Disabled => "disabled",
});
const LOADING_OPACITY: f32 = 0.75;
pub type PlayButtonRenderer = Arc<dyn Fn(PlayButtonView) -> Element + Send + Sync>;
pub struct PlayButton {
playing: bool,
loading: bool,
replay: bool,
disabled: bool,
size: Name,
variant: Name,
play_label: Arc<str>,
pause_label: Arc<str>,
replay_label: Arc<str>,
on_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Default for PlayButton {
fn default() -> Self {
Self::new()
}
}
impl PlayButton {
pub fn new() -> Self {
Self {
playing: false,
loading: false,
replay: false,
disabled: false,
size: PlayButtonSize::Md.into(),
variant: PlayButtonVariant::Primary.into(),
play_label: super::current_locale().media.play.clone(),
pause_label: super::current_locale().media.pause.clone(),
replay_label: super::current_locale().media.replay.clone(),
on_change: Vec::new(),
states: Vec::new(),
rules: Recipe::new(),
forced: None,
id: None,
key: None,
}
}
pub fn playing(mut self, playing: bool) -> Self {
self.playing = playing;
self
}
pub fn loading(mut self, loading: bool) -> Self {
self.loading = loading;
self
}
pub fn replay(mut self, replay: bool) -> Self {
self.replay = replay;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn size(mut self, size: impl Into<Name>) -> Self {
self.size = size.into();
self
}
pub fn variant(mut self, variant: impl Into<Name>) -> Self {
self.variant = variant.into();
self
}
pub fn play_label(mut self, label: &str) -> Self {
self.play_label = Arc::from(label);
self
}
pub fn pause_label(mut self, label: &str) -> Self {
self.pause_label = Arc::from(label);
self
}
pub fn replay_label(mut self, label: &str) -> Self {
self.replay_label = Arc::from(label);
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 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 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 fn build(self, theme: &Theme) -> Element {
let case = Case {
variant: Some(self.variant),
size: Some(self.size),
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(PlayButtonState::Playing, self.playing && !self.replay)
.state(PlayButtonState::Replay, self.replay)
.state(PlayButtonState::Loading, self.loading)
.state(PlayButtonState::Disabled, self.disabled);
let label = if self.replay {
self.replay_label
} else if self.playing {
self.pause_label
} else {
self.play_label
};
let view = PlayButtonView {
label,
playing: self.playing,
loading: self.loading,
replay: self.replay,
disabled: self.disabled,
variant: self.variant,
size: self.size,
recipe: theme
.resolve::<PlayButton>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
forced: self.forced,
on_change: self.on_change,
id: self.id,
key: self.key,
};
match theme.renderer::<PlayButton>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<PlayButton> for Element {
fn from(button: PlayButton) -> Self {
button.build(¤t_theme())
}
}
pub struct PlayButtonView {
pub label: Arc<str>,
pub playing: bool,
pub loading: bool,
pub replay: bool,
pub disabled: bool,
pub variant: Name,
pub size: Name,
pub recipe: ResolvedRecipe,
forced: Option<Interaction>,
on_change: Vec<Arc<dyn Fn(bool) + Send + Sync>>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl PlayButtonView {
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 icon_name(&self) -> &'static str {
if self.loading {
icons::SPINNER
} else if self.replay {
icons::ROTATE_LEFT
} else if self.playing {
icons::PAUSE
} else {
icons::PLAY
}
}
pub fn root(&self) -> components::Button {
let mut root = pressable(&self.part(PlayButtonPart::Root), self.disabled)
.describe("a11y_role", "button")
.describe("a11y_name", self.label.clone());
if !self.loading && !self.disabled && !self.on_change.is_empty() {
let (handlers, next) = (self.on_change.clone(), self.replay || !self.playing);
root = root.on_click(move |_| {
for handler in &handlers {
handler(next);
}
});
}
if self.loading {
root = root.describe("a11y_busy", "true");
}
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 icon(&self) -> Element {
glyph(
&self.part(PlayButtonPart::Icon),
Name::new(self.icon_name()),
self.loading,
)
}
pub fn render(self) -> Element {
self.root().child(self.icon()).into()
}
}
impl crate::ui::Styled for PlayButton {
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 PlayButton {
const NAME: &'static str = "play_button";
type Renderer = PlayButtonRenderer;
fn parts() -> crate::ui::Declared {
PlayButtonPart::declared()
}
fn states() -> crate::ui::Declared {
PlayButtonState::declared()
}
fn recipe() -> Recipe {
use PaletteColor::*;
use PlayButtonPart::{Icon, Root};
let white = Color::from_hex("#FFFFFF");
let overlay = PlayButtonVariant::Overlay;
let sized = |recipe: Recipe, size: PlayButtonSize, side: f32, icon: f32| {
recipe
.size(size, Root, |s| s.square(side))
.size(size, Icon, |s| s.square(icon))
};
let recipe = Recipe::new()
.base(Root, |s| {
fading(
s.square(40.0)
.radius(RadiusSize::Full)
.background(Primary)
.foreground(PrimaryForeground)
.cursor(CursorIcon::Pointer),
)
})
.base(Icon, |s| s.square(16.0))
.hovered(Root, |s| s.background(PrimaryHover))
.pressed(Root, |s| s.background(Primary).veil(StatePressed))
.focus_visible(Root, |s| s.focus_ring())
.variant(overlay, Root, |s| s.background(Color::from_hex("#00000099")).foreground(white))
.when(Selector::new().variant(overlay).hovered(), Root, |s| {
s.background(Color::from_hex("#000000B3"))
})
.when(Selector::new().variant(overlay).pressed(), Root, |s| {
s.background(Color::from_hex("#000000B3"))
})
.when(Selector::new().variant(overlay).focus_visible(), Root, |s| {
s.outline(Outline::new(2.0, white).offset(2.0))
})
.state(PlayButtonState::Loading, Root, |s| {
s.opacity(LOADING_OPACITY).cursor(CursorIcon::Default)
})
.state(PlayButtonState::Disabled, Root, |s| {
s.opacity(OpacityLevel::Disabled).cursor(CursorIcon::Default)
});
let recipe = sized(recipe, PlayButtonSize::Sm, 32.0, 14.0);
let recipe = sized(recipe, PlayButtonSize::Lg, 48.0, 20.0);
sized(recipe, PlayButtonSize::Xl, 64.0, 28.0)
}
}