]> uap-core.de Git - note.git/commitdiff
change notelist type to NoteItem, that includes the optional Note UI model
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 30 May 2026 21:36:39 +0000 (23:36 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Sat, 30 May 2026 21:36:39 +0000 (23:36 +0200)
application/src/notebook.rs
application/src/window.rs
ui-rs/src/ui/toolkit.rs

index f3b9fd187227a173703c0532df5266f603c60cd7..917e09a7df78b569d73bc255f9ee3580ace00a66 100644 (file)
@@ -5,6 +5,7 @@ use ui_rs::ui::*;
 use entity::note::{Model as Note};
 use crate::backend::{BackendHandle, BroadcastMessage, NoteId};
 
+/// UI model for a notebook
 #[derive(UiModel)]
 pub struct Notebook {
     pub doc_ref: UiDocRef<Notebook>,
@@ -15,7 +16,7 @@ pub struct Notebook {
     pub selected_note: Option<UiDoc<crate::note::Note>>,
 
     #[bind("notes")]
-    pub notes: UiList<Note>
+    pub notes: UiList<NoteItem>
 }
 
 #[ui_actions]
@@ -34,9 +35,11 @@ impl Notebook {
     pub fn set_notes(&mut self, notes: Vec<Note>) {
         let notes_list = self.notes.data_mut();
         notes_list.clear();
+
         for note in notes {
-            notes_list.push(note);
+            notes_list.push(NoteItem::new(note));
         }
+
         self.notes.update();
     }
 
@@ -63,52 +66,89 @@ impl Notebook {
     #[action]
     pub fn note_selected(&mut self, event: &ActionEvent) {
         if let EventType::ListSelection(s) = event.event_type {
-            if let Some(note) = s.selected_element(self.notes.data()) {
-                self.detach_current_note();
-                self.selected_note = None;
-
-                // Create the new note
-                let note_data = crate::note::Note::new(self.collection_id, self.backend.clone());
-
-                // Create the note document object
-                let note_doc = UiDoc::new2(note_data, |n,d| {
-                    // TODO: remove this when toolkit is not that buggy
-                    //       for some reason we have to attach the doc first, before we can
-                    //       set the text
-                    if let Some(mut nb) = self.doc_ref.get_doc() {
-                        nb.ctx.attach(&d);
-                    }
+            self.detach_current_note();
+            self.selected_note = None;
+            if let Some(note) = s.selected_element_mut(self.notes.data_mut()) {
+                let doc = if let Some(doc) = &note.model {
+                    doc.clone()
+                } else {
+                    // Create the new note
+                    let note_data = crate::note::Note::new(self.collection_id, self.backend.clone());
+
+                    // Create the note document object
+                    UiDoc::new2(note_data, |n,d| {
+                        // TODO: remove this when toolkit is not that buggy
+                        //       for some reason we have to attach the doc first, before we can
+                        //       set the text
+                        if let Some(mut nb) = self.doc_ref.get_doc() {
+                            nb.ctx.attach(&d);
+                        }
 
-                    n.init_from_model(&note);
-
-                    let proxy = d.doc_proxy();
-                    self.backend.get_note_content(note.note_id, move|result|{
-                        proxy.call_mainthread(|doc, note|{
-                            match result {
-                                Ok(content) => {
-                                    if let Some(content) = content {
-                                        note.init_content(&content);
-                                    } else {
-                                        println!("note {}: no content", note.note_id);
+                        n.init_from_model(&note.data);
+
+                        let proxy = d.doc_proxy();
+                        self.backend.get_note_content(note.data.note_id, move|result|{
+                            proxy.call_mainthread(|doc, note|{
+                                match result {
+                                    Ok(content) => {
+                                        if let Some(content) = content {
+                                            note.init_content(&content);
+                                        } else {
+                                            println!("note {}: no content", note.note_id);
+                                        }
+                                    },
+                                    Err(e) => {
+                                        println!("Cannot get note content: {}", e);
                                     }
-                                },
-                                Err(e) => {
-                                    println!("Cannot get note content: {}", e);
                                 }
-                            }
+                            });
                         });
-                    });
-                });
+                    })
+                };
 
-                // Attach the new note
                 if let Some(mut nb) = self.doc_ref.get_doc() {
-                    nb.ctx.attach(&note_doc);
-                    self.selected_note = Some(note_doc);
+                    nb.ctx.attach(&doc);
+                    self.selected_note = Some(doc);
                 }
             }
         }
     }
 
+    pub fn load_note(&self, note: &entity::note::Model) -> UiDoc<crate::note::Note> {
+        // Create the new note
+        let note_data = crate::note::Note::new(self.collection_id, self.backend.clone());
+
+        // Create the note document object
+        UiDoc::new2(note_data, |n,d| {
+            // TODO: remove this when toolkit is not that buggy
+            //       for some reason we have to attach the doc first, before we can
+            //       set the text
+            if let Some(mut nb) = self.doc_ref.get_doc() {
+                nb.ctx.attach(&d);
+            }
+
+            n.init_from_model(note);
+
+            let proxy = d.doc_proxy();
+            self.backend.get_note_content(note.note_id, move|result|{
+                proxy.call_mainthread(|doc, note|{
+                    match result {
+                        Ok(content) => {
+                            if let Some(content) = content {
+                                note.init_content(&content);
+                            } else {
+                                println!("note {}: no content", note.note_id);
+                            }
+                        },
+                        Err(e) => {
+                            println!("Cannot get note content: {}", e);
+                        }
+                    }
+                });
+            });
+        })
+    }
+
     #[action]
     pub fn message(&mut self, _event: &ActionEvent) {
         while let Ok(msg) = self.broadcast_rx.try_recv() {
@@ -119,10 +159,10 @@ impl Notebook {
                         if let NoteId::Id(id) = u.note_id {
                             let note_data = self.notes.data_mut();
                             for (i, note) in note_data.iter_mut().enumerate() {
-                                if note.note_id == id {
+                                if note.data.note_id == id {
                                     // update note
                                     if let Some(title) = &u.title {
-                                        note.title = title.clone();
+                                        note.data.title = title.clone();
                                     }
                                     update_rows.push(i);
                                 }
@@ -142,10 +182,10 @@ impl Notebook {
     }
 }
 
-pub fn notelist_getvalue<'a>(elm: &Note, col: i32, _row: i32) -> ListValue<'a> {
+pub fn notelist_getvalue<'a>(elm: &NoteItem, col: i32, _row: i32) -> ListValue<'a> {
     match col {
-        0 => ListValue::String(elm.title.clone()),
-        1 => ListValue::String(elm.lastmodified.to_string()),
+        0 => ListValue::String(elm.data.title.clone()),
+        1 => ListValue::String(elm.data.lastmodified.to_string()),
         _ => ListValue::None
     }
 }
@@ -154,4 +194,21 @@ impl Drop for Notebook {
     fn drop(&mut self) {
 
     }
+}
+
+/// ListView item
+pub struct NoteItem {
+    id: NoteId,
+    data: entity::note::Model,
+    model: Option<UiDoc<crate::note::Note>>,
+}
+
+impl NoteItem {
+    pub fn new(data: entity::note::Model) -> Self {
+        NoteItem {
+            id: NoteId::Id(data.note_id),
+            data: data,
+            model: None
+        }
+    }
 }
\ No newline at end of file
index 1aefa7a62cd5b38f09b6545808856f3b38b12450..fa0c9d3006a0687eea2e1170be8c6881c8ef9690 100644 (file)
@@ -33,7 +33,7 @@ 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::{notelist_getvalue, Notebook};
+use crate::notebook::{notelist_getvalue, NoteItem, Notebook};
 
 #[derive(UiModel)]
 pub struct MainWindow {
@@ -168,7 +168,7 @@ 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::<Note>()
+            obj.tableview_builder::<NoteItem>()
                 .varname("notes")
                 .model(&model)
                 .fill(true)
index bb9d111dfedd104886cf9afc0db4cb40f99aa697..e939b5a0c2c298c69598e6e806cf485f5f296711 100644 (file)
@@ -869,20 +869,30 @@ impl ListSelection {
         sel
     }
 
-    pub fn selected_element<'a, T>(&self, data: &'a Vec<T>) -> Option<&'a T> {
+    pub fn selected_index(&self) -> Option<usize> {
         unsafe {
             let count = ui_list_selection_get_count(self.ptr);
             if count == 1 {
                 let sel = ui_list_selection_get_rows(self.ptr);
                 let index = *sel;
                 if index >= 0 {
-                    return data.get(index as usize);
+                    return Some(index as usize);
                 }
             }
         }
         None
     }
 
+    pub fn selected_element<'a, T>(&self, data: &'a Vec<T>) -> Option<&'a T> {
+        let index = self.selected_index()?;
+        data.get(index)
+    }
+
+    pub fn selected_element_mut<'a, T>(&self, data: &'a mut Vec<T>) -> Option<&'a mut T> {
+        let index = self.selected_index()?;
+        data.get_mut(index)
+    }
+
     pub fn count(&self) -> usize {
         unsafe {
             ui_list_selection_get_count(self.ptr) as usize