refactor: optional config subscriptions using dbus

This commit is contained in:
Ashley Wulber 2023-12-15 17:00:08 -05:00 committed by Michael Murphy
parent a4d1b1b651
commit 06c33dcf06
14 changed files with 381 additions and 139 deletions

View file

@ -69,6 +69,9 @@ pub struct Core {
#[cfg(feature = "single-instance")]
pub(crate) single_instance: bool,
#[cfg(feature = "dbus-config")]
pub(crate) settings_daemon: Option<cosmic_settings_daemon::CosmicSettingsDaemonProxy<'static>>,
}
impl Default for Core {
@ -114,6 +117,8 @@ impl Default for Core {
applet: crate::applet::Context::default(),
#[cfg(feature = "single-instance")]
single_instance: false,
#[cfg(feature = "dbus-config")]
settings_daemon: None,
}
}
}
@ -208,4 +213,16 @@ impl Core {
pub fn system_theme_mode(&self) -> ThemeMode {
self.system_theme_mode
}
#[cfg(feature = "dbus-config")]
pub fn watch_config<T: CosmicConfigEntry + Send + Sync + Default + 'static + Clone>(
&self,
config_id: &'static str,
) -> iced::Subscription<cosmic_config::dbus::ConfigUpdate<T>> {
if let Some(settings_daemon) = self.settings_daemon.clone() {
cosmic_config::dbus::watcher_subscription(settings_daemon, config_id)
} else {
iced::Subscription::none()
}
}
}

View file

@ -1,6 +1,8 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use std::sync::Arc;
use super::{command, Application, ApplicationExt, Core, Subscription};
use crate::theme::{self, Theme, ThemeType, THEME};
use crate::widget::nav_bar;
@ -64,6 +66,9 @@ pub enum Message {
WmCapabilities(window::Id, WindowManagerCapabilities),
/// Activate the application
Activate(String),
#[cfg(feature = "dbus-config")]
/// dbus settings daemon setup
SettingsDaemon(zbus::Result<cosmic_settings_daemon::CosmicSettingsDaemonProxy<'static>>),
}
#[derive(Default)]
@ -83,6 +88,13 @@ where
fn new((core, flags): Self::Flags) -> (Self, iced::Command<Self::Message>) {
let (model, command) = T::init(core, flags);
#[cfg(feature = "dbus-config")]
let command = iced::Command::batch(vec![
command,
iced::Command::perform(cosmic_config::dbus::settings_daemon_proxy(), |p| {
super::Message::Cosmic(super::cosmic::Message::SettingsDaemon(p))
}),
]);
(Self::new(model), command)
}
@ -164,12 +176,26 @@ where
keyboard_nav::subscription()
.map(Message::KeyboardNav)
.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),
#[cfg(feature = "dbus-config")]
self.app
.core()
.watch_config::<cosmic_theme::Theme>(if self.app.core().system_theme_mode.is_dark {
cosmic_theme::DARK_THEME_ID
} else {
cosmic_theme::LIGHT_THEME_ID
})
.map(|update| {
for e in update.errors {
tracing::error!("{e}");
}
Message::SystemThemeChange(crate::theme::Theme::system(Arc::new(update.config)))
})
.map(super::Message::Cosmic),
#[cfg(not(feature = "dbus-config"))]
theme::subscription(self.app.core().system_theme_mode.is_dark)
.map(Message::SystemThemeChange)
.map(super::Message::Cosmic),
#[cfg(not(feature = "dbus-config"))]
cosmic_config::config_subscription::<_, cosmic_theme::ThemeMode>(
0,
cosmic_theme::THEME_MODE_ID.into(),
@ -185,6 +211,17 @@ where
}
})
.map(super::Message::Cosmic),
#[cfg(feature = "dbus-config")]
self.app
.core()
.watch_config::<ThemeMode>(cosmic_theme::THEME_MODE_ID)
.map(|update| {
for e in update.errors {
tracing::error!("{e}");
}
Message::SystemThemeModeChange(update.config)
})
.map(super::Message::Cosmic),
window_events.map(super::Message::Cosmic),
#[cfg(feature = "single-instance")]
self.app
@ -384,6 +421,15 @@ impl<T: Application> Cosmic<T> {
_token,
);
}
#[cfg(feature = "dbus-config")]
Message::SettingsDaemon(p) => match p {
Ok(p) => {
self.app.core_mut().settings_daemon = Some(p);
}
Err(e) => {
tracing::error!("Failed to connect to settings daemon: {e}");
}
},
}
iced::Command::none()

View file

@ -551,7 +551,7 @@ impl<App: Application> ApplicationExt for App {
#[cfg(any(feature = "multi-window", feature = "wayland"))]
fn title(&self, id: window::Id) -> &str {
self.core().title.get(&id).map(|s| s.as_str()).unwrap_or("")
self.core().title.get(&id).map_or("", |s| s.as_str())
}
#[cfg(not(any(feature = "multi-window", feature = "wayland")))]

View file

@ -153,11 +153,11 @@ impl Context {
Container::<Message, Renderer>::new(Container::<Message, Renderer>::new(content).style(
theme::Container::custom(|theme| {
let cosmic = theme.cosmic();
let corners = cosmic.corner_radii.clone();
Appearance {
text_color: Some(cosmic.background.on.into()),
background: Some(Color::from(cosmic.background.base).into()),
border_radius: cosmic.corner_radii.radius_m.into(),
border_radius: corners.radius_m.into(),
border_width: 1.0,
border_color: cosmic.background.divider.into(),
icon_color: Some(cosmic.background.on.into()),

View file

@ -16,6 +16,9 @@ use iced_futures::Subscription;
use std::cell::RefCell;
use std::sync::Arc;
#[cfg(feature = "dbus-config")]
use cosmic_config::dbus;
pub type CosmicColor = ::palette::rgb::Srgba;
pub type CosmicComponent = cosmic_theme::Component;
pub type CosmicTheme = cosmic_theme::Theme;
@ -68,9 +71,12 @@ pub fn is_high_contrast() -> bool {
}
/// Watches for changes to the system's theme preference.
pub fn subscription(id: u64, is_dark: bool) -> Subscription<crate::theme::Theme> {
pub fn subscription(is_dark: bool) -> Subscription<crate::theme::Theme> {
config_subscription::<_, crate::cosmic_theme::Theme>(
(id, is_dark),
(
std::any::TypeId::of::<crate::cosmic_theme::Theme>(),
is_dark,
),
if is_dark {
cosmic_theme::DARK_THEME_ID
} else {