From 8dcfbc6718ff193f3465bfe73b8a46d388d87b95 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 13 Jul 2022 08:54:24 -0700 Subject: [PATCH] x11/wayland: use xdg desktop portal settings interface to get dark mode There are some other settings in there that could also help with things like the cursor theme on Wayland. Note that we don't currently subscribe to the settings changed signal: that can be done in a follow up. refs: https://github.com/wez/wezterm/issues/2258 refs: https://github.com/wez/wezterm/issues/1742 --- Cargo.lock | 3 +++ docs/changelog.md | 1 + wezterm-toast-notification/Cargo.toml | 4 ++-- window/Cargo.toml | 4 ++++ window/src/os/mod.rs | 1 + window/src/os/x_and_wayland.rs | 15 ++++++++++++ window/src/os/xdg_desktop_portal.rs | 33 +++++++++++++++++++++++++++ 7 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 window/src/os/xdg_desktop_portal.rs diff --git a/Cargo.lock b/Cargo.lock index 5b14cc6c6ba..8628fc875d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5435,6 +5435,7 @@ dependencies = [ "dirs-next", "euclid", "filedescriptor", + "futures-util", "gl_generator", "glium", "guillotiere", @@ -5470,6 +5471,8 @@ dependencies = [ "xcb", "xcb-imdkit", "xkbcommon", + "zbus", + "zvariant", ] [[package]] diff --git a/docs/changelog.md b/docs/changelog.md index 47c19eff54e..9eed5654cdc 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -27,6 +27,7 @@ As features stabilize some brief notes about them will accumulate here. * New [wezterm.json_parse()](config/lua/wezterm/json_parse.md) and [wezterm.json_encode()](config/lua/wezterm/json_encode.md) functions for working with JSON. * Hundreds of new color schemes have been imported from [base16](https://github.com/chriskempson/base16-schemes-source), [Gogh](https://gogh-co.github.io/Gogh/) and [terminal.sexy](https://terminal.sexy/). [Browse the schemes](colorschemes/index.md) and look for themes with `(base16)`, `(Gogh)` and `(terminal.sexy)` in the name to discover them! * [pane:is_alt_screen_active()](config/lua/pane/is_alt_screen_active.md) for testing whether the alt screen is active. Thanks to [@Funami580](https://github.com/Funami580)! [#2234](https://github.com/wez/wezterm/issues/2234) +* X11/Wayland: XDG desktop portal is now used to determine whether dark mode is in use [#2258](https://github.com/wez/wezterm/issues/2258) #### Fixed * [ActivateKeyTable](config/lua/keyassignment/ActivateKeyTable.md)'s `replace_current` field was not actually optional. Made it optional. [#2179](https://github.com/wez/wezterm/issues/2179) diff --git a/wezterm-toast-notification/Cargo.toml b/wezterm-toast-notification/Cargo.toml index 613074cfaab..ccaef9d4481 100644 --- a/wezterm-toast-notification/Cargo.toml +++ b/wezterm-toast-notification/Cargo.toml @@ -13,8 +13,8 @@ log = "0.4" [target.'cfg(all(not(windows), not(target_os="macos")))'.dependencies] serde = {version="1.0", features = ["derive"]} -zbus = "2.0.0" -zvariant = "3.0" +zbus = "2.3.0" +zvariant = "3.4" async-std = "1.4" futures-util = "0.3" diff --git a/window/Cargo.toml b/window/Cargo.toml index 09d63925b95..58f1a36ed7c 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -79,6 +79,10 @@ wayland-protocols = {version="0.29", optional=true} wayland-client = {version="0.29", optional=true} wayland-egl = {version="0.29", optional=true} xcb-imdkit = { version="0.2", git="https://github.com/wez/xcb-imdkit-rs.git", rev="ede7c71b85fe2537efef6cf999a45690316211cf"} +serde = {version="1.0", features = ["derive"]} +zbus = "2.3.0" +zvariant = "3.4" +futures-util = "0.3" [target.'cfg(target_os="macos")'.dependencies] cocoa = "0.20" diff --git a/window/src/os/mod.rs b/window/src/os/mod.rs index 26b132349bf..21c6b01835b 100644 --- a/window/src/os/mod.rs +++ b/window/src/os/mod.rs @@ -7,6 +7,7 @@ pub use self::windows::*; pub mod wayland; pub mod x11; pub mod x_and_wayland; +pub mod xdg_desktop_portal; pub mod xkeysyms; #[cfg(all(unix, not(target_os = "macos")))] diff --git a/window/src/os/x_and_wayland.rs b/window/src/os/x_and_wayland.rs index d61a84334ff..142d9f3f2f1 100644 --- a/window/src/os/x_and_wayland.rs +++ b/window/src/os/x_and_wayland.rs @@ -132,6 +132,21 @@ impl ConnectionOps for Connection { } fn get_appearance(&self) -> Appearance { + match promise::spawn::block_on(crate::os::xdg_desktop_portal::read_setting( + "org.freedesktop.appearance", + "color-scheme", + )) { + Ok(value) => match value.downcast_ref::() { + Some(1) => return Appearance::Dark, + Some(_) => return Appearance::Light, + None => { + log::debug!("Unable to resolve appearance using xdg-desktop-portal: expected a u32 value but got {value:#?}"); + } + }, + Err(err) => { + log::debug!("Unable to resolve appearance using xdg-desktop-portal: {err:#}"); + } + } match self { Self::X11(x) => x.get_appearance(), #[cfg(feature = "wayland")] diff --git a/window/src/os/xdg_desktop_portal.rs b/window/src/os/xdg_desktop_portal.rs new file mode 100644 index 00000000000..1d459b184b2 --- /dev/null +++ b/window/src/os/xdg_desktop_portal.rs @@ -0,0 +1,33 @@ +#![cfg(all(unix, not(target_os = "macos")))] + +//! + +use anyhow::Context; +use std::collections::HashMap; +use zbus::dbus_proxy; +use zvariant::OwnedValue; + +#[dbus_proxy( + interface = "org.freedesktop.portal.Settings", + default_service = "org.freedesktop.portal.Desktop", + default_path = "/org/freedesktop/portal/desktop" +)] +trait PortalSettings { + fn ReadAll( + &self, + namespaces: &[&str], + ) -> zbus::Result>>; + + fn Read(&self, namespace: &str, key: &str) -> zbus::Result; + + #[dbus_proxy(signal)] + fn SettingChanged(&self, namespace: &str, key: &str, value: OwnedValue) -> Result<()>; +} + +pub async fn read_setting(namespace: &str, key: &str) -> anyhow::Result { + let connection = zbus::ConnectionBuilder::session()?.build().await?; + let proxy = PortalSettingsProxy::new(&connection) + .await + .context("make proxy")?; + proxy.Read(namespace, key).await.context("Read") +}