ribir_core 0.0.1-alpha.5

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
pub(crate) use crate::widget_tree::*;
use crate::{context::*, prelude::*};
use ribir_algo::ShareResource;
use rxrust::subscription::{BoxSubscription, SubscriptionGuard};

#[doc(hidden)]
pub use std::{
  any::{Any, TypeId},
  marker::PhantomData,
  ops::Deref,
};
use std::{cell::RefCell, rc::Rc};
pub trait Compose: Sized {
  /// Describes the part of the user interface represented by this widget.
  /// Called by framework, should never directly call it.
  fn compose(this: State<Self>) -> Widget;
}

pub struct HitTest {
  pub hit: bool,
  pub can_hit_child: bool,
}

/// RenderWidget is a widget which want to paint something or do a layout to
/// calc itself size and update children positions.
///
/// Render Widget should at least implement one of `Layout` or `Paint`, if all
/// of `as_layout` and `as_paint` return None, the widget will not display.
///
/// If `as_layout` return none, widget size will detected by its single child if
/// it has or as large as possible.
pub trait Render: Query {
  /// Do the work of computing the layout for this widget, and return the
  /// size it need.
  ///
  /// In implementing this function, You are responsible for calling every
  /// children's perform_layout across the `LayoutCtx`
  fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size;

  /// `paint` is a low level trait to help you draw your widget to paint device
  /// across `PaintingCtx::painter` by itself coordinate system. Not care
  /// about children's paint in this method, framework will call children's
  /// paint individual. And framework guarantee always paint parent before
  /// children.
  fn paint(&self, ctx: &mut PaintingCtx);

  /// Whether the constraints from parent are the only input to detect the
  /// widget size, and child nodes' size not affect its size.
  fn only_sized_by_parent(&self) -> bool { false }

  /// Determines the set of render widgets located at the given position.
  fn hit_test(&self, ctx: &HitTestCtx, pos: Point) -> HitTest {
    let is_hit = hit_test_impl(ctx, pos);
    HitTest { hit: is_hit, can_hit_child: is_hit }
  }

  fn get_transform(&self) -> Option<Transform> { None }
}

pub(crate) fn hit_test_impl(ctx: &HitTestCtx, pos: Point) -> bool {
  ctx.box_rect().map_or(false, |rect| rect.contains(pos))
}

pub enum Widget {
  Compose(Box<dyn for<'r> FnOnce(&'r BuildCtx) -> Widget>),
  Render {
    render: Box<dyn Render>,
    children: Option<Vec<Widget>>,
  },
}

/// A trait to query dynamic type and its inner type on runtime, use this trait
/// to provide type information you want framework know.
pub trait Query {
  /// A type can composed by others, this method query all type(include self)
  /// match the type id, and call the callback one by one. The callback accept
  /// an `& dyn Any` of the target type, and return if it want to continue.
  fn query_all(
    &self,
    type_id: TypeId,
    callback: &mut dyn FnMut(&dyn Any) -> bool,
    order: QueryOrder,
  );
}

#[derive(Clone, Copy)]
pub enum QueryOrder {
  InnerFirst,
  OutsideFirst,
}

/// Trait to detect if a type is match the `type_id`.
pub trait QueryFiler {
  /// query self type by type id, and return a reference of `Any` trait to cast
  /// to target type if type match.
  fn query_filter(&self, type_id: TypeId) -> Option<&dyn Any>;
  /// query self type by type id, and return a mut reference of `Any` trait to
  /// cast to target type if type match.
  fn query_filter_mut(&mut self, type_id: TypeId) -> Option<&mut dyn Any>;
}

/// Convert a widget to `Widget`
pub trait IntoWidget<M: ImplMarker> {
  fn into_widget(self) -> Widget;
}

impl<W: 'static> QueryFiler for W {
  #[inline]
  fn query_filter(&self, type_id: TypeId) -> Option<&dyn Any> {
    (self.type_id() == type_id).then_some(self as &dyn Any)
  }

  #[inline]
  fn query_filter_mut(&mut self, type_id: TypeId) -> Option<&mut dyn Any> {
    ((*self).type_id() == type_id).then_some(self as &mut dyn Any)
  }
}

