mirror of
https://github.com/slint-ui/slint.git
synced 2025-09-30 22:01:13 +00:00
C++ Api to run a functor from a thread
This commit is contained in:
parent
7293129fe1
commit
aabd320e83
5 changed files with 111 additions and 14 deletions
|
@ -9,6 +9,11 @@
|
|||
LICENSE END */
|
||||
/*! This scrates just expose the function used by the C++ integration */
|
||||
|
||||
use core::ffi::c_void;
|
||||
use sixtyfps_corelib::window::ffi::ComponentWindowOpaque;
|
||||
use sixtyfps_corelib::window::ComponentWindow;
|
||||
use sixtyfps_rendering_backend_default::backend;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[cold]
|
||||
pub fn use_modules() -> usize {
|
||||
|
@ -18,11 +23,6 @@ pub fn use_modules() -> usize {
|
|||
sixtyfps_corelib::use_modules()
|
||||
}
|
||||
|
||||
use sixtyfps_rendering_backend_default::backend;
|
||||
|
||||
use sixtyfps_corelib::window::ffi::ComponentWindowOpaque;
|
||||
use sixtyfps_corelib::window::ComponentWindow;
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sixtyfps_component_window_init(out: *mut ComponentWindowOpaque) {
|
||||
assert_eq!(
|
||||
|
@ -38,6 +38,33 @@ pub unsafe extern "C" fn sixtyfps_run_event_loop() {
|
|||
.run_event_loop(sixtyfps_corelib::backend::EventLoopQuitBehavior::QuitOnLastWindowClosed);
|
||||
}
|
||||
|
||||
/// Will execute the fiven functor in the main thread
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sixtyfps_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 };
|
||||
|
||||
crate::backend().post_event(Box::new(move || {
|
||||
let ud = &ud;
|
||||
event(ud.user_data);
|
||||
}));
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sixtyfps_quit_event_loop() {
|
||||
crate::backend().quit_event_loop();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue