freya_core/
window_config.rsuse std::sync::Arc;
use dioxus_core::{
fc_to_builder,
Element,
};
use dioxus_core_macro::rsx;
use dioxus_signals::{
GlobalSignal,
Readable,
};
use freya_engine::prelude::Color;
use winit::window::{
Icon,
Window,
WindowAttributes,
};
use crate::{
event_loop_messages::{
EventLoopMessage,
EventLoopMessageAction,
},
parsing::Parse,
};
pub type WindowCallback = Box<dyn FnOnce(&mut Window) + Send + Sync>;
pub type OnCloseCallback = Box<dyn FnOnce(&mut Window) -> OnCloseResponse + Send + Sync>;
pub type WindowBuilderHook = Box<dyn FnOnce(WindowAttributes) -> WindowAttributes + Send + Sync>;
impl From<accesskit_winit::Event> for EventLoopMessage {
fn from(value: accesskit_winit::Event) -> Self {
Self {
window_id: Some(value.window_id),
action: EventLoopMessageAction::Accessibility(value.window_event),
}
}
}
#[derive(PartialEq)]
pub enum OnCloseResponse {
Close,
NotClose,
}
pub struct WindowConfig {
pub app: Arc<dyn Fn() -> Element + Send + Sync>,
pub size: (f64, f64),
pub min_size: Option<(f64, f64)>,
pub max_size: Option<(f64, f64)>,
pub decorations: bool,
pub title: &'static str,
pub transparent: bool,
pub background: Color,
pub visible: bool,
pub icon: Option<Icon>,
pub on_setup: Option<WindowCallback>,
pub on_close: Option<OnCloseCallback>,
pub window_attributes_hook: Option<WindowBuilderHook>,
pub max_gpu_resources_bytes: Option<usize>,
}
impl WindowConfig {
pub fn new(app: fn() -> Element) -> Self {
#[allow(non_snake_case)]
let App = app;
Self::new_with_defaults(Arc::new(move || rsx!(App {})))
}
pub fn new_with_props<T: dioxus_core::Properties + Sync + Send>(
app: fn(T) -> Element,
props: T,
) -> Self {
#[allow(non_snake_case)]
let App = app;
Self::new_with_defaults(Arc::new(move || rsx!(App { ..props.clone() })))
}
fn new_with_defaults(app: Arc<dyn Fn() -> Element + Send + Sync>) -> Self {
Self {
app,
size: (700.0, 500.0),
min_size: None,
max_size: None,
decorations: true,
title: "Freya",
transparent: false,
background: Color::WHITE,
visible: true,
icon: None,
on_setup: None,
on_close: None,
window_attributes_hook: None,
max_gpu_resources_bytes: None,
}
}
pub fn with_size(mut self, width: f64, height: f64) -> Self {
self.size = (width, height);
self
}
pub fn with_min_size(mut self, min_width: f64, min_height: f64) -> Self {
self.min_size = Some((min_width, min_height));
self
}
pub fn with_max_size(mut self, max_width: f64, max_height: f64) -> Self {
self.max_size = Some((max_width, max_height));
self
}
pub fn with_decorations(mut self, decorations: bool) -> Self {
self.decorations = decorations;
self
}
pub fn with_title(mut self, title: &'static str) -> Self {
self.title = title;
self
}
pub fn with_transparency(mut self, transparency: bool) -> Self {
self.transparent = transparency;
self
}
pub fn with_max_gpu_resources_bytes(mut self, max_gpu_resources_bytes: usize) -> Self {
self.max_gpu_resources_bytes = Some(max_gpu_resources_bytes);
self
}
pub fn with_background(mut self, background: &str) -> Self {
self.background = Color::parse(background).unwrap_or(Color::WHITE);
self
}
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
pub fn with_icon(mut self, icon: Icon) -> Self {
self.icon = Some(icon);
self
}
pub fn on_setup(mut self, callback: impl FnOnce(&mut Window) + 'static + Send + Sync) -> Self {
self.on_setup = Some(Box::new(callback));
self
}
pub fn on_close(
mut self,
callback: impl FnOnce(&mut Window) -> OnCloseResponse + 'static + Send + Sync,
) -> Self {
self.on_close = Some(Box::new(callback));
self
}
pub fn with_window_attributes(
mut self,
window_attributes_hook: impl FnOnce(WindowAttributes) -> WindowAttributes
+ 'static
+ Send
+ Sync,
) -> Self {
self.window_attributes_hook = Some(Box::new(window_attributes_hook));
self
}
}