use std::time::Duration;
use freya_components::{
button::{
Button,
ButtonLayoutThemePartialExt,
ButtonLayoutThemePreference,
ButtonLayoutVariant,
},
get_theme,
};
use freya_core::prelude::*;
use crate::ripple::Ripple;
pub trait ButtonRippleExt {
fn ripple(self) -> RippleButton;
}
impl ButtonRippleExt for Button {
fn ripple(self) -> RippleButton {
RippleButton {
button: self,
ripple: Ripple::new(),
}
}
}
#[derive(Clone, PartialEq)]
pub struct RippleButton {
button: Button,
ripple: Ripple,
}
impl ChildrenExt for RippleButton {
fn children_mut(&mut self) -> &mut Vec<Element> {
self.ripple.children_mut()
}
}
impl RippleButton {
pub fn color(mut self, color: impl Into<Color>) -> Self {
self.ripple = self.ripple.color(color);
self
}
pub fn duration(mut self, duration: Duration) -> Self {
self.ripple = self.ripple.duration(duration);
self
}
}
impl Component for RippleButton {
fn render(&self) -> impl IntoElement {
let mut button = self.button.clone();
let theme_layout = match button.get_layout_variant() {
ButtonLayoutVariant::Normal => get_theme!(
&button.get_theme_layout(),
ButtonLayoutThemePreference,
"button_layout"
),
ButtonLayoutVariant::Compact => {
get_theme!(
&button.get_theme_layout(),
ButtonLayoutThemePreference,
"compact_button_layout"
)
}
ButtonLayoutVariant::Expanded => {
get_theme!(
&button.get_theme_layout(),
ButtonLayoutThemePreference,
"expanded_button_layout"
)
}
};
let ripple = self.ripple.clone().padding(theme_layout.padding);
button.children_mut().clear();
button.children_mut().push(ripple.into());
button.padding(0.)
}
fn render_key(&self) -> DiffKey {
self.button.render_key()
}
}