fix(app): restore distinction between app theme changes, and system theme changes

This commit is contained in:
Michael Aaron Murphy 2023-09-14 01:25:01 +02:00 committed by Michael Murphy
parent baad15033c
commit 023d8ad3a0
3 changed files with 34 additions and 7 deletions

View file

@ -44,7 +44,7 @@ pub fn set_scaling_factor<M: Send + 'static>(factor: f32) -> iced::Command<Messa
} }
pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Command<Message<M>> { pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Command<Message<M>> {
message::cosmic(super::cosmic::Message::ThemeChange(theme)) message::cosmic(super::cosmic::Message::AppThemeChange(theme))
} }
pub fn set_title<M: Send + 'static>(title: String) -> iced::Command<Message<M>> { pub fn set_title<M: Send + 'static>(title: String) -> iced::Command<Message<M>> {

View file

@ -1,6 +1,8 @@
// Copyright 2023 System76 <info@system76.com> // Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use crate::Theme;
/// Status of the nav bar and its panels. /// Status of the nav bar and its panels.
#[derive(Clone)] #[derive(Clone)]
pub struct NavBar { pub struct NavBar {
@ -41,7 +43,10 @@ pub struct Core {
/// Scaling factor used by the application /// Scaling factor used by the application
scale_factor: f32, scale_factor: f32,
pub(crate) title: String, /// Last known system theme
pub(super) system_theme: Theme,
pub(super) title: String,
pub window: Window, pub window: Window,
#[cfg(feature = "applet")] #[cfg(feature = "applet")]
pub applet_helper: super::applet::CosmicAppletHelper, pub applet_helper: super::applet::CosmicAppletHelper,
@ -59,6 +64,7 @@ impl Default for Core {
}, },
scale_factor: 1.0, scale_factor: 1.0,
title: String::new(), title: String::new(),
system_theme: crate::theme::active(),
window: Window { window: Window {
header_title: String::new(), header_title: String::new(),
use_template: true, use_template: true,

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
use super::{command, Application, ApplicationExt, Core, Subscription}; 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::widget::nav_bar;
use crate::{keyboard_nav, Element}; use crate::{keyboard_nav, Element};
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
@ -20,6 +20,8 @@ use sctk::reexports::csd_frame::{WindowManagerCapabilities, WindowState};
/// A message managed internally by COSMIC. /// A message managed internally by COSMIC.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Message { pub enum Message {
/// Application requests theme change.
AppThemeChange(Theme),
/// Requests to close the window. /// Requests to close the window.
Close, Close,
/// Requests to drag the window. /// Requests to drag the window.
@ -34,12 +36,12 @@ pub enum Message {
NavBar(nav_bar::Id), NavBar(nav_bar::Id),
/// Set scaling factor /// Set scaling factor
ScaleFactor(f32), ScaleFactor(f32),
/// Requests theme changes.
ThemeChange(Theme),
/// Toggles visibility of the nav bar. /// Toggles visibility of the nav bar.
ToggleNavBar, ToggleNavBar,
/// Toggles the condensed status of the nav bar. /// Toggles the condensed status of the nav bar.
ToggleNavBarCondensed, ToggleNavBarCondensed,
/// Notification of system theme changes.
SystemThemeChange(Theme),
/// Updates the tracked window geometry. /// Updates the tracked window geometry.
WindowResize(window::Id, u32, u32), WindowResize(window::Id, u32, u32),
/// Tracks updates to window state. /// Tracks updates to window state.
@ -149,7 +151,7 @@ where
.map(Message::KeyboardNav) .map(Message::KeyboardNav)
.map(super::Message::Cosmic), .map(super::Message::Cosmic),
theme::subscription(0) theme::subscription(0)
.map(Message::ThemeChange) .map(Message::SystemThemeChange)
.map(super::Message::Cosmic), .map(super::Message::Cosmic),
window_events.map(super::Message::Cosmic), window_events.map(super::Message::Cosmic),
]) ])
@ -272,13 +274,32 @@ impl<T: Application> Cosmic<T> {
self.app.core_mut().nav_bar_toggle_condensed(); 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| { THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut(); let mut cosmic_theme = t.borrow_mut();
cosmic_theme.set_theme(theme.theme_type); 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) => { Message::ScaleFactor(factor) => {
self.app.core_mut().set_scale_factor(factor); self.app.core_mut().set_scale_factor(factor);
} }