From 15358b1895487cb9c258a8ca4d2336b4215e2a8f Mon Sep 17 00:00:00 2001 From: Cobalt Date: Fri, 4 Feb 2022 21:44:12 +0100 Subject: [PATCH] Expose event interface. fixes #2733 (#3321) Co-authored-by: Cobalt Co-authored-by: Amr Bashir --- .changes/api-change-events.md | 6 ++++++ core/tauri/src/app.rs | 37 ++++++++++++++++++++--------------- core/tauri/src/lib.rs | 13 ++++++------ core/tauri/src/plugin.rs | 7 ++++--- 4 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 .changes/api-change-events.md diff --git a/.changes/api-change-events.md b/.changes/api-change-events.md new file mode 100644 index 00000000000..df0b1f5563d --- /dev/null +++ b/.changes/api-change-events.md @@ -0,0 +1,6 @@ +--- +"tauri": patch +--- + +* **Breaking change**: Renamed `tauri::Event` to `tauri::RunEvent` +* Exported `tauri::Event` and `tauri::EventHandler` so you can define a function and pass it to `Window::listen` diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 2bc0a8812eb..ae84009c8a3 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -17,7 +17,7 @@ use crate::{ http::{Request as HttpRequest, Response as HttpResponse}, webview::{WebviewAttributes, WindowBuilder}, window::{PendingWindow, WindowEvent}, - Dispatch, ExitRequestedEventAction, RunEvent, Runtime, + Dispatch, ExitRequestedEventAction, RunEvent as RuntimeRunEvent, Runtime, }, scope::FsScope, sealed::{ManagerBase, RuntimeOrDispatch}, @@ -80,7 +80,7 @@ impl CloseRequestApi { /// An application event, triggered from the event loop. #[derive(Debug)] #[non_exhaustive] -pub enum Event { +pub enum RunEvent { /// Event loop is exiting. Exit, /// The app is about to exit @@ -502,13 +502,18 @@ impl App { /// }); /// } /// ``` - pub fn run, Event) + 'static>(mut self, mut callback: F) { + pub fn run, RunEvent) + 'static>(mut self, mut callback: F) { let app_handle = self.handle(); let manager = self.manager.clone(); self.runtime.take().unwrap().run(move |event| match event { - RunEvent::Exit => { + RuntimeRunEvent::Exit => { app_handle.cleanup_before_exit(); - on_event_loop_event(&app_handle, RunEvent::Exit, &manager, Some(&mut callback)); + on_event_loop_event( + &app_handle, + RuntimeRunEvent::Exit, + &manager, + Some(&mut callback), + ); } _ => { on_event_loop_event(&app_handle, event, &manager, Some(&mut callback)); @@ -545,7 +550,7 @@ impl App { &app_handle, event, &manager, - Option::<&mut Box, Event)>>::None, + Option::<&mut Box, RunEvent)>>::None, ) }) } @@ -1187,30 +1192,30 @@ impl Builder { } } -fn on_event_loop_event, Event) + 'static>( +fn on_event_loop_event, RunEvent) + 'static>( app_handle: &AppHandle, - event: RunEvent, + event: RuntimeRunEvent, manager: &WindowManager, callback: Option<&mut F>, ) { - if let RunEvent::WindowClose(label) = &event { + if let RuntimeRunEvent::WindowClose(label) = &event { manager.on_window_close(label); } let event = match event { - RunEvent::Exit => Event::Exit, - RunEvent::ExitRequested { window_label, tx } => Event::ExitRequested { + RuntimeRunEvent::Exit => RunEvent::Exit, + RuntimeRunEvent::ExitRequested { window_label, tx } => RunEvent::ExitRequested { window_label, api: ExitRequestApi(tx), }, - RunEvent::CloseRequested { label, signal_tx } => Event::CloseRequested { + RuntimeRunEvent::CloseRequested { label, signal_tx } => RunEvent::CloseRequested { label, api: CloseRequestApi(signal_tx), }, - RunEvent::WindowClose(label) => Event::WindowClosed(label), - RunEvent::Ready => Event::Ready, - RunEvent::Resumed => Event::Resumed, - RunEvent::MainEventsCleared => Event::MainEventsCleared, + RuntimeRunEvent::WindowClose(label) => RunEvent::WindowClosed(label), + RuntimeRunEvent::Ready => RunEvent::Ready, + RuntimeRunEvent::Resumed => RunEvent::Resumed, + RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared, _ => unimplemented!(), }; diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index 0e65ba27f01..105468a65cb 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -172,10 +172,7 @@ pub type Result = std::result::Result; /// A task to run on the main thread. pub type SyncTask = Box; -use crate::{ - event::{Event as EmittedEvent, EventHandler}, - runtime::window::PendingWindow, -}; +use crate::runtime::window::PendingWindow; use serde::Serialize; use std::{collections::HashMap, fmt, sync::Arc}; @@ -197,12 +194,14 @@ pub use { }; pub use { self::app::WindowMenuEvent, + self::event::{Event, EventHandler}, self::runtime::menu::{CustomMenuItem, Menu, MenuEntry, MenuItem, Submenu}, self::window::menu::MenuEvent, }; pub use { self::app::{ - App, AppHandle, AssetResolver, Builder, CloseRequestApi, Event, GlobalWindowEvent, PathResolver, + App, AppHandle, AssetResolver, Builder, CloseRequestApi, GlobalWindowEvent, PathResolver, + RunEvent, }, self::hooks::{ Invoke, InvokeError, InvokeHandler, InvokeMessage, InvokePayload, InvokeResolver, @@ -418,7 +417,7 @@ pub trait Manager: sealed::ManagerBase { /// Listen to a global event. fn listen_global(&self, event: impl Into, handler: F) -> EventHandler where - F: Fn(EmittedEvent) + Send + 'static, + F: Fn(Event) + Send + 'static, { self.manager().listen(event.into(), None, handler) } @@ -426,7 +425,7 @@ pub trait Manager: sealed::ManagerBase { /// Listen to a global event only once. fn once_global(&self, event: impl Into, handler: F) -> EventHandler where - F: Fn(EmittedEvent) + Send + 'static, + F: Fn(Event) + Send + 'static, { self.manager().once(event.into(), None, handler) } diff --git a/core/tauri/src/plugin.rs b/core/tauri/src/plugin.rs index c3b7a3c55c9..2648d23454b 100644 --- a/core/tauri/src/plugin.rs +++ b/core/tauri/src/plugin.rs @@ -5,7 +5,8 @@ //! The Tauri plugin extension to expand Tauri functionality. use crate::{ - runtime::Runtime, utils::config::PluginConfig, AppHandle, Event, Invoke, PageLoadPayload, Window, + runtime::Runtime, utils::config::PluginConfig, AppHandle, Invoke, PageLoadPayload, RunEvent, + Window, }; use serde_json::Value as JsonValue; use tauri_macros::default_runtime; @@ -45,7 +46,7 @@ pub trait Plugin: Send { /// Callback invoked when the event loop receives a new event. #[allow(unused_variables)] - fn on_event(&mut self, app: &AppHandle, event: &Event) {} + fn on_event(&mut self, app: &AppHandle, event: &RunEvent) {} /// Extend commands to [`crate::Builder::invoke_handler`]. #[allow(unused_variables)] @@ -126,7 +127,7 @@ impl PluginStore { } /// Runs the on_event hook for all plugins in the store. - pub(crate) fn on_event(&mut self, app: &AppHandle, event: &Event) { + pub(crate) fn on_event(&mut self, app: &AppHandle, event: &RunEvent) { self .store .values_mut()