From 016e122c5f10eb61f8abe052a888950a460e0804 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Tue, 24 Sep 2024 07:58:32 +0800 Subject: [PATCH] fix(windows): changing the theme activates the window (#978) * Fix changing theme activates the window * Add change file * Combine unsafe blocks --- .changes/windows-theme-active-state.md | 5 +++++ src/platform_impl/windows/dark_mode.rs | 11 ++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 .changes/windows-theme-active-state.md diff --git a/.changes/windows-theme-active-state.md b/.changes/windows-theme-active-state.md new file mode 100644 index 000000000..4471fd7bd --- /dev/null +++ b/.changes/windows-theme-active-state.md @@ -0,0 +1,5 @@ +--- +tao: patch +--- + +Fix changing the theme activates the window on Windows diff --git a/src/platform_impl/windows/dark_mode.rs b/src/platform_impl/windows/dark_mode.rs index b12f98b4b..85a907c0c 100644 --- a/src/platform_impl/windows/dark_mode.rs +++ b/src/platform_impl/windows/dark_mode.rs @@ -12,7 +12,7 @@ use windows::{ Foundation::{BOOL, HANDLE, HMODULE, HWND, WPARAM}, Graphics::Dwm::{DwmSetWindowAttribute, DWMWINDOWATTRIBUTE}, System::LibraryLoader::*, - UI::{Accessibility::*, WindowsAndMessaging::*}, + UI::{Accessibility::*, Input::KeyboardAndMouse::GetActiveWindow, WindowsAndMessaging::*}, }, }; @@ -187,9 +187,14 @@ fn refresh_titlebar_theme_color(hwnd: HWND, is_dark_mode: bool) { &dark_mode as *const BOOL as *const c_void, std::mem::size_of::() as u32, ); + if GetActiveWindow() == hwnd { + DefWindowProcW(hwnd, WM_NCACTIVATE, None, None); + DefWindowProcW(hwnd, WM_NCACTIVATE, WPARAM(true.into()), None); + } else { + DefWindowProcW(hwnd, WM_NCACTIVATE, WPARAM(true.into()), None); + DefWindowProcW(hwnd, WM_NCACTIVATE, None, None); + } } - unsafe { DefWindowProcW(hwnd, WM_NCACTIVATE, None, None) }; - unsafe { DefWindowProcW(hwnd, WM_NCACTIVATE, WPARAM(true.into()), None) }; } } }