ribir_core 0.3.0-alpha.2

Ribir is a framework for building modern native/wasm cross-platform user interface 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use crate::{
  events::{
    dispatcher::Dispatcher,
    focus_mgr::{FocusManager, FocusType},
  },
  prelude::*,
  ticker::{FrameMsg, FrameTicker},
};
use futures::{task::LocalSpawnExt, Future};
use std::{
  cell::{Cell, RefCell},
  collections::VecDeque,
  convert::Infallible,
  rc::Rc,
  time::Instant,
};
use winit::event::{DeviceId, ElementState, MouseButton, WindowEvent};
pub use winit::window::CursorIcon;

/// Window is the root to represent.
///
/// We use `RefCell` to wrap every field of `Window` to make sure we can split
/// borrow the fields in runtime. So we can pass `Window` to user when the
/// framework borrower  one of the fields. e.g. `dispatcher` is borrowed when
/// dispatch the event, but user may access the `Window` to change the title in
/// event callback.
pub struct Window {
  pub(crate) painter: RefCell<Painter>,
  pub(crate) dispatcher: RefCell<Dispatcher>,
  pub(crate) widget_tree: RefCell<WidgetTree>,
  pub(crate) frame_ticker: FrameTicker,
  pub(crate) focus_mgr: RefCell<FocusManager>,
  pub(crate) running_animates: Rc<Cell<u32>>,
  /// This vector store the task to emit events. When perform layout, dispatch
  /// event and so on, some part of window may be already mutable borrowed and
  /// the user event callback may also query borrow that part, so we can't emit
  /// event immediately. So we store the event emitter in this vector,
  /// and emit them after all borrow finished.
  pub(crate) delay_emitter: RefCell<VecDeque<DelayEvent>>,
  /// A task pool use to process `Future` or `rxRust` task, and will block until
  /// all task finished before current frame end.
  frame_pool: RefCell<FuturesLocalSchedulerPool>,
  /// A priority queue of tasks. So that tasks with lower priority value will be
  /// executed first.
  priority_task_queue: PriorityTaskQueue<'static>,
  shell_wnd: RefCell<Box<dyn ShellWindow>>,
  /// A vector store the widget id pair of (parent, child). The child need to
  /// drop after its `DelayDrop::delay_drop_until` be false or its parent
  /// is dropped.
  ///
  /// This widgets it's detached from its parent, but still need to paint.
  delay_drop_widgets: RefCell<Vec<(Option<WidgetId>, WidgetId)>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
pub struct WindowId(u64);

pub trait ShellWindow {
  fn id(&self) -> WindowId;
  fn inner_size(&self) -> Size;
  fn outer_size(&self) -> Size;
  fn set_ime_cursor_area(&mut self, rect: &Rect);
  fn set_ime_allowed(&mut self, allowed: bool);

  fn request_resize(&mut self, size: Size);
  fn on_resize(&mut self, size: Size);
  fn set_min_size(&mut self, size: Size);
  fn cursor(&self) -> CursorIcon;
  fn set_cursor(&mut self, cursor: CursorIcon);
  fn set_title(&mut self, str: &str);
  fn set_icon(&mut self, icon: &PixelImage);
  fn set_visible(&mut self, visible: bool);
  fn is_resizable(&self) -> bool;
  fn set_resizable(&mut self, resizable: bool);
  fn is_minimized(&self) -> bool;
  fn set_minimized(&mut self, minimized: bool);
  fn focus_window(&mut self);
  fn set_decorations(&mut self, decorations: bool);
  fn as_any(&self) -> &dyn Any;
  fn as_any_mut(&mut self) -> &mut dyn Any;
  /// The device pixel ratio of Window interface returns the ratio of the
  /// resolution in physical pixels to the logic pixels for the current display
  /// device.
  fn device_pixel_ratio(&self) -> f32;
  fn begin_frame(&mut self);
  fn draw_commands(&mut self, viewport: Rect, commands: Vec<PaintCommand>, surface: Color);
  fn end_frame(&mut self);
}

impl Window {
  #[deprecated(note = "The core window should not depends on shell window event.")]
  #[inline]
  /// processes native events from this native window
  pub fn processes_native_event(&self, event: WindowEvent) {
    let ratio = self.device_pixel_ratio() as f64;
    self.dispatcher.borrow_mut().dispatch(event, ratio);
  }

