From 8f571e9e251f43ab2c6a9f9fcdcf16879c67d58c Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Thu, 25 Apr 2024 14:10:09 -0700 Subject: [PATCH] Use custom style for workspace items None of the default styles seem to work quite right here. --- src/main.rs | 6 ------ src/view/mod.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7bb0df2..f6f71cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -671,12 +671,6 @@ impl Application for App { fn core_mut(&mut self) -> &mut cosmic::app::Core { &mut self.core } - - fn style( - &self, - ) -> Option<::Style> { - Some(cosmic::theme::style::iced::Application::default()) - } } fn init_localizer() { diff --git a/src/view/mod.rs b/src/view/mod.rs index 8afb159..e2b5ee5 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -87,16 +87,46 @@ fn close_button(on_press: Msg) -> cosmic::Element<'static, Msg> { .into() } +fn workspace_item_appearance( + theme: &cosmic::Theme, + is_active: bool, + hovered: bool, +) -> cosmic::widget::button::Appearance { + let cosmic = theme.cosmic(); + let mut appearance = cosmic::widget::button::Appearance::new(); + appearance.border_radius = cosmic.corner_radii.radius_s.into(); + if is_active { + appearance.border_width = 2.0; + appearance.border_color = cosmic.accent.base.into(); + } + if hovered { + appearance.background = Some(iced::Background::Color(cosmic.button.base.into())); + } + appearance +} + pub(crate) fn workspace_item<'a>( workspace: &'a Workspace, output: &wl_output::WlOutput, ) -> cosmic::Element<'a, Msg> { let image = capture_image(workspace.img_for_output.get(output)); + let is_active = workspace.is_active; column![ // TODO editable name? widget::button(column![image, widget::text(&workspace.name)]) .selected(workspace.is_active) - .style(cosmic::theme::Button::Image) + .style(cosmic::theme::Button::Custom { + active: Box::new(move |_focused, theme| workspace_item_appearance( + theme, is_active, false + )), + disabled: Box::new(|_theme| { unreachable!() }), + hovered: Box::new(move |_focused, theme| workspace_item_appearance( + theme, is_active, true + )), + pressed: Box::new(move |_focused, theme| workspace_item_appearance( + theme, is_active, true + )), + }) .on_press(Msg::ActivateWorkspace(workspace.handle.clone())), ] .spacing(4)