]> uap-core.de Git - note.git/commitdiff
add UiContext onattach/ondetach callbacks main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 13 Jun 2026 15:32:24 +0000 (17:32 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 13 Jun 2026 15:32:24 +0000 (17:32 +0200)
ui-rs/src/ui/event.rs
ui-rs/src/ui/toolkit.rs

index a691e9ac5698713a958332901deff8736a1fc540..2610d9546144c16513f701936743c7891454364d 100644 (file)
@@ -331,6 +331,17 @@ pub extern "C" fn untyped_event_wrapper(e: *const ffi::UiEvent, data: *mut c_voi
 }
 
 
+pub struct SimpleEventWrapper {
+    pub callback: Box<dyn FnMut()>
+}
+
+pub extern "C" fn simple_event_wrapper(_e: *const ffi::UiEvent, data: *mut c_void) {
+    unsafe {
+        let wrapper = &mut *(data as *mut SimpleEventWrapper);
+        (wrapper.callback)();
+    }
+}
+
 pub struct SubListEvent {
     /// SubList index
     pub sublist_index: usize,
index 5fc3452a5f70546c43eeaedfdc9de20a2ff39db1..1bae83018a27ec2b02e4194470b7869062d84560 100644 (file)
 
 use std::any::Any;
 use std::ffi::{c_char, c_int, c_void, CStr, CString};
-use crate::ui::{action_event_wrapper, event, ffi, ui_object_get_context, ui_object_get_windowdata, ui_reg_destructor, ui_remove_destructor, untyped_event_wrapper, ActionEventWrapper, Event, EventWrapper, SubList, UntypedEventWrapper, RUST_TYPED_OBJECT_BOX_ANY};
+use crate::ui::{action_event_wrapper, event, ffi, simple_event_wrapper, ui_object_get_context, ui_object_get_windowdata, ui_reg_destructor, ui_remove_destructor, untyped_event_wrapper, ActionEventWrapper, Event, EventWrapper, SimpleEventWrapper, SubList, UntypedEventWrapper, RUST_TYPED_OBJECT_BOX_ANY};
 
 use std::marker::PhantomData;
 use std::mem;
 use std::ptr::null_mut;
 use std::slice::{Iter, IterMut};
 use std::sync::{Arc, Mutex};
-use crate::ui::ffi::{UiCallback, UiThreadFunc};
+use crate::ui::ffi::{UiCallback, UiEvent, UiThreadFunc};
 
 pub struct UiContext {
     pub ptr: *mut ffi::UiContext
@@ -86,6 +86,38 @@ impl UiContext {
         }
     }
 
+    pub fn onattach<F>(&self, f: F)
+    where F: FnMut() + 'static {
+        let wrapper = Box::new(SimpleEventWrapper { callback: Box::new(f) });
+        let ptr = self.reg_box(wrapper);
+        unsafe {
+            ui_onattach(self.ptr, Some(simple_event_wrapper), ptr as *mut c_void);
+        }
+    }
+
+    pub fn ondetach<F>(&self, f: F)
+    where F: FnMut() + 'static {
+        let wrapper = Box::new(SimpleEventWrapper { callback: Box::new(f) });
+        let ptr = self.reg_box(wrapper);
+        unsafe {
+            ui_ondetach(self.ptr, Some(simple_event_wrapper), ptr as *mut c_void);
+        }
+    }
+
+    pub fn onattach_action(&self, action: &str) {
+        let cstr = CString::new(action).unwrap();
+        unsafe {
+            ui_onattach_action(self.ptr, cstr.as_ptr());
+        }
+    }
+
+    pub fn ondetach_action(&self, action: &str) {
+        let cstr = CString::new(action).unwrap();
+        unsafe {
+            ui_ondetach_action(self.ptr, cstr.as_ptr());
+        }
+    }
+
     pub fn list<T>(&self) -> UiList<T> {
         let mut ls = UiList::<T>::default();
         ls.init(self, None);
@@ -1254,6 +1286,10 @@ extern "C" {
     fn ui_document_context(doc: *mut c_void) -> *mut ffi::UiContext;
     fn ui_attach_document(ctx: *mut ffi::UiContext, doc: *mut c_void);
     fn ui_detach_document(ctx: *mut ffi::UiContext, doc: *mut c_void);
+    fn ui_onattach(ctx: *mut ffi::UiContext, callback: UiCallback, data: *mut c_void);
+    fn ui_ondetach(ctx: *mut ffi::UiContext, callback: UiCallback, data: *mut c_void);
+    fn ui_onattach_action(ctx: *mut ffi::UiContext, action: *const c_char);
+    fn ui_ondetach_action(ctx: *mut ffi::UiContext, action: *const c_char);
     fn ui_context_document(ctx: *mut ffi::UiContext) -> *mut c_void;
     fn ui_context_obj(ctx: *mut ffi::UiContext) -> *mut ffi::UiObject;