From 1fd4839c46d86b7e093730c2f1551ea215c3690a Mon Sep 17 00:00:00 2001 From: uuhan Date: Tue, 1 Apr 2025 18:41:13 +0800 Subject: [PATCH 1/2] fix: windows7 SetForegroundWindow compatibility --- Cargo.toml | 1 + src/platform_impl/windows/mod.rs | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ef1b8ef..1faff0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ features = [ "Win32_System_SystemServices", "Win32_Graphics_Gdi", "Win32_UI_Shell", + "Win32_System_Threading", ] [target."cfg(target_os = \"linux\")".dependencies] diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 513f18c..4300beb 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -11,20 +11,21 @@ use windows_sys::{ s, Win32::{ Foundation::{FALSE, HWND, LPARAM, LRESULT, POINT, RECT, S_OK, TRUE, WPARAM}, + System::Threading::{AttachThreadInput, GetCurrentThreadId}, UI::{ Shell::{ Shell_NotifyIconGetRect, Shell_NotifyIconW, NIF_ICON, NIF_MESSAGE, NIF_TIP, NIM_ADD, NIM_DELETE, NIM_MODIFY, NOTIFYICONDATAW, NOTIFYICONIDENTIFIER, }, WindowsAndMessaging::{ - CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, KillTimer, - RegisterClassW, RegisterWindowMessageA, SendMessageW, SetForegroundWindow, - SetTimer, TrackPopupMenu, CREATESTRUCTW, CW_USEDEFAULT, GWL_USERDATA, HICON, HMENU, - TPM_BOTTOMALIGN, TPM_LEFTALIGN, WM_CREATE, WM_DESTROY, WM_LBUTTONDBLCLK, - WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDBLCLK, WM_MBUTTONDOWN, WM_MBUTTONUP, - WM_MOUSEMOVE, WM_NCCREATE, WM_RBUTTONDBLCLK, WM_RBUTTONDOWN, WM_RBUTTONUP, - WM_TIMER, WNDCLASSW, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, - WS_EX_TRANSPARENT, WS_OVERLAPPED, + CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetForegroundWindow, + GetWindowThreadProcessId, KillTimer, RegisterClassW, RegisterWindowMessageA, + SendMessageW, SetForegroundWindow, SetTimer, TrackPopupMenu, CREATESTRUCTW, + CW_USEDEFAULT, GWL_USERDATA, HICON, HMENU, TPM_BOTTOMALIGN, TPM_LEFTALIGN, + WM_CREATE, WM_DESTROY, WM_LBUTTONDBLCLK, WM_LBUTTONDOWN, WM_LBUTTONUP, + WM_MBUTTONDBLCLK, WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MOUSEMOVE, WM_NCCREATE, + WM_RBUTTONDBLCLK, WM_RBUTTONDOWN, WM_RBUTTONUP, WM_TIMER, WNDCLASSW, WS_EX_LAYERED, + WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TRANSPARENT, WS_OVERLAPPED, }, }, }, @@ -505,7 +506,12 @@ unsafe extern "system" fn tray_timer_proc(hwnd: HWND, msg: u32, wparam: WPARAM, unsafe fn show_tray_menu(hwnd: HWND, menu: HMENU, x: i32, y: i32) { // bring the hidden window to the foreground so the pop up menu // would automatically hide on click outside - SetForegroundWindow(hwnd); + // SetForegroundWindow(hwnd); + AttachThreadInput( + GetWindowThreadProcessId(GetForegroundWindow(), std::ptr::null_mut()), + GetCurrentThreadId(), + TRUE, + ); TrackPopupMenu( menu, // align bottom / right, maybe we could expose this later.. From 748128f93d3321198508321543cbd13430cf5951 Mon Sep 17 00:00:00 2001 From: uuhan Date: Tue, 1 Apr 2025 20:32:04 +0800 Subject: [PATCH 2/2] Comments out SetForegroundWindow --- src/platform_impl/windows/mod.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 4300beb..9288b4a 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -20,12 +20,12 @@ use windows_sys::{ WindowsAndMessaging::{ CreateWindowExW, DefWindowProcW, DestroyWindow, GetCursorPos, GetForegroundWindow, GetWindowThreadProcessId, KillTimer, RegisterClassW, RegisterWindowMessageA, - SendMessageW, SetForegroundWindow, SetTimer, TrackPopupMenu, CREATESTRUCTW, - CW_USEDEFAULT, GWL_USERDATA, HICON, HMENU, TPM_BOTTOMALIGN, TPM_LEFTALIGN, - WM_CREATE, WM_DESTROY, WM_LBUTTONDBLCLK, WM_LBUTTONDOWN, WM_LBUTTONUP, - WM_MBUTTONDBLCLK, WM_MBUTTONDOWN, WM_MBUTTONUP, WM_MOUSEMOVE, WM_NCCREATE, - WM_RBUTTONDBLCLK, WM_RBUTTONDOWN, WM_RBUTTONUP, WM_TIMER, WNDCLASSW, WS_EX_LAYERED, - WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TRANSPARENT, WS_OVERLAPPED, + SendMessageW, SetTimer, TrackPopupMenu, CREATESTRUCTW, CW_USEDEFAULT, GWL_USERDATA, + HICON, HMENU, TPM_BOTTOMALIGN, TPM_LEFTALIGN, WM_CREATE, WM_DESTROY, + WM_LBUTTONDBLCLK, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MBUTTONDBLCLK, WM_MBUTTONDOWN, + WM_MBUTTONUP, WM_MOUSEMOVE, WM_NCCREATE, WM_RBUTTONDBLCLK, WM_RBUTTONDOWN, + WM_RBUTTONUP, WM_TIMER, WNDCLASSW, WS_EX_LAYERED, WS_EX_NOACTIVATE, + WS_EX_TOOLWINDOW, WS_EX_TRANSPARENT, WS_OVERLAPPED, }, }, }, @@ -506,12 +506,15 @@ unsafe extern "system" fn tray_timer_proc(hwnd: HWND, msg: u32, wparam: WPARAM, unsafe fn show_tray_menu(hwnd: HWND, menu: HMENU, x: i32, y: i32) { // bring the hidden window to the foreground so the pop up menu // would automatically hide on click outside - // SetForegroundWindow(hwnd); + // + // `SetForegroundWindow(hwnd);` seems quite straightforward, but it **panics** + // in Windows 7. AttachThreadInput( GetWindowThreadProcessId(GetForegroundWindow(), std::ptr::null_mut()), GetCurrentThreadId(), TRUE, ); + TrackPopupMenu( menu, // align bottom / right, maybe we could expose this later..