From e8acf8b9ec125eb1c55cc780f005b480e6e45f7b Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 8 Nov 2022 18:48:56 +0200 Subject: [PATCH] Add `Window::set_theme` --- CHANGELOG.md | 1 + src/platform_impl/android/mod.rs | 2 ++ src/platform_impl/ios/window.rs | 5 +++++ src/platform_impl/linux/mod.rs | 5 +++++ src/platform_impl/linux/wayland/window/mod.rs | 6 ++++++ src/platform_impl/linux/x11/window.rs | 3 +++ src/platform_impl/macos/window.rs | 5 +++++ src/platform_impl/web/window.rs | 3 +++ src/platform_impl/windows/window.rs | 5 +++++ src/window.rs | 12 +++++++++++- 10 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3e90880b..d374f151c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- On Windows, macOS and Wayland, add `Window::set_theme`. - On Windows, fix icons specified on `WindowBuilder` not taking effect for windows created after the first one. - On Windows and macOS, add `Window::title` to query the current window title. - On Windows, fix focusing menubar when pressing `Alt`. diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 350c39d0e..f55bffde4 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -853,6 +853,8 @@ impl Window { ndk_glue::content_rect() } + pub fn set_theme(&self, _theme: Theme) {} + pub fn theme(&self) -> Option { None } diff --git a/src/platform_impl/ios/window.rs b/src/platform_impl/ios/window.rs index ba2e3a3be..0d54177e1 100644 --- a/src/platform_impl/ios/window.rs +++ b/src/platform_impl/ios/window.rs @@ -348,6 +348,11 @@ impl Inner { None } + #[inline] + pub fn set_theme(&self, _theme: Theme) { + warn!("`Window::set_theme` is ignored on iOS"); + } + pub fn title(&self) -> String { warn!("`Window::title` is ignored on iOS"); String::new() diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 3ffba8991..3a6fe1a5b 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -585,6 +585,11 @@ impl Window { x11_or_wayland!(match self; Window(window) => window.raw_display_handle()) } + #[inlnie] + pub fn set_theme(&self, theme: Theme) { + x11_or_wayland!(match self; Window(window) => window.set_theme()) + } + #[inline] pub fn theme(&self) -> Option { x11_or_wayland!(match self; Window(window) => window.theme()) diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index e30d0873e..c8d26c089 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -620,6 +620,12 @@ impl Window { self.event_loop_awakener.ping(); } + #[inline] + pub fn set_theme(&self, _theme: Theme) { + #[cfg(feature = "sctk-adwaita")] + window.set_frame_config(theme.into()); + } + #[inline] pub fn theme(&self) -> Option { None diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 3135febcd..6dc83a30e 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1547,6 +1547,9 @@ impl UnownedWindow { RawDisplayHandle::Xlib(display_handle) } + #[inline] + pub fn set_theme(&self, _theme: Theme) {} + #[inline] pub fn theme(&self) -> Option { None diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 5e96fbac7..a888c4b10 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -1122,6 +1122,11 @@ impl WinitWindow { state.current_theme } + #[inline] + pub fn set_theme(&self, theme: Theme) { + set_ns_theme(theme); + } + #[inline] pub fn title(&self) -> String { self.title_().to_string() diff --git a/src/platform_impl/web/window.rs b/src/platform_impl/web/window.rs index 14290a8a5..ce99c6b1b 100644 --- a/src/platform_impl/web/window.rs +++ b/src/platform_impl/web/window.rs @@ -374,6 +374,9 @@ impl Window { RawDisplayHandle::Web(WebDisplayHandle::empty()) } + #[inline] + pub fn set_theme(&self, _theme: Theme) {} + #[inline] pub fn theme(&self) -> Option { None diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index e2515961b..fcafebedb 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -693,6 +693,11 @@ impl Window { }); } + #[inline] + pub fn set_theme(&self, theme: Theme) { + try_theme(self.window.0, Some(theme)); + } + #[inline] pub fn theme(&self) -> Option { Some(self.window_state_lock().current_theme) diff --git a/src/window.rs b/src/window.rs index c11be5da7..ce0ffb8ca 100644 --- a/src/window.rs +++ b/src/window.rs @@ -943,12 +943,22 @@ impl Window { self.window.request_user_attention(request_type) } - /// Returns the current window theme. + /// Sets the current window theme. /// /// ## Platform-specific /// /// - **iOS / Android / Web / x11:** Unsupported. #[inline] + pub fn set_theme(&self, theme: Theme) { + self.window.set_theme(theme) + } + + /// Returns the current window theme. + /// + /// ## Platform-specific + /// + /// - **iOS / Android / Web / Wayland / x11:** Unsupported. + #[inline] pub fn theme(&self) -> Option { self.window.theme() }