diff --git a/.changes/simplify-tag-label-usage.md b/.changes/simplify-tag-label-usage.md index 986a5c604..38ca6a143 100644 --- a/.changes/simplify-tag-label-usage.md +++ b/.changes/simplify-tag-label-usage.md @@ -2,4 +2,22 @@ "tauri": patch --- -Simplify usage of app event and window label types. +Simplify usage of app event and window label types. The following functions now +accept references the `Tag` can be borrowed as. This means an `&str` can now be +accepted for functions like `Window::emit`. This is a breaking change for the +following items, which now need to take a reference. Additionally, type inference +for `&"event".into()` will no longer work, but `&"event".to_string()` will. The +solution for this is to now just pass `"event"` because `Borrow` is implemented +for the default event type `String`. + +* **Breaking:** `Window::emit` now accepts `Borrow` for the event. +* **Breaking:** `Window::emit_others` now accepts `Borrow` for the event +* **Breaking:** `Window::trigger` now accepts `Borrow` for the event. +* **Breaking:** `Manager::emit_all` now accepts `Borrow` for the event. +* **Breaking:** `Manager::emit_to` now accepts `Borrow` for both the event and window label. +* **Breaking:** `Manager::trigger_global` now accepts `Borrow` for the event. +* **Breaking:** `Manager::get_window` now accepts `Borrow` for the window label. +* _(internal):_ `trait tauri::runtime::tag::TagRef` helper for accepting tag references. + Any time you want to accept a tag reference, that trait will handle requiring the reference + to have all the necessary bounds, and generate errors when the exposed function doesn't + set a bound like `P::Event: Borrow`. diff --git a/core/tauri/src/event.rs b/core/tauri/src/event.rs index 469ea9a16..371407006 100644 --- a/core/tauri/src/event.rs +++ b/core/tauri/src/event.rs @@ -192,10 +192,14 @@ impl Listeners { } /// Triggers the given global event with its payload. - pub(crate) fn trigger(&self, event: &E, window: Option, payload: Option) - where - E: TagRef + ?Sized, + pub(crate) fn trigger( + &self, + event: &E, + window: Option, + payload: Option, + ) where Event: Borrow, + E: TagRef, { let mut maybe_pending = false; match self.inner.handlers.try_lock() { diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index baf2b9ad8..ee606589a 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -139,24 +139,27 @@ pub trait Manager: sealed::ManagerBase

{ } /// Emits a event to all windows. - fn emit_all(&self, event: &E, payload: Option) -> Result<()> + fn emit_all(&self, event: &E, payload: Option) -> Result<()> where - E: TagRef + ?Sized, + P::Event: Borrow, + E: TagRef, S: Serialize + Clone, { self.manager().emit_filter(event, payload, |_| true) } /// Emits an event to a window with the specified label. - fn emit_to( + fn emit_to( &self, label: &L, event: &E, payload: Option, ) -> Result<()> where - L: TagRef + ?Sized, - E: TagRef + ?Sized, + P::Label: Borrow, + P::Event: Borrow, + L: TagRef, + E: TagRef, { self .manager() @@ -193,10 +196,10 @@ pub trait Manager: sealed::ManagerBase

{ } /// Trigger a global event. - fn trigger_global(&self, event: &E, data: Option) + fn trigger_global(&self, event: &E, data: Option) where - E: TagRef + ?Sized, P::Event: Borrow, + E: TagRef, { self.manager().trigger(event, None, data) } @@ -207,10 +210,10 @@ pub trait Manager: sealed::ManagerBase

{ } /// Fetch a single window from the manager. - fn get_window(&self, label: &L) -> Option> + fn get_window(&self, label: &L) -> Option> where - L: TagRef + ?Sized, P::Label: Borrow, + L: TagRef, { self.manager().get_window(label) } diff --git a/core/tauri/src/runtime/manager.rs b/core/tauri/src/runtime/manager.rs index 3b8bab6ea..3404585c0 100644 --- a/core/tauri/src/runtime/manager.rs +++ b/core/tauri/src/runtime/manager.rs @@ -493,9 +493,15 @@ impl WindowManager

{ window } - pub fn emit_filter(&self, event: &E, payload: Option, filter: F) -> crate::Result<()> + pub fn emit_filter( + &self, + event: &E, + payload: Option, + filter: F, + ) -> crate::Result<()> where - E: TagRef + ?Sized, + P::Event: Borrow, + E: TagRef, S: Serialize + Clone, F: Fn(&Window

) -> bool, { @@ -519,10 +525,10 @@ impl WindowManager

{ self.inner.listeners.unlisten(handler_id) } - pub fn trigger(&self, event: &E, window: Option, data: Option) + pub fn trigger(&self, event: &E, window: Option, data: Option) where - E: TagRef + ?Sized, P::Event: Borrow, + E: TagRef, { self.inner.listeners.trigger(event, window, data) } @@ -578,10 +584,10 @@ impl WindowManager

{ .remove(&uuid) } - pub fn get_window(&self, label: &L) -> Option> + pub fn get_window(&self, label: &L) -> Option> where - L: TagRef + ?Sized, P::Label: Borrow, + L: TagRef, { self.windows_lock().get(label).cloned() } diff --git a/core/tauri/src/runtime/tag.rs b/core/tauri/src/runtime/tag.rs index 0ea085bfe..0359828bd 100644 --- a/core/tauri/src/runtime/tag.rs +++ b/core/tauri/src/runtime/tag.rs @@ -91,11 +91,17 @@ impl Tag for T where /// * [`ToOwned`] to make sure we can clone it into the owned tag in specific cases. /// * [`PartialEq`] so that we can compare refs to the owned tags easily. /// * [`Hash`] + [`Eq`] because we want to be able to use a ref as a key to internal hashmaps. -pub trait TagRef: Display + ToOwned + PartialEq + Eq + Hash {} +pub trait TagRef: Display + ToOwned + PartialEq + Eq + Hash +where + T: std::borrow::Borrow, +{ +} /// Automatically implement [`TagRef`] for all types that fit the requirements. -impl TagRef for R where - R: Display + ToOwned + PartialEq + Eq + Hash + ?Sized +impl TagRef for R +where + T: std::borrow::Borrow, + R: Display + ToOwned + PartialEq + Eq + Hash + ?Sized, { } diff --git a/core/tauri/src/runtime/window.rs b/core/tauri/src/runtime/window.rs index 2ca01b617..e5a265ee1 100644 --- a/core/tauri/src/runtime/window.rs +++ b/core/tauri/src/runtime/window.rs @@ -196,9 +196,14 @@ pub(crate) mod export { &self.window.label } - pub(crate) fn emit_internal(&self, event: &E, payload: Option) -> crate::Result<()> + pub(crate) fn emit_internal( + &self, + event: &E, + payload: Option, + ) -> crate::Result<()> where - E: TagRef + ?Sized, + P::Event: Borrow, + E: TagRef, S: Serialize, { let js_payload = match payload { @@ -218,20 +223,22 @@ pub(crate) mod export { } /// Emits an event to the current window. - pub fn emit(&self, event: &E, payload: Option) -> crate::Result<()> + pub fn emit(&self, event: &E, payload: Option) -> crate::Result<()> where - E: TagRef + ?Sized, + P::Event: Borrow, + E: TagRef, S: Serialize, { self.emit_internal(event, payload) } /// Emits an event on all windows except this one. - pub fn emit_others + ?Sized, S: Serialize + Clone>( - &self, - event: &E, - payload: Option, - ) -> crate::Result<()> { + pub fn emit_others(&self, event: &E, payload: Option) -> crate::Result<()> + where + P::Event: Borrow, + E: TagRef, + S: Serialize + Clone, + { self.manager.emit_filter(event, payload, |w| w != self) } @@ -254,10 +261,10 @@ pub(crate) mod export { } /// Triggers an event on this window. - pub fn trigger(&self, event: &E, data: Option) + pub fn trigger(&self, event: &E, data: Option) where - E: TagRef + ?Sized, P::Event: Borrow, + E: TagRef, { let label = self.window.label.clone(); self.manager.trigger(event, Some(label), data)