Skip to content

Commit

Permalink
subscribe to NSWorkspaceDidActivateApplicationNotification (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
quangngd authored Oct 8, 2023
1 parent c99d863 commit a968c5d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use input::{rebuild_keyboard_layout_map, INPUT_STATE};
use log::debug;
use once_cell::sync::OnceCell;
use platform::{
ensure_accessibility_permission, run_event_listener, send_backspace, send_string, Handle,
KeyModifier, PressedKey, KEY_DELETE, KEY_ENTER, KEY_ESCAPE, KEY_SPACE, KEY_TAB, RAW_KEY_GLOBE,
add_app_change_callback, ensure_accessibility_permission, run_event_listener, send_backspace,
send_string, Handle, KeyModifier, PressedKey, KEY_DELETE, KEY_ENTER, KEY_ESCAPE, KEY_SPACE,
KEY_TAB, RAW_KEY_GLOBE,
};

use ui::{UIDataAdapter, UPDATE_UI};
Expand Down Expand Up @@ -88,7 +89,6 @@ unsafe fn auto_toggle_vietnamese() {

fn event_handler(handle: Handle, pressed_key: Option<PressedKey>, modifiers: KeyModifier) -> bool {
unsafe {
auto_toggle_vietnamese();
let pressed_key_code = pressed_key.and_then(|p| match p {
PressedKey::Char(c) => Some(c),
_ => None,
Expand Down Expand Up @@ -207,6 +207,9 @@ fn main() {
thread::spawn(|| {
run_event_listener(&event_handler);
});
add_app_change_callback(|| {
unsafe { auto_toggle_vietnamese() };
});
_ = app.launch(UIDataAdapter::new());
}
}
7 changes: 7 additions & 0 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ pub fn send_string(handle: Handle, string: &str) -> Result<(), ()> {
Ok(())
}

pub fn add_app_change_callback<F>(cb: F)
where
F: Fn() + Send + 'static,
{
macos_ext::add_app_change_callback(cb);
}

pub fn run_event_listener(callback: &CallbackFn) {
let current = CFRunLoop::get_current();
if let Ok(event_tap) = new_tap::CGEventTap::new(
Expand Down
29 changes: 26 additions & 3 deletions src/platform/macos_ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cocoa::appkit::{
NSApp, NSApplication, NSButton, NSMenu, NSMenuItem, NSStatusBar, NSStatusItem,
};
use cocoa::base::{nil, YES};
use cocoa::base::{id, nil, YES};
use cocoa::foundation::{NSAutoreleasePool, NSString};
use core_foundation::dictionary::CFDictionaryRef;
use core_foundation::string::CFStringRef;
Expand All @@ -12,10 +12,10 @@ use core_graphics::{
use druid::{Data, Lens};
use libc::c_void;
use objc::{
class,
declare::ClassDecl,
msg_send,
runtime::Class,
runtime::{Object, Sel},
runtime::{Class, Object, Sel},
sel, sel_impl, Message,
};
use objc_foundation::{INSObject, NSObject};
Expand Down Expand Up @@ -342,3 +342,26 @@ extern "C" {
pub fn AXIsProcessTrustedWithOptions(options: CFDictionaryRef) -> bool;
pub static kAXTrustedCheckOptionPrompt: CFStringRef;
}

#[link(name = "AppKit", kind = "framework")]
extern "C" {
pub static NSWorkspaceDidActivateApplicationNotification: CFStringRef;
}

pub fn add_app_change_callback<F>(cb: F)
where
F: Fn() + Send + 'static,
{
unsafe {
let shared_workspace: id = msg_send![class!(NSWorkspace), sharedWorkspace];
let notification_center: id = msg_send![shared_workspace, notificationCenter];
let cb_obj = Callback::from(Box::new(cb));

let _: id = msg_send![notification_center,
addObserver:cb_obj
selector:sel!(call)
name:NSWorkspaceDidActivateApplicationNotification
object:nil
];
}
}
6 changes: 3 additions & 3 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use std::fmt::Display;

use bitflags::bitflags;
pub use os::{
ensure_accessibility_permission, get_active_app_name, get_home_dir, is_in_text_selection,
is_launch_on_login, run_event_listener, send_backspace, send_string, update_launch_on_login,
Handle, SYMBOL_ALT, SYMBOL_CTRL, SYMBOL_SHIFT, SYMBOL_SUPER,
add_app_change_callback, ensure_accessibility_permission, get_active_app_name, get_home_dir,
is_in_text_selection, is_launch_on_login, run_event_listener, send_backspace, send_string,
update_launch_on_login, Handle, SYMBOL_ALT, SYMBOL_CTRL, SYMBOL_SHIFT, SYMBOL_SUPER,
};

#[cfg(target_os = "macos")]
Expand Down

0 comments on commit a968c5d

Please sign in to comment.