Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: macos: Always run TIS.. and NS.. functions on main thread #414

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
WIP: macos: Always run TIS.. and NS.. functions on main thread
Some functions can only be called from the main thread. If they are called from another thread, it can lead to panics. This likely is the cause for Tauri crashing with enigo. There are functions available to send functions to the main thread that then get executed there. We should use those so enigo can be used from other threads too

Fixes #153
Fixes tauri-apps/tauri#6421
Fixes #352
pentamassiv committed Mar 24, 2025
commit 2d67a1016fa35d7d35f2f68dcee3cd5ce29eb710
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ objc2-foundation = { version = "0.3", default-features = false, features = [
"objc2-core-foundation",
"NSGeometry",
] }
dispatch2 = "0.2"
foreign-types-shared = "0.3"

[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies]
4 changes: 4 additions & 0 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ use core_graphics::{
},
event_source::{CGEventSource, CGEventSourceStateID},
};
use dispatch2::run_on_main;
use foreign_types_shared::ForeignTypeRef as _;
use log::{debug, error, info};
use objc2::msg_send;
@@ -549,6 +550,9 @@ impl Enigo {

let held = (Vec::new(), Vec::new());

run_on_main(|mtm| {
// Do something on the main thread with the given marker
});
let mut event_flags = CGEventFlags::CGEventFlagNonCoalesced;
event_flags.set(CGEventFlags::from_bits_retain(0x2000_0000), true); // I don't know if this is needed or what this flag does. Correct events have it
// set so we also do it (until we know it is wrong)