From e91ee811cb74b9f0c0e32280e01a82e0f9655298 Mon Sep 17 00:00:00 2001 From: Alex Butler Date: Wed, 24 Aug 2022 21:43:36 +0100 Subject: [PATCH] Use sctk-adwaita 0.5.1 auto theme selection Co-authored-by: Kirill Chibisov --- CHANGELOG.md | 1 + src/platform/unix.rs | 4 ++ src/platform_impl/linux/wayland/window/mod.rs | 37 ++++++++++++++----- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 696a41cb..60ab4234 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ And please only add new entries to the top of this list, right below the `# Unre - **Breaking:** On macOS, add support for two-finger touchpad magnification and rotation gestures with new events `WindowEvent::TouchpadMagnify` and `WindowEvent::TouchpadRotate`. - On Wayland, `wayland-csd-adwaita` now uses `ab_glyph` instead of `crossfont` to render the title for decorations. - On Wayland, a new `wayland-csd-adwaita-crossfont` feature was added to use `crossfont` instead of `ab_glyph` for decorations. +- On Wayland, if not otherwise specified use upstream automatic CSD theme selection. # 0.27.2 (2022-8-12) diff --git a/src/platform/unix.rs b/src/platform/unix.rs index 1d33e846..99e82619 100644 --- a/src/platform/unix.rs +++ b/src/platform/unix.rs @@ -212,6 +212,8 @@ pub trait WindowExtUnix { /// /// You can also use `WINIT_WAYLAND_CSD_THEME` env variable to set the theme. /// Possible values for env variable are: "dark" and light" + /// + /// When unspecified a theme is automatically selected. #[cfg(feature = "wayland")] fn wayland_set_csd_theme(&self, config: Theme); @@ -349,6 +351,8 @@ pub trait WindowBuilderExtUnix { /// /// You can also use `WINIT_WAYLAND_CSD_THEME` env variable to set the theme. /// Possible values for env variable are: "dark" and light" + /// + /// When unspecified a theme is automatically selected. #[cfg(feature = "wayland")] fn with_wayland_csd_theme(self, theme: Theme) -> Self; diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index 43b13f61..db63781d 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -170,17 +170,14 @@ impl Window { ) .map_err(|_| os_error!(OsError::WaylandMisc("failed to create window.")))?; - // Set CSD frame config + // Set CSD frame config from theme if specified, + // otherwise use upstream automatic selection. #[cfg(feature = "sctk-adwaita")] - { - let theme = platform_attributes.csd_theme.unwrap_or_else(|| { - let env = std::env::var(WAYLAND_CSD_THEME_ENV_VAR).unwrap_or_default(); - match env.to_lowercase().as_str() { - "dark" => Theme::Dark, - _ => Theme::Light, - } - }); - + if let Some(theme) = platform_attributes.csd_theme.or_else(|| { + std::env::var(WAYLAND_CSD_THEME_ENV_VAR) + .ok() + .and_then(|s| s.as_str().try_into().ok()) + }) { window.set_frame_config(theme.into()); } @@ -636,3 +633,23 @@ impl From for sctk_adwaita::FrameConfig { } } } + +impl TryFrom<&str> for Theme { + type Error = (); + + /// ``` + /// use winit::window::Theme; + /// + /// assert_eq!("dark".try_into(), Ok(Theme::Dark)); + /// assert_eq!("lIghT".try_into(), Ok(Theme::Light)); + /// ``` + fn try_from(theme: &str) -> Result { + if theme.eq_ignore_ascii_case("dark") { + Ok(Self::Dark) + } else if theme.eq_ignore_ascii_case("light") { + Ok(Self::Light) + } else { + Err(()) + } + } +}