git.lucas.co / cce-ui
GPU-accelerated UI toolkit (Vulkan)
git clone https://git.lucas.co/cce-ui.git

examples/demo_web.rs (1.9K)

 1 //! The reference app, `src/main.rs`, in a browser: the same `DemoApp` the
 2 //! Wayland shell runs, run on a canvas by `cce_ui::web::run`. Built for
 3 //! wasm32 as a cdylib and bound with wasm-bindgen; a page calls
 4 //! `start(canvas, fonts, families, fill)` — `fonts` each a font file's bytes,
 5 //! `families` the generic serif / sans-serif / monospace families as
 6 //! "serif,sans,mono" (empty for the defaults; any part may be empty), `fill`
 7 //! true to let the page size the canvas (`Sizing::Page`) rather than the app
 8 //! (`Sizing::App`) — and `capture()` for the next frame's pixels.
 9 //! `scripts/web-probe/demo` serves it and drives it.
10 //!
11 //! On a native target this example is empty.
12 
13 #[cfg(target_arch = "wasm32")]
14 #[allow(dead_code)]
15 #[path = "../src/main.rs"]
16 mod demo;
17 
18 #[cfg(target_arch = "wasm32")]
19 mod web {
20     use cce_ui::web::{Fonts, Sizing};
21     use wasm_bindgen::prelude::*;
22 
23     #[wasm_bindgen]
24     pub async fn start(canvas: web_sys::HtmlCanvasElement, fonts: js_sys::Array, families: String, fill: bool) -> Result<(), JsValue> {
25         std::panic::set_hook(Box::new(|info| web_sys::console::error_1(&info.to_string().into())));
26         let mut fonts = Fonts::new(fonts.iter().map(|f| js_sys::Uint8Array::new(&f).to_vec()).collect());
27         let mut names = families.split(',').map(|f| Some(f.trim()).filter(|f| !f.is_empty()).map(String::from));
28         fonts.serif = names.next().flatten();
29         fonts.sans_serif = names.next().flatten();
30         fonts.monospace = names.next().flatten();
31         let sizing = if fill { Sizing::Page } else { Sizing::App };
32         cce_ui::web::run::<super::demo::DemoApp>(canvas, fonts, sizing).await
33     }
34 
35     /// The next frame, read back from the GPU as RGBA8 (`cce_ui::web::capture`).
36     #[wasm_bindgen]
37     pub async fn capture() -> Result<js_sys::Uint8Array, JsValue> {
38         let frame = cce_ui::web::capture().await?;
39         Ok(js_sys::Uint8Array::from(&frame.rgba[..]))
40     }
41 }