Skip to content

Commit 4c4ab1e

Browse files
authored
fix(core): trigger tauri://* events to Rust listeners, closes #2901 (#2902)
1 parent 000d126 commit 4c4ab1e

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Emit `tauri://*` events to Rust listeners.

core/tauri/src/manager.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,11 @@ impl<R: Runtime> WindowManager<R> {
493493
crate::async_runtime::block_on(async move {
494494
let window = Window::new(manager.clone(), window, app_handle);
495495
let _ = match event {
496-
FileDropEvent::Hovered(paths) => window.emit("tauri://file-drop-hover", Some(paths)),
497-
FileDropEvent::Dropped(paths) => window.emit("tauri://file-drop", Some(paths)),
498-
FileDropEvent::Cancelled => window.emit("tauri://file-drop-cancelled", Some(())),
496+
FileDropEvent::Hovered(paths) => {
497+
window.emit_and_trigger("tauri://file-drop-hover", paths)
498+
}
499+
FileDropEvent::Dropped(paths) => window.emit_and_trigger("tauri://file-drop", paths),
500+
FileDropEvent::Cancelled => window.emit_and_trigger("tauri://file-drop-cancelled", ()),
499501
_ => unimplemented!(),
500502
};
501503
});
@@ -796,13 +798,13 @@ fn on_window_event<R: Runtime>(
796798
event: &WindowEvent,
797799
) -> crate::Result<()> {
798800
match event {
799-
WindowEvent::Resized(size) => window.emit(WINDOW_RESIZED_EVENT, Some(size))?,
800-
WindowEvent::Moved(position) => window.emit(WINDOW_MOVED_EVENT, Some(position))?,
801+
WindowEvent::Resized(size) => window.emit_and_trigger(WINDOW_RESIZED_EVENT, size)?,
802+
WindowEvent::Moved(position) => window.emit_and_trigger(WINDOW_MOVED_EVENT, position)?,
801803
WindowEvent::CloseRequested => {
802-
window.emit(WINDOW_CLOSE_REQUESTED_EVENT, Some(()))?;
804+
window.emit_and_trigger(WINDOW_CLOSE_REQUESTED_EVENT, ())?;
803805
}
804806
WindowEvent::Destroyed => {
805-
window.emit(WINDOW_DESTROYED_EVENT, Some(()))?;
807+
window.emit_and_trigger(WINDOW_DESTROYED_EVENT, ())?;
806808
let label = window.label();
807809
for window in manager.inner.windows.lock().unwrap().values() {
808810
window.eval(&format!(
@@ -811,24 +813,24 @@ fn on_window_event<R: Runtime>(
811813
))?;
812814
}
813815
}
814-
WindowEvent::Focused(focused) => window.emit(
816+
WindowEvent::Focused(focused) => window.emit_and_trigger(
815817
if *focused {
816818
WINDOW_FOCUS_EVENT
817819
} else {
818820
WINDOW_BLUR_EVENT
819821
},
820-
Some(()),
822+
(),
821823
)?,
822824
WindowEvent::ScaleFactorChanged {
823825
scale_factor,
824826
new_inner_size,
825827
..
826-
} => window.emit(
828+
} => window.emit_and_trigger(
827829
WINDOW_SCALE_FACTOR_CHANGED_EVENT,
828-
Some(ScaleFactorChanged {
830+
ScaleFactorChanged {
829831
scale_factor: *scale_factor,
830832
size: *new_inner_size,
831-
}),
833+
},
832834
)?,
833835
_ => unimplemented!(),
834836
}
@@ -843,5 +845,5 @@ struct ScaleFactorChanged {
843845
}
844846

845847
fn on_menu_event<R: Runtime>(window: &Window<R>, event: &MenuEvent) -> crate::Result<()> {
846-
window.emit(MENU_EVENT, Some(event.menu_item_id.clone()))
848+
window.emit_and_trigger(MENU_EVENT, event.menu_item_id.clone())
847849
}

core/tauri/src/updater/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,13 @@ pub(crate) fn listener<R: Runtime>(
461461
let body = updater.body.clone().unwrap_or_else(|| String::from(""));
462462

463463
// Emit `tauri://update-available`
464-
let _ = window.emit(
464+
let _ = window.emit_and_trigger(
465465
EVENT_UPDATE_AVAILABLE,
466-
Some(UpdateManifest {
466+
UpdateManifest {
467467
body,
468468
date: updater.date.clone(),
469469
version: updater.version.clone(),
470-
}),
470+
},
471471
);
472472

473473
// Listen for `tauri://update-install`
@@ -510,12 +510,12 @@ pub(crate) fn listener<R: Runtime>(
510510

511511
// Send a status update via `tauri://update-status` event.
512512
fn send_status_update<R: Runtime>(window: Window<R>, status: &str, error: Option<String>) {
513-
let _ = window.emit(
513+
let _ = window.emit_and_trigger(
514514
EVENT_STATUS_UPDATE,
515-
Some(StatusEvent {
515+
StatusEvent {
516516
error,
517517
status: String::from(status),
518-
}),
518+
},
519519
);
520520
}
521521

core/tauri/src/window.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,37 @@ impl<R: Runtime> Window<R> {
266266
&self.window.label
267267
}
268268

269-
/// Emits an event to the current window.
269+
/// Emits an event to both the JavaScript and the Rust listeners.
270+
pub fn emit_and_trigger<S: Serialize>(&self, event: &str, payload: S) -> crate::Result<()> {
271+
self.trigger(event, Some(serde_json::to_string(&payload)?));
272+
self.emit(event, payload)
273+
}
274+
275+
/// Emits an event to the JavaScript listeners on the current window.
276+
///
277+
/// The event is only delivered to listeners that used the `appWindow.listen` method on the @tauri-apps/api `window` module.
270278
pub fn emit<S: Serialize>(&self, event: &str, payload: S) -> crate::Result<()> {
271279
self.eval(&format!(
272280
"window['{}']({{event: {}, payload: {}}})",
273281
self.manager.event_emit_function_name(),
274282
serde_json::to_string(event)?,
275283
serde_json::to_value(payload)?,
276284
))?;
277-
278285
Ok(())
279286
}
280287

281-
/// Emits an event on all windows except this one.
288+
/// Emits an event to the JavaScript listeners on all windows except this one.
289+
///
290+
/// The event is only delivered to listeners that used the `appWindow.listen` function from the `@tauri-apps/api `window` module.
282291
pub fn emit_others<S: Serialize + Clone>(&self, event: &str, payload: S) -> crate::Result<()> {
283292
self.manager.emit_filter(event, payload, |w| w != self)
284293
}
285294

286295
/// Listen to an event on this window.
296+
///
297+
/// This listener only receives events that are triggered using the
298+
/// [`trigger`](Window#method.trigger) and [`emit_and_trigger`](Window#method.emit_and_trigger) methods or
299+
/// the `appWindow.emit` function from the @tauri-apps/api `window` module.
287300
pub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
288301
where
289302
F: Fn(Event) + Send + 'static,
@@ -297,7 +310,7 @@ impl<R: Runtime> Window<R> {
297310
self.manager.unlisten(handler_id)
298311
}
299312

300-
/// Listen to a an event on this window a single time.
313+
/// Listen to an event on this window a single time.
301314
pub fn once<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
302315
where
303316
F: Fn(Event) + Send + 'static,
@@ -306,7 +319,9 @@ impl<R: Runtime> Window<R> {
306319
self.manager.once(event.into(), Some(label), handler)
307320
}
308321

309-
/// Triggers an event on this window.
322+
/// Triggers an event to the Rust listeners on this window.
323+
///
324+
/// The event is only delivered to listeners that used the [`listen`](Window#method.listen) method.
310325
pub fn trigger(&self, event: &str, data: Option<String>) {
311326
let label = self.window.label.clone();
312327
self.manager.trigger(event, Some(label), data)

0 commit comments

Comments
 (0)