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
mod dynamic_component;
mod dynamic_type;
mod eval;
mod value_model;
pub use eval::{ModelPtr, Value};
use dynamic_component::InstanceRef;
pub use sixtyfps_compilerlib::CompilerConfiguration;
use sixtyfps_corelib::component::{ComponentRef, ComponentRefPin};
use std::{collections::HashMap, pin::Pin, rc::Rc};
impl<'id> dynamic_component::ComponentDescription<'id> {
pub fn id(&self) -> &str {
self.original.id.as_str()
}
pub fn properties(&self) -> HashMap<String, sixtyfps_compilerlib::typeregister::Type> {
self.original
.root_element
.borrow()
.property_declarations
.iter()
.map(|(s, v)| (s.clone(), v.property_type.clone()))
.collect()
}
pub fn create(
self: Rc<Self>,
#[cfg(target_arch = "wasm32")] canvas_id: String,
) -> dynamic_component::ComponentBox<'id> {
dynamic_component::instantiate(
self,
None,
#[cfg(target_arch = "wasm32")]
canvas_id,
)
}
pub fn set_property(
&self,
component: ComponentRefPin,
name: &str,
value: Value,
) -> Result<(), ()> {
if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
return Err(());
}
generativity::make_guard!(guard);
let c = unsafe { InstanceRef::from_pin_ref(component, guard) };
if let Some(alias) = self
.original
.root_element
.borrow()
.property_declarations
.get(name)
.and_then(|d| d.is_alias.as_ref())
{
eval::store_property(c, &alias.element.upgrade().unwrap(), &alias.name, value)
} else {
eval::store_property(c, &self.original.root_element, name, value)
}
}
pub fn set_binding(
&self,
component: ComponentRef,
name: &str,
binding: Box<dyn Fn() -> Value>,
) -> Result<(), ()> {
if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
return Err(());
}
let x = self.custom_properties.get(name).ok_or(())?;
unsafe {
x.prop
.set_binding(Pin::new_unchecked(&*component.as_ptr().add(x.offset)), binding, None)
.unwrap()
};
Ok(())
}
pub fn get_property(&self, component: ComponentRefPin, name: &str) -> Result<Value, ()> {
if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
return Err(());
}
generativity::make_guard!(guard);
let c = unsafe { InstanceRef::from_pin_ref(component, guard) };
if let Some(alias) = self
.original
.root_element
.borrow()
.property_declarations
.get(name)
.and_then(|d| d.is_alias.as_ref())
{
eval::load_property(c, &alias.element.upgrade().unwrap(), &alias.name)
} else {
eval::load_property(c, &self.original.root_element, name)
}
}
pub fn set_signal_handler(
&self,
component: Pin<ComponentRef>,
name: &str,
handler: Box<dyn Fn(&[Value])>,
) -> Result<(), ()> {
if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
return Err(());
}
let x = self.custom_signals.get(name).ok_or(())?;
let sig = x.apply(unsafe { &*(component.as_ptr() as *const dynamic_type::Instance) });
sig.set_handler(handler);
Ok(())
}
pub fn emit_signal(
&self,
component: ComponentRefPin,
name: &str,
args: &[Value],
) -> Result<(), ()> {
if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
return Err(());
}
let x = self.custom_signals.get(name).ok_or(())?;
let sig = x.apply(unsafe { &*(component.as_ptr() as *const dynamic_type::Instance) });
sig.emit(args);
Ok(())
}
}
pub type ComponentDescription = dynamic_component::ComponentDescription<'static>;
pub type ComponentBox = dynamic_component::ComponentBox<'static>;
pub fn load(
source: String,
path: &std::path::Path,
mut compiler_config: CompilerConfiguration,
) -> (Result<Rc<ComponentDescription>, ()>, sixtyfps_compilerlib::diagnostics::BuildDiagnostics) {
if compiler_config.style.is_none() && std::env::var("SIXTYFPS_STYLE").is_err() {
compiler_config.style = Some(if sixtyfps_rendering_backend_default::HAS_NATIVE_STYLE {
"native"
} else {
"ugly"
});
}
dynamic_component::load(source, path, &compiler_config, unsafe {
generativity::Guard::new(generativity::Id::new())
})
}