From de8804156b254be4913c9123126f25e465e32032 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sun, 31 May 2026 21:18:12 +0200 Subject: [PATCH] change sourcelist item type to NotebookItem, that also stores the Notebook UI model --- application/src/notebook.rs | 44 ++++++++++++++++++++++++++++++++++++- application/src/window.rs | 27 +++++++++++++++++------ ui-rs/src/ui/event.rs | 8 +++++++ 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/application/src/notebook.rs b/application/src/notebook.rs index 786bc5e..d8030c9 100644 --- a/application/src/notebook.rs +++ b/application/src/notebook.rs @@ -104,7 +104,7 @@ impl Notebook { #[action] pub fn new_note(&mut self, _event: &ActionEvent) { println!("new note"); - + let item = NoteItem::new(new_note_id()); self.notes.data_mut().insert(0, item); self.notes.update(); @@ -187,6 +187,48 @@ impl Drop for Notebook { } } +/// SourceList sublist item +pub struct NotebookItem { + pub data: entity::collection::Model, + pub model: Option> +} + +impl NotebookItem { + pub fn from_model(data: entity::collection::Model) -> Self { + NotebookItem { + data: data, + model: None + } + } + + pub fn load_notebook(&mut self, backend: &BackendHandle) -> UiDoc { + // Create Notebook UI model + let notebook = Notebook::new(self.data.collection_id, backend); + let doc = UiDoc::new2(notebook, |notebook, doc| { + notebook.doc_ref = doc.doc_ref(); + }); + + // Load notes + let proxy = doc.doc_proxy(); + backend.get_notes(self.data.collection_id, |result|{ + proxy.call_mainthread(|_, nb| { + match result { + Ok(notes) => { + nb.set_notes(notes); + }, + Err(err) => { + eprintln!("get_notes failed: {}", err); + } + } + }); + }); + + self.model = Some(doc.clone()); + + doc + } +} + /// ListView item pub struct NoteItem { id: NoteId, diff --git a/application/src/window.rs b/application/src/window.rs index 855527c..69d6c84 100644 --- a/application/src/window.rs +++ b/application/src/window.rs @@ -32,7 +32,7 @@ use crate::App; use entity::collection::{Model as Collection, Node}; use crate::backend::{BackendHandle, BroadcastMessage}; use crate::newnotebook::new_notebook_dialog; -use crate::notebook::{notelist_getvalue, NoteItem, Notebook}; +use crate::notebook::{notelist_getvalue, NoteItem, Notebook, NotebookItem}; #[derive(UiModel)] pub struct MainWindow { @@ -47,7 +47,7 @@ pub struct MainWindow { groups: Vec, #[bind] - notebooks: UiSourceList + notebooks: UiSourceList } @@ -86,7 +86,7 @@ impl MainWindow { // TODO: maybe select a configured default notebook if let Some(group) = self.notebooks.get(0) { if let Some(notebook) = group.data().get(0) { - self.select_notebook(notebook.collection_id); + self.select_notebook(notebook.data.collection_id); // self.selected_notebook has a value after select_notebook() if let Some(doc) = &self.selected_notebook { // Now that a notebook is selected, we can open the new note UI there @@ -159,7 +159,7 @@ impl MainWindow { let sublist_data = notebook.list().data_mut(); for child in elm.children.iter() { - sublist_data.push(child.collection.clone()); + sublist_data.push(NotebookItem::from_model(child.collection.clone())); } self.notebooks.push(notebook); @@ -180,12 +180,25 @@ pub fn create_window(app: &App, ctx: &AppContext) -> UiObject(&self, sourcelist: &'a mut UiSourceList) -> Option<&'a mut T> { + if self.row_index < 0 { + return None; + } + + sourcelist.get_mut(self.sublist_index)?.data_mut().get_mut(self.row_index as usize) + } } -- 2.47.3