use std::sync::Arc;
use freya_node_state::Parse;
use skia_safe::Color;
use winit::window::Window;
#[derive(Clone)]
pub struct WindowConfig<T: Clone> {
pub width: f64,
pub height: f64,
pub decorations: bool,
pub title: &'static str,
pub transparent: bool,
pub state: Option<T>,
pub background: Color,
pub on_setup: Option<WindowCallback>,
pub on_exit: Option<WindowCallback>,
}
impl<T: Clone> Default for WindowConfig<T> {
fn default() -> Self {
Self {
width: 600.0,
height: 600.0,
decorations: true,
title: "Freya app",
transparent: false,
state: None,
background: Color::WHITE,
on_setup: None,
on_exit: None,
}
}
}
#[derive(Clone, Default)]
pub struct LaunchConfig<'a, T: Clone> {
pub window: WindowConfig<T>,
pub fonts: Vec<(&'a str, &'a [u8])>,
}
impl<'a, T: Clone> LaunchConfig<'a, T> {
pub fn builder() -> LaunchConfigBuilder<'a, T> {
LaunchConfigBuilder::default()
}
}
pub type WindowCallback = Arc<Box<fn(&mut Window)>>;
#[derive(Clone)]
pub struct LaunchConfigBuilder<'a, T> {
pub width: f64,
pub height: f64,
pub decorations: bool,
pub title: &'static str,
pub transparent: bool,
pub state: Option<T>,
pub background: Color,
pub fonts: Vec<(&'a str, &'a [u8])>,
pub on_setup: Option<WindowCallback>,
pub on_exit: Option<WindowCallback>,
}
impl<T> Default for LaunchConfigBuilder<'_, T> {
fn default() -> Self {
Self {
width: 350.0,
height: 350.0,
decorations: true,
title: "Freya app",
transparent: false,
state: None,
background: Color::WHITE,
fonts: Vec::default(),
on_setup: None,
on_exit: None,
}
}
}
impl<'a, T: Clone> LaunchConfigBuilder<'a, T> {
pub fn with_width(mut self, width: f64) -> Self {
self.width = width;
self
}
pub fn with_height(mut self, height: f64) -> Self {
self.height = 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_state(mut self, state: T) -> Self {
self.state = Some(state);
self
}
pub fn with_background(mut self, background: &str) -> Self {
self.background = Color::parse(background).unwrap_or(Color::WHITE);
self
}
pub fn with_font(mut self, font_name: &'a str, font: &'a [u8]) -> Self {
self.fonts.push((font_name, font));
self
}
pub fn on_setup(mut self, callback: fn(&mut Window)) -> Self {
self.on_setup = Some(Arc::new(Box::new(callback)));
self
}
pub fn on_exit(mut self, callback: fn(&mut Window)) -> Self {
self.on_exit = Some(Arc::new(Box::new(callback)));
self
}
pub fn build(self) -> LaunchConfig<'a, T> {
LaunchConfig {
window: WindowConfig {
width: self.width,
height: self.height,
title: self.title,
decorations: self.decorations,
transparent: self.transparent,
state: self.state,
background: self.background,
on_setup: self.on_setup,
on_exit: self.on_exit,
},
fonts: self.fonts,
}
}
}