| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | use std::rc::Weak; |
| 5 | |
| 6 | use i_slint_core::items::ColorScheme; |
| 7 | |
| 8 | use crate::WinitWindowAdapter; |
| 9 | |
| 10 | fn xdg_color_scheme_to_slint(value: zbus::zvariant::OwnedValue) -> ColorScheme { |
| 11 | match value.downcast_ref::<u32>() { |
| 12 | Ok(1) => ColorScheme::Dark, |
| 13 | Ok(2) => ColorScheme::Light, |
| 14 | _ => ColorScheme::Unknown, |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | pub async fn watch(window_weak: Weak<WinitWindowAdapter>) -> zbus::Result<()> { |
| 19 | let connection = zbus::Connection::session().await?; |
| 20 | let settings_proxy: zbus::Proxy = zbus::proxy::Builder::new(&connection) |
| 21 | .interface("org.freedesktop.portal.Settings" )? |
| 22 | .path("/org/freedesktop/portal/desktop" )? |
| 23 | .destination("org.freedesktop.portal.Desktop" )? |
| 24 | .build() |
| 25 | .await?; |
| 26 | |
| 27 | let initial_value: zbus::zvariant::OwnedValue = |
| 28 | settings_proxy.call("ReadOne" , &("org.freedesktop.appearance" , "color-scheme" )).await?; |
| 29 | |
| 30 | if let Some(window) = window_weak.upgrade() { |
| 31 | window.set_color_scheme(xdg_color_scheme_to_slint(initial_value)); |
| 32 | } |
| 33 | |
| 34 | use futures::stream::StreamExt; |
| 35 | |
| 36 | let mut color_scheme_stream = settings_proxy |
| 37 | .receive_signal_with_args( |
| 38 | "SettingChanged" , |
| 39 | &[(0, "org.freedesktop.appearance" ), (1, "color-scheme" )], |
| 40 | ) |
| 41 | .await? |
| 42 | .map(|message| { |
| 43 | let (_, _, scheme): (String, String, zbus::zvariant::OwnedValue) = |
| 44 | message.body().deserialize().ok()?; |
| 45 | Some(scheme) |
| 46 | }); |
| 47 | |
| 48 | while let Some(Some(new_scheme)) = color_scheme_stream.next().await { |
| 49 | if let Some(window) = window_weak.upgrade() { |
| 50 | window.set_color_scheme(xdg_color_scheme_to_slint(new_scheme)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | Ok(()) |
| 55 | } |
| 56 | |