impl<'a> dyn Render + 'a {
  #[inline]
  pub fn query_all_type<T: Any>(&self, mut callback: impl FnMut(&T) -> bool, order: QueryOrder) {
    self.query_all(
      TypeId::of::<T>(),
      &mut |a: &dyn Any| a.downcast_ref().map_or(true, &mut callback),
      order,
    )
  }

  /// Query the first match type in all type by special order, and call
  /// `callback`
  pub fn query_on_first_type<T: Any>(&self, order: QueryOrder, callback: impl FnOnce(&T)) {
    let mut callback = Some(callback);
    self.query_all_type(
      move |a| {
        let cb = callback.take().expect("should only call once");
        cb(a);
        false
      },
      order,
    );
  }

  pub fn contain_type<T: Any>(&self) -> bool {
    let mut hit = false;
    self.query_all_type(
      |_: &T| {
        hit = true;
        false
      },
      QueryOrder::OutsideFirst,
    );
    hit
  }
}

pub trait ImplMarker {}
/// implement marker means this converter not hope to convert continue.
pub struct SelfImpl;
/// implement marker means this converter can use as a generic bounds to convert
/// continue.
pub struct NotSelf<M>(PhantomData<fn(M)>);

impl ImplMarker for SelfImpl {}
impl<M> ImplMarker for NotSelf<M> {}

impl IntoWidget<SelfImpl> for Widget {
  #[inline]
  fn into_widget(self) -> Widget { self }
}

macro_rules! impl_compose_into_widget {
  ($ty: ty) => {
    impl<C: Compose> IntoWidget<NotSelf<[(); 0]>> for $ty {
      #[inline]
      fn into_widget(self) -> Widget { Compose::compose(State::<C>::from(self)) }
    }
  };
}

impl_compose_into_widget!(State<C>);
impl_compose_into_widget!(C);
impl_compose_into_widget!(Stateful<C>);
// `Stateful<DynWidget<C>>` has its own implementation.
// impl_compose_into_widget!(Stateful<DynWidget<C>>);

impl<R: Render + 'static> IntoWidget<NotSelf<[(); 1]>> for R {
  #[inline]
  fn into_widget(self) -> Widget {
    Widget::Render {
      render: Box::new(self),
      children: None,
    }
  }
}

impl<M1, M2, W> IntoWidget<NotSelf<(M1, M2)>> for State<W>
where
  W: IntoWidget<M1>,
  Stateful<W>: IntoWidget<M2>,
  M1: ImplMarker,
  M2: ImplMarker,
{
  fn into_widget(self) -> crate::widget::Widget {
    match self {
      State::Stateless(w) => w.into_widget(),
      State::Stateful(s) => s.into_widget(),
    }
  }
}

macro_rules! impl_compose_option_child_into_widget {
  ($ty: ty) => {
    impl<T, C> IntoWidget<NotSelf<[(); 2]>> for $ty
    where
      T: ComposeChild<Child = Option<C>> + 'static,
    {
      #[inline]
      fn into_widget(self) -> Widget { ComposeChild::compose_child(State::<T>::from(self), None) }
    }
  };
}

impl_compose_option_child_into_widget!(Stateful<T>);
impl_compose_option_child_into_widget!(T);
// `Stateful<DynWidget<T>>` has its own implementation.
// impl_compose_option_child_into_widget!(Stateful<DynWidget<T>>);

impl<F, R, M> IntoWidget<NotSelf<[M; 3]>> for F
where
  F: FnOnce(&BuildCtx) -> R + 'static,
  R: IntoWidget<M>,
  M: ImplMarker,
{
  #[inline]
  fn into_widget(self) -> Widget { Widget::Compose(Box::new(move |ctx| self(ctx).into_widget())) }
}

