diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 7d2805fad089..1375ab33c0f3 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -784,131 +784,6 @@ macro_rules! shared_app_impl { } } - /// Event system APIs. - impl $app { - /// Listen to an event on this app. - /// - /// # Examples - /// - /// ``` - /// tauri::Builder::default() - /// .setup(|app| { - /// app.listen("component-loaded", move |event| { - /// println!("window just loaded a component"); - /// }); - /// - /// Ok(()) - /// }); - /// ``` - pub fn listen(&self, event: impl Into, handler: F) -> EventId - where - F: Fn(Event) + Send + 'static, - { - self.manager.listen(event.into(), EventTarget::App, handler) - } - - /// Listen to an event on this app only once. - /// - /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) -> EventId - where - F: FnOnce(Event) + Send + 'static, - { - self.manager.once(event.into(), EventTarget::App, handler) - } - - /// Unlisten to an event on this app. - /// - /// # Examples - /// - /// ``` - /// tauri::Builder::default() - /// .setup(|app| { - /// let handler = app.listen("component-loaded", move |event| { - /// println!("app just loaded a component"); - /// }); - /// - /// // stop listening to the event when you do not need it anymore - /// app.unlisten(handler); - /// - /// Ok(()) - /// }); - /// ``` - pub fn unlisten(&self, id: EventId) { - self.manager.unlisten(id) - } - - /// Emits an event to all [targets](EventTarget). - /// - /// # Examples - /// ``` - /// #[tauri::command] - /// fn synchronize(app: tauri::AppHandle) { - /// // emits the synchronized event to all webviews - /// app.emit("synchronized", ()); - /// } - /// ``` - pub fn emit(&self, event: &str, payload: S) -> Result<()> { - self.manager.emit(event, payload) - } - - /// Emits an event to all [targets](EventTarget) matching the given target. - /// - /// # Examples - /// ``` - /// use tauri::EventTarget; - /// - /// #[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 - /// app.emit_to(EventTarget::any(), "download-progress", i); - /// // emit an event to listeners that used App::listen or AppHandle::listen - /// app.emit_to(EventTarget::app(), "download-progress", i); - /// // emit an event to any webview/window/webviewWindow matching the given label - /// app.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled - /// app.emit_to(EventTarget::labeled("updater"), "download-progress", i); - /// // emit an event to listeners that used WebviewWindow::listen - /// app.emit_to(EventTarget::webview_window("updater"), "download-progress", i); - /// } - /// } - /// ``` - pub fn emit_to(&self, target: I, event: &str, payload: S) -> Result<()> - where - I: Into, - S: Serialize + Clone, - { - self.manager.emit_to(target, event, payload) - } - - /// Emits an event to all [targets](EventTarget) based on the given filter. - /// - /// # Examples - /// ``` - /// use tauri::EventTarget; - /// - /// #[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_filter("download-progress", i, |t| match t { - /// EventTarget::WebviewWindow { label } => label == "main", - /// _ => false, - /// }); - /// } - /// } - /// ``` - pub fn emit_filter(&self, event: &str, payload: S, filter: F) -> Result<()> - where - S: Serialize + Clone, - F: Fn(&EventTarget) -> bool, - { - self.manager.emit_filter(event, payload, filter) - } - } - impl Listener for $app { /// Listen to an event on this app. /// @@ -930,7 +805,7 @@ macro_rules! shared_app_impl { where F: Fn(Event) + Send + 'static, { - self.listen(event.into(), handler) + self.manager.listen(event.into(), EventTarget::App, handler) } /// Listen to an event on this app only once. @@ -940,7 +815,7 @@ macro_rules! shared_app_impl { where F: FnOnce(Event) + Send + 'static, { - self.once(event, handler) + self.manager.once(event.into(), EventTarget::App, handler) } /// Unlisten to an event on this app. @@ -963,7 +838,7 @@ macro_rules! shared_app_impl { /// }); /// ``` fn unlisten(&self, id: EventId) { - self.unlisten(id) + self.manager.unlisten(id) } } @@ -981,7 +856,7 @@ macro_rules! shared_app_impl { /// } /// ``` fn emit(&self, event: &str, payload: S) -> Result<()> { - self.emit(event, payload) + self.manager.emit(event, payload) } /// Emits an event to all [targets](EventTarget) matching the given target. @@ -1011,7 +886,7 @@ macro_rules! shared_app_impl { I: Into, S: Serialize + Clone, { - self.emit_to(target, event, payload) + self.manager.emit_to(target, event, payload) } /// Emits an event to all [targets](EventTarget) based on the given filter. @@ -1037,7 +912,7 @@ macro_rules! shared_app_impl { S: Serialize + Clone, F: Fn(&EventTarget) -> bool, { - self.emit_filter(event, payload, filter) + self.manager.emit_filter(event, payload, filter) } } }; diff --git a/core/tauri/src/event/plugin.rs b/core/tauri/src/event/plugin.rs index 32db4a805db4..3857b0c7457f 100644 --- a/core/tauri/src/event/plugin.rs +++ b/core/tauri/src/event/plugin.rs @@ -10,7 +10,7 @@ use tauri_runtime::window::is_label_valid; use crate::plugin::{Builder, TauriPlugin}; use crate::{command, ipc::CallbackFn, EventId, Result, Runtime}; -use crate::{AppHandle, Webview}; +use crate::{AppHandle, Emitter, Webview}; use super::{is_event_name_valid, EventTarget}; diff --git a/core/tauri/src/manager/mod.rs b/core/tauri/src/manager/mod.rs index aad0dce7260c..5144ed191805 100644 --- a/core/tauri/src/manager/mod.rs +++ b/core/tauri/src/manager/mod.rs @@ -466,10 +466,6 @@ impl AppManager { self.listeners().listen(event, target, handler) } - pub fn unlisten(&self, id: EventId) { - self.listeners().unlisten(id) - } - pub fn once( &self, event: String, @@ -480,6 +476,10 @@ impl AppManager { self.listeners().once(event, target, handler) } + pub fn unlisten(&self, id: EventId) { + self.listeners().unlisten(id) + } + #[cfg_attr( feature = "tracing", tracing::instrument("app::emit", skip(self, payload)) @@ -499,6 +499,35 @@ impl AppManager { Ok(()) } + #[cfg_attr( + feature = "tracing", + tracing::instrument("app::emit::filter", skip(self, payload, filter)) + )] + pub fn emit_filter(&self, event: &str, payload: S, filter: F) -> crate::Result<()> + where + S: Serialize + Clone, + F: Fn(&EventTarget) -> bool, + { + assert_event_name_is_valid(event); + + #[cfg(feature = "tracing")] + let _span = tracing::debug_span!("emit::run").entered(); + let emit_args = EmitArgs::new(event, payload)?; + + let listeners = self.listeners(); + + listeners.emit_js_filter( + self.webview.webviews_lock().values(), + event, + &emit_args, + Some(&filter), + )?; + + listeners.emit_filter(emit_args, Some(filter))?; + + Ok(()) + } + #[cfg_attr( feature = "tracing", tracing::instrument("app::emit::to", skip(self, target, payload), fields(target)) @@ -531,35 +560,6 @@ impl AppManager { } } - #[cfg_attr( - feature = "tracing", - tracing::instrument("app::emit::filter", skip(self, payload, filter)) - )] - pub fn emit_filter(&self, event: &str, payload: S, filter: F) -> crate::Result<()> - where - S: Serialize + Clone, - F: Fn(&EventTarget) -> bool, - { - assert_event_name_is_valid(event); - - #[cfg(feature = "tracing")] - let _span = tracing::debug_span!("emit::run").entered(); - let emit_args = EmitArgs::new(event, payload)?; - - let listeners = self.listeners(); - - listeners.emit_js_filter( - self.webview.webviews_lock().values(), - event, - &emit_args, - Some(&filter), - )?; - - listeners.emit_filter(emit_args, Some(filter))?; - - Ok(()) - } - pub fn get_window(&self, label: &str) -> Option> { self.window.windows_lock().get(label).cloned() } diff --git a/core/tauri/src/manager/webview.rs b/core/tauri/src/manager/webview.rs index f1bd79033226..777f6bf4fc7e 100644 --- a/core/tauri/src/manager/webview.rs +++ b/core/tauri/src/manager/webview.rs @@ -25,7 +25,7 @@ use crate::{ pattern::PatternJavascript, sealed::ManagerBase, webview::PageLoadPayload, - AppHandle, EventLoopMessage, EventTarget, Manager, Runtime, Scopes, Webview, Window, + AppHandle, Emitter, EventLoopMessage, EventTarget, Manager, Runtime, Scopes, Webview, Window, }; use super::{ diff --git a/core/tauri/src/manager/window.rs b/core/tauri/src/manager/window.rs index 17c8e3ea754d..1988059720e8 100644 --- a/core/tauri/src/manager/window.rs +++ b/core/tauri/src/manager/window.rs @@ -17,8 +17,8 @@ use tauri_runtime::{ }; use crate::{ - app::GlobalWindowEventListener, image::Image, sealed::ManagerBase, AppHandle, EventLoopMessage, - EventTarget, Manager, Runtime, Scopes, Window, WindowEvent, + app::GlobalWindowEventListener, image::Image, sealed::ManagerBase, AppHandle, Emitter, + EventLoopMessage, EventTarget, Manager, Runtime, Scopes, Window, WindowEvent, }; const WINDOW_RESIZED_EVENT: &str = "tauri://resize"; diff --git a/core/tauri/src/webview/mod.rs b/core/tauri/src/webview/mod.rs index 643dadaf109f..e4145f317fd5 100644 --- a/core/tauri/src/webview/mod.rs +++ b/core/tauri/src/webview/mod.rs @@ -1462,8 +1462,7 @@ tauri::Builder::default() } } -/// Event system APIs. -impl Webview { +impl Listener for Webview { /// Listen to an event on this webview. /// /// # Examples @@ -1471,7 +1470,7 @@ impl Webview { feature = "unstable", doc = r####" ``` -use tauri::Manager; +use tauri::Listener; tauri::Builder::default() .setup(|app| { @@ -1485,7 +1484,7 @@ tauri::Builder::default() ``` "#### )] - pub fn listen(&self, event: impl Into, handler: F) -> EventId + fn listen(&self, event: impl Into, handler: F) -> EventId where F: Fn(Event) + Send + 'static, { @@ -1498,180 +1497,22 @@ tauri::Builder::default() ) } - /// Unlisten to an event on this webview. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::Manager; - -tauri::Builder::default() - .setup(|app| { - let webview = app.get_webview("main").unwrap(); - let webview_ = webview.clone(); - let handler = webview.listen("component-loaded", move |event| { - println!("webview just loaded a component"); - - // we no longer need to listen to the event - // we also could have used `webview.once` instead - webview_.unlisten(event.id()); - }); - - // stop listening to the event when you do not need it anymore - webview.unlisten(handler); - - Ok(()) - }); -``` - "#### - )] - pub fn unlisten(&self, id: EventId) { - self.manager.unlisten(id) - } - /// Listen to an event on this webview only once. /// /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) -> EventId + fn once(&self, event: impl Into, handler: F) -> EventId where F: FnOnce(Event) + Send + 'static, { self.manager.once( event.into(), - EventTarget::Window { + EventTarget::Webview { label: self.label().to_string(), }, handler, ) } - /// Emits an event to all [targets](EventTarget). - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -#[tauri::command] -fn synchronize(webview: tauri::Webview) { - // emits the synchronized event to all webviews - webview.emit("synchronized", ()); -} - ``` - "#### - )] - pub fn emit(&self, event: &str, payload: S) -> crate::Result<()> { - self.manager.emit(event, payload) - } - - /// Emits an event to all [targets](EventTarget) matching the given target. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::EventTarget; - -#[tauri::command] -fn download(webview: tauri::Webview) { - for i in 1..100 { - std::thread::sleep(std::time::Duration::from_millis(150)); - // emit a download progress event to all listeners - webview.emit_to(EventTarget::any(), "download-progress", i); - // emit an event to listeners that used App::listen or AppHandle::listen - webview.emit_to(EventTarget::app(), "download-progress", i); - // emit an event to any webview/window/webviewWindow matching the given label - webview.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled - webview.emit_to(EventTarget::labeled("updater"), "download-progress", i); - // emit an event to listeners that used WebviewWindow::listen - webview.emit_to(EventTarget::webview_window("updater"), "download-progress", i); - } -} -``` -"#### - )] - pub fn emit_to(&self, target: I, event: &str, payload: S) -> crate::Result<()> - where - I: Into, - S: Serialize + Clone, - { - self.manager.emit_to(target, event, payload) - } - - /// Emits an event to all [targets](EventTarget) based on the given filter. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::EventTarget; - -#[tauri::command] -fn download(webview: tauri::Webview) { - for i in 1..100 { - std::thread::sleep(std::time::Duration::from_millis(150)); - // emit a download progress event to the updater window - webview.emit_filter("download-progress", i, |t| match t { - EventTarget::WebviewWindow { label } => label == "main", - _ => false, - }); - } -} - ``` - "#### - )] - pub fn emit_filter(&self, event: &str, payload: S, filter: F) -> crate::Result<()> - where - S: Serialize + Clone, - F: Fn(&EventTarget) -> bool, - { - self.manager.emit_filter(event, payload, filter) - } -} - -impl Listener for Webview { - /// Listen to an event on this webview. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::Listener; - -tauri::Builder::default() - .setup(|app| { - let webview = app.get_webview("main").unwrap(); - webview.listen("component-loaded", move |event| { - println!("webview just loaded a component"); - }); - - Ok(()) - }); -``` - "#### - )] - fn listen(&self, event: impl Into, handler: F) -> EventId - where - F: Fn(Event) + Send + 'static, - { - self.listen(event, handler) - } - - /// Listen to an event on this webview only once. - /// - /// See [`Self::listen`] for more information. - fn once(&self, event: impl Into, handler: F) -> EventId - where - F: FnOnce(Event) + Send + 'static, - { - self.once(event, handler) - } - /// Unlisten to an event on this webview. /// /// # Examples @@ -1702,7 +1543,7 @@ tauri::Builder::default() "#### )] fn unlisten(&self, id: EventId) { - self.unlisten(id) + self.manager.unlisten(id) } } @@ -1725,7 +1566,7 @@ fn synchronize(webview: tauri::Webview) { "#### )] fn emit(&self, event: &str, payload: S) -> crate::Result<()> { - self.emit(event, payload) + self.manager.emit(event, payload) } /// Emits an event to all [targets](EventTarget) matching the given target. @@ -1760,7 +1601,7 @@ fn download(webview: tauri::Webview) { I: Into, S: Serialize + Clone, { - self.emit_to(target, event, payload) + self.manager.emit_to(target, event, payload) } /// Emits an event to all [targets](EventTarget) based on the given filter. @@ -1791,7 +1632,7 @@ fn download(webview: tauri::Webview) { S: Serialize + Clone, F: Fn(&EventTarget) -> bool, { - self.emit_filter(event, payload, filter) + self.manager.emit_filter(event, payload, filter) } } diff --git a/core/tauri/src/webview/webview_window.rs b/core/tauri/src/webview/webview_window.rs index 88ae0651fcde..f810f7e252b0 100644 --- a/core/tauri/src/webview/webview_window.rs +++ b/core/tauri/src/webview/webview_window.rs @@ -1703,153 +1703,6 @@ impl WebviewWindow { } } -/// Event system APIs. -impl WebviewWindow { - /// Listen to an event on this webview window. - /// - /// # Examples - /// - /// ``` - /// use tauri::Manager; - /// - /// tauri::Builder::default() - /// .setup(|app| { - /// let webview_window = app.get_webview_window("main").unwrap(); - /// webview_window.listen("component-loaded", move |event| { - /// println!("window just loaded a component"); - /// }); - /// - /// Ok(()) - /// }); - /// ``` - pub fn listen(&self, event: impl Into, handler: F) -> EventId - where - F: Fn(Event) + Send + 'static, - { - self.manager().listen( - event.into(), - EventTarget::WebviewWindow { - label: self.label().to_string(), - }, - handler, - ) - } - - /// Listen to an event on this window webview only once. - /// - /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) -> EventId - where - F: FnOnce(Event) + Send + 'static, - { - self.manager().once( - event.into(), - EventTarget::WebviewWindow { - label: self.label().to_string(), - }, - handler, - ) - } - - /// Unlisten to an event on this webview window. - /// - /// # Examples - /// ``` - /// use tauri::Manager; - /// - /// tauri::Builder::default() - /// .setup(|app| { - /// let webview_window = app.get_webview_window("main").unwrap(); - /// let webview_window_ = webview_window.clone(); - /// let handler = webview_window.listen("component-loaded", move |event| { - /// println!("webview_window just loaded a component"); - /// - /// // we no longer need to listen to the event - /// // we also could have used `webview_window.once` instead - /// webview_window_.unlisten(event.id()); - /// }); - /// - /// // stop listening to the event when you do not need it anymore - /// webview_window.unlisten(handler); - /// - /// Ok(()) - /// }); - /// ``` - pub fn unlisten(&self, id: EventId) { - self.manager().unlisten(id) - } - - /// Emits an event to all [targets](EventTarget). - /// - /// # Examples - /// ``` - /// #[tauri::command] - /// fn synchronize(window: tauri::WebviewWindow) { - /// // emits the synchronized event to all webviews - /// window.emit("synchronized", ()); - /// } - /// ``` - pub fn emit(&self, event: &str, payload: S) -> crate::Result<()> { - self.manager().emit(event, payload) - } - - /// Emits an event to all [targets](EventTarget) matching the given target. - /// - /// # Examples - /// ``` - /// use tauri::EventTarget; - /// - /// #[tauri::command] - /// fn download(window: tauri::WebviewWindow) { - /// for i in 1..100 { - /// std::thread::sleep(std::time::Duration::from_millis(150)); - /// // emit a download progress event to all listeners - /// window.emit_to(EventTarget::any(), "download-progress", i); - /// // emit an event to listeners that used App::listen or AppHandle::listen - /// window.emit_to(EventTarget::app(), "download-progress", i); - /// // emit an event to any webview/window/webviewWindow matching the given label - /// window.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled - /// window.emit_to(EventTarget::labeled("updater"), "download-progress", i); - /// // emit an event to listeners that used WebviewWindow::listen - /// window.emit_to(EventTarget::webview_window("updater"), "download-progress", i); - /// } - /// } - /// ``` - pub fn emit_to(&self, target: I, event: &str, payload: S) -> crate::Result<()> - where - I: Into, - S: Serialize + Clone, - { - self.manager().emit_to(target, event, payload) - } - - /// Emits an event to all [targets](EventTarget) based on the given filter. - /// - /// # Examples - /// ``` - /// use tauri::EventTarget; - /// - /// #[tauri::command] - /// fn download(window: tauri::WebviewWindow) { - /// for i in 1..100 { - /// std::thread::sleep(std::time::Duration::from_millis(150)); - /// // emit a download progress event to the updater window - /// window.emit_filter("download-progress", i, |t| match t { - /// EventTarget::WebviewWindow { label } => label == "main", - /// _ => false, - /// }); - /// } - /// } - /// ``` - pub fn emit_filter(&self, event: &str, payload: S, filter: F) -> crate::Result<()> - where - S: Serialize + Clone, - F: Fn(&EventTarget) -> bool, - { - self.manager().emit_filter(event, payload, filter) - } -} - impl Listener for WebviewWindow { /// Listen to an event on this webview window. /// diff --git a/core/tauri/src/window/mod.rs b/core/tauri/src/window/mod.rs index 84ebd5385f94..42c3d28bc09e 100644 --- a/core/tauri/src/window/mod.rs +++ b/core/tauri/src/window/mod.rs @@ -2006,8 +2006,7 @@ pub struct ProgressBarState { pub progress: Option, } -/// Event system APIs. -impl Window { +impl Listener for Window { /// Listen to an event on this window. /// /// # Examples @@ -2015,7 +2014,7 @@ impl Window { feature = "unstable", doc = r####" ``` -use tauri::Manager; +use tauri::Listener; tauri::Builder::default() .setup(|app| { @@ -2029,7 +2028,7 @@ tauri::Builder::default() ``` "#### )] - pub fn listen(&self, event: impl Into, handler: F) -> EventId + fn listen(&self, event: impl Into, handler: F) -> EventId where F: Fn(Event) + Send + 'static, { @@ -2042,43 +2041,10 @@ tauri::Builder::default() ) } - /// Unlisten to an event on this window. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::Manager; - -tauri::Builder::default() - .setup(|app| { - let window = app.get_window("main").unwrap(); - let window_ = window.clone(); - let handler = window.listen("component-loaded", move |event| { - println!("window just loaded a component"); - - // we no longer need to listen to the event - // we also could have used `window.once` instead - window_.unlisten(event.id()); - }); - - // stop listening to the event when you do not need it anymore - window.unlisten(handler); - - Ok(()) - }); -``` - "#### - )] - pub fn unlisten(&self, id: EventId) { - self.manager.unlisten(id) - } - /// Listen to an event on this window only once. /// /// See [`Self::listen`] for more information. - pub fn once(&self, event: impl Into, handler: F) -> EventId + fn once(&self, event: impl Into, handler: F) -> EventId where F: FnOnce(Event) + Send + 'static, { @@ -2091,131 +2057,6 @@ tauri::Builder::default() ) } - /// Emits an event to all [targets](EventTarget). - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -#[tauri::command] -fn synchronize(window: tauri::Window) { - // emits the synchronized event to all webviews - window.emit("synchronized", ()); -} - ``` - "#### - )] - pub fn emit(&self, event: &str, payload: S) -> crate::Result<()> { - self.manager.emit(event, payload) - } - - /// Emits an event to all [targets](EventTarget) matching the given target. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::EventTarget; - -#[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 - window.emit_to(EventTarget::any(), "download-progress", i); - // emit an event to listeners that used App::listen or AppHandle::listen - window.emit_to(EventTarget::app(), "download-progress", i); - // emit an event to any webview/window/webviewWindow matching the given label - window.emit_to("updater", "download-progress", i); // similar to using EventTarget::labeled - window.emit_to(EventTarget::labeled("updater"), "download-progress", i); - // emit an event to listeners that used WebviewWindow::listen - window.emit_to(EventTarget::webview_window("updater"), "download-progress", i); - } -} -``` -"#### - )] - pub fn emit_to(&self, target: I, event: &str, payload: S) -> crate::Result<()> - where - I: Into, - S: Serialize + Clone, - { - self.manager.emit_to(target, event, payload) - } - - /// Emits an event to all [targets](EventTarget) based on the given filter. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::EventTarget; - -#[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 the updater window - window.emit_filter("download-progress", i, |t| match t { - EventTarget::WebviewWindow { label } => label == "main", - _ => false, - }); - } -} - ``` - "#### - )] - pub fn emit_filter(&self, event: &str, payload: S, filter: F) -> crate::Result<()> - where - S: Serialize + Clone, - F: Fn(&EventTarget) -> bool, - { - self.manager.emit_filter(event, payload, filter) - } -} - -impl Listener for Window { - /// Listen to an event on this window. - /// - /// # Examples - #[cfg_attr( - feature = "unstable", - doc = r####" -``` -use tauri::Listener; - -tauri::Builder::default() - .setup(|app| { - let window = app.get_window("main").unwrap(); - window.listen("component-loaded", move |event| { - println!("window just loaded a component"); - }); - - Ok(()) - }); -``` - "#### - )] - fn listen(&self, event: impl Into, handler: F) -> EventId - where - F: Fn(Event) + Send + 'static, - { - self.listen(event, handler) - } - - /// Listen to an event on this window only once. - /// - /// See [`Self::listen`] for more information. - fn once(&self, event: impl Into, handler: F) -> EventId - where - F: FnOnce(Event) + Send + 'static, - { - self.once(event, handler) - } - /// Unlisten to an event on this window. /// /// # Examples @@ -2246,7 +2087,7 @@ tauri::Builder::default() "#### )] fn unlisten(&self, id: EventId) { - self.unlisten(id) + self.manager.unlisten(id) } } @@ -2269,7 +2110,7 @@ fn synchronize(window: tauri::Window) { "#### )] fn emit(&self, event: &str, payload: S) -> crate::Result<()> { - self.emit(event, payload) + self.manager.emit(event, payload) } /// Emits an event to all [targets](EventTarget) matching the given target. @@ -2304,7 +2145,7 @@ fn download(window: tauri::Window) { I: Into, S: Serialize + Clone, { - self.emit_to(target, event, payload) + self.manager.emit_to(target, event, payload) } /// Emits an event to all [targets](EventTarget) based on the given filter. @@ -2335,7 +2176,7 @@ fn download(window: tauri::Window) { S: Serialize + Clone, F: Fn(&EventTarget) -> bool, { - self.emit_filter(event, payload, filter) + self.manager.emit_filter(event, payload, filter) } } diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index e9c95e73ed64..ac9ed47971d6 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -356,12 +356,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cfg_aliases" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" - [[package]] name = "cfg_aliases" version = "0.2.0" @@ -378,7 +372,7 @@ dependencies = [ "iana-time-zone", "num-traits", "serde", - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -1152,7 +1146,7 @@ version = "0.18.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bb0228f477c0900c880fd78c8759b95c7636dbd7842707f49e132378aa2acdc" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro-crate 2.0.2", "proc-macro-error", "proc-macro2", @@ -1257,6 +1251,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.8" @@ -2814,7 +2814,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071916a85d1db274b4ed57af3a14afb66bd836ae7f82ebb6f1fd3455107830d9" dependencies = [ "bytemuck", - "cfg_aliases 0.2.0", + "cfg_aliases", "cocoa", "core-graphics", "foreign-types", @@ -2954,7 +2954,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a2d580ff6a20c55dfb86be5f9c238f67835d0e81cbdea8bf5680e0897320331" dependencies = [ "cfg-expr", - "heck", + "heck 0.4.1", "pkg-config", "toml 0.8.2", "version-compare", @@ -3017,18 +3017,19 @@ checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" [[package]] name = "tauri" -version = "2.0.0-beta.14" +version = "2.0.0-beta.17" dependencies = [ "anyhow", "bytes", "cocoa", "dirs-next", + "dunce", "embed_plist", "futures-util", "getrandom 0.2.12", "glob", "gtk", - "heck", + "heck 0.5.0", "http", "http-range", "image", @@ -3061,18 +3062,18 @@ dependencies = [ "webkit2gtk", "webview2-com", "window-vibrancy", - "windows 0.54.0", + "windows 0.56.0", ] [[package]] name = "tauri-build" -version = "2.0.0-beta.11" +version = "2.0.0-beta.13" dependencies = [ "anyhow", "cargo_toml", "dirs-next", "glob", - "heck", + "heck 0.5.0", "json-patch", "quote", "schemars", @@ -3088,7 +3089,7 @@ dependencies = [ [[package]] name = "tauri-codegen" -version = "2.0.0-beta.11" +version = "2.0.0-beta.13" dependencies = [ "base64 0.22.0", "brotli", @@ -3113,9 +3114,9 @@ dependencies = [ [[package]] name = "tauri-macros" -version = "2.0.0-beta.11" +version = "2.0.0-beta.13" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", "syn 2.0.52", @@ -3125,7 +3126,7 @@ dependencies = [ [[package]] name = "tauri-plugin" -version = "2.0.0-beta.11" +version = "2.0.0-beta.13" dependencies = [ "anyhow", "glob", @@ -3151,7 +3152,7 @@ dependencies = [ [[package]] name = "tauri-runtime" -version = "2.0.0-beta.11" +version = "2.0.0-beta.14" dependencies = [ "dpi", "gtk", @@ -3163,12 +3164,12 @@ dependencies = [ "tauri-utils", "thiserror", "url", - "windows 0.54.0", + "windows 0.56.0", ] [[package]] name = "tauri-runtime-wry" -version = "2.0.0-beta.11" +version = "2.0.0-beta.14" dependencies = [ "cocoa", "gtk", @@ -3184,13 +3185,13 @@ dependencies = [ "url", "webkit2gtk", "webview2-com", - "windows 0.54.0", + "windows 0.56.0", "wry", ] [[package]] name = "tauri-utils" -version = "2.0.0-beta.11" +version = "2.0.0-beta.13" dependencies = [ "aes-gcm", "brotli", @@ -3199,7 +3200,7 @@ dependencies = [ "dunce", "getrandom 0.2.12", "glob", - "heck", + "heck 0.5.0", "html5ever", "infer", "json-patch", @@ -3893,16 +3894,16 @@ dependencies = [ [[package]] name = "webview2-com" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38d5949fc3f537e90240c3e4f78dda2fa0431b671d50845a2f582173ef8a1201" +checksum = "5c914dd492a52f0377bef56fd1b6e74a79090f9ee631d625d5b505a00e4538b6" dependencies = [ "webview2-com-macros", "webview2-com-sys", - "windows 0.54.0", - "windows-core 0.54.0", - "windows-implement", - "windows-interface", + "windows 0.56.0", + "windows-core 0.56.0", + "windows-implement 0.56.0", + "windows-interface 0.56.0", ] [[package]] @@ -3918,13 +3919,13 @@ dependencies = [ [[package]] name = "webview2-com-sys" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd1eaa1be63d6fdcadf893c40d7d53c889a6342b3a94930d34e6964d5bb7e8db" +checksum = "2a46bcf03482ec28eeb764ca788f67998cde4213adfbbfa90462622058530f5e" dependencies = [ "thiserror", - "windows 0.54.0", - "windows-core 0.54.0", + "windows 0.56.0", + "windows-core 0.56.0", ] [[package]] @@ -3978,7 +3979,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efc5cf48f83140dcaab716eeaea345f9e93d0018fb81162753a3f76c3397b538" dependencies = [ "windows-core 0.53.0", - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -3988,9 +3989,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9252e5725dbed82865af151df558e754e4a3c2c30818359eb17465f1346a1b49" dependencies = [ "windows-core 0.54.0", - "windows-implement", - "windows-interface", - "windows-targets 0.52.4", + "windows-implement 0.53.0", + "windows-interface 0.53.0", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1de69df01bdf1ead2f4ac895dc77c9351aefff65b2f3db429a343f9cbf05e132" +dependencies = [ + "windows-core 0.56.0", + "windows-targets 0.52.5", ] [[package]] @@ -3999,7 +4010,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4009,7 +4020,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9dcc5b895a6377f1ab9fa55acedab1fd5ac0db66ad1e6c7f47e28a22e446a5dd" dependencies = [ "windows-result", - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4019,7 +4030,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12661b9c89351d684a50a8a643ce5f608e20243b9fb84687800163429f161d65" dependencies = [ "windows-result", - "windows-targets 0.52.4", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-core" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4698e52ed2d08f8658ab0c39512a7c00ee5fe2688c65f8c0a4f06750d729f2a6" +dependencies = [ + "windows-implement 0.56.0", + "windows-interface 0.56.0", + "windows-result", + "windows-targets 0.52.5", ] [[package]] @@ -4033,6 +4056,17 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "windows-implement" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.52", +] + [[package]] name = "windows-interface" version = "0.53.0" @@ -4044,13 +4078,24 @@ dependencies = [ "syn 2.0.52", ] +[[package]] +name = "windows-interface" +version = "0.56.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.52", +] + [[package]] name = "windows-result" -version = "0.1.0" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd19df78e5168dfb0aedc343d1d1b8d422ab2db6756d2dc3fef75035402a3f64" +checksum = "749f0da9cc72d82e600d8d2e44cadd0b9eedb9038f71a1c58556ac1c5791813b" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4077,7 +4122,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4112,17 +4157,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "windows_aarch64_gnullvm 0.52.4", - "windows_aarch64_msvc 0.52.4", - "windows_i686_gnu 0.52.4", - "windows_i686_msvc 0.52.4", - "windows_x86_64_gnu 0.52.4", - "windows_x86_64_gnullvm 0.52.4", - "windows_x86_64_msvc 0.52.4", + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", ] [[package]] @@ -4131,7 +4177,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75aa004c988e080ad34aff5739c39d0312f4684699d6d71fc8a198d057b8b9b4" dependencies = [ - "windows-targets 0.52.4", + "windows-targets 0.52.5", ] [[package]] @@ -4148,9 +4194,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] name = "windows_aarch64_msvc" @@ -4166,9 +4212,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" [[package]] name = "windows_i686_gnu" @@ -4184,9 +4230,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" [[package]] name = "windows_i686_msvc" @@ -4202,9 +4254,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" [[package]] name = "windows_x86_64_gnu" @@ -4220,9 +4272,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" [[package]] name = "windows_x86_64_gnullvm" @@ -4238,9 +4290,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" [[package]] name = "windows_x86_64_msvc" @@ -4256,9 +4308,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" [[package]] name = "winnow" @@ -4291,13 +4343,12 @@ dependencies = [ [[package]] name = "wry" -version = "0.39.0" +version = "0.39.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eca9d50437c04fc67e82c196ddd31d8e35794150713ae2d647f3a58c7f45d1a" +checksum = "6e180ac2740d6cb4d5cec0abf63eacbea90f1b7e5e3803043b13c1c84c4b7884" dependencies = [ - "base64 0.21.7", + "base64 0.22.0", "block", - "cfg_aliases 0.1.1", "cocoa", "core-graphics", "crossbeam-channel", @@ -4326,7 +4377,8 @@ dependencies = [ "webkit2gtk", "webkit2gtk-sys", "webview2-com", - "windows 0.54.0", + "windows 0.56.0", + "windows-core 0.56.0", "windows-version", "x11-dl", ] diff --git a/examples/api/src-tauri/src/lib.rs b/examples/api/src-tauri/src/lib.rs index 9b558a2b4dc3..87fadd562995 100644 --- a/examples/api/src-tauri/src/lib.rs +++ b/examples/api/src-tauri/src/lib.rs @@ -12,7 +12,7 @@ use serde::Serialize; use tauri::{ ipc::Channel, webview::{PageLoadEvent, WebviewWindowBuilder}, - App, AppHandle, Manager, RunEvent, Runtime, WebviewUrl, + App, AppHandle, Emitter, Listener, Manager, RunEvent, Runtime, WebviewUrl, }; use tauri_plugin_sample::{PingRequest, SampleExt}; diff --git a/examples/multiwindow/main.rs b/examples/multiwindow/main.rs index ce7ba50626db..e36650293207 100644 --- a/examples/multiwindow/main.rs +++ b/examples/multiwindow/main.rs @@ -4,7 +4,7 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use tauri::{webview::PageLoadEvent, WebviewWindowBuilder}; +use tauri::{webview::PageLoadEvent, Listener, WebviewWindowBuilder}; use tauri_utils::acl::ExecutionContext; fn main() { diff --git a/examples/parent-window/main.rs b/examples/parent-window/main.rs index 0804433e4b4f..c95617e40966 100644 --- a/examples/parent-window/main.rs +++ b/examples/parent-window/main.rs @@ -4,7 +4,7 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use tauri::{webview::PageLoadEvent, WebviewUrl, WebviewWindowBuilder}; +use tauri::{webview::PageLoadEvent, Listener, WebviewUrl, WebviewWindowBuilder}; use tauri_utils::acl::ExecutionContext; fn main() {