-
Notifications
You must be signed in to change notification settings - Fork 6
/
prefsColumnView.js
174 lines (147 loc) · 6.32 KB
/
prefsColumnView.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
'use strict';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import Gio from 'gi://Gio';
import * as PrefsWindowPickableEntry from './prefsWindowPickableEntry.js';
import * as PrefsWidgets from './prefsWidgets.js';
import {PrefsUtils} from './utils/prefsUtils.js';
export const ColumnView = GObject.registerClass({
Signals: {
'activate': {
param_types: [Gtk.CheckButton, GObject.TYPE_OBJECT]
},
'row-deleted': {
param_types: [GObject.TYPE_OBJECT]
},
},
}, class ColumnView extends Gtk.Box {
_init(datalist, params = {}) {
super._init({
orientation: Gtk.Orientation.VERTICAL
});
datalist = datalist ? datalist : [];
this.datalist = datalist;
this._initUI();
this.updateView(datalist);
}
_initUI() {
this.model = new Gio.ListStore({ item_type: GObject.TYPE_OBJECT });
this.selectionModel = new Gtk.SingleSelection({ model: this.model });
this.view = new Gtk.ColumnView({
css_classes: ['view'],
// I feel it's ugly to set this to true
// show_column_separators: true
});
this.view.set_model(this.selectionModel);
const enabledColumn = PrefsWidgets.newColumnViewColumn('Enabled',
(factory, listItem) => {
const checkButton = new Gtk.CheckButton()
listItem.set_child(checkButton);
}, (factory, listItem) => {
const widget = listItem.get_child();
// item is the CloseWindowsWhitelist instance that is added into the model
const item = listItem.get_item();
// So we can get `enabled` value from `item` here
const enabled = item.enabled
widget.set_active(enabled);
widget.connect('notify::active', () => {
this.emit('activate', widget, item);
});
});
const operationColumn = PrefsWidgets.newColumnViewColumn('Operation',
(factory, listItem) => {
const button = PrefsWidgets.newRemoveButton();
listItem.set_child(button);
button.connect('clicked', () => {
const item = listItem.get_item();
this.emit('row-deleted', item);
});
}, null);
this.view.append_column(enabledColumn);
this.view.append_column(operationColumn);
// Add the ColumnView to the Box
this.append(this.view);
}
updateView(dataList) {
this.model.remove_all();
for(const item of dataList) {
this.model.append(item);
}
}
updateRow(settingName, keyName, keyValue, propertyName, value) {
const oldCloseWindowsRules = this._settings.get_string(settingName);
let oldCloseWindowsRulesObj = JSON.parse(oldCloseWindowsRules);
const rule = oldCloseWindowsRulesObj[keyValue];
rule[propertyName] = value;
const newCloseWindowsRules = JSON.stringify(oldCloseWindowsRulesObj);
this._settings.set_string(settingName, newCloseWindowsRules);
}
});
export const WhitelistColumnView = GObject.registerClass({
Signals: {},
Properties: {},
}, class WhitelistColumnView extends ColumnView {
_init(datalist) {
super._init(datalist, {});
const settingKey = 'close-windows-whitelist';
this._settings = PrefsUtils.getSettings();
const nameColumn = PrefsWidgets.newColumnViewColumn('Name',
null, (factory, listItem) => {
const item = listItem.get_item();
const name = item.name ? item.name : '';
const nameEntry = new PrefsWindowPickableEntry.WindowPickableEntry({
text: name,
tooltip_text: name,
pickConditionFunc: (() => {
return 'wm_class';
}).bind(this)
});
listItem.set_child(nameEntry);
nameEntry.connect('entry-edit-complete', (source, entry) => {
this.updateRow(settingKey, 'id', item.id, 'name', entry.get_text());
});
});
const closeWindowsColumn = PrefsWidgets.newColumnViewColumn('Close windows',
(factory, listItem) => {
const switcher = new Gtk.Switch({halign: Gtk.Align.START, valign: Gtk.Align.CENTER});
listItem.set_child(switcher);
}, (factory, listItem) => {
const widget = listItem.get_child();
const item = listItem.get_item();
const enableWhenCloseWindows = item.enableWhenCloseWindows
widget.set_active(enableWhenCloseWindows);
widget.connect('notify::active', (source) => {
this.updateRow(settingKey, 'id', item.id, 'enableWhenCloseWindows', source.get_active());
});
});
const logoffColumn = PrefsWidgets.newColumnViewColumn('Log Out, Reboot, Power Off',
(factory, listItem) => {
const switcher = new Gtk.Switch({halign: Gtk.Align.START, valign: Gtk.Align.CENTER});
listItem.set_child(switcher);
}, (factory, listItem) => {
const widget = listItem.get_child();
const item = listItem.get_item();
const enableWhenLogout = item.enableWhenLogout;
widget.set_active(enableWhenLogout);
widget.connect('notify::active', (source) => {
this.updateRow(settingKey, 'id', item.id, 'enableWhenLogout', source.get_active());
});
});
// The first column is assigned to Enabled column
let index = 1;
this.view.insert_column(index++, nameColumn);
this.view.insert_column(index++, closeWindowsColumn);
this.view.insert_column(index++, logoffColumn);
this.connect('activate', (source, checkButton, item) => {
const enabled = checkButton.get_active();
this.updateRow(settingKey, 'id', item.id, 'enabled', enabled);
});
this.connect('row-deleted', (source, item) => {
const oldCloseWindowsRules = this._settings.get_string(settingKey);
let oldCloseWindowsRulesObj = JSON.parse(oldCloseWindowsRules);
delete oldCloseWindowsRulesObj[item.id];
const newCloseWindowsRules = JSON.stringify(oldCloseWindowsRulesObj);
this._settings.set_string(settingKey, newCloseWindowsRules);
});
}
});