From 06d00b940d167fe9d4b80b999e4c66a4ca5d4e62 Mon Sep 17 00:00:00 2001 From: Matin Aniss <76515905+MatinAniss@users.noreply.github.com> Date: Mon, 27 Jan 2025 23:07:28 +1100 Subject: [PATCH] gpui: Support windows dark mode title bar (#23700) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will allow Windows GPUI applications that utilise the native title bar to support the dark mode variant when the system user has dark mode enabled in their system settings. [Related Win32 information](https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/ui/apply-windows-themes) | Before | After | | ------------- | ------------- | | ![image](https://github.com/user-attachments/assets/50f3d131-8f41-4b91-8012-f8120b150033) | ![image](https://github.com/user-attachments/assets/a36a6c6b-a469-49ba-85a8-9b55db9ea20f) | Release Notes: - N/A --------- Co-authored-by: 张小白 <364772080@qq.com> --- crates/gpui/src/platform/windows/events.rs | 8 ++++++-- crates/gpui/src/platform/windows/util.rs | 18 +++++++++++++++++- crates/gpui/src/platform/windows/window.rs | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/gpui/src/platform/windows/events.rs b/crates/gpui/src/platform/windows/events.rs index 5e0888e82524910d39ea516c86dcaf28f9a6f4ee..4ac573766b3f1f00822066b48a537ff2450777e9 100644 --- a/crates/gpui/src/platform/windows/events.rs +++ b/crates/gpui/src/platform/windows/events.rs @@ -89,7 +89,7 @@ pub(crate) fn handle_msg( WM_IME_COMPOSITION => handle_ime_composition(handle, lparam, state_ptr), WM_SETCURSOR => handle_set_cursor(lparam, state_ptr), WM_SETTINGCHANGE => handle_system_settings_changed(handle, state_ptr), - WM_DWMCOLORIZATIONCOLORCHANGED => handle_system_theme_changed(state_ptr), + WM_DWMCOLORIZATIONCOLORCHANGED => handle_system_theme_changed(handle, state_ptr), WM_GPUI_CURSOR_STYLE_CHANGED => handle_cursor_changed(lparam, state_ptr), _ => None, }; @@ -1215,7 +1215,10 @@ fn handle_system_command(wparam: WPARAM, state_ptr: Rc) - None } -fn handle_system_theme_changed(state_ptr: Rc) -> Option { +fn handle_system_theme_changed( + handle: HWND, + state_ptr: Rc, +) -> Option { let mut callback = state_ptr .state .borrow_mut() @@ -1224,6 +1227,7 @@ fn handle_system_theme_changed(state_ptr: Rc) -> Option HCURSOR { })) } +pub(crate) fn set_dwm_window_appearance(hwnd: HWND) { + let dark_mode_enabled: BOOL = match system_appearance().log_err().unwrap_or_default() { + WindowAppearance::Dark | WindowAppearance::VibrantDark => true.into(), + WindowAppearance::Light | WindowAppearance::VibrantLight => false.into(), + }; + unsafe { + DwmSetWindowAttribute( + hwnd, + DWMWA_USE_IMMERSIVE_DARK_MODE, + &dark_mode_enabled as *const _ as _, + std::mem::size_of::() as u32, + ) + .log_err(); + } +} + #[inline] pub(crate) fn logical_point(x: f32, y: f32, scale_factor: f32) -> Point { Point { diff --git a/crates/gpui/src/platform/windows/window.rs b/crates/gpui/src/platform/windows/window.rs index 2d873d23bc3fabd2bde3a7f0f991a5c7994ad49f..9dec526d444b85063ab9435d8ff4871aa0cd61eb 100644 --- a/crates/gpui/src/platform/windows/window.rs +++ b/crates/gpui/src/platform/windows/window.rs @@ -443,7 +443,7 @@ impl WindowsWindow { let state_ptr = context.inner.take().unwrap()?; let hwnd = creation_result?; register_drag_drop(state_ptr.clone())?; - + set_dwm_window_appearance(hwnd); state_ptr.state.borrow_mut().border_offset.update(hwnd)?; let placement = retrieve_window_placement( hwnd,