ribir_core 0.4.0-alpha.55

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use std::sync::atomic::{AtomicU64, Ordering};

#[cfg(test)]
#[cfg(target_arch = "wasm32")]
pub use wasm_bindgen_test::wasm_bindgen_test;

#[cfg(test)]
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

use crate::{
  prelude::*,
  window::{BoxShellWindow, Shell, ShellWindow, WindowFlags, WindowId},
};

pub struct Frame {
  pub commands: Vec<PaintCommand>,
  pub viewport: Rect,
  pub surface: Color,
}

pub fn split_value<T: 'static>(v: T) -> (Watcher<Reader<T>>, Stateful<T>) {
  let src = Stateful::new(v);
  (src.clone_watcher(), src.clone_writer())
}

/// The Window assists in writing unit tests; animations are disabled by
/// default.
#[derive(Clone)]
pub struct TestWindow(pub Rc<Window>);

#[macro_export]
macro_rules! reset_test_env {
  () => {
    let _guard = $crate::prelude::AppCtx::new_lock_scope();
  };
}

pub struct TestShell {}

impl Shell for TestShell {
  fn exit(&self) {}

  fn new_shell_window(&self, attr: window::WindowAttributes) -> BoxFuture<'static, BoxShellWindow> {
    Box::pin(async move {
      Box::new(TestShellWindow::new(attr.0.inner_size.map_or_else(
        || Size::new(1024., 1024.),
        |s| {
          let s = s.to_logical(1.);
          Size::new(s.width, s.height)
        },
      ))) as BoxShellWindow
    })
  }

  fn run_in_shell(&self, f: BoxFuture<'static, ()>) { AppCtx::spawn_local(f); }
}

impl TestWindow {
  /// Create a 1024x1024 window for test
  pub fn from_widget<K: ?Sized>(root: impl RInto<GenWidget, K>) -> Self {
    Self::new(root, Size::new(1024., 1024.), WindowFlags::empty())
  }

  pub fn new_with_size<K: ?Sized>(root: impl RInto<GenWidget, K>, size: Size) -> Self {
    Self::new(root, size, WindowFlags::empty())
  }

  #[track_caller]
  pub fn assert_root_size(&self, size: Size) {
    let info = self.layout_info_by_path(&[0]).unwrap();
    assert_eq!(info.size.unwrap(), size);
  }

  pub fn new<K: ?Sized>(root: impl RInto<GenWidget, K>, size: Size, flags: WindowFlags) -> Self {
    let wnd = Window::new(Box::new(TestShellWindow::new(size)), flags);

    AppCtx::insert_window(wnd.clone());
    wnd.init(root.r_into());

    wnd.run_frame_tasks();
    Self(wnd)
  }

  /// Ues a index path to access widget tree and return the layout info,
  /// [0, 1] means use the second child of the root.
  /// [0, 1, 2] the first node at the root level (must be 0), then down to its
  /// second child, then down to third child.
  pub fn layout_info_by_path(&self, path: &[usize]) -> Option<LayoutInfo> {
    let tree = self.0.tree();
    let mut node = tree.root();
    for (level, idx) in path[..].iter().enumerate() {
      node = node.children(tree).nth(*idx).unwrap_or_else(|| {
        panic!("node no exist: {:?}", &path[0..level]);
      });
    }
    tree.store.layout_info(node).cloned()
  }

  pub fn take_last_frame(&mut self) -> Option<Frame> {
    self
      .shell_wnd()
      .borrow_mut()
      .as_any_mut()
      .downcast_mut::<TestShellWindow>()
      .unwrap()
      .last_frame
      .take()
  }

  pub fn content_count(&self) -> usize {
    let tree = self.0.tree();
    let root = tree.root();
    let content = root.first_child(tree).unwrap();
    tree.count(content)
  }

  #[track_caller]
  pub fn draw_frame(&self) {
    // Test window not have a eventloop, manually wake-up every frame.
    #[cfg(not(target_arch = "wasm32"))]
    AppCtx::new_test_frame(self);
  }

  pub fn fmt_tree(&self) -> String { self.tree().display_tree(self.tree().root()) }

  pub fn request_resize(&self, size: Size) {
    let root = self.0.tree().root();
    self
      .0
      .tree()
      .dirty_marker()
      .mark(root, DirtyPhase::Layout);
    self.0.request_resize(size);
  }
}

impl std::ops::Deref for TestWindow {
  type Target = Window;

  fn deref(&self) -> &Self::Target { &self.0 }
}

pub struct TestShellWindow {
  pub cursor: CursorIcon,
  pub id: WindowId,
  pub surface_color: Color,
  pub last_frame: Option<Frame>,
  pub size: Size,
}

