|
| 1 | +//! Background monitor for system theme changes via XDG Desktop Portal. |
| 2 | +//! |
| 3 | +//! This module sets up a listener for the SettingsChanged signal from the |
| 4 | +//! XDG Desktop Portal, specifically monitoring for color-scheme preference changes. |
| 5 | +
|
| 6 | +use crate::window::Theme; |
| 7 | +use std::sync::atomic::{AtomicU8, Ordering}; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +// Cache for the current theme (0 = None, 1 = Dark, 2 = Light) |
| 11 | +static CACHED_THEME: AtomicU8 = AtomicU8::new(0); |
| 12 | + |
| 13 | +/// Get the cached theme without blocking |
| 14 | +pub fn get_cached_theme() -> Option<Theme> { |
| 15 | + match CACHED_THEME.load(Ordering::Relaxed) { |
| 16 | + 1 => Some(Theme::Dark), |
| 17 | + 2 => Some(Theme::Light), |
| 18 | + _ => None, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +pub(crate) fn set_cached_theme(theme: Option<Theme>) { |
| 23 | + let value = match theme { |
| 24 | + Some(Theme::Dark) => 1, |
| 25 | + Some(Theme::Light) => 2, |
| 26 | + None => 0, |
| 27 | + }; |
| 28 | + CACHED_THEME.store(value, Ordering::Relaxed); |
| 29 | +} |
| 30 | + |
| 31 | +/// Starts monitoring for system theme changes in a background thread. |
| 32 | +/// |
| 33 | +/// When the system color scheme preference changes (dark/light), the provided |
| 34 | +/// callback will be invoked. This allows applications to respond to theme |
| 35 | +/// changes in real-time without needing to restart. |
| 36 | +/// |
| 37 | +/// # Arguments |
| 38 | +/// |
| 39 | +/// * `on_change` - Callback function to invoke when theme changes are detected |
| 40 | +/// |
| 41 | +/// # Returns |
| 42 | +/// |
| 43 | +/// Returns `Ok(())` if the monitor was successfully started, or `Err` if |
| 44 | +/// the XDG Desktop Portal is not available or there was an error setting up |
| 45 | +/// the signal listener. |
| 46 | +pub fn start_theme_monitor<F>(on_change: F) -> Result<(), Box<dyn std::error::Error>> |
| 47 | +where |
| 48 | + F: Fn() + Send + Sync + 'static, |
| 49 | +{ |
| 50 | + let callback = Arc::new(on_change); |
| 51 | + |
| 52 | + std::thread::spawn(move || { |
| 53 | + // Create a new tokio runtime for this thread |
| 54 | + let rt = match tokio::runtime::Runtime::new() { |
| 55 | + Ok(rt) => rt, |
| 56 | + Err(_) => return, |
| 57 | + }; |
| 58 | + |
| 59 | + rt.block_on(async { |
| 60 | + let _ = monitor_theme_changes(callback).await; |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + Ok(()) |
| 65 | +} |
| 66 | + |
| 67 | +async fn monitor_theme_changes<F>( |
| 68 | + on_change: Arc<F>, |
| 69 | +) -> Result<(), Box<dyn std::error::Error>> |
| 70 | +where |
| 71 | + F: Fn() + Send + Sync + 'static, |
| 72 | +{ |
| 73 | + use ashpd::zbus::fdo::DBusProxy; |
| 74 | + use ashpd::zbus::{Connection, MatchRule, MessageStream}; |
| 75 | + use futures_util::stream::StreamExt; |
| 76 | + |
| 77 | + // Connect to session bus |
| 78 | + let connection = Connection::session().await?; |
| 79 | + |
| 80 | + // Create match rule for Settings.SettingChanged signal |
| 81 | + let match_rule = MatchRule::builder() |
| 82 | + .msg_type(ashpd::zbus::message::Type::Signal) |
| 83 | + .interface("org.freedesktop.portal.Settings")? |
| 84 | + .member("SettingChanged")? |
| 85 | + .build(); |
| 86 | + |
| 87 | + let dbus_proxy = DBusProxy::new(&connection).await?; |
| 88 | + dbus_proxy.add_match_rule(match_rule.clone()).await?; |
| 89 | + |
| 90 | + // Create message stream |
| 91 | + let mut stream = |
| 92 | + MessageStream::for_match_rule(match_rule, &connection, Some(100)).await?; |
| 93 | + |
| 94 | + // Process signals as they arrive |
| 95 | + while let Some(msg) = stream.next().await { |
| 96 | + let msg = msg?; |
| 97 | + |
| 98 | + // Try to parse the signal arguments |
| 99 | + if let Ok((namespace, key, value)) = |
| 100 | + msg.body() |
| 101 | + .deserialize::<(String, String, ashpd::zbus::zvariant::Value)>() |
| 102 | + { |
| 103 | + if namespace == "org.freedesktop.appearance" && key == "color-scheme" { |
| 104 | + // Extract the theme value (uint32: 0=no pref, 1=dark, 2=light) |
| 105 | + if let Ok(variant) = value.downcast::<ashpd::zbus::zvariant::Value>() { |
| 106 | + if let Ok(scheme_value) = variant.downcast::<u32>() { |
| 107 | + let theme = match scheme_value { |
| 108 | + 1 => Some(Theme::Dark), |
| 109 | + 2 => Some(Theme::Light), |
| 110 | + _ => None, |
| 111 | + }; |
| 112 | + set_cached_theme(theme); |
| 113 | + // Invoke the callback to notify the application |
| 114 | + on_change(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Ok(()) |
| 122 | +} |
0 commit comments