1

I am new to both Tauri and Rust, I'm trying to emit events to the frontend when certain menu items are clicked

fn main() {

    Builder::default()
        .menu(build_menu())
        .on_menu_event(|event| {
            match event.menu_item_id() {
                "new" => {
                    println!("New fired");
                    // Emit event here
                }
                "save" => {
                    println!("Save fired");
                }
                "open" => {
                    println!("Open fired")
                }
                "export" => {
                    println!("Export fired");
                }
                _ => {}
            }
        })
        .run(generate_context!())
        .expect("error while running tauri application");

}

I've been looking at the Tauri documentation a bunch and other SO questions but so far I can only figure out how emit events from setup

Example from tauri.app

fn main() {
  tauri::Builder::default()
    .setup(|app| {
      // `main` here is the window label; it is defined on the window creation or under `tauri.conf.json`
      // the default value is `main`. note that it must be unique
      let main_window = app.get_window("main").unwrap();

      // listen to the `event-name` (emitted on the `main` window)
      let id = main_window.listen("event-name", |event| {
        println!("got window event-name with payload {:?}", event.payload());
      });
      // unlisten to the event using the `id` returned on the `listen` function
      // an `once` API is also exposed on the `Window` struct
      main_window.unlisten(id);

      // emit the `event-name` event to the `main` window
      main_window.emit("event-name", Payload { message: "Tauri is awesome!".into() }).unwrap();
      Ok(())
    })
    .invoke_handler(tauri::generate_handler![init_process])
    .run(tauri::generate_context!())
    .expect("failed to run app");
}

  • To emit events from within the `on_menu_event` handler, you can use the `Window::emit` method to emit an event to the frontend. – Jishan Shaikh Apr 28 '23 at 04:02

1 Answers1

0

I think you may be able to get the window from the event in an on_menu_event handler like so :

fn main() {
    /*create menu "What" with submenu "Hello"*/
    let hello = CustomMenuItem::new("backend_hello".to_string(), "Hello");
    let submenu = Submenu::new("What", Menu::new().add_item(hello));
    let menu = Menu::new().add_submenu(submenu);

    tauri::Builder::default()
        .menu(menu)
        .on_menu_event(|event| {
            match event.menu_item_id() {
                "backend_hello" => {
                    /*emit event for frontend here*/
                    event.window().emit("frontend_hello", Payload { message: "hello world".to_string() }).unwrap();
                }
                _ => {}
            }
        })
        .invoke_handler(tauri::generate_handler![my_custom_command])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}