impl ShellWindow for TestShellWindow {
  fn inner_size(&self) -> Size { self.size }

  fn request_resize(&mut self, size: Size) { self.on_resize(size); }

  fn set_min_size(&mut self, _: Size) {}

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

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

  fn set_title(&mut self, _: &str) {}

  fn set_icon(&mut self, _: &PixelImage) {}

  fn set_ime_cursor_area(&mut self, _: &Rect) {}

  fn set_visible(&mut self, _: bool) {}

  fn is_visible(&self) -> Option<bool> { Some(true) }

  fn set_resizable(&mut self, _: bool) {}

  fn is_resizable(&self) -> bool { true }

  fn focus_window(&mut self) {}

  // fn set_decorations(&mut self, _: bool) {}

  fn is_minimized(&self) -> bool { false }

  fn set_minimized(&mut self, _: bool) {}

  fn set_window_level(&mut self, _: WindowLevel) {}

  fn set_ime_allowed(&mut self, _: bool) {}

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

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

  fn draw_commands(
    &mut self, _wnd_size: Size, viewport: Rect, surface_color: Color, commands: &[PaintCommand],
  ) {
    self.last_frame =
      Some(Frame { commands: commands.to_owned(), viewport, surface: surface_color });
  }

  fn request_draw(&self) {}

  fn id(&self) -> WindowId { self.id }

  fn position(&self) -> Point { Point::new(0., 0.) }

  fn set_position(&mut self, _: Point) {}

  fn close(&self) {}
}

impl TestShellWindow {
  fn new(size: Size) -> Self {
    static ID: AtomicU64 = AtomicU64::new(0);
    TestShellWindow {
      cursor: CursorIcon::Default,
      id: ID.fetch_add(1, Ordering::Relaxed).into(),
      last_frame: None,
      surface_color: Color::WHITE,
      size,
    }
  }

  fn on_resize(&mut self, size: Size) {
    self.size = size;
    self.last_frame = None;
  }
}

#[derive(Declare, MultiChild)]
pub struct MockStack {}

impl Render for MockStack {
  fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
    let mut size = ZERO_SIZE;
    let (ctx, children) = ctx.split_children();
    for c in children {
      let child_size = ctx.perform_child_layout(c, clamp);
      size = size.max(child_size);
    }

    size
  }
  fn paint(&self, _: &mut PaintingCtx) {}
}

#[derive(Declare, MultiChild, Default)]
pub struct MockMulti;

#[derive(Declare, Clone, SingleChild)]
pub struct MockBox {
  pub size: Size,
}

impl Render for MockMulti {
  fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
    let mut size = ZERO_SIZE;
    let (ctx, children) = ctx.split_children();
    for c in children {
      let child_size = ctx.perform_child_layout(c, clamp);
      ctx.update_position(c, Point::new(size.width, 0.));
      size.width += child_size.width;
      size.height = size.height.max(child_size.height);
    }

    size
  }

  fn paint(&self, _: &mut PaintingCtx) {}
}

impl Render for MockBox {
  fn perform_layout(&self, mut clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
    let size = clamp.clamp(self.size);
    clamp.max = clamp.max.min(size);
    ctx.perform_single_child_layout(clamp);

    size
  }
  #[inline]
  fn size_affected_by_child(&self) -> bool { false }

  #[inline]
  fn paint(&self, _: &mut PaintingCtx) {}
}

/// The layout case describes the expected position and size of a widget node
/// based on its index path.
///
/// In terms of the index path:
///   - [0, 1] indicates the second child of the root, where the root level is
///     denoted as 0.
///   - [0, 1, 2] signifies the first node at the root level (which is 0),
///     followed by its second child, and then the third child below that.
pub struct LayoutCase {
  path: &'static [usize],
  x: Option<f32>,
  y: Option<f32>,
  width: Option<f32>,
  height: Option<f32>,
  visual_rect: Option<Rect>,
}

impl LayoutCase {
  #[track_caller]
  pub fn expect_x(wnd: &TestWindow, path: &'static [usize], x: f32) {
    LayoutCase::new(path).with_x(x).check(wnd);
  }

  #[track_caller]
  pub fn expect_y(wnd: &TestWindow, path: &'static [usize], y: f32) {
    LayoutCase::new(path).with_y(y).check(wnd);
  }

  #[track_caller]
  pub fn expect_size(wnd: &TestWindow, path: &'static [usize], size: Size) {
    LayoutCase::new(path).with_size(size).check(wnd);
  }

  #[track_caller]
  pub fn expect_pos(wnd: &TestWindow, path: &'static [usize], pos: Point) {
    LayoutCase::new(path).with_pos(pos).check(wnd);
  }

