-
Notifications
You must be signed in to change notification settings - Fork 69
/
render.rs
68 lines (58 loc) · 1.92 KB
/
render.rs
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
//! Built-in capability used to notify the Shell that a UI update is necessary.
use serde::{Deserialize, Serialize};
use crate::{
capability::{CapabilityContext, Operation},
Capability,
};
/// Use an instance of `Render` to notify the Shell that it should update the user
/// interface. This assumes a declarative UI framework is used in the Shell, which will
/// take the ViewModel provided by [`Core::view`](crate::Core::view) and reconcile the new UI state based
/// on the view model with the previous one.
///
/// For imperative UIs, the Shell will need to understand the difference between the two
/// view models and update the user interface accordingly.
pub struct Render<Ev> {
context: CapabilityContext<RenderOperation, Ev>,
}
impl<Ev> Clone for Render<Ev> {
fn clone(&self) -> Self {
Self {
context: self.context.clone(),
}
}
}
/// The single operation `Render` implements.
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct RenderOperation;
impl Operation for RenderOperation {
type Output = ();
}
/// Public API of the capability, called by App::update.
impl<Ev> Render<Ev>
where
Ev: 'static,
{
pub fn new(context: CapabilityContext<RenderOperation, Ev>) -> Self {
Self { context }
}
/// Call `render` from [`App::update`](crate::App::update) to signal to the Shell that
/// UI should be re-drawn.
pub fn render(&self) {
let ctx = self.context.clone();
self.context.spawn(async move {
ctx.notify_shell(RenderOperation).await;
});
}
}
impl<Ev> Capability<Ev> for Render<Ev> {
type Operation = RenderOperation;
type MappedSelf<MappedEv> = Render<MappedEv>;
fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static,
{
Render::new(self.context.map_event(f))
}
}