فهرست منبع

feat(core): add API to send wry window message to the event loop (#2339)

* feat(core): add API to send wry window message to the event loop

* expose types
Lucas Fernandes Nogueira 4 سال پیش
والد
کامیت
15566cfd64
3فایلهای تغییر یافته به همراه38 افزوده شده و 16 حذف شده
  1. 1 1
      .changes/tauri-empty-window.md
  2. 25 15
      core/tauri-runtime-wry/src/lib.rs
  3. 12 0
      core/tauri/src/app.rs

+ 1 - 1
.changes/tauri-empty-window.md

@@ -4,4 +4,4 @@
 "tauri-runtime-wry": patch
 ---
 
-Allow creation of empty Window with `create_tao_window()` on the AppHandler.
+Allow creation of empty Window with `create_tao_window()` and management with `send_tao_window_event()` on the AppHandler.

+ 25 - 15
core/tauri-runtime-wry/src/lib.rs

@@ -50,7 +50,7 @@ use wry::{
     event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget},
     global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager},
     monitor::MonitorHandle,
-    window::{Fullscreen, Icon as WindowIcon, UserAttentionType as WryUserAttentionType, WindowId},
+    window::{Fullscreen, Icon as WindowIcon, UserAttentionType as WryUserAttentionType},
   },
   webview::{
     FileDropEvent as WryFileDropEvent, RpcRequest as WryRpcRequest, RpcResponse, WebContext,
@@ -58,7 +58,7 @@ use wry::{
   },
 };
 
-pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder};
+pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder, WindowId};
 
 #[cfg(target_os = "windows")]
 use wry::webview::WebviewExtWindows;
@@ -134,7 +134,7 @@ struct EventLoopContext {
 }
 
 #[derive(Debug, Clone)]
-struct GlobalShortcutWrapper(GlobalShortcut);
+pub struct GlobalShortcutWrapper(GlobalShortcut);
 
 unsafe impl Send for GlobalShortcutWrapper {}
 
@@ -412,7 +412,7 @@ impl From<Position> for PositionWrapper {
 }
 
 #[derive(Debug, Clone)]
-struct UserAttentionTypeWrapper(WryUserAttentionType);
+pub struct UserAttentionTypeWrapper(WryUserAttentionType);
 
 impl From<UserAttentionType> for UserAttentionTypeWrapper {
   fn from(request_type: UserAttentionType) -> UserAttentionTypeWrapper {
@@ -626,12 +626,12 @@ impl From<FileDropEventWrapper> for FileDropEvent {
 }
 
 #[cfg(target_os = "macos")]
-struct NSWindow(*mut std::ffi::c_void);
+pub struct NSWindow(*mut std::ffi::c_void);
 #[cfg(target_os = "macos")]
 unsafe impl Send for NSWindow {}
 
 #[cfg(windows)]
-struct Hwnd(HWND);
+pub struct Hwnd(HWND);
 #[cfg(windows)]
 unsafe impl Send for Hwnd {}
 
@@ -642,7 +642,7 @@ unsafe impl Send for Hwnd {}
   target_os = "netbsd",
   target_os = "openbsd"
 ))]
-struct GtkWindow(gtk::ApplicationWindow);
+pub struct GtkWindow(gtk::ApplicationWindow);
 #[cfg(any(
   target_os = "linux",
   target_os = "dragonfly",
@@ -653,7 +653,7 @@ struct GtkWindow(gtk::ApplicationWindow);
 unsafe impl Send for GtkWindow {}
 
 #[derive(Debug, Clone)]
-enum WindowMessage {
+pub enum WindowMessage {
   // Getters
   ScaleFactor(Sender<f64>),
   InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
@@ -714,7 +714,7 @@ enum WindowMessage {
 }
 
 #[derive(Debug, Clone)]
-enum WebviewMessage {
+pub enum WebviewMessage {
   EvaluateScript(String),
   #[allow(dead_code)]
   WebviewEvent(WebviewEvent),
@@ -723,13 +723,13 @@ enum WebviewMessage {
 
 #[allow(dead_code)]
 #[derive(Debug, Clone)]
-enum WebviewEvent {
+pub enum WebviewEvent {
   Focused(bool),
 }
 
 #[cfg(feature = "system-tray")]
 #[derive(Clone)]
-pub(crate) enum TrayMessage {
+pub enum TrayMessage {
   UpdateItem(u16, menu::MenuUpdate),
   UpdateIcon(Icon),
   #[cfg(target_os = "macos")]
@@ -737,7 +737,7 @@ pub(crate) enum TrayMessage {
 }
 
 #[derive(Clone)]
-pub(crate) enum GlobalShortcutMessage {
+pub enum GlobalShortcutMessage {
   IsRegistered(Accelerator, Sender<bool>),
   Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
   Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
@@ -745,12 +745,12 @@ pub(crate) enum GlobalShortcutMessage {
 }
 
 #[derive(Clone)]
-pub(crate) enum ClipboardMessage {
+pub enum ClipboardMessage {
   WriteText(String, Sender<()>),
   ReadText(Sender<Option<String>>),
 }
 
-pub(crate) enum Message {
+pub enum Message {
   Task(Box<dyn FnOnce() + Send>),
   Window(WindowId, WindowMessage),
   Webview(WindowId, WebviewMessage),
@@ -1235,7 +1235,7 @@ impl WindowHandle {
   }
 }
 
-struct WindowWrapper {
+pub struct WindowWrapper {
   label: String,
   inner: WindowHandle,
   #[cfg(feature = "menu")]
@@ -1280,6 +1280,16 @@ impl WryHandle {
       .map_err(|_| Error::FailedToSendMessage)?;
     rx.recv().unwrap()
   }
+
+  /// Send a message to the event loop.
+  pub fn send_event(&self, message: Message) -> Result<()> {
+    self
+      .dispatcher_context
+      .proxy
+      .send_event(message)
+      .map_err(|_| Error::FailedToSendMessage)?;
+    Ok(())
+  }
 }
 
 impl RuntimeHandle for WryHandle {

+ 12 - 0
core/tauri/src/app.rs

@@ -158,6 +158,18 @@ impl AppHandle<crate::Wry> {
   ) -> crate::Result<Arc<tauri_runtime_wry::Window>> {
     self.runtime_handle.create_tao_window(f).map_err(Into::into)
   }
+
+  /// Sends a window message to the event loop.
+  pub fn send_tao_window_event(
+    &self,
+    window_id: tauri_runtime_wry::WindowId,
+    message: tauri_runtime_wry::WindowMessage,
+  ) -> crate::Result<()> {
+    self
+      .runtime_handle
+      .send_event(tauri_runtime_wry::Message::Window(window_id, message))
+      .map_err(Into::into)
+  }
 }
 
 impl<R: Runtime> Clone for AppHandle<R> {