Skip to content

Commit 15566cf

Browse files
authored
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
1 parent 5b7be81 commit 15566cf

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

.changes/tauri-empty-window.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"tauri-runtime-wry": patch
55
---
66

7-
Allow creation of empty Window with `create_tao_window()` on the AppHandler.
7+
Allow creation of empty Window with `create_tao_window()` and management with `send_tao_window_event()` on the AppHandler.

core/tauri-runtime-wry/src/lib.rs

+25-15
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ use wry::{
5050
event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget},
5151
global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager},
5252
monitor::MonitorHandle,
53-
window::{Fullscreen, Icon as WindowIcon, UserAttentionType as WryUserAttentionType, WindowId},
53+
window::{Fullscreen, Icon as WindowIcon, UserAttentionType as WryUserAttentionType},
5454
},
5555
webview::{
5656
FileDropEvent as WryFileDropEvent, RpcRequest as WryRpcRequest, RpcResponse, WebContext,
5757
WebView, WebViewBuilder,
5858
},
5959
};
6060

61-
pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder};
61+
pub use wry::application::window::{Window, WindowBuilder as WryWindowBuilder, WindowId};
6262

6363
#[cfg(target_os = "windows")]
6464
use wry::webview::WebviewExtWindows;
@@ -134,7 +134,7 @@ struct EventLoopContext {
134134
}
135135

136136
#[derive(Debug, Clone)]
137-
struct GlobalShortcutWrapper(GlobalShortcut);
137+
pub struct GlobalShortcutWrapper(GlobalShortcut);
138138

139139
unsafe impl Send for GlobalShortcutWrapper {}
140140

@@ -412,7 +412,7 @@ impl From<Position> for PositionWrapper {
412412
}
413413

414414
#[derive(Debug, Clone)]
415-
struct UserAttentionTypeWrapper(WryUserAttentionType);
415+
pub struct UserAttentionTypeWrapper(WryUserAttentionType);
416416

417417
impl From<UserAttentionType> for UserAttentionTypeWrapper {
418418
fn from(request_type: UserAttentionType) -> UserAttentionTypeWrapper {
@@ -626,12 +626,12 @@ impl From<FileDropEventWrapper> for FileDropEvent {
626626
}
627627

628628
#[cfg(target_os = "macos")]
629-
struct NSWindow(*mut std::ffi::c_void);
629+
pub struct NSWindow(*mut std::ffi::c_void);
630630
#[cfg(target_os = "macos")]
631631
unsafe impl Send for NSWindow {}
632632

633633
#[cfg(windows)]
634-
struct Hwnd(HWND);
634+
pub struct Hwnd(HWND);
635635
#[cfg(windows)]
636636
unsafe impl Send for Hwnd {}
637637

@@ -642,7 +642,7 @@ unsafe impl Send for Hwnd {}
642642
target_os = "netbsd",
643643
target_os = "openbsd"
644644
))]
645-
struct GtkWindow(gtk::ApplicationWindow);
645+
pub struct GtkWindow(gtk::ApplicationWindow);
646646
#[cfg(any(
647647
target_os = "linux",
648648
target_os = "dragonfly",
@@ -653,7 +653,7 @@ struct GtkWindow(gtk::ApplicationWindow);
653653
unsafe impl Send for GtkWindow {}
654654

655655
#[derive(Debug, Clone)]
656-
enum WindowMessage {
656+
pub enum WindowMessage {
657657
// Getters
658658
ScaleFactor(Sender<f64>),
659659
InnerPosition(Sender<Result<PhysicalPosition<i32>>>),
@@ -714,7 +714,7 @@ enum WindowMessage {
714714
}
715715

716716
#[derive(Debug, Clone)]
717-
enum WebviewMessage {
717+
pub enum WebviewMessage {
718718
EvaluateScript(String),
719719
#[allow(dead_code)]
720720
WebviewEvent(WebviewEvent),
@@ -723,34 +723,34 @@ enum WebviewMessage {
723723

724724
#[allow(dead_code)]
725725
#[derive(Debug, Clone)]
726-
enum WebviewEvent {
726+
pub enum WebviewEvent {
727727
Focused(bool),
728728
}
729729

730730
#[cfg(feature = "system-tray")]
731731
#[derive(Clone)]
732-
pub(crate) enum TrayMessage {
732+
pub enum TrayMessage {
733733
UpdateItem(u16, menu::MenuUpdate),
734734
UpdateIcon(Icon),
735735
#[cfg(target_os = "macos")]
736736
UpdateIconAsTemplate(bool),
737737
}
738738

739739
#[derive(Clone)]
740-
pub(crate) enum GlobalShortcutMessage {
740+
pub enum GlobalShortcutMessage {
741741
IsRegistered(Accelerator, Sender<bool>),
742742
Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
743743
Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
744744
UnregisterAll(Sender<Result<()>>),
745745
}
746746

747747
#[derive(Clone)]
748-
pub(crate) enum ClipboardMessage {
748+
pub enum ClipboardMessage {
749749
WriteText(String, Sender<()>),
750750
ReadText(Sender<Option<String>>),
751751
}
752752

753-
pub(crate) enum Message {
753+
pub enum Message {
754754
Task(Box<dyn FnOnce() + Send>),
755755
Window(WindowId, WindowMessage),
756756
Webview(WindowId, WebviewMessage),
@@ -1235,7 +1235,7 @@ impl WindowHandle {
12351235
}
12361236
}
12371237

1238-
struct WindowWrapper {
1238+
pub struct WindowWrapper {
12391239
label: String,
12401240
inner: WindowHandle,
12411241
#[cfg(feature = "menu")]
@@ -1280,6 +1280,16 @@ impl WryHandle {
12801280
.map_err(|_| Error::FailedToSendMessage)?;
12811281
rx.recv().unwrap()
12821282
}
1283+
1284+
/// Send a message to the event loop.
1285+
pub fn send_event(&self, message: Message) -> Result<()> {
1286+
self
1287+
.dispatcher_context
1288+
.proxy
1289+
.send_event(message)
1290+
.map_err(|_| Error::FailedToSendMessage)?;
1291+
Ok(())
1292+
}
12831293
}
12841294

12851295
impl RuntimeHandle for WryHandle {

core/tauri/src/app.rs

+12
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ impl AppHandle<crate::Wry> {
158158
) -> crate::Result<Arc<tauri_runtime_wry::Window>> {
159159
self.runtime_handle.create_tao_window(f).map_err(Into::into)
160160
}
161+
162+
/// Sends a window message to the event loop.
163+
pub fn send_tao_window_event(
164+
&self,
165+
window_id: tauri_runtime_wry::WindowId,
166+
message: tauri_runtime_wry::WindowMessage,
167+
) -> crate::Result<()> {
168+
self
169+
.runtime_handle
170+
.send_event(tauri_runtime_wry::Message::Window(window_id, message))
171+
.map_err(Into::into)
172+
}
161173
}
162174

163175
impl<R: Runtime> Clone for AppHandle<R> {

0 commit comments

Comments
 (0)