forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
107 lines (95 loc) · 3.09 KB
/
lib.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
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
/*! This crate just expose the function used by the C++ integration */
use core::ffi::c_void;
use i_slint_core::window::{ffi::WindowAdapterRcOpaque, WindowAdapter};
use std::rc::Rc;
#[cfg(feature = "experimental")]
pub mod platform;
#[doc(hidden)]
#[cold]
pub fn use_modules() -> usize {
#[cfg(feature = "slint-interpreter")]
slint_interpreter::use_modules();
i_slint_backend_selector::use_modules();
i_slint_core::use_modules()
}
#[no_mangle]
pub unsafe extern "C" fn slint_windowrc_init(out: *mut WindowAdapterRcOpaque) {
assert_eq!(
core::mem::size_of::<Rc<dyn WindowAdapter>>(),
core::mem::size_of::<WindowAdapterRcOpaque>()
);
let win = i_slint_backend_selector::with_platform(|b| b.create_window_adapter()).unwrap();
core::ptr::write(out as *mut Rc<dyn WindowAdapter>, win);
}
#[no_mangle]
pub unsafe extern "C" fn slint_run_event_loop() {
i_slint_backend_selector::with_platform(|b| b.run_event_loop()).unwrap();
}
/// Will execute the given functor in the main thread
#[no_mangle]
pub unsafe extern "C" fn slint_post_event(
event: extern "C" fn(user_data: *mut c_void),
user_data: *mut c_void,
drop_user_data: Option<extern "C" fn(*mut c_void)>,
) {
struct UserData {
user_data: *mut c_void,
drop_user_data: Option<extern "C" fn(*mut c_void)>,
}
impl Drop for UserData {
fn drop(&mut self) {
if let Some(x) = self.drop_user_data {
x(self.user_data)
}
}
}
unsafe impl Send for UserData {}
let ud = UserData { user_data, drop_user_data };
i_slint_core::api::invoke_from_event_loop(move || {
let ud = &ud;
event(ud.user_data);
})
.unwrap();
}
#[no_mangle]
pub unsafe extern "C" fn slint_quit_event_loop() {
i_slint_core::api::quit_event_loop().unwrap();
}
#[no_mangle]
pub unsafe extern "C" fn slint_register_font_from_path(
win: *const WindowAdapterRcOpaque,
path: &i_slint_core::SharedString,
error_str: *mut i_slint_core::SharedString,
) {
let window_adapter = &*(win as *const Rc<dyn WindowAdapter>);
core::ptr::write(
error_str,
match window_adapter.renderer().register_font_from_path(std::path::Path::new(path.as_str()))
{
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},
)
}
#[no_mangle]
pub unsafe extern "C" fn slint_register_font_from_data(
win: *const WindowAdapterRcOpaque,
data: i_slint_core::slice::Slice<'static, u8>,
error_str: *mut i_slint_core::SharedString,
) {
let window_adapter = &*(win as *const Rc<dyn WindowAdapter>);
core::ptr::write(
error_str,
match window_adapter.renderer().register_font_from_memory(data.as_slice()) {
Ok(()) => Default::default(),
Err(err) => err.to_string().into(),
},
)
}
#[cfg(feature = "testing")]
#[no_mangle]
pub unsafe extern "C" fn slint_testing_init_backend() {
i_slint_backend_testing::init();
}