| 1 |
use std::{collections::HashMap, sync::Arc}; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
PortalError, |
| 7 |
desktop::{ |
| 8 |
Color, |
| 9 |
settings::{ |
| 10 |
ACCENT_COLOR_SCHEME_KEY, APPEARANCE_NAMESPACE, COLOR_SCHEME_KEY, CONTRAST_KEY, |
| 11 |
ColorScheme, Contrast, Namespace, |
| 12 |
}, |
| 13 |
}, |
| 14 |
zbus::object_server::SignalEmitter, |
| 15 |
zvariant::{OwnedValue, Value}, |
| 16 |
}; |
| 17 |
|
| 18 |
#[async_trait] |
| 19 |
pub trait SettingsSignalEmitter: Send + Sync { |
| 20 |
#[doc(alias = "SettingChanged")] |
| 21 |
async fn emit_changed(&self, namespace: &str, key: &str, value: Value<'_>) -> zbus::Result<()>; |
| 22 |
async fn emit_contrast_changed(&self, contrast: Contrast) -> zbus::Result<()>; |
| 23 |
async fn emit_accent_color_changed(&self, color: Color) -> zbus::Result<()>; |
| 24 |
async fn emit_color_scheme_changed(&self, scheme: ColorScheme) -> zbus::Result<()>; |
| 25 |
} |
| 26 |
|
| 27 |
#[async_trait] |
| 28 |
pub trait SettingsImpl: Send + Sync { |
| 29 |
#[doc(alias = "ReadAll")] |
| 30 |
async fn read_all( |
| 31 |
&self, |
| 32 |
namespaces: Vec<String>, |
| 33 |
) -> Result<HashMap<String, Namespace>, PortalError>; |
| 34 |
|
| 35 |
#[doc(alias = "Read")] |
| 36 |
async fn read(&self, namespace: &str, key: &str) -> Result<OwnedValue, PortalError>; |
| 37 |
|
| 38 |
// Set the signal emitter, allowing to notify of changes. |
| 39 |
fn set_signal_emitter(&mut self, signal_emitter: Arc<dyn SettingsSignalEmitter>); |
| 40 |
} |
| 41 |
|
| 42 |
pub(crate) struct SettingsInterface { |
| 43 |
imp: Arc<dyn SettingsImpl>, |
| 44 |
cnx: zbus::Connection, |
| 45 |
#[allow(dead_code)] |
| 46 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 47 |
} |
| 48 |
|
| 49 |
impl SettingsInterface { |
| 50 |
pub fn new( |
| 51 |
imp: Arc<dyn SettingsImpl>, |
| 52 |
cnx: zbus::Connection, |
| 53 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 54 |
) -> Self { |
| 55 |
Self { imp, cnx, spawn } |
| 56 |
} |
| 57 |
|
| 58 |
pub async fn changed(&self, namespace: &str, key: &str, value: Value<'_>) -> zbus::Result<()> { |
| 59 |
let object_server = self.cnx.object_server(); |
| 60 |
let iface_ref = object_server |
| 61 |
.interface::<_, Self>(crate::proxy::DESKTOP_PATH) |
| 62 |
.await?; |
| 63 |
Self::setting_changed(iface_ref.signal_emitter(), namespace, key, value).await |
| 64 |
} |
| 65 |
|
| 66 |
pub async fn contrast_changed(&self, contrast: Contrast) -> zbus::Result<()> { |
| 67 |
self.changed( |
| 68 |
APPEARANCE_NAMESPACE, |
| 69 |
CONTRAST_KEY, |
| 70 |
OwnedValue::from(contrast).into(), |
| 71 |
) |
| 72 |
.await |
| 73 |
} |
| 74 |
|
| 75 |
pub async fn accent_color_changed(&self, color: Color) -> zbus::Result<()> { |
| 76 |
self.changed( |
| 77 |
APPEARANCE_NAMESPACE, |
| 78 |
ACCENT_COLOR_SCHEME_KEY, |
| 79 |
OwnedValue::try_from(color).unwrap().into(), |
| 80 |
) |
| 81 |
.await |
| 82 |
} |
| 83 |
|
| 84 |
pub async fn color_scheme_changed(&self, scheme: ColorScheme) -> zbus::Result<()> { |
| 85 |
self.changed( |
| 86 |
APPEARANCE_NAMESPACE, |
| 87 |
COLOR_SCHEME_KEY, |
| 88 |
OwnedValue::from(scheme).into(), |
| 89 |
) |
| 90 |
.await |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[async_trait] |
| 95 |
impl SettingsSignalEmitter for SettingsInterface { |
| 96 |
async fn emit_changed(&self, namespace: &str, key: &str, value: Value<'_>) -> zbus::Result<()> { |
| 97 |
self.changed(namespace, key, value).await |
| 98 |
} |
| 99 |
|
| 100 |
async fn emit_contrast_changed(&self, contrast: Contrast) -> zbus::Result<()> { |
| 101 |
self.contrast_changed(contrast).await |
| 102 |
} |
| 103 |
|
| 104 |
async fn emit_accent_color_changed(&self, color: Color) -> zbus::Result<()> { |
| 105 |
self.accent_color_changed(color).await |
| 106 |
} |
| 107 |
|
| 108 |
async fn emit_color_scheme_changed(&self, scheme: ColorScheme) -> zbus::Result<()> { |
| 109 |
self.color_scheme_changed(scheme).await |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#[zbus::interface(name = "org.freedesktop.impl.portal.Settings")] |
| 114 |
impl SettingsInterface { |
| 115 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 116 |
fn version(&self) -> u32 { |
| 117 |
2 |
| 118 |
} |
| 119 |
|
| 120 |
#[zbus(out_args("value"))] |
| 121 |
async fn read_all( |
| 122 |
&self, |
| 123 |
namespaces: Vec<String>, |
| 124 |
) -> Result<HashMap<String, Namespace>, PortalError> { |
| 125 |
#[cfg(feature = "tracing")] |
| 126 |
tracing::debug!("Settings::ReadAll"); |
| 127 |
|
| 128 |
let response = self.imp.read_all(namespaces).await; |
| 129 |
|
| 130 |
#[cfg(feature = "tracing")] |
| 131 |
tracing::debug!("Settings::ReadAll returned {:#?}", response); |
| 132 |
response |
| 133 |
} |
| 134 |
|
| 135 |
#[zbus(out_args("value"))] |
| 136 |
async fn read(&self, namespace: &str, key: &str) -> Result<OwnedValue, PortalError> { |
| 137 |
#[cfg(feature = "tracing")] |
| 138 |
tracing::debug!("Settings::Read"); |
| 139 |
|
| 140 |
let response = self.imp.read(namespace, key).await; |
| 141 |
|
| 142 |
#[cfg(feature = "tracing")] |
| 143 |
tracing::debug!("Settings::Read returned {:#?}", response); |
| 144 |
response |
| 145 |
} |
| 146 |
|
| 147 |
#[zbus(signal)] |
| 148 |
async fn setting_changed( |
| 149 |
signal_ctxt: &SignalEmitter<'_>, |
| 150 |
namespace: &str, |
| 151 |
key: &str, |
| 152 |
value: Value<'_>, |
| 153 |
) -> zbus::Result<()>; |
| 154 |
} |