use core::fmt;
use std::string::String;
use std::sync::Arc;
use crate::async_rt::{NativeStdScheduler, SchedulerBackend, Task};
use crate::element::View;
use crate::engine::VeloxEngine;
use crate::i18n::{DynamicCatalog, Locale, StaticCatalog};
use crate::platform::winit::WinitPlatform;
use crate::platform::{DesktopRenderBackend, Platform};
use crate::pod::{CoreTheme, Handle};
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum AppError {
WindowAlreadyOpen,
}
impl fmt::Display for AppError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::WindowAlreadyOpen => {
write!(f, "primary window has already been opened in this AppContext")
}
}
}
}
impl std::error::Error for AppError {}
#[derive(Clone, Debug)]
pub struct WindowOptions {
pub title: String,
pub width: i16,
pub height: i16,
pub theme: Option<CoreTheme>,
pub locale: Option<Locale>,
pub dpi_scale: Option<usize>,
pub backend: Option<DesktopRenderBackend>,
pub scroll_list_id: Option<u32>,
}
impl Default for WindowOptions {
fn default() -> Self {
Self {
title: String::from("Velox Application"),
width: 960,
height: 640,
theme: None,
locale: None,
dpi_scale: None,
backend: None,
scroll_list_id: None,
}
}
}
impl WindowOptions {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
..Self::default()
}
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
self
}
#[inline]
pub const fn size(mut self, width: i16, height: i16) -> Self {
self.width = width;
self.height = height;
self
}
#[inline]
pub const fn theme(mut self, theme: CoreTheme) -> Self {
self.theme = Some(theme);
self
}
#[inline]
pub const fn locale(mut self, locale: Locale) -> Self {
self.locale = Some(locale);
self
}
#[inline]
pub const fn dpi_scale(mut self, scale: usize) -> Self {
self.dpi_scale = Some(scale);
self
}
#[inline]
pub const fn backend(mut self, backend: DesktopRenderBackend) -> Self {
self.backend = Some(backend);
self
}
#[inline]
pub const fn software(mut self) -> Self {
self.backend = Some(DesktopRenderBackend::Software);
self
}
#[inline]
pub const fn gpu(mut self) -> Self {
self.backend = Some(DesktopRenderBackend::Wgpu);
self
}
#[inline]
pub const fn scroll_list_id(mut self, id: u32) -> Self {
self.scroll_list_id = Some(id);
self
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct WindowHandle<V> {
root: Handle<V>,
}
impl<V> WindowHandle<V> {
#[inline(always)]
pub const fn root(&self) -> Handle<V> {
self.root
}
}
pub struct WindowContext<'a> {
engine: &'a mut VeloxEngine,
}
impl<'a> WindowContext<'a> {
#[inline]
pub fn new<V: View>(&mut self, build: impl FnOnce(&mut Self) -> V) -> Handle<V> {
let view = build(self);
self.engine.mount_root(view)
}
#[inline]
pub fn insert_view<V: View>(&mut self, view: V) -> Handle<V> {
self.engine.insert_view(view)
}
#[inline]
pub fn mount_root<V: View>(&mut self, view: V) -> Handle<V> {
self.engine.mount_root(view)
}
#[inline(always)]
pub fn engine(&self) -> &VeloxEngine {
self.engine
}
#[inline(always)]
pub fn engine_mut(&mut self) -> &mut VeloxEngine {
self.engine
}
#[inline]
pub fn scheduler_name(&self) -> &'static str {
self.engine.scheduler_name()
}
#[inline]
pub fn spawn_for<V, T, F, Fut>(&mut self, handle: Handle<V>, f: F) -> Task<T>
where
V: View,
T: 'static,
F: FnOnce(Handle<V>, crate::async_rt::AsyncApp) -> Fut,
Fut: core::future::Future<Output = T> + 'static,
{
self.engine.spawn_for(handle, f)
}
}
pub struct BuiltWindow {
pub options: WindowOptions,
pub engine: VeloxEngine,
}
pub struct AppContext {
scheduler: Arc<dyn SchedulerBackend>,
theme: CoreTheme,
locale: Option<Locale>,
static_catalog: Option<&'static StaticCatalog>,
dynamic_catalog: Option<Arc<DynamicCatalog>>,
backend: DesktopRenderBackend,
dpi_scale: Option<usize>,
primary_window: Option<BuiltWindow>,
}
impl AppContext {
pub fn set_scheduler(&mut self, scheduler: Arc<dyn SchedulerBackend>) {
self.scheduler = scheduler;
}
#[inline]
pub fn scheduler_name(&self) -> &'static str {
self.scheduler.name()
}
#[inline]
pub fn set_theme(&mut self, theme: CoreTheme) {
self.theme = theme;
}
#[inline]
pub fn set_locale(&mut self, locale: Locale) {
self.locale = Some(locale);
}
#[inline]
pub fn set_i18n_catalog(&mut self, catalog: &'static StaticCatalog) {
self.static_catalog = Some(catalog);
}
#[inline]
pub fn set_dynamic_catalog(&mut self, catalog: Arc<DynamicCatalog>) {
self.dynamic_catalog = Some(catalog);
}
#[inline]
pub fn set_backend(&mut self, backend: DesktopRenderBackend) {
self.backend = backend;
}
pub fn open_window<V: View>(
&mut self,
options: WindowOptions,
build_root: impl FnOnce(&mut WindowContext<'_>) -> Handle<V>,
) -> Result<WindowHandle<V>, AppError> {
if self.primary_window.is_some() {
return Err(AppError::WindowAlreadyOpen);
}
let w = options.width.max(1);
let h = options.height.max(1);
let mut engine = VeloxEngine::new(w, h).with_scheduler(Arc::clone(&self.scheduler));
engine.set_theme(options.theme.unwrap_or(self.theme));
if let Some(loc) = options.locale.or(self.locale) {
engine.set_locale(loc);
}
if let Some(cat) = self.static_catalog {
engine.set_i18n_catalog(cat);
}
if let Some(cat) = &self.dynamic_catalog {
engine.set_dynamic_catalog(Arc::clone(cat));
}
let root = {
let mut wcx = WindowContext {
engine: &mut engine,
};
build_root(&mut wcx)
};
let mut merged_opts = options;
if merged_opts.backend.is_none() {
merged_opts.backend = Some(self.backend);
}
if merged_opts.dpi_scale.is_none() {
merged_opts.dpi_scale = self.dpi_scale;
}
self.primary_window = Some(BuiltWindow {
options: merged_opts,
engine,
});
Ok(WindowHandle { root })
}
}
pub struct Application {
scheduler: Arc<dyn SchedulerBackend>,
theme: CoreTheme,
locale: Option<Locale>,
static_catalog: Option<&'static StaticCatalog>,
dynamic_catalog: Option<Arc<DynamicCatalog>>,
backend: DesktopRenderBackend,
dpi_scale: Option<usize>,
}
impl Default for Application {
fn default() -> Self {
Self::new()
}
}
impl Application {
pub fn new() -> Self {
Self {
scheduler: NativeStdScheduler::global(),
theme: CoreTheme::dark(),
locale: None,
static_catalog: None,
dynamic_catalog: None,
backend: DesktopRenderBackend::default_desktop(),
dpi_scale: None,
}
}
pub fn with_scheduler(mut self, scheduler: Arc<dyn SchedulerBackend>) -> Self {
self.scheduler = scheduler;
self
}
#[inline]
pub const fn with_theme(mut self, theme: CoreTheme) -> Self {
self.theme = theme;
self
}
#[inline]
pub const fn with_locale(mut self, locale: Locale) -> Self {
self.locale = Some(locale);
self
}
#[inline]
pub const fn with_i18n_catalog(mut self, catalog: &'static StaticCatalog) -> Self {
self.static_catalog = Some(catalog);
self
}
pub fn with_dynamic_catalog(mut self, catalog: Arc<DynamicCatalog>) -> Self {
self.dynamic_catalog = Some(catalog);
self
}
#[inline]
pub const fn with_backend(mut self, backend: DesktopRenderBackend) -> Self {
self.backend = backend;
self
}
#[inline]
pub const fn with_dpi_scale(mut self, scale: usize) -> Self {
self.dpi_scale = Some(scale);
self
}
fn into_context(self) -> AppContext {
AppContext {
scheduler: self.scheduler,
theme: self.theme,
locale: self.locale,
static_catalog: self.static_catalog,
dynamic_catalog: self.dynamic_catalog,
backend: self.backend,
dpi_scale: self.dpi_scale,
primary_window: None,
}
}
pub fn build_headless(self, setup: impl FnOnce(&mut AppContext)) -> Option<BuiltWindow> {
let mut cx = self.into_context();
setup(&mut cx);
cx.primary_window
}
pub fn run(self, setup: impl FnOnce(&mut AppContext)) {
let Some(built) = self.build_headless(setup) else {
return;
};
let BuiltWindow { options, engine } = built;
let mut plat = WinitPlatform::new(
options.title,
options.width.max(1) as usize,
options.height.max(1) as usize,
)
.with_backend(
options
.backend
.unwrap_or_else(DesktopRenderBackend::default_desktop),
);
if let Some(scale) = options.dpi_scale {
plat = plat.with_dpi_scale(scale);
}
if let Some(scroll_id) = options.scroll_list_id {
plat.run(engine, Some(scroll_id));
} else {
plat.run_event_loop(engine);
}
}
}
#[inline]
pub fn application() -> Application {
Application::new()
}
#[inline]
pub fn init(_cx: &mut AppContext) {}
#[inline]
pub fn open_window<V: View>(
options: WindowOptions,
cx: &mut AppContext,
build_root: impl FnOnce(&mut WindowContext<'_>) -> Handle<V>,
) -> Result<WindowHandle<V>, AppError> {
cx.open_window(options, build_root)
}