changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / ui/src/lib.rs

changeset 16: af615d1895cb
author: ellis <ellis@rwest.io>
date: Fri, 26 May 2023 21:59:40 -0400
permissions: -rw-r--r--
description: refactoring, ui stuff (goin with slint for DSL power)
1 #[cfg(target_arch="wasm32")]
2 use wasm_bindgen::prelude::*;
3 use winit::{
4  event::*,
5  event_loop::{ControlFlow, EventLoop},
6  window::WindowBuilder,
7 };
8 
9 #[cfg_attr(target_arch="wasm32", wasm_bindgen(start))]
10 pub fn run() {
11  cfg_if::cfg_if! {
12  if #[cfg(target_arch = "wasm32")] {
13  std::panic::set_hook(Box::new(console_error_panic_hook::hook));
14  console_log::init_with_level(log::Level::Warn).expect("Couldn't initialize logger");
15  } else {
16  env_logger::init();
17  }
18  }
19  let event_loop = EventLoop::new();
20  let window = WindowBuilder::new().build(&event_loop).unwrap();
21 
22  #[cfg(target_arch = "wasm32")]
23 {
24  // Winit prevents sizing with CSS, so we have to set
25  // the size manually when on web.
26  use winit::dpi::PhysicalSize;
27  window.set_inner_size(PhysicalSize::new(450, 400));
28 
29  use winit::platform::web::WindowExtWebSys;
30  web_sys::window()
31  .and_then(|win| win.document())
32  .and_then(|doc| {
33  let dst = doc.get_element_by_id("wasm-example")?;
34  let canvas = web_sys::Element::from(window.canvas());
35  dst.append_child(&canvas).ok()?;
36  Some(())
37  })
38  .expect("Couldn't append canvas to document body.");
39 }
40  event_loop.run(move |event, _, control_flow| match event {
41  Event::WindowEvent {
42  ref event,
43  window_id,
44  } if window_id == window.id() => match event {
45  WindowEvent::CloseRequested
46  | WindowEvent::KeyboardInput {
47  input:
48  KeyboardInput {
49  state: ElementState::Pressed,
50  virtual_keycode: Some(VirtualKeyCode::Escape),
51  ..
52  },
53  ..
54  } => *control_flow = ControlFlow::Exit,
55  _ => {}
56  },
57  _ => {}
58  });
59 }