|
@@ -611,18 +611,66 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
|
|
|
}
|
|
|
|
|
|
/// Emits a event to all windows.
|
|
|
+ ///
|
|
|
+ /// Only the webviews receives this event.
|
|
|
+ /// To trigger Rust listeners, use [`Self::trigger_global`], [`Window::trigger`] or [`Window::emit_and_trigger`].
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ /// ```
|
|
|
+ /// use tauri::Manager;
|
|
|
+ ///
|
|
|
+ /// #[tauri::command]
|
|
|
+ /// fn synchronize(app: tauri::AppHandle) {
|
|
|
+ /// // emits the synchronized event to all windows
|
|
|
+ /// app.emit_all("synchronized", ());
|
|
|
+ /// }
|
|
|
+ /// ```
|
|
|
fn emit_all<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
|
|
|
self.manager().emit_filter(event, None, payload, |_| true)
|
|
|
}
|
|
|
|
|
|
- /// Emits an event to a window with the specified label.
|
|
|
+ /// Emits an event to the window with the specified label.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ /// ```
|
|
|
+ /// use tauri::Manager;
|
|
|
+ ///
|
|
|
+ /// #[tauri::command]
|
|
|
+ /// fn download(app: tauri::AppHandle) {
|
|
|
+ /// for i in 1..100 {
|
|
|
+ /// std::thread::sleep(std::time::Duration::from_millis(150));
|
|
|
+ /// // emit a download progress event to the updater window
|
|
|
+ /// app.emit_to("updater", "download-progress", i);
|
|
|
+ /// }
|
|
|
+ /// }
|
|
|
+ /// ```
|
|
|
fn emit_to<S: Serialize + Clone>(&self, label: &str, event: &str, payload: S) -> Result<()> {
|
|
|
self
|
|
|
.manager()
|
|
|
.emit_filter(event, None, payload, |w| label == w.label())
|
|
|
}
|
|
|
|
|
|
- /// Listen to a global event.
|
|
|
+ /// Listen to a event triggered on any window ([`Window::trigger`] or [`Window::emit_and_trigger`]) or with [`Self::trigger_global`].
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ /// ```
|
|
|
+ /// use tauri::Manager;
|
|
|
+ ///
|
|
|
+ /// #[tauri::command]
|
|
|
+ /// fn synchronize(window: tauri::Window) {
|
|
|
+ /// // emits the synchronized event to all windows
|
|
|
+ /// window.emit_and_trigger("synchronized", ());
|
|
|
+ /// }
|
|
|
+ ///
|
|
|
+ /// tauri::Builder::default()
|
|
|
+ /// .setup(|app| {
|
|
|
+ /// app.listen_global("synchronized", |event| {
|
|
|
+ /// println!("app is in sync");
|
|
|
+ /// });
|
|
|
+ /// Ok(())
|
|
|
+ /// })
|
|
|
+ /// .invoke_handler(tauri::generate_handler![synchronize]);
|
|
|
+ /// ```
|
|
|
fn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
|
|
|
where
|
|
|
F: Fn(Event) + Send + 'static,
|
|
@@ -631,6 +679,8 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
|
|
|
}
|
|
|
|
|
|
/// Listen to a global event only once.
|
|
|
+ ///
|
|
|
+ /// See [`Self::listen_global`] for more information.
|
|
|
fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
|
|
|
where
|
|
|
F: FnOnce(Event) + Send + 'static,
|
|
@@ -638,12 +688,54 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
|
|
|
self.manager().once(event.into(), None, handler)
|
|
|
}
|
|
|
|
|
|
- /// Trigger a global event.
|
|
|
+ /// Trigger a global event to Rust listeners.
|
|
|
+ /// To send the events to the webview, see [`Self::emit_all`] and [`Self::emit_to`].
|
|
|
+ /// To trigger listeners registed on an specific window, see [`Window::trigger`].
|
|
|
+ /// To trigger all listeners, see [`Window::emit_and_trigger`].
|
|
|
+ ///
|
|
|
+ /// A global event does not have a source or target window attached.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ /// ```
|
|
|
+ /// use tauri::Manager;
|
|
|
+ ///
|
|
|
+ /// #[tauri::command]
|
|
|
+ /// fn download(app: tauri::AppHandle) {
|
|
|
+ /// for i in 1..100 {
|
|
|
+ /// std::thread::sleep(std::time::Duration::from_millis(150));
|
|
|
+ /// // emit a download progress event to all listeners registed in Rust
|
|
|
+ /// app.trigger_global("download-progress", Some(i.to_string()));
|
|
|
+ /// }
|
|
|
+ /// }
|
|
|
+ /// ```
|
|
|
fn trigger_global(&self, event: &str, data: Option<String>) {
|
|
|
self.manager().trigger(event, None, data)
|
|
|
}
|
|
|
|
|
|
/// Remove an event listener.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ /// ```
|
|
|
+ /// use tauri::Manager;
|
|
|
+ ///
|
|
|
+ /// tauri::Builder::default()
|
|
|
+ /// .setup(|app| {
|
|
|
+ /// let handle = app.handle();
|
|
|
+ /// let handler = app.listen_global("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(())
|
|
|
+ /// });
|
|
|
+ /// ```
|
|
|
fn unlisten(&self, handler_id: EventHandler) {
|
|
|
self.manager().unlisten(handler_id)
|
|
|
}
|