-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbuilder.rs
More file actions
264 lines (236 loc) · 8.97 KB
/
builder.rs
File metadata and controls
264 lines (236 loc) · 8.97 KB
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
use std::sync::atomic::Ordering;
use anathema_backend::Backend;
use anathema_default_widgets::register_default_widgets;
use anathema_geometry::Size;
use anathema_templates::{Document, Expression, ToSourceKind, Variables};
use anathema_value_resolver::{Function, FunctionTable};
use anathema_widgets::components::deferred::DeferredComponents;
use anathema_widgets::components::events::Event;
use anathema_widgets::components::{Component, ComponentId, ComponentRegistry, Emitter, ViewMessage};
use anathema_widgets::tabindex::TabIndex;
use anathema_widgets::{Factory, Widget};
use notify::{Event as NotifyEvent, RecommendedWatcher, RecursiveMode, Watcher, recommended_watcher};
use crate::REBUILD;
pub use crate::error::{Error, Result};
use crate::events::GlobalEventHandler;
use crate::runtime::{Runtime, show_error};
pub struct Builder<G> {
factory: Factory,
document: Document,
component_registry: ComponentRegistry,
emitter: Emitter,
message_receiver: flume::Receiver<ViewMessage>,
fps: u32,
size: Size,
global_event_handler: G,
hot_reload: bool,
function_table: FunctionTable,
variables: Variables,
}
impl<G: GlobalEventHandler> Builder<G> {
/// Create a new runtime builder with a receiver.
/// Use this if the `Emitter` was created outside of the runtime.
pub(super) fn with_receiver(
message_receiver: flume::Receiver<ViewMessage>,
emitter: Emitter,
document: Document,
size: Size,
global_event_handler: G,
) -> Self {
let mut factory = Factory::new();
register_default_widgets(&mut factory);
Self {
factory,
document,
component_registry: ComponentRegistry::new(),
emitter,
message_receiver,
fps: 30,
size,
global_event_handler,
hot_reload: true,
function_table: FunctionTable::new(),
variables: Variables::new(),
}
}
/// Create a new runtime builder
pub(super) fn new(document: Document, size: Size, global_event_handler: G) -> Self {
let (tx, rx) = flume::unbounded();
let emitter = Emitter::from(tx);
Self::with_receiver(rx, emitter.clone(), document, size, global_event_handler)
}
/// Enable/Disable hot reloading
pub fn hot_reload(&mut self, value: bool) {
self.hot_reload = value;
}
/// Register a new widget
pub fn register_widget<T: Widget + Default + 'static>(&mut self, ident: &'static str) {
self.factory.register_default::<T>(ident);
}
/// Set the expected frame rate
pub fn fps(&mut self, fps: u32) {
self.fps = fps;
}
/// Returns an [Emitter] to send messages to components
pub fn emitter(&self) -> Emitter {
self.emitter.clone()
}
/// Registers a component as a template-only component.
///
/// This component has no state or reacts to any events
pub fn template(&mut self, ident: impl Into<String>, template: impl ToSourceKind) -> Result<()> {
self.prototype(ident, template, || (), || ())
}
/// Registers a [Component] with the runtime.
/// This returns a unique [ComponentId] that is used to send messages to the component.
///
/// A component can only be used once in a template.
/// If you want multiple instances, register the component as a prototype instead,
/// see [RuntimeBuilder::prototype].
pub fn component<C: Component>(
&mut self,
ident: impl Into<String>,
template: impl ToSourceKind,
component: C,
state: C::State,
) -> Result<ComponentId<C::Message>> {
let id = self.document.add_component(ident, template.to_source_kind())?;
self.component_registry.add_component(id, component, state);
Ok(id.into())
}
/// Registers a [Component] with the runtime as long as the component and the associated state
/// implements the `Default` trait.
/// This returns a unique [ComponentId] that is used to send messages to the component.
pub fn default<C>(
&mut self,
ident: impl Into<String>,
template: impl ToSourceKind,
) -> Result<ComponentId<C::Message>>
where
C: Component + Default,
C::State: Default,
{
let component = C::default();
let state = C::State::default();
let id = self.document.add_component(ident, template.to_source_kind())?;
self.component_registry.add_component(id, component, state);
Ok(id.into())
}
/// Registers a [Component] as a prototype with the [Runtime],
/// which allows for multiple instances of the component to exist the templates.
pub fn prototype<FC, FS, C>(
&mut self,
ident: impl Into<String>,
template: impl ToSourceKind,
proto: FC,
state: FS,
) -> Result<()>
where
FC: 'static + Fn() -> C,
FS: 'static + FnMut() -> C::State,
C: Component + 'static,
{
let id = self.document.add_component(ident, template.to_source_kind())?;
self.component_registry.add_prototype(id, proto, state);
Ok(())
}
/// Assign a new event handler (make sure not to forget to add some mechanism to stop the
/// runtime)
pub fn with_global_event_handler<Eh>(self, global_event_handler: Eh) -> Builder<Eh>
where
Eh: Fn(Event, &mut TabIndex<'_, '_>, &mut DeferredComponents) -> Option<Event>,
{
Builder {
factory: self.factory,
document: self.document,
component_registry: self.component_registry,
emitter: self.emitter,
message_receiver: self.message_receiver,
fps: self.fps,
size: self.size,
global_event_handler,
hot_reload: self.hot_reload,
function_table: self.function_table,
variables: Variables::new(),
}
}
pub fn register_global(&mut self, key: impl Into<String>, value: impl Into<Expression>) -> Result<()> {
self.variables.define_global(key, value).map_err(|e| e.to_error(None))?;
Ok(())
}
pub fn finish<F, B>(mut self, backend: &mut B, mut f: F) -> Result<()>
where
F: FnMut(&mut Runtime<G>, &mut B) -> Result<()>,
B: Backend,
{
#[cfg(feature = "profile")]
let _puffin_server = {
let server_addr = format!("127.0.0.1:{}", puffin_http::DEFAULT_PORT);
let server = puffin_http::Server::new(&server_addr).unwrap();
puffin::set_scopes_on(true);
server
};
let blueprint = loop {
match self.document.compile(&mut self.variables) {
Ok(val) => break val,
// This can only show template errors.
// Widget errors doesn't become available until after the first tick.
Err(error) => match show_error(error.into(), backend, &self.document) {
Ok(()) => return Err(Error::Stop),
Err(Error::Reload) if self.hot_reload => {
_ = self.document.reload_templates();
}
err => err?,
},
}
};
let watcher = self.set_watcher(self.hot_reload)?;
let mut inst = Runtime::new(
blueprint,
self.variables,
self.component_registry,
self.document,
self.factory,
self.message_receiver,
self.emitter,
watcher,
self.size,
self.fps,
self.global_event_handler,
self.function_table,
self.hot_reload,
);
f(&mut inst, backend)
}
fn set_watcher(&mut self, hot_reload: bool) -> Result<Option<RecommendedWatcher>> {
if !hot_reload {
return Ok(None);
}
let paths = self
.document
.template_paths()
.filter_map(|p| p.canonicalize().ok())
.collect::<Vec<_>>();
let mut watcher = recommended_watcher(move |event: std::result::Result<NotifyEvent, _>| match event {
Ok(event) => match event.kind {
notify::EventKind::Create(_) | notify::EventKind::Remove(_) | notify::EventKind::Modify(_) => {
if paths.iter().any(|p| event.paths.contains(p)) {
REBUILD.store(true, Ordering::Relaxed);
}
}
notify::EventKind::Any | notify::EventKind::Access(_) | notify::EventKind::Other => (),
},
Err(_err) => (),
})?;
for path in self.document.template_paths() {
let path = path.canonicalize().unwrap();
if let Some(parent) = path.parent() {
watcher.watch(parent, RecursiveMode::NonRecursive)?;
}
}
Ok(Some(watcher))
}
pub fn register_function(&mut self, ident: impl Into<String>, f: impl Into<Function>) -> Result<()> {
Ok(self.function_table.insert(ident, f)?)
}
}