ribir_core 0.2.0-alpha.4

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
use crate::prelude::*;
use rxrust::prelude::*;
use std::{
  cell::Cell,
  convert::Infallible,
  time::{Duration, Instant},
};

use self::focus_mgr::FocusType;

const MULTI_TAP_DURATION: Duration = Duration::from_millis(250);

bitflags! {
  #[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
  pub struct BuiltinFlags: u64 {
    // Listener flags, the flags are used to indicate what
    // kind of events the widget are listening to.
    const Lifecycle = 1 << 0;
    /// Pointer listener flag, hint the widget is listening to pointer events
    const Pointer = 1 << 1;
    /// Wheel listener flag, hint the widget is listening to wheel events
    const Wheel = 1 << 2;
    /// Keyboard listener flag, hint the widget is listening to keyboard events
    const KeyBoard = 1 << 3 | Self::Focus.bits();
    /// Whether the widget is a focus node also hint the widget
    /// is listening to focus/blur events
    const Focus = 1 << 4;
    /// Bubble focus event listener flag, hint the widget is listening to
    /// FocusIn/FocusOut and their capture events
    const FocusInOut = 1 << 5;

    const AllListeners = Self::Lifecycle.bits()
      | Self::Pointer.bits()
      | Self::Wheel.bits()
      | Self::KeyBoard.bits()
      | Self::Focus.bits()
      | Self::FocusInOut.bits();
    // listener end

    const AutoFocus = 1 << 47;
    // 16 bits keep for tab index
  }
}

pub type EventSubject = MutRefItemSubject<'static, Event, Infallible>;

#[derive(Default, Query)]
pub struct MixBuiltin {
  flags: Cell<BuiltinFlags>,
  subject: EventSubject,
}

/// The declarer of the `MixBuiltin` widget
pub struct MixBuiltinDeclarer(State<MixBuiltin>);

macro_rules! event_map_filter {
  ($event_name: ident, $event_ty: ident) => {
    (|e| match e {
      Event::$event_name(e) => Some(e),
      _ => None,
    }) as fn(&mut Event) -> Option<&mut $event_ty>
  };
}

macro_rules! impl_event_callback {
  ($this: ident, $listen_type: ident, $event_name: ident, $event_ty: ident, $handler: ident) => {{
    $this.flag_mark(BuiltinFlags::$listen_type);
    let _ = $this
      .subject()
      .filter_map(event_map_filter!($event_name, $event_ty))
      .subscribe($handler);

    $this
  }};
}

impl MixBuiltin {
  #[inline]
  pub fn contain_flag(&self, t: BuiltinFlags) -> bool { self.flags.get().contains(t) }

  pub fn flag_mark(&self, t: BuiltinFlags) {
    let t = self.flags.get() | t;
    self.flags.set(t)
  }

  pub fn dispatch(&self, event: &mut Event) { self.subject.clone().next(event) }

  pub fn subject(&self) -> EventSubject { self.subject.clone() }

  /// Listen to all events
  pub fn on_event(&self, handler: impl FnMut(&mut Event) + 'static) -> &Self {
    self.flag_mark(BuiltinFlags::AllListeners);
    let _ = self.subject().subscribe(handler);
    self
  }

  pub fn on_mounted(&self, handler: impl FnMut(&mut LifecycleEvent) + 'static) -> &Self {
    self.flag_mark(BuiltinFlags::Lifecycle);
    let _ = self
      .subject()
      .filter_map(event_map_filter!(Mounted, LifecycleEvent))
      .take(1)
      .subscribe(handler);

    self
  }

  pub fn on_performed_layout(&self, handler: impl FnMut(&mut LifecycleEvent) + 'static) -> &Self {
    impl_event_callback!(self, Lifecycle, PerformedLayout, LifecycleEvent, handler)
  }

  pub fn on_disposed(&self, handler: impl FnMut(&mut LifecycleEvent) + 'static) -> &Self {
    self.flag_mark(BuiltinFlags::Lifecycle);
    let _ = self
      .subject()
      .filter_map(event_map_filter!(Disposed, LifecycleEvent))
      .take(1)
      .subscribe(handler);

    self
  }

