-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprefs.js
118 lines (96 loc) · 3.63 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
'use strict';
const Gtk = imports.gi.Gtk;
const GObject = imports.gi.GObject;
const ExtensionUtils = imports.misc.extensionUtils;
const COLUMN_ID = 0;
const COLUMN_DESC = 1;
const COLUMN_KEY = 2;
const COLUMN_MODS = 3;
// eslint-disable-next-line no-unused-vars
function init() {}
// eslint-disable-next-line no-unused-vars
function buildPrefsWidget() {
const settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.multi-monitor-swap');
const prefsWidget = new Gtk.Grid({
margin_top: 20,
margin_bottom: 20,
margin_start: 20,
margin_end: 20,
row_spacing: 20,
});
const keybindingLabel = new Gtk.Label({
label: 'Keyboard shortcuts',
hexpand: true,
halign: Gtk.Align.START,
});
prefsWidget.attach(keybindingLabel, 0, 0, 1, 1);
// Setup the store
let store = new Gtk.ListStore();
store.set_column_types([
GObject.TYPE_STRING, // COLUMN_ID
GObject.TYPE_STRING, // COLUMN_DESC
GObject.TYPE_INT, // COLUMN_KEY
GObject.TYPE_INT, // COLUMN_MODS
]);
addKeybinding(store, settings, 'swap-up', 'Swap up');
addKeybinding(store, settings, 'swap-down', 'Swap down');
addKeybinding(store, settings, 'swap-left', 'Swap left');
addKeybinding(store, settings, 'swap-right', 'Swap right');
addKeybinding(store, settings, 'focus-up', 'Focus up');
addKeybinding(store, settings, 'focus-down', 'Focus down');
addKeybinding(store, settings, 'focus-left', 'Focus left');
addKeybinding(store, settings, 'focus-right', 'Focus right');
addKeybinding(store, settings, 'select-up', 'Select up');
addKeybinding(store, settings, 'select-down', 'Select down');
let treeView = new Gtk.TreeView();
treeView.model = store;
treeView.headers_visible = false;
// Desc text
let renderer, column;
renderer = new Gtk.CellRendererText();
column = new Gtk.TreeViewColumn();
column.expand = true;
column.pack_start(renderer, true);
column.add_attribute(renderer, 'text', COLUMN_DESC);
treeView.append_column(column);
// Key binding
renderer = new Gtk.CellRendererAccel();
renderer.accel_mode = Gtk.CellRendererAccelMode.GTK;
renderer.editable = true;
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);
prefsWidget.attach(treeView, 0, 1, 2, 1);
// Events
renderer.connect('accel-edited', (_, path, key, mods, __) => {
let [ok, iter] = store.get_iter_from_string(path);
if (!ok)
return;
store.set(iter, [COLUMN_KEY, COLUMN_MODS], [key, mods]);
let id = store.get_value(iter, COLUMN_ID);
let accelString = Gtk.accelerator_name(key, mods);
settings.set_strv(id, [accelString]);
});
renderer.connect('accel-cleared', (_, path) => {
let [ok, iter] = store.get_iter_from_string(path);
if (!ok)
return;
store.set(iter, [COLUMN_KEY, COLUMN_MODS], [0, 0]);
let id = store.get_value(iter, COLUMN_ID);
settings.set_strv(id, []);
});
return prefsWidget;
}
function addKeybinding(model, settings, id, description) {
// Get the current accelerator.
let accelerator = settings.get_strv(id)[0];
let key, mods;
if (!accelerator)
[key, mods] = [0, 0];
else
[, key, mods] = Gtk.accelerator_parse(accelerator);
let row = model.insert(100);
model.set(row, [COLUMN_ID, COLUMN_DESC, COLUMN_KEY, COLUMN_MODS], [id, description, key, mods]);
}