ribir 0.4.0-alpha.60

A non-intrusive declarative GUI framework, to build modern native/wasm cross-platform applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use std::{future::Future, sync::Arc};

use ribir_core::{
  prelude::{font_db::FontDBGlyphProvider, *},
  window::{
    BoxShellWindow, RedrawDemand, Shell, ShellWindow, UiEvent, WindowAttributes, WindowId,
    WindowLevel,
  },
};
use winit::dpi::{LogicalPosition, LogicalSize};

#[cfg(target_arch = "wasm32")]
pub const RIBIR_CANVAS: &str = "ribir_canvas";
#[cfg(target_arch = "wasm32")]
pub const RIBIR_CONTAINER: &str = "ribir_container";

#[cfg(feature = "debug")]
use ribir_core::debug_tool::{FRAME_TX, FramePacket};

use crate::{
  app::{App, CmdSender},
  backends::*,
};

pub enum ShellCmd {
  Exit,
  RequestDraw {
    id: WindowId,
    demand: RedrawDemand,
  },
  Draw {
    id: WindowId,
    wnd_size: Size,
    viewport: Rect,
    surface_color: Color,
    commands: Vec<PaintCommand>,
    glyph_provider: FontDBGlyphProvider,
  },
  Close {
    id: WindowId,
  },
  RunAsync {
    fut: BoxFuture<'static, ()>,
  },
  /// Create a new window. The winit window is created synchronously in the
  /// event loop (where active_event_loop is available), then the backend is
  /// initialized asynchronously.
  NewWindow {
    attrs: Box<WindowAttributes>,
    sender: tokio::sync::oneshot::Sender<BoxShellWindow>,
  },
}

impl ShellCmd {
  pub fn wnd_id(&self) -> Option<WindowId> {
    match self {
      ShellCmd::RequestDraw { id, .. } | ShellCmd::Draw { id, .. } | ShellCmd::Close { id } => {
        Some(*id)
      }
      ShellCmd::RunAsync { .. } | ShellCmd::Exit | ShellCmd::NewWindow { .. } => None,
    }
  }
}

pub(crate) struct RibirShell {
  pub(crate) cmd_sender: CmdSender,
}

impl Shell for RibirShell {
  fn new_shell_window(
    &self, attrs: ribir_core::window::WindowAttributes,
  ) -> BoxFuture<'static, BoxShellWindow> {
    let (sender, receiver) = tokio::sync::oneshot::channel::<BoxShellWindow>();

    // Send NewWindow command to create winit window synchronously in the event
    // loop, where active_event_loop is available. This fixes the WASM bug where
    // spawn_local schedules futures outside the event loop callback.
    self
      .cmd_sender
      .send(ShellCmd::NewWindow { attrs: Box::new(attrs), sender });

    Box::pin(async move { receiver.await.unwrap() })
  }

  fn exit(&self) { self.cmd_sender.send(ShellCmd::Exit); }

  fn run_in_shell(&self, fut: BoxFuture<'static, ()>) {
    self.cmd_sender.send(ShellCmd::RunAsync { fut });
  }
}

pub(crate) struct ShellWndHandle {
  pub(crate) sender: CmdSender,
  pub(crate) winit_wnd: Arc<winit::window::Window>,
  pub(crate) cursor: CursorIcon,
}

pub trait WinitBackend<'a>: Sized {
  fn new(window: &'a winit::window::Window) -> impl Future<Output = Self>;

  fn on_resize(&mut self, size: DeviceSize);

  fn begin_frame(&mut self, surface_color: Color);

  fn draw_commands(
    &mut self, viewport: DeviceRect, global_matrix: &Transform, commands: &[PaintCommand],
    glyph_provider: &dyn GlyphProvider,
  );

  fn end_frame(&mut self);

  /// Capture the current frame from the GPU surface.
  #[cfg(feature = "debug")]
  fn capture_screenshot(&mut self) -> Option<BoxFuture<'static, Option<PixelImage>>>;
}

pub(crate) struct WinitShellWnd {
  pub(crate) winit_wnd: Arc<winit::window::Window>,
  backend: Backend<'static>,
}

fn window_size(winit_wnd: &winit::window::Window) -> Size {
  let size = winit_wnd
    .inner_size()
    .to_logical(winit_wnd.scale_factor());
  Size::new(size.width, size.height)
}

impl WinitShellWnd {
  pub(crate) fn id(&self) -> WindowId { new_id(self.winit_wnd.id()) }

