Skip to main content

webgpu/
lib.rs

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