Skip to content

Commit 587e2c4

Browse files
committed
fix: only poll theme changes for system
1 parent 6be220b commit 587e2c4

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

src/palettes/changeTheme/index.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,47 @@ function generateHints(type) {
4343
let previousDark = isDeviceDarkTheme();
4444
const updateTimeMs = 2000;
4545

46-
let intervalId = setInterval(async () => {
46+
let intervalId = null;
47+
48+
function syncSystemTheme() {
4749
if (appSettings.value.appTheme.toLowerCase() === "system") {
4850
const isDark = isDeviceDarkTheme();
4951
if (isDark !== previousDark) {
5052
previousDark = isDark;
5153
updateSystemTheme(isDark);
5254
}
5355
}
54-
}, updateTimeMs);
56+
}
57+
58+
function startSystemThemeWatcher() {
59+
if (intervalId) return;
60+
intervalId = setInterval(syncSystemTheme, updateTimeMs);
61+
}
62+
63+
function stopSystemThemeWatcher() {
64+
if (!intervalId) return;
65+
clearInterval(intervalId);
66+
intervalId = null;
67+
}
68+
69+
function updateSystemThemeWatcher(theme) {
70+
if (String(theme).toLowerCase() === "system") {
71+
startSystemThemeWatcher();
72+
syncSystemTheme();
73+
return;
74+
}
75+
stopSystemThemeWatcher();
76+
}
77+
78+
updateSystemThemeWatcher(appSettings.value.appTheme);
79+
appSettings.on("update:appTheme", updateSystemThemeWatcher);
5580

5681
function onselect(value) {
5782
if (!value) return;
5883

5984
const selection = JSON.parse(value);
6085

61-
if (selection.theme === "system") {
62-
// Start interval if not already started
63-
if (!intervalId) {
64-
intervalId = setInterval(async () => {
65-
if (appSettings.value.appTheme.toLowerCase() === "system") {
66-
const isDark = isDeviceDarkTheme();
67-
if (isDark !== previousDark) {
68-
previousDark = isDark;
69-
updateSystemTheme(isDark);
70-
}
71-
}
72-
}, updateTimeMs);
73-
}
74-
} else {
75-
// Cancel interval if it's running
76-
if (intervalId) {
77-
clearInterval(intervalId);
78-
intervalId = null;
79-
}
80-
}
86+
updateSystemThemeWatcher(selection.theme);
8187

8288
if (selection.type === "editor") {
8389
editorManager.editor.setTheme(selection.theme);

0 commit comments

Comments
 (0)