浏览代码

refactor!: remove `GlobalWindowEvent` type (#8430)

* refactor!: remove `GlobalWindowEvent` type

* takee references instead
Amr Bashir 1 年之前
父节点
当前提交
2032228cad
共有 4 个文件被更改,包括 12 次插入34 次删除
  1. 5 0
      .changes/tauri-remove-global-window-event-type.md
  2. 4 24
      core/tauri/src/app.rs
  3. 1 4
      core/tauri/src/lib.rs
  4. 2 6
      core/tauri/src/manager/window.rs

+ 5 - 0
.changes/tauri-remove-global-window-event-type.md

@@ -0,0 +1,5 @@
+---
+'tauri': 'major:breaking'
+---
+
+Removed `GlobalWindowEvent` struct, and unpacked its field to be passed directly to `tauri::Builder::on_window_event`.

+ 4 - 24
core/tauri/src/app.rs

@@ -63,7 +63,7 @@ pub(crate) type GlobalMenuEventListener<T> = Box<dyn Fn(&T, crate::menu::MenuEve
 #[cfg(all(desktop, feature = "tray-icon"))]
 pub(crate) type GlobalTrayIconEventListener<T> =
   Box<dyn Fn(&T, crate::tray::TrayIconEvent) + Send + Sync>;
-pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>) + Send + Sync>;
+pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(&Window<R>, &WindowEvent) + Send + Sync>;
 /// A closure that is run when the Tauri application is setting up.
 pub type SetupHook<R> =
   Box<dyn FnOnce(&mut App<R>) -> Result<(), Box<dyn std::error::Error>> + Send>;
@@ -219,26 +219,6 @@ impl From<EventLoopMessage> for RunEvent {
   }
 }
 
-/// A window event that was triggered on the specified window.
-#[default_runtime(crate::Wry, wry)]
-#[derive(Debug)]
-pub struct GlobalWindowEvent<R: Runtime> {
-  pub(crate) event: WindowEvent,
-  pub(crate) window: Window<R>,
-}
-
-impl<R: Runtime> GlobalWindowEvent<R> {
-  /// The event payload.
-  pub fn event(&self) -> &WindowEvent {
-    &self.event
-  }
-
-  /// The window that the event belongs to.
-  pub fn window(&self) -> &Window<R> {
-    &self.window
-  }
-}
-
 /// The asset resolver is a helper to access the [`tauri_utils::assets::Assets`] interface.
 #[derive(Debug, Clone)]
 pub struct AssetResolver<R: Runtime> {
@@ -1304,18 +1284,18 @@ impl<R: Runtime> Builder<R> {
   /// # Examples
   /// ```
   /// tauri::Builder::default()
-  ///   .on_window_event(|event| match event.event() {
+  ///   .on_window_event(|window, event| match event {
   ///     tauri::WindowEvent::Focused(focused) => {
   ///       // hide window whenever it loses focus
   ///       if !focused {
-  ///         event.window().hide().unwrap();
+  ///         window.hide().unwrap();
   ///       }
   ///     }
   ///     _ => {}
   ///   });
   /// ```
   #[must_use]
-  pub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(
+  pub fn on_window_event<F: Fn(&Window<R>, &WindowEvent) + Send + Sync + 'static>(
     mut self,
     handler: F,
   ) -> Self {

+ 1 - 4
core/tauri/src/lib.rs

@@ -198,10 +198,7 @@ pub use self::utils::TitleBarStyle;
 
 pub use self::event::{Event, EventId};
 pub use {
-  self::app::{
-    App, AppHandle, AssetResolver, Builder, CloseRequestApi, GlobalWindowEvent, RunEvent,
-    WindowEvent,
-  },
+  self::app::{App, AppHandle, AssetResolver, Builder, CloseRequestApi, RunEvent, WindowEvent},
   self::manager::Asset,
   self::runtime::{
     webview::WebviewAttributes,

+ 2 - 6
core/tauri/src/manager/window.rs

@@ -28,8 +28,7 @@ use crate::{
   ipc::{InvokeHandler, InvokeResponder},
   pattern::PatternJavascript,
   window::PageLoadPayload,
-  AppHandle, EventLoopMessage, GlobalWindowEvent, Icon, Manager, Runtime, Scopes, Window,
-  WindowEvent,
+  AppHandle, EventLoopMessage, Icon, Manager, Runtime, Scopes, Window, WindowEvent,
 };
 
 use super::AppManager;
@@ -574,10 +573,7 @@ impl<R: Runtime> WindowManager<R> {
     window.on_window_event(move |event| {
       let _ = on_window_event(&window_, &manager, event);
       for handler in window_event_listeners.iter() {
-        handler(GlobalWindowEvent {
-          window: window_.clone(),
-          event: event.clone(),
-        });
+        handler(&window_, event);
       }
     });