From 8b693fbfe25cada2116ca14553ed19bde30262f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sved=C3=A4ng?= Date: Wed, 12 Jun 2024 13:38:35 +0200 Subject: [PATCH 1/2] WIP. --- src/lib.rs | 4 +++ src/native/apple/apple_util.rs | 56 ++++++++++++++++++++++++++++++++++ src/native/apple/frameworks.rs | 25 +++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ef402e7a..e5c4a0ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -295,3 +295,7 @@ where native::ios::run(conf, f); } } + +pub fn hack() { + crate::native::apple::apple_util::startup_hack(); +} diff --git a/src/native/apple/apple_util.rs b/src/native/apple/apple_util.rs index 6e2a038b..b73c4eb3 100644 --- a/src/native/apple/apple_util.rs +++ b/src/native/apple/apple_util.rs @@ -565,3 +565,59 @@ pub extern "C" fn yes(_: &Object, _: Sel) -> BOOL { pub extern "C" fn yes1(_: &Object, _: Sel, _: ObjcId) -> BOOL { YES } + +static mut HACKED: bool = false; + +pub fn define_app_delegate_class() -> *const Class { + println!("Defining app delegate!"); + + let superclass = class!(NSObject); + let mut decl = ClassDecl::new("MyAppDelegate", superclass).unwrap(); + + unsafe { + decl.add_method( + sel!(applicationShouldTerminateAfterLastWindowClosed:), + yes1 as extern "C" fn(&Object, Sel, ObjcId) -> BOOL, + ); + decl.add_method( + sel!(applicationDidUpdate:), + application_did_update as extern "C" fn(&Object, Sel, ObjcId), + ); + } + + extern "C" fn application_did_update(this: &Object, _: Sel, _: ObjcId) { + unsafe { + if !HACKED { + HACKED = true; + println!("Application did update, delegate was called!"); + + let psn = ProcessSerialNumber { + a: 0, + b: WhichProcess::kCurrentProcess as u32, + }; + + let os_status: i32 = TransformProcessType( + std::ptr::addr_of!(psn), + TransformProcess::kProcessTransformToForegroundApplication as u32, + ); + + println!("os_status: {}", os_status); + + let ns_app: ObjcId = msg_send![class!(NSApplication), sharedApplication]; + let () = msg_send![ns_app, activateIgnoringOtherApps: YES]; + } + } + } + + return decl.register(); +} + +pub fn startup_hack() { + unsafe { + let app_delegate_class = define_app_delegate_class(); + let app_delegate_instance: ObjcId = msg_send![app_delegate_class, new]; + + let ns_app: ObjcId = msg_send![class!(NSApplication), sharedApplication]; + let () = msg_send![ns_app, setDelegate: app_delegate_instance]; + } +} diff --git a/src/native/apple/frameworks.rs b/src/native/apple/frameworks.rs index fd81e94e..96168fbf 100644 --- a/src/native/apple/frameworks.rs +++ b/src/native/apple/frameworks.rs @@ -94,6 +94,31 @@ extern "C" { pub fn NSLog(fmt: ObjcId, ...); } +#[repr(packed)] +pub struct ProcessSerialNumber { + pub a: u32, + pub b: u32, +} + +#[repr(i32)] +pub enum WhichProcess { + kNoProcess = 0, + kSystemProcess, + kCurrentProcess, +} + +#[repr(i32)] +pub enum TransformProcess { + kProcessTransformToForegroundApplication = 1, + kProcessTransformToBackgroundApplication = 2, + kProcessTransformToUIElementApplication = 4, +} + +#[link(name = "ApplicationServices", kind = "framework")] +extern "C" { + pub fn TransformProcessType(psn: *const ProcessSerialNumber, transformState: u32) -> i32; +} + #[link(name = "ImageIO", kind = "framework")] extern "C" { pub static kUTTypePNG: ObjcId; From 603022948f2ea1243956a5ce82047b98af7b495a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sved=C3=A4ng?= Date: Wed, 12 Jun 2024 14:41:39 +0200 Subject: [PATCH 2/2] This actually seems to work..? --- src/lib.rs | 4 ++-- src/native/apple/apple_util.rs | 30 +++++------------------------- src/native/apple/frameworks.rs | 25 ------------------------- 3 files changed, 7 insertions(+), 52 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0c7ce86e..78747d39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -389,6 +389,6 @@ where } } -pub fn hack() { - crate::native::apple::apple_util::startup_hack(); +pub fn prevent_double_click_bug_on_macos() { + crate::native::apple::apple_util::prevent_double_click_bug_on_macos(); } diff --git a/src/native/apple/apple_util.rs b/src/native/apple/apple_util.rs index b73c4eb3..25ace308 100644 --- a/src/native/apple/apple_util.rs +++ b/src/native/apple/apple_util.rs @@ -569,42 +569,23 @@ pub extern "C" fn yes1(_: &Object, _: Sel, _: ObjcId) -> BOOL { static mut HACKED: bool = false; pub fn define_app_delegate_class() -> *const Class { - println!("Defining app delegate!"); - let superclass = class!(NSObject); let mut decl = ClassDecl::new("MyAppDelegate", superclass).unwrap(); unsafe { - decl.add_method( - sel!(applicationShouldTerminateAfterLastWindowClosed:), - yes1 as extern "C" fn(&Object, Sel, ObjcId) -> BOOL, - ); decl.add_method( sel!(applicationDidUpdate:), application_did_update as extern "C" fn(&Object, Sel, ObjcId), ); } - extern "C" fn application_did_update(this: &Object, _: Sel, _: ObjcId) { + extern "C" fn application_did_update(_this: &Object, _: Sel, _: ObjcId) { unsafe { if !HACKED { HACKED = true; - println!("Application did update, delegate was called!"); - - let psn = ProcessSerialNumber { - a: 0, - b: WhichProcess::kCurrentProcess as u32, - }; - - let os_status: i32 = TransformProcessType( - std::ptr::addr_of!(psn), - TransformProcess::kProcessTransformToForegroundApplication as u32, - ); - - println!("os_status: {}", os_status); - - let ns_app: ObjcId = msg_send![class!(NSApplication), sharedApplication]; - let () = msg_send![ns_app, activateIgnoringOtherApps: YES]; + let current_application: ObjcId = + msg_send![class!(NSRunningApplication), currentApplication]; + let _: BOOL = msg_send![current_application, activateWithOptions: nil]; } } } @@ -612,11 +593,10 @@ pub fn define_app_delegate_class() -> *const Class { return decl.register(); } -pub fn startup_hack() { +pub fn prevent_double_click_bug_on_macos() { unsafe { let app_delegate_class = define_app_delegate_class(); let app_delegate_instance: ObjcId = msg_send![app_delegate_class, new]; - let ns_app: ObjcId = msg_send![class!(NSApplication), sharedApplication]; let () = msg_send![ns_app, setDelegate: app_delegate_instance]; } diff --git a/src/native/apple/frameworks.rs b/src/native/apple/frameworks.rs index 2f91a0e6..3a30880d 100644 --- a/src/native/apple/frameworks.rs +++ b/src/native/apple/frameworks.rs @@ -94,31 +94,6 @@ extern "C" { pub fn NSLog(fmt: ObjcId, ...); } -#[repr(packed)] -pub struct ProcessSerialNumber { - pub a: u32, - pub b: u32, -} - -#[repr(i32)] -pub enum WhichProcess { - kNoProcess = 0, - kSystemProcess, - kCurrentProcess, -} - -#[repr(i32)] -pub enum TransformProcess { - kProcessTransformToForegroundApplication = 1, - kProcessTransformToBackgroundApplication = 2, - kProcessTransformToUIElementApplication = 4, -} - -#[link(name = "ApplicationServices", kind = "framework")] -extern "C" { - pub fn TransformProcessType(psn: *const ProcessSerialNumber, transformState: u32) -> i32; -} - #[link(name = "ImageIO", kind = "framework")] extern "C" { pub static kUTTypePNG: ObjcId;