#[macro_export]
macro_rules! impl_proxy_query {
  (reverse [$first: expr $(, $rest: expr)*] $($reversed: expr)*) => {
    impl_proxy_query!(reverse [$($rest),*] $first $($reversed)*);
  };
  (reverse [] $($reversed: expr)*) => { $($reversed)* };
  (
    $($self: ident .$name: ident $(($($args: ident),*))?),+
  ) => {
    #[inline]
    fn query_all(
      &self,
      type_id: TypeId,
      callback: &mut dyn FnMut(&dyn Any) -> bool,
      order: QueryOrder,
    ) {
      let mut query_more = true;
      match order {
        QueryOrder::InnerFirst => {
          impl_proxy_query!(reverse
            [$(
              if query_more {
                self.$name $(($($args),*))?
                  .query_all(
                    type_id,
                    &mut |any| {
                      query_more = callback(any);
                      query_more
                    },
                    order,
                  );
              }
            ),+]
          );
          if let Some(a) = self.query_filter(type_id) {
            callback(a);
          }
        }
        QueryOrder::OutsideFirst => {
          if let Some(a) = self.query_filter(type_id) {
            query_more = callback(a);
          }
          if query_more {
            $(
              if query_more {
                self.$name $(($($args),*))?
                  .query_all(
                    type_id,
                    &mut |any| {
                      query_more = callback(any);
                      query_more
                    },
                    order,
                  );
              }
            )+
          }
        }
      }
    }
  };
}

#[macro_export]
macro_rules! impl_query_self_only {
  () => {
    #[inline]
    fn query_all(
      &self,
      type_id: TypeId,
      callback: &mut dyn FnMut(&dyn Any) -> bool,
      _: QueryOrder,
    ) {
      if let Some(a) = self.query_filter(type_id) {
        callback(a);
      }
    }
  };
}

impl<T: Render> Render for ribir_algo::ShareResource<T> {
  #[inline]
  fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
    T::perform_layout(self, clamp, ctx)
  }

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

  #[inline]
  fn only_sized_by_parent(&self) -> bool { T::only_sized_by_parent(self) }

  #[inline]
  fn hit_test(&self, ctx: &HitTestCtx, pos: Point) -> HitTest { T::hit_test(self, ctx, pos) }

  #[inline]
  fn get_transform(&self) -> Option<Transform> { T::get_transform(self) }
}

impl<T: Query> Query for ShareResource<T> {
  fn query_all(
    &self,
    type_id: TypeId,
    callback: &mut dyn FnMut(&dyn Any) -> bool,
    order: QueryOrder,
  ) {
    (**self).query_all(type_id, callback, order)
  }
}

#[macro_export]
macro_rules! impl_proxy_render {
  ($($proxy: tt)*) => {
    #[inline]
    fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
      self.$($proxy)*.perform_layout(clamp, ctx)
    }

    #[inline]
    fn paint(&self, ctx: &mut PaintingCtx) { self.$($proxy)*.paint(ctx) }

    #[inline]
    fn only_sized_by_parent(&self) -> bool {
      self.$($proxy)*.only_sized_by_parent()
    }

    #[inline]
    fn hit_test(&self, ctx: &HitTestCtx, pos: Point) -> HitTest {
      self.$($proxy)*.hit_test(ctx, pos)
    }

    #[inline]
    fn get_transform(&self) -> Option<Transform> {
      self.$($proxy)*.get_transform()
    }
  };
}

impl<W: Render + 'static> Render for RefCell<W> {
  impl_proxy_render!(borrow());
}

impl<W: Query + 'static> Query for RefCell<W> {
  impl_proxy_query!(self.borrow());
}

impl<W: Render + 'static> Render for Rc<W> {
  impl_proxy_render!(deref());
}

impl<W: Query + 'static> Query for Rc<W> {
  impl_proxy_query!(self.deref());
}

impl Render for Box<dyn Render> {
  impl_proxy_render!(deref());
}

impl Query for Box<dyn Render> {
  impl_proxy_query!(self.deref());
}

impl Query for Vec<SubscriptionGuard<BoxSubscription<'static>>> {
  impl_query_self_only!();
}

/// Directly return `v`, this function does nothing, but it's useful to help you
/// declare a widget expression in `widget!` macro.
#[inline]
pub const fn from<W>(v: W) -> W { v }

/// Return OptionWidget::Some widget if the `b` is true, the return value wrap
/// from the return value of `f` method called.
#[inline]
pub fn then<W>(b: bool, f: impl FnOnce() -> W) -> Option<W> { b.then(f) }

/// calls the closure on `value` and returns
#[inline]
pub fn map<T, W>(value: T, f: impl FnOnce(T) -> W) -> W { f(value) }