-
Notifications
You must be signed in to change notification settings - Fork 660
/
Copy pathglobal_component.rs
374 lines (337 loc) · 12.7 KB
/
global_component.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
use crate::api::Value;
use crate::dynamic_item_tree::{
ErasedItemTreeBox, ErasedItemTreeDescription, PopupMenuDescription,
};
use crate::SetPropertyError;
use core::cell::RefCell;
use core::pin::Pin;
use i_slint_compiler::langtype::ElementType;
use i_slint_compiler::namedreference::NamedReference;
use i_slint_compiler::object_tree::{Component, Document, PropertyDeclaration};
use i_slint_core::item_tree::ItemTreeVTable;
use i_slint_core::rtti;
use smol_str::SmolStr;
use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;
pub struct CompiledGlobalCollection {
/// compiled globals
pub compiled_globals: Vec<CompiledGlobal>,
/// Map of all exported global singletons and their index in the compiled_globals vector. The key
/// is the normalized name of the global.
pub exported_globals_by_name: BTreeMap<SmolStr, usize>,
#[cfg(feature = "internal-highlight")]
pub(crate) debug_hook_callback: RefCell<Option<crate::debug_hook::DebugHookCallback>>,
}
impl CompiledGlobalCollection {
pub fn compile(doc: &Document) -> Self {
let mut exported_globals_by_name = BTreeMap::new();
let compiled_globals = doc
.used_types
.borrow()
.globals
.iter()
.enumerate()
.map(|(index, component)| {
let mut global = generate(component);
if !component.exported_global_names.borrow().is_empty() {
global.extend_public_properties(
component.root_element.borrow().property_declarations.clone(),
);
exported_globals_by_name.extend(
component
.exported_global_names
.borrow()
.iter()
.map(|exported_name| (exported_name.name.clone(), index)),
)
}
global
})
.collect();
Self {
compiled_globals,
exported_globals_by_name,
#[cfg(feature = "internal-highlight")]
debug_hook_callback: RefCell::new(None),
}
}
}
#[derive(Clone)]
pub enum GlobalStorage {
Strong(Rc<RefCell<HashMap<String, Pin<Rc<dyn GlobalComponent>>>>>),
/// When the storage is held by another global
Weak(std::rc::Weak<RefCell<HashMap<String, Pin<Rc<dyn GlobalComponent>>>>>),
}
impl GlobalStorage {
pub fn get(&self, name: &str) -> Option<Pin<Rc<dyn GlobalComponent>>> {
match self {
GlobalStorage::Strong(storage) => storage.borrow().get(name).cloned(),
GlobalStorage::Weak(storage) => storage.upgrade().unwrap().borrow().get(name).cloned(),
}
}
}
impl Default for GlobalStorage {
fn default() -> Self {
GlobalStorage::Strong(Default::default())
}
}
pub enum CompiledGlobal {
Builtin {
name: SmolStr,
element: Rc<i_slint_compiler::langtype::BuiltinElement>,
// dummy needed for iterator accessor
public_properties: BTreeMap<SmolStr, PropertyDeclaration>,
/// keep the Component alive as it is boing referenced by `NamedReference`s
_original: Rc<Component>,
},
Component {
component: ErasedItemTreeDescription,
public_properties: BTreeMap<SmolStr, PropertyDeclaration>,
},
}
impl CompiledGlobal {
pub fn names(&self) -> Vec<SmolStr> {
match self {
CompiledGlobal::Builtin { name, .. } => vec![name.clone()],
CompiledGlobal::Component { component, .. } => {
generativity::make_guard!(guard);
let component = component.unerase(guard);
let mut names = component.original.global_aliases();
names.push(component.original.root_element.borrow().original_name());
names
}
}
}
pub fn visible_in_public_api(&self) -> bool {
match self {
CompiledGlobal::Builtin { .. } => false,
CompiledGlobal::Component { component, .. } => {
generativity::make_guard!(guard);
let component = component.unerase(guard);
let is_exported = !component.original.exported_global_names.borrow().is_empty();
is_exported
}
}
}
pub fn public_properties(&self) -> impl Iterator<Item = (&SmolStr, &PropertyDeclaration)> + '_ {
match self {
CompiledGlobal::Builtin { public_properties, .. } => public_properties.iter(),
CompiledGlobal::Component { public_properties, .. } => public_properties.iter(),
}
}
pub fn extend_public_properties(
&mut self,
iter: impl IntoIterator<Item = (SmolStr, PropertyDeclaration)>,
) {
match self {
CompiledGlobal::Builtin { public_properties, .. } => public_properties.extend(iter),
CompiledGlobal::Component { public_properties, .. } => public_properties.extend(iter),
}
}
}
pub trait GlobalComponent {
fn invoke_callback(
self: Pin<&Self>,
callback_name: &SmolStr,
args: &[Value],
) -> Result<Value, ()>;
fn set_callback_handler(
self: Pin<&Self>,
callback_name: &str,
handler: Box<dyn Fn(&[Value]) -> Value>,
) -> Result<(), ()>;
fn set_property(
self: Pin<&Self>,
prop_name: &str,
value: Value,
) -> Result<(), SetPropertyError>;
fn get_property(self: Pin<&Self>, prop_name: &str) -> Result<Value, ()>;
fn get_property_ptr(self: Pin<&Self>, prop_name: &SmolStr) -> *const ();
fn eval_function(self: Pin<&Self>, fn_name: &str, args: Vec<Value>) -> Result<Value, ()>;
}
/// Instantiate the global singleton and store it in `globals`
pub fn instantiate(
description: &CompiledGlobal,
globals: &mut GlobalStorage,
root: vtable::VWeak<ItemTreeVTable, ErasedItemTreeBox>,
) {
let GlobalStorage::Strong(ref mut globals) = globals else {
panic!("Global storage is not strong")
};
let instance = match description {
CompiledGlobal::Builtin { element, .. } => {
trait Helper {
fn instantiate(name: &str) -> Pin<Rc<dyn GlobalComponent>> {
panic!("Cannot find native global {name}")
}
}
impl Helper for () {}
impl<T: rtti::BuiltinGlobal + 'static, Next: Helper> Helper for (T, Next) {
fn instantiate(name: &str) -> Pin<Rc<dyn GlobalComponent>> {
if name == T::name() {
T::new()
} else {
Next::instantiate(name)
}
}
}
i_slint_backend_selector::NativeGlobals::instantiate(
element.native_class.class_name.as_ref(),
)
}
CompiledGlobal::Component { component, .. } => {
generativity::make_guard!(guard);
let description = component.unerase(guard);
let inst = crate::dynamic_item_tree::instantiate(
description.clone(),
None,
Some(root),
None,
GlobalStorage::Weak(Rc::downgrade(globals)),
);
inst.run_setup_code();
Rc::pin(GlobalComponentInstance(inst))
}
};
globals.borrow_mut().extend(
description
.names()
.iter()
.map(|name| (crate::normalize_identifier(name).to_string(), instance.clone())),
);
}
/// For the global components, we don't use the dynamic_type optimization,
/// and we don't try to optimize the property to their real type
pub struct GlobalComponentInstance(vtable::VRc<ItemTreeVTable, ErasedItemTreeBox>);
impl GlobalComponent for GlobalComponentInstance {
fn set_property(
self: Pin<&Self>,
prop_name: &str,
value: Value,
) -> Result<(), SetPropertyError> {
generativity::make_guard!(guard);
let comp = self.0.unerase(guard);
comp.description().set_property(comp.borrow(), prop_name, value)
}
fn get_property(self: Pin<&Self>, prop_name: &str) -> Result<Value, ()> {
generativity::make_guard!(guard);
let comp = self.0.unerase(guard);
comp.description().get_property(comp.borrow(), prop_name)
}
fn get_property_ptr(self: Pin<&Self>, prop_name: &SmolStr) -> *const () {
generativity::make_guard!(guard);
let comp = self.0.unerase(guard);
crate::dynamic_item_tree::get_property_ptr(
&NamedReference::new(&comp.description().original.root_element, prop_name.clone()),
comp.borrow_instance(),
)
}
fn invoke_callback(
self: Pin<&Self>,
callback_name: &SmolStr,
args: &[Value],
) -> Result<Value, ()> {
generativity::make_guard!(guard);
let comp = self.0.unerase(guard);
comp.description().invoke(comp.borrow(), callback_name, args)
}
fn set_callback_handler(
self: Pin<&Self>,
callback_name: &str,
handler: Box<dyn Fn(&[Value]) -> Value>,
) -> Result<(), ()> {
generativity::make_guard!(guard);
let comp = self.0.unerase(guard);
comp.description().set_callback_handler(comp.borrow(), callback_name, handler)
}
fn eval_function(self: Pin<&Self>, fn_name: &str, args: Vec<Value>) -> Result<Value, ()> {
generativity::make_guard!(guard);
let comp = self.0.unerase(guard);
let mut ctx =
crate::eval::EvalLocalContext::from_function_arguments(comp.borrow_instance(), args);
let result = crate::eval::eval_expression(
&comp
.description()
.original
.root_element
.borrow()
.bindings
.get(fn_name)
.ok_or(())?
.borrow()
.expression,
&mut ctx,
);
Ok(result)
}
}
impl<T: rtti::BuiltinItem + 'static> GlobalComponent for T {
fn set_property(
self: Pin<&Self>,
prop_name: &str,
value: Value,
) -> Result<(), SetPropertyError> {
let prop = Self::properties()
.into_iter()
.find(|(k, _)| *k == prop_name)
.ok_or(SetPropertyError::NoSuchProperty)?
.1;
prop.set(self, value, None).map_err(|()| SetPropertyError::WrongType)
}
fn get_property(self: Pin<&Self>, prop_name: &str) -> Result<Value, ()> {
let prop = Self::properties().into_iter().find(|(k, _)| *k == prop_name).ok_or(())?.1;
prop.get(self)
}
fn get_property_ptr(self: Pin<&Self>, prop_name: &SmolStr) -> *const () {
let prop: &dyn rtti::PropertyInfo<Self, Value> =
Self::properties().into_iter().find(|(k, _)| *k == prop_name).unwrap().1;
unsafe { (self.get_ref() as *const Self as *const u8).add(prop.offset()) as *const () }
}
fn invoke_callback(
self: Pin<&Self>,
callback_name: &SmolStr,
args: &[Value],
) -> Result<Value, ()> {
let cb = Self::callbacks().into_iter().find(|(k, _)| *k == callback_name).ok_or(())?.1;
cb.call(self, args)
}
fn set_callback_handler(
self: Pin<&Self>,
callback_name: &str,
handler: Box<dyn Fn(&[Value]) -> Value>,
) -> Result<(), ()> {
let cb = Self::callbacks().into_iter().find(|(k, _)| *k == callback_name).ok_or(())?.1;
cb.set_handler(self, handler)
}
fn eval_function(self: Pin<&Self>, _fn_name: &str, _args: Vec<Value>) -> Result<Value, ()> {
Err(())
}
}
fn generate(component: &Rc<Component>) -> CompiledGlobal {
debug_assert!(component.is_global());
match &component.root_element.borrow().base_type {
ElementType::Global => {
generativity::make_guard!(guard);
CompiledGlobal::Component {
component: crate::dynamic_item_tree::generate_item_tree(
component,
None,
PopupMenuDescription::Weak(Default::default()),
false,
guard,
)
.into(),
public_properties: Default::default(),
}
}
ElementType::Builtin(b) => CompiledGlobal::Builtin {
name: component.id.clone(),
element: b.clone(),
public_properties: Default::default(),
_original: component.clone(),
},
ElementType::Error | ElementType::Native(_) | ElementType::Component(_) => unreachable!(),
}
}