]> uap-core.de Git - note.git/commitdiff
change sourcelist item type to NotebookItem, that also stores the Notebook UI model main
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 31 May 2026 19:18:12 +0000 (21:18 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sun, 31 May 2026 19:18:12 +0000 (21:18 +0200)
application/src/notebook.rs
application/src/window.rs
ui-rs/src/ui/event.rs

index 786bc5ebae69ea380908d778af51f658cd88a3be..d8030c97692d34e090f9aff7904020f4bd77d409 100644 (file)
@@ -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<UiDoc<Notebook>>
+}
+
+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<Notebook> {
+        // 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,
index 855527cb280419921e3ffbd0179107cc2d1264c3..69d6c84673c284f96d3495cb6c7ebdc7ae4f0844 100644 (file)
@@ -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<Collection>,
 
     #[bind]
-    notebooks: UiSourceList<Collection>
+    notebooks: UiSourceList<NotebookItem>
 }
 
 
@@ -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<MainWindow>) -> UiObject<MainWi
                 b.fill(true);
                 b.value(&data.notebooks);
                 b.getvalue(|elm,_row,item|{
-                    item.label = Some(elm.name.clone());
+                    item.label = Some(elm.data.name.clone());
                 });
                 b.onactivate(|e|{
                     if let EventType::SubList(sl) = &e.event_type {
-                        if let Some(nb) = sl.get_from(&e.data.notebooks) {
-                            e.data.select_notebook(nb.collection_id);
+                        if let Some(nb) = sl.get_mut_from(&mut e.data.notebooks) {
+                            // detach current notebook
+                            if let Some(current) = &e.data.selected_notebook {
+                                e.obj.ctx.detach(current);
+                            }
+
+                            let notebook_doc = if let Some(doc) = &nb.model {
+                                doc.clone()
+                            } else {
+                                let doc = nb.load_notebook(&e.data.backend);
+                                doc
+                            };
+
+                            e.obj.ctx.attach(&notebook_doc);
+                            e.data.selected_notebook = Some(notebook_doc);
                         }
                     }
                 });
index 884baf44fa05721ac659835a80f6b9e42686a938..984a8fa48e5f216c8fd1903be502c55e457e6217 100644 (file)
@@ -313,6 +313,14 @@ impl SubListEvent {
             .data()
             .get(self.row_index as usize)
     }
+
+    pub fn get_mut_from<'a, T>(&self, sourcelist: &'a mut UiSourceList<T>) -> 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)
+    }
 }