  pub fn on_pointer_down(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerDown, PointerEvent, handler)
  }

  pub fn on_pointer_down_capture(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerDownCapture, PointerEvent, handler)
  }

  pub fn on_pointer_up(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerUp, PointerEvent, handler)
  }

  pub fn on_pointer_up_capture(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerUpCapture, PointerEvent, handler)
  }

  pub fn on_pointer_move(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerMove, PointerEvent, handler)
  }

  pub fn on_pointer_move_capture(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerMoveCapture, PointerEvent, handler)
  }

  pub fn on_pointer_cancel(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerCancel, PointerEvent, handler)
  }

  pub fn on_pointer_enter(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerEnter, PointerEvent, handler)
  }

  pub fn on_pointer_leave(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, PointerLeave, PointerEvent, handler)
  }

  pub fn on_tap(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, Tap, PointerEvent, handler)
  }

  pub fn on_tap_capture(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    impl_event_callback!(self, Pointer, TapCapture, PointerEvent, handler)
  }

  pub fn on_double_tap(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    self.on_x_times_tap((2, handler))
  }

  pub fn on_double_tap_capture(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    self.on_x_times_tap_capture((2, handler))
  }

  pub fn on_triple_tap(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    self.on_x_times_tap((3, handler))
  }

  pub fn on_triple_tap_capture(&self, handler: impl FnMut(&mut PointerEvent) + 'static) -> &Self {
    self.on_x_times_tap_capture((3, handler))
  }

  pub fn on_x_times_tap(
    &self,
    (times, handler): (usize, impl FnMut(&mut PointerEvent) + 'static),
  ) -> &Self {
    self.on_x_times_tap_impl(times, MULTI_TAP_DURATION, false, handler)
  }

  pub fn on_x_times_tap_capture(
    &self,
    (times, handler): (usize, impl FnMut(&mut PointerEvent) + 'static),
  ) -> &Self {
    self.on_x_times_tap_impl(times, MULTI_TAP_DURATION, true, handler)
  }

  pub fn on_wheel(&self, handler: impl FnMut(&mut WheelEvent) + 'static) -> &Self {
    impl_event_callback!(self, Wheel, Wheel, WheelEvent, handler)
  }

  pub fn on_wheel_capture(&self, handler: impl FnMut(&mut WheelEvent) + 'static) -> &Self {
    impl_event_callback!(self, Wheel, WheelCapture, WheelEvent, handler)
  }

  fn on_x_times_tap_impl(
    &self,
    times: usize,
    dur: Duration,
    capture: bool,
    handler: impl FnMut(&mut PointerEvent) + 'static,
  ) -> &Self {
    self.flag_mark(BuiltinFlags::Pointer);
    self
      .subject()
      .filter_map(x_times_tap_map_filter(times, dur, capture))
      .subscribe(handler);
    self
  }

  pub fn on_ime_pre_edit(&self, f: impl FnMut(&mut ImePreEditEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, ImePreEdit, ImePreEditEvent, f)
  }

  pub fn on_ime_pre_edit_capture(&self, f: impl FnMut(&mut ImePreEditEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, ImePreEditCapture, ImePreEditEvent, f)
  }

  pub fn on_chars(&self, f: impl FnMut(&mut CharsEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, Chars, CharsEvent, f)
  }

  pub fn on_chars_capture(&self, f: impl FnMut(&mut CharsEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, CharsCapture, CharsEvent, f)
  }

  pub fn on_key_down(&self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, KeyDown, KeyboardEvent, f)
  }

  pub fn on_key_down_capture(&self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, KeyDownCapture, KeyboardEvent, f)
  }

  pub fn on_key_up(&self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, KeyUp, KeyboardEvent, f)
  }

  pub fn on_key_up_capture(&self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> &Self {
    impl_event_callback!(self, KeyBoard, KeyUpCapture, KeyboardEvent, f)
  }

  pub fn on_focus(&self, f: impl FnMut(&mut FocusEvent) + 'static) -> &Self {
    impl_event_callback!(self, Focus, Focus, FocusEvent, f)
  }

  pub fn on_blur(&self, f: impl FnMut(&mut FocusEvent) + 'static) -> &Self {
    impl_event_callback!(self, Focus, Blur, FocusEvent, f)
  }

  pub fn on_focus_in(&self, f: impl FnMut(&mut FocusEvent) + 'static) -> &Self {
    impl_event_callback!(self, FocusInOut, FocusIn, FocusEvent, f)
  }

  pub fn on_focus_in_capture(&self, f: impl FnMut(&mut FocusEvent) + 'static) -> &Self {
    impl_event_callback!(self, FocusInOut, FocusInCapture, FocusEvent, f)
  }

  pub fn on_focus_out(&self, f: impl FnMut(&mut FocusEvent) + 'static) -> &Self {
    impl_event_callback!(self, FocusInOut, FocusOut, FocusEvent, f)
  }

  pub fn on_focus_out_capture(&self, f: impl FnMut(&mut FocusEvent) + 'static) -> &Self {
    impl_event_callback!(self, FocusInOut, FocusOutCapture, FocusEvent, f)
  }

  /// Indicates that `widget` can be focused, and where it participates in
  /// sequential keyboard navigation (usually with the Tab key).
  pub fn is_focus_node(&self) -> bool { self.flags.get().contains(BuiltinFlags::Focus) }

  /// It accepts an integer as a value, with different results depending on the
  /// integer's value:
  /// - A negative value (usually -1) means that the widget is not reachable via
  ///   sequential keyboard navigation, but could be focused with API or
  ///   visually by clicking with the mouse.
  /// - Zero means that the element should be focusable in sequential keyboard
  ///   navigation, after any positive tab_index values and its order is defined
  ///   by the tree's source order.
  /// - A positive value means the element should be focusable in sequential
  ///   keyboard navigation, with its order defined by the value of the number.
  ///   That is, tab_index=4 is focused before tab_index=5 and tab_index=0, but
  ///   after tab_index=3. If multiple elements share the same positive
  ///   tab_index value, their order relative to each other follows their
  ///   position in the tree source. The maximum value for tab_index is 32767.
  ///   If not specified, it takes the default value 0.
  pub fn tab_index(&self) -> i16 { (self.flags.get().bits() >> 48) as i16 }

  /// Set the tab index of the focus node
  pub fn set_tab_index(&self, tab_idx: i16) {
    let flags = self.flags.get().bits() | ((tab_idx as u64) << 48);
    self.flags.set(BuiltinFlags::from_bits_retain(flags));
  }

  /// Indicates whether the `widget` should automatically get focus when the
  /// window loads.
  ///
  /// Only one widget should have this attribute specified.  If there are
  /// several, the widget nearest the root, get the initial
  /// focus.
  pub fn auto_focus(&self) -> bool { self.flags.get().contains(BuiltinFlags::AutoFocus) }

  pub fn set_auto_focus(&self, v: bool) {
    if v {
      self.flag_mark(BuiltinFlags::AutoFocus);
    } else {
      let mut flag = self.flags.get();
      flag.remove(BuiltinFlags::AutoFocus);
      self.flags.set(flag)
    }
  }

  fn merge(&self, other: Self) {
    let tab_index = self.tab_index();
    let other_tab_index = other.tab_index();
    self.flags.set(self.flags.get() | other.flags.get());
    if other_tab_index != 0 {
      self.set_tab_index(other_tab_index);
    } else if tab_index != 0 {
      self.set_tab_index(tab_index);
    }

    let other_subject = other.subject();
    fn subscribe_fn(subject: EventSubject) -> impl FnMut(&mut Event) {
      move |e: &mut Event| {
        subject.clone().next(e);
      }
    }
    self.subject().subscribe(subscribe_fn(other_subject));
  }

  fn callbacks_for_focus_node(&self) {
    self
      .on_mounted(move |e| {
        e.query_type(|mix: &MixBuiltin| {
          let auto_focus = mix.auto_focus();
          e.window().add_focus_node(e.id, auto_focus, FocusType::Node)
        });
      })
      .on_disposed(|e| e.window().remove_focus_node(e.id, FocusType::Node));
  }
}

