Is it possible to access managed state in event handler? #6823
-
I want to mutate state through events like sample code below : struct Task(Mutex<T>);
fn main() {
tauri::Builder::default()
.setup(|app| {
app.manage(Task(Default::default()));
let main_window = app.get_window("main").unwrap();
main_window.listen("set_start", |event| {
// access task here
//task.0.lock().unwrap().set_start();
});
main_window.listen("set_stop", |event| {
// access task here
//task.0.lock().unwrap().set_stop();
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
} but I don't find a proper solution I know I can do this by Commands according to official guide just curious is this can be achieved through events ? |
Beta Was this translation helpful? Give feedback.
Answered by
FabianLars
Jun 13, 2023
Replies: 1 comment
-
Sorry for the long delay. Yes, this is possible. First import the Manager trait via use tauri::Manager;
struct Task(Mutex<T>);
fn main() {
tauri::Builder::default()
.setup(|app| {
app.manage(Task(Default::default()));
let main_window = app.get_window("main").unwrap();
let handle = app.handle();
main_window.listen("set_start", move |event| { // note the move keyword
let my_state = handle.state::<Task<Mutex<T>>>(): // T must be the actual type
});
let handle = app.handle()
main_window.listen("set_stop", move |event| { // note the move keyword
let my_state: tauri::State<Task<Mutex<T>>> = handle.state(): // T must be the actual type
});
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
FabianLars
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry for the long delay. Yes, this is possible. First import the Manager trait via
use tauri::Manager;
. Then you can do use thestate()
methods to get the state. Using your code as a base: