From c231dd41f14ff40c9f780223d23502cef292a1ac Mon Sep 17 00:00:00 2001 From: nashaofu <19303058+nashaofu@users.noreply.github.com> Date: Sun, 12 Jan 2025 15:32:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20windows=20=E6=94=AF=E6=8C=81=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=AA=97=E5=8F=A3=E6=98=AF=E5=90=A6=20focused?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/window.rs | 2 +- src/window.rs | 4 ++++ src/windows/impl_window.rs | 9 ++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index 6fc67d9..1e06884 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -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()) ); } } diff --git a/src/window.rs b/src/window.rs index 73a875a..7e66d55 100644 --- a/src/window.rs +++ b/src/window.rs @@ -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 { diff --git a/src/windows/impl_window.rs b/src/windows/impl_window.rs index e2882fd..8d15500 100644 --- a/src/windows/impl_window.rs +++ b/src/windows/impl_window.rs @@ -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, }, }, }; @@ -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 { @@ -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, @@ -342,6 +344,7 @@ impl ImplWindow { height: (rc_client.bottom - rc_client.top) as u32, is_minimized, is_maximized, + is_focused, }) } }