-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
246 lines (204 loc) · 8.25 KB
/
extension.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
import St from 'gi://St';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import { fetchLatestVersion, getVariant } from './utils.js'; // Adjust path as needed
const INTERFACE_SCHEMA = 'org.gnome.desktop.interface';
const ACCENT_COLOR = 'accent-color';
const ICON_THEME = 'icon-theme';
const CHECK_INTERVAL = 3600; // Check every hour (in seconds)
let notificationSource = null;
const NotificationPolicy = GObject.registerClass(
class NotificationPolicy extends MessageTray.NotificationPolicy {
get enable() { return true; }
get enableSound() { return true; }
get showBanners() { return true; }
get forceExpanded() { return false; }
get showInLockScreen() { return false; }
get detailsInLockScreen() { return false; }
}
);
function getNotificationSource() {
if (!notificationSource) {
const notificationPolicy = new NotificationPolicy();
notificationSource = new MessageTray.Source({
title: _('Auto Adwaita Colors'),
icon: new Gio.ThemedIcon({ name: 'dialog-information' }),
iconName: 'dialog-information',
policy: notificationPolicy,
});
notificationSource.connect('destroy', _source => {
notificationSource = null;
});
Main.messageTray.add(notificationSource);
}
return notificationSource;
}
export default class AccentColorExtension extends Extension {
constructor(metadata) {
super(metadata);
this._syncAccentColorId = null;
this._accentColorChangedId = null;
this._updateCheckTimer = null;
}
enable() {
this.settingsSchema = new Gio.Settings({ schema: INTERFACE_SCHEMA });
this._settings = this.getSettings();
// Connect to the accent-color setting change event and store the ID
this._syncAccentColorId = this.settingsSchema.connect(
'changed::' + ACCENT_COLOR,
this._syncAccentColor.bind(this)
);
// Connect to the accent-color setting change event and store the ID
this._accentColorChangedId = this._settings.connect(
'changed::' + ACCENT_COLOR,
this._onAccentColorChanged.bind(this)
);
// Get the initial accent color and apply the corresponding icon theme
this._syncAccentColor();
this._onAccentColorChanged();
// Start the periodic update check
this._startUpdateCheck();
this._checkForUpdates();
}
disable() {
// Reset the icon-theme when the extension is disabled
this.settingsSchema.set_string(ICON_THEME, 'Adwaita');
// Disconnect the signal using the stored ID
if (this._syncAccentColorId !== null) {
this.settingsSchema.disconnect(this._syncAccentColor);
this._syncAccentColorId = null;
}
if (this._accentColorChangedId !== null) {
this._settings.disconnect(this._onAccentColorChanged);
this._accentColorChangedId = null;
}
if (this._updateCheckTimer) {
GLib.source_remove(this._updateCheckTimer);
this._updateCheckTimer = null;
}
this._settings = null;
this.settingsSchema = null;
}
_syncAccentColor() {
// Sync settings between global schema and _settings
this._settings.set_string(ACCENT_COLOR, this.settingsSchema.get_string(ACCENT_COLOR));
}
_onAccentColorChanged() {
// When the accent color changes, get the new color and update the icon theme
let accentColor = this._settings.get_string(ACCENT_COLOR);
this._setIconTheme(accentColor);
}
async _setIconTheme(color) {
if (color) {
let iconTheme = `Adwaita-${color}`;
const { found } = getVariant(iconTheme);
if (found) {
await this._loopAndUpdate(iconTheme);
this.settingsSchema.set_string(ICON_THEME, iconTheme);
} else {
this.settingsSchema.set_string(ICON_THEME, 'Adwaita');
this._sendNotification({
title: _('Icon Theme Not Found'),
body: _('The selected icon theme is not installed. Please install it through the preferences page.'),
onActivate: () => this.openPreferences(),
});
}
}
}
async _loopAndUpdate(iconTheme) {
let directories = [GLib.get_home_dir(), '/var'];
let file = Gio.File.new_for_path(GLib.get_home_dir());
for (let dirPath of directories) {
// Initialize counters
let fileCount = 0;
let dirCount = 0;
// Get the contents of the directory
let enumerator = file.enumerate_children('standard::*,metadata::*',
Gio.FileQueryInfoFlags.NONE,
null);
let info;
// Iterate through the contents
while ((info = await enumerator.next_file(null)) !== null) {
await this._updateIcon(info, iconTheme, dirPath);
// Count files and directories
if (info.get_file_type() === Gio.FileType.DIRECTORY) {
dirCount++;
// Recursively process directories if needed
} else {
fileCount++;
}
}
// Log the counts
console.log(`Files: ${fileCount}, Directories: ${dirCount}`);
}
}
async _updateIcon(fileInfo, iconTheme, parentDir) {
let file = Gio.File.new_for_path(parentDir);
let childFile = file.get_child(fileInfo.get_name());
try {
let iconValue = fileInfo.get_attribute_string('metadata::custom-icon');
const regex = /Adwaita-(\w+)(?=\/)/;
if (regex.test(iconValue)) {
let newIconValue = iconValue.replace(regex, iconTheme);
childFile.set_attribute_string(
'metadata::custom-icon',
newIconValue,
Gio.FileAttributeType.STRING,
null
);
console.log(`Updated icon to ${newIconValue}`);
}
} catch (e) {
console.log(`Error updating icon: ${e.message}`);
}
}
_startUpdateCheck() {
// Set up a periodic function to check for updates
this._updateCheckTimer = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT, CHECK_INTERVAL, () => {
this._checkForUpdates().catch(error => {
logError(error, 'Periodic update check failed');
});
return GLib.SOURCE_CONTINUE; // Continue the timer
}
);
}
async _checkForUpdates() {
if (! this._settings.get_boolean('notify-about-releases'))
return;
try {
const latestVersion = await fetchLatestVersion();
if (latestVersion) {
const currentVersion = this._settings.get_string('current-version');
if (currentVersion !== latestVersion) {
this._sendNotification({
title: _('Update Available'),
body: _('A new version of Adwaita-Colors is available.'),
onActivate: () => this.openPreferences(),
});
}
}
} catch (error) {
logError(error, 'Error checking for updates');
}
}
_sendNotification({ title, body, icon = 'dialog-information', urgency = MessageTray.Urgency.NORMAL, onActivate }) {
const source = getNotificationSource();
const notification = new MessageTray.Notification({
source: source,
title: title,
body: body,
gicon: new Gio.ThemedIcon({ name: icon }),
iconName: icon,
urgency: urgency,
});
if (onActivate) {
notification.connect('activated', onActivate);
}
source.addNotification(notification);
}
}