浏览代码

Expose event interface. fixes #2733 (#3321)

Co-authored-by: Cobalt <c0balt@disroot.org>
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Cobalt 3 年之前
父节点
当前提交
15358b1895
共有 4 个文件被更改,包括 37 次插入26 次删除
  1. 6 0
      .changes/api-change-events.md
  2. 21 16
      core/tauri/src/app.rs
  3. 6 7
      core/tauri/src/lib.rs
  4. 4 3
      core/tauri/src/plugin.rs

+ 6 - 0
.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`

+ 21 - 16
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<R: Runtime> App<R> {
   ///   });
   /// }
   /// ```
-  pub fn run<F: FnMut(&AppHandle<R>, Event) + 'static>(mut self, mut callback: F) {
+  pub fn run<F: FnMut(&AppHandle<R>, 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<R: Runtime> App<R> {
         &app_handle,
         event,
         &manager,
-        Option::<&mut Box<dyn FnMut(&AppHandle<R>, Event)>>::None,
+        Option::<&mut Box<dyn FnMut(&AppHandle<R>, RunEvent)>>::None,
       )
     })
   }
@@ -1187,30 +1192,30 @@ impl<R: Runtime> Builder<R> {
   }
 }
 
-fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, Event) + 'static>(
+fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
   app_handle: &AppHandle<R>,
-  event: RunEvent,
+  event: RuntimeRunEvent,
   manager: &WindowManager<R>,
   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!(),
   };
 

+ 6 - 7
core/tauri/src/lib.rs

@@ -172,10 +172,7 @@ pub type Result<T> = std::result::Result<T, Error>;
 /// A task to run on the main thread.
 pub type SyncTask = Box<dyn FnOnce() + Send>;
 
-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<R: Runtime>: sealed::ManagerBase<R> {
   /// Listen to a global event.
   fn listen_global<F>(&self, event: impl Into<String>, 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<R: Runtime>: sealed::ManagerBase<R> {
   /// Listen to a global event only once.
   fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
   where
-    F: Fn(EmittedEvent) + Send + 'static,
+    F: Fn(Event) + Send + 'static,
   {
     self.manager().once(event.into(), None, handler)
   }

+ 4 - 3
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<R: Runtime>: Send {
 
   /// Callback invoked when the event loop receives a new event.
   #[allow(unused_variables)]
-  fn on_event(&mut self, app: &AppHandle<R>, event: &Event) {}
+  fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {}
 
   /// Extend commands to [`crate::Builder::invoke_handler`].
   #[allow(unused_variables)]
@@ -126,7 +127,7 @@ impl<R: Runtime> PluginStore<R> {
   }
 
   /// Runs the on_event hook for all plugins in the store.
-  pub(crate) fn on_event(&mut self, app: &AppHandle<R>, event: &Event) {
+  pub(crate) fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {
     self
       .store
       .values_mut()