///| Chandra GUI loader component backed by wasi-gfx (surface + frame-buffer). priv struct GfxGuiState { mut surface : @surface.Surface? mut context : @sfb.Context? mut width : Int mut height : Int mut launcher_open : Bool mut pointer_x : Double mut pointer_y : Double mut last_key : String mut frame_count : Int } let state : GfxGuiState = { surface: None, context: None, width: 640, height: 360, launcher_open: false, pointer_x: 0.0, pointer_y: 0.0, last_key: "", frame_count: 0, } ///| pub fn init_gui(width : UInt, height : UInt) -> Unit { let w = if width < 1U { 1U } else { width } let h = if height < 1U { 1U } else { height } state.width = w.reinterpret_as_int() state.height = h.reinterpret_as_int() let surf = @surface.Surface::surface({ height: Some(h), width: Some(w), }) let ctx = @sfb.Context::context(surf) state.surface = Some(surf) state.context = Some(ctx) render_frame() } ///| pub fn render_frame() -> Unit { match state.context { None => () Some(ctx) => { state.frame_count = state.frame_count + 1 let w = state.width let h = state.height let theme = @scene.ThemeTokens::moon_night() let sg = @scene.SceneGraph::new(w, h) let titles : Array[String] = ["wasi-gfx Desktop"] let (bg_ops, panel_ops) = @scene.build_shell_ops( w, h, theme, "wasi-gfx", titles, ) sg.background_ops = bg_ops sg.panel_ops = panel_ops // Add a sample decorated window so the GUI surface is rich and verifiable. let win_w = if w > 320 { w / 2 } else { w - 32 } let win_h = if h > 220 { h / 2 } else { h - 64 } let style = @scene.WindowStyle::Flat let (insets, deco_ops) = @scene.build_window_decoration( style, { title: "chandra (wasi-gfx)", app_id: "org.chandra.gfx", focused: true, maximized: false, hover: 0, pressed: 0, }, win_w + 2 * style.frame_side(), win_h + style.frame_top() + style.frame_side(), theme, fn(s, size, bold) { @renderer.text_width(s, size, bold, false) }, ) sg.windows.push({ id: 1UL, title: "chandra (wasi-gfx)", app_id: "org.chandra.gfx", focused: true, fullscreen: false, geometry: @scene.Rect::new(24, 48, win_w, win_h), deco_insets: insets, deco_ops, surface: None, }) if state.launcher_open { sg.widgets.push({ id: "launcher", rect: @scene.Rect::new(8, h - theme.panel_height - 168, 220, 160), ops: [ @scene.DrawOp::FillRect( @scene.Rect::new(0, 0, 220, 160), theme.palette.surface_popup, theme.radius, ), @scene.DrawOp::StrokeRect( @scene.Rect::new(0, 0, 220, 160), theme.palette.border, theme.radius, 1, ), @scene.DrawOp::Text( 14, 24, "Applications (wasi-gfx)", theme.font_size, theme.palette.foreground, @scene.TextAlign::Left, true, ), ], popup: None, }) } let pixmap = @renderer.render_scene(sg) let buf = ctx.get_current_buffer() buf.set_with_copy(pixmap.data) ctx.present() } } } ///| pub fn on_resize(width : UInt, height : UInt) -> Unit { let w = if width < 1U { 1U } else { width } let h = if height < 1U { 1U } else { height } state.width = w.reinterpret_as_int() state.height = h.reinterpret_as_int() match state.surface { Some(surf) => surf.request_set_size(Some(h), Some(w)) None => () } render_frame() } ///| pub fn on_pointer(kind : Byte, x : Double, y : Double) -> Unit { state.pointer_x = x state.pointer_y = y // kind == 1: pointer down if kind == b'\x01' { let theme = @scene.ThemeTokens::moon_night() if y.to_int() >= state.height - theme.panel_height && x.to_int() <= 120 { state.launcher_open = !state.launcher_open } render_frame() } } ///| pub fn on_key(pressed : Bool, text : String) -> Unit { if pressed { state.last_key = text if text == "Escape" { state.launcher_open = false } else if text == "Super" || text == "Meta" { state.launcher_open = !state.launcher_open } render_frame() } }