Skip to main content

freya_material_design/
menu.rs

1use std::time::Duration;
2
3use freya_components::menu::MenuItem;
4use freya_core::prelude::*;
5use torin::size::Size;
6
7use crate::ripple::Ripple;
8
9/// Extension trait that adds ripple effect support to [MenuItem].
10pub trait MenuItemRippleExt {
11    /// Enable ripple effect on this menu item.
12    fn ripple(self) -> RippleMenuItem;
13}
14
15impl MenuItemRippleExt for MenuItem {
16    fn ripple(self) -> RippleMenuItem {
17        RippleMenuItem {
18            item: self,
19            ripple: Ripple::new(),
20        }
21    }
22}
23
24/// A MenuItem with a Ripple effect wrapper.
25///
26/// Created by calling [MenuItemRippleExt::ripple] on a MenuItem.
27#[derive(Clone, PartialEq)]
28pub struct RippleMenuItem {
29    item: MenuItem,
30    ripple: Ripple,
31}
32
33impl ChildrenExt for RippleMenuItem {
34    fn get_children(&mut self) -> &mut Vec<Element> {
35        self.ripple.get_children()
36    }
37}
38
39impl KeyExt for RippleMenuItem {
40    fn write_key(&mut self) -> &mut DiffKey {
41        self.item.write_key()
42    }
43}
44
45impl RippleMenuItem {
46    /// Set the color of the ripple effect.
47    pub fn color(mut self, color: impl Into<Color>) -> Self {
48        self.ripple = self.ripple.color(color);
49        self
50    }
51
52    /// Set the duration of the ripple animation.
53    pub fn duration(mut self, duration: Duration) -> Self {
54        self.ripple = self.ripple.duration(duration);
55        self
56    }
57}
58
59impl Component for RippleMenuItem {
60    fn render(&self) -> impl IntoElement {
61        let mut item = self.item.clone();
62
63        let padding = item.get_padding();
64
65        let ripple = self
66            .ripple
67            .clone()
68            .padding(padding)
69            .width(Size::fill_minimum());
70
71        item.get_children().clear();
72        item.get_children().push(ripple.into());
73        item.padding(0.)
74    }
75
76    fn render_key(&self) -> DiffKey {
77        ComponentOwned::render_key(&self.item)
78    }
79}