Skip to content

Commit 080ee2b

Browse files
committed
feat: plasma支持
1 parent 465af9b commit 080ee2b

File tree

2 files changed

+60
-34
lines changed

2 files changed

+60
-34
lines changed

CHANGELOG.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.06.2504010-3 / 2025-07-
2+
- refactor: 重构主题监听处理方案
3+
14
# 1.06.2504010-2 / 2025-07-15
25
- fix: cli使用异常。#147
36

res/scripts/hackrequire.js

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,55 @@
175175
}
176176
return originalExec.apply(this, [command, options, callback])
177177
}
178-
179178
{
179+
const {spawn, execSync} = require('child_process')
180+
let isDark = (function () {
181+
try {
182+
const { DESKTOP_SESSION } = process.env;
183+
console.log(DESKTOP_SESSION);
184+
let theme = "";
185+
switch (DESKTOP_SESSION) {
186+
case "deepin":
187+
theme = execSync(
188+
`gsettings get com.deepin.dde.appearance gtk-theme`
189+
);
190+
break;
191+
case "gnome":
192+
case "gnome-classic":
193+
theme = execSync(
194+
`gsettings get org.gnome.desktop.interface ${this.gnomeScheme}`
195+
);
196+
break;
197+
198+
case "plasma":
199+
theme = execSync(
200+
`gsettings get org.gnome.desktop.interface color-scheme`,
201+
);
202+
break;
203+
default:
204+
console.warn(
205+
`NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}`
206+
);
207+
break;
208+
}
209+
console.log(theme.toString());
210+
return theme.toString().toLowerCase().includes("dark");
211+
} catch (error) {
212+
console.error("尝试获取主题信息失败,使用默认暗色");
213+
return true;
214+
}
215+
})()
216+
const originalMatchMedia = window.matchMedia
217+
window.matchMedia = function (...args) {
218+
console.warn('----------------> matchMedia:', ...args)
219+
const mm = originalMatchMedia.apply(this, args)
220+
Object.defineProperty(mm, 'matches', {
221+
get(){
222+
return isDark
223+
}
224+
})
225+
return mm
226+
}
180227
class CheckDark {
181228
// 监听gsettings monitor org.gnome.desktop.interface gtk-theme
182229
monitorTheme(callback) {
@@ -204,6 +251,13 @@
204251
this.gnomeScheme,
205252
]);
206253
break;
254+
case "plasma":
255+
monitor = spawn("gsettings", [
256+
"monitor",
257+
"org.gnome.desktop.interface",
258+
'color-scheme',
259+
]);
260+
break;
207261
default:
208262
console.warn(
209263
`NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}`
@@ -216,9 +270,9 @@
216270
});
217271
monitor &&
218272
monitor.stdout.on("data", (chunk) => {
219-
// TODO: 防抖动包装
220273
const data = chunk.toString();
221-
const isDark = data.toLowerCase().includes("dark");
274+
console.warn('Theme changed:', data)
275+
isDark = data.toLowerCase().includes("dark");
222276
this.callback(isDark)
223277
});
224278
process.on("SIGTERM", (signal) => {
@@ -228,37 +282,6 @@
228282
console.error("尝试监听主题失败!", err);
229283
}
230284
}
231-
get isDark() {
232-
try {
233-
const { DESKTOP_SESSION } = process.env;
234-
console.log(DESKTOP_SESSION);
235-
let theme = "";
236-
switch (DESKTOP_SESSION) {
237-
case "deepin":
238-
theme = execSync(
239-
`gsettings get com.deepin.dde.appearance gtk-theme`
240-
);
241-
break;
242-
case "gnome":
243-
case "gnome-classic":
244-
theme = execSync(
245-
`gsettings get org.gnome.desktop.interface ${this.gnomeScheme}`
246-
);
247-
break;
248-
249-
default:
250-
console.warn(
251-
`NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}`
252-
);
253-
break;
254-
}
255-
console.log(theme.toString());
256-
return theme.toString().toLowerCase().includes("dark");
257-
} catch (error) {
258-
console.error("尝试获取主题信息失败,使用默认暗色");
259-
return true;
260-
}
261-
}
262285
get gnomeScheme() {
263286
try {
264287
// 判断 Gnome-Shell 版本 from @icepie

0 commit comments

Comments
 (0)