Skip to content

Conversation

@aliaksandr-trush
Copy link
Contributor

@aliaksandr-trush aliaksandr-trush commented Nov 22, 2025

Applet for switching Light/Dark theme

Fixing #739

@aliaksandr-trush aliaksandr-trush changed the title WIP feate: Theme applet WIP feat: Theme applet Nov 22, 2025
Copy link
Member

@mmstick mmstick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want this at the moment, but these would need to be fixed.

core: cosmic::app::Core,
icon_name: String,
popup: Option<window::Id>,
is_dark: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property isn't needed because it's managed by libcosmic

fn subscription(&self) -> cosmic::iced::Subscription<Message> {
cosmic::iced::Subscription::batch(vec![
theme_subscription::theme_subscription(self.theme_file.clone())
.map(Message::ThemeUpdate),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A subscription for theme changes is not necessary since you can get that from libcosmic directly.

let toggle = cosmic::widget::button::custom(
widget::container(
row![
icon::from_name(if self.is_dark {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use cosmic::theme::is_dark() instead.

enum Message {
Action(ThemeAction),
Closed(window::Id),
ThemeUpdate(theme_subscription::ThemeUpdate),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variant can be removed because libcosmic is already subscribed to theme changes on startup

}
fs::write(&theme_file, is_dark.to_string())
.expect("Failed to write initial theme state");
}
Copy link
Member

@mmstick mmstick Nov 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need to write this file, or assume that dark is the system default.

"weather-sunny-symbolic"
}
.to_string();
Task::none()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to toggle it through libcosmic

use cosmic::cosmic_config::ConfigSet;
use cosmic::cosmic_theme::ThemeMode;
ToggleTheme => {
    if let Ok(config) = ThemeMode::config() {
        config.set::<bool>("is_dark", !cosmic::theme::is_dark());
    }
}

"weather-clear-night-symbolic"
} else {
"weather-sunny-symbolic"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to cache the icon handle instead of the name

widget::icon::from_name("icon-name").handle()

ThemeAction::ToggleTheme => {
self.is_dark = !self.is_dark;
fs::write(&self.theme_file, self.is_dark.to_string())
.expect("Failed to write theme state");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't write to files directly here. See cosmic_config below.

Changed(bool), // is_dark
}

pub fn theme_subscription(theme_file: PathBuf) -> Subscription<ThemeUpdate> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be removed. In the future you'd want to use the application's core to subscribe to a cosmic_config interface.

self.core.watch_config::<MyAppConfig>("com.github.MyAppName")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants