|
@@ -266,7 +266,15 @@ impl<R: Runtime> Window<R> {
|
|
|
&self.window.label
|
|
|
}
|
|
|
|
|
|
- /// Emits an event to the current window.
|
|
|
+ /// Emits an event to both the JavaScript and the Rust listeners.
|
|
|
+ pub fn emit_and_trigger<S: Serialize>(&self, event: &str, payload: S) -> crate::Result<()> {
|
|
|
+ self.trigger(event, Some(serde_json::to_string(&payload)?));
|
|
|
+ self.emit(event, payload)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Emits an event to the JavaScript listeners on the current window.
|
|
|
+ ///
|
|
|
+ /// The event is only delivered to listeners that used the `appWindow.listen` method on the @tauri-apps/api `window` module.
|
|
|
pub fn emit<S: Serialize>(&self, event: &str, payload: S) -> crate::Result<()> {
|
|
|
self.eval(&format!(
|
|
|
"window['{}']({{event: {}, payload: {}}})",
|
|
@@ -274,16 +282,21 @@ impl<R: Runtime> Window<R> {
|
|
|
serde_json::to_string(event)?,
|
|
|
serde_json::to_value(payload)?,
|
|
|
))?;
|
|
|
-
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
- /// Emits an event on all windows except this one.
|
|
|
+ /// Emits an event to the JavaScript listeners on all windows except this one.
|
|
|
+ ///
|
|
|
+ /// The event is only delivered to listeners that used the `appWindow.listen` function from the `@tauri-apps/api `window` module.
|
|
|
pub fn emit_others<S: Serialize + Clone>(&self, event: &str, payload: S) -> crate::Result<()> {
|
|
|
self.manager.emit_filter(event, payload, |w| w != self)
|
|
|
}
|
|
|
|
|
|
/// 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 `appWindow.emit` function from the @tauri-apps/api `window` module.
|
|
|
pub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
|
|
|
where
|
|
|
F: Fn(Event) + Send + 'static,
|
|
@@ -297,7 +310,7 @@ impl<R: Runtime> Window<R> {
|
|
|
self.manager.unlisten(handler_id)
|
|
|
}
|
|
|
|
|
|
- /// Listen to a an event on this window a single time.
|
|
|
+ /// Listen to an event on this window a single time.
|
|
|
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
|
|
|
where
|
|
|
F: Fn(Event) + Send + 'static,
|
|
@@ -306,7 +319,9 @@ impl<R: Runtime> Window<R> {
|
|
|
self.manager.once(event.into(), Some(label), handler)
|
|
|
}
|
|
|
|
|
|
- /// Triggers an event on this window.
|
|
|
+ /// Triggers an event to the Rust listeners on this window.
|
|
|
+ ///
|
|
|
+ /// The event is only delivered to listeners that used the [`listen`](Window#method.listen) method.
|
|
|
pub fn trigger(&self, event: &str, data: Option<String>) {
|
|
|
let label = self.window.label.clone();
|
|
|
self.manager.trigger(event, Some(label), data)
|