Skip to content

Commit

Permalink
This actually seems to work..?
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksvedang committed Jun 12, 2024
1 parent 5ddfea5 commit 6030229
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Check failure on line 393 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

failed to resolve: could not find `apple` in `native`

Check failure on line 393 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, wasm32-unknown-unknown)

failed to resolve: could not find `apple` in `native`

Check failure on line 393 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-unknown-linux-gnu)

failed to resolve: could not find `apple` in `native`

Check failure on line 393 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, armv7-unknown-linux-gnueabihf)

failed to resolve: could not find `apple` in `native`

Check failure on line 393 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, aarch64-unknown-linux-gnu)

failed to resolve: could not find `apple` in `native`

Check failure on line 393 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

failed to resolve: could not find `apple` in `native`
}
30 changes: 5 additions & 25 deletions src/native/apple/apple_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,54 +569,34 @@ 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];
}
}
}

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];
}
Expand Down
25 changes: 0 additions & 25 deletions src/native/apple/frameworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6030229

Please sign in to comment.