Skip to content

Commit

Permalink
feat: windows 支持获取窗口是否 focused
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu committed Jan 12, 2025
1 parent 4c61e8d commit c231dd4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
window.current_monitor().name(),
(window.x(), window.y(), window.z()),
(window.width(), window.height()),
(window.is_minimized(), window.is_maximized())
(window.is_minimized(), window.is_maximized(), window.is_focused())
);
}
}
4 changes: 4 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl Window {
pub fn is_maximized(&self) -> bool {
self.impl_window.is_maximized
}
/// The window is focused.
pub fn is_focused(&self) -> bool {
self.impl_window.is_focused
}
}

impl Window {
Expand Down
9 changes: 6 additions & 3 deletions src/windows/impl_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use windows::{
},
},
UI::WindowsAndMessaging::{
EnumWindows, GetClassNameW, GetWindowInfo, GetWindowLongPtrW, GetWindowTextLengthW,
GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindow, IsWindowVisible,
IsZoomed, GWL_EXSTYLE, WINDOWINFO, WINDOW_EX_STYLE, WS_EX_TOOLWINDOW,
EnumWindows, GetClassNameW, GetForegroundWindow, GetWindowInfo, GetWindowLongPtrW,
GetWindowTextLengthW, GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindow,
IsWindowVisible, IsZoomed, GWL_EXSTYLE, WINDOWINFO, WINDOW_EX_STYLE, WS_EX_TOOLWINDOW,
},
},
};
Expand Down Expand Up @@ -52,6 +52,7 @@ pub(crate) struct ImplWindow {
pub height: u32,
pub is_minimized: bool,
pub is_maximized: bool,
pub is_focused: bool,
}

fn is_window_cloaked(hwnd: HWND) -> bool {
Expand Down Expand Up @@ -326,6 +327,7 @@ impl ImplWindow {
let rc_client = window_info.rcClient;
let is_minimized = IsIconic(hwnd).as_bool();
let is_maximized = IsZoomed(hwnd).as_bool();
let is_focused = GetForegroundWindow() == hwnd;

Ok(ImplWindow {
hwnd,
Expand All @@ -342,6 +344,7 @@ impl ImplWindow {
height: (rc_client.bottom - rc_client.top) as u32,
is_minimized,
is_maximized,
is_focused,
})
}
}
Expand Down

0 comments on commit c231dd4

Please sign in to comment.