Skip to content

Commit

Permalink
feat(core): add API to send wry window message to the event loop (#2339)
Browse files Browse the repository at this point in the history
* feat(core): add API to send wry window message to the event loop

* expose types
  • Loading branch information
lucasfernog committed Aug 2, 2021
1 parent 5b7be81 commit 15566cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .changes/tauri-empty-window.md
Expand Up @@ -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.
40 changes: 25 additions & 15 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -50,15 +50,15 @@ 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,
WebView, WebViewBuilder,
},
};

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;
Expand Down Expand Up @@ -134,7 +134,7 @@ struct EventLoopContext {
}

#[derive(Debug, Clone)]
struct GlobalShortcutWrapper(GlobalShortcut);
pub struct GlobalShortcutWrapper(GlobalShortcut);

unsafe impl Send for GlobalShortcutWrapper {}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {}

Expand All @@ -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",
Expand All @@ -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>>>),
Expand Down Expand Up @@ -714,7 +714,7 @@ enum WindowMessage {
}

#[derive(Debug, Clone)]
enum WebviewMessage {
pub enum WebviewMessage {
EvaluateScript(String),
#[allow(dead_code)]
WebviewEvent(WebviewEvent),
Expand All @@ -723,34 +723,34 @@ 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")]
UpdateIconAsTemplate(bool),
}

#[derive(Clone)]
pub(crate) enum GlobalShortcutMessage {
pub enum GlobalShortcutMessage {
IsRegistered(Accelerator, Sender<bool>),
Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
UnregisterAll(Sender<Result<()>>),
}

#[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),
Expand Down Expand Up @@ -1235,7 +1235,7 @@ impl WindowHandle {
}
}

struct WindowWrapper {
pub struct WindowWrapper {
label: String,
inner: WindowHandle,
#[cfg(feature = "menu")]
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions core/tauri/src/app.rs
Expand Up @@ -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> {
Expand Down

0 comments on commit 15566cf

Please sign in to comment.