1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-22 21:01:36 +03:00

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
This commit is contained in:
Wez Furlong 2022-07-13 08:54:24 -07:00
parent fede46f6b7
commit 8dcfbc6718
7 changed files with 59 additions and 2 deletions

3
Cargo.lock generated
View File

@ -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]]

View File

@ -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)

View File

@ -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"

View File

@ -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"

View File

@ -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")))]

View File

@ -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")]

View File

@ -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")
}