1use super::*;
2
3pub fn create_showcase_app(
14 headless: bool,
15 mode: RenderMode,
16) -> Result<(App, ShowcaseState), PlatformError> {
17 let render_cfg: RenderConfig = match mode {
18 RenderMode::Software => RenderConfig::software(),
19 RenderMode::Hardware => RenderConfig::hardware(),
20 RenderMode::Hybrid => RenderConfig::hybrid(),
21 RenderMode::Auto => RenderConfig::default(),
22 };
23 let mut window_cfg: WindowConfig = WindowConfig::default();
24 window_cfg
25 .set_title(SHOWCASE_WINDOW_TITLE.to_string())
26 .set_size(SHOWCASE_WINDOW_WIDTH, SHOWCASE_WINDOW_HEIGHT)
27 .set_render(render_cfg);
28 let mut app_cfg: AppConfig = AppConfig::default();
29 app_cfg.set_window(window_cfg);
30 let mut app: App = if headless {
31 App::headless(app_cfg)?
32 } else {
33 App::new(app_cfg)?
34 };
35 let mut state: ShowcaseState = ShowcaseState::new();
36 let host_plat_idx: usize = match app.platform_kind() {
37 PlatformKind::Ios => 0,
38 PlatformKind::IpadOs => 1,
39 PlatformKind::Android => 2,
40 PlatformKind::MacOs => 3,
41 PlatformKind::Windows => 4,
42 PlatformKind::Linux => 5,
43 PlatformKind::Web | PlatformKind::Headless => 6,
44 };
45 let _: bool = state
46 .get_os_target_platform_idx()
47 .set_if_changed(host_plat_idx);
48 state.attach_dynamic_hub(app.get_dynamic_hub().clone());
49 app.attach_game_host(state.get_game_host().clone());
50 app.attach_game_host(state.get_game3d_host().clone());
51 let mount_state: ShowcaseState = state.clone();
52 let _: NodeId = app.mount(move || mount_showcase_root(&mount_state));
53 Ok((app, state))
54}
55
56pub fn create_showcase_app_for_platform(
67 platform: PlatformKind,
68 mode: RenderMode,
69) -> Result<(App, ShowcaseState), PlatformError> {
70 let render_cfg: RenderConfig = match mode {
71 RenderMode::Software => RenderConfig::software(),
72 RenderMode::Hardware => RenderConfig::hardware(),
73 RenderMode::Hybrid => RenderConfig::hybrid(),
74 RenderMode::Auto => RenderConfig::default(),
75 };
76 let mut window_cfg: WindowConfig = WindowConfig::default();
77 window_cfg
78 .set_title(SHOWCASE_WINDOW_TITLE.to_string())
79 .set_size(SHOWCASE_WINDOW_WIDTH, SHOWCASE_WINDOW_HEIGHT)
80 .set_render(render_cfg);
81 let mut app_cfg: AppConfig = AppConfig::default();
82 app_cfg.set_window(window_cfg);
83 let backend: Box<dyn PlatformBackend> = Box::new(HeadlessPlatform::emulating(
84 platform,
85 1.0,
86 SafeAreaInsets::default(),
87 ));
88 let mut app: App = App::with_platform(app_cfg, backend)?;
89 let mut state: ShowcaseState = ShowcaseState::new();
90 let host_plat_idx: usize = match app.platform_kind() {
91 PlatformKind::Ios => 0,
92 PlatformKind::IpadOs => 1,
93 PlatformKind::Android => 2,
94 PlatformKind::MacOs => 3,
95 PlatformKind::Windows => 4,
96 PlatformKind::Linux => 5,
97 PlatformKind::Web | PlatformKind::Headless => 6,
98 };
99 let _: bool = state
100 .get_os_target_platform_idx()
101 .set_if_changed(host_plat_idx);
102 state.attach_dynamic_hub(app.get_dynamic_hub().clone());
103 app.attach_game_host(state.get_game_host().clone());
104 app.attach_game_host(state.get_game3d_host().clone());
105 let mount_state: ShowcaseState = state.clone();
106 let _: NodeId = app.mount(move || mount_showcase_root(&mount_state));
107 Ok((app, state))
108}
109
110pub fn run_headless_smoke(mode: RenderMode) -> Result<FrameStats, PlatformError> {
120 let (mut app, _): (App, ShowcaseState) = create_showcase_app(true, mode)?;
121 app.step_frame()
122}
123
124#[cfg(not(target_arch = "wasm32"))]
138fn dump_rgba_frame(
139 app: &mut App,
140 out_dir: &Path,
141 name: &str,
142 manifest: &mut String,
143 count: &mut usize,
144) -> Result<(), PlatformError> {
145 let _: FrameStats = app.step_frame()?;
146 for _ in 0..20 {
147 if !app.has_active_animations() {
148 break;
149 }
150 let _: FrameStats = app.step_game_frame(1.0 / 60.0)?;
151 }
152 if let Some(fb) = app.get_renderer().framebuffer() {
153 let file_name: String = format!("{name}.rgba");
154 let file_path: PathBuf = out_dir.join(&file_name);
155 fs::write(&file_path, fb.get_pixels().as_slice())
156 .map_err(|err: Error| PlatformError::SurfaceError(err.to_string()))?;
157 manifest.push_str(&format!("{name} {} {}\n", fb.get_width(), fb.get_height()));
158 *count += 1;
159 }
160 Ok(())
161}
162
163#[cfg(not(target_arch = "wasm32"))]
173pub fn capture_showcase_screenshots(out_dir: &Path) -> Result<usize, PlatformError> {
174 reset_reactive_runtime();
175 fs::create_dir_all(out_dir)
176 .map_err(|err: Error| PlatformError::InitializationFailed(err.to_string()))?;
177 let mut window_cfg: WindowConfig = WindowConfig::default();
178 window_cfg
179 .set_title(SHOWCASE_WINDOW_TITLE.to_string())
180 .set_size(1200, 800)
181 .set_render(RenderConfig::software());
182 let mut app_cfg: AppConfig = AppConfig::default();
183 app_cfg.set_window(window_cfg);
184 let backend: Box<dyn PlatformBackend> = Box::new(HeadlessPlatform::emulating(
185 PlatformKind::Headless,
186 2.0,
187 SafeAreaInsets::default(),
188 ));
189 let mut app: App = App::with_platform(app_cfg, backend)?;
190 let mut state: ShowcaseState = ShowcaseState::new();
191 state.attach_dynamic_hub(app.get_dynamic_hub().clone());
192 app.attach_game_host(state.get_game_host().clone());
193 app.attach_game_host(state.get_game3d_host().clone());
194 let mount_state: ShowcaseState = state.clone();
195 let _: NodeId = app.mount(move || mount_showcase_root(&mount_state));
196 app.get_mut_renderer()
197 .get_mut_software()
198 .set_font_provider(NativeSystemFont::system());
199 let wid: WindowId = app.get_window_state().get_id();
200 let mut manifest: String = String::new();
201 let mut count: usize = 0;
202 app.dispatch_window_event(WindowEvent::Resized(wid, 1200, 800, 2.0));
203 let desktop_routes: [(&str, &str); 12] = [
204 (ROUTE_OVERVIEW, "01_overview_desktop"),
205 (ROUTE_REACTIVE, "02_reactive_desktop"),
206 (ROUTE_RENDER, "03_render_desktop"),
207 (ROUTE_WINDOW, "04_window_desktop"),
208 (ROUTE_FORMS, "05_forms_desktop"),
209 (ROUTE_THEMES, "06_themes_desktop"),
210 (ROUTE_DATA, "07_data_desktop"),
211 (ROUTE_GAME, "08_game_desktop"),
212 (ROUTE_GAME_3D, "08b_game3d_desktop"),
213 (ROUTE_MEDIA, "08c_media_desktop"),
214 (ROUTE_OVERLAYS, "09_overlays_desktop"),
215 (ROUTE_DYNAMIC, "10_dynamic_desktop"),
216 ];
217 for (route, label) in desktop_routes.iter() {
218 let _: bool = state.get_route().set_if_changed((*route).to_string());
219 if *route == ROUTE_OVERVIEW {
220 app.dispatch_window_event(WindowEvent::PointerMove(
221 wid,
222 PointerEvent::mouse(100.0, 108.0),
223 ));
224 }
225 dump_rgba_frame(&mut app, out_dir, label, &mut manifest, &mut count)?;
226 }
227 let _: bool = state.get_route().set_if_changed(ROUTE_FORMS.to_string());
228 let _: FrameStats = app.step_frame()?;
229 app.dispatch_window_event(WindowEvent::PointerMove(
230 wid,
231 PointerEvent::mouse(600.0, 400.0),
232 ));
233 app.dispatch_window_event(WindowEvent::Scroll(wid, Point::new(0.0, 420.0)));
234 dump_rgba_frame(
235 &mut app,
236 out_dir,
237 "05b_forms_scrolled",
238 &mut manifest,
239 &mut count,
240 )?;
241 let _: bool = state.get_route().set_if_changed(ROUTE_DATA.to_string());
242 let _: FrameStats = app.step_frame()?;
243 app.dispatch_window_event(WindowEvent::PointerMove(
244 wid,
245 PointerEvent::mouse(600.0, 400.0),
246 ));
247 app.dispatch_window_event(WindowEvent::Scroll(wid, Point::new(0.0, 460.0)));
248 dump_rgba_frame(
249 &mut app,
250 out_dir,
251 "07b_data_vlist_scrolled",
252 &mut manifest,
253 &mut count,
254 )?;
255 app.dispatch_window_event(WindowEvent::PointerMove(
256 wid,
257 PointerEvent::mouse(320.0, 120.0),
258 ));
259 app.dispatch_window_event(WindowEvent::Scroll(wid, Point::new(0.0, 720.0)));
260 dump_rgba_frame(
261 &mut app,
262 out_dir,
263 "07c_data_bottom",
264 &mut manifest,
265 &mut count,
266 )?;
267 let _: bool = state.get_route().set_if_changed(ROUTE_OVERVIEW.to_string());
268 let _: FrameStats = app.step_frame()?;
269 app.dispatch_window_event(WindowEvent::PointerMove(
270 wid,
271 PointerEvent::mouse(100.0, 108.0),
272 ));
273 dump_rgba_frame(
274 &mut app,
275 out_dir,
276 "01b_overview_hovered",
277 &mut manifest,
278 &mut count,
279 )?;
280 let _: bool = state.get_route().set_if_changed(ROUTE_OVERLAYS.to_string());
281 let _: bool = state.get_modal_open().set_if_changed(true);
282 dump_rgba_frame(
283 &mut app,
284 out_dir,
285 "09b_overlays_modal",
286 &mut manifest,
287 &mut count,
288 )?;
289 let _: bool = state.get_modal_open().set_if_changed(false);
290 let _: bool = state.get_alert_dialog_open().set_if_changed(true);
291 dump_rgba_frame(
292 &mut app,
293 out_dir,
294 "09c_overlays_alert",
295 &mut manifest,
296 &mut count,
297 )?;
298 let _: bool = state.get_alert_dialog_open().set_if_changed(false);
299 let _: bool = state.get_hover_card_open().set_if_changed(true);
300 dump_rgba_frame(
301 &mut app,
302 out_dir,
303 "09d_overlays_hovercard",
304 &mut manifest,
305 &mut count,
306 )?;
307 let _: bool = state.get_hover_card_open().set_if_changed(false);
308 let _: bool = state.get_route().set_if_changed(ROUTE_THEMES.to_string());
309 state.toggle_theme_mode();
310 dump_rgba_frame(
311 &mut app,
312 out_dir,
313 "06b_themes_dark",
314 &mut manifest,
315 &mut count,
316 )?;
317 state.toggle_theme_mode();
318 let _: bool = state.get_route().set_if_changed(ROUTE_OVERVIEW.to_string());
319 app.dispatch_window_event(WindowEvent::Resized(wid, 768, 1024, 2.0));
320 dump_rgba_frame(
321 &mut app,
322 out_dir,
323 "11_overview_tablet",
324 &mut manifest,
325 &mut count,
326 )?;
327 app.dispatch_window_event(WindowEvent::Resized(wid, 390, 844, 2.0));
328 dump_rgba_frame(
329 &mut app,
330 out_dir,
331 "12_overview_mobile",
332 &mut manifest,
333 &mut count,
334 )?;
335 app.dispatch_window_event(WindowEvent::PointerDown(
336 wid,
337 PointerEvent::mouse(350.0, 26.0),
338 ));
339 app.dispatch_window_event(WindowEvent::PointerUp(
340 wid,
341 PointerEvent::mouse(350.0, 26.0),
342 ));
343 dump_rgba_frame(
344 &mut app,
345 out_dir,
346 "12b_mobile_menu_open",
347 &mut manifest,
348 &mut count,
349 )?;
350 app.dispatch_window_event(WindowEvent::PointerDown(
351 wid,
352 PointerEvent::mouse(240.0, 26.0),
353 ));
354 app.dispatch_window_event(WindowEvent::PointerUp(
355 wid,
356 PointerEvent::mouse(240.0, 26.0),
357 ));
358 let _: bool = state.get_route().set_if_changed(ROUTE_FORMS.to_string());
359 dump_rgba_frame(
360 &mut app,
361 out_dir,
362 "13_forms_mobile",
363 &mut manifest,
364 &mut count,
365 )?;
366 fs::write(out_dir.join("manifest.txt"), manifest)
367 .map_err(|err: Error| PlatformError::SurfaceError(err.to_string()))?;
368 Ok(count)
369}
370
371#[cfg(target_arch = "wasm32")]
373#[wasm_bindgen(start)]
374pub fn euv_web_boot() {
375 let _: u64 = euv_example_web_start();
376 let Some(win): Option<Window> = window() else {
377 return;
378 };
379 let Some(doc): Option<Document> = win.document() else {
380 return;
381 };
382 let Ok(Some(el)) = doc.query_selector(WEB_SELECTOR_CANVAS) else {
383 return;
384 };
385 let Ok(canvas) = el.dyn_into::<HtmlCanvasElement>() else {
386 return;
387 };
388 let _: bool = web_sync_exact_viewport(&win, &canvas);
389 let canvas_move: HtmlCanvasElement = canvas.clone();
390 let on_move: Closure<dyn FnMut(web_sys::PointerEvent)> =
391 Closure::wrap(Box::new(move |ev: web_sys::PointerEvent| {
392 let ptr: PointerEvent = web_pointer_from_dom(&ev, &canvas_move, None);
393 web_queue_pointer_move_event(ptr);
394 }) as Box<dyn FnMut(web_sys::PointerEvent)>);
395 drop(canvas.add_event_listener_with_callback(
396 WEB_EVENT_POINTER_MOVE,
397 on_move.as_ref().unchecked_ref(),
398 ));
399 on_move.forget();
400 let canvas_down: HtmlCanvasElement = canvas.clone();
401 let on_down: Closure<dyn FnMut(web_sys::PointerEvent)> =
402 Closure::wrap(Box::new(move |ev: web_sys::PointerEvent| {
403 drop(canvas_down.focus());
404 drop(canvas_down.set_pointer_capture(ev.pointer_id()));
405 let ptr: PointerEvent = web_pointer_from_dom(&ev, &canvas_down, Some(true));
406 web_queue_pointer_down_event(ptr);
407 }) as Box<dyn FnMut(web_sys::PointerEvent)>);
408 drop(canvas.add_event_listener_with_callback(
409 WEB_EVENT_POINTER_DOWN,
410 on_down.as_ref().unchecked_ref(),
411 ));
412 on_down.forget();
413 let canvas_up: HtmlCanvasElement = canvas.clone();
414 let on_up: Closure<dyn FnMut(web_sys::PointerEvent)> =
415 Closure::wrap(Box::new(move |ev: web_sys::PointerEvent| {
416 drop(canvas_up.release_pointer_capture(ev.pointer_id()));
417 let ptr: PointerEvent = web_pointer_from_dom(&ev, &canvas_up, Some(false));
418 web_queue_pointer_up_event(ptr);
419 }) as Box<dyn FnMut(web_sys::PointerEvent)>);
420 drop(
421 canvas
422 .add_event_listener_with_callback(WEB_EVENT_POINTER_UP, on_up.as_ref().unchecked_ref()),
423 );
424 on_up.forget();
425 let on_cancel: Closure<dyn FnMut(web_sys::PointerEvent)> =
426 Closure::wrap(Box::new(move |ev: web_sys::PointerEvent| {
427 web_queue_pointer_cancel(web_pointer_id_from_dom(&ev));
428 }) as Box<dyn FnMut(web_sys::PointerEvent)>);
429 drop(canvas.add_event_listener_with_callback(
430 WEB_EVENT_POINTER_CANCEL,
431 on_cancel.as_ref().unchecked_ref(),
432 ));
433 drop(canvas.add_event_listener_with_callback(
434 WEB_EVENT_LOST_POINTER_CAPTURE,
435 on_cancel.as_ref().unchecked_ref(),
436 ));
437 on_cancel.forget();
438 let canvas_win_up: HtmlCanvasElement = canvas.clone();
439 let on_win_up: Closure<dyn FnMut(web_sys::PointerEvent)> =
440 Closure::wrap(Box::new(move |ev: web_sys::PointerEvent| {
441 let ptr: PointerEvent = web_pointer_from_dom(&ev, &canvas_win_up, Some(false));
442 web_queue_pointer_up_event(ptr);
443 }) as Box<dyn FnMut(web_sys::PointerEvent)>);
444 drop(win.add_event_listener_with_callback(
445 WEB_EVENT_POINTER_UP,
446 on_win_up.as_ref().unchecked_ref(),
447 ));
448 on_win_up.forget();
449 let on_win_cancel: Closure<dyn FnMut(web_sys::PointerEvent)> =
450 Closure::wrap(Box::new(move |ev: web_sys::PointerEvent| {
451 web_queue_pointer_cancel(web_pointer_id_from_dom(&ev));
452 }) as Box<dyn FnMut(web_sys::PointerEvent)>);
453 drop(win.add_event_listener_with_callback(
454 WEB_EVENT_POINTER_CANCEL,
455 on_win_cancel.as_ref().unchecked_ref(),
456 ));
457 on_win_cancel.forget();
458 let on_blur: Closure<dyn FnMut(Event)> = Closure::wrap(Box::new(move |_: Event| {
459 web_queue_pointer_cancel(0);
460 }) as Box<dyn FnMut(Event)>);
461 drop(win.add_event_listener_with_callback(WEB_EVENT_BLUR, on_blur.as_ref().unchecked_ref()));
462 on_blur.forget();
463 let on_wheel: Closure<dyn FnMut(WheelEvent)> = Closure::wrap(Box::new(move |ev: WheelEvent| {
464 ev.prevent_default();
465 let mult: f32 = match ev.delta_mode() {
466 1 => 20.0,
467 2 => 320.0,
468 _ => 1.0,
469 };
470 web_queue_scroll((ev.delta_x() as f32) * mult, (ev.delta_y() as f32) * mult);
471 })
472 as Box<dyn FnMut(WheelEvent)>);
473 drop(
474 canvas.add_event_listener_with_callback(WEB_EVENT_WHEEL, on_wheel.as_ref().unchecked_ref()),
475 );
476 on_wheel.forget();
477 let on_key: Closure<dyn FnMut(KeyboardEvent)> =
478 Closure::wrap(Box::new(move |ev: KeyboardEvent| {
479 let key: String = ev.key();
480 let code_str: String = ev.code();
481 if matches!(
482 key.as_str(),
483 WEB_KEY_ARROW_UP
484 | WEB_KEY_ARROW_DOWN
485 | WEB_KEY_ARROW_LEFT
486 | WEB_KEY_ARROW_RIGHT
487 | WEB_KEY_BACKSPACE
488 | WEB_KEY_SPACE
489 ) {
490 ev.prevent_default();
491 }
492 web_queue_key_down(&key, &code_str);
493 }) as Box<dyn FnMut(KeyboardEvent)>);
494 drop(win.add_event_listener_with_callback(WEB_EVENT_KEY_DOWN, on_key.as_ref().unchecked_ref()));
495 on_key.forget();
496 let on_key_up: Closure<dyn FnMut(KeyboardEvent)> =
497 Closure::wrap(Box::new(move |ev: KeyboardEvent| {
498 let key: String = ev.key();
499 let code_str: String = ev.code();
500 web_queue_key_up(&key, &code_str);
501 }) as Box<dyn FnMut(KeyboardEvent)>);
502 drop(
503 win.add_event_listener_with_callback(WEB_EVENT_KEY_UP, on_key_up.as_ref().unchecked_ref()),
504 );
505 on_key_up.forget();
506 let win_resize: Window = win.clone();
507 let canvas_resize: HtmlCanvasElement = canvas.clone();
508 let on_resize: Closure<dyn FnMut(Event)> = Closure::wrap(Box::new(move |_: Event| {
509 if web_sync_exact_viewport(&win_resize, &canvas_resize) {
510 web_mark_needs_frame();
511 }
512 }) as Box<dyn FnMut(Event)>);
513 drop(
514 win.add_event_listener_with_callback(WEB_EVENT_RESIZE, on_resize.as_ref().unchecked_ref()),
515 );
516 on_resize.forget();
517 let raf_cell: WebRafSlot = Rc::new(RefCell::new(None));
518 let raf_loop: WebRafSlot = raf_cell.clone();
519 let win_raf: Window = win.clone();
520 let canvas_raf: HtmlCanvasElement = canvas.clone();
521 let last_ts: Rc<RefCell<f64>> = Rc::new(RefCell::new(0.0));
522 if let Ok(mut slot) = raf_cell.try_borrow_mut() {
523 *slot = Some(Closure::wrap(Box::new(move |ts: f64| {
524 let mut dt: f32 = 1.0 / 60.0;
525 if let Ok(mut prev) = last_ts.try_borrow_mut() {
526 if *prev > 0.0 {
527 dt = (((ts - *prev) / 1000.0) as f32).clamp(0.001, 0.05);
528 }
529 *prev = ts;
530 }
531 let resized_or_scaled: bool = web_sync_exact_viewport(&win_raf, &canvas_raf);
532 if resized_or_scaled {
533 let _: u64 = web_flush_frame(dt);
534 }
535 let cur_route: String = euv_web_current_route();
536 let on_game: bool = cur_route == ROUTE_GAME || cur_route == ROUTE_GAME_3D;
537 let animating: bool = web_has_active_animations();
538 let dirty: bool = WEB_NEEDS_FRAME.with(|flag: &Cell<bool>| flag.replace(false));
539 if on_game || animating {
540 let _: u64 = euv_web_tick(dt);
541 } else if dirty && !resized_or_scaled {
542 let _: u64 = web_flush_frame(dt);
543 }
544 if let Ok(cb_ref) = raf_loop.try_borrow()
545 && let Some(cb) = cb_ref.as_ref()
546 {
547 let func: &Function = cb.as_ref().unchecked_ref();
548 drop(win_raf.request_animation_frame(func));
549 }
550 }) as Box<dyn FnMut(f64)>));
551 }
552 if let Ok(cb_ref) = raf_cell.try_borrow()
553 && let Some(cb) = cb_ref.as_ref()
554 {
555 let func: &Function = cb.as_ref().unchecked_ref();
556 drop(win.request_animation_frame(func));
557 }
558}
559
560#[cfg(target_arch = "wasm32")]
571fn web_map_key(key: &str, code_str: &str) -> KeyCode {
572 match code_str {
573 "KeyW" => return KeyCode::Character('w'),
574 "KeyA" => return KeyCode::Character('a'),
575 "KeyS" => return KeyCode::Character('s'),
576 "KeyD" => return KeyCode::Character('d'),
577 "KeyE" => return KeyCode::Character('e'),
578 "KeyR" => return KeyCode::Character('r'),
579 "KeyP" => return KeyCode::Character('p'),
580 "KeyJ" => return KeyCode::Character('j'),
581 "Space" => return KeyCode::Space,
582 "ArrowLeft" => return KeyCode::ArrowLeft,
583 "ArrowRight" => return KeyCode::ArrowRight,
584 "ArrowUp" => return KeyCode::ArrowUp,
585 "ArrowDown" => return KeyCode::ArrowDown,
586 _ => {}
587 }
588 match key {
589 WEB_KEY_ENTER => KeyCode::Enter,
590 WEB_KEY_BACKSPACE => KeyCode::Backspace,
591 WEB_KEY_DELETE => KeyCode::Delete,
592 WEB_KEY_ESCAPE => KeyCode::Escape,
593 WEB_KEY_TAB => KeyCode::Tab,
594 WEB_KEY_SPACE => KeyCode::Space,
595 WEB_KEY_ARROW_LEFT => KeyCode::ArrowLeft,
596 WEB_KEY_ARROW_RIGHT => KeyCode::ArrowRight,
597 WEB_KEY_ARROW_UP => KeyCode::ArrowUp,
598 WEB_KEY_ARROW_DOWN => KeyCode::ArrowDown,
599 other => other
600 .chars()
601 .next()
602 .map(KeyCode::Character)
603 .unwrap_or(KeyCode::Unknown),
604 }
605}
606
607#[cfg(target_arch = "wasm32")]
613fn web_has_active_animations() -> bool {
614 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
615 match slot.try_borrow() {
616 Ok(guard) => guard
617 .as_ref()
618 .map(|(app, _): &(App, ShowcaseState)| app.has_active_animations())
619 .unwrap_or(false),
620 Err(_) => false,
621 }
622 })
623}
624
625#[cfg(target_arch = "wasm32")]
627fn web_mark_needs_frame() {
628 WEB_NEEDS_FRAME.with(|flag: &Cell<bool>| flag.set(true));
629}
630
631#[cfg(target_arch = "wasm32")]
641fn web_pointer_id_from_dom(ev: &web_sys::PointerEvent) -> u64 {
642 (ev.pointer_id() as i64).unsigned_abs().saturating_add(1)
643}
644
645#[cfg(target_arch = "wasm32")]
657fn web_pointer_from_dom(
658 ev: &web_sys::PointerEvent,
659 canvas: &HtmlCanvasElement,
660 force_pressed: Option<bool>,
661) -> PointerEvent {
662 let rect: DomRect = canvas.get_bounding_client_rect();
663 let x: f32 = (ev.client_x() as f64 - rect.left()) as f32;
664 let y: f32 = (ev.client_y() as f64 - rect.top()) as f32;
665 let pid: u64 = web_pointer_id_from_dom(ev);
666 let ptype: String = ev.pointer_type();
667 let device: PointerDevice = if ptype == WEB_PTR_TYPE_TOUCH {
668 PointerDevice::Touch
669 } else if ptype == WEB_PTR_TYPE_PEN {
670 PointerDevice::Stylus
671 } else {
672 PointerDevice::Mouse
673 };
674 let is_pressed: bool =
675 force_pressed.unwrap_or_else(|| ev.buttons() != 0 || device == PointerDevice::Touch);
676 let pressure: f32 = if is_pressed { 1.0 } else { 0.0 };
677 PointerEvent::new(
678 pid,
679 device,
680 Point::new(x, y),
681 PointerButton::Primary,
682 pressure,
683 Modifiers::default(),
684 )
685}
686
687#[cfg(target_arch = "wasm32")]
693fn web_queue_pointer_move_event(ptr: PointerEvent) {
694 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
695 if let Ok(mut borrow) = slot.try_borrow_mut()
696 && let Some((app, _)) = borrow.as_mut()
697 {
698 let wid: WindowId = app.get_window_state().get_id();
699 app.dispatch_window_event(WindowEvent::PointerMove(wid, ptr));
700 web_mark_needs_frame();
701 }
702 });
703}
704
705#[cfg(target_arch = "wasm32")]
711fn web_queue_pointer_down_event(ptr: PointerEvent) {
712 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
713 if let Ok(mut borrow) = slot.try_borrow_mut()
714 && let Some((app, _)) = borrow.as_mut()
715 {
716 let wid: WindowId = app.get_window_state().get_id();
717 app.dispatch_window_event(WindowEvent::PointerDown(wid, ptr));
718 web_mark_needs_frame();
719 }
720 });
721}
722
723#[cfg(target_arch = "wasm32")]
729fn web_queue_pointer_up_event(ptr: PointerEvent) {
730 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
731 if let Ok(mut borrow) = slot.try_borrow_mut()
732 && let Some((app, _)) = borrow.as_mut()
733 {
734 let wid: WindowId = app.get_window_state().get_id();
735 app.dispatch_window_event(WindowEvent::PointerUp(wid, ptr));
736 web_mark_needs_frame();
737 }
738 });
739}
740
741#[cfg(target_arch = "wasm32")]
747fn web_queue_pointer_cancel(pointer_id: u64) {
748 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
749 if let Ok(mut borrow) = slot.try_borrow_mut()
750 && let Some((app, _)) = borrow.as_mut()
751 {
752 let wid: WindowId = app.get_window_state().get_id();
753 app.dispatch_window_event(WindowEvent::PointerCancel(wid, pointer_id));
754 web_mark_needs_frame();
755 }
756 });
757}
758
759#[cfg(target_arch = "wasm32")]
766fn web_queue_scroll(dx: f32, dy: f32) {
767 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
768 if let Ok(mut borrow) = slot.try_borrow_mut()
769 && let Some((app, _)) = borrow.as_mut()
770 {
771 let wid: WindowId = app.get_window_state().get_id();
772 app.dispatch_window_event(WindowEvent::Scroll(wid, Point::new(dx, dy)));
773 web_mark_needs_frame();
774 }
775 });
776}
777
778#[cfg(target_arch = "wasm32")]
785fn web_queue_key_down(key: &str, code_str: &str) {
786 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
787 if let Ok(mut borrow) = slot.try_borrow_mut()
788 && let Some((app, _)) = borrow.as_mut()
789 {
790 let wid: WindowId = app.get_window_state().get_id();
791 let code: KeyCode = web_map_key(key, code_str);
792 let key_ev: WindowEvent =
793 WindowEvent::KeyDown(wid, KeyEvent::new(code, Modifiers::default(), false));
794 app.dispatch_window_event(key_ev);
795 if key.chars().count() == 1 {
796 app.dispatch_window_event(WindowEvent::TextInput(wid, key.to_string()));
797 }
798 web_mark_needs_frame();
799 }
800 });
801}
802
803#[cfg(target_arch = "wasm32")]
810fn web_queue_key_up(key: &str, code_str: &str) {
811 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
812 if let Ok(mut borrow) = slot.try_borrow_mut()
813 && let Some((app, _)) = borrow.as_mut()
814 {
815 let wid: WindowId = app.get_window_state().get_id();
816 let code: KeyCode = web_map_key(key, code_str);
817 app.dispatch_window_event(WindowEvent::KeyUp(
818 wid,
819 KeyEvent::new(code, Modifiers::default(), false),
820 ));
821 }
822 });
823}
824
825#[cfg(target_arch = "wasm32")]
836fn web_sync_exact_viewport(win: &Window, canvas: &HtmlCanvasElement) -> bool {
837 let rect: DomRect = canvas.get_bounding_client_rect();
838 let css_w: f64 = if rect.width() >= 320.0 {
839 rect.width()
840 } else {
841 win.inner_width()
842 .ok()
843 .and_then(|v: JsValue| v.as_f64())
844 .filter(|w: &f64| *w >= 320.0)
845 .unwrap_or(1200.0)
846 };
847 let css_h: f64 = if rect.height() >= 320.0 {
848 rect.height()
849 } else {
850 win.inner_height()
851 .ok()
852 .and_then(|v: JsValue| v.as_f64())
853 .filter(|h: &f64| *h >= 320.0)
854 .unwrap_or(800.0)
855 };
856 let dpr_raw: f64 = win.device_pixel_ratio();
857 let dpr_f64: f64 = if dpr_raw.is_finite() && dpr_raw > 0.0 {
858 dpr_raw.clamp(0.5, 4.5)
859 } else {
860 1.0
861 };
862 let dpr: f32 = dpr_f64 as f32;
863 let phys_w: u32 = (css_w * dpr_f64).round().max(1.0) as u32;
864 let phys_h: u32 = (css_h * dpr_f64).round().max(1.0) as u32;
865 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
866 let Ok(mut borrow) = slot.try_borrow_mut() else {
867 return false;
868 };
869 let Some((app, _)) = borrow.as_mut() else {
870 return false;
871 };
872 let ws: WindowState = app.get_window_state();
873 let size_diff: bool = ws.get_physical_width() != phys_w
874 || ws.get_physical_height() != phys_h
875 || (ws.get_scale_factor() - dpr).abs() > 0.005
876 || canvas.width() != phys_w
877 || canvas.height() != phys_h;
878 if !size_diff {
879 return false;
880 }
881 if canvas.width() != phys_w {
882 canvas.set_width(phys_w);
883 }
884 if canvas.height() != phys_h {
885 canvas.set_height(phys_h);
886 }
887 app.set_exact_physical_viewport(phys_w, phys_h, dpr);
888 true
889 })
890}
891
892#[cfg(target_arch = "wasm32")]
902fn web_flush_frame(dt_secs: f32) -> u64 {
903 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
904 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
905 Ok(guard) => guard,
906 Err(_) => return 0,
907 };
908 let Some((app, _)) = borrow.as_mut() else {
909 return 0;
910 };
911 app.step_game_frame(dt_secs)
912 .map(|stats: FrameStats| stats.get_frame_hash())
913 .unwrap_or(0)
914 })
915}
916
917#[cfg(target_arch = "wasm32")]
923#[wasm_bindgen]
924pub fn euv_example_web_start() -> u64 {
925 reset_reactive_runtime();
926 match create_showcase_app(false, RenderMode::Software) {
927 Ok((mut app, state)) => {
928 if let Some(win) = window()
929 && let Ok(hash_str) = win.location().hash()
930 {
931 let cleaned: &str = hash_str.trim_start_matches('#');
932 if cleaned.starts_with('/') {
933 let _: bool = state.get_route().set_if_changed(cleaned.to_string());
934 }
935 }
936 let route_watch: Signal<String> = state.get_route();
937 let (hash_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
938 Effect::new(move || {
939 let r: String = route_watch.get();
940 if let Some(win) = window() {
941 let target_hash: String = format!("#{r}");
942 if win.location().hash().ok().as_deref() != Some(target_hash.as_str()) {
943 let _: Result<(), JsValue> = win.location().set_hash(&target_hash);
944 }
945 }
946 })
947 });
948 let root_id: NodeId = app
949 .get_scene()
950 .with_read(SceneTree::get_root, NodeId::default());
951 app.get_scene().with_write(
952 |tree: &mut SceneTree| {
953 tree.attach_scope(root_id, hash_scope);
954 },
955 (),
956 );
957 let hash: u64 = app
958 .step_frame()
959 .map(|stats: FrameStats| stats.get_frame_hash())
960 .unwrap_or(0);
961 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
962 if let Ok(mut borrow) = slot.try_borrow_mut() {
963 *borrow = Some((app, state));
964 }
965 });
966 hash
967 }
968 Err(_) => 0,
969 }
970}
971
972#[cfg(target_arch = "wasm32")]
982#[wasm_bindgen]
983pub fn euv_example_render_route(route: &str) -> u64 {
984 let has_runtime: bool = WEB_SHOWCASE_RUNTIME.with(
985 |slot: &RefCell<Option<(App, ShowcaseState)>>| matches!(slot.try_borrow(), Ok(guard) if guard.is_some()),
986 );
987 if !has_runtime {
988 let _: u64 = euv_example_web_start();
989 }
990 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
991 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
992 Ok(guard) => guard,
993 Err(_) => return 0,
994 };
995 let Some((app, state)) = borrow.as_mut() else {
996 return 0;
997 };
998 let _: bool = state.get_route().set_if_changed(route.to_string());
999 app.step_frame()
1000 .map(|stats: FrameStats| stats.get_frame_hash())
1001 .unwrap_or(0)
1002 })
1003}
1004
1005#[cfg(target_arch = "wasm32")]
1016#[wasm_bindgen]
1017pub fn euv_web_pointer_move(x: f32, y: f32) -> u64 {
1018 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1019 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1020 Ok(guard) => guard,
1021 Err(_) => return 0,
1022 };
1023 let Some((app, _)) = borrow.as_mut() else {
1024 return 0;
1025 };
1026 let wid: WindowId = app.get_window_state().get_id();
1027 app.dispatch_window_event(WindowEvent::PointerMove(wid, PointerEvent::mouse(x, y)));
1028 app.step_frame()
1029 .map(|stats: FrameStats| stats.get_frame_hash())
1030 .unwrap_or(0)
1031 })
1032}
1033
1034#[cfg(target_arch = "wasm32")]
1045#[wasm_bindgen]
1046pub fn euv_web_pointer_down(x: f32, y: f32) -> u64 {
1047 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1048 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1049 Ok(guard) => guard,
1050 Err(_) => return 0,
1051 };
1052 let Some((app, _)) = borrow.as_mut() else {
1053 return 0;
1054 };
1055 let wid: WindowId = app.get_window_state().get_id();
1056 app.dispatch_window_event(WindowEvent::PointerDown(wid, PointerEvent::mouse(x, y)));
1057 app.step_frame()
1058 .map(|stats: FrameStats| stats.get_frame_hash())
1059 .unwrap_or(0)
1060 })
1061}
1062
1063#[cfg(target_arch = "wasm32")]
1074#[wasm_bindgen]
1075pub fn euv_web_pointer_up(x: f32, y: f32) -> u64 {
1076 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1077 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1078 Ok(guard) => guard,
1079 Err(_) => return 0,
1080 };
1081 let Some((app, _)) = borrow.as_mut() else {
1082 return 0;
1083 };
1084 let wid: WindowId = app.get_window_state().get_id();
1085 app.dispatch_window_event(WindowEvent::PointerUp(wid, PointerEvent::mouse(x, y)));
1086 app.step_frame()
1087 .map(|stats: FrameStats| stats.get_frame_hash())
1088 .unwrap_or(0)
1089 })
1090}
1091
1092#[cfg(target_arch = "wasm32")]
1103#[wasm_bindgen]
1104pub fn euv_web_scroll(dx: f32, dy: f32) -> u64 {
1105 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1106 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1107 Ok(guard) => guard,
1108 Err(_) => return 0,
1109 };
1110 let Some((app, _)) = borrow.as_mut() else {
1111 return 0;
1112 };
1113 let wid: WindowId = app.get_window_state().get_id();
1114 app.dispatch_window_event(WindowEvent::Scroll(wid, Point::new(dx, dy)));
1115 app.step_frame()
1116 .map(|stats: FrameStats| stats.get_frame_hash())
1117 .unwrap_or(0)
1118 })
1119}
1120
1121#[cfg(target_arch = "wasm32")]
1131#[wasm_bindgen]
1132pub fn euv_web_key_down(key: &str) -> u64 {
1133 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1134 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1135 Ok(guard) => guard,
1136 Err(_) => return 0,
1137 };
1138 let Some((app, state)) = borrow.as_mut() else {
1139 return 0;
1140 };
1141 let wid: WindowId = app.get_window_state().get_id();
1142 let code: KeyCode = match key {
1143 WEB_KEY_ENTER => KeyCode::Enter,
1144 WEB_KEY_BACKSPACE => KeyCode::Backspace,
1145 WEB_KEY_DELETE => KeyCode::Delete,
1146 WEB_KEY_ESCAPE => KeyCode::Escape,
1147 WEB_KEY_TAB => KeyCode::Tab,
1148 WEB_KEY_SPACE => KeyCode::Space,
1149 WEB_KEY_ARROW_LEFT => KeyCode::ArrowLeft,
1150 WEB_KEY_ARROW_RIGHT => KeyCode::ArrowRight,
1151 WEB_KEY_ARROW_UP => KeyCode::ArrowUp,
1152 WEB_KEY_ARROW_DOWN => KeyCode::ArrowDown,
1153 other => other
1154 .chars()
1155 .next()
1156 .map(KeyCode::Character)
1157 .unwrap_or(KeyCode::Unknown),
1158 };
1159 let key_ev: WindowEvent =
1160 WindowEvent::KeyDown(wid, KeyEvent::new(code, Modifiers::default(), false));
1161 state.get_game_host().ingest_event(&key_ev);
1162 app.dispatch_window_event(key_ev);
1163 if key.chars().count() == 1 {
1164 app.dispatch_window_event(WindowEvent::TextInput(wid, key.to_string()));
1165 }
1166 app.step_frame()
1167 .map(|stats: FrameStats| stats.get_frame_hash())
1168 .unwrap_or(0)
1169 })
1170}
1171
1172#[cfg(target_arch = "wasm32")]
1182#[wasm_bindgen]
1183pub fn euv_web_tick(dt_secs: f32) -> u64 {
1184 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1185 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1186 Ok(guard) => guard,
1187 Err(_) => return 0,
1188 };
1189 let Some((app, state)) = borrow.as_mut() else {
1190 return 0;
1191 };
1192 let cur_route: String = state.get_route().get_untracked();
1193 let on_game: bool = cur_route == ROUTE_GAME || cur_route == ROUTE_GAME_3D;
1194 if on_game || app.has_active_animations() {
1195 return app
1196 .step_game_frame(dt_secs)
1197 .map(|stats: FrameStats| stats.get_frame_hash())
1198 .unwrap_or(0);
1199 }
1200 app.get_renderer().get_last_stats().get_frame_hash()
1201 })
1202}
1203
1204#[cfg(target_arch = "wasm32")]
1216#[wasm_bindgen]
1217pub fn euv_web_resize(width: u32, height: u32, dpr: f32) -> u64 {
1218 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1219 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1220 Ok(guard) => guard,
1221 Err(_) => return 0,
1222 };
1223 let Some((app, _)) = borrow.as_mut() else {
1224 return 0;
1225 };
1226 let w: u32 = width.max(320);
1227 let h: u32 = height.max(320);
1228 let scale: f32 = if dpr.is_finite() && dpr > 0.0 {
1229 dpr.clamp(0.5, 4.5)
1230 } else {
1231 1.0
1232 };
1233 let phys_w: u32 = ((w as f64) * (scale as f64)).round().max(1.0) as u32;
1234 let phys_h: u32 = ((h as f64) * (scale as f64)).round().max(1.0) as u32;
1235 if let Some(win) = window()
1236 && let Some(doc) = win.document()
1237 && let Ok(Some(el)) = doc.query_selector(WEB_SELECTOR_CANVAS)
1238 && let Ok(canvas) = el.dyn_into::<HtmlCanvasElement>()
1239 {
1240 if canvas.width() != phys_w {
1241 canvas.set_width(phys_w);
1242 }
1243 if canvas.height() != phys_h {
1244 canvas.set_height(phys_h);
1245 }
1246 }
1247 app.set_exact_physical_viewport(phys_w, phys_h, scale);
1248 app.step_frame()
1249 .map(|stats: FrameStats| stats.get_frame_hash())
1250 .unwrap_or(0)
1251 })
1252}
1253
1254#[cfg(all(debug_assertions, target_arch = "wasm32"))]
1264#[wasm_bindgen]
1265pub fn euv_dev_ai_schema(request_json: &str) -> String {
1266 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1267 let mut borrow: RefMut<'_, Option<(App, ShowcaseState)>> = match slot.try_borrow_mut() {
1268 Ok(guard) => guard,
1269 Err(_) => return String::new(),
1270 };
1271 let Some((app, state)) = borrow.as_mut() else {
1272 return String::new();
1273 };
1274 let hooks: AiAppHooks = state.build_ai_hooks();
1275 AiSchemaProtocol::handle_json_with_hooks(app, request_json, Some(&hooks))
1276 })
1277}
1278
1279#[cfg(target_arch = "wasm32")]
1285#[wasm_bindgen]
1286pub fn euv_web_current_route() -> String {
1287 WEB_SHOWCASE_RUNTIME.with(|slot: &RefCell<Option<(App, ShowcaseState)>>| {
1288 match slot.try_borrow() {
1289 Ok(guard) => guard
1290 .as_ref()
1291 .map(|(_, state): &(App, ShowcaseState)| state.get_route().get_untracked())
1292 .unwrap_or_default(),
1293 Err(_) => String::new(),
1294 }
1295 })
1296}