Fix edge cases when restoring system theme

This commit is contained in:
Héctor Ramón Jiménez 2025-09-08 11:03:45 +02:00
parent 74425d5cb1
commit 7fedb0cc9b
No known key found for this signature in database
GPG key ID: 4C07CEC81AFA161F
2 changed files with 40 additions and 4 deletions

View file

@ -32,6 +32,7 @@ enum Message {
TogglerToggled(bool),
PreviousTheme,
NextTheme,
ClearTheme,
}
impl Styling {
@ -67,6 +68,9 @@ impl Styling {
}
});
}
Message::ClearTheme => {
self.theme = None;
}
}
}
@ -192,6 +196,9 @@ impl Styling {
keyboard::key::Named::ArrowDown
| keyboard::key::Named::ArrowRight,
) => Some(Message::NextTheme),
keyboard::Key::Named(keyboard::key::Named::Space) => {
Some(Message::ClearTheme)
}
_ => None,
})
}

View file

@ -252,14 +252,43 @@ where
self.theme = program.theme(window_id);
self.style = program.style(self.theme());
if let Some(theme) = &self.theme {
let new_mode = theme::Base::mode(theme);
let new_mode = self
.theme
.as_ref()
.map(theme::Base::mode)
.unwrap_or_default();
if self.theme_mode != new_mode {
if self.theme_mode != new_mode {
#[cfg(not(target_os = "linux"))]
{
window.set_theme(conversion::window_theme(new_mode));
self.theme_mode = new_mode;
// Assume the old mode matches the system one
// We will be notified otherwise
if new_mode == theme::Mode::None {
self.default_theme =
<P::Theme as theme::Base>::default(self.theme_mode);
if self.theme.is_none() {
self.style = program.style(&self.default_theme);
}
}
}
#[cfg(target_os = "linux")]
{
// mundy always notifies system theme changes, so we
// just restore the default theme mode.
let new_mode = if new_mode == theme::Mode::None {
theme::Base::mode(&self.default_theme)
} else {
new_mode
};
window.set_theme(conversion::window_theme(new_mode));
}
self.theme_mode = new_mode;
}
}
}