cosmic/theme/style/
menu_bar.rs1use std::sync::Arc;
5
6use crate::Theme;
7use iced_widget::core::Color;
8
9#[derive(Debug, Clone, Copy)]
11pub struct Appearance {
12    pub background: Color,
14    pub border_width: f32,
16    pub bar_border_radius: [f32; 4],
18    pub menu_border_radius: [f32; 4],
20    pub border_color: Color,
22    pub background_expand: [u16; 4],
24    pub path: Color,
26}
27
28pub trait StyleSheet {
30    type Style: Default;
32
33    fn appearance(&self, style: &Self::Style) -> Appearance;
35}
36
37#[derive(Default, Clone)]
39#[allow(missing_debug_implementations)]
40pub enum MenuBarStyle {
41    #[default]
43    Default,
44    Custom(Arc<dyn StyleSheet<Style = Theme> + Send + Sync>),
46}
47
48impl From<fn(&Theme) -> Appearance> for MenuBarStyle {
49    fn from(f: fn(&Theme) -> Appearance) -> Self {
50        Self::Custom(Arc::new(f))
51    }
52}
53
54impl StyleSheet for fn(&Theme) -> Appearance {
55    type Style = Theme;
56
57    fn appearance(&self, style: &Self::Style) -> Appearance {
58        (self)(style)
59    }
60}
61
62impl StyleSheet for Theme {
63    type Style = MenuBarStyle;
64
65    fn appearance(&self, style: &Self::Style) -> Appearance {
66        let cosmic = self.cosmic();
67        let component = &cosmic.background.component;
68
69        match style {
70            MenuBarStyle::Default => Appearance {
71                background: component.base.into(),
72                border_width: 1.0,
73                bar_border_radius: cosmic.corner_radii.radius_xl,
74                menu_border_radius: cosmic.corner_radii.radius_s.map(|x| x + 2.0),
75                border_color: component.divider.into(),
76                background_expand: [1; 4],
77                path: component.hover.into(),
78            },
79            MenuBarStyle::Custom(c) => c.appearance(self),
80        }
81    }
82}