1use core::fmt;
28use std::string::String;
29use std::sync::Arc;
30
31use crate::async_rt::{NativeStdScheduler, SchedulerBackend, Task};
32use crate::element::View;
33use crate::engine::VeloxEngine;
34use crate::i18n::{DynamicCatalog, Locale, StaticCatalog};
35use crate::platform::winit::WinitPlatform;
36use crate::platform::{DesktopRenderBackend, Platform};
37use crate::pod::{CoreTheme, Handle};
38
39#[derive(Clone, PartialEq, Eq, Debug)]
41pub enum AppError {
42 WindowAlreadyOpen,
44}
45
46impl fmt::Display for AppError {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 match self {
49 Self::WindowAlreadyOpen => {
50 write!(f, "primary window has already been opened in this AppContext")
51 }
52 }
53 }
54}
55
56impl std::error::Error for AppError {}
57
58#[derive(Clone, Debug)]
60pub struct WindowOptions {
61 pub title: String,
63 pub width: i16,
65 pub height: i16,
67 pub theme: Option<CoreTheme>,
69 pub locale: Option<Locale>,
71 pub dpi_scale: Option<usize>,
73 pub backend: Option<DesktopRenderBackend>,
75 pub scroll_list_id: Option<u32>,
77}
78
79impl Default for WindowOptions {
80 fn default() -> Self {
81 Self {
82 title: String::from("Velox Application"),
83 width: 960,
84 height: 640,
85 theme: None,
86 locale: None,
87 dpi_scale: None,
88 backend: None,
89 scroll_list_id: None,
90 }
91 }
92}
93
94impl WindowOptions {
95 pub fn new(title: impl Into<String>) -> Self {
97 Self {
98 title: title.into(),
99 ..Self::default()
100 }
101 }
102
103 pub fn title(mut self, title: impl Into<String>) -> Self {
105 self.title = title.into();
106 self
107 }
108
109 #[inline]
111 pub const fn size(mut self, width: i16, height: i16) -> Self {
112 self.width = width;
113 self.height = height;
114 self
115 }
116
117 #[inline]
119 pub const fn theme(mut self, theme: CoreTheme) -> Self {
120 self.theme = Some(theme);
121 self
122 }
123
124 #[inline]
126 pub const fn locale(mut self, locale: Locale) -> Self {
127 self.locale = Some(locale);
128 self
129 }
130
131 #[inline]
133 pub const fn dpi_scale(mut self, scale: usize) -> Self {
134 self.dpi_scale = Some(scale);
135 self
136 }
137
138 #[inline]
140 pub const fn backend(mut self, backend: DesktopRenderBackend) -> Self {
141 self.backend = Some(backend);
142 self
143 }
144
145 #[inline]
147 pub const fn software(mut self) -> Self {
148 self.backend = Some(DesktopRenderBackend::Software);
149 self
150 }
151
152 #[inline]
154 pub const fn gpu(mut self) -> Self {
155 self.backend = Some(DesktopRenderBackend::Wgpu);
156 self
157 }
158
159 #[inline]
161 pub const fn scroll_list_id(mut self, id: u32) -> Self {
162 self.scroll_list_id = Some(id);
163 self
164 }
165}
166
167#[derive(Copy, Clone, PartialEq, Eq, Debug)]
169pub struct WindowHandle<V> {
170 root: Handle<V>,
171}
172
173impl<V> WindowHandle<V> {
174 #[inline(always)]
176 pub const fn root(&self) -> Handle<V> {
177 self.root
178 }
179}
180
181pub struct WindowContext<'a> {
183 engine: &'a mut VeloxEngine,
184}
185
186impl<'a> WindowContext<'a> {
187 #[inline]
189 pub fn new<V: View>(&mut self, build: impl FnOnce(&mut Self) -> V) -> Handle<V> {
190 let view = build(self);
191 self.engine.mount_root(view)
192 }
193
194 #[inline]
196 pub fn insert_view<V: View>(&mut self, view: V) -> Handle<V> {
197 self.engine.insert_view(view)
198 }
199
200 #[inline]
202 pub fn mount_root<V: View>(&mut self, view: V) -> Handle<V> {
203 self.engine.mount_root(view)
204 }
205
206 #[inline(always)]
208 pub fn engine(&self) -> &VeloxEngine {
209 self.engine
210 }
211
212 #[inline(always)]
214 pub fn engine_mut(&mut self) -> &mut VeloxEngine {
215 self.engine
216 }
217
218 #[inline]
220 pub fn scheduler_name(&self) -> &'static str {
221 self.engine.scheduler_name()
222 }
223
224 #[inline]
226 pub fn spawn_for<V, T, F, Fut>(&mut self, handle: Handle<V>, f: F) -> Task<T>
227 where
228 V: View,
229 T: 'static,
230 F: FnOnce(Handle<V>, crate::async_rt::AsyncApp) -> Fut,
231 Fut: core::future::Future<Output = T> + 'static,
232 {
233 self.engine.spawn_for(handle, f)
234 }
235}
236
237pub struct BuiltWindow {
239 pub options: WindowOptions,
240 pub engine: VeloxEngine,
241}
242
243pub struct AppContext {
245 scheduler: Arc<dyn SchedulerBackend>,
246 theme: CoreTheme,
247 locale: Option<Locale>,
248 static_catalog: Option<&'static StaticCatalog>,
249 dynamic_catalog: Option<Arc<DynamicCatalog>>,
250 backend: DesktopRenderBackend,
251 dpi_scale: Option<usize>,
252 primary_window: Option<BuiltWindow>,
253}
254
255impl AppContext {
256 pub fn set_scheduler(&mut self, scheduler: Arc<dyn SchedulerBackend>) {
258 self.scheduler = scheduler;
259 }
260
261 #[inline]
263 pub fn scheduler_name(&self) -> &'static str {
264 self.scheduler.name()
265 }
266
267 #[inline]
269 pub fn set_theme(&mut self, theme: CoreTheme) {
270 self.theme = theme;
271 }
272
273 #[inline]
275 pub fn set_locale(&mut self, locale: Locale) {
276 self.locale = Some(locale);
277 }
278
279 #[inline]
281 pub fn set_i18n_catalog(&mut self, catalog: &'static StaticCatalog) {
282 self.static_catalog = Some(catalog);
283 }
284
285 #[inline]
287 pub fn set_dynamic_catalog(&mut self, catalog: Arc<DynamicCatalog>) {
288 self.dynamic_catalog = Some(catalog);
289 }
290
291 #[inline]
293 pub fn set_backend(&mut self, backend: DesktopRenderBackend) {
294 self.backend = backend;
295 }
296
297 pub fn open_window<V: View>(
299 &mut self,
300 options: WindowOptions,
301 build_root: impl FnOnce(&mut WindowContext<'_>) -> Handle<V>,
302 ) -> Result<WindowHandle<V>, AppError> {
303 if self.primary_window.is_some() {
304 return Err(AppError::WindowAlreadyOpen);
305 }
306
307 let w = options.width.max(1);
308 let h = options.height.max(1);
309 let mut engine = VeloxEngine::new(w, h).with_scheduler(Arc::clone(&self.scheduler));
310 engine.set_theme(options.theme.unwrap_or(self.theme));
311 if let Some(loc) = options.locale.or(self.locale) {
312 engine.set_locale(loc);
313 }
314 if let Some(cat) = self.static_catalog {
315 engine.set_i18n_catalog(cat);
316 }
317 if let Some(cat) = &self.dynamic_catalog {
318 engine.set_dynamic_catalog(Arc::clone(cat));
319 }
320
321 let root = {
322 let mut wcx = WindowContext {
323 engine: &mut engine,
324 };
325 build_root(&mut wcx)
326 };
327
328 let mut merged_opts = options;
329 if merged_opts.backend.is_none() {
330 merged_opts.backend = Some(self.backend);
331 }
332 if merged_opts.dpi_scale.is_none() {
333 merged_opts.dpi_scale = self.dpi_scale;
334 }
335
336 self.primary_window = Some(BuiltWindow {
337 options: merged_opts,
338 engine,
339 });
340 Ok(WindowHandle { root })
341 }
342}
343
344pub struct Application {
346 scheduler: Arc<dyn SchedulerBackend>,
347 theme: CoreTheme,
348 locale: Option<Locale>,
349 static_catalog: Option<&'static StaticCatalog>,
350 dynamic_catalog: Option<Arc<DynamicCatalog>>,
351 backend: DesktopRenderBackend,
352 dpi_scale: Option<usize>,
353}
354
355impl Default for Application {
356 fn default() -> Self {
357 Self::new()
358 }
359}
360
361impl Application {
362 pub fn new() -> Self {
364 Self {
365 scheduler: NativeStdScheduler::global(),
366 theme: CoreTheme::dark(),
367 locale: None,
368 static_catalog: None,
369 dynamic_catalog: None,
370 backend: DesktopRenderBackend::default_desktop(),
371 dpi_scale: None,
372 }
373 }
374
375 pub fn with_scheduler(mut self, scheduler: Arc<dyn SchedulerBackend>) -> Self {
377 self.scheduler = scheduler;
378 self
379 }
380
381 #[inline]
383 pub const fn with_theme(mut self, theme: CoreTheme) -> Self {
384 self.theme = theme;
385 self
386 }
387
388 #[inline]
390 pub const fn with_locale(mut self, locale: Locale) -> Self {
391 self.locale = Some(locale);
392 self
393 }
394
395 #[inline]
397 pub const fn with_i18n_catalog(mut self, catalog: &'static StaticCatalog) -> Self {
398 self.static_catalog = Some(catalog);
399 self
400 }
401
402 pub fn with_dynamic_catalog(mut self, catalog: Arc<DynamicCatalog>) -> Self {
404 self.dynamic_catalog = Some(catalog);
405 self
406 }
407
408 #[inline]
410 pub const fn with_backend(mut self, backend: DesktopRenderBackend) -> Self {
411 self.backend = backend;
412 self
413 }
414
415 #[inline]
417 pub const fn with_dpi_scale(mut self, scale: usize) -> Self {
418 self.dpi_scale = Some(scale);
419 self
420 }
421
422 fn into_context(self) -> AppContext {
423 AppContext {
424 scheduler: self.scheduler,
425 theme: self.theme,
426 locale: self.locale,
427 static_catalog: self.static_catalog,
428 dynamic_catalog: self.dynamic_catalog,
429 backend: self.backend,
430 dpi_scale: self.dpi_scale,
431 primary_window: None,
432 }
433 }
434
435 pub fn build_headless(self, setup: impl FnOnce(&mut AppContext)) -> Option<BuiltWindow> {
437 let mut cx = self.into_context();
438 setup(&mut cx);
439 cx.primary_window
440 }
441
442 pub fn run(self, setup: impl FnOnce(&mut AppContext)) {
444 let Some(built) = self.build_headless(setup) else {
445 return;
446 };
447 let BuiltWindow { options, engine } = built;
448 let mut plat = WinitPlatform::new(
449 options.title,
450 options.width.max(1) as usize,
451 options.height.max(1) as usize,
452 )
453 .with_backend(
454 options
455 .backend
456 .unwrap_or_else(DesktopRenderBackend::default_desktop),
457 );
458 if let Some(scale) = options.dpi_scale {
459 plat = plat.with_dpi_scale(scale);
460 }
461 if let Some(scroll_id) = options.scroll_list_id {
462 plat.run(engine, Some(scroll_id));
463 } else {
464 plat.run_event_loop(engine);
465 }
466 }
467}
468
469#[inline]
471pub fn application() -> Application {
472 Application::new()
473}
474
475#[inline]
477pub fn init(_cx: &mut AppContext) {}
478
479#[inline]
481pub fn open_window<V: View>(
482 options: WindowOptions,
483 cx: &mut AppContext,
484 build_root: impl FnOnce(&mut WindowContext<'_>) -> Handle<V>,
485) -> Result<WindowHandle<V>, AppError> {
486 cx.open_window(options, build_root)
487}