impl MixBuiltinDeclarer {
  pub fn tab_index<_M, _V>(self, v: _V) -> Self
  where
    DeclareInit<i16>: DeclareFrom<_V, _M>,
  {
    let inner = self.0.read();
    self.0.read().flag_mark(BuiltinFlags::Focus);
    let v = DeclareInit::<i16>::declare_from(v);
    match v {
      DeclareInit::Value(v) => inner.set_tab_index(v),
      DeclareInit::Pipe(p) => {
        let (v, p) = p.into_pipe().unzip();
        inner.set_tab_index(v);
        let this = self.0.clone_reader();
        p.subscribe(move |(_, v)| this.read().set_tab_index(v));
      }
    }
    drop(inner);
    self
  }

  pub fn auto_focus<_M, _V>(self, v: _V) -> Self
  where
    DeclareInit<bool>: DeclareFrom<_V, _M>,
  {
    let inner = self.0.read();
    inner.flag_mark(BuiltinFlags::Focus);
    let v = DeclareInit::<bool>::declare_from(v);
    match v {
      DeclareInit::Value(v) => inner.set_auto_focus(v),
      DeclareInit::Pipe(p) => {
        let (v, p) = p.into_pipe().unzip();
        inner.set_auto_focus(v);
        let this = self.0.clone_reader();
        p.subscribe(move |(_, v)| this.read().set_auto_focus(v));
      }
    }
    drop(inner);
    self
  }

