]> uap-core.de Git - note.git/commitdiff
add reference counting to UiObject main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 11 May 2026 19:38:40 +0000 (21:38 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 11 May 2026 19:38:40 +0000 (21:38 +0200)
ui-rs/src/ui/toolkit.rs
ui-rs/src/ui/window.rs

index 38ff894bfc970509421173d1a0502fc20bf350da..48e26f3edc692290450dd6c947371e09c0706ca7 100644 (file)
@@ -191,6 +191,26 @@ pub struct UiObject<T> {
     pub _data: PhantomData<T>
 }
 
+impl<T> Clone for UiObject<T> {
+    fn clone(&self) -> Self {
+        unsafe {
+            ui_object_ref(self.ptr);
+        }
+        UiObject {
+            ptr: self.ptr,
+            ctx: UiContext { ptr: self.ctx.ptr },
+            _data: PhantomData
+        }
+    }
+}
+
+impl<T> Drop for UiObject<T> {
+    fn drop(&mut self) {
+        unsafe {
+            ui_object_unref(self.ptr);
+        }
+    }
+}
 
 pub struct UiText {
     pub ptr: *mut ffi::UiText
@@ -765,6 +785,9 @@ type UiListGetFunc   = extern "C" fn(*const ffi::UiList, c_int) -> *mut c_void;
 type UiListCountFunc = extern "C" fn(*const ffi::UiList) -> c_int;
 
 extern "C" {
+    pub fn ui_object_ref(obj: *mut ffi::UiObject);
+    pub fn ui_object_unref(obj: *mut ffi::UiObject);
+
     fn ui_list_new(ctx: *mut ffi::UiContext, name: *const c_char) -> *mut ffi::UiList;
     fn ui_list_new2(ctx: *mut ffi::UiContext, name: *const c_char, init: UiListInitFunc, userdata: *mut c_void) -> *mut ffi::UiList;
     fn ui_list_class_set_first(list: *mut ffi::UiList, func: UiListFirstFunc);
index 47b42a2e11b71264afe25b7e9925495a099b6414..408e9c1e574b7088d88f99088d7ca9b0fda884f6 100644 (file)
@@ -32,7 +32,7 @@
 use std::ffi::{c_char, c_int, c_void};
 use std::ffi::CString;
 use std::marker::PhantomData;
-use crate::ui::{event, ffi, toolkit, AppContext, EventWrapper, NoAppData, UiActions, UiModel};
+use crate::ui::{event, ffi, toolkit, ui_object_ref, AppContext, EventWrapper, NoAppData, UiActions, UiModel};
 use crate::ui::ffi::{UiButtonArgs, UiCallback, UiContext, UiDestructor, UiDialogArgs, UiObject};
 use crate::ui::widget::widget_fn;
 
@@ -65,6 +65,11 @@ where F: FnOnce(&mut toolkit::UiObject<T>, &mut T) {
             WindowType::Simple => ui_simple_window(str.as_ptr())
         }
     };
+    // because we use additional reference counting in the Rust UiObject wrapper,
+    // we always increase the reference counter here
+    unsafe {
+        ui_object_ref(objptr);
+    }
 
     let ctxptr = unsafe {
         ui_object_get_context(objptr)
@@ -92,7 +97,7 @@ where F: FnOnce(&mut toolkit::UiObject<T>, &mut T) {
 
     // call ui building closure
     create_ui(&mut obj, wdata);
-
+    
     obj
 }