Skip to content

Commit 48af73a

Browse files
committed
feat: improve handling of list items using data in theme
1 parent 36e3eb5 commit 48af73a

5 files changed

Lines changed: 461 additions & 35 deletions

File tree

src/theme/mod.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
pub mod portal;
88
pub mod style;
99

10+
use ::iced::Alignment;
1011
use cosmic_config::{CosmicConfigEntry, config_subscription};
1112
use cosmic_theme::{Component, LayeredTheme, Spacing, ThemeMode};
1213
use iced_futures::Subscription;
@@ -47,6 +48,7 @@ pub(crate) static THEME: Mutex<Theme> = Mutex::new(Theme {
4748
theme_type: ThemeType::Dark,
4849
layer: cosmic_theme::Layer::Background,
4950
transparent: false,
51+
list_item_position: None,
5052
});
5153

5254
/// Currently-defined theme.
@@ -83,34 +85,6 @@ pub fn is_high_contrast() -> bool {
8385
active_type().is_high_contrast()
8486
}
8587

86-
// /// Watches for changes to the system's theme preference.
87-
// #[cold]
88-
// pub fn subscription(is_dark: bool) -> Subscription<crate::theme::Theme> {
89-
// config_subscription::<_, crate::cosmic_theme::Theme>(
90-
// (
91-
// std::any::TypeId::of::<crate::cosmic_theme::Theme>(),
92-
// is_dark,
93-
// ),
94-
// if is_dark {
95-
// cosmic_theme::DARK_THEME_ID
96-
// } else {
97-
// cosmic_theme::LIGHT_THEME_ID
98-
// }
99-
// .into(),
100-
// crate::cosmic_theme::Theme::VERSION,
101-
// )
102-
// .map(|res| {
103-
// for error in res.errors.into_iter().filter(cosmic_config::Error::is_err) {
104-
// tracing::error!(
105-
// ?error,
106-
// "error while watching system theme preference changes"
107-
// );
108-
// }
109-
110-
// Theme::system(Arc::new(res.config))
111-
// })
112-
// }
113-
11488
pub fn system_dark() -> Theme {
11589
let Ok(helper) = crate::cosmic_theme::Theme::dark_config() else {
11690
return Theme::dark();
@@ -211,6 +185,8 @@ pub struct Theme {
211185
pub theme_type: ThemeType,
212186
pub layer: cosmic_theme::Layer,
213187
pub transparent: bool,
188+
/// Only meaningful for widgets that must be in a list. Otherwise it should be ignored.
189+
pub list_item_position: Option<(Alignment, usize)>,
214190
}
215191

216192
impl Theme {
@@ -281,9 +257,9 @@ impl Theme {
281257
/// can be used in a component that is intended to be a child of a `CosmicContainer`
282258
pub fn current_container(&self) -> &cosmic_theme::Container {
283259
match self.layer {
284-
cosmic_theme::Layer::Background => &self.cosmic().background(self.transparent),
285-
cosmic_theme::Layer::Primary => &self.cosmic().primary(self.transparent),
286-
cosmic_theme::Layer::Secondary => &self.cosmic().secondary(self.transparent),
260+
cosmic_theme::Layer::Background => self.cosmic().background(self.transparent),
261+
cosmic_theme::Layer::Primary => self.cosmic().primary(self.transparent),
262+
cosmic_theme::Layer::Secondary => self.cosmic().secondary(self.transparent),
287263
}
288264
}
289265

@@ -292,6 +268,14 @@ impl Theme {
292268
pub fn set_theme(&mut self, theme: ThemeType) {
293269
self.theme_type = theme;
294270
}
271+
272+
#[inline]
273+
/// Clone theme with a list position
274+
pub fn with_list_item_position(&self, position: Option<(Alignment, usize)>) -> Self {
275+
let mut new = self.clone();
276+
new.list_item_position = position;
277+
new
278+
}
295279
}
296280

297281
impl LayeredTheme for Theme {

src/widget/button/widget.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//!
77
//! A [`Button`] has some local [`State`].
88
9+
use iced::Alignment;
910
use iced_runtime::core::widget::Id;
1011
use iced_runtime::{Action, Task, keyboard, task};
1112

@@ -449,7 +450,7 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
449450

450451
let state = tree.state.downcast_ref::<State>();
451452

452-
let styling = if !is_enabled {
453+
let mut styling = if !is_enabled {
453454
theme.disabled(&self.style)
454455
} else if is_mouse_over {
455456
if state.is_pressed {
@@ -471,6 +472,21 @@ impl<'a, Message: 'a + Clone> Widget<Message, crate::Theme, crate::Renderer>
471472

472473
theme.active(state.is_focused, self.selected, &self.style)
473474
};
475+
if matches!(self.style, crate::theme::Button::MenuItem) {
476+
match theme.list_item_position {
477+
Some((Alignment::Start, _)) => {
478+
styling.border_radius =
479+
styling.border_radius.bottom(theme.cosmic().radius_0()[3]);
480+
}
481+
Some((Alignment::End, _)) => {
482+
styling.border_radius = styling.border_radius.top(theme.cosmic().radius_0()[0]);
483+
}
484+
Some((Alignment::Center, _)) => {}
485+
None => {
486+
styling.border_radius = theme.cosmic().radius_0().into();
487+
}
488+
};
489+
}
474490

475491
let mut icon_color = styling.icon_color.unwrap_or(renderer_style.icon_color);
476492

src/widget/menu.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ mod menu_bar;
6666
pub(crate) use menu_bar::MenuBarState;
6767
pub use menu_bar::{MenuBar, menu_bar as bar};
6868

69+
pub mod menu_column;
70+
6971
mod menu_inner;
7072
mod menu_tree;
7173
pub use menu_tree::{

0 commit comments

Comments
 (0)