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
#![warn(missing_docs)]
use crate::accessibility::AccessibleStringProperty;
use crate::item_tree::{
ItemTreeNode, ItemVisitorVTable, ItemWeak, TraversalOrder, VisitChildrenResult,
};
use crate::items::{AccessibleRole, ItemVTable};
use crate::layout::{LayoutInfo, Orientation};
use crate::slice::Slice;
use crate::window::WindowAdapter;
use crate::SharedString;
use alloc::rc::Rc;
use vtable::*;
#[repr(C)]
pub struct IndexRange {
pub start: usize,
pub end: usize,
}
impl From<core::ops::Range<usize>> for IndexRange {
fn from(r: core::ops::Range<usize>) -> Self {
Self { start: r.start, end: r.end }
}
}
impl From<IndexRange> for core::ops::Range<usize> {
fn from(r: IndexRange) -> Self {
Self { start: r.start, end: r.end }
}
}
#[vtable]
#[repr(C)]
pub struct ComponentVTable {
pub visit_children_item: extern "C" fn(
core::pin::Pin<VRef<ComponentVTable>>,
index: isize,
order: TraversalOrder,
visitor: VRefMut<ItemVisitorVTable>,
) -> VisitChildrenResult,
pub get_item_ref: extern "C" fn(
core::pin::Pin<VRef<ComponentVTable>>,
index: usize,
) -> core::pin::Pin<VRef<ItemVTable>>,
pub get_subtree_range:
extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>, index: usize) -> IndexRange,
pub get_subtree_component: extern "C" fn(
core::pin::Pin<VRef<ComponentVTable>>,
index: usize,
subindex: usize,
result: &mut vtable::VWeak<ComponentVTable, Dyn>,
),
pub get_item_tree: extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>) -> Slice<ItemTreeNode>,
pub parent_node: extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>, result: &mut ItemWeak),
pub subtree_index: extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>) -> usize,
pub layout_info:
extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>, Orientation) -> LayoutInfo,
pub accessible_role:
extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>, item_index: usize) -> AccessibleRole,
pub accessible_string_property: extern "C" fn(
core::pin::Pin<VRef<ComponentVTable>>,
item_index: usize,
what: AccessibleStringProperty,
result: &mut SharedString,
),
pub drop_in_place: unsafe fn(VRefMut<ComponentVTable>) -> vtable::Layout,
pub dealloc: unsafe fn(&ComponentVTable, ptr: *mut u8, layout: vtable::Layout),
}
#[cfg(test)]
pub(crate) use ComponentVTable_static;
pub type ComponentRef<'a> = vtable::VRef<'a, ComponentVTable>;
pub type ComponentRefPin<'a> = core::pin::Pin<ComponentRef<'a>>;
pub type ComponentRc = vtable::VRc<ComponentVTable, Dyn>;
pub type ComponentWeak = vtable::VWeak<ComponentVTable, Dyn>;
pub fn register_component<Base>(
base: core::pin::Pin<&Base>,
item_array: &[vtable::VOffset<Base, ItemVTable, vtable::AllowPin>],
window_adapter: &Rc<dyn WindowAdapter>,
) {
item_array.iter().for_each(|item| item.apply_pin(base).as_ref().init(window_adapter));
window_adapter.register_component();
}
pub fn unregister_component<Base>(
base: core::pin::Pin<&Base>,
component: ComponentRef,
item_array: &[vtable::VOffset<Base, ItemVTable, vtable::AllowPin>],
window_adapter: &Rc<dyn WindowAdapter>,
) {
window_adapter.renderer().free_graphics_resources(
component,
&mut item_array.iter().map(|item| item.apply_pin(base)),
).expect("Fatal error encountered when freeing graphics resources while destroying Slint component");
window_adapter
.unregister_component(component, &mut item_array.iter().map(|item| item.apply_pin(base)));
}
#[cfg(feature = "ffi")]
pub(crate) mod ffi {
#![allow(unsafe_code)]
use crate::window::WindowAdapter;
use super::*;
#[no_mangle]
pub unsafe extern "C" fn slint_register_component(
component: ComponentRefPin,
item_array: Slice<vtable::VOffset<u8, ItemVTable, vtable::AllowPin>>,
window_handle: *const crate::window::ffi::WindowAdapterRcOpaque,
) {
let window_adapter = &*(window_handle as *const Rc<dyn WindowAdapter>);
super::register_component(
core::pin::Pin::new_unchecked(&*(component.as_ptr() as *const u8)),
item_array.as_slice(),
window_adapter,
)
}
#[no_mangle]
pub unsafe extern "C" fn slint_unregister_component(
component: ComponentRefPin,
item_array: Slice<vtable::VOffset<u8, ItemVTable, vtable::AllowPin>>,
window_handle: *const crate::window::ffi::WindowAdapterRcOpaque,
) {
let window_adapter = &*(window_handle as *const Rc<dyn WindowAdapter>);
super::unregister_component(
core::pin::Pin::new_unchecked(&*(component.as_ptr() as *const u8)),
core::pin::Pin::into_inner(component),
item_array.as_slice(),
window_adapter,
)
}
}