fix: set theme builder to match theme after theme is changed

This commit is contained in:
Ashley Wulber 2025-03-26 11:36:36 -04:00 • committed by Michael Murphy
parent 94a2b26499
commit 5ec142a480
2 changed files with 82 additions and 41 deletions

View file

@ -1,6 +1,7 @@
// Copyright 2023 System76 <info@system76.com> // Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use crate::PageCommands;
use crate::config::Config; use crate::config::Config;
#[cfg(feature = "page-accessibility")] #[cfg(feature = "page-accessibility")]
use crate::pages::accessibility; use crate::pages::accessibility;
@ -21,25 +22,24 @@ use crate::pages::sound;
use crate::pages::{self, system, time}; use crate::pages::{self, system, time};
use crate::subscription::desktop_files; use crate::subscription::desktop_files;
use crate::widget::{page_title, search_header}; use crate::widget::{page_title, search_header};
use crate::PageCommands;
use cosmic::app::context_drawer::ContextDrawer; use cosmic::app::context_drawer::ContextDrawer;
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
use cosmic::cctk::{sctk::output::OutputInfo, wayland_client::protocol::wl_output::WlOutput}; use cosmic::cctk::{sctk::output::OutputInfo, wayland_client::protocol::wl_output::WlOutput};
use cosmic::iced::Subscription; use cosmic::iced::Subscription;
use cosmic::widget::{self, button, row, text_input}; use cosmic::widget::{self, button, row, text_input};
use cosmic::{ use cosmic::{
Element,
app::{Core, Task}, app::{Core, Task},
iced::{ iced::{
self, self, Length,
event::{self, PlatformSpecific}, event::{self, PlatformSpecific},
window, Length, window,
}, },
prelude::*, prelude::*,
surface, surface,
widget::{ widget::{
column, container, icon, id_container, nav_bar, scrollable, segmented_button, settings, column, container, icon, id_container, nav_bar, scrollable, segmented_button, settings,
}, },
Element,
}; };
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
use cosmic_panel_config::CosmicPanelConfig; use cosmic_panel_config::CosmicPanelConfig;
@ -841,16 +841,24 @@ impl cosmic::Application for SettingsApp {
_keys: &[&'static str], _keys: &[&'static str],
new_theme: &cosmic::cosmic_theme::Theme, new_theme: &cosmic::cosmic_theme::Theme,
) -> Task<Self::Message> { ) -> Task<Self::Message> {
let mut tasks = Vec::new();
#[cfg(feature = "page-accessibility")] #[cfg(feature = "page-accessibility")]
if let Some(page) = self.pages.page_mut::<accessibility::Page>() { if let Some(page) = self.pages.page_mut::<accessibility::Page>() {
return page tasks.push(
.update(accessibility::Message::SystemTheme(Box::new( page.update(accessibility::Message::SystemTheme(Box::new(
new_theme.clone(), new_theme.clone(),
))) )))
.map(Into::into); .map(Into::into),
);
}
if let Some(page) = self.pages.page_mut::<appearance::Page>() {
tasks.push(
page.update(appearance::Message::NewTheme(Box::new(new_theme.clone())))
.map(Into::into),
);
} }
Task::none() Task::batch(tasks)
} }
} }

View file

