whisker-runtime 0.13.5

Host-independent Whisker runtime: reactivity, retained elements, layout, and frames.
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
//! [`IntoView`] — uniform return type for components.
//!
//! A component fn returns `impl IntoView`. The renderer or parent
//! component calls `.into_view()` to get either an [`Element`]
//! (for "this is one element") or a `View` (for fragments, tuples,
//! components nested inside `render!`).
//!
//! The trait is intentionally minimal: every implementor produces a
//! [`View`], which is a single element, a text snippet, a fragment of
//! children, or empty.

use super::handle::Element;

/// A renderable. Components return `impl IntoView`; the renderer
/// (called by the macro at mount time, or by the parent's
/// `render!` expansion) calls `.into_view()` to get the underlying
/// `View`.
pub trait IntoView {
    fn into_view(self) -> View;
}

/// Collector used by public builder `body` methods.
///
/// The compose macros only call [`push`](Self::push); the concrete parent
/// builder decides whether to attach the collected views immediately or keep
/// a closure for later component remounts.
#[derive(Default)]
pub struct ChildrenBuilder {
    children: Vec<View>,
}

impl ChildrenBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(&mut self, child: impl IntoView) {
        self.children.push(child.into_view());
    }

    pub fn extend(&mut self, children: impl IntoIterator<Item = impl IntoView>) {
        self.children
            .extend(children.into_iter().map(IntoView::into_view));
    }

    pub fn finish(self) -> View {
        match self.children.len() {
            0 => View::Empty,
            1 => self.children.into_iter().next().unwrap_or(View::Empty),
            _ => View::Fragment(self.children),
        }
    }
}

/// Type used by `#[component]` for the conventional `children` prop.
/// The `render!` macro routes a component invocation's non-kwarg
/// children into a `move || View::Fragment(…)` closure of this type;
/// the component body invokes it to materialise the children at the
/// point in the tree where they should appear.
///
/// `Fn` (not `FnOnce`) so the closure survives being re-invoked across
/// hot-reload remounts, and `Rc` (not `Box`) so `Children` is `Clone` —
/// `#[component]` re-clones every prop on every body invocation.
pub type Children = ::std::rc::Rc<dyn ::std::ops::Fn() -> View + 'static>;

/// Authoring children whose rendered value must contain only plain-text
/// fragments.
///
/// This wrapper lets an element declaration distinguish text content from
/// ordinary scene children without changing the React-like `View` value built
/// by `render!`. The Rust renderer validates and normalizes the materialized
/// raw-text nodes before a Host sees the resulting `SetText` operation.
#[derive(Clone)]
pub struct TextChildren(pub Children);

impl TextChildren {
    /// Wraps the children closure emitted by `render!`.
    pub fn new(children: Children) -> Self {
        Self(children)
    }

    /// Materializes the wrapped authoring children.
    pub fn render(&self) -> View {
        (self.0)()
    }
}

/// Mount a `Children` prop at the current position in the tree.
///
/// Returns a phantom element (no on-screen footprint — `is_phantom`
/// is true) with the children's view attached. The caller (typically
/// framework-level control flow projecting a [`Children`] handle)
/// hands this back to the parent so the parent
/// sees a single Element handle, exactly like any other child node.
///
/// Takes `&Children` so nothing moves out of the surrounding
/// `#[component]` body and the hot-reload outer can re-call it without
/// `cannot move out of`. The children `Rc` is left untouched, so it can
/// be handed to further `mount_children` calls at other positions
/// (multi-projection a la Leptos `ChildrenFn`).
pub fn mount_children(children: &Children) -> Element {
    let ph = super::create_phantom_element();
    let view = children();
    view.attach_to(ph);
    ph
}

/// Mounts one dynamically typed `IntoView` value behind a phantom slot.
#[doc(hidden)]
pub fn mount_view(view: impl IntoView) -> Element {
    let ph = super::create_phantom_element();
    view.into_view().attach_to(ph);
    ph
}

/// Mount plain-text authoring children at their declared element.
pub fn mount_text_children(children: &TextChildren, parent: Element) -> Vec<Element> {
    children.render().attach_to(parent)
}

// Function-shaped prop types for control-flow components. These
// newtypes let `#[component]`-annotated control-flow functions accept
// closure literals via `Into` in the typed-builder `Props`, and wrap
// `Rc<dyn Fn>` for the same `Clone` reason as [`Children`].

/// `Fn() -> Vec<T>` — the "what items to render" closure for a
/// keyed-list control flow. Wrapping in a newtype gives typed-builder
/// a concrete Props field type plus an `Into` path from any matching
/// closure literal.
pub struct EachFn<T: 'static>(pub ::std::rc::Rc<dyn ::std::ops::Fn() -> Vec<T> + 'static>);

