ribir_core/widget_children/
compose_child_impl.rs

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
use super::*;

/// The trait is used to enable child composition for `ComposeChild`.
///
/// We choose to return a pair of parent and child instead of directly composing
/// and returning a `Widget`. This approach allows for continued composition
/// with certain child types like `Vec`.
///
/// The `M` and `WRITER` use to avoid implementation conflicts.
///
/// - The `WRITER` marker if it is a writer.
/// - The `TML` marker if it is a template.
/// - The `N` marker is used to distinguish the type fill in `Template`
/// - The `M` marker is used for child conversion.
pub trait ComposeWithChild<
  'w,
  C,
  const WRITER: bool,
  const TML: bool,
  const N: usize,
  const M: usize,
>
{
  type Target;
  fn with_child(self, child: C) -> Self::Target;
}

// ComposeWithChild implementations
impl<'w, P, C, const M: usize> ComposeWithChild<'w, C, true, false, 0, M> for P
where
  P: StateWriter,
  P::Value: ComposeChild<'w>,
  C: IntoChildCompose<<P::Value as ComposeChild<'w>>::Child, M>,
{
  type Target = Pair<P, <P::Value as ComposeChild<'w>>::Child>;

  fn with_child(self, child: C) -> Self::Target {
    Pair { parent: self, child: child.into_child_compose() }
  }
}

impl<'w, P, Builder, C, const N: usize, const M: usize> ComposeWithChild<'w, C, true, true, N, M>
  for P
where
  P: StateWriter,
  P::Value: ComposeChild<'w, Child: Template<Builder = Builder>>,
  Builder: ComposeWithChild<'w, C, false, true, N, M>,
{
  type Target = Pair<Self, Builder::Target>;

  fn with_child(self, child: C) -> Self::Target {
    let child = <P::Value as ComposeChild<'w>>::Child::builder().with_child(child);
    Pair { parent: self, child }
  }
}

impl<'w, P, C, Target, const TML: bool, const N: usize, const M: usize>
  ComposeWithChild<'w, C, false, TML, N, M> for P
where
  P: ComposeChild<'w>,
  State<P>: ComposeWithChild<'w, C, true, TML, N, M, Target = Target>,
{
  type Target = Target;

  fn with_child(self, child: C) -> Self::Target { State::value(self).with_child(child) }
}

impl<'w, W, C, const TML: bool, const WRITER: bool, const N: usize, const M: usize>
  ComposeWithChild<'w, C, WRITER, TML, N, M> for FatObj<W>
where
  W: ComposeWithChild<'w, C, WRITER, TML, N, M>,
{
  type Target = FatObj<W::Target>;

  fn with_child(self, child: C) -> Self::Target { self.map(|host| host.with_child(child)) }
}

// Option needn't implement for `Template`
impl<'w, T, C, const WRITER: bool, const N: usize, const M: usize>
  ComposeWithChild<'w, C, WRITER, false, N, M> for Option<T>
where
  T: ComposeWithChild<'w, C, WRITER, false, 0, M>,
  C: IntoChildCompose<Widget<'w>, M>,
  T::Target: IntoWidget<'w, N>,
{
  type Target = Widget<'w>;

  fn with_child(self, c: C) -> Self::Target {
    if let Some(p) = self { p.with_child(c).into_widget() } else { c.into_child_compose() }
  }
}

// The continuation with a child is only possible if the child of `Pair` is a
// `Template`.
impl<'w, W, C1, C2: 'w, const WRITER: bool, const M: usize, const N: usize>
  ComposeWithChild<'w, C2, WRITER, true, N, M> for Pair<W, C1>
where
  C1: ComposeWithChild<'w, C2, WRITER, true, N, M>,
{
  type Target = Pair<W, C1::Target>;

  fn with_child(self, c: C2) -> Self::Target {
    let Pair { parent: widget, child } = self;
    Pair { parent: widget, child: child.with_child(c) }
  }
}

impl<'w, W, C, const M: usize> IntoWidgetStrict<'w, M> for Pair<W, C>
where
  W: StateWriter,
  W::Value: ComposeChild<'w>,
  C: IntoChildCompose<<W::Value as ComposeChild<'w>>::Child, M> + 'w,
{
  #[inline]
  fn into_widget_strict(self) -> Widget<'w> {
    let Self { parent, child } = self;
    ComposeChild::compose_child(parent, child.into_child_compose()).into_widget()
  }
}

// impl Option as Template
impl<T> Template for Option<T> {
  type Builder = OptionBuilder<T>;

  #[inline]
  fn builder() -> Self::Builder { OptionBuilder(None) }
}

/// The template builder for `Option` introduces a new type to disambiguate the
/// `with_child` method call for `Option`, especially when `Option` acts as a
/// parent for a widget with `with_child` method.
pub struct OptionBuilder<T>(Option<T>);

impl<T> TemplateBuilder for OptionBuilder<T> {
  type Target = Option<T>;
  #[inline]
  fn build_tml(self) -> Self::Target { self.0 }
}

impl<T> ComposeChildFrom<OptionBuilder<T>, 1> for Option<T> {
  #[inline]
  fn compose_child_from(from: OptionBuilder<T>) -> Self { from.build_tml() }
}