  pub(crate) fn deal_cmd(&mut self, cmd: ShellCmd) {
    match cmd {
      ShellCmd::RequestDraw { demand, .. } => {
        if demand.requires_forced_draw() {
          App::send_event(UiEvent::RedrawRequest { wnd_id: self.id(), demand });
        } else {
          self.winit_wnd.request_redraw();
        }
      }
      ShellCmd::Draw { viewport, surface_color, commands, glyph_provider, wnd_size, .. } => {
        if wnd_size == window_size(&self.winit_wnd) {
          self.backend.begin_frame(surface_color);
          let scale_factor = self.winit_wnd.scale_factor() as f32;

          let viewport: DeviceRect = viewport
            .scale(scale_factor, scale_factor)
            .round_out()
            .to_i32()
            .cast_unit();

          self.backend.draw_commands(
            viewport,
            &Transform::scale(scale_factor, scale_factor),
            &commands,
            &glyph_provider,
          );

          #[cfg(feature = "debug")]
          self.capture_debug_frame();

          self.backend.end_frame();
        } else {
          self.winit_wnd.request_redraw();
        }
      }
      ShellCmd::Close { id } => {
        App::remove_shell_window(id);
      }
      _ => (),
    }
  }

  pub(crate) fn on_resize(&mut self, size: DeviceSize) { self.backend.on_resize(size); }

  /// Capture and send frame to debug server if enabled.
  #[cfg(feature = "debug")]
  fn capture_debug_frame(&mut self) {
    if FRAME_TX.get().is_some()
      && let Some(fut) = self.backend.capture_screenshot()
    {
      let wnd_id = self.id();
      App::spawn_local(async move {
        if let Some(img) = fut.await
          && let Some(frame_tx) = FRAME_TX.get()
        {
          let ts_unix_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;
          static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
          let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);

          let _ = frame_tx.send(FramePacket { wnd_id, ts_unix_ms, seq, image: Arc::new(img) });
        }
      });
    }
  }
}

impl ShellWindow for ShellWndHandle {
  fn id(&self) -> WindowId { new_id(self.winit_wnd.id()) }

  fn inner_size(&self) -> Size { window_size(&self.winit_wnd) }

  fn as_any(&self) -> &dyn Any { self }

  fn as_any_mut(&mut self) -> &mut dyn Any { self }

  fn set_cursor(&mut self, cursor: CursorIcon) {
    self.cursor = cursor;
    self.winit_wnd.set_cursor(cursor);
  }

  fn cursor(&self) -> CursorIcon { self.cursor }

  fn focus_window(&mut self) {
    self.winit_wnd.focus_window();
    self.winit_wnd.request_redraw();
  }

  fn set_minimized(&mut self, minimized: bool) { self.winit_wnd.set_minimized(minimized); }

  fn is_minimized(&self) -> bool { self.winit_wnd.is_minimized().unwrap_or_default() }

  fn set_min_size(&mut self, size: Size) {
    self
      .winit_wnd
      .set_min_inner_size(Some(LogicalSize::new(size.width, size.height)));
  }

  fn set_icon(&mut self, icon: &PixelImage) {
    self
      .winit_wnd
      .set_window_icon(Some(img_to_winit_icon(icon)));
  }

  fn close(&self) {
    self
      .sender
      .send(ShellCmd::Close { id: self.id() });
  }

  fn set_title(&mut self, str: &str) { self.winit_wnd.set_title(str); }

  fn set_resizable(&mut self, resizable: bool) { self.winit_wnd.set_resizable(resizable); }

  fn is_resizable(&self) -> bool { self.winit_wnd.is_resizable() }

  fn set_visible(&mut self, visible: bool) { self.winit_wnd.set_visible(visible); }

  fn is_visible(&self) -> Option<bool> { self.winit_wnd.is_visible() }

  fn set_ime_allowed(&mut self, allowed: bool) { self.winit_wnd.set_ime_allowed(allowed); }

  fn set_window_level(&mut self, level: WindowLevel) { self.winit_wnd.set_window_level(level); }

  fn set_ime_cursor_area(&mut self, rect: &Rect) {
    let position: LogicalPosition<f32> = LogicalPosition::new(rect.origin.x, rect.origin.y);
    let size: LogicalSize<f32> = LogicalSize::new(rect.size.width, rect.size.height);
    self.winit_wnd.set_ime_cursor_area(position, size);
  }

  fn request_resize(&mut self, size: Size) {
    let _ = self
      .winit_wnd
      .request_inner_size(LogicalSize::new(size.width, size.height));
  }

  fn draw_commands(
    &mut self, wnd_size: Size, viewport: Rect, surface_color: Color, commands: &[PaintCommand],
  ) {
    let font_db = AppCtx::font_db();
    let glyph_provider = font_db.borrow_mut().glyph_provider();
    self.sender.send(ShellCmd::Draw {
      id: self.id(),
      wnd_size,
      viewport,
      surface_color,
      commands: commands.to_vec(),
      glyph_provider,
    });
  }

  fn request_draw(&self, demand: RedrawDemand) {
    self
      .sender
      .send(ShellCmd::RequestDraw { id: self.id(), demand });
  }

  fn position(&self) -> Point {
    let scale_factor = self.winit_wnd.scale_factor() as f32;
    self
      .winit_wnd
      .outer_position()
      .map(|pos| Point::new(pos.x as f32 / scale_factor, pos.y as f32 / scale_factor))
      .unwrap_or_default()
  }

