feat(mac-utils): add methods to show/hide macOS dock icon

This commit is contained in:
Federico Terzi 2021-06-29 20:03:45 +02:00
parent 40ea11ef94
commit ddf8a35aeb
5 changed files with 34 additions and 0 deletions

View File

@ -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() {

View File

@ -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();
}

View File

@ -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 {

View File

@ -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

View File

@ -21,6 +21,7 @@
#include <libproc.h>
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
// 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);
}