diff --git a/packages/desktop/src/cfg.rs b/packages/desktop/src/cfg.rs index 4456eb2d7..dcf032f10 100644 --- a/packages/desktop/src/cfg.rs +++ b/packages/desktop/src/cfg.rs @@ -1,5 +1,7 @@ use std::path::PathBuf; +use wry::application::event::Event; +use wry::application::event_loop::EventLoopWindowTarget; use wry::application::window::Icon; use wry::{ application::window::{Window, WindowBuilder}, @@ -8,12 +10,15 @@ use wry::{ Result as WryResult, }; +use crate::desktop_context::UserWindowEvent; + // pub(crate) type DynEventHandlerFn = dyn Fn(&mut EventLoop<()>, &mut WebView); /// The configuration for the desktop application. pub struct Config { pub(crate) window: WindowBuilder, pub(crate) file_drop_handler: Option, + pub(crate) event_handler: Option, pub(crate) protocols: Vec, pub(crate) pre_rendered: Option, // pub(crate) event_handler: Option>, @@ -25,6 +30,7 @@ pub struct Config { } type DropHandler = Box bool>; +type EventHandler = Box, &EventLoopWindowTarget)>; pub(crate) type WryProtocol = ( String, @@ -42,6 +48,7 @@ impl Config { window, protocols: Vec::new(), file_drop_handler: None, + event_handler: None, pre_rendered: None, disable_context_menu: !cfg!(debug_assertions), resource_dir: None, @@ -77,14 +84,15 @@ impl Config { self } - // /// Set a custom event handler - // pub fn with_event_handler( - // mut self, - // handler: impl Fn(&mut EventLoop<()>, &mut WebView) + 'static, - // ) -> Self { - // self.event_handler = Some(Box::new(handler)); - // self - // } + /// Set a custom wry event handler. This can be used to listen to window and webview events + /// This only has an effect on the main window + pub fn with_event_handler( + mut self, + handler: impl Fn(&Event, &EventLoopWindowTarget) + 'static, + ) -> Self { + self.event_handler = Some(Box::new(handler)); + self + } /// Set a file drop handler pub fn with_file_drop_handler( diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index a75e6bbbb..0fa27d99f 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -104,7 +104,7 @@ pub fn launch_cfg(root: Component, config_builder: Config) { /// }) /// } /// ``` -pub fn launch_with_props(root: Component

, props: P, cfg: Config) { +pub fn launch_with_props(root: Component

, props: P, mut cfg: Config) { let event_loop = EventLoop::::with_user_event(); let proxy = event_loop.create_proxy(); @@ -125,6 +125,8 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) let queue = WebviewQueue::default(); + let event_handler = cfg.event_handler.take(); + // By default, we'll create a new window when the app starts queue.borrow_mut().push(create_new_window( cfg, @@ -134,9 +136,13 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) &queue, )); - event_loop.run(move |window_event, _event_loop, control_flow| { + event_loop.run(move |window_event, event_loop, control_flow| { *control_flow = ControlFlow::Wait; + if let Some(handler) = &event_handler { + handler(&window_event, event_loop); + } + match window_event { Event::WindowEvent { event, window_id, ..