Skip to content

Commit

Permalink
Merge pull request #14 from miniben-90/fix-active-app-osx
Browse files Browse the repository at this point in the history
[MACOS] Replace workflow recovery of active window
  • Loading branch information
miniben-90 authored Feb 11, 2024
2 parents 54a1cd3 + ce276ef commit a67c46c
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/macos/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use std::process::Command;

use cocoa::base::{id, nil};
use cocoa::foundation::{NSAutoreleasePool, NSString, NSUInteger, NSURL};
use cocoa::base::id;
use cocoa::foundation::{NSString, NSURL};
use core_foundation::array::{CFArrayGetCount, CFArrayGetValueAtIndex};
use core_foundation::base::{CFType, TCFType};
use core_foundation::boolean::CFBoolean;
Expand All @@ -30,6 +30,8 @@ use crate::common::{
},
};

use objc::runtime::{BOOL, NO};

pub struct MacosAPI {}

/**
Expand Down Expand Up @@ -76,27 +78,25 @@ fn os_name() -> String {
r#"darwin"#.to_owned()
}

// fn get_window_information

fn get_active_window_pid() -> NSUInteger {
unsafe {
let _pool = NSAutoreleasePool::new(nil);
let _shared_app_id: id = msg_send![class!(NSApplication), sharedApplication];
// NSApplication::finishLaunching(shared_app_id);
let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
let frontapp: id = msg_send![workspace, frontmostApplication];
let active_window_pid: NSUInteger = msg_send![frontapp, processIdentifier];
return active_window_pid;
}
}
// fn get_active_window_pid() -> NSUInteger {
// unsafe {
// let _pool = NSAutoreleasePool::new(nil);
// let _shared_app_id: id = msg_send![class!(NSApplication), sharedApplication];
// // NSApplication::finishLaunching(shared_app_id);
// let workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
// let frontapp: id = msg_send![workspace, frontmostApplication];
// let active_window_pid: NSUInteger = msg_send![frontapp, processIdentifier];
// return active_window_pid;
// }
// }

fn get_windows_informations(only_active: bool) -> Vec<WindowInfo> {
let mut windows: Vec<WindowInfo> = Vec::new();
let mut active_window_pid: u64 = 0;
// let mut active_window_pid: u64 = 0;

if only_active {
active_window_pid = get_active_window_pid();
}
// if only_active {
// active_window_pid = get_active_window_pid();
// }

let options = kCGWindowListOptionOnScreenOnly
| kCGWindowListExcludeDesktopElements
Expand Down Expand Up @@ -140,9 +140,9 @@ fn get_windows_informations(only_active: bool) -> Vec<WindowInfo> {
let process_id = cfd.get(unsafe { kCGWindowOwnerPID });
let process_id = process_id.downcast::<CFNumber>().unwrap().to_i64().unwrap();

if only_active && process_id.ne(&(active_window_pid as i64)) {
continue;
}
// if only_active && process_id.ne(&(active_window_pid as i64)) {
// continue;
// }

let app: id = unsafe {
msg_send![
Expand All @@ -151,6 +151,15 @@ fn get_windows_informations(only_active: bool) -> Vec<WindowInfo> {
]
};

let is_not_active: bool = unsafe {
let is_active: BOOL = msg_send![app, isActive];
is_active == NO
};

if only_active && is_not_active {
continue;
}

let bundle_identifier: id = unsafe { msg_send![app, bundleIdentifier] };
let bundle_identifier = unsafe { NSString::UTF8String(bundle_identifier) };
let bundle_identifier =
Expand Down Expand Up @@ -224,7 +233,7 @@ fn get_windows_informations(only_active: bool) -> Vec<WindowInfo> {
url,
});

if only_active && process_id.ne(&(active_window_pid as i64)) {
if only_active && is_not_active {
break;
}
}
Expand Down

0 comments on commit a67c46c

Please sign in to comment.