tauri

Trait Listener

Source
pub trait Listener<R: Runtime>: ManagerBase<R> {
    // Required methods
    fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
       where F: Fn(Event) + Send + 'static;
    fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
       where F: FnOnce(Event) + Send + 'static;
    fn unlisten(&self, id: EventId);

    // Provided methods
    fn listen_any<F>(&self, event: impl Into<String>, handler: F) -> EventId
       where F: Fn(Event) + Send + 'static { ... }
    fn once_any<F>(&self, event: impl Into<String>, handler: F) -> EventId
       where F: FnOnce(Event) + Send + 'static { ... }
}
Expand description

Listen to events.

Required Methods§

Source

fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
where F: Fn(Event) + Send + 'static,

Listen to an emitted event on this manager.

§Examples
use tauri::{Manager, Listener, Emitter};

#[tauri::command]
fn synchronize(window: tauri::Window) {
  // emits the synchronized event to all windows
  window.emit("synchronized", ());
}

tauri::Builder::default()
  .setup(|app| {
    app.listen("synchronized", |event| {
      println!("app is in sync");
    });
    Ok(())
  })
  .invoke_handler(tauri::generate_handler![synchronize]);
Source

fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
where F: FnOnce(Event) + Send + 'static,

Listen to an event on this manager only once.

See Self::listen for more information.

Source

fn unlisten(&self, id: EventId)

Remove an event listener.

§Examples
use tauri::{Manager, Listener};

tauri::Builder::default()
  .setup(|app| {
    let handle = app.handle().clone();
    let handler = app.listen_any("ready", move |event| {
      println!("app is ready");

      // we no longer need to listen to the event
      // we also could have used `app.once_global` instead
      handle.unlisten(event.id());
    });

    // stop listening to the event when you do not need it anymore
    app.unlisten(handler);


    Ok(())
  });

Provided Methods§

Source

fn listen_any<F>(&self, event: impl Into<String>, handler: F) -> EventId
where F: Fn(Event) + Send + 'static,

Listen to an emitted event to any target.

§Examples
use tauri::{Manager, Emitter, Listener};

#[tauri::command]
fn synchronize(window: tauri::Window) {
  // emits the synchronized event to all windows
  window.emit("synchronized", ());
}

tauri::Builder::default()
  .setup(|app| {
    app.listen_any("synchronized", |event| {
      println!("app is in sync");
    });
    Ok(())
  })
  .invoke_handler(tauri::generate_handler![synchronize]);
Source

fn once_any<F>(&self, event: impl Into<String>, handler: F) -> EventId
where F: FnOnce(Event) + Send + 'static,

Listens once to an emitted event to any target .

See Self::listen_any for more information.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<R: Runtime> Listener<R> for App<R>

Source§

impl<R: Runtime> Listener<R> for AppHandle<R>

Source§

impl<R: Runtime> Listener<R> for Webview<R>

Source§

impl<R: Runtime> Listener<R> for WebviewWindow<R>

Source§

impl<R: Runtime> Listener<R> for Window<R>