  pub fn processes_keyboard_event(
    &self,
    physical_key: PhysicalKey,
    key: VirtualKey,
    is_repeat: bool,
    location: KeyLocation,
    state: ElementState,
  ) {
    self.dispatcher.borrow_mut().dispatch_keyboard_input(
      physical_key,
      key,
      is_repeat,
      location,
      state,
    );
  }

  pub fn processes_receive_chars(&self, chars: String) {
    self.dispatcher.borrow_mut().dispatch_receive_chars(chars)
  }

  pub fn processes_ime_pre_edit(&self, ime: ImePreEdit) {
    self.dispatcher.borrow_mut().dispatch_ime_pre_edit(ime)
  }

  pub fn process_mouse_input(&self, device_id: DeviceId, state: ElementState, button: MouseButton) {
    self
      .dispatcher
      .borrow_mut()
      .dispatch_mouse_input(device_id, state, button);
  }

  /// Request switch the focus to next widget.
  pub fn request_next_focus(&self) {
    self
      .focus_mgr
      .borrow_mut()
      .focus_next_widget(&self.widget_tree.borrow().arena);
  }

  /// Request switch the focus to prev widget.
  pub fn request_prev_focus(&self) {
    self
      .focus_mgr
      .borrow_mut()
      .focus_prev_widget(&self.widget_tree.borrow().arena);
  }

  /// Return an `rxRust` Scheduler, which will guarantee all task add to the
  /// scheduler will finished before current frame finished.
  #[inline]
  pub fn frame_scheduler(&self) -> FuturesLocalScheduler { self.frame_pool.borrow().spawner() }

  /// Spawns a task that polls the given future with output `()` to completion.
  /// And guarantee wait this task will finished in current frame.
  pub fn frame_spawn(&self, f: impl Future<Output = ()> + 'static) -> Result<(), SpawnError> {
    self.frame_scheduler().spawn_local(f)
  }

  pub fn priority_task_queue(&self) -> &PriorityTaskQueue<'static> { &self.priority_task_queue }

  pub fn frame_tick_stream(&self) -> Subject<'static, FrameMsg, Infallible> {
    self.frame_ticker.frame_tick_stream()
  }

  pub fn inc_running_animate(&self) { self.running_animates.set(self.running_animates.get() + 1); }

  pub fn dec_running_animate(&self) { self.running_animates.set(self.running_animates.get() - 1); }

  /// Draw an image what current render tree represent.
  #[track_caller]
  pub fn draw_frame(&self) -> bool {
    AppCtx::run_until_stalled();
    self.frame_ticker.emit(FrameMsg::NewFrame(Instant::now()));
    self.run_frame_tasks();

    self.update_painter_viewport();
    let draw = self.need_draw() && !self.size().is_empty();
    if draw {
      self.shell_wnd.borrow_mut().begin_frame();

      self.layout();

      self.widget_tree.borrow().draw();
      self.draw_delay_drop_widgets();

      let surface = match AppCtx::app_theme() {
        Theme::Full(theme) => theme.palette.surface(),
        Theme::Inherit(_) => unreachable!(),
      };

      let mut shell = self.shell_wnd.borrow_mut();
      let inner_size = shell.inner_size();
      let paint_cmds = self.painter.borrow_mut().finish();
      shell.draw_commands(Rect::from_size(inner_size), paint_cmds, surface);

      shell.end_frame();
    }

    AppCtx::end_frame();
    self.frame_ticker.emit(FrameMsg::Finish(Instant::now()));

    draw
  }

  pub fn layout(&self) {
    loop {
      self.run_frame_tasks();

      self
        .widget_tree
        .borrow_mut()
        .layout(self.shell_wnd.borrow().inner_size());
      self.run_frame_tasks();

      if !self.widget_tree.borrow().is_dirty() {
        self
          .focus_mgr
          .borrow_mut()
          .refresh_focus(&self.widget_tree.borrow().arena);
        self.run_frame_tasks();
      }

      if !self.widget_tree.borrow().is_dirty() {
        let ready = FrameMsg::LayoutReady(Instant::now());
        self.frame_ticker.emit(ready);
        self.run_frame_tasks();
      }

      if !self.widget_tree.borrow().is_dirty() {
        break;
      }
    }
  }

  pub fn update_painter_viewport(&self) {
    let size = self.shell_wnd.borrow().inner_size();
    if self.painter.borrow().viewport().size != size {
      let mut tree = self.widget_tree.borrow_mut();
      let root = tree.root();
      tree.mark_dirty(root);
      tree.store.remove(root);
      let mut painter = self.painter.borrow_mut();
      painter.set_viewport(Rect::from_size(size));
      painter.reset();
    }
  }

