slint_interpreter/
item_registry.rs1use crate::erased::ErasedItemRc;
11use crate::item_holder::make_item;
12use crate::prelude::*;
13use i_slint_core::items::*;
14use i_slint_core::rtti::BuiltinItem;
15
16pub type ItemFactory = fn() -> ErasedItemRc;
17
18pub struct ItemRegistry {
19 factories: HashMap<&'static str, ItemFactory>,
20}
21
22impl ItemRegistry {
23 pub fn factory(&self, class_name: &str) -> Option<ItemFactory> {
24 self.factories.get(class_name).copied()
25 }
26
27 pub fn global() -> &'static ItemRegistry {
28 static REGISTRY: once_cell::sync::OnceCell<ItemRegistry> = once_cell::sync::OnceCell::new();
29 REGISTRY.get_or_init(ItemRegistry::build)
30 }
31
32 fn build() -> Self {
33 let mut factories: HashMap<&'static str, ItemFactory> = HashMap::new();
34
35 macro_rules! reg {
36 ($($ty:ident),* $(,)?) => {
37 $(
38 factories.insert(<$ty as BuiltinItem>::name(), make_item::<$ty> as ItemFactory);
39 )*
40 };
41 }
42
43 reg!(
44 ComponentContainer,
45 Empty,
46 ImageItem,
47 ClippedImage,
48 ComplexText,
49 StyledTextItem,
50 SimpleText,
51 Rectangle,
52 BasicBorderRectangle,
53 BorderRectangle,
54 TouchArea,
55 TooltipArea,
56 FocusScope,
57 KeyBinding,
58 SwipeGestureHandler,
59 ScaleRotateGestureHandler,
60 Path,
61 Flickable,
62 WindowItem,
63 TextInput,
64 Clip,
65 BoxShadow,
66 BackdropBlur,
67 Transform,
68 Opacity,
69 Layer,
70 DragArea,
71 DropArea,
72 WindowMoveArea,
73 ContextMenu,
74 MenuItem,
75 SystemTrayIcon,
76 );
77
78 <i_slint_backend_selector::NativeWidgets as NativeHelper>::push(&mut factories);
80
81 ItemRegistry { factories }
82 }
83}
84
85trait NativeHelper {
87 fn push(factories: &mut HashMap<&'static str, ItemFactory>);
88}
89
90impl NativeHelper for () {
91 fn push(_: &mut HashMap<&'static str, ItemFactory>) {}
92}
93
94impl<T: 'static + Default + BuiltinItem + vtable::HasStaticVTable<ItemVTable>, Next: NativeHelper>
95 NativeHelper for (T, Next)
96{
97 fn push(factories: &mut HashMap<&'static str, ItemFactory>) {
98 factories.insert(<T as BuiltinItem>::name(), make_item::<T> as ItemFactory);
99 Next::push(factories);
100 }
101}