From 06484e790f24c5346c94236a84a6b79bd8caddd4 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Fri, 22 May 2026 17:58:15 +0200 Subject: [PATCH] add UiDocRef --- application/src/notebook.rs | 16 +++++++- application/src/window.rs | 8 ++-- ui-rs/src/ui/toolkit.rs | 76 ++++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/application/src/notebook.rs b/application/src/notebook.rs index a601006..7386fe9 100644 --- a/application/src/notebook.rs +++ b/application/src/notebook.rs @@ -5,13 +5,25 @@ use entity::note::Model as Note; #[derive(UiModel, Default)] pub struct Notebook { + pub collection_id: i32, + #[bind] pub notes: UiList } #[ui_actions] impl Notebook { - pub fn new() -> Self { - Default::default() + pub fn new(id: i32) -> Self { + Notebook { + collection_id: id, ..Default::default() + } } } + +pub fn note_getvalue<'a>(elm: &Note, col: i32, _row: i32) -> ListValue<'a> { + match col { + 0 => ListValue::String(elm.title.clone()), + 1 => ListValue::String(elm.lastmodified.to_string()), + _ => ListValue::None + } +} \ No newline at end of file diff --git a/application/src/window.rs b/application/src/window.rs index 84d07ef..b7a2cbf 100644 --- a/application/src/window.rs +++ b/application/src/window.rs @@ -30,9 +30,10 @@ use ui_rs::{action, ui_actions, UiModel}; use ui_rs::ui::*; use crate::App; use entity::collection::{Model as Collection, Node}; +use entity::note::Model as Note; use crate::backend::{BackendHandle, BroadcastMessage}; use crate::newnotebook::new_notebook_dialog; -use crate::notebook::Notebook; +use crate::notebook::{note_getvalue, Notebook}; #[derive(UiModel)] pub struct MainWindow { @@ -131,7 +132,7 @@ pub fn create_window(app: &App, ctx: &AppContext) -> UiObject) -> UiObject() + obj.tableview_builder::() .varname("notes") .fill(true) + .getvalue(note_getvalue) .create(); }); diff --git a/ui-rs/src/ui/toolkit.rs b/ui-rs/src/ui/toolkit.rs index e615080..4ca8ca9 100644 --- a/ui-rs/src/ui/toolkit.rs +++ b/ui-rs/src/ui/toolkit.rs @@ -177,6 +177,14 @@ impl UiDoc { } } + pub fn doc_ref(&self) -> UiDocRef { + UiDocRef::from_ptr(self.ptr) + } + + pub fn doc_proxy(&self) -> UiDocProxy { + UiDocProxy::from_ptr(self.ptr) + } + pub unsafe fn get_data_ptr(&self) -> *mut T { let ptr: *mut *mut T = self.ptr.cast(); *ptr @@ -330,7 +338,6 @@ impl Default for UiObjRef { } } - pub struct UiObjProxy { ptr: *mut ffi::UiObject, _data: PhantomData @@ -368,6 +375,73 @@ impl Drop for UiObjProxy { } } + + + +pub struct UiDocRef { + ptr: Box<*mut c_void>, + _data: PhantomData +} + +impl UiDocRef { + fn from_ptr(doc_ptr: *mut c_void) -> UiDocRef { + let obj_ref = UiDocRef { ptr: Box::new(doc_ptr), _data: PhantomData }; + if !doc_ptr.is_null() { + let ctx = unsafe { ui_document_context(doc_ptr) }; + unsafe { + let xptr = &*obj_ref.ptr as *const *mut c_void; + ui_reg_destructor(ctx, xptr as *mut c_void, ref_obj_destroyed); + } + } + obj_ref + } + + pub fn get_doc(&self) -> Option> { + if self.ptr.is_null() { + None + } else { + Some(UiDoc::from_ptr(*self.ptr)) + } + } +} + +impl Drop for UiDocRef { + fn drop(&mut self) { + unsafe { + let doc_ptr = *self.ptr; + + // only unregister if the document still exists + if !doc_ptr.is_null() { + let ctx = ui_document_context(doc_ptr); + let xptr = &mut *self.ptr as *mut *mut c_void; + ui_remove_destructor(ctx, xptr as *mut c_void); + } + } + } +} + +extern "C" fn ref_doc_destroyed(data: *mut c_void) { + unsafe { + let ptr_ptr = data as *mut *mut c_void; + + if !ptr_ptr.is_null() { + *ptr_ptr = std::ptr::null_mut(); + } + } +} + +impl Clone for UiDocRef { + fn clone(&self) -> Self { + UiDocRef::from_ptr(*self.ptr) + } +} + +impl Default for UiDocRef { + fn default() -> Self { + UiDocRef::from_ptr(std::ptr::null_mut()) + } +} + pub struct UiDocProxy { ptr: *mut c_void, _data: PhantomData -- 2.47.3