  pub fn need_draw(&self) -> bool {
    self.widget_tree.borrow().is_dirty() || self.running_animates.get() > 0
  }

  pub fn new(shell_wnd: Box<dyn ShellWindow>) -> Rc<Self> {
    let focus_mgr = RefCell::new(FocusManager::new());
    let widget_tree = RefCell::new(WidgetTree::default());
    let dispatcher = RefCell::new(Dispatcher::new());
    let size = shell_wnd.inner_size();
    let mut painter = Painter::new(Rect::from_size(size));
    painter.set_viewport(Rect::from_size(size));
    let window = Self {
      dispatcher,
      widget_tree,
      painter: RefCell::new(painter),
      focus_mgr,
      delay_emitter: <_>::default(),
      frame_ticker: FrameTicker::default(),
      running_animates: <_>::default(),
      frame_pool: <_>::default(),
      priority_task_queue: PriorityTaskQueue::default(),
      shell_wnd: RefCell::new(shell_wnd),
      delay_drop_widgets: <_>::default(),
    };
    let window = Rc::new(window);
    window.dispatcher.borrow_mut().init(Rc::downgrade(&window));
    window.focus_mgr.borrow_mut().init(Rc::downgrade(&window));
    window.widget_tree.borrow_mut().init(Rc::downgrade(&window));

    window
  }

  pub fn set_content_widget(&self, root: impl WidgetBuilder) {
    let build_ctx = BuildCtx::new(None, &self.widget_tree);
    let root = root.build(&build_ctx);
    self.widget_tree.borrow_mut().set_content(root.consume())
  }

  #[inline]
  pub fn id(&self) -> WindowId { self.shell_wnd.borrow().id() }

  /// Return the current focused widget id.
  pub fn focusing(&self) -> Option<WidgetId> { self.focus_mgr.borrow().focusing() }

  /// The device pixel ratio of Window interface returns the ratio of the
  /// resolution in physical pixels to the logic pixels for the current display
  /// device.
  pub fn device_pixel_ratio(&self) -> f32 { self.shell_wnd.borrow().device_pixel_ratio() }

  pub fn set_title(&self, title: &str) { self.shell_wnd.borrow_mut().set_title(title); }

  pub fn set_icon(&self, icon: &PixelImage) { self.shell_wnd.borrow_mut().set_icon(icon); }

  /// Returns the cursor icon of the window.
  pub fn get_cursor(&self) -> CursorIcon { self.shell_wnd.borrow().cursor() }

  /// Modifies the cursor icon of the window.
  pub fn set_cursor(&self, cursor: CursorIcon) { self.shell_wnd.borrow_mut().set_cursor(cursor); }

  /// Sets location of IME candidate box in window global coordinates relative
  /// to the top left.
  pub fn set_ime_cursor_area(&self, rect: &Rect) {
    self.shell_wnd.borrow_mut().set_ime_cursor_area(rect);
  }

  pub fn set_ime_allowed(&self, allowed: bool) {
    self.shell_wnd.borrow_mut().set_ime_allowed(allowed);
  }

  pub fn request_resize(&self, size: Size) { self.shell_wnd.borrow_mut().request_resize(size) }

  pub fn size(&self) -> Size { self.shell_wnd.borrow().inner_size() }

  pub fn set_min_size(&self, size: Size) { self.shell_wnd.borrow_mut().set_min_size(size); }

  pub fn shell_wnd(&self) -> &RefCell<Box<dyn ShellWindow>> { &self.shell_wnd }

  pub(crate) fn add_focus_node(&self, wid: WidgetId, auto_focus: bool, focus_type: FocusType) {
    self
      .focus_mgr
      .borrow_mut()
      .add_focus_node(wid, auto_focus, focus_type);
  }

  pub(crate) fn remove_focus_node(&self, wid: WidgetId, focus_tyep: FocusType) {
    self
      .focus_mgr
      .borrow_mut()
      .remove_focus_node(wid, focus_tyep);
  }

  pub(crate) fn add_delay_event(&self, e: DelayEvent) {
    self.delay_emitter.borrow_mut().push_back(e);
  }

