forked from GittyMac/PanelNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefs.js
More file actions
317 lines (273 loc) · 10.3 KB
/
prefs.js
File metadata and controls
317 lines (273 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import Gtk from "gi://Gtk";
import Adw from "gi://Adw";
import { ExtensionPreferences } from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";
function rangesOverlap(s1, e1, s2, e2) {
function normalize(s, e) {
if (s < e) return [[s, e]];
return [[s, 1440], [0, e]];
}
let r1 = normalize(s1, e1);
let r2 = normalize(s2, e2);
for (let [a1, b1] of r1) {
for (let [a2, b2] of r2) {
if (a1 < b2 && a2 < b1) return true;
}
}
return false;
}
function hasOverlaps(entries) {
for (let i = 0; i < entries.length; i++) {
for (let j = i + 1; j < entries.length; j++) {
let s1 = entries[i][1] * 60 + entries[i][2];
let e1 = entries[i][3] * 60 + entries[i][4];
let s2 = entries[j][1] * 60 + entries[j][2];
let e2 = entries[j][3] * 60 + entries[j][4];
if (rangesOverlap(s1, e1, s2, e2)) return true;
}
}
return false;
}
function is24hFormat() {
let desktopSettings = new Gio.Settings({ schema_id: "org.gnome.desktop.interface" });
return desktopSettings.get_string("clock-format") === "24h";
}
// Convert 24h hour to 12h display hour + AM/PM index
function to12h(hour24) {
let period = hour24 < 12 ? 0 : 1; // 0=AM, 1=PM
let h12 = hour24 % 12;
if (h12 === 0) h12 = 12;
return [h12, period];
}
// Convert 12h display hour + AM/PM back to 24h
function to24h(hour12, period) {
if (period === 0) // AM
return hour12 === 12 ? 0 : hour12;
else // PM
return hour12 === 12 ? 12 : hour12 + 12;
}
export default class PanelNotePreferences extends ExtensionPreferences {
fillPreferencesWindow(window) {
let buildable = new Gtk.Builder();
buildable.add_from_file(this.dir.get_path() + "/prefs.xml");
let settings = this.getSettings();
let use24h = is24hFormat();
// Position bindings
settings.bind(
"enable-positioning",
buildable.get_object("field_enablepositioning"),
"active",
Gio.SettingsBindFlags.DEFAULT
);
settings.bind(
"enable-positioning",
buildable.get_object("row_position_area"),
"sensitive",
Gio.SettingsBindFlags.DEFAULT
);
settings.bind(
"enable-positioning",
buildable.get_object("row_position_index"),
"sensitive",
Gio.SettingsBindFlags.DEFAULT
);
settings.bind(
"position",
buildable.get_object("field_position"),
"active",
Gio.SettingsBindFlags.DEFAULT
);
settings.bind(
"position-number",
buildable.get_object("field_positionnumber"),
"value",
Gio.SettingsBindFlags.DEFAULT
);
// Main note
settings.bind(
"note",
buildable.get_object("field_main_note"),
"text",
Gio.SettingsBindFlags.DEFAULT
);
// Time-based notes
let groupTimeNotes = buildable.get_object("group_time_notes");
let rowWidgets = [];
let isUpdating = false;
let addButton = new Gtk.Button({
label: "Add Scheduled Note",
halign: Gtk.Align.CENTER,
valign: Gtk.Align.CENTER,
margin_top: 6,
margin_bottom: 6,
});
groupTimeNotes.add(addButton);
function saveAllRows() {
if (isUpdating) return;
isUpdating = true;
let entries = rowWidgets.map(w => w.getValues());
if (hasOverlaps(entries)) {
for (let w of rowWidgets) {
w.row.add_css_class("error");
}
isUpdating = false;
return;
}
for (let w of rowWidgets) {
w.row.remove_css_class("error");
}
let variant = new GLib.Variant("a(siiii)", entries);
settings.set_value("time-notes", variant);
isUpdating = false;
}
function makeHourSpin(lower, upper, value) {
return new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({ lower, upper, step_increment: 1 }),
value,
numeric: true,
wrap: true,
});
}
function makeMinSpin(value) {
return new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({ lower: 0, upper: 59, step_increment: 1 }),
value,
numeric: true,
wrap: true,
});
}
function makePeriodCombo(activeIdx) {
let combo = new Gtk.ComboBoxText();
combo.append("am", "AM");
combo.append("pm", "PM");
combo.set_active_id(activeIdx === 1 ? "pm" : "am");
return combo;
}
function buildTimeNoteRow(index, entryData) {
let row = new Adw.ExpanderRow({
title: `Scheduled Note ${index + 1}`,
});
// Note text entry
let noteEntry = new Gtk.Entry({
text: entryData[0] || "",
hexpand: true,
placeholder_text: "Enter note text…",
});
let noteRow = new Adw.ActionRow({ title: "Note Text" });
noteRow.add_suffix(noteEntry);
row.add_row(noteRow);
// Start time
let startHour24 = entryData[1] || 9;
let startMin = entryData[2] || 0;
let startHourSpin, startPeriodCombo;
if (use24h) {
startHourSpin = makeHourSpin(0, 23, startHour24);
} else {
let [h12, period] = to12h(startHour24);
startHourSpin = makeHourSpin(1, 12, h12);
startPeriodCombo = makePeriodCombo(period);
}
let startMinSpin = makeMinSpin(startMin);
let startBox = new Gtk.Box({ spacing: 4, halign: Gtk.Align.END });
startBox.append(startHourSpin);
startBox.append(new Gtk.Label({ label: ":" }));
startBox.append(startMinSpin);
if (!use24h) startBox.append(startPeriodCombo);
let startRow = new Adw.ActionRow({ title: "Starts At" });
startRow.add_suffix(startBox);
row.add_row(startRow);
// End time
let endHour24 = entryData[3] || 17;
let endMin = entryData[4] || 0;
let endHourSpin, endPeriodCombo;
if (use24h) {
endHourSpin = makeHourSpin(0, 23, endHour24);
} else {
let [h12, period] = to12h(endHour24);
endHourSpin = makeHourSpin(1, 12, h12);
endPeriodCombo = makePeriodCombo(period);
}
let endMinSpin = makeMinSpin(endMin);
let endBox = new Gtk.Box({ spacing: 4, halign: Gtk.Align.END });
endBox.append(endHourSpin);
endBox.append(new Gtk.Label({ label: ":" }));
endBox.append(endMinSpin);
if (!use24h) endBox.append(endPeriodCombo);
let endRow = new Adw.ActionRow({ title: "Ends At" });
endRow.add_suffix(endBox);
row.add_row(endRow);
// Remove button
let removeButton = new Gtk.Button({
label: "Remove Note",
css_classes: ["destructive-action"],
halign: Gtk.Align.END,
margin_top: 6,
margin_bottom: 6,
});
let removeRow = new Adw.ActionRow();
removeRow.add_suffix(removeButton);
row.add_row(removeRow);
removeButton.connect("clicked", () => {
groupTimeNotes.remove(row);
rowWidgets = rowWidgets.filter(w => w.row !== row);
saveAllRows();
});
// Connect change signals
noteEntry.connect("changed", () => saveAllRows());
startHourSpin.connect("value-changed", () => saveAllRows());
startMinSpin.connect("value-changed", () => saveAllRows());
endHourSpin.connect("value-changed", () => saveAllRows());
endMinSpin.connect("value-changed", () => saveAllRows());
if (!use24h) {
startPeriodCombo.connect("changed", () => saveAllRows());
endPeriodCombo.connect("changed", () => saveAllRows());
}
function getHour24(spin, combo) {
let h = spin.get_value_as_int();
if (use24h) return h;
let period = combo.get_active_id() === "pm" ? 1 : 0;
return to24h(h, period);
}
return {
row,
getValues: () => [
noteEntry.get_text(),
getHour24(startHourSpin, startPeriodCombo),
startMinSpin.get_value_as_int(),
getHour24(endHourSpin, endPeriodCombo),
endMinSpin.get_value_as_int(),
],
};
}
function rebuildList() {
for (let w of rowWidgets) {
groupTimeNotes.remove(w.row);
}
rowWidgets = [];
let entries = settings.get_value("time-notes").deep_unpack();
isUpdating = true;
for (let i = 0; i < entries.length; i++) {
let widget = buildTimeNoteRow(i, entries[i]);
groupTimeNotes.remove(addButton);
groupTimeNotes.add(widget.row);
groupTimeNotes.add(addButton);
rowWidgets.push(widget);
}
isUpdating = false;
}
addButton.connect("clicked", () => {
let current = settings.get_value("time-notes").deep_unpack();
current.push(["", 9, 0, 17, 0]);
let variant = new GLib.Variant("a(siiii)", current);
settings.set_value("time-notes", variant);
rebuildList();
});
settings.connect("changed::time-notes", () => {
if (!isUpdating) rebuildList();
});
rebuildList();
window.search_enabled = true;
window.add(buildable.get_object("page_basic"));
}
}