-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenuConfigWidget.js
397 lines (330 loc) · 12.5 KB
/
menuConfigWidget.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
/*jshint indent: 2*/
/*jshint camelcase: true*/
/*jshint immed: true*/
/*jshint curly: true*/
const Gtk = imports.gi.Gtk;
const GObject = imports.gi.GObject;
const Lang = imports.lang;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = new Me.imports.utils.Utils();
const MenuConfigWidget = new GObject.Class({
Name: 'SimpleMenu.Prefs.MenuConfigWidget',
GTypeName: 'SimpleMenuMenuConfigWidget',
Extends: Gtk.Grid,
_menuConfig: {},
_entries: [],
_selectedEntry: null,
_init: function(params) {
this.parent({margin_left: 20, margin_right: 5, column_homogeneous: true });
this._menuConfig = Utils.getParameter(Utils.SIMPLE_MENU_ENTRY);
let row = 0;
// Keybinding
this.attach(this._getKeybindingEntry(), 0, row, 5, 1);
row++;
// position combo
this.attach(new Gtk.Label({
halign: Gtk.Align.START,
label: "PanelButton position",
}), 0, row, 2, 1);
let cb = this._getComboBox([GObject.TYPE_STRING], [["left"], ["center left"], ["center right"], ["right"]], Utils.getString(Utils.SIMPLE_MENU_POSITION, "right"));
cb.connect('changed',
Lang.bind(this, function(combo) {
let [success, iter] = combo.get_active_iter();
if (!success) {
return;
}
Utils.setParameter(Utils.SIMPLE_MENU_POSITION, combo.get_model().get_value(iter, 0));
})
);
this.attach(cb, 3, row, 2, 1);
row++;
// terminal to execute
this.attach(new Gtk.Label({
halign: Gtk.Align.START,
label: "Terminal command",
}), 0, row, 2, 1);
let terminalName = new Gtk.Entry({ text: Utils.getString(Utils.SIMPLE_MENU_TERMINAL, "gnome-terminal" ) });
terminalName.connect("changed",
Lang.bind(this, function(input) {
Utils.setParameter(Utils.SIMPLE_MENU_TERMINAL, input.text);
})
);
this.attach(terminalName, 3, row, 2, 1);
row++;
// menu entries
this.attach(this._generateMenuEntryList(), 0, row, 5, 1);
row++;
},
_getComboBox: function( types, values, selectedValue) {
let model = new Gtk.ListStore();
model.set_column_types(types);
let ret = new Gtk.ComboBox({
model: model,
hexpand: true,
margin_top: 6,
margin_bottom: 6
});
ret.get_style_context().add_class(Gtk.STYLE_CLASS_RAISED);
let renderer = new Gtk.CellRendererText();
ret.pack_start(renderer, true);
ret.add_attribute(renderer, 'text', 0);
let numValues = values.length;
//[GObject.TYPE_STRING, GObject.TYPE_INT]);
let valueDefinition = [];
for (let i = 0; i < types.length; i++) {
valueDefinition.push(i);
}
for (let i = 0; i < numValues; i++ ) {
let iter = model.append();
model.set(iter, [0], values[i]);
if (values[i][0] == selectedValue) {
ret.set_active_iter(iter);
}
}
return ret;
},
_getKeybindingEntry: function() {
let model = new Gtk.ListStore();
model.set_column_types([ GObject.TYPE_STRING, GObject.TYPE_STRING, GObject.TYPE_INT, GObject.TYPE_INT ]);
let [key, mods] = Gtk.accelerator_parse(Utils.get_strv(Utils.SIMPLE_MENU_KEY_BINDING, null)[0]);
let iter = model.insert(10);
model.set(iter, [0, 1, 2, 3], [Utils.SIMPLE_MENU_KEY_BINDING, "Key binding", mods, key ]);
let treeView = new Gtk.TreeView({
model: model,
headers_visible: false
});
let cellrend = new Gtk.CellRendererText();
let col = new Gtk.TreeViewColumn({ 'title': 'Action', 'expand': true });
col.pack_start(cellrend, true);
col.add_attribute(cellrend, 'text', 1);
treeView.append_column(col);
// keybinding column
cellrend = new Gtk.CellRendererAccel({
'editable': true,
'accel-mode': Gtk.CellRendererAccelMode.GTK
});
cellrend.connect('accel-edited', function(rend, iter, key, mods) {
let value = Gtk.accelerator_name(key, mods);
let [succ, iterator ] = model.get_iter_from_string(iter);
if(!succ) {
throw new Error("Error updating Keybinding");
}
let name = model.get_value(iterator, 0);
model.set(iterator, [ 2, 3], [ mods, key ]);
Utils.set_strv(name, [value]);
});
col = new Gtk.TreeViewColumn({'title': 'Modify'});
col.pack_end(cellrend, false);
col.add_attribute(cellrend, 'accel-mods', 2);
col.add_attribute(cellrend, 'accel-key', 3);
treeView.append_column(col);
return treeView;
},
_addEntry: function(name) {
let iter = this._treeModel.append();
this._treeModel.set(iter, [0], [ name ]);
let entry = {name: name, listElement: iter};
this._entries.push(entry);
return entry;
},
_generateMenuEntryList: function() {
this._entries = [];
// list all entries
this._treeModel = new Gtk.ListStore();
this._treeModel.set_column_types([GObject.TYPE_STRING]);
let treeView = new Gtk.TreeView({model: this._treeModel, headers_visible: false});
treeView.get_style_context().add_class(Gtk.STYLE_CLASS_RAISED);
let column = new Gtk.TreeViewColumn({ min_width: 150 });
let renderer = new Gtk.CellRendererText();
column.pack_start(renderer, true);
column.add_attribute(renderer, 'text', 0);
treeView.append_column(column);
treeView.get_selection().connect("changed",
Lang.bind(this, function(selection) {
let s = selection.get_selected();
if (!s[0]) {
return;
}
let selectedValue = s[1].get_value(s[2], 0);
if (this._selectedEntry !== null && this._selectedEntry == selectedValue) {
return;
}
this._selectedEntry = selectedValue;
this._removeButton.set_sensitive(true);
this._updateEntryContainer(this._createEntryWidget(selectedValue));
})
);
// sort the menu entries by position
var entries = Object.getOwnPropertyNames(this._menuConfig);
let size = entries.length;
// sort entries by _menuconfig.foobar.position
for (let i=0; i < size; i++) {
entries[i] = {
name: entries[i],
position: this._menuConfig[entries[i]].position
};
}
entries.sort(Utils.sortMenuEntries);
for(let i=0; i < entries.length; i++) {
this._addEntry(entries[i].name);
}
// toolbar to add/remove entries
let toolbar = new Gtk.Toolbar({});
toolbar.set_icon_size(Gtk.IconSize.MENU);
toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_INLINE_TOOLBAR);
this._removeButton = new Gtk.ToolButton( {stock_id: Gtk.STOCK_REMOVE} );
this._removeButton.set_sensitive(false);
this._removeButton.set_tooltip_text("Remove an existing application");
this._removeButton.connect("clicked",
Lang.bind(this, function() {
let delme = this._selectedEntry;
let dialog = new Gtk.MessageDialog({
modal: true,
message_type: Gtk.MessageType.QUESTION,
title: "Delete entry '" + delme + "'?",
text: "Are you sure to delete the configuration for '" + delme + "'?"
});
dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL);
dialog.add_button(Gtk.STOCK_DELETE, Gtk.ResponseType.DELETE_EVENT);
dialog.connect("response",
Lang.bind(this, function(dialog, responseType) {
if (responseType != Gtk.ResponseType.DELETE_EVENT) {
return;
}
// remove the widget
if (this._entryContainer.get_child()) {
this._entryContainer.get_child().destroy();
}
// remove the list entry
let entryObject = this._findListEntryByName(delme);
if (entryObject == null) {
global.log("can not find listEntry by name " + delme);
return;
}
this._treeModel.remove(entryObject.listElement);
// unset the parameter
Utils.unsetParameter(Utils.SIMPLE_MENU_ENTRY + "." + delme);
// remove the entry from _apps array
this._entries.pop(entryObject);
})
);
dialog.run();
dialog.destroy();
})
);
let addButton = new Gtk.ToolButton({ stock_id: Gtk.STOCK_ADD });
addButton.connect("clicked",
Lang.bind(this, function() {
let obj = this._addEntry( "Entry " + this._entries.length);
this._selectedEntry = obj.name;
treeView.get_selection().select_iter(obj.listElement);
this._updateEntryContainer(this._createEntryWidget(this._selectedEntry));
})
);
this._saveButton = new Gtk.ToolButton({stock_id: Gtk.STOCK_SAVE});
//this._saveButton.set_use_stock(true);
this._saveButton.set_tooltip_text("'Entries' config is not saved automatically.");
this._saveButton.connect("clicked", function() {
Utils.saveSettings();
});
toolbar.insert(this._removeButton, 0);
toolbar.insert(addButton, 1);
toolbar.insert(this._saveButton, 2);
let leftPanel = new Gtk.Box({orientation: Gtk.Orientation.VERTICAL, margin_right: 5});
leftPanel.pack_start(toolbar, false, false, 0);
leftPanel.pack_start(treeView, true, true, 0);
// entry details
this._entryContainer = new Gtk.Frame({ hexpand: true});
this._entryContainer.add(new Gtk.Label({label: "Select an application to configure using the list on the left side." }));
let scroll = new Gtk.ScrolledWindow({
'vexpand': true
});
scroll.add_with_viewport(this._entryContainer);
let ret = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, margin_top: 5, margin_bottom: 5 });
ret.add(leftPanel);
ret.add(scroll);
return ret;
},
_findListEntryByName: function(name) {
for (let i=0; i<this._entries.length; i++) {
if (this._entries[i].name == name) {
return this._entries[i];
}
}
return null;
},
_updateEntryContainer: function(widget) {
if (this._entryContainer.get_child()) {
this._entryContainer.get_child().destroy();
}
this._entryContainer.add(widget);
this._entryContainer.show_all();
},
_createEntryWidget: function(name) {
let configBase = Utils.SIMPLE_MENU_ENTRY + "." + name;
let config = Utils.getParameter(configBase);
if (typeof config == "undefined") {
config = {display: name, command: "", terminal: false, position: 1};
} else {
if (!config.display) {
config.display = name;
}
if (!config.command) {
config.command = "";
}
if (typeof config.terminal == "undefined") {
config.terminal = false;
}
if (typeof config.position == "undefined") {
config.position = 1;
}
}
let grid = new Gtk.Grid({ column_homogeneous: true, margin: 5});
let row = 0;
grid.attach(new Gtk.Label({ label: "Display name", xalign: 0}), 0, row, 1, 1);
let displayEntry = new Gtk.Entry({ text: config.display });
displayEntry.connect("changed", Lang.bind(this, function(input) {
let value = input.text;
if (value.indexOf(".") != -1) {
value = value.replace(/\./g,"-");
}
Utils.setParameter(configBase + ".display", input.text);
let entry = this._findListEntryByName(this._selectedEntry);
entry.name = value;
this._selectedEntry = value;
this._treeModel.set(entry.listElement, [0], [ value ]);
}));
grid.attach(displayEntry, 2, row, 2, 1);
row++;
grid.attach(new Gtk.Label({ label: "Command", xalign: 0}), 0, row, 1, 1);
let commandEntry = new Gtk.Entry({ text: config.command });
commandEntry.connect("changed", Lang.bind(this, function(input) {
Utils.setParameter(configBase + ".command", input.text);
}));
grid.attach(commandEntry, 2, row, 2, 1);
row++;
// position inside the created menu
grid.attach(new Gtk.Label({ label: "Position", xalign: 0}), 0, row, 1, 1);
let position = new Gtk.Scale({digits: 0, sensitive: true, orientation: Gtk.Orientation.HORIZONTAL, margin_right: 6, margin_left: 6});
position.set_range(0, 20);
position.set_value(config.position);
position.connect("value_changed", Lang.bind(position, function() {
Utils.setParameter(configBase + ".position", this.get_value());
}));
grid.attach(position, 2, row, 2, 1);
row++;
grid.attach(new Gtk.Label({ label: "Run in terminal", xalign: 0}), 0, row, 1, 1);
let sw = new Gtk.Switch({ sensitive: true, halign: Gtk.Align.END });
sw.set_active(config.terminal);
sw.connect("notify::active",
function(obj) {
Utils.setParameter(configBase + ".terminal", obj.get_active());
});
grid.attach(sw, 2, row, 2, 1);
row++;
let ret = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL });
ret.pack_start(grid, false, false, 0);
return ret;
}
});