]> uap-core.de Git - note.git/commitdiff
add init functions for UiInteger, UiString and UiText main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 16 Apr 2026 06:52:32 +0000 (08:52 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Thu, 16 Apr 2026 06:52:32 +0000 (08:52 +0200)
ui-rs/src/ui/ffi.rs
ui-rs/src/ui/toolkit.rs

index c75f4a7bb4efb0575fd80dd0980c6a05ca98f579..75aa1284d13a4514eba8bc6ffadaad264aab2a8b 100644 (file)
@@ -47,6 +47,11 @@ pub struct UiList {
     _private: [u8; 0],
 }
 
+#[repr(C)]
+pub struct UiGeneric {
+    _private: [u8; 0],
+}
+
 #[repr(C)]
 pub struct UiListSelection {
     _private: [u8; 0],
index d550975610131614ce66f6763089d87a1f4a628a..01c3760cf9b03b0a135e0935921ff6bd52f962e1 100644 (file)
@@ -91,57 +91,6 @@ pub struct UiList<T> {
     data: Box<Vec<T>>
 }
 
-impl<T> UiList<T> {
-    pub fn data(&mut self) -> &mut Vec<T> {
-        self.data.as_mut()
-    }
-
-    pub fn init(&mut self, ctx: &UiContext, name: Option<&str>) {
-        unsafe {
-            let c_string = name.map(|n| CString::new(n).unwrap());
-            let c_str = c_string.as_ref().map_or(std::ptr::null(), |s| s.as_ptr());
-            let data: *mut Vec<T> = &mut *self.data;
-            self.ptr = ui_list_new2(ctx.ptr, c_str, list_init::<T>, data as *mut c_void);
-        }
-    }
-
-    pub fn update(&mut self) {
-        unsafe {
-            ui_list_update(self.ptr);
-        }
-    }
-
-    pub fn update_row(&mut self, row: i32) {
-        unsafe {
-            ui_list_update_row(self.ptr, row);
-        }
-    }
-
-    pub fn selection(&mut self) -> Vec<i32> {
-        let mut sel = Vec::new();
-        unsafe {
-            let selection = ui_list_get_selection_allocated(self.ptr);
-            let count = ui_list_selection_get_count(selection) as usize;
-            if count > 0 {
-                let indices = ui_list_selection_get_rows(selection);
-                sel.set_len(count as usize);
-                for i in 0..count {
-                    sel.push(*indices.add(i) as i32);
-                }
-            }
-            ui_list_selection_free(selection);
-        }
-        sel
-    }
-
-    pub fn set_selection(&mut self, sel: &Vec<i32>) {
-        unsafe {
-            ui_list_set_selected_indices(self.ptr, sel.as_ptr(), sel.len() as c_int);
-        }
-    }
-}
-
-
 /* -------------------------------- Default implementation -------------------------------- */
 
 macro_rules! value_default_impl {
@@ -200,6 +149,14 @@ pub fn app_init(appname: &str) {
 }
 
 impl UiText {
+    pub fn init(&mut self, ctx: &UiContext, name: Option<&str>) {
+        let c_string = name.map(|n| CString::new(n).unwrap());
+        let c_str = c_string.as_ref().map_or(std::ptr::null(), |s| s.as_ptr());
+        unsafe {
+            self.ptr = ui_text_new(ctx.ptr, c_str);
+        }
+    }
+    
     pub fn get(&self) -> String {
         unsafe {
             let cstr = ui_text_get(self.ptr);
@@ -218,6 +175,13 @@ impl UiText {
 }
 
 impl UiString {
+    pub fn init(&mut self, ctx: &UiContext, name: Option<&str>) {
+        let c_string = name.map(|n| CString::new(n).unwrap());
+        let c_str = c_string.as_ref().map_or(std::ptr::null(), |s| s.as_ptr());
+        unsafe {
+            self.ptr = ui_string_new(ctx.ptr, c_str);
+        }
+    }
     pub fn get(&self) -> String {
         unsafe {
             let cstr = ui_string_get(self.ptr);
@@ -236,6 +200,14 @@ impl UiString {
 }
 
 impl UiInteger {
+    pub fn init(&mut self, ctx: &UiContext, name: Option<&str>) {
+        let c_string = name.map(|n| CString::new(n).unwrap());
+        let c_str = c_string.as_ref().map_or(std::ptr::null(), |s| s.as_ptr());
+        unsafe {
+            self.ptr = ui_int_new(ctx.ptr, c_str);
+        }
+    }
+    
     pub fn get(&self) -> i64 {
         unsafe {
             ui_int_get(self.ptr)
@@ -250,6 +222,67 @@ impl UiInteger {
 }
 
 
+impl<T> UiList<T> {
+    pub fn data(&mut self) -> &mut Vec<T> {
+        self.data.as_mut()
+    }
+
+    pub fn init(&mut self, ctx: &UiContext, name: Option<&str>) {
+        let c_string = name.map(|n| CString::new(n).unwrap());
+        let c_str = c_string.as_ref().map_or(std::ptr::null(), |s| s.as_ptr());
+        unsafe {
+            let data: *mut Vec<T> = &mut *self.data;
+            self.ptr = ui_list_new2(ctx.ptr, c_str, list_init::<T>, data as *mut c_void);
+        }
+    }
+
+    pub fn update(&mut self) {
+        unsafe {
+            ui_list_update(self.ptr);
+        }
+    }
+
+    pub fn update_row(&mut self, row: i32) {
+        unsafe {
+            ui_list_update_row(self.ptr, row);
+        }
+    }
+
+    pub fn selection(&mut self) -> Vec<i32> {
+        let mut sel = Vec::new();
+        unsafe {
+            let selection = ui_list_get_selection_allocated(self.ptr);
+            let count = ui_list_selection_get_count(selection) as usize;
+            if count > 0 {
+                let indices = ui_list_selection_get_rows(selection);
+                sel.set_len(count as usize);
+                for i in 0..count {
+                    sel.push(*indices.add(i) as i32);
+                }
+            }
+            ui_list_selection_free(selection);
+        }
+        sel
+    }
+
+    pub fn set_selection(&mut self, sel: &Vec<i32>) {
+        unsafe {
+            ui_list_set_selected_indices(self.ptr, sel.as_ptr(), sel.len() as c_int);
+        }
+    }
+}
+
+impl<T> Drop for UiList<T> {
+    fn drop(&mut self) {
+        unsafe {
+            // This does not free the UiList pointer, because it could still be in use by
+            // UI elements, but it will have no reference to any object managed by Rust
+            ui_list_class_set_data(self.ptr, std::ptr::null_mut());
+        }
+    }
+}
+
+
 /* -------------------------------- List -------------------------------- */
 
 /*
@@ -337,6 +370,12 @@ type UiListGetFunc   = extern "C" fn(*mut ffi::UiList, c_int) -> *mut c_void;
 type UiListCountFunc = extern "C" fn(*mut ffi::UiList) -> c_int;
 
 extern "C" {
+    fn ui_int_new(ctx: *mut ffi::UiContext, name: *const c_char) -> *mut ffi::UiInteger;
+    fn ui_double_new(ctx: *mut ffi::UiContext, name: *const c_char) -> *mut ffi::UiDouble;
+    fn ui_range_new(ctx: *mut ffi::UiContext, name: *const c_char) -> *mut ffi::UiRange;
+    fn ui_string_new(ctx: *mut ffi::UiContext, name: *const c_char) -> *mut ffi::UiString;
+    fn ui_text_new(ctx: *mut ffi::UiContext, name: *const c_char) -> *mut ffi::UiText;
+
     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);
     fn ui_list_class_set_next(list: *mut ffi::UiList, func: UiListNextFunc);