-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsettingsWidget.js
246 lines (192 loc) · 7.18 KB
/
settingsWidget.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
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import {gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
const BOX_PADDING = 8;
const MARGIN_BOTTOM = 8;
const getMarginAll = (value) => ({
margin_start: value,
margin_top: value,
margin_end: value,
margin_bottom: value,
});
const addToBox = (box, element) => {
box.append(element);
}
export default GObject.registerClass(
class SettingsWidget extends Gtk.Box {
_init(settings) {
super._init();
this._settings = settings;
this.set_orientation(Gtk.Orientation.VERTICAL);
addToBox(this, this._getIndicatorSettingsFrame());
this._getDevicesFrame().then((element) => {
addToBox(this, element);
});
}
_getIndicatorSettingsFrame() {
const hBox1 = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
...getMarginAll(BOX_PADDING),
});
addToBox(hBox1, this._getIntervalLabel());
addToBox(hBox1, this._getIntervalSpinButton());
const hBox2 = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
...getMarginAll(BOX_PADDING),
});
addToBox(hBox2, this._getHideIndicatorLabel());
addToBox(hBox2, this._getHideIndicatorSwitchButton());
const vBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
...getMarginAll(BOX_PADDING),
});
addToBox(vBox, hBox1);
addToBox(vBox, hBox2);
const frame = new Gtk.Frame({
label: _('Indicator Settings'),
margin_bottom: MARGIN_BOTTOM,
});
frame.set_child(vBox);
return frame;
}
_getIntervalLabel() {
return new Gtk.Label({
label: _('Refresh interval (minutes)'),
xalign: 0,
hexpand: true,
});
}
_getHideIndicatorLabel() {
return new Gtk.Label({
label: _('Hide indicator if there are no devices'),
xalign: 0,
hexpand: true,
});
}
_getIntervalSpinButton() {
const spinButton = new Gtk.SpinButton();
const interval = this._settings.getInterval();
spinButton.set_sensitive(true);
spinButton.set_range(1, 60);
spinButton.set_value(interval || 2);
spinButton.set_increments(1, 2);
spinButton.connect('value-changed', (w) => {
this._settings.setInterval(w.get_value_as_int());
});
return spinButton;
}
_getHideIndicatorSwitchButton() {
return this._getSwitchButton(
() => this._settings.getHideIndicator(),
(value) => this._settings.setHideIndicator(value)
);
}
_getSwitchButton(getValue, setValue) {
const switchButton = new Gtk.Switch({
active: getValue()
});
switchButton.connect('notify::active', ({ active }) => {
setValue(active)
});
const vBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
});
addToBox(vBox, switchButton);
return vBox;
}
_getDevicesHeaders() {
const hBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
...getMarginAll(BOX_PADDING),
});
addToBox(hBox, this._getLabel(_('Name')));
addToBox(hBox, this._getLabel(_('Status'), false, 60));
addToBox(hBox, this._getLabel(_('Icon'), false, 80));
return hBox;
}
async _getDevicesFrame() {
const vBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
...getMarginAll(BOX_PADDING),
});
const headers = this._getDevicesHeaders();
addToBox(vBox, headers);
const devices = await this._settings.getDevices();
devices.forEach((device) => {
addToBox(vBox, this._getDeviceBox(device));
});
const frame = new Gtk.Frame({
label: _('Devices'),
margin_bottom: MARGIN_BOTTOM,
});
frame.set_child(vBox);
return frame;
}
_getDeviceBox(device) {
const hBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
...getMarginAll(BOX_PADDING),
});
addToBox(hBox, this._getLabel(device.name));
addToBox(hBox, this._getDeviceSwitchButton(device));
addToBox(hBox, this._getDeviceIconComboBox(device));
return hBox;
}
_getLabel(labelName, hexpand = true, marginEnd = 0) {
const box = new Gtk.Box({
margin_end: marginEnd,
})
const label = new Gtk.Label({
label: labelName,
xalign: 0,
hexpand,
});
addToBox(box, label);
return box;
}
_getDeviceIconComboBox(device) {
const comboBox = new Gtk.ComboBoxText();
const defaultIcon = device.defaultIcon || 'battery-full-symbolic';
const icons = [
{ key: defaultIcon, text: _('Default') },
{ key: 'audio-headphones-symbolic', text: _('Headphones') },
{ key: 'input-mouse-symbolic', text: _('Mouse') },
{ key: 'input-keyboard-symbolic', text: _('Keyboard') },
{ key: 'audio-headset-symbolic', text: _('Headset') },
{ key: 'input-gaming-symbolic', text: _('Game Controller') },
{ key: 'audio-speakers-symbolic', text: _('Speaker') },
{ key: 'battery-full-symbolic', text: _('Battery') },
];
icons.forEach((icon) => {
comboBox.append_text(icon.text);
})
const activeIndex = icons.findIndex(({ key }) => device.icon === key);
comboBox.set_active(activeIndex !== -1 ? activeIndex : 0);
comboBox.connect('changed', () => {
const i = comboBox.get_active();
this._settings.setDevice({
mac: device.mac,
icon: icons[i].key,
});
});
return comboBox;
}
_getDeviceSwitchButton(device) {
const switchButton = new Gtk.Switch({
active: device.isActive || false,
});
switchButton.connect('notify::active', ({ active }) => {
this._settings.setDevice({
mac: device.mac,
isActive: active
});
});
const vBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
...getMarginAll(BOX_PADDING),
});
addToBox(vBox, switchButton);
return vBox;
}
}
);