Skip to content

Commit

Permalink
feat: macos 实现获取窗口是否 focused
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu committed Jan 13, 2025
1 parent e61ef09 commit aa69cb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ thiserror = "2.0"
[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = "0.10"
core-graphics = "0.24"
objc2-app-kit = { version = "0.2.2", features = [
"libc",
"NSWorkspace",
"NSRunningApplication",
] }

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.58", features = [
Expand Down
16 changes: 14 additions & 2 deletions src/macos/impl_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use core_graphics::{
window::{kCGNullWindowID, kCGWindowSharingNone},
};
use image::RgbaImage;
use objc2_app_kit::NSWorkspace;

use crate::{error::XCapResult, XCapError};

Expand All @@ -35,6 +36,7 @@ pub(crate) struct ImplWindow {
pub height: u32,
pub is_minimized: bool,
pub is_maximized: bool,
pub is_focused: bool,
}

unsafe impl Send for ImplWindow {}
Expand Down Expand Up @@ -129,9 +131,10 @@ impl ImplWindow {
window_name: String,
window_owner_name: String,
z: i32,
focused_app_pid: Option<i32>,
) -> XCapResult<ImplWindow> {
let id = get_cf_number_i32_value(window_cf_dictionary_ref, "kCGWindowNumber")? as u32;
let pid = get_cf_number_i32_value(window_cf_dictionary_ref, "kCGWindowOwnerPID")? as u32;
let pid = get_cf_number_i32_value(window_cf_dictionary_ref, "kCGWindowOwnerPID")?;

let cg_rect = get_window_cg_rect(window_cf_dictionary_ref)?;

Expand Down Expand Up @@ -164,11 +167,13 @@ impl ImplWindow {
let is_minimized =
!get_cf_bool_value(window_cf_dictionary_ref, "kCGWindowIsOnscreen")? && !is_maximized;

let is_focused = focused_app_pid.eq(&Some(pid));

Ok(ImplWindow {
id,
title: window_name,
app_name: window_owner_name,
pid,
pid: pid as u32,
current_monitor: current_monitor.clone(),
x: cg_rect.origin.x as i32,
y: cg_rect.origin.y as i32,
Expand All @@ -177,12 +182,18 @@ impl ImplWindow {
height: cg_rect.size.height as u32,
is_minimized,
is_maximized,
is_focused,
})
}

pub fn all() -> XCapResult<Vec<ImplWindow>> {
unsafe {
let impl_monitors = ImplMonitor::all()?;
let workspace = NSWorkspace::sharedWorkspace();
let focused_app_pid = workspace
.frontmostApplication()
.map(|focused_app| focused_app.processIdentifier());

let mut impl_windows = Vec::new();

// CGWindowListCopyWindowInfo 返回窗口顺序为从顶层到最底层
Expand Down Expand Up @@ -240,6 +251,7 @@ impl ImplWindow {
window_name.clone(),
window_owner_name.clone(),
num_windows as i32 - i as i32 - 1,
focused_app_pid,
) {
impl_windows.push(impl_window);
} else {
Expand Down

0 comments on commit aa69cb7

Please sign in to comment.