fix: increment a counter when there is a system theme mode change, or the theme is set to system

this forces a new subscription for the system theme, which seemed to fall behind previously unless the mode was changed back and forth
This commit is contained in:
Ashley Wulber 2023-10-26 14:21:07 -04:00 committed by Ashley Wulber
parent 047a8376ee
commit 6b517ddb0e
2 changed files with 10 additions and 4 deletions

View file

@ -49,6 +49,7 @@ pub struct Core {
/// Scaling factor used by the application
scale_factor: f32,
pub(super) theme_sub_counter: u64,
/// Last known system theme
pub(super) system_theme: Theme,
@ -75,6 +76,7 @@ impl Default for Core {
},
scale_factor: 1.0,
title: String::new(),
theme_sub_counter: 0,
system_theme: crate::theme::active(),
system_theme_mode: ThemeMode::config()
.map(|c| {

View file

@ -155,9 +155,12 @@ where
keyboard_nav::subscription()
.map(Message::KeyboardNav)
.map(super::Message::Cosmic),
theme::subscription(0, self.app.core().system_theme_mode.is_dark)
.map(Message::SystemThemeChange)
.map(super::Message::Cosmic),
theme::subscription(
self.app.core().theme_sub_counter,
self.app.core().system_theme_mode.is_dark,
)
.map(Message::SystemThemeChange)
.map(super::Message::Cosmic),
cosmic_config::config_subscription::<_, cosmic_theme::ThemeMode>(
0,
cosmic_theme::THEME_MODE_ID.into(),
@ -298,6 +301,7 @@ impl<T: Application> Cosmic<T> {
Message::AppThemeChange(mut theme) => {
// Apply last-known system theme if the system theme is preferred.
if let ThemeType::System(_) = theme.theme_type {
self.app.core_mut().theme_sub_counter += 1;
theme = self.app.core().system_theme.clone();
}
@ -310,7 +314,6 @@ impl<T: Application> Cosmic<T> {
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();
@ -333,6 +336,7 @@ impl<T: Application> Cosmic<T> {
let core = self.app.core_mut();
let changed = core.system_theme_mode.is_dark != mode.is_dark;
core.system_theme_mode = mode;
core.theme_sub_counter += 1;
if changed {
let new_theme = crate::theme::system_preference();
core.system_theme = new_theme.clone();