iced_aw/style/
menu_bar.rs

1//! Change the appearance of menu bars and their menus.
2use super::{Status, StyleFn};
3use iced_core::{Background, Border, Color, Shadow, Theme, Vector};
4
5/// The appearance of a menu bar and its menus.
6#[derive(Debug, Clone, Copy)]
7pub struct Style {
8    /// The background of the menu bar.
9    pub bar_background: Background,
10    /// The border of the menu bar.
11    pub bar_border: Border,
12    /// The shadow of the menu bar.
13    pub bar_shadow: Shadow,
14
15    /// The background of the menus.
16    pub menu_background: Background,
17    /// The border of the menus.
18    pub menu_border: Border,
19    /// The shadow of the menus
20    pub menu_shadow: Shadow,
21
22    /// The backgraound of the path
23    pub path: Background,
24    /// The border of the path
25    pub path_border: Border,
26}
27
28impl std::default::Default for Style {
29    fn default() -> Self {
30        Self {
31            bar_background: Color::from([0.85; 3]).into(),
32            bar_border: Border {
33                radius: 8.0.into(),
34                ..Default::default()
35            },
36            bar_shadow: Shadow::default(),
37
38            menu_background: Color::from([0.85; 3]).into(),
39            menu_border: Border {
40                radius: 8.0.into(),
41                ..Default::default()
42            },
43            menu_shadow: Shadow {
44                color: Color::from([0.0, 0.0, 0.0, 0.5]),
45                offset: Vector::ZERO,
46                blur_radius: 10.0,
47            },
48            path: Color::from([0.3; 3]).into(),
49            path_border: Border {
50                radius: 6.0.into(),
51                ..Default::default()
52            },
53        }
54    }
55}
56
57/// The Catalog of a [`Menu`](crate::widget::menu::Menu).
58pub trait Catalog {
59    ///Style for the trait to use.
60    type Class<'a>;
61
62    /// The default class produced by the [`Catalog`].
63    fn default<'a>() -> Self::Class<'a>;
64
65    /// The [`Style`] of a class with the given status.
66    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
67}
68
69impl Catalog for Theme {
70    type Class<'a> = StyleFn<'a, Self, Style>;
71
72    fn default<'a>() -> Self::Class<'a> {
73        Box::new(primary)
74    }
75
76    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
77        class(self, status)
78    }
79}
80
81/// The primary theme of a [`Menu`](crate::widget::menu::Menu).
82#[must_use]
83pub fn primary(theme: &Theme, _status: Status) -> Style {
84    let palette = theme.extended_palette();
85
86    Style {
87        bar_background: palette.background.base.color.into(),
88        menu_background: palette.background.base.color.into(),
89        path: palette.primary.weak.color.into(),
90        ..Default::default()
91    }
92}