refactor(theme): create Container::ContextDrawer style variant

This commit is contained in:
Michael Aaron Murphy 2024-03-05 15:01:32 +01:00 • committed by Jeremy Soller
parent 88da3fcc14
commit db5d989972
4 changed files with 82 additions and 59 deletions

View file

@ -238,7 +238,6 @@ impl Theme {
/// get current container /// get current container
/// can be used in a component that is intended to be a child of a `CosmicContainer` /// can be used in a component that is intended to be a child of a `CosmicContainer`
#[must_use]
pub fn current_container(&self) -> &cosmic_theme::Container { pub fn current_container(&self) -> &cosmic_theme::Container {
match self.layer { match self.layer {
cosmic_theme::Layer::Background => &self.cosmic().background, cosmic_theme::Layer::Background => &self.cosmic().background,

View file

@ -369,6 +369,7 @@ pub enum Container {
WindowBackground, WindowBackground,
Background, Background,
Card, Card,
ContextDrawer,
Custom(Box<dyn Fn(&Theme) -> container::Appearance>), Custom(Box<dyn Fn(&Theme) -> container::Appearance>),
Dialog, Dialog,
Dropdown, Dropdown,
@ -384,6 +385,48 @@ impl Container {
pub fn custom<F: Fn(&Theme) -> container::Appearance + 'static>(f: F) -> Self { pub fn custom<F: Fn(&Theme) -> container::Appearance + 'static>(f: F) -> Self {
Self::Custom(Box::new(f)) Self::Custom(Box::new(f))
} }
#[must_use]
pub fn background(theme: &cosmic_theme::Theme) -> container::Appearance {
container::Appearance {
icon_color: Some(Color::from(theme.background.on)),
text_color: Some(Color::from(theme.background.on)),
background: Some(iced::Background::Color(theme.background.base.into())),
border: Border {
radius: theme.corner_radii.radius_xs.into(),
..Default::default()
},
shadow: Shadow::default(),
}
}
#[must_use]
pub fn primary(theme: &cosmic_theme::Theme) -> container::Appearance {
container::Appearance {
icon_color: Some(Color::from(theme.primary.on)),
text_color: Some(Color::from(theme.primary.on)),
background: Some(iced::Background::Color(theme.primary.base.into())),
border: Border {
radius: theme.corner_radii.radius_xs.into(),
..Default::default()
},
shadow: Shadow::default(),
}
}
#[must_use]
pub fn secondary(theme: &cosmic_theme::Theme) -> container::Appearance {
container::Appearance {
icon_color: Some(Color::from(theme.secondary.on)),
text_color: Some(Color::from(theme.secondary.on)),
background: Some(iced::Background::Color(theme.secondary.base.into())),
border: Border {
radius: theme.corner_radii.radius_xs.into(),
..Default::default()
},
shadow: Shadow::default(),
}
}
} }
impl container::StyleSheet for Theme { impl container::StyleSheet for Theme {
@ -394,7 +437,9 @@ impl container::StyleSheet for Theme {
let cosmic = self.cosmic(); let cosmic = self.cosmic();
match style { match style {
Container::Transparent => container::Appearance::default(), Container::Transparent => container::Appearance::default(),
Container::Custom(f) => f(self), Container::Custom(f) => f(self),
Container::WindowBackground => container::Appearance { Container::WindowBackground => container::Appearance {
icon_color: Some(Color::from(cosmic.background.on)), icon_color: Some(Color::from(cosmic.background.on)),
text_color: Some(Color::from(cosmic.background.on)), text_color: Some(Color::from(cosmic.background.on)),
@ -411,56 +456,48 @@ impl container::StyleSheet for Theme {
}, },
shadow: Shadow::default(), shadow: Shadow::default(),
}, },
Container::Background => container::Appearance {
icon_color: Some(Color::from(cosmic.background.on)), Container::HeaderBar => container::Appearance {
icon_color: Some(Color::from(cosmic.accent.base)),
text_color: Some(Color::from(cosmic.background.on)), text_color: Some(Color::from(cosmic.background.on)),
background: Some(iced::Background::Color(cosmic.background.base.into())), background: Some(iced::Background::Color(cosmic.background.base.into())),
border: Border { border: Border {
radius: cosmic.corner_radii.radius_xs.into(), radius: [
cosmic.corner_radii.radius_xs[0],
cosmic.corner_radii.radius_xs[1],
cosmic.corner_radii.radius_0[2],
cosmic.corner_radii.radius_0[3],
]
.into(),
..Default::default() ..Default::default()
}, },
shadow: Shadow::default(), shadow: Shadow::default(),
}, },
Container::HeaderBar => {
let header_top = cosmic.background.base;
container::Appearance { Container::ContextDrawer => {
icon_color: Some(Color::from(cosmic.accent.base)), let mut appearance = crate::style::Container::primary(cosmic);
text_color: Some(Color::from(cosmic.background.on)),
background: Some(iced::Background::Color(header_top.into())), appearance.border = Border {
border: Border { color: cosmic.primary.divider.into(),
radius: [ width: 1.0,
cosmic.corner_radii.radius_xs[0], radius: cosmic.corner_radii.radius_s.into(),
cosmic.corner_radii.radius_xs[1], };
cosmic.corner_radii.radius_0[2],
cosmic.corner_radii.radius_0[3], appearance.shadow = Shadow {
] color: cosmic.shade.into(),
.into(), offset: Vector::new(0.0, 0.0),
..Default::default() blur_radius: 16.0,
}, };
shadow: Shadow::default(),
} appearance
} }
Container::Primary => container::Appearance {
icon_color: Some(Color::from(cosmic.primary.on)), Container::Background => Container::background(cosmic),
text_color: Some(Color::from(cosmic.primary.on)),
background: Some(iced::Background::Color(cosmic.primary.base.into())), Container::Primary => Container::primary(cosmic),
border: Border {
radius: cosmic.corner_radii.radius_xs.into(), Container::Secondary => Container::secondary(cosmic),
..Default::default()
},
shadow: Shadow::default(),
},
Container::Secondary => container::Appearance {
icon_color: Some(Color::from(cosmic.secondary.on)),
text_color: Some(Color::from(cosmic.secondary.on)),
background: Some(iced::Background::Color(cosmic.secondary.base.into())),
border: Border {
radius: cosmic.corner_radii.radius_xs.into(),
..Default::default()
},
shadow: Shadow::default(),
},
Container::Dropdown => { Container::Dropdown => {
let theme = self.cosmic(); let theme = self.cosmic();
@ -475,6 +512,7 @@ impl container::StyleSheet for Theme {
shadow: Shadow::default(), shadow: Shadow::default(),
} }
} }
Container::Tooltip => container::Appearance { Container::Tooltip => container::Appearance {
icon_color: None, icon_color: None,
text_color: None, text_color: None,

View file

@ -8,9 +8,9 @@ use crate::{Apply, Element, Renderer, Theme};
use super::overlay::Overlay; use super::overlay::Overlay;
use iced_core::alignment;
use iced_core::event::{self, Event}; use iced_core::event::{self, Event};
use iced_core::widget::{Operation, Tree}; use iced_core::widget::{Operation, Tree};
use iced_core::{alignment, Border};
use iced_core::{ use iced_core::{
layout, mouse, overlay as iced_overlay, renderer, Clipboard, Color, Layout, Length, Padding, layout, mouse, overlay as iced_overlay, renderer, Clipboard, Color, Layout, Length, Padding,
Rectangle, Shell, Widget, Rectangle, Shell, Widget,
@ -85,20 +85,7 @@ impl<'a, Message: Clone + 'static> ContextDrawer<'a, Message> {
// XXX this is a hack to get around that // XXX this is a hack to get around that
drawer: container( drawer: container(
LayerContainer::new(pane) LayerContainer::new(pane)
.style(crate::style::Container::custom(move |theme| { .style(crate::style::Container::ContextDrawer)
let palette = theme.cosmic();
container::Appearance {
icon_color: Some(Color::from(palette.primary.on)),
text_color: Some(Color::from(palette.primary.on)),
background: Some(iced::Background::Color(palette.primary.base.into())),
border: Border {
radius: palette.corner_radii.radius_s.into(),
..Default::default()
},
..Default::default()
}
}))
.layer(cosmic_theme::Layer::Primary) .layer(cosmic_theme::Layer::Primary)
.width(Length::Fill) .width(Length::Fill)
.height(Length::Fill) .height(Length::Fill)

View file

@ -9,7 +9,6 @@ use crate::{
}; };
use derive_setters::Setters; use derive_setters::Setters;
use iced_core::Length; use iced_core::Length;
use iced_widget::container;
/// A settings item aligned in a row /// A settings item aligned in a row
#[must_use] #[must_use]
@ -80,7 +79,7 @@ impl<'a, Message: 'static> Item<'a, Message> {
contents.push(text(self.title).width(Length::Fill).into()); contents.push(text(self.title).width(Length::Fill).into());
} }
contents.push(widget.into().into()); contents.push(widget.into());
item_row(contents) item_row(contents)
} }