Skip to content

Commit

Permalink
fix(app): restore distinction between app theme changes, and system t…
Browse files Browse the repository at this point in the history
…heme changes
  • Loading branch information
mmstick committed Sep 13, 2023
1 parent bba7888 commit d139bc0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
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>> {
Expand Down
8 changes: 7 additions & 1 deletion src/app/core.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

use crate::Theme;

/// Status of the nav bar and its panels.
#[derive(Clone)]
pub struct NavBar {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
31 changes: 26 additions & 5 deletions src/app/cosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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),
])
Expand Down Expand Up @@ -272,13 +274,32 @@ impl<T: Application> Cosmic<T> {
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);
}
Expand Down

0 comments on commit d139bc0

Please sign in to comment.