i_slint_core/
callbacks.rs1#![warn(missing_docs)]
12
13use alloc::boxed::Box;
14use core::cell::Cell;
15
16#[repr(C)]
21pub struct Callback<Arg: ?Sized, Ret = ()> {
22 handler: Cell<Option<Box<dyn FnMut(&Arg, &mut Ret)>>>,
24}
25
26impl<Arg: ?Sized, Ret> Default for Callback<Arg, Ret> {
27 fn default() -> Self {
28 Self { handler: Default::default() }
29 }
30}
31
32impl<Arg: ?Sized, Ret: Default> Callback<Arg, Ret> {
33 pub fn call(&self, a: &Arg) -> Ret {
35 let mut r = Ret::default();
36 if let Some(mut h) = self.handler.take() {
37 h(a, &mut r);
38 assert!(self.handler.take().is_none(), "Callback Handler set while called");
39 self.handler.set(Some(h));
40 }
41 r
42 }
43
44 pub fn has_handler(&self) -> bool {
46 let handler = self.handler.take();
47 let result = handler.is_some();
48 self.handler.set(handler);
49 result
50 }
51
52 pub fn set_handler(&self, mut f: impl FnMut(&Arg) -> Ret + 'static) {
56 self.handler.set(Some(Box::new(move |a: &Arg, r: &mut Ret| *r = f(a))));
57 }
58}
59
60#[test]
61fn callback_simple_test() {
62 use std::rc::Rc;
63 #[derive(Default)]
64 struct Component {
65 pressed: core::cell::Cell<bool>,
66 clicked: Callback<()>,
67 }
68 let c = Rc::new(Component::default());
69 let weak = Rc::downgrade(&c);
70 c.clicked.set_handler(move |()| weak.upgrade().unwrap().pressed.set(true));
71 c.clicked.call(&());
72 assert!(c.pressed.get());
73}
74
75#[cfg(feature = "ffi")]
76pub(crate) mod ffi {
77 #![allow(unsafe_code)]
78
79 use super::*;
80
81 #[allow(non_camel_case_types)]
82 type c_void = ();
83 #[repr(C)]
84 pub struct CallbackOpaque(*const c_void, *const c_void);
86
87 static_assertions::assert_eq_align!(CallbackOpaque, Callback<()>);
88 static_assertions::assert_eq_size!(CallbackOpaque, Callback<()>);
89 static_assertions::assert_eq_align!(CallbackOpaque, Callback<(alloc::string::String,)>);
90 static_assertions::assert_eq_size!(CallbackOpaque, Callback<(alloc::string::String,)>);
91
92 #[unsafe(no_mangle)]
95 pub unsafe extern "C" fn slint_callback_init(out: *mut CallbackOpaque) {
96 assert_eq!(core::mem::size_of::<CallbackOpaque>(), core::mem::size_of::<Callback<()>>());
97 unsafe { core::ptr::write(out as *mut Callback<()>, Default::default()) };
98 }
99
100 #[unsafe(no_mangle)]
102 pub unsafe extern "C" fn slint_callback_call(
103 sig: *const CallbackOpaque,
104 arg: *const c_void,
105 ret: *mut c_void,
106 ) {
107 unsafe {
108 let sig = &*(sig as *const Callback<c_void>);
109 if let Some(mut h) = sig.handler.take() {
110 h(&*arg, &mut *ret);
111 assert!(sig.handler.take().is_none(), "Callback Handler set while called");
112 sig.handler.set(Some(h));
113 }
114 }
115 }
116
117 #[unsafe(no_mangle)]
121 pub unsafe extern "C" fn slint_callback_set_handler(
122 sig: *const CallbackOpaque,
123 binding: extern "C" fn(user_data: *mut c_void, arg: *const c_void, ret: *mut c_void),
124 user_data: *mut c_void,
125 drop_user_data: Option<extern "C" fn(*mut c_void)>,
126 ) {
127 unsafe {
128 let sig = &mut *(sig as *mut Callback<c_void>);
129
130 struct UserData {
131 user_data: *mut c_void,
132 drop_user_data: Option<extern "C" fn(*mut c_void)>,
133 binding:
134 extern "C" fn(user_data: *mut c_void, arg: *const c_void, ret: *mut c_void),
135 }
136
137 impl Drop for UserData {
138 fn drop(&mut self) {
139 if let Some(x) = self.drop_user_data {
140 x(self.user_data)
141 }
142 }
143 }
144
145 impl UserData {
146 unsafe fn call(&self, arg: *const c_void, ret: *mut c_void) {
148 (self.binding)(self.user_data, arg, ret)
149 }
150 }
151
152 let ud = UserData { user_data, drop_user_data, binding };
153 sig.handler.set(Some(Box::new(move |a: &(), r: &mut ()| {
154 ud.call(a as *const c_void, r as *mut c_void)
155 })));
156 }
157 }
158
159 #[unsafe(no_mangle)]
161 pub unsafe extern "C" fn slint_callback_drop(handle: *mut CallbackOpaque) {
162 unsafe { core::ptr::drop_in_place(handle as *mut Callback<()>) };
163 }
164}