Skip to content

Commit

Permalink
x11/wayland: use xdg desktop portal settings interface to get dark mode
Browse files Browse the repository at this point in the history
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: #2258
refs: #1742
  • Loading branch information
wez committed Jul 13, 2022
1 parent fede46f commit 8dcfbc6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions wezterm-toast-notification/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 4 additions & 0 deletions window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions window/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down
15 changes: 15 additions & 0 deletions window/src/os/x_and_wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>() {
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")]
Expand Down
33 changes: 33 additions & 0 deletions window/src/os/xdg_desktop_portal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![cfg(all(unix, not(target_os = "macos")))]

//! <https://github.com/flatpak/xdg-desktop-portal/blob/main/data/org.freedesktop.portal.Settings.xml>
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<HashMap<String, HashMap<String, OwnedValue>>>;

fn Read(&self, namespace: &str, key: &str) -> zbus::Result<OwnedValue>;

#[dbus_proxy(signal)]
fn SettingChanged(&self, namespace: &str, key: &str, value: OwnedValue) -> Result<()>;
}

pub async fn read_setting(namespace: &str, key: &str) -> anyhow::Result<OwnedValue> {
let connection = zbus::ConnectionBuilder::session()?.build().await?;
let proxy = PortalSettingsProxy::new(&connection)
.await
.context("make proxy")?;
proxy.Read(namespace, key).await.context("Read")
}

0 comments on commit 8dcfbc6

Please sign in to comment.