Replies: 3 comments
-
I'd suggest looking into the |
Beta Was this translation helpful? Give feedback.
-
I use once_cell like this: // Declare global static variable
static APP_HANDLE: OnceCell<Mutex<AppHandle>> = OnceCell::new();
// Set APP_HANDLE
APP_HANDLE
.set(Mutex::new(app.app_handle().to_owned()))
.unwrap();
// Get APP_HANDLE
let app = APP_HANDLE.get().unwrap().lock();
if let Ok(app_handle) = app {
app_handle
.emit_all("plugin:iap|exception", ExceptionError { code, message })
.unwrap();
} To set |
Beta Was this translation helpful? Give feedback.
-
From the other comments, here's how I did it with Tauri v2 use std::sync::OnceLock; // OnceLock is part of std now
static APP_HANDLE: OnceLock<AppHandle> = OnceLock::new();
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let context = tauri::generate_context!();
let app = tauri::Builder::default()
// config omitted for brevity
.build(context)
.unwrap();
APP_HANDLE.set(app.app_handle().to_owned()).unwrap();
app.run(|_, _| {});
} Then I made a small helper function to use it: fn app_handle<'a>() -> &'a AppHandle {
APP_HANDLE.get().unwrap()
}
// Enjoy:
fn foo() {
let app_handle = app_handle();
} |
Beta Was this translation helpful? Give feedback.
-
I'm using windows api that listens to mouse and keyboard, like "SetWindowsHookExA" but this api needs a function address for callbacks, is there any way I can get AppHandle in one function? If there is a global reference to it, I searched the documentation but couldn't find the corresponding method, thanks a lot
Beta Was this translation helpful? Give feedback.
All reactions