summaryrefslogtreecommitdiffstats
path: root/web/src/element.rs
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/element.rs')
-rw-r--r--web/src/element.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/web/src/element.rs b/web/src/element.rs
new file mode 100644
index 00000000..8270d8db
--- /dev/null
+++ b/web/src/element.rs
@@ -0,0 +1,62 @@
+use crate::{Bus, Color, Widget};
+
+use dodrio::bumpalo;
+use std::rc::Rc;
+
+pub struct Element<'a, Message> {
+ pub(crate) widget: Box<dyn Widget<Message> + 'a>,
+}
+
+impl<'a, Message> Element<'a, Message> {
+ pub fn new(widget: impl Widget<Message> + 'a) -> Self {
+ Self {
+ widget: Box::new(widget),
+ }
+ }
+
+ pub fn explain(self, _color: Color) -> Element<'a, Message> {
+ self
+ }
+
+ 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)),
+ }
+ }
+}
+
+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>,
+ ) -> dodrio::Node<'b> {
+ self.widget.node(bump, &bus.map(self.mapper.clone()))
+ }
+}