  fn draw_delay_drop_widgets(&self) {
    let mut delay_widgets = self.delay_drop_widgets.borrow_mut();
    let mut painter = self.painter.borrow_mut();

    delay_widgets.retain(|(parent, wid)| {
      let tree = self.widget_tree.borrow();
      let drop_conditional = wid
        .assert_get(&self.widget_tree.borrow().arena)
        .query_most_outside(|d: &DelayDrop| d.delay_drop_until)
        .unwrap_or(true);
      let parent_dropped = parent.map_or(false, |p| {
        p.is_dropped(&tree.arena) || p.ancestors(&tree.arena).last() != Some(tree.root())
      });
      let need_drop = drop_conditional || parent_dropped;
      if need_drop {
        drop(tree);
        self.widget_tree.borrow_mut().remove_subtree(*wid);
      } else {
        let mut painter = painter.save_guard();
        if let Some(p) = parent {
          let offset = tree.store.map_to_global(Point::zero(), *p, &tree.arena);
          painter.translate(offset.x, offset.y);
        }
        let mut ctx = PaintingCtx::new(*wid, self.id(), &mut painter);
        wid.paint_subtree(&mut ctx);
      }
      !need_drop
    });
  }

  fn run_priority_tasks(&self) {
    while let Some((task, _)) = self.priority_task_queue.pop() {
      // `pipe` used priority task queue to update the subtree, we need to force
      // execute the async task and emit events in order. For example, `pipe1`
      // run first and remove a subtree, then `pipe2` run. We need to make sure
      // when `pipe2` run, the subtree is really removed.
      self.emit_events();
      task.run();
    }
  }
  /// Immediately emit all delay events. You should not call this method only if
  /// you want to interfere with the framework event dispatch process and know
  /// what you are doing.
  pub fn emit_events(&self) {
    loop {
      let Some(e) = self.delay_emitter.borrow_mut().pop_front() else {
        break;
      };

      match e {
        DelayEvent::Mounted(id) => {
          let mut e = Event::Mounted(LifecycleEvent::new(id, self.id()));
          self.emit(id, &mut e);
        }
        DelayEvent::PerformedLayout(id) => {
          let mut e = Event::PerformedLayout(LifecycleEvent::new(id, self.id()));
          self.emit(id, &mut e);
        }
        DelayEvent::Disposed { id, parent } => {
          id.descendants(&self.widget_tree.borrow().arena)
            .collect::<Vec<_>>()
            .into_iter()
            .rev()
            .for_each(|id| {
              if Some(id) == self.focusing() {
                self.focus_mgr.borrow_mut().blur_on_dispose();
              }
              let mut e = Event::Disposed(LifecycleEvent::new(id, self.id()));
              self.emit(id, &mut e);
            });

          let delay_drop = id
            .assert_get(&self.widget_tree.borrow().arena)
            .contain_type::<DelayDrop>();

          if delay_drop {
            self.delay_drop_widgets.borrow_mut().push((parent, id));
          } else {
            self.add_delay_event(DelayEvent::RemoveSubtree(id));
          }
        }
        DelayEvent::RemoveSubtree(id) => {
          self.widget_tree.borrow_mut().remove_subtree(id);
        }
        DelayEvent::Focus(id) => {
          let mut e = Event::Focus(FocusEvent::new(id, self.id()));
          self.emit(id, &mut e);
        }
        DelayEvent::FocusIn { bottom, up } => {
          let mut e = Event::FocusInCapture(FocusEvent::new(bottom, self.id()));
          self.top_down_emit(&mut e, bottom, up);
          let mut e = Event::FocusIn(FocusEvent::new(bottom, self.id()));
          self.bottom_up_emit(&mut e, bottom, up);
        }
        DelayEvent::Blur(id) => {
          let mut e = Event::Blur(FocusEvent::new(id, self.id()));
          self.emit(id, &mut e);
        }
        DelayEvent::FocusOut { bottom, up } => {
          let mut e = Event::FocusOutCapture(FocusEvent::new(bottom, self.id()));
          self.top_down_emit(&mut e, bottom, up);
          let mut e = Event::FocusOut(FocusEvent::new(bottom, self.id()));
          self.bottom_up_emit(&mut e, bottom, up);
        }
        DelayEvent::KeyDown(event) => {
          let id = event.id();

          let mut e = Event::KeyDownCapture(event);
          self.top_down_emit(&mut e, id, None);
          let Event::KeyDownCapture(e) = e else {
            unreachable!()
          };
          let mut e = Event::KeyDown(e);
          self.bottom_up_emit(&mut e, id, None);
          let Event::KeyDown(e) = e else { unreachable!() };
          if !e.is_prevent_default() && *e.key() == VirtualKey::Named(NamedKey::Tab) {
            self.add_delay_event(DelayEvent::TabFocusMove);
          }
        }
        DelayEvent::TabFocusMove => {
          let pressed_shift = {
            let dispatcher = self.dispatcher.borrow();
            dispatcher.info.modifiers().contains(ModifiersState::SHIFT)
          };

          let mut focus_mgr = self.focus_mgr.borrow_mut();
          if pressed_shift {
            focus_mgr.focus_prev_widget(&self.widget_tree.borrow().arena);
          } else {
            focus_mgr.focus_next_widget(&self.widget_tree.borrow().arena);
          }
        }
        DelayEvent::KeyUp(event) => {
          let id = event.id();
          let mut e = Event::KeyUpCapture(event);
          self.top_down_emit(&mut e, id, None);
          let Event::KeyUpCapture(e) = e else {
            unreachable!()
          };
          let mut e = Event::KeyUp(e);
          self.bottom_up_emit(&mut e, id, None);
        }
        DelayEvent::Chars { id, chars } => {
          let mut e = Event::CharsCapture(CharsEvent::new(chars, id, self.id()));
          self.top_down_emit(&mut e, id, None);
          let Event::CharsCapture(e) = e else {
            unreachable!()
          };
          let mut e = Event::Chars(e);
          self.bottom_up_emit(&mut e, id, None);
        }
        DelayEvent::Wheel { id, delta_x, delta_y } => {
          let mut e = Event::WheelCapture(WheelEvent::new(delta_x, delta_y, id, self.id()));
          self.top_down_emit(&mut e, id, None);
          let mut e = Event::Wheel(WheelEvent::new(delta_x, delta_y, id, self.id()));
          self.bottom_up_emit(&mut e, id, None);
        }
        DelayEvent::PointerDown(id) => {
          let mut e = Event::PointerDownCapture(PointerEvent::from_mouse(id, self));
          self.top_down_emit(&mut e, id, None);
          let mut e = Event::PointerDown(PointerEvent::from_mouse(id, self));
          self.bottom_up_emit(&mut e, id, None);
          self
            .focus_mgr
            .borrow_mut()
            .refresh_focus(&self.widget_tree.borrow().arena);
        }
        DelayEvent::PointerMove(id) => {
          let mut e = Event::PointerMoveCapture(PointerEvent::from_mouse(id, self));
          self.top_down_emit(&mut e, id, None);
          let mut e = Event::PointerMove(PointerEvent::from_mouse(id, self));
          self.bottom_up_emit(&mut e, id, None);
        }
        DelayEvent::PointerUp(id) => {
          let mut e = Event::PointerUpCapture(PointerEvent::from_mouse(id, self));
          self.top_down_emit(&mut e, id, None);
          let mut e = Event::PointerUp(PointerEvent::from_mouse(id, self));
          self.bottom_up_emit(&mut e, id, None);
        }
        DelayEvent::_PointerCancel(id) => {
          let mut e = Event::PointerCancel(PointerEvent::from_mouse(id, self));
          self.bottom_up_emit(&mut e, id, None);
        }
        DelayEvent::PointerEnter { bottom, up } => {
          let mut e = Event::PointerEnter(PointerEvent::from_mouse(bottom, self));
          self.top_down_emit(&mut e, bottom, up);
        }
        DelayEvent::PointerLeave { bottom, up } => {
          let mut e = Event::PointerLeave(PointerEvent::from_mouse(bottom, self));
          self.bottom_up_emit(&mut e, bottom, up);
        }
        DelayEvent::Tap(wid) => {
          let mut e = Event::TapCapture(PointerEvent::from_mouse(wid, self));
          self.top_down_emit(&mut e, wid, None);
          let mut e = Event::Tap(PointerEvent::from_mouse(wid, self));
          self.bottom_up_emit(&mut e, wid, None);
        }
        DelayEvent::ImePreEdit { wid, pre_edit } => {
          let mut e = Event::ImePreEditCapture(ImePreEditEvent::new(pre_edit, wid, self));
          self.top_down_emit(&mut e, wid, None);
          let Event::ImePreEditCapture(e) = e else {
            unreachable!()
          };
          self.bottom_up_emit(&mut Event::ImePreEdit(e), wid, None);
        }
      }
    }
  }