  pub fn on_event(self, handler: impl FnMut(&mut Event) + 'static) -> Self {
    self.0.read().on_event(handler);
    self
  }

  pub fn on_mounted(self, handler: impl FnMut(&mut LifecycleEvent) + 'static) -> Self {
    self.0.read().on_mounted(handler);
    self
  }

  pub fn on_performed_layout(self, handler: impl FnMut(&mut LifecycleEvent) + 'static) -> Self {
    self.0.read().on_performed_layout(handler);
    self
  }

  pub fn on_disposed(self, handler: impl FnMut(&mut LifecycleEvent) + 'static) -> Self {
    self.0.read().on_disposed(handler);
    self
  }

  pub fn on_pointer_down(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_down(handler);
    self
  }

  pub fn on_pointer_down_capture(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_down_capture(handler);
    self
  }

  pub fn on_pointer_up(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_up(handler);
    self
  }

  pub fn on_pointer_up_capture(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_up_capture(handler);
    self
  }

  pub fn on_pointer_move(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_move(handler);
    self
  }

  pub fn on_pointer_move_capture(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_move_capture(handler);
    self
  }

  pub fn on_pointer_cancel(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_cancel(handler);
    self
  }

  pub fn on_pointer_enter(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_enter(handler);
    self
  }

  pub fn on_pointer_leave(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_pointer_leave(handler);
    self
  }

  pub fn on_tap(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_tap(handler);
    self
  }

  pub fn on_tap_capture(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_tap_capture(handler);
    self
  }

  pub fn on_double_tap(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_double_tap(handler);
    self
  }

  pub fn on_double_tap_capture(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_double_tap_capture(handler);
    self
  }

  pub fn on_triple_tap(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_triple_tap(handler);
    self
  }

  pub fn on_triple_tap_capture(self, handler: impl FnMut(&mut PointerEvent) + 'static) -> Self {
    self.0.read().on_triple_tap_capture(handler);
    self
  }

