diff --git a/core/tauri/src/event/listener.rs b/core/tauri/src/event/listener.rs index ea73c3f59c5e..b565a22f1d6d 100644 --- a/core/tauri/src/event/listener.rs +++ b/core/tauri/src/event/listener.rs @@ -186,7 +186,18 @@ impl Listeners { if let Some(handlers) = lock.get(event) { let handlers = handlers .iter() - .filter(|h| h.1.window.as_ref().map(|w| filter(w)).unwrap_or(true)) + .filter(|h| { + h.1 + .window + .as_ref() + .map(|w| { + // clippy sees this as redundant closure but + // fixing it will result in a compiler error + #[allow(clippy::redundant_closure)] + filter(w) + }) + .unwrap_or(true) + }) .collect::>(); if !handlers.is_empty() { diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index 1f8f7cf12add..341e7ae9ecf0 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -552,7 +552,7 @@ pub trait Manager: sealed::ManagerBase { self.manager().package_info() } - /// Listen to a event triggered on any window ([`Window::trigger`] or [`Window::emit_and_trigger`]) or with [`Self::trigger_global`]. + /// Listen to an event emitted on any window. /// /// # Examples /// ``` @@ -561,7 +561,7 @@ pub trait Manager: sealed::ManagerBase { /// #[tauri::command] /// fn synchronize(window: tauri::Window) { /// // emits the synchronized event to all windows - /// window.emit_and_trigger("synchronized", ()); + /// window.emit("synchronized", ()); /// } /// /// tauri::Builder::default() @@ -618,10 +618,7 @@ pub trait Manager: sealed::ManagerBase { self.manager().once(event.into(), None, handler) } - /// 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`]. + /// Emits an event to all windows. /// /// # Examples /// ``` @@ -658,7 +655,7 @@ pub trait Manager: sealed::ManagerBase { .emit_filter(event, None, payload, |w| label == w.label()) } - /// Emits an event to the windows based on a filter. + /// Emits an event to specific windows based on a filter. /// /// # Examples /// ``` diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index d725f83e1d8d..719c772d5490 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -946,7 +946,37 @@ impl PartialEq for Window { } } -impl Manager for Window {} +impl Manager for Window { + fn emit(&self, event: &str, payload: S) -> crate::Result<()> { + self + .manager() + .emit_filter(event, Some(self.label()), payload, |w| { + w.has_js_listener(None, event) || w.has_js_listener(Some(self.label().into()), event) + })?; + Ok(()) + } + + fn emit_to( + &self, + label: &str, + event: &str, + payload: S, + ) -> crate::Result<()> { + self + .manager() + .emit_filter(event, Some(self.label()), payload, |w| label == w.label()) + } + + fn emit_filter(&self, event: &str, payload: S, filter: F) -> crate::Result<()> + where + S: Serialize + Clone, + F: Fn(&Window) -> bool, + { + self + .manager() + .emit_filter(event, Some(self.label()), payload, filter) + } +} impl ManagerBase for Window { fn manager(&self) -> &WindowManager { &self.manager @@ -2387,36 +2417,8 @@ impl Window { /// Event system APIs. impl Window { - /// Emits an event to the JavaScript listeners on the current window or globally. - /// - /// # Examples - /// ``` - /// use tauri::Manager; - /// - /// #[tauri::command] - /// fn download(window: tauri::Window) { - /// for i in 1..100 { - /// std::thread::sleep(std::time::Duration::from_millis(150)); - /// // emit a download progress event to all listeners registed in the webview - /// window.emit("download-progress", i); - /// } - /// } - /// ``` - pub fn emit(&self, event: &str, payload: S) -> crate::Result<()> { - self - .manager() - .emit_filter(event, Some(self.label()), payload, |w| { - w.has_js_listener(None, event) || w.has_js_listener(Some(self.label().into()), event) - })?; - Ok(()) - } - /// Listen to an event on this window. /// - /// This listener only receives events that are triggered using the - /// [`trigger`](Window#method.trigger) and [`emit_and_trigger`](Window#method.emit_and_trigger) methods or - /// the `emit` function from the window plugin (`@tauri-apps/plugin-window` package). - /// /// # Examples /// ``` /// use tauri::Manager; @@ -2468,7 +2470,7 @@ impl Window { self.manager.unlisten(handler_id) } - /// Listen to an event on this window a single time. + /// Listen to an event on this window only once. /// /// See [`Self::listen`] for more information. pub fn once(&self, event: impl Into, handler: F) -> EventHandler