  fn emit(&self, id: WidgetId, e: &mut Event) {
    // Safety: we only use tree to query the inner data of a node and dispatch a
    // event by it, and never read or write the node. And in the callback, there is
    // no way to mut access the inner data of node or destroy the node.
    let tree = unsafe { &*(&*self.widget_tree.borrow() as *const WidgetTree) };
    id.assert_get(&tree.arena)
      .query_type_inside_first(|m: &MixBuiltin| {
        if m.contain_flag(e.flags()) {
          m.dispatch(e);
        }
        true
      });
  }

  fn top_down_emit(&self, e: &mut Event, bottom: WidgetId, up: Option<WidgetId>) {
    let tree = self.widget_tree.borrow();
    let path = bottom
      .ancestors(&tree.arena)
      .take_while(|id| Some(*id) != up)
      .collect::<Vec<_>>();

    path.iter().rev().all(|id| {
      id.assert_get(&tree.arena)
        .query_type_outside_first(|m: &MixBuiltin| {
          if m.contain_flag(e.flags()) {
            e.set_current_target(*id);
            m.dispatch(e);
          }
          e.is_propagation()
        })
    });
  }

  fn bottom_up_emit(&self, e: &mut Event, bottom: WidgetId, up: Option<WidgetId>) {
    if !e.is_propagation() {
      return;
    }

    let tree = self.widget_tree.borrow();
    bottom
      .ancestors(&tree.arena)
      .take_while(|id| Some(*id) != up)
      .all(|id| {
        id.assert_get(&tree.arena)
          .query_type_inside_first(|m: &MixBuiltin| {
            if m.contain_flag(e.flags()) {
              e.set_current_target(id);
              m.dispatch(e);
            }
            e.is_propagation()
          })
      });
  }

