use dioxus_core::{LaunchConfig, VirtualDom};
use std::path::PathBuf;
use std::{borrow::Cow, sync::Arc};
use tao::window::{Icon, WindowBuilder};
use tao::{
event_loop::{EventLoop, EventLoopWindowTarget},
window::Window,
};
use wry::http::{Request as HttpRequest, Response as HttpResponse};
use wry::{RequestAsyncResponder, WebViewId};
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>,
),
>;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum WindowCloseBehaviour {
WindowHides,
WindowCloses,
}
pub(crate) enum MenuBuilderState {
Unset,
Set(Option<DioxusMenu>),
}
impl From<MenuBuilderState> for Option<DioxusMenu> {
fn from(val: MenuBuilderState) -> Self {
match val {
MenuBuilderState::Unset => Some(default_menu_bar()),
MenuBuilderState::Set(menu) => menu,
}
}
}
pub struct Config {
pub(crate) event_loop: Option<EventLoop<UserWindowEvent>>,
pub(crate) window: WindowBuilder,
pub(crate) as_child_window: bool,
pub(crate) menu: MenuBuilderState,
pub(crate) protocols: Vec<WryProtocol>,
pub(crate) asynchronous_protocols: Vec<AsyncWryProtocol>,
pub(crate) pre_rendered: Option<String>,
pub(crate) disable_context_menu: bool,
pub(crate) resource_dir: Option<PathBuf>,
pub(crate) data_dir: Option<PathBuf>,
pub(crate) custom_head: Option<String>,
pub(crate) custom_index: Option<String>,
pub(crate) root_name: String,
pub(crate) background_color: Option<(u8, u8, u8, u8)>,
pub(crate) exit_on_last_window_close: bool,
pub(crate) window_close_behavior: WindowCloseBehaviour,
pub(crate) custom_event_handler: Option<CustomEventHandler>,
pub(crate) disable_file_drop_handler: bool,
pub(crate) disable_dma_buf_on_wayland: bool,
pub(crate) additional_windows_args: Option<String>,
#[allow(clippy::type_complexity)]
pub(crate) on_window: Option<Box<dyn FnMut(Arc<Window>, &mut VirtualDom) + 'static>>,
}
impl LaunchConfig for Config {}
pub(crate) type WryProtocol = (
String,
Box<dyn Fn(WebViewId, HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static>,
);
pub(crate) type AsyncWryProtocol = (
String,
Box<dyn Fn(WebViewId, HttpRequest<Vec<u8>>, RequestAsyncResponder) + 'static>,
);
impl Config {
#[inline]
pub fn new() -> Self {
let mut window: WindowBuilder = WindowBuilder::new()
.with_title(dioxus_cli_config::app_title().unwrap_or_else(|| "Dioxus App".to_string()));
let always_on_top = dioxus_cli_config::always_on_top().unwrap_or(true);
if cfg!(debug_assertions) {
window = window.with_always_on_top(always_on_top);
}
Self {
window,
as_child_window: false,
event_loop: None,
menu: MenuBuilderState::Unset,
protocols: Vec::new(),
asynchronous_protocols: Vec::new(),
pre_rendered: None,
disable_context_menu: !cfg!(debug_assertions),
resource_dir: None,
data_dir: None,
custom_head: None,
custom_index: None,
root_name: "main".to_string(),
background_color: None,
exit_on_last_window_close: true,
window_close_behavior: WindowCloseBehaviour::WindowCloses,
custom_event_handler: None,
disable_file_drop_handler: false,
disable_dma_buf_on_wayland: true,
on_window: None,
additional_windows_args: None,
}
}
pub fn with_resource_directory(mut self, path: impl Into<PathBuf>) -> Self {
self.resource_dir = Some(path.into());
self
}
pub fn with_data_directory(mut self, path: impl Into<PathBuf>) -> Self {
self.data_dir = Some(path.into());
self
}
pub fn with_disable_context_menu(mut self, disable: bool) -> Self {
self.disable_context_menu = disable;
self
}
pub fn with_disable_drag_drop_handler(mut self, disable: bool) -> Self {
self.disable_file_drop_handler = disable;
self
}
pub fn with_prerendered(mut self, content: String) -> Self {
self.pre_rendered = Some(content);
self
}
pub fn with_event_loop(mut self, event_loop: EventLoop<UserWindowEvent>) -> Self {
self.event_loop = Some(event_loop);
self
}
pub fn with_window(mut self, window: WindowBuilder) -> Self {
self.window = window;
if !self.window.window.decorations && matches!(self.menu, MenuBuilderState::Unset) {
self.menu = MenuBuilderState::Set(None);
}
self
}
pub fn with_as_child_window(mut self) -> Self {
self.as_child_window = true;
self
}
pub fn with_exits_when_last_window_closes(mut self, exit: bool) -> Self {
self.exit_on_last_window_close = exit;
self
}
pub fn with_close_behaviour(mut self, behaviour: WindowCloseBehaviour) -> Self {
self.window_close_behavior = behaviour;
self
}
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
}
pub fn with_custom_protocol<F>(mut self, name: impl ToString, handler: F) -> Self
where
F: Fn(WebViewId, HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static,
{
self.protocols.push((name.to_string(), Box::new(handler)));
self
}
pub fn with_asynchronous_custom_protocol<F>(mut self, name: impl ToString, handler: F) -> Self
where
F: Fn(WebViewId, HttpRequest<Vec<u8>>, RequestAsyncResponder) + 'static,
{
self.asynchronous_protocols
.push((name.to_string(), Box::new(handler)));
self
}
pub fn with_icon(mut self, icon: Icon) -> Self {
self.window.window.window_icon = Some(icon);
self
}
pub fn with_custom_head(mut self, head: String) -> Self {
self.custom_head = Some(head);
self
}
pub fn with_custom_index(mut self, index: String) -> Self {
self.custom_index = Some(index);
self
}
pub fn with_root_name(mut self, name: impl Into<String>) -> Self {
self.root_name = name.into();
self
}
pub fn with_background_color(mut self, color: (u8, u8, u8, u8)) -> Self {
self.background_color = Some(color);
self
}
#[allow(unused)]
pub fn with_menu(mut self, menu: impl Into<Option<DioxusMenu>>) -> Self {
#[cfg(not(any(target_os = "ios", target_os = "android")))]
{
if self.window.window.decorations {
self.menu = MenuBuilderState::Set(menu.into())
}
}
self
}
pub fn with_on_window(mut self, f: impl FnMut(Arc<Window>, &mut VirtualDom) + 'static) -> Self {
self.on_window = Some(Box::new(f));
self
}
pub fn with_disable_dma_buf_on_wayland(mut self, disable: bool) -> Self {
self.disable_dma_buf_on_wayland = disable;
self
}
pub fn with_windows_browser_args(mut self, additional_args: impl ToString) -> Self {
self.additional_windows_args = Some(additional_args.to_string());
self
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}