forked from paperwm/PaperWM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winpropsPane.js
306 lines (273 loc) · 9.05 KB
/
winpropsPane.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
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
export const WinpropsPane = GObject.registerClass({
GTypeName: 'WinpropsPane',
Template: GLib.uri_resolve_relative(import.meta.url, './WinpropsPane.ui', GLib.UriFlags.NONE),
InternalChildren: [
'search',
'listbox',
'addButton',
'scrolledWindow',
],
Signals: {
'changed': {},
},
}, class WinpropsPane extends Gtk.Box {
_init(params = {}) {
super._init(params);
// define search box filter function (searches wm_class, title, and accelLabel)
this._listbox.set_filter_func(row => {
let search = this._search.get_text().toLowerCase();
let wmclass = row.winprop.wm_class?.toLowerCase() ?? '';
let title = row.winprop.title?.toLowerCase() ?? '';
let accelLabel = row._accelLabel.label?.toLowerCase() ?? '';
return wmclass.includes(search) || title.includes(search) || accelLabel.includes(search);
});
this._search.connect('changed', () => {
this._listbox.invalidate_filter();
});
this._expandedRow = null;
this.rows = [];
}
addWinprops(winprops) {
winprops.forEach(winprop => {
this._listbox.insert(this._createRow(winprop), -1);
});
}
_removeRow(row) {
this._listbox.remove(row);
let remove = this.rows.findIndex(r => r === row);
if (remove >= 0) {
this.rows.splice(remove, 1);
}
this.emit('changed');
}
_onAddButtonClicked() {
// first clear search text, otherwise won't be able to see new row
this._search.set_text('');
let row = this._createRow();
row.expanded = true;
this._listbox.insert(row, 0);
this._scrolledWindow.get_vadjustment().set_value(0);
}
_createRow(winprop) {
let wp = winprop ?? { wm_class: '' };
const row = new WinpropsRow({ winprop: wp });
this.rows.push(row);
row.connect('notify::expanded', row => this._onRowExpanded(row));
row.connect('row-deleted', row => this._removeRow(row));
row.connect('changed', () => this.emit('changed'));
return row;
}
_onRowActivated(list, row) {
if (!row.is_focus()) {
return;
}
row.expanded = !row.expanded;
}
_onRowExpanded(row) {
if (row.expanded) {
if (this._expandedRow) {
this._expandedRow.expanded = false;
}
this._expandedRow = row;
} else if (this._expandedRow === row) {
this._expandedRow = null;
}
}
});
export const WinpropsRow = GObject.registerClass({
GTypeName: 'WinpropsRow',
Template: GLib.uri_resolve_relative(import.meta.url, './WinpropsRow.ui', GLib.UriFlags.NONE),
InternalChildren: [
'header',
'descLabel',
'accelLabel',
'revealer',
'optionList',
'wmClass',
'title',
'scratchLayer',
'preferredWidth',
'deleteButton',
],
Properties: {
winprop: GObject.ParamSpec.jsobject(
'winprop',
'winprop',
'Winprop',
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY
),
expanded: GObject.ParamSpec.boolean(
'expanded',
'Expanded',
'Expanded',
GObject.ParamFlags.READWRITE,
false
),
},
Signals: {
'changed': {},
'row-deleted': {},
},
}, class WinpropsRow extends Gtk.ListBoxRow {
_init(params = {}) {
super._init(params);
// description label
this._setDescLabel();
// set the values to current state and connect to 'changed' signal
this._wmClass.set_text(this.winprop.wm_class ?? '');
this._wmClass.connect('changed', () => {
// check if null or empty (we still emit changed if wm_class is wiped)
this.checkHasWmClassOrTitle();
this.winprop.wm_class = this._wmClass.get_text();
this._setDescLabel();
this.emit('changed');
});
this._title.set_text(this.winprop.title ?? '');
this._title.connect('changed', () => {
this.checkHasWmClassOrTitle();
this.winprop.title = this._title.get_text();
this._setDescLabel();
this.emit('changed');
});
this._scratchLayer.set_active(this.winprop.scratch_layer ?? false);
this._scratchLayer.connect('state-set', () => {
let isActive = this._scratchLayer.get_active();
this.winprop.scratch_layer = isActive;
// if is active then disable the preferredWidth input
this._preferredWidth.set_sensitive(!isActive);
this.emit('changed');
});
this._preferredWidth.set_text(this.winprop.preferredWidth ?? '');
// if scratchLayer is active then users can't edit preferredWidth
this._preferredWidth.set_sensitive(!this.winprop.scratch_layer ?? true);
this._preferredWidth.connect('changed', () => {
// if has value, needs to be valid (have a value or unit)
if (this._preferredWidth.get_text()) {
let value = this._preferredWidth.get_text();
let digits = (value.match(/\d+/) ?? [null])[0];
let isPercent = /^.*%$/.test(value);
let isPixel = /^.*px$/.test(value);
// check had valid number
if (!digits) {
this._setError(this._preferredWidth);
}
// if no unit defined
else if (!isPercent && !isPixel) {
this._setError(this._preferredWidth);
}
else {
this._setError(this._preferredWidth, false);
this.winprop.preferredWidth = this._preferredWidth.get_text();
this.emit('changed');
}
} else {
// having no preferredWidth is valid
this._setError(this._preferredWidth, false);
delete this.winprop.preferredWidth;
this.emit('changed');
}
});
this._updateState();
}
/**
* Checks has an input for either wmClass or title.
* Sets 'error' cssClass is neither.
*/
checkHasWmClassOrTitle() {
if (!this._wmClass.get_text() && !this._title.get_text()) {
this._setError(this._wmClass);
this._setError(this._title);
return false;
} else {
this._setError(this._wmClass, false);
this._setError(this._title, false);
return true;
}
}
/**
* Get the wmClass if it exists, otherwise returns the title.
* @returns String
*/
getWmClassOrTitle() {
if (this.winprop.wm_class) {
return this.winprop.wm_class;
}
else if (this.winprop.title) {
return this.winprop.title;
}
else {
return '';
}
}
_setError(child, option = true) {
if (child) {
if (option) {
child.add_css_class('error');
} else {
child.remove_css_class('error');
}
}
}
get expanded() {
if (this._expanded === undefined)
this._expanded = false;
return this._expanded;
}
set expanded(value) {
if (this._expanded === value)
return;
this._expanded = value;
this.notify('expanded');
this._updateState();
}
_onDeleteButtonClicked() {
this.emit('row-deleted');
}
_onRowActivated(list, row) {
if (row.is_focus()) {
row.editing = !row.editing;
}
}
_setAccelLabel() {
let isScratch = this.winprop.scratch_layer ?? false;
let isPreferredWidth = this.winprop.preferredWidth || false;
if (isScratch) {
return 'scratch layer';
}
else if (isPreferredWidth) {
return 'preferred width';
} else {
return 'no setting';
}
}
/**
* Sets the description label for this row.
* @returns boolean
*/
_setDescLabel() {
// if wmClass, use that, otherwise use title (fallback)
if (this.winprop.wm_class) {
this._descLabel.label = this.winprop.wm_class;
}
else if (this.winprop.title) {
this._descLabel.label = this.winprop.title;
}
}
_updateState() {
GLib.idle_add(0, () => {
this._accelLabel.label = this._setAccelLabel();
if (this.expanded) {
this._accelLabel.hide();
this._revealer.reveal_child = true;
this.add_css_class('expanded');
} else {
this._accelLabel.show();
this._revealer.reveal_child = false;
this.remove_css_class('expanded');
}
});
}
});