#[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
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 {
e.obj.ctx.detach(current);
}
- let notebook = Notebook::new();
+ let notebook = Notebook::new(nb.collection_id);
let doc = UiDoc::new(notebook);
// TODO: load notes
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();
});
}
}
+ 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
}
}
-
pub struct UiObjProxy<T> {
ptr: *mut ffi::UiObject,
_data: PhantomData<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>