@ -12,8 +12,8 @@ use cosmic::config::CosmicTk;
use cosmic::cosmic_config::{Config, ConfigSet, CosmicConfigEntry}; use cosmic::cosmic_config::{Config, ConfigSet, CosmicConfigEntry};
use cosmic::cosmic_theme::palette::{FromColor, Hsv, Srgb, Srgba}; use cosmic::cosmic_theme::palette::{FromColor, Hsv, Srgb, Srgba};
use cosmic::cosmic_theme::{ use cosmic::cosmic_theme::{
CornerRadii, Density, Spacing, Theme, ThemeBuilder, ThemeMode, DARK_THEME_BUILDER_ID, CornerRadii, DARK_THEME_BUILDER_ID, Density, LIGHT_THEME_BUILDER_ID, Spacing, Theme,
LIGHT_THEME_BUILDER_ID, ThemeBuilder, ThemeMode,
}; };
#[cfg(feature = "xdg-portal")] #[cfg(feature = "xdg-portal")]
use cosmic::dialog::file_chooser::{self, FileFilter}; use cosmic::dialog::file_chooser::{self, FileFilter};
@ -21,10 +21,10 @@ use cosmic::iced_core::{Alignment, Color, Length};
use cosmic::iced_widget::scrollable::{Direction, Scrollbar}; use cosmic::iced_widget::scrollable::{Direction, Scrollbar};
use cosmic::widget::icon::{from_name, icon}; use cosmic::widget::icon::{from_name, icon};
use cosmic::widget::{ use cosmic::widget::{
button, color_picker::ColorPickerUpdate, container, flex_row, horizontal_space, radio, row, ColorPickerModel, button, color_picker::ColorPickerUpdate, container, flex_row,
scrollable, settings, text, ColorPickerModel, horizontal_space, radio, row, scrollable, settings, text,
}; };
use cosmic::{widget, Apply, Element, Task}; use cosmic::{Apply, Element, Task, widget};
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
use cosmic_panel_config::CosmicPanelConfig; use cosmic_panel_config::CosmicPanelConfig;
use cosmic_settings_page::Section; use cosmic_settings_page::Section;
@ -162,7 +162,7 @@ impl
theme_mode_config, theme_mode_config,
theme_mode, theme_mode,
theme_builder_config, theme_builder_config,
theme_builder, mut theme_builder,
tk_config, tk_config,
accent_palette, accent_palette,
): ( ): (
@ -180,6 +180,18 @@ impl
} else { } else {
Theme::light_default() Theme::light_default()
}; };
theme_builder = theme_builder
.clone()
.accent(theme.accent.base.color)
.bg_color(theme.bg_color())
.corner_radii(theme.corner_radii)
.destructive(theme.destructive.base.color)
.spacing(theme.spacing)
.success(theme.success.base.color)
.warning(theme.warning.base.color)
.neutral_tint(theme.palette.neutral_5.color)
.text_tint(theme.background.on.color);
theme_builder.gaps = theme.gaps;
let custom_accent = theme_builder.accent.filter(|c| { let custom_accent = theme_builder.accent.filter(|c| {
let c = Srgba::new(c.red, c.green, c.blue, 1.0); let c = Srgba::new(c.red, c.green, c.blue, 1.0);
@ -580,6 +592,19 @@ impl Page {
Message::NewTheme(theme) => { Message::NewTheme(theme) => {
self.theme = *theme; self.theme = *theme;
self.theme_builder = self
.theme_builder
.clone()
.accent(self.theme.accent.base.color)
.bg_color(self.theme.bg_color())
.corner_radii(self.theme.corner_radii)
.destructive(self.theme.destructive.base.color)
.spacing(self.theme.spacing)
.success(self.theme.success.base.color)
.warning(self.theme.warning.base.color)
.neutral_tint(self.theme.palette.neutral_5.color)
.text_tint(self.theme.background.on.color);
self.theme_builder.gaps = self.theme.gaps;
} }
Message::DarkMode(enabled) => { Message::DarkMode(enabled) => {
if let Some(config) = self.theme_mode_config.as_ref() { if let Some(config) = self.theme_mode_config.as_ref() {
@ -1011,7 +1036,7 @@ impl Page {
#[cfg(feature = "xdg-portal")] #[cfg(feature = "xdg-portal")]
Message::ImportFile(f) => { Message::ImportFile(f) => {
let path_res = let path_res =
f.0 .0 f.0.0
.uris() .uris()
.first() .first()
.filter(|f| f.scheme() == "file") .filter(|f| f.scheme() == "file")
@ -1036,7 +1061,7 @@ impl Page {
#[cfg(feature = "xdg-portal")] #[cfg(feature = "xdg-portal")]
Message::ExportFile(f) => { Message::ExportFile(f) => {
let path_res = let path_res =
f.0 .0 f.0.0
.uris() .uris()
.first() .first()
.filter(|f| f.scheme() == "file") .filter(|f| f.scheme() == "file")
@ -1158,7 +1183,9 @@ impl Page {
if let Some(config) = self.tk_config.as_ref() { if let Some(config) = self.tk_config.as_ref() {
_ = config.set("apply_theme_global", enabled); _ = config.set("apply_theme_global", enabled);
} else { } else {
tracing::error!("Failed to apply theme to GNOME config because the CosmicTK config does not exist."); tracing::error!(
"Failed to apply theme to GNOME config because the CosmicTK config does not exist."
);
} }
return Task::none(); return Task::none();
@ -2051,30 +2078,36 @@ pub fn interface_density() -> Section<crate::pages::Message> {
settings::section() settings::section()
.title(&section.title) .title(&section.title)
.add(settings::item_row(vec![radio( .add(settings::item_row(vec![
text::body(&descriptions[compact]), radio(
Density::Compact, text::body(&descriptions[compact]),
Some(density), Density::Compact,
Message::Density, Some(density),
) Message::Density,
.width(Length::Fill) )
.into()])) .width(Length::Fill)
.add(settings::item_row(vec![radio( .into(),
text::body(&descriptions[comfortable]), ]))
Density::Standard, .add(settings::item_row(vec![
Some(density), radio(
Message::Density, text::body(&descriptions[comfortable]),
) Density::Standard,
.width(Length::Fill) Some(density),
.into()])) Message::Density,
.add(settings::item_row(vec![radio( )
text::body(&descriptions[spacious]), .width(Length::Fill)
Density::Spacious, .into(),
Some(density), ]))
Message::Density, .add(settings::item_row(vec![
) radio(
.width(Length::Fill) text::body(&descriptions[spacious]),
.into()])) Density::Spacious,
Some(density),
Message::Density,
)
.width(Length::Fill)
.into(),
]))
.apply(Element::from) .apply(Element::from)
.map(crate::pages::Message::Appearance) .map(crate::pages::Message::Appearance)
}) })