Skip to content

Commit

Permalink
backend: Add a signal emitter
Browse files Browse the repository at this point in the history
A mechanism to allow emitting signals without exposing DBus internals.
Portals implementations should store the emitter in set_signal_emitter
and can then be used to emit the signals supported by the portal.
  • Loading branch information
bilelmoussaoui committed Oct 27, 2024
1 parent 72446a6 commit 2bbd644
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
13 changes: 11 additions & 2 deletions backend-demo/src/settings.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Arc<dyn SettingsSignalEmitter>>,
}

#[async_trait]
Expand Down Expand Up @@ -45,4 +50,8 @@ impl SettingsImpl for Settings {
)))
}
}

fn set_signal_emitter(&mut self, signal_emitter: Arc<dyn SettingsSignalEmitter>) {
self.signal_emitter.replace(signal_emitter);
}
}
15 changes: 15 additions & 0 deletions src/backend/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<AppID, AppState>, PortalError>;
Expand All @@ -61,6 +66,9 @@ pub trait BackgroundImpl: RequestImpl {
commandline: Vec<String>,
flags: BitFlags<AutoStartFlags>,
) -> Result<bool, PortalError>;

// Set the signal emitter, allowing to notify of changes.
fn set_signal_emitter(&mut self, signal_emitter: Arc<dyn BackgroundSignalEmitter>);
}

pub(crate) struct BackgroundInterface {
Expand All @@ -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")]
Expand Down
30 changes: 30 additions & 0 deletions src/backend/permission_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppID, Vec<Permission>>,
) -> zbus::Result<()>;
}

#[async_trait]
pub trait PermissionStoreImpl: Send + Sync {
async fn lookup(
Expand Down Expand Up @@ -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<dyn PermissionStoreEmitter>);
}

pub(crate) struct PermissionStoreInterface {
Expand Down Expand Up @@ -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<AppID, Vec<Permission>>,
) -> 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")]
Expand Down
30 changes: 30 additions & 0 deletions src/backend/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -23,6 +31,9 @@ pub trait SettingsImpl: Send + Sync {
) -> Result<HashMap<String, Namespace>, PortalError>;

async fn read(&self, namespace: &str, key: &str) -> Result<OwnedValue, PortalError>;

// Set the signal emitter, allowing to notify of changes.
fn set_signal_emitter(&mut self, signal_emitter: Arc<dyn SettingsSignalEmitter>);
}

pub(crate) struct SettingsInterface {
Expand Down Expand Up @@ -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")]
Expand Down

0 comments on commit 2bbd644

Please sign in to comment.