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, is_overlay: bool) -> 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, _is_overlay: bool) -> Appearance {
58 (self)(style)
59 }
60}
61
62impl StyleSheet for Theme {
63 type Style = MenuBarStyle;
64
65 fn appearance(&self, style: &Self::Style, is_overlay: bool) -> Appearance {
66 let cosmic = self.cosmic();
67 let component = &cosmic.background(!is_overlay && self.transparent).component;
68 let mut bg = component.base;
69
70 match style {
71 MenuBarStyle::Default => Appearance {
72 background: bg.into(),
73 border_width: 1.0,
74 bar_border_radius: cosmic.corner_radii.radius_xl,
75 menu_border_radius: cosmic.corner_radii.radius_s,
76 border_color: component.divider.into(),
77 background_expand: [1; 4],
78 path: component.hover.into(),
79 },
80 MenuBarStyle::Custom(c) => c.appearance(self, is_overlay),
81 }
82 }
83}