cosmic/theme/style/
menu_bar.rs

1// From iced_aw, license MIT
2
3//! Change the appearance of menu bars and their menus.
4use std::sync::Arc;
5
6use crate::Theme;
7use iced_widget::core::Color;
8
9/// The appearance of a menu bar and its menus.
10#[derive(Debug, Clone, Copy)]
11pub struct Appearance {
12    /// The background color of the menu bar and its menus.
13    pub background: Color,
14    /// The border width of the menu bar and its menus.
15    pub border_width: f32,
16    /// The border radius of the menu bar.
17    pub bar_border_radius: [f32; 4],
18    /// The border radius of the menus.
19    pub menu_border_radius: [f32; 4],
20    /// The border [`Color`] of the menu bar and its menus.
21    pub border_color: Color,
22    /// The expand value of the menus' background
23    pub background_expand: [u16; 4],
24    // /// The highlighted path [`Color`] of the the menu bar and its menus.
25    pub path: Color,
26}
27
28/// The style sheet of a menu bar and its menus.
29pub trait StyleSheet {
30    /// The supported style of the [`StyleSheet`].
31    type Style: Default;
32
33    /// Produces the [`Appearance`] of a menu bar and its menus.
34    fn appearance(&self, style: &Self::Style) -> Appearance;
35}
36
37/// The style of a menu bar and its menus
38#[derive(Default, Clone)]
39#[allow(missing_debug_implementations)]
40pub enum MenuBarStyle {
41    /// The default style.
42    #[default]
43    Default,
44    /// A [`Theme`] that uses a `Custom` palette.
45    Custom(Arc<dyn StyleSheet<Style = Theme>>),
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}