  fn set_position(&mut self, point: Point) {
    let pos = self.position();
    if pos != point {
      self
        .winit_wnd
        .set_outer_position(LogicalPosition::new(point.x, point.y));
    }
  }
}

pub(crate) fn new_id(id: winit::window::WindowId) -> WindowId {
  let id: u64 = id.into();
  id.into()
}

impl WinitShellWnd {
  /// Synchronously create a winit window. This must be called from within the
  /// event loop callback where `active_event_loop` is available.
  #[cfg(target_arch = "wasm32")]
  pub(crate) fn create_winit_window(mut attrs: WindowAttributes) -> Arc<winit::window::Window> {
    use web_sys::wasm_bindgen::JsCast;
    use winit::platform::web::WindowAttributesExtWebSys;

    let document = web_sys::window().unwrap().document().unwrap();
    let canvas = document
      .create_element("canvas")
      .unwrap()
      .dyn_into::<web_sys::HtmlCanvasElement>()
      .unwrap();
    canvas.set_class_name(RIBIR_CANVAS);
    let style = canvas.style();
    let _ = style.set_property("width", "100%");
    let _ = style.set_property("height", "100%");
    let elems = document.get_elements_by_class_name(RIBIR_CONTAINER);

    if let Some(elem) = elems.item(0) {
      elem.set_class_name(&elem.class_name().replace(RIBIR_CONTAINER, ""));
      elem.append_child(&canvas).unwrap();
    } else if let Some(body) = document.body() {
      body.append_child(&canvas).unwrap();
    } else {
      document.append_child(&canvas).unwrap();
    }

    attrs.0 = attrs.0.with_canvas(Some(canvas));

    Arc::new(
      App::active_event_loop()
        .create_window(attrs.0)
        .unwrap(),
    )
  }

  /// Synchronously create a winit window. This must be called from within the
  /// event loop callback where `active_event_loop` is available.
  #[cfg(not(target_arch = "wasm32"))]
  pub(crate) fn create_winit_window(attrs: WindowAttributes) -> Arc<winit::window::Window> {
    Arc::new(
      App::active_event_loop()
        .create_window(attrs.0)
        .unwrap(),
    )
  }

  /// Asynchronously initialize the backend for an existing winit window.
  /// This doesn't require `active_event_loop` to be available.
  pub(crate) async fn from_winit_window(winit_wnd: Arc<winit::window::Window>) -> Self {
    let ptr = winit_wnd.as_ref() as *const winit::window::Window;
    // Safety: a reference to winit_wnd is valid as long as the WinitShellWnd is
    // alive.
    let backend = Backend::new(unsafe { &*ptr }).await;
    WinitShellWnd { backend, winit_wnd }
  }

  #[cfg(target_arch = "wasm32")]
  pub(crate) async fn new(mut attrs: WindowAttributes) -> Self {
    use web_sys::wasm_bindgen::JsCast;
    use winit::platform::web::WindowAttributesExtWebSys;

    let document = web_sys::window().unwrap().document().unwrap();
    let canvas = document
      .create_element("canvas")
      .unwrap()
      .dyn_into::<web_sys::HtmlCanvasElement>()
      .unwrap();
    canvas.set_class_name(RIBIR_CANVAS);
    let style = canvas.style();
    let _ = style.set_property("width", "100%");
    let _ = style.set_property("height", "100%");
    let elems = document.get_elements_by_class_name(RIBIR_CONTAINER);

    if let Some(elem) = elems.item(0) {
      elem.set_class_name(&elem.class_name().replace(RIBIR_CONTAINER, ""));
      elem.append_child(&canvas).unwrap();
    } else if let Some(body) = document.body() {
      body.append_child(&canvas).unwrap();
    } else {
      document.append_child(&canvas).unwrap();
    }

    attrs.0 = attrs.0.with_canvas(Some(canvas));

    Self::inner_new(attrs).await
  }

  #[cfg(not(target_arch = "wasm32"))]
  pub(crate) async fn new(attrs: WindowAttributes) -> Self { Self::inner_new(attrs).await }

  async fn inner_new(attrs: WindowAttributes) -> Self {
    let winit_wnd = Arc::new(
      App::active_event_loop()
        .create_window(attrs.0)
        .unwrap(),
    );
    let ptr = winit_wnd.as_ref() as *const winit::window::Window;
    // Safety: a reference to winit_wnd is valid as long as the WinitShellWnd is
    // alive.
    let backend = Backend::new(unsafe { &*ptr }).await;
    WinitShellWnd { backend, winit_wnd }
  }
}

fn img_to_winit_icon(icon: &PixelImage) -> winit::window::Icon {
  assert!(icon.color_format() == ColorFormat::Rgba8);
  winit::window::Icon::from_rgba(icon.pixel_bytes().to_vec(), icon.width(), icon.height()).unwrap()
}