impl<T: 'static> Clone for EachFn<T> {
    fn clone(&self) -> Self {
        EachFn(::std::rc::Rc::clone(&self.0))
    }
}

impl<T: 'static, F: Fn() -> Vec<T> + 'static> From<F> for EachFn<T> {
    fn from(f: F) -> Self {
        EachFn(::std::rc::Rc::new(f))
    }
}

impl<T: 'static> EachFn<T> {
    /// Invoke the wrapped closure.
    pub fn call(&self) -> Vec<T> {
        (self.0)()
    }
}

/// `Fn(&T) -> K` — the "key extractor" closure for a keyed-list
/// control flow. Items whose keys match across reactive reruns
/// reuse their owners + per-item state.
pub struct KeyFn<T: 'static, K: 'static>(pub ::std::rc::Rc<dyn ::std::ops::Fn(&T) -> K + 'static>);

impl<T: 'static, K: 'static> Clone for KeyFn<T, K> {
    fn clone(&self) -> Self {
        KeyFn(::std::rc::Rc::clone(&self.0))
    }
}

impl<T: 'static, K: 'static, F: Fn(&T) -> K + 'static> From<F> for KeyFn<T, K> {
    fn from(f: F) -> Self {
        KeyFn(::std::rc::Rc::new(f))
    }
}

impl<T: 'static, K: 'static> KeyFn<T, K> {
    /// Invoke the wrapped closure on `item`.
    pub fn call(&self, item: &T) -> K {
        (self.0)(item)
    }
}

/// `Fn(T) -> Element` — the "render one item" closure for a
/// keyed-list control flow. The returned [`Element`] is what gets
/// attached to the surrounding fragment / list.
pub struct ItemFn<T: 'static>(pub ::std::rc::Rc<dyn ::std::ops::Fn(T) -> Element + 'static>);

impl<T: 'static> Clone for ItemFn<T> {
    fn clone(&self) -> Self {
        ItemFn(::std::rc::Rc::clone(&self.0))
    }
}

impl<T: 'static, F: Fn(T) -> Element + 'static> From<F> for ItemFn<T> {
    fn from(f: F) -> Self {
        ItemFn(::std::rc::Rc::new(f))
    }
}

impl<T: 'static> ItemFn<T> {
    /// Invoke the wrapped closure on `item`.
    pub fn call(&self, item: T) -> Element {
        (self.0)(item)
    }
}

/// `Fn() -> bool` — the predicate closure for a `show`-style
/// conditional control flow. Wrapping in a newtype gives
/// typed-builder a concrete Props field type plus an `Into` path
/// from any matching closure literal.
pub struct WhenFn(pub ::std::rc::Rc<dyn ::std::ops::Fn() -> bool + 'static>);

impl Clone for WhenFn {
    fn clone(&self) -> Self {
        WhenFn(::std::rc::Rc::clone(&self.0))
    }
}

impl<F: Fn() -> bool + 'static> From<F> for WhenFn {
    fn from(f: F) -> Self {
        WhenFn(::std::rc::Rc::new(f))
    }
}

impl WhenFn {
    /// Invoke the wrapped predicate.
    pub fn call(&self) -> bool {
        (self.0)()
    }
}

/// The fallback branch of a `show`-style conditional. Wraps an
/// optional `Fn() -> Element` closure — `None` means "render
/// nothing on false"; `Some(closure)` is what the user typed as
/// `fallback: || …`.
///
/// `From<F: Fn() -> Element + 'static>` lets a closure literal flow
/// through typed-builder's `Into<Fallback>` path; `Default` (used via
/// `#[prop(default)]`) is `None`. The closure returns an `Element`
/// rather than `Children`'s `View` because the typical fallback is a
/// single component invocation like `|| render! { status_banner(...) }`.
#[derive(Clone, Default)]
pub struct Fallback(pub Option<::std::rc::Rc<dyn ::std::ops::Fn() -> Element + 'static>>);

impl<F: Fn() -> Element + 'static> From<F> for Fallback {
    fn from(f: F) -> Self {
        Fallback(Some(::std::rc::Rc::new(f)))
    }
}