  /// Run all async tasks need finished in current frame and emit all delay
  /// events.
  pub fn run_frame_tasks(&self) {
    loop {
      self.frame_pool.borrow_mut().run();

      if self.delay_emitter.borrow().is_empty()
        && self.priority_task_queue.is_empty()
        && AppCtx::run_until_stalled() == 0
      {
        break;
      }

      self.run_priority_tasks();
      self.emit_events();
    }
  }

  pub fn map_to_global(&self, point: Point, id: WidgetId) -> Point {
    self
      .widget_tree
      .borrow()
      .store
      .map_to_global(point, id, &self.widget_tree.borrow().arena)
  }

  pub fn layout_size(&self, id: WidgetId) -> Option<Size> {
    self.widget_tree.borrow().store.layout_box_size(id)
  }
}

/// Event that delay to emit, emit it when the window is not busy(nobody borrow
/// parts of the window).
#[derive(Debug)]
pub(crate) enum DelayEvent {
  Mounted(WidgetId),
  PerformedLayout(WidgetId),
  Disposed {
    parent: Option<WidgetId>,
    id: WidgetId,
  },
  RemoveSubtree(WidgetId),
  Focus(WidgetId),
  Blur(WidgetId),
  FocusIn {
    bottom: WidgetId,
    up: Option<WidgetId>,
  },
  FocusOut {
    bottom: WidgetId,
    up: Option<WidgetId>,
  },
  KeyDown(KeyboardEvent),
  KeyUp(KeyboardEvent),
  TabFocusMove,
  Chars {
    id: WidgetId,
    chars: String,
  },
  Wheel {
    id: WidgetId,
    delta_x: f32,
    delta_y: f32,
  },
  PointerDown(WidgetId),
  PointerMove(WidgetId),
  PointerUp(WidgetId),
  _PointerCancel(WidgetId),
  PointerEnter {
    bottom: WidgetId,
    up: Option<WidgetId>,
  },
  PointerLeave {
    bottom: WidgetId,
    up: Option<WidgetId>,
  },
  Tap(WidgetId),
  ImePreEdit {
    wid: WidgetId,
    pre_edit: ImePreEdit,
  },
}

impl From<u64> for WindowId {
  #[inline]
  fn from(value: u64) -> Self { WindowId(value) }
}

impl From<WindowId> for u64 {
  #[inline]
  fn from(value: WindowId) -> Self { value.0 }
}
#[cfg(test)]
mod tests {
  use super::*;
  use crate::{reset_test_env, test_helper::*};
  use ribir_dev_helper::assert_layout_result_by_path;

  #[test]
  fn layout_after_wnd_resize() {
    reset_test_env!();

    let size = Size::new(100., 100.);
    let mut wnd = TestWindow::new_with_size(fn_widget! { MockBox { size: INFINITY_SIZE } }, size);
    wnd.draw_frame();
    assert_layout_result_by_path!(wnd, { path = [0], size == size, });

    let new_size = Size::new(200., 200.);
    wnd.request_resize(new_size);

    wnd.draw_frame();
    assert_layout_result_by_path!(wnd, { path = [0], size == new_size, });
  }
}