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
use std::any::Any;
use std::collections::hash_map::DefaultHasher;
use std::future::Future;
use std::hash::{Hash, Hasher};
use futures::Stream;
use crate::node::component_node::{Runtime, State};
use crate::node::Node;
use crate::style::builder::StyleBuilder;
use crate::widget::Context as WidgetContext;
pub trait Component {
type State: 'static + Any + Send + Sync;
type Message: 'static;
type Output: 'static;
fn mount(&self) -> Self::State;
fn view<'a>(&'a self, state: &'a Self::State) -> Node<'a, Self::Message>;
fn update(
&self,
_message: Self::Message,
_state: State<Self::State>,
_context: Context<Self::Message, Self::Output>,
) {
}
fn style() -> StyleBuilder {
StyleBuilder::default()
}
fn into_node<'a>(self) -> Node<'a, Self::Output>
where
Self: 'a + Sized,
{
Node::from_component(self)
}
fn class<'a>(self, class: &'a str) -> Node<'a, Self::Output>
where
Self: 'a + Sized,
{
let mut node = self.into_node();
node.set_class(class);
node
}
fn key<'a, K>(self, key: K) -> Node<'a, Self::Output>
where
Self: 'a + Sized,
K: Hash,
{
let mut hasher = DefaultHasher::new();
key.hash(&mut hasher);
let mut node = self.into_node();
node.set_key(hasher.finish());
node
}
}
pub struct Context<'a, Message, Output> {
widget_context: &'a mut WidgetContext<Output>,
runtime: &'a mut Runtime<Message>,
}
impl<'a, Message, Output> Context<'a, Message, Output> {
pub(crate) fn new(widget_context: &'a mut WidgetContext<Output>, runtime: &'a mut Runtime<Message>) -> Self {
Self {
widget_context,
runtime,
}
}
pub fn push(&mut self, message: Output) {
self.widget_context.push(message);
}
pub fn extend<I: IntoIterator<Item = Output>>(&mut self, iter: I) {
self.widget_context.extend(iter);
}
pub fn cursor(&self) -> (f32, f32) {
self.widget_context.cursor()
}
pub fn wait<F: 'static + Future<Output = Message> + Send + Sync + Unpin>(&mut self, fut: F) {
self.runtime.wait(fut);
}
pub fn stream<S: 'static + Stream<Item = Message> + Send + Sync + Unpin>(&mut self, stream: S) {
self.runtime.stream(stream);
}
}