Add support for user-provided event loops & event handlers (#3180)

* added support for custom event loop
* added support for custom event callbacks
This commit is contained in:
Alex Andreba authored and GitHub committed 2024-11-13 18:44:29 -05:00
1 parent e7e624f05e
commit 1370bce182
3 files changed
+42 -4

No files matched your search

+30
View File
@@ -1,12 +1,22 @@
use dioxus_core::LaunchConfig;
use std::borrow::Cow;
use std::path::PathBuf;
use tao::event_loop::{EventLoop, EventLoopWindowTarget};
use tao::window::{Icon, WindowBuilder};
use wry::http::{Request as HttpRequest, Response as HttpResponse};
use wry::RequestAsyncResponder;
use crate::ipc::UserWindowEvent;
use crate::menubar::{default_menu_bar, DioxusMenu};
type CustomEventHandler = Box<
dyn 'static
+ for<'a> FnMut(
&tao::event::Event<'a, UserWindowEvent>,
&EventLoopWindowTarget<UserWindowEvent>,
),
>;
/// The behaviour of the application when the last window is closed.
#[derive(Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
@@ -37,6 +47,7 @@ impl From<MenuBuilderState> for Option<DioxusMenu> {
/// The configuration for the desktop application.
pub struct Config {
pub(crate) event_loop: Option<EventLoop<UserWindowEvent>>,
pub(crate) window: WindowBuilder,
pub(crate) menu: MenuBuilderState,
pub(crate) protocols: Vec<WryProtocol>,
@@ -50,6 +61,7 @@ pub struct Config {
pub(crate) root_name: String,
pub(crate) background_color: Option<(u8, u8, u8, u8)>,
pub(crate) last_window_close_behavior: WindowCloseBehaviour,
pub(crate) custom_event_handler: Option<CustomEventHandler>,
}
impl LaunchConfig for Config {}
@@ -80,6 +92,7 @@ impl Config {
Self {
window,
event_loop: None,
menu: MenuBuilderState::Unset,
protocols: Vec::new(),
asynchronous_protocols: Vec::new(),
@@ -92,6 +105,7 @@ impl Config {
root_name: "main".to_string(),
background_color: None,
last_window_close_behavior: WindowCloseBehaviour::LastWindowExitsApp,
custom_event_handler: None,
}
}
@@ -121,6 +135,12 @@ impl Config {
self
}
/// Set the event loop to be used
pub fn with_event_loop(mut self, event_loop: EventLoop<UserWindowEvent>) -> Self {
self.event_loop = Some(event_loop);
self
}
/// Set the configuration for the window.
pub fn with_window(mut self, window: WindowBuilder) -> Self {
// We need to do a swap because the window builder only takes itself as muy self
@@ -138,6 +158,16 @@ impl Config {
self
}
/// Sets a custom callback to run whenever the event pool receives an event.
pub fn with_custom_event_handler(
mut self,
f: impl FnMut(&tao::event::Event<'_, UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>)
+ 'static,
) -> Self {
self.custom_event_handler = Some(Box::new(f));
self
}
/// Set a custom protocol
pub fn with_custom_protocol<F>(mut self, name: impl ToString, handler: F) -> Self
where