refactor: icon styling and headerbar icon styling

Headerbar icons are transparent when their window is not focused, but otherwise share the same style as icons with selection. This updates the icon styles to match figma when selected.
This commit is contained in:
Ashley Wulber 2024-03-11 15:21:48 -04:00 committed by Ashley Wulber
parent 6f6eeec0e7
commit e47684ffdb
11 changed files with 121 additions and 115 deletions

View file

@ -6,6 +6,7 @@ use std::collections::HashMap;
use crate::config::CosmicTk;
use cosmic_config::CosmicConfigEntry;
use cosmic_theme::ThemeMode;
use iced::window;
use iced_core::window::Id;
use palette::Srgba;
@ -57,6 +58,9 @@ pub struct Core {
/// Scaling factor used by the application
scale_factor: f32,
/// Window focus state
pub(super) focused_window: Option<window::Id>,
pub(super) theme_sub_counter: u64,
/// Last known system theme
pub(super) system_theme: Theme,
@ -136,6 +140,7 @@ impl Default for Core {
height: 0,
width: 0,
},
focused_window: None,
#[cfg(feature = "applet")]
applet: crate::applet::Context::default(),
#[cfg(feature = "single-instance")]
@ -285,6 +290,12 @@ impl Core {
)
}
/// Get the current focused window if it exists
#[must_use]
pub fn focused_window(&self) -> Option<window::Id> {
self.focused_window.clone()
}
/// Whether the application should use a dark theme, according to the system
#[must_use]
pub fn system_is_dark(&self) -> bool {

View file

@ -78,6 +78,10 @@ pub enum Message {
ShowWindowMenu,
#[cfg(feature = "xdg-portal")]
DesktopSettings(crate::theme::portal::Desktop),
/// Window focus changed
Focus(window::Id),
/// Window focus lost
Unfocus(window::Id),
}
#[derive(Default)]
@ -159,6 +163,10 @@ where
iced::Event::Window(id, window::Event::Closed) => {
return Some(Message::SurfaceClosed(id))
}
iced::Event::Window(id, window::Event::Focused) => return Some(Message::Focus(id)),
iced::Event::Window(id, window::Event::Unfocused) => {
return Some(Message::Unfocus(id))
}
#[cfg(feature = "wayland")]
iced::Event::PlatformSpecific(PlatformSpecific::Wayland(event)) => match event {
wayland::Event::Window(WindowEvent::State(state), _surface, id) => {
@ -557,6 +565,22 @@ impl<T: Application> Cosmic<T> {
Message::ToolkitConfig(config) => {
self.app.core_mut().toolkit_config = config;
}
Message::Focus(f) => {
self.app.core_mut().focused_window = Some(f);
}
Message::Unfocus(id) => {
let core = self.app.core_mut();
if core
.focused_window
.as_ref()
.map(|cur| *cur == id)
.unwrap_or_default()
{
core.focused_window = None;
}
}
}
iced::Command::none()

View file

@ -642,6 +642,10 @@ impl<App: Application> ApplicationExt for App {
fn view_main(&self) -> Element<Message<Self::Message>> {
let core = self.core();
let is_condensed = core.is_condensed();
let focused = core
.focused_window()
.map(|i| i == self.main_window_id())
.unwrap_or_default();
let content_row = crate::widget::row::with_children({
let mut widgets = Vec::with_capacity(2);
@ -686,7 +690,7 @@ impl<App: Application> ApplicationExt for App {
.push_maybe(if core.window.show_headerbar {
Some({
let mut header = crate::widget::header_bar()
.window_id(self.main_window_id())
.focused(focused)
.title(&core.window.header_title)
.on_drag(Message::Cosmic(cosmic::Message::Drag))
.on_close(Message::Cosmic(cosmic::Message::Close))