use std::sync::mpsc;
use super::ThemeChangeEvent;
#[allow(dead_code)] pub(crate) fn watch_gnome(
callback: impl Fn(ThemeChangeEvent) + Send + 'static,
) -> crate::Result<super::ThemeSubscription> {
let (shutdown_tx, shutdown_rx) = mpsc::channel::<()>();
let (conn_tx, conn_rx) = mpsc::channel::<ashpd::zbus::blocking::Connection>();
let thread = std::thread::spawn(move || {
let conn = match ashpd::zbus::blocking::Connection::session() {
Ok(c) => c,
Err(_) => return,
};
if conn_tx.send(conn.clone()).is_err() {
return;
}
let proxy = match ashpd::zbus::blocking::Proxy::new(
&conn,
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.Settings",
) {
Ok(p) => p,
Err(_) => return,
};
let signals = match proxy
.receive_signal_with_args("SettingChanged", &[(0, "org.freedesktop.appearance")])
{
Ok(s) => s,
Err(_) => return,
};
for signal in signals {
match shutdown_rx.try_recv() {
Ok(()) | Err(mpsc::TryRecvError::Disconnected) => break,
Err(mpsc::TryRecvError::Empty) => {}
}
let _ = signal;
callback(ThemeChangeEvent::Changed);
}
});
let conn = conn_rx.recv().map_err(|_| crate::Error::ReaderFailed {
reader: "gnome_watcher",
source: "GNOME watcher thread could not connect to the session bus".into(),
})?;
let platform_shutdown = Box::new(move || {
let _ = conn.close();
});
Ok(super::ThemeSubscription::new(
shutdown_tx,
thread,
Some(platform_shutdown),
))
}
#[cfg(test)]
mod tests {
use std::sync::mpsc;
use std::time::Duration;
#[test]
fn dropping_a_gnome_subscription_returns_without_a_signal() {
let subscription = match super::watch_gnome(|_| {}) {
Ok(subscription) => subscription,
Err(_) => return, };
let (done_tx, done_rx) = mpsc::channel::<()>();
std::thread::spawn(move || {
drop(subscription);
let _ = done_tx.send(());
});
assert!(
done_rx.recv_timeout(Duration::from_secs(5)).is_ok(),
"dropping the GNOME subscription blocked for 5 s: the watcher thread is still \
inside the blocking signal iterator"
);
}
}