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
use crate::{Bus, Color, Css, Widget};

use dodrio::bumpalo;
use std::rc::Rc;

/// A generic [`Widget`].
///
/// It is useful to build composable user interfaces that do not leak
/// implementation details in their __view logic__.
///
/// If you have a [built-in widget], you should be able to use `Into<Element>`
/// to turn it into an [`Element`].
///
/// [built-in widget]: mod@crate::widget
#[allow(missing_debug_implementations)]
pub struct Element<'a, Message> {
    pub(crate) widget: Box<dyn Widget<Message> + 'a>,
}

impl<'a, Message> Element<'a, Message> {
    /// Create a new [`Element`] containing the given [`Widget`].
    pub fn new(widget: impl Widget<Message> + 'a) -> Self {
        Self {
            widget: Box::new(widget),
        }
    }

    /// Applies a transformation to the produced message of the [`Element`].
    ///
    /// This method is useful when you want to decouple different parts of your
    /// UI and make them __composable__.
    pub fn map<F, B>(self, f: F) -> Element<'a, B>
    where
        Message: 'static,
        B: 'static,
        F: 'static + Fn(Message) -> B,
    {
        Element {
            widget: Box::new(Map::new(self.widget, f)),
        }
    }

    /// Marks the [`Element`] as _to-be-explained_.
    pub fn explain(self, _color: Color) -> Element<'a, Message> {
        self
    }

    /// Produces a VDOM node for the [`Element`].
    pub fn node<'b>(
        &self,
        bump: &'b bumpalo::Bump,
        bus: &Bus<Message>,
        style_sheet: &mut Css<'b>,
    ) -> dodrio::Node<'b> {
        self.widget.node(bump, bus, style_sheet)
    }
}

struct Map<'a, A, B> {
    widget: Box<dyn Widget<A> + 'a>,
    mapper: Rc<Box<dyn Fn(A) -> B>>,
}

impl<'a, A, B> Map<'a, A, B> {
    pub fn new<F>(widget: Box<dyn Widget<A> + 'a>, mapper: F) -> Map<'a, A, B>
    where
        F: 'static + Fn(A) -> B,
    {
        Map {
            widget,
            mapper: Rc::new(Box::new(mapper)),
        }
    }
}

impl<'a, A, B> Widget<B> for Map<'a, A, B>
where
    A: 'static,
    B: 'static,
{
    fn node<'b>(
        &self,
        bump: &'b bumpalo::Bump,
        bus: &Bus<B>,
        style_sheet: &mut Css<'b>,
    ) -> dodrio::Node<'b> {
        self.widget
            .node(bump, &bus.map(self.mapper.clone()), style_sheet)
    }
}