Skip to content

Commit

Permalink
fix: increment a counter when there is a system theme mode change, or…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
wash2 committed Oct 26, 2023
1 parent 047a837 commit 6b517dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/app/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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| {
Expand Down
12 changes: 8 additions & 4 deletions src/app/cosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
}

Expand All @@ -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();

Expand All @@ -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();
Expand Down

0 comments on commit 6b517dd

Please sign in to comment.