From ddf8a35aeb4ededceedde53c65af5300b9b7ee00 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Tue, 29 Jun 2021 20:03:45 +0200 Subject: [PATCH] feat(mac-utils): add methods to show/hide macOS dock icon --- espanso-mac-utils/build.rs | 1 + espanso-mac-utils/src/ffi.rs | 2 ++ espanso-mac-utils/src/lib.rs | 14 ++++++++++++++ espanso-mac-utils/src/native.h | 6 ++++++ espanso-mac-utils/src/native.mm | 11 +++++++++++ 5 files changed, 34 insertions(+) diff --git a/espanso-mac-utils/build.rs b/espanso-mac-utils/build.rs index 90c93de..d6f921a 100644 --- a/espanso-mac-utils/build.rs +++ b/espanso-mac-utils/build.rs @@ -34,6 +34,7 @@ fn cc_config() { println!("cargo:rustc-link-lib=dylib=c++"); println!("cargo:rustc-link-lib=static=espansomacutils"); println!("cargo:rustc-link-lib=framework=Cocoa"); + println!("cargo:rustc-link-lib=framework=Carbon"); } fn main() { diff --git a/espanso-mac-utils/src/ffi.rs b/espanso-mac-utils/src/ffi.rs index 770e98d..190a673 100644 --- a/espanso-mac-utils/src/ffi.rs +++ b/espanso-mac-utils/src/ffi.rs @@ -27,4 +27,6 @@ extern "C" { pub fn mac_utils_get_path_from_pid(pid: i64, buffer: *mut c_char, size: i32) -> i32; pub fn mac_utils_check_accessibility() -> i32; pub fn mac_utils_prompt_accessibility() -> i32; + pub fn mac_utils_transition_to_foreground_app(); + pub fn mac_utils_transition_to_background_app(); } diff --git a/espanso-mac-utils/src/lib.rs b/espanso-mac-utils/src/lib.rs index 742034d..7a13b2d 100644 --- a/espanso-mac-utils/src/lib.rs +++ b/espanso-mac-utils/src/lib.rs @@ -115,6 +115,20 @@ pub fn prompt_accessibility() -> bool { } } +#[cfg(target_os = "macos")] +pub fn convert_to_foreground_app() { + unsafe { + ffi::mac_utils_transition_to_foreground_app(); + } +} + +#[cfg(target_os = "macos")] +pub fn convert_to_background_app() { + unsafe { + ffi::mac_utils_transition_to_background_app(); + } +} + #[cfg(test)] #[cfg(target_os = "macos")] mod tests { diff --git a/espanso-mac-utils/src/native.h b/espanso-mac-utils/src/native.h index 1d2a70d..984b540 100644 --- a/espanso-mac-utils/src/native.h +++ b/espanso-mac-utils/src/native.h @@ -34,4 +34,10 @@ extern "C" int32_t mac_utils_check_accessibility(); // Return 1 if the accessibility permissions have been granted, 0 otherwise extern "C" int32_t mac_utils_prompt_accessibility(); +// When called, convert the current process to a foreground app (showing the dock icon). +extern "C" void mac_utils_transition_to_foreground_app(); + +// When called, convert the current process to a background app (hide the dock icon). +extern "C" void mac_utils_transition_to_background_app(); + #endif //ESPANSO_MAC_UTILS_H \ No newline at end of file diff --git a/espanso-mac-utils/src/native.mm b/espanso-mac-utils/src/native.mm index 11c0770..f262e66 100644 --- a/espanso-mac-utils/src/native.mm +++ b/espanso-mac-utils/src/native.mm @@ -21,6 +21,7 @@ #include #import #import +#import // Taken (with a few modifications) from the MagicKeys project: https://github.com/zsszatmari/MagicKeys int32_t mac_utils_get_secure_input_process(int64_t *pid) { @@ -75,4 +76,14 @@ int32_t mac_utils_check_accessibility() { int32_t mac_utils_prompt_accessibility() { NSDictionary* opts = @{(__bridge id)kAXTrustedCheckOptionPrompt: @YES}; return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)opts); +} + +void mac_utils_transition_to_foreground_app() { + ProcessSerialNumber psn = { 0, kCurrentProcess }; + TransformProcessType(&psn, kProcessTransformToForegroundApplication); +} + +void mac_utils_transition_to_background_app() { + ProcessSerialNumber psn = { 0, kCurrentProcess }; + TransformProcessType(&psn, kProcessTransformToUIElementApplication); } \ No newline at end of file