Skip to content

Commit edaf53f

Browse files
committed
feat: update title on note content change
1 parent 9a22dc8 commit edaf53f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/util.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export interface INote {
5151
v: 1;
5252
uuid: string;
5353
content: string;
54+
title: string;
5455
style: Style;
5556
tags: ITag[];
5657
modified: Date;
@@ -151,6 +152,7 @@ export class Note extends GObject.Object {
151152
v: 1;
152153
uuid: string;
153154
content: string;
155+
title: string;
154156
style: Style;
155157
tag_list: Gio.ListStore<Tag>;
156158
modified: GLib.DateTime;
@@ -201,6 +203,7 @@ export class Note extends GObject.Object {
201203
this.v = note.v;
202204
this.uuid = note.uuid;
203205
this.content = note.content;
206+
this.title = note.title;
204207
this.style = note.style;
205208
this.tag_list = Gio.ListStore.new(Tag.$gtype) as Gio.ListStore<Tag>;
206209
this.tags = note.tags;
@@ -211,13 +214,31 @@ export class Note extends GObject.Object {
211214
this.width = note.width;
212215
this.height = note.height;
213216
this.open = note.open ?? false;
217+
218+
// @ts-expect-error incorrect types
219+
this.bind_property_full(
220+
"content",
221+
this,
222+
"title",
223+
GObject.BindingFlags.SYNC_CREATE,
224+
(_, content) => {
225+
if (!content) return [false, ""];
226+
return [true, content.substring(0, 24)]
227+
},
228+
null
229+
);
230+
231+
this.connect("notify::title", () => {
232+
this.emit('title-changed');
233+
});
214234
}
215235

216236
static generate() {
217237
return new this({
218238
v: 1,
219239
uuid: GLib.uuid_string_random(),
220240
content: "",
241+
title: "",
221242
style: SETTINGS.DEFAULT_STYLE,
222243
tags: [],
223244
modified: new Date(),
@@ -232,6 +253,7 @@ export class Note extends GObject.Object {
232253
v: this.v,
233254
uuid: this.uuid,
234255
content: this.content,
256+
title: this.title,
235257
style: this.style,
236258
tags: this.tags.map((tag) => ({
237259
name: tag.name,
@@ -250,6 +272,7 @@ export class Note extends GObject.Object {
250272
v: this.v,
251273
uuid: this.uuid,
252274
content: this.content,
275+
title: this.title,
253276
style: this.style,
254277
tags: this.tags,
255278
modified: this.modified_date,
@@ -270,6 +293,8 @@ export class Note extends GObject.Object {
270293
// deno-fmt-ignore
271294
content: GObject.ParamSpec.string("content", "Content", "Content of the note", GObject.ParamFlags.READWRITE, ""),
272295
// deno-fmt-ignore
296+
title: GObject.ParamSpec.string("title", "Title", "Title of the note", GObject.ParamFlags.READWRITE, ""),
297+
// deno-fmt-ignore
273298
style: GObject.ParamSpec.int("style", "Style", "Style of the note", GObject.ParamFlags.READWRITE, 0, 100, 0),
274299
// deno-fmt-ignore
275300
tag_list: this.tag_list_pspec,
@@ -282,6 +307,9 @@ export class Note extends GObject.Object {
282307
// deno-fmt-ignore
283308
open: GObject.ParamSpec.boolean("open", "Open", "Whether the note was open when the application was closed", GObject.ParamFlags.READWRITE, false),
284309
},
310+
Signals: {
311+
'title-changed': {},
312+
},
285313
}, this);
286314
}
287315
}

src/window.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export class Window extends Adw.ApplicationWindow {
9898
this.default_width = note.width;
9999
this.default_height = note.height;
100100

101+
this.update_title();
102+
103+
this.note.connect("title-changed", () => {
104+
this.update_title();
105+
});
106+
101107
this.connect("close-request", () => {
102108
if (this.deleted) return;
103109

@@ -151,6 +157,14 @@ export class Window extends Adw.ApplicationWindow {
151157
popover.add_child(this.selector, "notestyleswitcher");
152158
}
153159

160+
update_title() {
161+
if (this.note.title === "") {
162+
this.set_title(_("Untitled Note"));
163+
return;
164+
}
165+
this.set_title(this.note.title);
166+
}
167+
154168
last_revealer = false;
155169

156170
update_link(selected: boolean, text: string) {

0 commit comments

Comments
 (0)