let _ = self.tx.send(cmd);
}
- pub fn rename_note<F>(&self, note_id: i32, nodename: String, callback: F)
+ pub fn rename_note<F>(&self, note_id: i32, nodename: String, title: String, callback: F)
where F: FnOnce(Result<UpdateResult, DbErr>) + Send + 'static {
let bhandle = self.clone();
let cmd = Box::pin(async move {
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;
* 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;
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
// 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) => {
});
}
-}
\ 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
+}