]> uap-core.de Git - note.git/commitdiff
add UiDocRef
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 22 May 2026 15:58:15 +0000 (17:58 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 22 May 2026 15:58:15 +0000 (17:58 +0200)
application/src/notebook.rs
application/src/window.rs
ui-rs/src/ui/toolkit.rs

index a601006697814ff43c84d5eddd9b0f1a4932211e..7386fe9e067931eb9d82c8edcef2131fa76b6342 100644 (file)
@@ -5,13 +5,25 @@ use entity::note::Model as Note;
 
 #[derive(UiModel, Default)]
 pub struct Notebook {
+    pub collection_id: i32,
+
     #[bind]
     pub notes: UiList<Note>
 }
 
 #[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
index 84d07ef38d4bcef5b3e2b5bc1dabc2deef8c5eb5..b7a2cbf733f9b268a18613e3ee7003dcb0e4ecca 100644 (file)
@@ -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<MainWindow>) -> UiObject<MainWi
                                 e.obj.ctx.detach(current);
                             }
 
-                            let notebook = Notebook::new();
+                            let notebook = Notebook::new(nb.collection_id);
                             let doc = UiDoc::new(notebook);
 
                             // TODO: load notes
@@ -149,9 +150,10 @@ pub fn create_window(app: &App, ctx: &AppContext<MainWindow>) -> UiObject<MainWi
             model.add_column("Name", ColumnType::String, -1);
             model.add_column("Last Modified", ColumnType::String, 0);
 
-            obj.tableview_builder::<String>()
+            obj.tableview_builder::<Note>()
                 .varname("notes")
                 .fill(true)
+                .getvalue(note_getvalue)
                 .create();
         });
 
index e615080722b9d4ab964e7ca20a88e1537e745b31..4ca8ca9677e54386d05f31493130def8c84dd85b 100644 (file)
@@ -177,6 +177,14 @@ impl<T: UiModel + UiActions> UiDoc<T> {
         }
     }
 
+    pub fn doc_ref(&self) -> UiDocRef<T> {
+        UiDocRef::from_ptr(self.ptr)
+    }
+
+    pub fn doc_proxy(&self) -> UiDocProxy<T> {
+        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<T> Default for UiObjRef<T> {
     }
 }
 
-
 pub struct UiObjProxy<T> {
     ptr: *mut ffi::UiObject,
     _data: PhantomData<T>
@@ -368,6 +375,73 @@ impl<T> Drop for UiObjProxy<T> {
     }
 }
 
+
+
+
+pub struct UiDocRef<T: UiModel + UiActions> {
+    ptr: Box<*mut c_void>,
+    _data: PhantomData<T>
+}
+
+impl<T: UiModel + UiActions> UiDocRef<T> {
+    fn from_ptr(doc_ptr: *mut c_void) -> UiDocRef<T> {
+        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<UiDoc<T>> {
+        if self.ptr.is_null() {
+            None
+        } else {
+            Some(UiDoc::from_ptr(*self.ptr))
+        }
+    }
+}
+
+impl<T: UiModel + UiActions> Drop for UiDocRef<T> {
+    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<T: UiModel + UiActions> Clone for UiDocRef<T> {
+    fn clone(&self) -> Self {
+        UiDocRef::from_ptr(*self.ptr)
+    }
+}
+
+impl<T: UiModel + UiActions> Default for UiDocRef<T> {
+    fn default() -> Self {
+        UiDocRef::from_ptr(std::ptr::null_mut())
+    }
+}
+
 pub struct UiDocProxy<T: UiModel + UiActions> {
     ptr: *mut c_void,
     _data: PhantomData<T>