/// A rendered (or about-to-be-rendered) tree fragment.
#[derive(Debug, Clone)]
pub enum View {
    /// A single element handle the caller has already created.
    Element(Element),
    /// A text snippet — `materialize` creates a `raw_text` element
    /// with `text=<value>`. The `IntoView` impls for `&str` /
    /// `String` / primitive numeric types route through here so
    /// `{count.get()}` inside `render!` can interpolate scalar
    /// values without the caller having to manually wrap them in a
    /// `raw_text { … }` element.
    Text(String),
    /// Zero-or-more child views in order. Tuples, option-some /
    /// option-none → empty, iterator flattening, and the macro's
    /// multi-child `Show` children all use this.
    Fragment(Vec<View>),
    /// A view with no on-screen footprint — `Show { when: false }`
    /// and `Option::None`.
    Empty,
}

impl View {
    /// Realise the view: create whatever element handles the text /
    /// fragment variants require, append them to `parent`, and return
    /// the resulting flat list of leaf handles in attach order. The
    /// returned list is what the `{expr}` macro path stashes so the
    /// next effect re-run can detach the previous children before
    /// attaching the new ones.
    pub fn attach_to(self, parent: Element) -> Vec<Element> {
        let mut out = Vec::new();
        self.materialise_into(parent, &mut out);
        out
    }

    fn materialise_into(self, parent: Element, out: &mut Vec<Element>) {
        match self {
            View::Element(h) => {
                super::append_child(parent, h);
                out.push(h);
            }
            View::Text(s) => {
                let h = super::create_element(crate::element::ElementTag::RawText);
                super::set_attribute(h, "text", &s);
                super::append_child(parent, h);
                out.push(h);
            }
            View::Fragment(children) => {
                for child in children {
                    child.materialise_into(parent, out);
                }
            }
            View::Empty => {}
        }
    }

    /// Collect the (already-realised) leaf element handles this view
    /// contributes, in child-order. **Only `Element` and `Fragment`
    /// contribute.** `Text` returns nothing here because its element
    /// only exists once `attach_to` has run.
    pub fn elements(&self) -> Vec<Element> {
        let mut out = Vec::new();
        self.collect_into(&mut out);
        out
    }

    fn collect_into(&self, out: &mut Vec<Element>) {
        match self {
            View::Element(h) => out.push(*h),
            View::Fragment(children) => {
                for c in children {
                    c.collect_into(out);
                }
            }
            View::Text(_) | View::Empty => {}
        }
    }
}

impl IntoView for View {
    fn into_view(self) -> View {
        self
    }
}

impl IntoView for Element {
    fn into_view(self) -> View {
        View::Element(self)
    }
}

impl IntoView for () {
    fn into_view(self) -> View {
        View::Empty
    }
}

impl<T: IntoView> IntoView for Option<T> {
    fn into_view(self) -> View {
        match self {
            Some(v) => v.into_view(),
            None => View::Empty,
        }
    }
}

// Text-shaped `IntoView` impls. These are what let a `{expr}` inside
// `render!` interpolate a scalar directly. No blanket
// `impl<T: Display>`: an explicit list keeps the surface predictable
// and the orphan rules tractable, and custom types implement
// `IntoView` themselves.

impl IntoView for String {
    fn into_view(self) -> View {
        View::Text(self)
    }
}

impl IntoView for &str {
    fn into_view(self) -> View {
        View::Text(self.to_owned())
    }
}

impl IntoView for &String {
    fn into_view(self) -> View {
        View::Text(self.clone())
    }
}

macro_rules! impl_into_view_via_display {
    ($($t:ty),+) => {
        $(
            impl IntoView for $t {
                fn into_view(self) -> View {
                    View::Text(self.to_string())
                }
            }
        )+
    };
}

impl_into_view_via_display!(i8, i16, i32, i64, i128, isize);
impl_into_view_via_display!(u8, u16, u32, u64, u128, usize);
impl_into_view_via_display!(f32, f64, bool, char);

// Tuple impls for 1–8 elements. Tuples render as fragments —
// children mount in declaration order.
macro_rules! impl_into_view_tuple {
    ($($name:ident),+) => {
        impl<$($name: IntoView),+> IntoView for ($($name,)+) {
            #[allow(non_snake_case)]
            fn into_view(self) -> View {
                let ($($name,)+) = self;
                View::Fragment(vec![$($name.into_view()),+])
            }
        }
    };
}

impl_into_view_tuple!(A);
impl_into_view_tuple!(A, B);
impl_into_view_tuple!(A, B, C);
impl_into_view_tuple!(A, B, C, D);
impl_into_view_tuple!(A, B, C, D, E);
impl_into_view_tuple!(A, B, C, D, E, F);
impl_into_view_tuple!(A, B, C, D, E, F, G);
impl_into_view_tuple!(A, B, C, D, E, F, G, H);