Add proxy for the Settings portal (dark mode, reduced motion, etc.)

This commit is contained in:
Val Packett 2025-12-12 04:37:46 -03:00
parent 1d931ae191
commit df832a5141
3 changed files with 92 additions and 5 deletions

View file

@ -149,10 +149,12 @@ async fn main() -> eyre::Result<()> {
cli.guest_mountpoint,
)
.await?;
let settings_imp = portal::settings::Settings::new(&host_session_conn).await?;
async fn on_vm_bus_connected(
vm_bus_conn: zbus::Connection,
file_chooser: portal::file_chooser::FileChooser,
settings: portal::settings::Settings,
) -> Result<(), eyre::Report> {
vm_bus_conn
.request_name("org.freedesktop.portal.Desktop")
@ -164,20 +166,38 @@ async fn main() -> eyre::Result<()> {
{
error!("org.freedesktop.portal.Desktop already provided");
};
if !vm_bus_conn
.object_server()
.at("/org/freedesktop/portal/desktop", settings)
.await?
{
error!("org.freedesktop.portal.Desktop already provided");
};
let settings_ref = vm_bus_conn
.object_server()
.interface::<_, portal::settings::Settings>("/org/freedesktop/portal/desktop")
.await?;
tokio::spawn(async move {
let settings = settings_ref.get().await;
let emitter = settings_ref.signal_emitter();
if let Err(err) = settings.forward_changes(emitter).await {
error!(%err, "forwarding settings changes ended");
}
});
// XXX: no method for "wait until the conn dies"?
Ok(std::future::pending::<()>().await)
}
if let Some(path) = cli.unix_path {
let vm_unix_listener = UnixListener::bind(path)?;
server_tasks.spawn(enclose!((file_chooser_imp) async move {
server_tasks.spawn(enclose!((file_chooser_imp, settings_imp) async move {
while let Ok((socket, remote_addr)) = vm_unix_listener.accept().await {
let f = enclose!((file_chooser_imp) async move {
let f = enclose!((file_chooser_imp, settings_imp) async move {
let client_conn = zbus::connection::Builder::unix_stream(socket)
.auth_mechanism(zbus::AuthMechanism::Anonymous)
.build()
.await?;
on_vm_bus_connected(client_conn, file_chooser_imp).await
on_vm_bus_connected(client_conn, file_chooser_imp, settings_imp).await
});
tokio::spawn(
async {
@ -198,10 +218,10 @@ async fn main() -> eyre::Result<()> {
vsock::ListenerBuilder::new(vsock::VsockAddr::new(vsock::VMADDR_CID_HOST, port))
.with_label("VM Bus")
.listen(move |client| {
enclose!((file_chooser_imp) async move {
enclose!((file_chooser_imp, settings_imp) async move {
// TODO: Not necessary to go through the channel, add vsock support to the Peer too?
let client_conn = client.build().await?;
on_vm_bus_connected(client_conn, file_chooser_imp).await
on_vm_bus_connected(client_conn, file_chooser_imp, settings_imp).await
})
})
.map_ok_or_else(

View file

@ -1,3 +1,4 @@
pub mod documents;
pub mod file_chooser;
pub mod request;
pub mod settings;

View file

@ -0,0 +1,66 @@
use std::collections::HashMap;
use tokio_stream::StreamExt;
use tracing::warn;
use zbus::{Connection, fdo::Result, object_server::SignalEmitter, zvariant};
#[derive(Clone)]
pub struct Settings {
host: SettingsProxy<'static>,
}
#[zbus::interface(
name = "org.freedesktop.portal.Settings",
proxy(
default_service = "org.freedesktop.portal.Desktop",
default_path = "/org/freedesktop/portal/desktop"
)
)]
impl Settings {
async fn read(&self, namespace: &str, key: &str) -> Result<zvariant::OwnedValue> {
self.host.read(namespace, key).await
}
async fn read_all(
&self,
namespaces: Vec<&str>,
) -> Result<HashMap<String, HashMap<String, zvariant::OwnedValue>>> {
self.host.read_all(namespaces).await
}
async fn read_one(&self, namespace: &str, key: &str) -> Result<zvariant::OwnedValue> {
self.host.read_one(namespace, key).await
}
/// SettingChanged signal
#[zbus(signal)]
async fn setting_changed(
signal_emitter: &SignalEmitter<'_>,
namespace: &str,
key: &str,
value: zvariant::Value<'_>,
) -> zbus::Result<()>;
#[zbus(property, name = "version")]
async fn version(&self) -> Result<u32> {
Ok(2)
}
}
impl Settings {
pub(crate) async fn new(host_session_conn: &Connection) -> Result<Self> {
let host = SettingsProxy::builder(host_session_conn).build().await?;
Ok(Self { host })
}
pub async fn forward_changes(&self, signal_emitter: &SignalEmitter<'static>) -> Result<()> {
let mut stream = self.host.receive_setting_changed().await?;
while let Some(x) = stream.next().await {
let args = x.args()?;
Settings::setting_changed(signal_emitter, args.namespace, args.key, args.value).await?;
()
}
warn!("settings change stream end");
Ok(())
}
}