feat(mac-utils): add methods to start and stop headless eventloops

This commit is contained in:
Federico Terzi 2021-10-13 21:14:41 +02:00
parent 12ba0b8755
commit 806ac8112e
4 changed files with 32 additions and 0 deletions

View File

@ -29,4 +29,6 @@ extern "C" {
pub fn mac_utils_prompt_accessibility() -> i32;
pub fn mac_utils_transition_to_foreground_app();
pub fn mac_utils_transition_to_background_app();
pub fn mac_utils_start_headless_eventloop();
pub fn mac_utils_exit_headless_eventloop();
}

View File

@ -113,6 +113,20 @@ pub fn convert_to_background_app() {
}
}
#[cfg(target_os = "macos")]
pub fn start_headless_eventloop() {
unsafe {
ffi::mac_utils_start_headless_eventloop();
}
}
#[cfg(target_os = "macos")]
pub fn exit_headless_eventloop() {
unsafe {
ffi::mac_utils_exit_headless_eventloop();
}
}
#[cfg(test)]
#[cfg(target_os = "macos")]
mod tests {

View File

@ -40,4 +40,8 @@ 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();
// Start and stop a "headless" eventloop to receive NSApplication events.
extern "C" void mac_utils_start_headless_eventloop();
extern "C" void mac_utils_exit_headless_eventloop();
#endif //ESPANSO_MAC_UTILS_H

View File

@ -89,3 +89,15 @@ void mac_utils_transition_to_background_app() {
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
}
void mac_utils_start_headless_eventloop() {
NSApplication * application = [NSApplication sharedApplication];
[NSApp run];
}
void mac_utils_exit_headless_eventloop() {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[NSApp stop:nil];
[NSApp abortModal];
});
}