Skip to content

Is it possible to access managed state in event handler? #6823

Closed Answered by FabianLars
xieqiaoyu asked this question in Q&A
Discussion options

You must be logged in to vote

Sorry for the long delay. Yes, this is possible. First import the Manager trait via use tauri::Manager;. Then you can do use the state() methods to get the state. Using your code as a base:

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()

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by FabianLars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants