mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-02-17 06:08:26 +00:00
create with event handler
This commit is contained in:
parent
e5b39bb61f
commit
2772b86629
2 changed files with 24 additions and 10 deletions
|
@ -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<DropHandler>,
|
||||
pub(crate) event_handler: Option<EventHandler>,
|
||||
pub(crate) protocols: Vec<WryProtocol>,
|
||||
pub(crate) pre_rendered: Option<String>,
|
||||
// pub(crate) event_handler: Option<Box<DynEventHandlerFn>>,
|
||||
|
@ -25,6 +30,7 @@ pub struct Config {
|
|||
}
|
||||
|
||||
type DropHandler = Box<dyn Fn(&Window, FileDropEvent) -> bool>;
|
||||
type EventHandler = Box<dyn Fn(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>)>;
|
||||
|
||||
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<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||
) -> Self {
|
||||
self.event_handler = Some(Box::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set a 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 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 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<P: 'static>(root: Component<P>, 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, ..
|
||||
|
|
Loading…
Add table
Reference in a new issue