forked from SUPERCILEX/gnome-clipboard-history
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefs.js
431 lines (394 loc) · 11.7 KB
/
prefs.js
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import Gio from 'gi://Gio';
import Adw from 'gi://Adw';
import {
ExtensionPreferences,
gettext as _,
} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
import Fields from './settingsFields.js';
export default class ClipboardHistoryPrefs extends ExtensionPreferences {
// fillPreferencesWindow() is passed a Adw.PreferencesWindow,
// we need to wrap our widget in a Adw.PreferencesPage and Adw.PreferencesGroup
// ourselves.
// It would be great to port the preferences to standard Adw widgets.
// https://gjs.guide/extensions/development/preferences.html#prefs-js
fillPreferencesWindow(window) {
const settings = this.getSettings();
const main = new Gtk.Grid({
margin_top: 10,
margin_bottom: 10,
margin_start: 10,
margin_end: 10,
row_spacing: 12,
column_spacing: 18,
column_homogeneous: false,
row_homogeneous: false,
});
const field_size = new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({
lower: 1,
upper: 100_000,
step_increment: 100,
}),
});
const window_width_percentage = new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({
lower: 0,
upper: 100,
step_increment: 5,
}),
});
const field_cache_size = new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({
lower: 1,
upper: 1024,
step_increment: 5,
}),
});
const field_topbar_preview_size = new Gtk.SpinButton({
adjustment: new Gtk.Adjustment({
lower: 1,
upper: 100,
step_increment: 10,
}),
});
const field_display_mode = new Gtk.ComboBox({
model: this._create_display_mode_options(),
});
const rendererText = new Gtk.CellRendererText();
field_display_mode.pack_start(rendererText, false);
field_display_mode.add_attribute(rendererText, 'text', 0);
const field_disable_down_arrow = new Gtk.Switch();
const field_cache_disable = new Gtk.Switch();
const field_notification_toggle = new Gtk.Switch();
const field_confirm_clear_toggle = new Gtk.Switch();
const field_strip_text = new Gtk.Switch();
const field_paste_on_selection = new Gtk.Switch();
const field_process_primary_selection = new Gtk.Switch();
const field_move_item_first = new Gtk.Switch();
const field_keybinding = createKeybindingWidget(settings);
addKeybinding(
field_keybinding.model,
settings,
'toggle-menu',
_('Toggle the menu'),
);
addKeybinding(
field_keybinding.model,
settings,
'clear-history',
_('Clear history'),
);
addKeybinding(
field_keybinding.model,
settings,
'prev-entry',
_('Previous entry'),
);
addKeybinding(
field_keybinding.model,
settings,
'next-entry',
_('Next entry'),
);
addKeybinding(
field_keybinding.model,
settings,
'toggle-private-mode',
_('Toggle private mode'),
);
const field_keybinding_activation = new Gtk.Switch();
field_keybinding_activation.connect('notify::active', (widget) => {
field_keybinding.set_sensitive(widget.active);
});
const sizeLabel = new Gtk.Label({
label: _('Max number of items'),
hexpand: true,
halign: Gtk.Align.START,
});
const windowWidthPercentageLabel = new Gtk.Label({
label: _('Window width (%)'),
hexpand: true,
halign: Gtk.Align.START,
});
const cacheSizeLabel = new Gtk.Label({
label: _('Max clipboard history size (MiB)'),
hexpand: true,
halign: Gtk.Align.START,
});
const cacheDisableLabel = new Gtk.Label({
label: _('Only save favorites to disk'),
hexpand: true,
halign: Gtk.Align.START,
});
const notificationLabel = new Gtk.Label({
label: _('Show notification on copy'),
hexpand: true,
halign: Gtk.Align.START,
});
const confirmClearLabel = new Gtk.Label({
label: _('Ask for confirmation before clearing history'),
hexpand: true,
halign: Gtk.Align.START,
});
const moveFirstLabel = new Gtk.Label({
label: _('Move previously copied items to the top'),
hexpand: true,
halign: Gtk.Align.START,
});
const keybindingLabel = new Gtk.Label({
label: _('Keyboard shortcuts'),
hexpand: true,
halign: Gtk.Align.START,
});
const topbarPreviewLabel = new Gtk.Label({
label: _('Number of characters in status bar'),
hexpand: true,
halign: Gtk.Align.START,
});
const displayModeLabel = new Gtk.Label({
label: _('What to show in status bar'),
hexpand: true,
halign: Gtk.Align.START,
});
const disableDownArrowLabel = new Gtk.Label({
label: _('Remove down arrow in status bar'),
hexpand: true,
halign: Gtk.Align.START,
});
const stripTextLabel = new Gtk.Label({
label: _('Remove whitespace around text'),
hexpand: true,
halign: Gtk.Align.START,
});
const pasteOnSelectionLabel = new Gtk.Label({
label: _('Paste on selection'),
hexpand: true,
halign: Gtk.Align.START,
});
const processPrimarySelection = new Gtk.Label({
label: _('Save selected text to history'),
hexpand: true,
halign: Gtk.Align.START,
});
const addRow = ((main) => {
let row = 0;
return (label, input) => {
let inputWidget = input;
if (input instanceof Gtk.Switch) {
inputWidget = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
});
inputWidget.append(input);
}
if (label) {
main.attach(label, 0, row, 1, 1);
main.attach(inputWidget, 1, row, 1, 1);
} else {
main.attach(inputWidget, 0, row, 2, 1);
}
row++;
};
})(main);
addRow(windowWidthPercentageLabel, window_width_percentage);
addRow(sizeLabel, field_size);
addRow(cacheSizeLabel, field_cache_size);
addRow(cacheDisableLabel, field_cache_disable);
addRow(moveFirstLabel, field_move_item_first);
addRow(stripTextLabel, field_strip_text);
addRow(pasteOnSelectionLabel, field_paste_on_selection);
addRow(processPrimarySelection, field_process_primary_selection);
addRow(displayModeLabel, field_display_mode);
addRow(disableDownArrowLabel, field_disable_down_arrow);
addRow(topbarPreviewLabel, field_topbar_preview_size);
addRow(notificationLabel, field_notification_toggle);
addRow(confirmClearLabel, field_confirm_clear_toggle);
addRow(keybindingLabel, field_keybinding_activation);
addRow(null, field_keybinding);
settings.bind(
Fields.HISTORY_SIZE,
field_size,
'value',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.WINDOW_WIDTH_PERCENTAGE,
window_width_percentage,
'value',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.CACHE_FILE_SIZE,
field_cache_size,
'value',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.CACHE_ONLY_FAVORITES,
field_cache_disable,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.NOTIFY_ON_COPY,
field_notification_toggle,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.CONFIRM_ON_CLEAR,
field_confirm_clear_toggle,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.MOVE_ITEM_FIRST,
field_move_item_first,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.TOPBAR_DISPLAY_MODE_ID,
field_display_mode,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.DISABLE_DOWN_ARROW,
field_disable_down_arrow,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.TOPBAR_PREVIEW_SIZE,
field_topbar_preview_size,
'value',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.STRIP_TEXT,
field_strip_text,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.PASTE_ON_SELECTION,
field_paste_on_selection,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.PROCESS_PRIMARY_SELECTION,
field_process_primary_selection,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
settings.bind(
Fields.ENABLE_KEYBINDING,
field_keybinding_activation,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
const group = new Adw.PreferencesGroup();
group.add(main);
const page = new Adw.PreferencesPage();
page.add(group);
window.add(page);
}
_create_display_mode_options() {
const options = [
{ name: _('Icon') },
{ name: _('Clipboard contents') },
{ name: _('Both') },
{ name: _('Neither') },
];
const liststore = new Gtk.ListStore();
liststore.set_column_types([GObject.TYPE_STRING]);
for (let i = 0; i < options.length; i++) {
const option = options[i];
const iter = liststore.append();
liststore.set(iter, [0], [option.name]);
}
return liststore;
}
}
//binding widgets
//////////////////////////////////
const COLUMN_ID = 0;
const COLUMN_DESCRIPTION = 1;
const COLUMN_KEY = 2;
const COLUMN_MODS = 3;
function addKeybinding(model, settings, id, description) {
// Get the current accelerator.
const accelerator = settings.get_strv(id)[0];
let key, mods;
if (accelerator == null) {
[key, mods] = [0, 0];
} else {
[, key, mods] = Gtk.accelerator_parse(settings.get_strv(id)[0]);
}
// Add a row for the keybinding.
const row = model.insert(100); // Erm...
model.set(
row,
[COLUMN_ID, COLUMN_DESCRIPTION, COLUMN_KEY, COLUMN_MODS],
[id, description, key, mods],
);
}
function createKeybindingWidget(Settings) {
const model = new Gtk.ListStore();
model.set_column_types([
GObject.TYPE_STRING, // COLUMN_ID
GObject.TYPE_STRING, // COLUMN_DESCRIPTION
GObject.TYPE_INT, // COLUMN_KEY
GObject.TYPE_INT,
]); // COLUMN_MODS
const treeView = new Gtk.TreeView();
treeView.model = model;
treeView.headers_visible = false;
let column, renderer;
// Description column.
renderer = new Gtk.CellRendererText();
column = new Gtk.TreeViewColumn();
column.expand = true;
column.pack_start(renderer, true);
column.add_attribute(renderer, 'text', COLUMN_DESCRIPTION);
treeView.append_column(column);
// Key binding column.
renderer = new Gtk.CellRendererAccel();
renderer.accel_mode = Gtk.CellRendererAccelMode.GTK;
renderer.editable = true;
renderer.connect(
'accel-edited',
function (renderer, path, key, mods, hwCode) {
const [ok, iter] = model.get_iter_from_string(path);
if (!ok) {
return;
}
// Update the UI.
model.set(iter, [COLUMN_KEY, COLUMN_MODS], [key, mods]);
// Update the stored setting.
const id = model.get_value(iter, COLUMN_ID);
const accelString = Gtk.accelerator_name(key, mods);
Settings.set_strv(id, [accelString]);
},
);
renderer.connect('accel-cleared', function (renderer, path) {
const [ok, iter] = model.get_iter_from_string(path);
if (!ok) {
return;
}
// Update the UI.
model.set(iter, [COLUMN_KEY, COLUMN_MODS], [0, 0]);
// Update the stored setting.
const id = model.get_value(iter, COLUMN_ID);
Settings.set_strv(id, []);
});
column = new Gtk.TreeViewColumn();
column.pack_end(renderer, false);
column.add_attribute(renderer, 'accel-key', COLUMN_KEY);
column.add_attribute(renderer, 'accel-mods', COLUMN_MODS);
treeView.append_column(column);
return treeView;
}