Skip to content

Commit 15358b1

Browse files
ChaostheorieCobaltamrbashir
authored
Expose event interface. fixes #2733 (#3321)
Co-authored-by: Cobalt <c0balt@disroot.org> Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
1 parent bff86ee commit 15358b1

4 files changed

Lines changed: 37 additions & 26 deletions

File tree

.changes/api-change-events.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
* **Breaking change**: Renamed `tauri::Event` to `tauri::RunEvent`
6+
* Exported `tauri::Event` and `tauri::EventHandler` so you can define a function and pass it to `Window::listen`

core/tauri/src/app.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
http::{Request as HttpRequest, Response as HttpResponse},
1818
webview::{WebviewAttributes, WindowBuilder},
1919
window::{PendingWindow, WindowEvent},
20-
Dispatch, ExitRequestedEventAction, RunEvent, Runtime,
20+
Dispatch, ExitRequestedEventAction, RunEvent as RuntimeRunEvent, Runtime,
2121
},
2222
scope::FsScope,
2323
sealed::{ManagerBase, RuntimeOrDispatch},
@@ -80,7 +80,7 @@ impl CloseRequestApi {
8080
/// An application event, triggered from the event loop.
8181
#[derive(Debug)]
8282
#[non_exhaustive]
83-
pub enum Event {
83+
pub enum RunEvent {
8484
/// Event loop is exiting.
8585
Exit,
8686
/// The app is about to exit
@@ -502,13 +502,18 @@ impl<R: Runtime> App<R> {
502502
/// });
503503
/// }
504504
/// ```
505-
pub fn run<F: FnMut(&AppHandle<R>, Event) + 'static>(mut self, mut callback: F) {
505+
pub fn run<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, mut callback: F) {
506506
let app_handle = self.handle();
507507
let manager = self.manager.clone();
508508
self.runtime.take().unwrap().run(move |event| match event {
509-
RunEvent::Exit => {
509+
RuntimeRunEvent::Exit => {
510510
app_handle.cleanup_before_exit();
511-
on_event_loop_event(&app_handle, RunEvent::Exit, &manager, Some(&mut callback));
511+
on_event_loop_event(
512+
&app_handle,
513+
RuntimeRunEvent::Exit,
514+
&manager,
515+
Some(&mut callback),
516+
);
512517
}
513518
_ => {
514519
on_event_loop_event(&app_handle, event, &manager, Some(&mut callback));
@@ -545,7 +550,7 @@ impl<R: Runtime> App<R> {
545550
&app_handle,
546551
event,
547552
&manager,
548-
Option::<&mut Box<dyn FnMut(&AppHandle<R>, Event)>>::None,
553+
Option::<&mut Box<dyn FnMut(&AppHandle<R>, RunEvent)>>::None,
549554
)
550555
})
551556
}
@@ -1187,30 +1192,30 @@ impl<R: Runtime> Builder<R> {
11871192
}
11881193
}
11891194

1190-
fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, Event) + 'static>(
1195+
fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
11911196
app_handle: &AppHandle<R>,
1192-
event: RunEvent,
1197+
event: RuntimeRunEvent,
11931198
manager: &WindowManager<R>,
11941199
callback: Option<&mut F>,
11951200
) {
1196-
if let RunEvent::WindowClose(label) = &event {
1201+
if let RuntimeRunEvent::WindowClose(label) = &event {
11971202
manager.on_window_close(label);
11981203
}
11991204

12001205
let event = match event {
1201-
RunEvent::Exit => Event::Exit,
1202-
RunEvent::ExitRequested { window_label, tx } => Event::ExitRequested {
1206+
RuntimeRunEvent::Exit => RunEvent::Exit,
1207+
RuntimeRunEvent::ExitRequested { window_label, tx } => RunEvent::ExitRequested {
12031208
window_label,
12041209
api: ExitRequestApi(tx),
12051210
},
1206-
RunEvent::CloseRequested { label, signal_tx } => Event::CloseRequested {
1211+
RuntimeRunEvent::CloseRequested { label, signal_tx } => RunEvent::CloseRequested {
12071212
label,
12081213
api: CloseRequestApi(signal_tx),
12091214
},
1210-
RunEvent::WindowClose(label) => Event::WindowClosed(label),
1211-
RunEvent::Ready => Event::Ready,
1212-
RunEvent::Resumed => Event::Resumed,
1213-
RunEvent::MainEventsCleared => Event::MainEventsCleared,
1215+
RuntimeRunEvent::WindowClose(label) => RunEvent::WindowClosed(label),
1216+
RuntimeRunEvent::Ready => RunEvent::Ready,
1217+
RuntimeRunEvent::Resumed => RunEvent::Resumed,
1218+
RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared,
12141219
_ => unimplemented!(),
12151220
};
12161221

core/tauri/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ pub type Result<T> = std::result::Result<T, Error>;
172172
/// A task to run on the main thread.
173173
pub type SyncTask = Box<dyn FnOnce() + Send>;
174174

175-
use crate::{
176-
event::{Event as EmittedEvent, EventHandler},
177-
runtime::window::PendingWindow,
178-
};
175+
use crate::runtime::window::PendingWindow;
179176
use serde::Serialize;
180177
use std::{collections::HashMap, fmt, sync::Arc};
181178

@@ -197,12 +194,14 @@ pub use {
197194
};
198195
pub use {
199196
self::app::WindowMenuEvent,
197+
self::event::{Event, EventHandler},
200198
self::runtime::menu::{CustomMenuItem, Menu, MenuEntry, MenuItem, Submenu},
201199
self::window::menu::MenuEvent,
202200
};
203201
pub use {
204202
self::app::{
205-
App, AppHandle, AssetResolver, Builder, CloseRequestApi, Event, GlobalWindowEvent, PathResolver,
203+
App, AppHandle, AssetResolver, Builder, CloseRequestApi, GlobalWindowEvent, PathResolver,
204+
RunEvent,
206205
},
207206
self::hooks::{
208207
Invoke, InvokeError, InvokeHandler, InvokeMessage, InvokePayload, InvokeResolver,
@@ -418,15 +417,15 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
418417
/// Listen to a global event.
419418
fn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
420419
where
421-
F: Fn(EmittedEvent) + Send + 'static,
420+
F: Fn(Event) + Send + 'static,
422421
{
423422
self.manager().listen(event.into(), None, handler)
424423
}
425424

426425
/// Listen to a global event only once.
427426
fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
428427
where
429-
F: Fn(EmittedEvent) + Send + 'static,
428+
F: Fn(Event) + Send + 'static,
430429
{
431430
self.manager().once(event.into(), None, handler)
432431
}

core/tauri/src/plugin.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
//! The Tauri plugin extension to expand Tauri functionality.
66
77
use crate::{
8-
runtime::Runtime, utils::config::PluginConfig, AppHandle, Event, Invoke, PageLoadPayload, Window,
8+
runtime::Runtime, utils::config::PluginConfig, AppHandle, Invoke, PageLoadPayload, RunEvent,
9+
Window,
910
};
1011
use serde_json::Value as JsonValue;
1112
use tauri_macros::default_runtime;
@@ -45,7 +46,7 @@ pub trait Plugin<R: Runtime>: Send {
4546

4647
/// Callback invoked when the event loop receives a new event.
4748
#[allow(unused_variables)]
48-
fn on_event(&mut self, app: &AppHandle<R>, event: &Event) {}
49+
fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {}
4950

5051
/// Extend commands to [`crate::Builder::invoke_handler`].
5152
#[allow(unused_variables)]
@@ -126,7 +127,7 @@ impl<R: Runtime> PluginStore<R> {
126127
}
127128

128129
/// Runs the on_event hook for all plugins in the store.
129-
pub(crate) fn on_event(&mut self, app: &AppHandle<R>, event: &Event) {
130+
pub(crate) fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {
130131
self
131132
.store
132133
.values_mut()

0 commit comments

Comments
 (0)