Skip to content

Commit 2fe3762

Browse files
authored
refactor: better set foreground (#183)
1 parent 9fde0cb commit 2fe3762

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ features = [
2727
"Win32_Graphics_GdiPlus",
2828
"Win32_Security",
2929
"Win32_Security_Authorization",
30-
"Win32_System_Console",
3130
"Win32_System_LibraryLoader",
3231
"Win32_System_Registry",
3332
"Win32_System_SystemInformation",

src/utils/window.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ use windows::Win32::{
99
Gdi::{GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST},
1010
},
1111
System::{
12-
Console::{AllocConsole, FreeConsole, GetConsoleWindow},
1312
LibraryLoader::GetModuleFileNameW,
1413
Threading::{
1514
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_INFORMATION,
1615
PROCESS_VM_READ,
1716
},
1817
},
19-
UI::WindowsAndMessaging::{
20-
EnumWindows, GetCursorPos, GetForegroundWindow, GetWindow, GetWindowLongPtrW,
21-
GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId, IsIconic,
22-
SetForegroundWindow, SetWindowPos, ShowWindow, GWL_EXSTYLE, GWL_STYLE, GWL_USERDATA,
23-
GW_OWNER, SWP_NOZORDER, SW_RESTORE, WINDOWPLACEMENT, WS_EX_TOOLWINDOW, WS_ICONIC,
24-
WS_VISIBLE,
18+
UI::{
19+
Input::KeyboardAndMouse::{SendInput, INPUT, INPUT_MOUSE},
20+
WindowsAndMessaging::{
21+
EnumWindows, GetCursorPos, GetForegroundWindow, GetWindow, GetWindowLongPtrW,
22+
GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId, IsIconic,
23+
SetForegroundWindow, ShowWindow, GWL_EXSTYLE, GWL_STYLE, GWL_USERDATA, GW_OWNER,
24+
SW_RESTORE, WINDOWPLACEMENT, WS_EX_TOOLWINDOW, WS_ICONIC, WS_VISIBLE,
25+
},
2526
},
2627
};
2728

@@ -146,24 +147,24 @@ pub fn get_window_exe(hwnd: HWND) -> Option<String> {
146147
return None;
147148
}
148149
let module_path = get_module_path(pid)?;
149-
module_path.split('\\').map(|v| v.to_string()).last()
150+
module_path.split('\\').map(|v| v.to_string()).next_back()
150151
}
151152

152153
pub fn set_foreground_window(hwnd: HWND) {
154+
// ref https://github.com/microsoft/PowerToys/blob/4cb72ee126caf1f720c507f6a1dbe658cd515366/src/modules/fancyzones/FancyZonesLib/WindowUtils.cpp#L191
153155
unsafe {
154156
if is_iconic_window(hwnd) {
155157
let _ = ShowWindow(hwnd, SW_RESTORE);
156158
}
157-
if hwnd == get_foreground_window() {
158-
return;
159-
}
160-
if SetForegroundWindow(hwnd).ok().is_err() {
161-
let _ = AllocConsole();
162-
let hwnd_console = GetConsoleWindow();
163-
let _ = SetWindowPos(hwnd_console, None, 0, 0, 0, 0, SWP_NOZORDER);
164-
let _ = FreeConsole();
165-
let _ = SetForegroundWindow(hwnd);
166-
}
159+
160+
let input = INPUT {
161+
r#type: INPUT_MOUSE,
162+
..Default::default()
163+
};
164+
165+
SendInput(&[input], std::mem::size_of::<INPUT>() as i32);
166+
167+
let _ = SetForegroundWindow(hwnd);
167168
};
168169
}
169170

0 commit comments

Comments
 (0)