webgpu/
lib.rs

1use floem::text::FONT_SYSTEM;
2use floem::window::WindowConfig;
3use floem::Application;
4use floem::{
5    reactive::{create_signal, SignalGet, SignalUpdate},
6    views::{label, ButtonClass, Decorators},
7    IntoView,
8};
9
10#[cfg(target_arch = "wasm32")]
11use wasm_bindgen::prelude::*;
12
13const FIRA_MONO: &[u8] = include_bytes!("../fonts/FiraMono-Medium.ttf");
14const FIRA_SANS: &[u8] = include_bytes!("../fonts/FiraSans-Medium.ttf");
15const DEJAVU_SERIF: &[u8] = include_bytes!("../fonts/DejaVuSerif.ttf");
16
17pub fn app_view() -> impl IntoView {
18    // Create a reactive signal with a counter value, defaulting to 0
19    let (counter, set_counter) = create_signal(0);
20
21    // Create a vertical layout
22    (
23        // The counter value updates automatically, thanks to reactivity
24        label(move || format!("Value: {}", counter.get())),
25        // Create a horizontal layout
26        (
27            "Increment".class(ButtonClass).on_click_stop(move |_| {
28                set_counter.update(|value| *value += 1);
29            }),
30            "Decrement".class(ButtonClass).on_click_stop(move |_| {
31                set_counter.update(|value| *value -= 1);
32            }),
33        ),
34    )
35        .style(|s| s.flex_col())
36}
37
38#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
39pub fn run() {
40    #[cfg(target_family = "wasm")]
41    console_error_panic_hook::set_once();
42
43    {
44        let mut font_system = FONT_SYSTEM.lock();
45        let font_db = font_system.db_mut();
46        font_db.load_font_data(Vec::from(FIRA_MONO));
47        font_db.load_font_data(Vec::from(FIRA_SANS));
48        font_db.load_font_data(Vec::from(DEJAVU_SERIF));
49    }
50
51    let window_config = WindowConfig::default().with_web_config(|w| w.canvas_id("the-canvas"));
52
53    Application::new()
54        .window(move |_| app_view(), Some(window_config))
55        .run()
56}