Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get is_focused value dynamically #191

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/focused_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::thread;
use xcap::Window;

fn main() {
thread::sleep(std::time::Duration::from_secs(3));

loop {
let windows = Window::all().unwrap();
ologbonowiwi marked this conversation as resolved.
Show resolved Hide resolved

windows.iter().filter(|w| w.is_focused()).for_each(|focused| {
println!(
"Focused Window:\n id: {}\n title: {}\n app_name: {}\n monitor: {:?}\n position: {:?}\n size {:?}\n state {:?}\n",
focused.id(),
focused.title(),
focused.app_name(),
focused.current_monitor().name(),
(focused.x(), focused.y(), focused.z()),
(focused.width(), focused.height()),
(focused.is_minimized(), focused.is_maximized(), focused.is_focused())
);
});

thread::sleep(std::time::Duration::from_secs(1));
}
}
8 changes: 6 additions & 2 deletions src/macos/impl_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use objc2_core_graphics::{
CGDisplayBounds, CGMainDisplayID, CGRectContainsPoint, CGRectIntersectsRect,
CGRectMakeWithDictionaryRepresentation, CGWindowListCopyWindowInfo, CGWindowListOption,
};
use objc2_foundation::{NSNumber, NSString};

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

Expand Down Expand Up @@ -177,9 +178,12 @@ impl ImplWindow {
unsafe {
let impl_monitors = ImplMonitor::all()?;
let workspace = NSWorkspace::sharedWorkspace();
let pid_key = NSString::from_str("NSApplicationProcessIdentifier");
let focused_app_pid = workspace
.frontmostApplication()
.map(|focused_app| focused_app.processIdentifier());
.activeApplication()
.and_then(|dictionary| dictionary.valueForKey(&pid_key))
.and_then(|pid| pid.downcast::<NSNumber>().ok())
.map(|pid| pid.intValue());

let mut impl_windows = Vec::new();

Expand Down