]> uap-core.de Git - note.git/commitdiff
add textfield for custom note titles
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 31 Jul 2026 19:31:27 +0000 (21:31 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Fri, 31 Jul 2026 19:31:27 +0000 (21:31 +0200)
application/note/src/main.rs
application/note/src/note.rs
application/note/src/window.rs

index 7d695b4bcd67a5456de9016695270b7981bb89bc..dfe91510ac6691bdb19bf534fb001b60e5f9679d 100644 (file)
@@ -145,6 +145,7 @@ fn create_menubar(app: &AppContext<MainWindow>) {
         menu.item("New Window").onclick(|_| new_app_window() ).create();
         menu.separator();
         menu.item("Delete").action("note_delete").create();
+        menu.item("Edit Title").action("note_edit_title").create();
     });
 }
 
@@ -171,6 +172,7 @@ fn create_toolbar(app: &AppContext<MainWindow>) {
         menu.item("New Window").onclick(|_| new_app_window() ).create();
         menu.separator();
         menu.item("Delete").action("delete_note").create();
+        menu.item("Edit Title").action("note_edit_title").create();
     });
 }
 
@@ -181,7 +183,8 @@ pub enum AppStates {
     NoteEnableNew,
     NoteShowInfo,
     NoteShowExtModInfo,
-    NoteMaximized
+    NoteMaximized,
+    NoteTitleInput,
 }
 
 fn username() -> String {
index dee819486f9487f860f898042875516a5e35da59..175d9651a9a829098dbb542535c32319f6c73c33 100644 (file)
@@ -69,6 +69,7 @@ pub struct Note {
 
     modified: bool,
     is_saving: bool,
+    fixed_title: bool,
 
     pub local_path: Option<PathBuf>,
     pub local_lastmodified: Option<SystemTime>,
@@ -76,6 +77,9 @@ pub struct Note {
     #[bind("note_type")]
     pub note_type: UiInteger,
 
+    #[bind("note_title")]
+    pub title: UiString,
+
     #[bind("note_text")]
     pub text: UiText,
     
@@ -103,10 +107,12 @@ impl Note {
             title_end: -1,
             modified: false,
             is_saving: false,
+            fixed_title: false,
             local_path: None,
             local_lastmodified: None,
 
             note_type: Default::default(),
+            title: Default::default(),
             text: Default::default(),
             info: Default::default(),
         }
@@ -201,6 +207,8 @@ impl Note {
             _ => NoteTypeTabView::Empty
         };
         self.note_type.set(tab as i64);
+        self.title.set(model.title.as_str());
+        self.fixed_title = model.fixed_title;
     }
 
     pub fn init_new(&mut self) {
@@ -258,16 +266,22 @@ impl Note {
             }
         };
 
+        self.title.set(title);
+
         if notify {
-            let update = NoteTitleUpdate {
-                collection_id: self.collection_id,
-                note_id: self.id.clone(),
-                title: title.to_string(),
-            };
-            _ = self.backend.as_ref().send_broadcast(BroadcastMessage::NoteTitleUpdate(update));
+            self.notify_title_change(title);
         }
     }
 
+    pub fn notify_title_change(&mut self, title: &str) {
+        let update = NoteTitleUpdate {
+            collection_id: self.collection_id,
+            note_id: self.id.clone(),
+            title: title.to_string(),
+        };
+        _ = self.backend.as_ref().send_broadcast(BroadcastMessage::NoteTitleUpdate(update));
+    }
+
     #[action(Event)]
     pub fn filesystem_notify(&mut self, _event: &ActionEvent, event: &Event) {
         // for now we only care about modify events
@@ -361,12 +375,32 @@ impl Note {
             }
         }
 
-        if !event.set && self.extract_title {
+        if !event.set && self.extract_title && !self.fixed_title {
             self.update_title(self.text.get().as_str(), true);
         }
         self.extract_title = false;
     }
 
+    #[action]
+    pub fn note_edit_title(&mut self, _event: &ActionEvent) {
+        let Some(doc) = self.doc.get_doc() else {
+            return;
+        };
+        doc.ctx.set_state(AppStates::NoteTitleInput as i32);
+        self.fixed_title = true;
+    }
+
+    #[action]
+    pub fn note_title_changed(&mut self, event: &ActionEvent) {
+        if event.set {
+            return;
+        }
+
+        let title = self.title.get();
+        self.notify_title_change(title.as_str());
+        self.modified = true;
+    }
+
     #[action]
     pub fn notebook_view_changed(&mut self, event: &mut ActionEvent) {
         // This action is for the toolbar button, to change the notebook view
@@ -409,9 +443,18 @@ impl NoteViewModel for Note {
         };
 
         let content = self.text.get();
-        let title = match generate_title(content.as_str()) {
-            Some(title) => title.0.to_string(),
-            None => "New Note".to_string() // TODO: see NoteItem::new()
+        let title = if self.fixed_title {
+            let title = self.title.get();
+            if title.is_empty() {
+                "New Note".to_string()
+            } else {
+                title
+            }
+        } else {
+            match generate_title(content.as_str()) {
+                Some(title) => title.0.to_string(),
+                None => "New Note".to_string() // TODO: see NoteItem::new()
+            }
         };
 
         let (note_id, created) = match &self.id {
@@ -427,6 +470,7 @@ impl NoteViewModel for Note {
             title: Set(title),
             lastmodified: Set(Utc::now().into()),
             created: created,
+            fixed_title: Set(self.fixed_title),
             version: Set(self.version)
         };
 
index 9607ebfc32c5793393ff9502be9b284f8ca6260e..4d4ec77126d19d1afc32c39009cfb4229638ae22 100644 (file)
@@ -254,6 +254,10 @@ pub fn create_window(app: &App, ctx: &AppContext<MainWindow>) -> UiObject<MainWi
                         button!(obj, label = "Reload", action = "note_reload");
                         button!(obj, label = "Close", action = "note_extmod_close");
                     });
+                    hbox!(obj, visibility_states = &[AppStates::NoteTitleInput as i32], spacing = 4, margin = 4, |obj|{
+                        label!(obj, label = "Title:");
+                        textfield!(obj, fill = true, varname = "note_title", onchange_action = "note_title_changed");
+                    });
                     let textarea = textarea!(obj, fill = true, varname = "note_text", onchange_action = "note_text_change", ontextchanged_action = "note_text_changed");
                     obj.ctx.add_action_untyped("textarea_focus", move|_event|{
                         textarea.focus();