-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefs.js
157 lines (133 loc) · 4.34 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
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
const { GObject, Gtk } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
let settings;
function init() {
settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.amneziawg');
}
/**
* Creates a labeled widget (label + child) inside a horizontal box.
* @param {string} labelText - The text for the label.
* @param {Gtk.Widget} widget - The widget to pair with the label.
* @returns {Gtk.Box} - The labeled container.
*/
function _createLabeledWidget(labelText, widget) {
const box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, spacing: 10 });
const label = new Gtk.Label({
label: labelText,
halign: Gtk.Align.START,
hexpand: true
});
box.append(label);
box.append(widget);
return box;
}
/**
* Creates a ComboBox with predefined options and binds it to a setting.
* @param {string} labelText - The text for the label.
* @param {string} settingKey - The key for the setting to bind.
* @param {Object[]} options - Array of { id, label } objects for ComboBox options.
* @returns {Gtk.Box} - The labeled ComboBox container.
*/
function _createLabeledComboBox(labelText, settingKey, options) {
const comboBox = new Gtk.ComboBoxText();
options.forEach(option => comboBox.append(option.id, option.label));
comboBox.set_active_id(settings.get_string(settingKey));
comboBox.connect('changed', () => {
settings.set_string(settingKey, comboBox.get_active_id());
});
return _createLabeledWidget(labelText, comboBox);
}
/**
* Creates a labeled Entry widget bound to a setting.
* @param {string} labelText - The text for the label.
* @param {string} settingKey - The key for the setting to bind.
* @returns {Object} - { box: Gtk.Box, entry: Gtk.Entry }.
*/
function _createLabeledEntry(labelText, settingKey) {
const entry = new Gtk.Entry({
text: settings.get_string(settingKey),
hexpand: true
});
return {
box: _createLabeledWidget(labelText, entry),
entry
};
}
/**
* Creates a labeled SpinButton widget bound to a setting.
* @param {string} labelText - The text for the label.
* @param {string} settingKey - The key for the setting to bind.
* @param {number} min - Minimum value.
* @param {number} max - Maximum value.
* @param {number} step - Step increment.
* @returns {Object} - { box: Gtk.Box, spinButton: Gtk.SpinButton }.
*/
function _createLabeledSpinButton(labelText, settingKey, min, max, step) {
const adjustment = new Gtk.Adjustment({
lower: min,
upper: max,
step_increment: step
});
const spinButton = new Gtk.SpinButton({
adjustment,
value: settings.get_int(settingKey),
hexpand: true
});
spinButton.connect('value-changed', () => {
settings.set_int(settingKey, spinButton.get_value_as_int());
});
return {
box: _createLabeledWidget(labelText, spinButton),
spinButton
};
}
/**
* Builds the preferences widget.
* @returns {Gtk.Box} - The main container with all preferences.
*/
function buildPrefsWidget() {
const widget = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 10,
margin_top: 20,
margin_bottom: 20,
margin_start: 20,
margin_end: 20
});
// Interface Name Entry
const { box: interfaceBox, entry: interfaceEntry } = _createLabeledEntry(
'Interface Name:',
'interface'
);
widget.append(interfaceBox);
// Icon Size Spin Button
const { box: iconSizeBox, spinButton: iconSizeSpinButton } = _createLabeledSpinButton(
'Icon Size:',
'icon-size',
16, // Minimum size
128, // Maximum size
1 // Step increment
);
widget.append(iconSizeBox);
// Icon Theme ComboBox
const iconThemeBox = _createLabeledComboBox(
'Icon Theme:',
'manual-theme',
[
{ id: 'dark', label: 'Dark' },
{ id: 'light', label: 'Light' }
]
);
widget.append(iconThemeBox);
// Save Button
const saveButton = new Gtk.Button({
label: 'Save',
halign: Gtk.Align.CENTER
});
saveButton.connect('clicked', () => {
settings.set_string('interface', interfaceEntry.text);
settings.sync(); // Ensure settings are saved immediately
});
widget.append(saveButton);
return widget;
}