  pub fn on_x_times_tap(self, handler: (usize, impl FnMut(&mut PointerEvent) + 'static)) -> Self {
    self.0.read().on_x_times_tap(handler);
    self
  }

  pub fn on_x_times_tap_capture(
    self,
    handler: (usize, impl FnMut(&mut PointerEvent) + 'static),
  ) -> Self {
    self.0.read().on_x_times_tap_capture(handler);
    self
  }

  pub fn on_wheel(self, handler: impl FnMut(&mut WheelEvent) + 'static) -> Self {
    self.0.read().on_wheel(handler);
    self
  }

  pub fn on_wheel_capture(self, handler: impl FnMut(&mut WheelEvent) + 'static) -> Self {
    self.0.read().on_wheel_capture(handler);
    self
  }

  pub fn on_ime_pre_edit(self, f: impl FnMut(&mut ImePreEditEvent) + 'static) -> Self {
    self.0.read().on_ime_pre_edit(f);
    self
  }

  pub fn on_ime_pre_edit_capture(self, f: impl FnMut(&mut ImePreEditEvent) + 'static) -> Self {
    self.0.read().on_ime_pre_edit_capture(f);
    self
  }

  pub fn on_chars(self, f: impl FnMut(&mut CharsEvent) + 'static) -> Self {
    self.0.read().on_chars(f);
    self
  }

  pub fn on_chars_capture(self, f: impl FnMut(&mut CharsEvent) + 'static) -> Self {
    self.0.read().on_chars_capture(f);
    self
  }

  pub fn on_key_down(self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> Self {
    self.0.read().on_key_down(f);
    self
  }

  pub fn on_key_down_capture(self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> Self {
    self.0.read().on_key_down_capture(f);
    self
  }

  pub fn on_key_up(self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> Self {
    self.0.read().on_key_up(f);
    self
  }

  pub fn on_key_up_capture(self, f: impl FnMut(&mut KeyboardEvent) + 'static) -> Self {
    self.0.read().on_key_up_capture(f);
    self
  }

  pub fn on_focus(self, f: impl FnMut(&mut FocusEvent) + 'static) -> Self {
    self.0.read().on_focus(f);
    self
  }

  pub fn on_blur(self, f: impl FnMut(&mut FocusEvent) + 'static) -> Self {
    self.0.read().on_blur(f);
    self
  }

  pub fn on_focus_in(self, f: impl FnMut(&mut FocusEvent) + 'static) -> Self {
    self.0.read().on_focus_in(f);
    self
  }

  pub fn on_focus_in_capture(self, f: impl FnMut(&mut FocusEvent) + 'static) -> Self {
    self.0.read().on_focus_in_capture(f);
    self
  }

  pub fn on_focus_out(self, f: impl FnMut(&mut FocusEvent) + 'static) -> Self {
    self.0.read().on_focus_out(f);
    self
  }

  pub fn on_focus_out_capture(self, f: impl FnMut(&mut FocusEvent) + 'static) -> Self {
    self.0.read().on_focus_out_capture(f);
    self
  }
}

impl Declare for MixBuiltin {
  type Builder = MixBuiltinDeclarer;
  fn declare_builder() -> Self::Builder { MixBuiltinDeclarer(State::value(MixBuiltin::default())) }
}

impl DeclareBuilder for MixBuiltinDeclarer {
  type Target = State<MixBuiltin>;
  fn build_declare(self, _: &BuildCtx) -> Self::Target { self.0 }
}

impl ComposeChild for MixBuiltin {
  type Child = Widget;
  #[inline]
  fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder {
    move |ctx: &BuildCtx| match this.try_into_value() {
      Ok(this) => {
        let mut this = Some(this);
        child
          .id()
          .assert_get(&ctx.tree.borrow().arena)
          .query_most_outside(|m: &MixBuiltin| {
            let this = this.take().unwrap();
            if !m.contain_flag(BuiltinFlags::Focus) && this.contain_flag(BuiltinFlags::Focus) {
              this.callbacks_for_focus_node();
            }
            m.merge(this)
          });
        if let Some(this) = this {
          if this.contain_flag(BuiltinFlags::Focus) {
            this.callbacks_for_focus_node();
          }
          child.attach_data(this, ctx)
        } else {
          child
        }
      }
      Err(this) => {
        if this.read().contain_flag(BuiltinFlags::Focus) {
          this.read().callbacks_for_focus_node();
        }
        child.attach_data(this, ctx)
      }
    }
  }
}

fn x_times_tap_map_filter(
  x: usize,
  dur: Duration,
  capture: bool,
) -> impl FnMut(&mut Event) -> Option<&mut PointerEvent> {
  assert!(x > 0);
  struct TapInfo {
    pointer_id: PointerId,
    stamps: Vec<Instant>,
  }

  let mut type_info: Option<TapInfo> = None;
  move |e: &mut Event| {
    let e = match e {
      Event::Tap(e) if !capture => e,
      Event::TapCapture(e) if capture => e,
      _ => return None,
    };
    let now = Instant::now();
    match &mut type_info {
      Some(info) if info.pointer_id == e.id => {
        if info.stamps.len() + 1 == x {
          if now.duration_since(info.stamps[0]) <= dur {
            // emit x-tap event and reset the tap info
            type_info = None;
            Some(e)
          } else {
            // remove the expired tap
            info.stamps.remove(0);
            info.stamps.push(now);
            None
          }
        } else {
          info.stamps.push(now);
          None
        }
      }
      _ => {
        type_info = Some(TapInfo { pointer_id: e.id, stamps: vec![now] });
        None
      }
    }
  }
}