create with event handler
This commit is contained in:
1 parent
e5b39bb61f
commit
2772b86629
2 files changed
+24
-10
No files matched your search
@@ -1,5 +1,7 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use wry::application::event::Event;
|
||||||
|
use wry::application::event_loop::EventLoopWindowTarget;
|
||||||
use wry::application::window::Icon;
|
use wry::application::window::Icon;
|
||||||
use wry::{
|
use wry::{
|
||||||
application::window::{Window, WindowBuilder},
|
application::window::{Window, WindowBuilder},
|
||||||
@@ -8,12 +10,15 @@ use wry::{
|
|||||||
Result as WryResult,
|
Result as WryResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::desktop_context::UserWindowEvent;
|
||||||
|
|
||||||
// pub(crate) type DynEventHandlerFn = dyn Fn(&mut EventLoop<()>, &mut WebView);
|
// pub(crate) type DynEventHandlerFn = dyn Fn(&mut EventLoop<()>, &mut WebView);
|
||||||
|
|
||||||
/// The configuration for the desktop application.
|
/// The configuration for the desktop application.
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub(crate) window: WindowBuilder,
|
pub(crate) window: WindowBuilder,
|
||||||
pub(crate) file_drop_handler: Option<DropHandler>,
|
pub(crate) file_drop_handler: Option<DropHandler>,
|
||||||
|
pub(crate) event_handler: Option<EventHandler>,
|
||||||
pub(crate) protocols: Vec<WryProtocol>,
|
pub(crate) protocols: Vec<WryProtocol>,
|
||||||
pub(crate) pre_rendered: Option<String>,
|
pub(crate) pre_rendered: Option<String>,
|
||||||
// pub(crate) event_handler: Option<Box<DynEventHandlerFn>>,
|
// pub(crate) event_handler: Option<Box<DynEventHandlerFn>>,
|
||||||
@@ -25,6 +30,7 @@ pub struct Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DropHandler = Box<dyn Fn(&Window, FileDropEvent) -> bool>;
|
type DropHandler = Box<dyn Fn(&Window, FileDropEvent) -> bool>;
|
||||||
|
type EventHandler = Box<dyn Fn(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>)>;
|
||||||
|
|
||||||
pub(crate) type WryProtocol = (
|
pub(crate) type WryProtocol = (
|
||||||
String,
|
String,
|
||||||
@@ -42,6 +48,7 @@ impl Config {
|
|||||||
window,
|
window,
|
||||||
protocols: Vec::new(),
|
protocols: Vec::new(),
|
||||||
file_drop_handler: None,
|
file_drop_handler: None,
|
||||||
|
event_handler: None,
|
||||||
pre_rendered: None,
|
pre_rendered: None,
|
||||||
disable_context_menu: !cfg!(debug_assertions),
|
disable_context_menu: !cfg!(debug_assertions),
|
||||||
resource_dir: None,
|
resource_dir: None,
|
||||||
@@ -77,14 +84,15 @@ impl Config {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
// /// Set a custom event handler
|
/// Set a custom wry event handler. This can be used to listen to window and webview events
|
||||||
// pub fn with_event_handler(
|
/// This only has an effect on the main window
|
||||||
// mut self,
|
pub fn with_event_handler(
|
||||||
// handler: impl Fn(&mut EventLoop<()>, &mut WebView) + 'static,
|
mut self,
|
||||||
// ) -> Self {
|
handler: impl Fn(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||||
// self.event_handler = Some(Box::new(handler));
|
) -> Self {
|
||||||
// self
|
self.event_handler = Some(Box::new(handler));
|
||||||
// }
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set a file drop handler
|
/// Set a file drop handler
|
||||||
pub fn with_file_drop_handler(
|
pub fn with_file_drop_handler(
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ pub fn launch_cfg(root: Component, config_builder: Config) {
|
|||||||
/// })
|
/// })
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config) {
|
pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, mut cfg: Config) {
|
||||||
let event_loop = EventLoop::<UserWindowEvent>::with_user_event();
|
let event_loop = EventLoop::<UserWindowEvent>::with_user_event();
|
||||||
|
|
||||||
let proxy = event_loop.create_proxy();
|
let proxy = event_loop.create_proxy();
|
||||||
@@ -125,6 +125,8 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
|
|||||||
|
|
||||||
let queue = WebviewQueue::default();
|
let queue = WebviewQueue::default();
|
||||||
|
|
||||||
|
let event_handler = cfg.event_handler.take();
|
||||||
|
|
||||||
// By default, we'll create a new window when the app starts
|
// By default, we'll create a new window when the app starts
|
||||||
queue.borrow_mut().push(create_new_window(
|
queue.borrow_mut().push(create_new_window(
|
||||||
cfg,
|
cfg,
|
||||||
@@ -134,9 +136,13 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
|
|||||||
&queue,
|
&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;
|
*control_flow = ControlFlow::Wait;
|
||||||
|
|
||||||
|
if let Some(handler) = &event_handler {
|
||||||
|
handler(&window_event, event_loop);
|
||||||
|
}
|
||||||
|
|
||||||
match window_event {
|
match window_event {
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event, window_id, ..
|
event, window_id, ..
|
||||||
|
|||||||
Reference in new issue
Block a user