ribir_core 0.4.0-alpha.2

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
use crate::{prelude::*, widget::Widget};
mod compose_child_impl;
mod multi_child_impl;
mod single_child_impl;
pub use compose_child_impl::*;
pub use multi_child_impl::*;
pub use single_child_impl::*;
pub mod child_convert;
pub use child_convert::{ChildFrom, FromAnother};

/// Trait to mark a widget can have one child.
pub trait SingleChild {}

/// A boxed render widget that support accept one child.
pub struct BoxedSingleChild(Widget);

/// Trait to tell Ribir a object that has multi children.
pub trait MultiChild {}

/// A boxed render widget that support accept multi children.
pub struct BoxedMultiChild(Widget);

/// Trait to mark an object that it should compose with its child as a
/// `SinglePair` and the parent and child keep their type.
pub trait PairChild {}

/// Trait mark widget can have one child and also have compose logic for widget
/// and its child.
pub trait ComposeChild: Sized {
  type Child;
  fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> impl WidgetBuilder;
}

/// A pair of object and its child without compose, this keep the type
/// information of parent and child. `PairChild` and `ComposeChild` can create a
/// `Pair` with its child.
pub struct Pair<W, C> {
  parent: W,
  child: C,
}

/// A alias of `Pair<W, Widget>`, means `Widget` is the child of the generic
/// type.
pub type WidgetOf<W> = Pair<W, Widget>;

impl RenderBuilder for BoxedSingleChild {
  #[inline]
  fn build(self, _: &BuildCtx) -> Widget { self.0 }
}

impl SingleParent for BoxedSingleChild {
  #[inline]
  fn compose_child(self, child: Widget, ctx: &BuildCtx) -> Widget {
    ctx.append_child(self.0.id(), child);
    self.0
  }
}

impl RenderBuilder for BoxedMultiChild {
  #[inline]
  fn build(self, _: &BuildCtx) -> Widget { self.0 }
}

impl MultiParent for BoxedMultiChild {
  fn compose_children(self, children: impl Iterator<Item = Widget>, ctx: &BuildCtx) -> Widget {
    for c in children {
      ctx.append_child(self.0.id(), c)
    }
    self.0
  }
}

/// The trait to help `SingleChild` to compose child so the `SingleChild` no
/// need to expose the compose logic. If user want have its own compose logic,
/// use `ComposeChild` instead.
pub(crate) trait SingleParent {
  fn compose_child(self, child: Widget, ctx: &BuildCtx) -> Widget;
}

/// The trait to help `MultiParent` to compose child so the `MultiParent` no
/// need to expose the compose logic. If user want have its own compose logic,
/// use `ComposeChild` instead.
pub(crate) trait MultiParent {
  fn compose_children(self, children: impl Iterator<Item = Widget>, ctx: &BuildCtx) -> Widget;
}

impl<T: Render + SingleChild> SingleParent for T {
  fn compose_child(self, child: Widget, ctx: &BuildCtx) -> Widget {
    let p = self.build(ctx);
    ctx.append_child(p.id(), child);

    p
  }
}

impl<T: SingleParent> SingleParent for Option<T> {
  fn compose_child(self, child: Widget, ctx: &BuildCtx) -> Widget {
    if let Some(this) = self { this.compose_child(child, ctx) } else { child }
  }
}

impl<T: Render + MultiChild> MultiParent for T {
  fn compose_children(self, children: impl Iterator<Item = Widget>, ctx: &BuildCtx) -> Widget {
    let p = self.build(ctx);
    for c in children {
      ctx.append_child(p.id(), c);
    }
    p
  }
}

impl BoxedSingleChild {
  #[inline]
  pub fn new(widget: impl RenderBuilder + SingleChild, ctx: &BuildCtx) -> Self {
    Self(widget.build(ctx))
  }

  /// Create a `BoxedSingleChild` from a `Widget` and not check the type , the
  /// caller should make sure the `w` is build from a `SingleChild`.
  #[inline]
  pub(crate) fn from_id(w: Widget) -> Self { Self(w) }
}

impl BoxedMultiChild {
  #[inline]
  pub fn new(widget: impl RenderBuilder + MultiChild, ctx: &BuildCtx) -> Self {
    Self(widget.build(ctx))
  }
}

impl<W, C> Pair<W, C> {
  #[inline]
  pub fn new(parent: W, child: C) -> Self { Self { parent, child } }

  #[inline]
  pub fn unzip(self) -> (W, C) {
    let Self { parent: widget, child } = self;
    (widget, child)
  }

  #[inline]
  pub fn child(self) -> C { self.child }

  #[inline]
  pub fn parent(self) -> W { self.parent }
}

impl<W, C> Pair<FatObj<W>, C> {
  /// Replace the host of the FatObj in parent with the child, this is useful
  /// when the host of the `FatObj` is useless.
  pub fn child_replace_host(self) -> FatObj<C> {
    let Self { parent, child } = self;
    parent.map(|_| child)
  }
}

pub trait PairWithChild<C> {
  type Target;
  fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target;
}

impl<W: PairChild, C> PairWithChild<C> for W {
  type Target = Pair<W, C>;

  #[inline]
  #[track_caller]
  fn with_child(self, child: C, _: &BuildCtx) -> Self::Target { Pair { parent: self, child } }
}

impl<W, C1: PairChild, C2> PairWithChild<C2> for Pair<W, C1> {
  type Target = Pair<W, Pair<C1, C2>>;