  #[track_caller]
  pub fn expect_rect(wnd: &TestWindow, path: &'static [usize], rect: Rect) {
    LayoutCase::new(path)
      .with_pos(rect.origin)
      .with_size(rect.size)
      .check(wnd);
  }

  pub fn new(path: &'static [usize]) -> Self { LayoutCase { path, ..<_>::default() } }

  pub fn with_pos(mut self, pos: Point) -> Self {
    self.x = Some(pos.x);
    self.y = Some(pos.y);
    self
  }
  pub fn with_x(mut self, x: f32) -> Self {
    self.x = Some(x);
    self
  }

  pub fn with_y(mut self, y: f32) -> Self {
    self.y = Some(y);
    self
  }

  pub fn with_width(mut self, width: f32) -> Self {
    self.width = Some(width);
    self
  }

  pub fn with_height(mut self, height: f32) -> Self {
    self.height = Some(height);
    self
  }

  pub fn with_size(mut self, size: Size) -> Self {
    self.width = Some(size.width);
    self.height = Some(size.height);
    self
  }

  pub fn with_visual_rect(mut self, rect: Rect) -> Self {
    self.visual_rect = Some(rect);
    self
  }

  pub fn with_rect(self, rect: Rect) -> Self { self.with_pos(rect.origin).with_size(rect.size) }

  #[track_caller]
  pub fn check(&self, wnd: &TestWindow) {
    let Self { path, x, y, width, height, visual_rect } = self;

    let info = wnd.layout_info_by_path(path).unwrap();
    if let Some(x) = x {
      assert_eq!(info.pos.x, *x, "unexpected x");
    }
    if let Some(y) = y {
      assert_eq!(info.pos.y, *y, "unexpected y");
    }
    if let Some(w) = width {
      assert_eq!(info.size.unwrap().width, *w, "unexpected width");
    }
    if let Some(h) = height {
      assert_eq!(info.size.unwrap().height, *h, "unexpected height");
    }
    if let Some(rect) = visual_rect {
      assert_eq!(info.visual_box.bounds_rect(), Some(*rect), "unexpected visual rect");
    }
  }
}

pub struct WidgetTester {
  pub widget: GenWidget,
  pub wnd_size: Option<Size>,
  pub flags: Option<WindowFlags>,
  pub env_init: Option<Box<dyn FnOnce()>>,
  pub on_initd: Option<InitdFn>,
  pub comparison: Option<f64>,
}

type InitdFn = Box<dyn FnOnce(&mut TestWindow)>;

impl WidgetTester {
  pub fn new<K: ?Sized>(widget: impl RInto<GenWidget, K>) -> Self {
    Self {
      wnd_size: None,
      widget: widget.r_into(),
      on_initd: None,
      env_init: None,
      comparison: None,
      flags: None,
    }
  }

  pub fn new_with_data<K, D: 'static, W: IntoWidget<'static, K>>(
    data: D, widget_builder: impl Fn(&'static D) -> W + 'static,
  ) -> Self {
    let w = move || widget_builder(unsafe { &*(&data as *const _) }).into_widget();
    Self::new(w)
  }

  pub fn with_env_init(mut self, env_init: impl Fn() + 'static) -> Self {
    self.env_init = Some(Box::new(env_init));
    self
  }

  /// This callback runs after creating the window and drawing the first
  /// frame.
  pub fn on_initd(mut self, on_initd: impl Fn(&mut TestWindow) + 'static) -> Self {
    self.on_initd = Some(Box::new(on_initd));
    self
  }

  pub fn with_wnd_size(mut self, size: Size) -> Self {
    self.wnd_size = Some(size);
    self
  }

  pub fn with_flags(mut self, flags: WindowFlags) -> Self {
    self.flags = Some(self.flags.unwrap_or(WindowFlags::empty()) | flags);
    self
  }

  pub fn with_comparison(mut self, comparison: f64) -> Self {
    self.comparison = Some(comparison);
    self
  }

  pub fn create_wnd(&mut self) -> TestWindow {
    if let Some(env_init) = self.env_init.take() {
      env_init();
    }

    let wnd_size = self.wnd_size.unwrap_or(Size::new(1024., 1024.));
    let mut wnd =
      TestWindow::new(self.widget.clone(), wnd_size, self.flags.unwrap_or(WindowFlags::empty()));

    if let Some(initd) = self.on_initd.take() {
      initd(&mut wnd);
    }

    wnd.draw_frame();
    wnd
  }

  #[track_caller]
  pub fn layout_check(mut self, cases: &[LayoutCase]) {
    let wnd = self.create_wnd();

    cases.iter().for_each(|c| c.check(&wnd));
  }
}

impl Default for LayoutCase {
  fn default() -> Self {
    Self { path: &[0], x: None, y: None, width: None, height: None, visual_rect: None }
  }
}