diff --git a/backend-demo/src/settings.rs b/backend-demo/src/settings.rs index cab987f9d..757474f03 100644 --- a/backend-demo/src/settings.rs +++ b/backend-demo/src/settings.rs @@ -1,7 +1,10 @@ -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use ashpd::{ - backend::{request::RequestImpl, settings::SettingsImpl}, + backend::{ + request::RequestImpl, + settings::{SettingsImpl, SettingsSignalEmitter}, + }, desktop::settings::{ColorScheme, Namespace, APPEARANCE_NAMESPACE, COLOR_SCHEME_KEY}, zbus::zvariant::OwnedValue, PortalError, @@ -11,6 +14,8 @@ use async_trait::async_trait; #[derive(Default, Clone)] pub struct Settings { color_scheme: ColorScheme, + // The signal emitter allows dispatching changed events without exposing DBus internals + signal_emitter: Option>, } #[async_trait] @@ -45,4 +50,8 @@ impl SettingsImpl for Settings { ))) } } + + fn set_signal_emitter(&mut self, signal_emitter: Arc) { + self.signal_emitter.replace(signal_emitter); + } } diff --git a/src/backend/background.rs b/src/backend/background.rs index ded2f64ae..d371395ff 100644 --- a/src/backend/background.rs +++ b/src/backend/background.rs @@ -47,6 +47,11 @@ pub enum AutoStartFlags { DBusActivation = 1, } +#[async_trait] +pub trait BackgroundSignalEmitter: Send + Sync { + async fn emit_changed(&self) -> zbus::Result<()>; +} + #[async_trait] pub trait BackgroundImpl: RequestImpl { async fn get_app_state(&self) -> Result, PortalError>; @@ -61,6 +66,9 @@ pub trait BackgroundImpl: RequestImpl { commandline: Vec, flags: BitFlags, ) -> Result; + + // Set the signal emitter, allowing to notify of changes. + fn set_signal_emitter(&mut self, signal_emitter: Arc); } pub(crate) struct BackgroundInterface { @@ -82,6 +90,13 @@ impl BackgroundInterface { } } +#[async_trait] +impl BackgroundSignalEmitter for BackgroundInterface { + async fn emit_changed(&self) -> zbus::Result<()> { + self.changed().await + } +} + #[zbus::interface(name = "org.freedesktop.impl.portal.Background")] impl BackgroundInterface { #[zbus(property(emits_changed_signal = "const"), name = "version")] diff --git a/src/backend/permission_store.rs b/src/backend/permission_store.rs index c2be94991..bde596f84 100644 --- a/src/backend/permission_store.rs +++ b/src/backend/permission_store.rs @@ -9,6 +9,18 @@ use crate::{ AppID, PortalError, }; +#[async_trait] +pub trait PermissionStoreEmitter: Send + Sync { + async fn emit_document_changed( + &self, + table: &str, + id: DocumentID, + deleted: bool, + data: Value<'_>, + permissions: HashMap>, + ) -> zbus::Result<()>; +} + #[async_trait] pub trait PermissionStoreImpl: Send + Sync { async fn lookup( @@ -60,6 +72,9 @@ pub trait PermissionStoreImpl: Send + Sync { id: DocumentID, app: AppID, ) -> Result<(), PortalError>; + + // Set the signal emitter, allowing to notify of changes. + fn set_signal_emitter(&mut self, signal_emitter: Arc); } pub(crate) struct PermissionStoreInterface { @@ -97,6 +112,21 @@ impl PermissionStoreInterface { } } +#[async_trait] +impl PermissionStoreEmitter for PermissionStoreInterface { + async fn emit_document_changed( + &self, + table: &str, + id: DocumentID, + deleted: bool, + data: Value<'_>, + permissions: HashMap>, + ) -> zbus::Result<()> { + self.document_changed(table, id, deleted, data, permissions) + .await + } +} + #[zbus::interface(name = "org.freedesktop.impl.portal.PermissionStore")] impl PermissionStoreInterface { #[zbus(property(emits_changed_signal = "const"), name = "version")] diff --git a/src/backend/settings.rs b/src/backend/settings.rs index d877dc9b7..7faf3a6eb 100644 --- a/src/backend/settings.rs +++ b/src/backend/settings.rs @@ -15,6 +15,14 @@ use crate::{ PortalError, }; +#[async_trait] +pub trait SettingsSignalEmitter: Send + Sync { + async fn emit_changed(&self, namespace: &str, key: &str, value: Value<'_>) -> zbus::Result<()>; + async fn emit_contrast_changed(&self, contrast: Contrast) -> zbus::Result<()>; + async fn emit_accent_color_changed(&self, color: Color) -> zbus::Result<()>; + async fn emit_color_scheme_changed(&self, scheme: ColorScheme) -> zbus::Result<()>; +} + #[async_trait] pub trait SettingsImpl: Send + Sync { async fn read_all( @@ -23,6 +31,9 @@ pub trait SettingsImpl: Send + Sync { ) -> Result, PortalError>; async fn read(&self, namespace: &str, key: &str) -> Result; + + // Set the signal emitter, allowing to notify of changes. + fn set_signal_emitter(&mut self, signal_emitter: Arc); } pub(crate) struct SettingsInterface { @@ -71,6 +82,25 @@ impl SettingsInterface { } } +#[async_trait] +impl SettingsSignalEmitter for SettingsInterface { + async fn emit_changed(&self, namespace: &str, key: &str, value: Value<'_>) -> zbus::Result<()> { + self.changed(namespace, key, value).await + } + + async fn emit_contrast_changed(&self, contrast: Contrast) -> zbus::Result<()> { + self.contrast_changed(contrast).await + } + + async fn emit_accent_color_changed(&self, color: Color) -> zbus::Result<()> { + self.accent_color_changed(color).await + } + + async fn emit_color_scheme_changed(&self, scheme: ColorScheme) -> zbus::Result<()> { + self.color_scheme_changed(scheme).await + } +} + #[zbus::interface(name = "org.freedesktop.impl.portal.Settings")] impl SettingsInterface { #[zbus(property(emits_changed_signal = "const"), name = "version")]