  #[track_caller]
  fn with_child(self, c: C2, ctx: &BuildCtx) -> Self::Target {
    let Pair { parent: widget, child } = self;
    Pair { parent: widget, child: child.with_child(c, ctx) }
  }
}

#[cfg(test)]
mod tests {
  use ribir_dev_helper::*;

  use super::*;
  use crate::{reset_test_env, test_helper::*};

  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn compose_template_child() {
    reset_test_env!();
    #[derive(Declare)]
    struct Page;
    #[derive(Declare, PairChild)]
    struct Header;
    #[derive(Declare, PairChild)]
    struct Content;
    #[derive(Declare, PairChild)]
    struct Footer;

    #[derive(Template)]
    struct PageTml {
      _header: WidgetOf<FatObj<Header>>,
      _content: WidgetOf<FatObj<Content>>,
      _footer: WidgetOf<FatObj<Footer>>,
    }

    impl ComposeChild for Page {
      type Child = PageTml;

      fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> impl WidgetBuilder {
        fn_widget!(Void)
      }
    }

    let _ = fn_widget! {
      @Page {
        @Header { @Void {} }
        @Content { @Void {} }
        @Footer { @Void {} }
      }
    };
  }

  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn compose_option_child() {
    reset_test_env!();

    #[derive(Declare)]
    struct Parent;
    #[derive(Declare, PairChild)]
    struct Child;

    impl ComposeChild for Parent {
      type Child = Option<Pair<FatObj<Child>, Widget>>;

      fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> impl WidgetBuilder {
        fn_widget!(Void)
      }
    }

    let _ = fn_widget! {
      @Parent {
        @Child { @Void {} }
      }
    };
  }
  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn compose_option_dyn_parent() {
    reset_test_env!();

    let _ = fn_widget! {
      let p = Some(MockBox { size: Size::zero() });
      @$p { @{ Void } }
    };
  }

  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn tuple_as_vec() {
    reset_test_env!();

    #[derive(Declare)]
    struct A;
    #[derive(Declare)]
    struct B;

    impl ComposeChild for A {
      type Child = Vec<B>;

      fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> impl WidgetBuilder {
        fn_widget!(Void)
      }
    }
    let a = A;
    let _ = fn_widget! {
      @$a {
        @ { B}
        @ { B }
      }
    };
  }

  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn expr_with_child() {
    reset_test_env!();

    let size = Stateful::new(Size::zero());
    let c_size = size.clone_watcher();
    // with single child
    let _e = fn_widget! {
      let p = pipe!{
        if $c_size.area() > 0. {
          MockBox { size: *$c_size }
        } else {
          MockBox { size: Size::new(1., 1.) }
        }
      };
      @$p { @MockBox { size: pipe!(*$c_size) } }
    };

    // with multi child
    let _e = fn_widget! {
      @MockMulti {
        @MockBox { size: Size::zero() }
        @MockBox { size: Size::zero() }
        @MockBox { size: Size::zero() }
      }
    };

    let c_size = size.clone_watcher();
    // option with single child
    let _e = fn_widget! {
      let p = pipe!(($c_size.area() > 0.).then(|| @MockBox { size: Size::zero() }));
      @$p { @MockBox { size: Size::zero() } }
    };

    // option with `Widget`
    let _e = fn_widget! {
      let p = pipe!(($size.area() > 0.).then(|| @MockBox { size: Size::zero() }));
      @$p { @ { Void }}
    };
  }

  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn compose_expr_option_widget() {
    reset_test_env!();

    let _ = fn_widget! {
      @MockBox {
        size: ZERO_SIZE,
        @{ Some(@MockBox { size: Size::zero() })}
      }
    };
  }

  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn pair_to_pair() {
    reset_test_env!();

    #[derive(Declare)]
    struct P;

    #[derive(Declare, PairChild)]
    struct X;

    impl ComposeChild for P {
      type Child = WidgetOf<FatObj<X>>;
      fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> impl WidgetBuilder {
        fn_widget!(Void)
      }
    }

    let _ = fn_widget! {
      @P { @X { @Void {} } }
    };
  }
  #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
  #[test]
  fn fix_multi_fill_for_pair() {
    reset_test_env!();

    struct X;
    impl ComposeChild for X {
      type Child = Widget;
      fn compose_child(
        _: impl StateWriter<Value = Self>, child: Self::Child,
      ) -> impl WidgetBuilder {
        fn_widget!(child)
      }
    }

    let _ = |ctx| -> Widget {
      let child = MockBox { size: ZERO_SIZE }.with_child(Void, ctx);
      X.with_child(child, ctx).build(ctx)
    };
  }

  const FIX_OPTION_TEMPLATE_EXPECT_SIZE: Size = Size::new(100., 200.);
  fn fix_option_template() -> impl WidgetBuilder {
    struct Field;

    #[derive(Template, Default)]
    pub struct ConfigTml {
      _field: Option<Field>,
    }
    #[derive(Declare)]
    struct Host {}

    impl ComposeChild for Host {
      type Child = Option<ConfigTml>;
      fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> impl WidgetBuilder {
        fn_widget! { @MockBox { size: FIX_OPTION_TEMPLATE_EXPECT_SIZE } }
      }
    }

    fn_widget! { @Host { @{ Field } }}
  }
  widget_layout_test!(
    fix_option_template,
    { path = [0], size == FIX_OPTION_TEMPLATE_EXPECT_SIZE, }
  );
}