]> uap-core.de Git - note.git/commitdiff
save notes when exiting the application
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 29 May 2026 10:18:23 +0000 (12:18 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 29 May 2026 10:18:23 +0000 (12:18 +0200)
application/src/notebook.rs
application/src/window.rs

index 366590be4c04b228786807525ef3dd14e73ca74a..cfafe64881fad3d0125ce0828ddaabd6dbc4c120 100644 (file)
@@ -37,33 +37,35 @@ impl Notebook {
         self.notes.update();
     }
 
+    /// Saves and detaches the current note. This does NOT change self.selected_note
+    pub fn detach_current_note(&self) {
+        // detach the current note
+        if let Some(current) = &self.selected_note {
+            // doc_ref.get_doc() should never fail here
+            if let Some(mut doc) = self.doc_ref.get_doc() {
+                // save the note
+                let note_proxy = current.doc_proxy();
+                let backend = self.backend.clone();
+                let collection_id = self.collection_id;
+                note_proxy.call_mainthread(move |_doc, note|{
+                    note.save(collection_id, &backend);
+                });
+
+                // detach the note
+                doc.ctx.detach(current);
+            }
+        }
+    }
+
     #[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()) {
-                // detach the current note
-                if let Some(current) = &self.selected_note {
-                    // doc_ref.get_doc() should never fail here
-                    if let Some(mut doc) = self.doc_ref.get_doc() {
-                        // save the note
-                        let note_proxy = current.doc_proxy();
-                        let backend = self.backend.clone();
-                        let collection_id = self.collection_id;
-                        note_proxy.call_mainthread(move |_doc, note|{
-                            note.save(collection_id, &backend);
-                        });
-                        
-                        // detach the note
-                        doc.ctx.detach(current);
-                        self.selected_note = None;
-                    }
-                }
+                self.detach_current_note();
+                self.selected_note = None;
 
                 // Create the new note
                 let note_data: crate::note::Note = Default::default();
-                // TODO: setting the text here is the right thing to do, but for some
-                //       reasons it doesn't work yet. Fix this shit.
-                //note_data.text.set(note.content.as_str());
 
                 // Create the note document object
                 let note_doc = UiDoc::new2(note_data, |n,d| {
@@ -115,6 +117,6 @@ pub fn notelist_getvalue<'a>(elm: &Note, col: i32, _row: i32) -> ListValue<'a> {
 
 impl Drop for Notebook {
     fn drop(&mut self) {
-        println!("Notebook dropped");
+
     }
 }
\ No newline at end of file
index 2a7436f7f32da3cbaa1688f35360490833010568..dab067feea030e1e4b475161a20fdc0816860e0c 100644 (file)
@@ -194,8 +194,13 @@ pub fn create_window(app: &App, ctx: &AppContext<MainWindow>) -> UiObject<MainWi
     });
 
     window.show();
-    //window.onclose(|event| {
-    //});
+    window.onclose(|event| {
+        if let Some(doc) = &event.data.selected_notebook {
+            doc.doc_proxy().call_mainthread(|doc, notebook| {
+                notebook.detach_current_note();
+            });
+        }
+    });
     window
 }