new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Tauri · Plugins and security · all subjects

plugin events

6 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Plugin lifecycle events

Plugins can hook into five lifecycle events: setup (plugin initialization, register mobile plugins, manage state, run background tasks), on_navigation (web view attempting navigation, validate or track URL changes), on_webview_ready (new window created, execute initialization scripts), on_event (event loop events, handle core events), and on_drop (plugin deconstruction).

Setup lifecycle hook code example

The setup hook receives app and api parameters and returns Result. Example shows using `app.manage()` to manage state and spawning a background thread that emits events: ```rust Builder::new("<plugin-name>") .setup(|app, api| { app.manage(DummyStore(Default::default())); let app_ = app.clone(); std::thread::spawn(move || { loop { app_.emit("tick", ()); std::thread::sleep(Duration::from_secs(1)); } }); Ok(()) }) ```

On_navigation lifecycle hook code example

The on_navigation hook receives window and url parameters and returns bool. Returning false cancels navigation. Example: ```rust Builder::new("<plugin-name>") .on_navigation(|window, url| { println!("window {} is navigating to {}", window.label(), url); url.scheme() != "forbidden" }) ```

On_webview_ready lifecycle hook code example

The on_webview_ready hook receives window parameter. Example shows listening for an event: ```rust Builder::new("<plugin-name>") .on_webview_ready(|window| { window.listen("content-loaded", |event| { println!("webview content has been loaded"); }); }) ```

On_event lifecycle hook for RunEvent handling

The on_event hook receives app and event parameters and can handle any RunEvent. Example shows handling ExitRequested and Exit events: ```rust Builder::new("<plugin-name>") .on_event(|app, event| { match event { RunEvent::ExitRequested { api, .. } => { api.prevent_exit(); } RunEvent::Exit => { let store = app.state::<DummyStore>(); write( app.path().app_local_data_dir().unwrap().join("store.json"), serde_json::to_string(&*store.0.lock().unwrap()).unwrap(), ).unwrap(); } _ => {} } }) ```

On_drop lifecycle hook code example

The on_drop hook receives app parameter and executes when plugin is destroyed. Example: ```rust Builder::new("<plugin-name>") .on_drop(|app| { // plugin has been destroyed... }) ```

Give your agent this brain