use std::borrow::Cow;
use std::path::PathBuf;
use tao::window::{Icon, WindowBuilder};
use wry::http::{Request as HttpRequest, Response as HttpResponse};
use crate::menubar::{default_menu_bar, DioxusMenu};
#[derive(Copy, Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum WindowCloseBehaviour {
LastWindowExitsApp,
LastWindowHides,
CloseWindow,
}
pub struct Config {
pub(crate) window: WindowBuilder,
pub(crate) menu: Option<DioxusMenu>,
pub(crate) protocols: Vec<WryProtocol>,
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) last_window_close_behavior: WindowCloseBehaviour,
}
pub(crate) type WryProtocol = (
String,
Box<dyn Fn(HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static>,
);
impl Config {
#[inline]
pub fn new() -> Self {
let window: WindowBuilder = WindowBuilder::new()
.with_title(
dioxus_cli_config::CURRENT_CONFIG
.as_ref()
.map(|c| c.dioxus_config.application.name.clone())
.unwrap_or("Dioxus App".to_string()),
)
.with_always_on_top(cfg!(debug_assertions));
Self {
window,
menu: Some(default_menu_bar()),
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,
last_window_close_behavior: WindowCloseBehaviour::LastWindowExitsApp,
}
}
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_prerendered(mut self, content: String) -> Self {
self.pre_rendered = Some(content);
self
}
pub fn with_window(mut self, window: WindowBuilder) -> Self {
self.window = window;
self
}
pub fn with_close_behaviour(mut self, behaviour: WindowCloseBehaviour) -> Self {
self.last_window_close_behavior = behaviour;
self
}
pub fn with_custom_protocol<F>(mut self, name: String, handler: F) -> Self
where
F: Fn(HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static,
{
self.protocols.push((name, 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")))]
{
self.menu = menu.into();
}
self
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}