From 4d5ed8109cd5330bc700b19007dba95ebfb5e8f1 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Wed, 22 Jul 2026 21:35:48 +0200 Subject: [PATCH] renaming a file note's nodename also changes updates the title --- application/backend/src/backend.rs | 3 +- application/note/src/file.rs | 46 +++++++++++++++++++++++++++--- application/note/src/window.rs | 1 + ui/common/args.c | 2 +- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/application/backend/src/backend.rs b/application/backend/src/backend.rs index 2375dbf..9179d12 100644 --- a/application/backend/src/backend.rs +++ b/application/backend/src/backend.rs @@ -660,7 +660,7 @@ impl BackendHandle { let _ = self.tx.send(cmd); } - pub fn rename_note(&self, note_id: i32, nodename: String, callback: F) + pub fn rename_note(&self, note_id: i32, nodename: String, title: String, callback: F) where F: FnOnce(Result) + Send + 'static { let bhandle = self.clone(); let cmd = Box::pin(async move { @@ -675,6 +675,7 @@ impl BackendHandle { let result = Note::update_many() .col_expr(Column::Nodename, Expr::value(nodename.clone())) + .col_expr(Column::Title, Expr::value(title.clone())) .filter(Column::NoteId.eq(note_id)) .exec(&bhandle.backend.db).await; diff --git a/application/note/src/file.rs b/application/note/src/file.rs index 5f02f53..ef3ec55 100644 --- a/application/note/src/file.rs +++ b/application/note/src/file.rs @@ -26,9 +26,9 @@ * POSSIBILITY OF SUCH DAMAGE. */ use std::mem; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::rc::Rc; -use backend::backend::{BackendHandle, NoteId, OpenExternResult}; +use backend::backend::{BackendHandle, BroadcastMessage, NoteId, NoteTitleUpdate, OpenExternResult}; use ui_rs::{action, doc_cast, ui_actions, UiModel}; use ui_rs::ui::*; use crate::AppStates; @@ -127,6 +127,28 @@ impl FileNote { self.save_note(); } + #[action] + pub fn note_nodename_change(&mut self, event: &ActionEvent) { + if event.set { + return; + } + let nodename = self.note_nodename.get(); + self.update_title(nodename.as_str(), true); + } + + pub fn update_title(&mut self, s: &str, notify: bool) { + let title = generate_title(s).unwrap_or("file"); + + 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)); + } + } + #[action] pub fn notebook_view_changed(&mut self, event: &mut ActionEvent) { // TODO: duplicated code in note.rs @@ -161,10 +183,14 @@ impl NoteViewModel for FileNote { // nothing has changed return; } + + // TODO: "file" is also used in update_title() and needs to be configured somewhere + let new_title = generate_title(&nodename).unwrap_or("file").to_string(); + mem::swap(&mut self.nodename, &mut nodename); let proxy = doc.doc_proxy(); - self.backend.rename_note(note_id, self.nodename.clone(), |result|{ + self.backend.rename_note(note_id, self.nodename.clone(), new_title, |result|{ proxy.call_mainthread(move |_doc, note|{ match result { Ok(updated) => { @@ -181,4 +207,16 @@ impl NoteViewModel for FileNote { }); } -} \ No newline at end of file +} + +fn generate_title(s: &str) -> Option<&str> { + if !s.trim().is_empty() { + let name = Path::new(s) + .file_stem() + .and_then(|s| s.to_str()) + .unwrap_or(s); + return Some(name); + } + + None +} diff --git a/application/note/src/window.rs b/application/note/src/window.rs index 6c48d81..2b5b019 100644 --- a/application/note/src/window.rs +++ b/application/note/src/window.rs @@ -303,6 +303,7 @@ pub fn create_window(app: &App, ctx: &AppContext) -> UiObjectonchange_action = action; + args->onchange_action = strdup(action); } void ui_textfield_args_set_varname(UiTextFieldArgs *args, const char *varname) { -- 2.52.0