impl<'w, C, T, const M: usize> ComposeWithChild<'w, C, false, true, 0, M> for OptionBuilder<T>
where
  C: IntoChildCompose<T, M>,
{
  type Target = Self;

  #[inline]
  fn with_child(self, child: C) -> Self::Target { self.with_child(Some(child)) }
}

impl<'w, C, T, const M: usize> ComposeWithChild<'w, Option<C>, false, true, 1, M>
  for OptionBuilder<T>
where
  C: IntoChildCompose<T, M>,
{
  type Target = Self;

  #[inline]
  fn with_child(mut self, child: Option<C>) -> Self::Target {
    debug_assert!(self.0.is_none(), "Option already has a child");
    self.0 = child.map(IntoChildCompose::into_child_compose);
    self
  }
}

// impl Vec<T> as Template

pub struct VecBuilder<T>(Vec<T>);

impl<T> Template for Vec<T> {
  type Builder = VecBuilder<T>;
  #[inline]
  fn builder() -> Self::Builder { VecBuilder(vec![]) }
}

impl<T> TemplateBuilder for VecBuilder<T> {
  type Target = Vec<T>;
  #[inline]
  fn build_tml(self) -> Self::Target { self.0 }
}

impl<T> ComposeChildFrom<VecBuilder<T>, 1> for Vec<T> {
  #[inline]
  fn compose_child_from(from: VecBuilder<T>) -> Self { from.build_tml() }
}

impl<'w, C, T, const M: usize> ComposeWithChild<'w, C, false, true, 0, M> for VecBuilder<T>
where
  C: IntoChildCompose<T, M>,
{
  type Target = Self;

  #[inline]
  fn with_child(mut self, child: C) -> Self::Target {
    self.0.push(child.into_child_compose());
    self
  }
}

impl<'w, C, T, const M: usize> ComposeWithChild<'w, C, false, true, 1, M> for VecBuilder<T>
where
  C: IntoIterator,
  C::Item: IntoChildCompose<T, M>,
{
  type Target = Self;

  #[inline]
  fn with_child(mut self, child: C) -> Self::Target {
    self
      .0
      .extend(child.into_iter().map(|v| v.into_child_compose()));
    self
  }
}

// todo: remove it, keep it for backward compatibility.

impl ChildOfCompose for Resource<PixelImage> {}

pub trait CompatibilityWithChild<'w, C, const N: usize, const M: usize> {
  type Target;
  fn with_child(self, child: C) -> Self::Target;
}

impl<'w, W, C, const M: usize> CompatibilityWithChild<'w, C, 8, M> for State<W>
where
  W: ComposeDecorator + 'static,
  C: IntoWidget<'w, M>,
{
  type Target = Widget<'w>;

  fn with_child(self, child: C) -> Self::Target {
    let host = child.into_widget();

    let f = move || {
      let tid = TypeId::of::<W>();
      let ctx = BuildCtx::get();
      let decor = Provider::of::<ComposeDecorators>(BuildCtx::get())
        .and_then(|t| QueryRef::filter_map(t, |t| t.styles.get(&tid)).ok());

      if let Some(style) = decor {
        style(Box::new(self), host, ctx)
      } else {
        ComposeDecorator::compose_decorator(self, host).into_widget()
      }
    };
    f.into_widget()
  }
}

impl<'w, T, C, const M: usize> CompatibilityWithChild<'w, C, 9, M> for T
where
  T: ComposeDecorator + 'static,
  C: IntoWidget<'w, M>,
{
  type Target = Widget<'w>;

  fn with_child(self, child: C) -> Self::Target { State::value(self).with_child(child) }
}

impl<'w, W, C, const N: usize, const M: usize> CompatibilityWithChild<'w, C, N, M> for FatObj<W>
where
  W: CompatibilityWithChild<'w, C, N, M>,
{
  type Target = FatObj<W::Target>;

  fn with_child(self, child: C) -> Self::Target { self.map(|host| host.with_child(child)) }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::test_helper::MockBox;

  #[derive(Template)]
  enum PTml {
    Void(Void),
  }

  impl ChildOfCompose for Void {}

  struct P;

  impl ComposeChild<'static> for P {
    type Child = PTml;
    fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> Widget<'static> {
      Void.into_widget()
    }
  }

  #[derive(Declare)]
  struct XX;

  impl<'c> ComposeChild<'c> for XX {
    type Child = Widget<'c>;

    fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> Widget<'c> {
      Void.into_widget()
    }
  }

  #[test]
  fn template_fill_template() { let _ = |_: &BuildCtx| P.with_child(Void).into_widget(); }

  #[test]
  fn pair_compose_child() {
    let _ = |_: &BuildCtx| -> Widget {
      MockBox { size: ZERO_SIZE }
        .with_child(XX.with_child(Void {}))
        .into_widget()
    };
  }

  #[derive(Declare)]
  struct PipeParent;

  impl ComposeChild<'static> for PipeParent {
    type Child = BoxPipe<usize>;

    fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> Widget<'static> {
      Void.into_widget()
    }
  }

  #[test]
  fn compose_pipe_child() {
    let _value_child = fn_widget! {
      @PipeParent {  @ { BoxPipe::value(0) } }
    };

    let _pipe_child = fn_widget! {
      let state = State::value(0);
      @PipeParent {  @ { pipe!(*$state) } }
    };
  }
}