Add menu code, adapted from iced_aw

This commit is contained in:
Jeremy Soller 2023-11-16 08:00:11 -07:00
parent 23b480e58d
commit bad4400779
9 changed files with 2186 additions and 0 deletions

View file

@ -32,6 +32,8 @@ pub enum Button {
Transparent,
AppletMenu,
AppletIcon,
MenuRoot,
MenuItem,
}
pub fn appearance(
@ -120,6 +122,19 @@ pub fn appearance(
appearance.icon_color = Some(cosmic.background.on.into());
appearance.text_color = Some(cosmic.background.on.into());
}
Button::MenuRoot => {
appearance.background = None;
appearance.icon_color = Some(cosmic.accent.base.into());
appearance.text_color = Some(cosmic.accent.base.into());
}
Button::MenuItem => {
let (background, _, _) = color(&cosmic.background.component);
appearance.background = Some(Background::Color(background));
appearance.icon_color = Some(cosmic.background.on.into());
appearance.text_color = Some(cosmic.background.on.into());
corner_radii = &cosmic.corner_radii.radius_s;
}
}
appearance.border_radius = (*corner_radii).into();

View file

@ -0,0 +1,80 @@
// From iced_aw, license MIT
//! Change the appearance of menu bars and their menus.
use iced_widget::core::Color;
use crate::Theme;
/// The appearance of a menu bar and its menus.
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The background color of the menu bar and its menus.
pub background: Color,
/// The border width of the menu bar and its menus.
pub border_width: f32,
/// The border radius of the menu bar.
pub bar_border_radius: [f32; 4],
/// The border radius of the menus.
pub menu_border_radius: [f32; 4],
/// The border [`Color`] of the menu bar and its menus.
pub border_color: Color,
/// The expand value of the menus' background
pub background_expand: [u16; 4],
/// The highlighted path [`Color`] of the the menu bar and its menus.
pub path: Color,
}
/// The style sheet of a menu bar and its menus.
pub trait StyleSheet {
/// The supported style of the [`StyleSheet`].
type Style: Default;
/// Produces the [`Appearance`] of a menu bar and its menus.
fn appearance(&self, style: &Self::Style) -> Appearance;
}
/// The style of a menu bar and its menus
#[derive(Default)]
#[allow(missing_debug_implementations)]
pub enum MenuBarStyle {
/// The default style.
#[default]
Default,
/// A [`Theme`] that uses a `Custom` palette.
Custom(Box<dyn StyleSheet<Style = Theme>>),
}
impl From<fn(&Theme) -> Appearance> for MenuBarStyle {
fn from(f: fn(&Theme) -> Appearance) -> Self {
Self::Custom(Box::new(f))
}
}
impl StyleSheet for fn(&Theme) -> Appearance {
type Style = Theme;
fn appearance(&self, style: &Self::Style) -> Appearance {
(self)(style)
}
}
impl StyleSheet for Theme {
type Style = MenuBarStyle;
fn appearance(&self, style: &Self::Style) -> Appearance {
let cosmic = self.cosmic();
let component = &cosmic.background.component;
match style {
MenuBarStyle::Default => Appearance {
background: component.base.into(),
border_width: 1.0,
bar_border_radius: cosmic.corner_radii.radius_xl,
menu_border_radius: cosmic.corner_radii.radius_s,
border_color: component.divider.into(),
background_expand: [1; 4],
path: component.hover.into(),
},
MenuBarStyle::Custom(c) => c.appearance(self),
}
}
}

View file

@ -17,6 +17,8 @@ pub use self::iced::Rule;
pub use self::iced::Svg;
pub use self::iced::Text;
pub mod menu_bar;
mod segmented_button;
pub use self::segmented_button::SegmentedButton;