1// From iced_aw, license MIT
23//! Change the appearance of menu bars and their menus.
4use std::sync::Arc;
56use crate::Theme;
7use iced_widget::core::Color;
89/// 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.
13pub background: Color,
14/// The border width of the menu bar and its menus.
15pub border_width: f32,
16/// The border radius of the menu bar.
17pub bar_border_radius: [f32; 4],
18/// The border radius of the menus.
19pub menu_border_radius: [f32; 4],
20/// The border [`Color`] of the menu bar and its menus.
21pub border_color: Color,
22/// The expand value of the menus' background
23pub background_expand: [u16; 4],
24// /// The highlighted path [`Color`] of the the menu bar and its menus.
25pub path: Color,
26}
2728/// The style sheet of a menu bar and its menus.
29pub trait StyleSheet {
30/// The supported style of the [`StyleSheet`].
31type Style: Default;
3233/// Produces the [`Appearance`] of a menu bar and its menus.
34fn appearance(&self, style: &Self::Style) -> Appearance;
35}
3637/// 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]
43Default,
44/// A [`Theme`] that uses a `Custom` palette.
45Custom(Arc<dyn StyleSheet<Style = Theme>>),
46}
4748impl From<fn(&Theme) -> Appearance> for MenuBarStyle {
49fn from(f: fn(&Theme) -> Appearance) -> Self {
50Self::Custom(Arc::new(f))
51 }
52}
5354impl StyleSheet for fn(&Theme) -> Appearance {
55type Style = Theme;
5657fn appearance(&self, style: &Self::Style) -> Appearance {
58 (self)(style)
59 }
60}
6162impl StyleSheet for Theme {
63type Style = MenuBarStyle;
6465fn appearance(&self, style: &Self::Style) -> Appearance {
66let cosmic = self.cosmic();
67let component = &cosmic.background.component;
6869match 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}