From edaf53f795d48e0b447982a5427880febe784715 Mon Sep 17 00:00:00 2001 From: anig1scur Date: Wed, 23 Oct 2024 21:06:08 +0800 Subject: [PATCH 1/5] feat: update title on note content change --- src/util.ts | 28 ++++++++++++++++++++++++++++ src/window.ts | 14 ++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/util.ts b/src/util.ts index 2d002b3..f1284e3 100644 --- a/src/util.ts +++ b/src/util.ts @@ -51,6 +51,7 @@ export interface INote { v: 1; uuid: string; content: string; + title: string; style: Style; tags: ITag[]; modified: Date; @@ -151,6 +152,7 @@ export class Note extends GObject.Object { v: 1; uuid: string; content: string; + title: string; style: Style; tag_list: Gio.ListStore; modified: GLib.DateTime; @@ -201,6 +203,7 @@ export class Note extends GObject.Object { this.v = note.v; this.uuid = note.uuid; this.content = note.content; + this.title = note.title; this.style = note.style; this.tag_list = Gio.ListStore.new(Tag.$gtype) as Gio.ListStore; this.tags = note.tags; @@ -211,6 +214,23 @@ export class Note extends GObject.Object { this.width = note.width; this.height = note.height; this.open = note.open ?? false; + + // @ts-expect-error incorrect types + this.bind_property_full( + "content", + this, + "title", + GObject.BindingFlags.SYNC_CREATE, + (_, content) => { + if (!content) return [false, ""]; + return [true, content.substring(0, 24)] + }, + null + ); + + this.connect("notify::title", () => { + this.emit('title-changed'); + }); } static generate() { @@ -218,6 +238,7 @@ export class Note extends GObject.Object { v: 1, uuid: GLib.uuid_string_random(), content: "", + title: "", style: SETTINGS.DEFAULT_STYLE, tags: [], modified: new Date(), @@ -232,6 +253,7 @@ export class Note extends GObject.Object { v: this.v, uuid: this.uuid, content: this.content, + title: this.title, style: this.style, tags: this.tags.map((tag) => ({ name: tag.name, @@ -250,6 +272,7 @@ export class Note extends GObject.Object { v: this.v, uuid: this.uuid, content: this.content, + title: this.title, style: this.style, tags: this.tags, modified: this.modified_date, @@ -270,6 +293,8 @@ export class Note extends GObject.Object { // deno-fmt-ignore content: GObject.ParamSpec.string("content", "Content", "Content of the note", GObject.ParamFlags.READWRITE, ""), // deno-fmt-ignore + title: GObject.ParamSpec.string("title", "Title", "Title of the note", GObject.ParamFlags.READWRITE, ""), + // deno-fmt-ignore style: GObject.ParamSpec.int("style", "Style", "Style of the note", GObject.ParamFlags.READWRITE, 0, 100, 0), // deno-fmt-ignore tag_list: this.tag_list_pspec, @@ -282,6 +307,9 @@ export class Note extends GObject.Object { // deno-fmt-ignore open: GObject.ParamSpec.boolean("open", "Open", "Whether the note was open when the application was closed", GObject.ParamFlags.READWRITE, false), }, + Signals: { + 'title-changed': {}, + }, }, this); } } diff --git a/src/window.ts b/src/window.ts index 2d971a9..ff09c77 100644 --- a/src/window.ts +++ b/src/window.ts @@ -98,6 +98,12 @@ export class Window extends Adw.ApplicationWindow { this.default_width = note.width; this.default_height = note.height; + this.update_title(); + + this.note.connect("title-changed", () => { + this.update_title(); + }); + this.connect("close-request", () => { if (this.deleted) return; @@ -151,6 +157,14 @@ export class Window extends Adw.ApplicationWindow { popover.add_child(this.selector, "notestyleswitcher"); } + update_title() { + if (this.note.title === "") { + this.set_title(_("Untitled Note")); + return; + } + this.set_title(this.note.title); + } + last_revealer = false; update_link(selected: boolean, text: string) { From fbe8af5e7f2f50112509a906a814c434d22df4f1 Mon Sep 17 00:00:00 2001 From: anig1scur Date: Wed, 23 Oct 2024 21:32:31 +0800 Subject: [PATCH 2/5] chore: update listen & add ellipsis --- src/util.ts | 11 ++++------- src/window.ts | 6 +++--- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/util.ts b/src/util.ts index f1284e3..c407e35 100644 --- a/src/util.ts +++ b/src/util.ts @@ -223,14 +223,14 @@ export class Note extends GObject.Object { GObject.BindingFlags.SYNC_CREATE, (_, content) => { if (!content) return [false, ""]; - return [true, content.substring(0, 24)] + + const title = content.split("\n")[0]; + return [true, title.length > 20 ? title.slice(0, 20) + "..." : title]; }, null ); - this.connect("notify::title", () => { - this.emit('title-changed'); - }); + } static generate() { @@ -307,9 +307,6 @@ export class Note extends GObject.Object { // deno-fmt-ignore open: GObject.ParamSpec.boolean("open", "Open", "Whether the note was open when the application was closed", GObject.ParamFlags.READWRITE, false), }, - Signals: { - 'title-changed': {}, - }, }, this); } } diff --git a/src/window.ts b/src/window.ts index ff09c77..ce1d938 100644 --- a/src/window.ts +++ b/src/window.ts @@ -100,7 +100,7 @@ export class Window extends Adw.ApplicationWindow { this.update_title(); - this.note.connect("title-changed", () => { + this.note.connect("notify::title", () => { this.update_title(); }); @@ -158,8 +158,8 @@ export class Window extends Adw.ApplicationWindow { } update_title() { - if (this.note.title === "") { - this.set_title(_("Untitled Note")); + if (!this.note.title) { + this.set_title("Untitled Note"); return; } this.set_title(this.note.title); From 45060704935953f7aab41fcc16f3ab05be189a99 Mon Sep 17 00:00:00 2001 From: anig1scur Date: Wed, 23 Oct 2024 22:38:02 +0800 Subject: [PATCH 3/5] chore: bind note title to window --- src/util.ts | 8 ++++---- src/window.ts | 19 ++++++------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/util.ts b/src/util.ts index c407e35..ae69669 100644 --- a/src/util.ts +++ b/src/util.ts @@ -222,10 +222,10 @@ export class Note extends GObject.Object { "title", GObject.BindingFlags.SYNC_CREATE, (_, content) => { - if (!content) return [false, ""]; - - const title = content.split("\n")[0]; - return [true, title.length > 20 ? title.slice(0, 20) + "..." : title]; + if (!content) return [true, ""]; + let title = content.split("\n")[0].slice(0, 20); + if (title.length != content.length) title += "…"; + return [true, title]; }, null ); diff --git a/src/window.ts b/src/window.ts index ce1d938..51c8b41 100644 --- a/src/window.ts +++ b/src/window.ts @@ -98,11 +98,12 @@ export class Window extends Adw.ApplicationWindow { this.default_width = note.width; this.default_height = note.height; - this.update_title(); - - this.note.connect("notify::title", () => { - this.update_title(); - }); + this.note.bind_property( + "title", + this, + "title", + GObject.BindingFlags.SYNC_CREATE + ); this.connect("close-request", () => { if (this.deleted) return; @@ -157,14 +158,6 @@ export class Window extends Adw.ApplicationWindow { popover.add_child(this.selector, "notestyleswitcher"); } - update_title() { - if (!this.note.title) { - this.set_title("Untitled Note"); - return; - } - this.set_title(this.note.title); - } - last_revealer = false; update_link(selected: boolean, text: string) { From d3fc6f07bb87fca2d0655ca1dc74961d769bd31c Mon Sep 17 00:00:00 2001 From: anig1scur Date: Thu, 24 Oct 2024 11:53:19 +0800 Subject: [PATCH 4/5] chore: Untitled Note for empty --- src/window.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/window.ts b/src/window.ts index 51c8b41..dedbd9f 100644 --- a/src/window.ts +++ b/src/window.ts @@ -98,11 +98,18 @@ export class Window extends Adw.ApplicationWindow { this.default_width = note.width; this.default_height = note.height; - this.note.bind_property( + this.note.bind_property_full( "title", this, "title", - GObject.BindingFlags.SYNC_CREATE + GObject.BindingFlags.SYNC_CREATE, + (_, title) => { + if (!title) { + return [true, "Untitled Note"]; + } + return [true, title]; + }, + null ); this.connect("close-request", () => { From ee60b6f383e9750d27550a319ae71014369f5805 Mon Sep 17 00:00:00 2001 From: anig1scur Date: Thu, 24 Oct 2024 16:46:27 +0800 Subject: [PATCH 5/5] chore: gettext --- src/window.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/window.ts b/src/window.ts index dedbd9f..312bdfd 100644 --- a/src/window.ts +++ b/src/window.ts @@ -103,9 +103,9 @@ export class Window extends Adw.ApplicationWindow { this, "title", GObject.BindingFlags.SYNC_CREATE, - (_, title) => { + (binding, title) => { if (!title) { - return [true, "Untitled Note"]; + return [true, _("Sticky Note")]; } return [true, title]; },