diff --git a/src/app/command.rs b/src/app/command.rs index 887379c42dc..74554c9ca5b 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -44,7 +44,7 @@ pub fn set_scaling_factor(factor: f32) -> iced::Command(theme: crate::Theme) -> iced::Command> { - message::cosmic(super::cosmic::Message::ThemeChange(theme)) + message::cosmic(super::cosmic::Message::AppThemeChange(theme)) } pub fn set_title(title: String) -> iced::Command> { diff --git a/src/app/core.rs b/src/app/core.rs index bf779add0c3..84232713518 100644 --- a/src/app/core.rs +++ b/src/app/core.rs @@ -1,6 +1,8 @@ // Copyright 2023 System76 // SPDX-License-Identifier: MPL-2.0 +use crate::Theme; + /// Status of the nav bar and its panels. #[derive(Clone)] pub struct NavBar { @@ -41,7 +43,10 @@ pub struct Core { /// Scaling factor used by the application scale_factor: f32, - pub(crate) title: String, + /// Last known system theme + pub(super) system_theme: Theme, + + pub(super) title: String, pub window: Window, #[cfg(feature = "applet")] pub applet_helper: super::applet::CosmicAppletHelper, @@ -59,6 +64,7 @@ impl Default for Core { }, scale_factor: 1.0, title: String::new(), + system_theme: crate::theme::active(), window: Window { header_title: String::new(), use_template: true, diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index f393110db43..d30209a9752 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: MPL-2.0 use super::{command, Application, ApplicationExt, Core, Subscription}; -use crate::theme::{self, Theme, THEME}; +use crate::theme::{self, Theme, ThemeType, THEME}; use crate::widget::nav_bar; use crate::{keyboard_nav, Element}; #[cfg(feature = "wayland")] @@ -20,6 +20,8 @@ use sctk::reexports::csd_frame::{WindowManagerCapabilities, WindowState}; /// A message managed internally by COSMIC. #[derive(Clone, Debug)] pub enum Message { + /// Application requests theme change. + AppThemeChange(Theme), /// Requests to close the window. Close, /// Requests to drag the window. @@ -34,12 +36,12 @@ pub enum Message { NavBar(nav_bar::Id), /// Set scaling factor ScaleFactor(f32), - /// Requests theme changes. - ThemeChange(Theme), /// Toggles visibility of the nav bar. ToggleNavBar, /// Toggles the condensed status of the nav bar. ToggleNavBarCondensed, + /// Notification of system theme changes. + SystemThemeChange(Theme), /// Updates the tracked window geometry. WindowResize(window::Id, u32, u32), /// Tracks updates to window state. @@ -149,7 +151,7 @@ where .map(Message::KeyboardNav) .map(super::Message::Cosmic), theme::subscription(0) - .map(Message::ThemeChange) + .map(Message::SystemThemeChange) .map(super::Message::Cosmic), window_events.map(super::Message::Cosmic), ]) @@ -272,13 +274,32 @@ impl Cosmic { self.app.core_mut().nav_bar_toggle_condensed(); } - Message::ThemeChange(theme) => { + Message::AppThemeChange(mut theme) => { + // Apply last-known system theme if the system theme is preferred. + if let ThemeType::System(_) = theme.theme_type { + theme = self.app.core().system_theme.clone(); + } + THEME.with(move |t| { let mut cosmic_theme = t.borrow_mut(); cosmic_theme.set_theme(theme.theme_type); }); } + Message::SystemThemeChange(theme) => { + // Record the last-known system theme in event that the current theme is custom. + self.app.core_mut().system_theme = theme.clone(); + + THEME.with(move |t| { + let mut cosmic_theme = t.borrow_mut(); + + // Anly apply update if the theme is set to load a system theme + if let ThemeType::System(_) = cosmic_theme.theme_type { + cosmic_theme.set_theme(theme.theme_type); + } + }); + } + Message::ScaleFactor(factor) => { self.app.core_mut().set_scale_factor(factor); }