From: Olaf Wintermann Date: Sat, 30 May 2026 21:36:39 +0000 (+0200) Subject: change notelist type to NoteItem, that includes the optional Note UI model X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=ef0afaaca6ffe87b6c0974b7508d5d230f915152;p=note.git change notelist type to NoteItem, that includes the optional Note UI model --- diff --git a/application/src/notebook.rs b/application/src/notebook.rs index f3b9fd1..917e09a 100644 --- a/application/src/notebook.rs +++ b/application/src/notebook.rs @@ -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, @@ -15,7 +16,7 @@ pub struct Notebook { pub selected_note: Option>, #[bind("notes")] - pub notes: UiList + pub notes: UiList } #[ui_actions] @@ -34,9 +35,11 @@ impl Notebook { pub fn set_notes(&mut self, notes: Vec) { 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) = ¬e.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(¬e); - - 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(¬e.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(¬e_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 { + // 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>, +} + +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 diff --git a/application/src/window.rs b/application/src/window.rs index 1aefa7a..fa0c9d3 100644 --- a/application/src/window.rs +++ b/application/src/window.rs @@ -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) -> UiObject() + obj.tableview_builder::() .varname("notes") .model(&model) .fill(true) diff --git a/ui-rs/src/ui/toolkit.rs b/ui-rs/src/ui/toolkit.rs index bb9d111..e939b5a 100644 --- a/ui-rs/src/ui/toolkit.rs +++ b/ui-rs/src/ui/toolkit.rs @@ -869,20 +869,30 @@ impl ListSelection { sel } - pub fn selected_element<'a, T>(&self, data: &'a Vec) -> Option<&'a T> { + pub fn selected_index(&self) -> Option { 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) -> Option<&'a T> { + let index = self.selected_index()?; + data.get(index) + } + + pub fn selected_element_mut<'a, T>(&self, data: &'a mut Vec) -> 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