diff --git a/CHANGELOG.MD b/CHANGELOG.MD index c65f20b..7ba1e0c 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -1,3 +1,6 @@ +# 1.06.2504010-3 / 2025-07- +- refactor: 重构主题监听处理方案 + # 1.06.2504010-2 / 2025-07-15 - fix: cli使用异常。#147 diff --git a/res/scripts/hackrequire.js b/res/scripts/hackrequire.js index e8a3ec1..b86d71b 100644 --- a/res/scripts/hackrequire.js +++ b/res/scripts/hackrequire.js @@ -175,8 +175,55 @@ } return originalExec.apply(this, [command, options, callback]) } - { + const {spawn, execSync} = require('child_process') + let isDark = (function () { + try { + const { DESKTOP_SESSION } = process.env; + console.log(DESKTOP_SESSION); + let theme = ""; + switch (DESKTOP_SESSION) { + case "deepin": + theme = execSync( + `gsettings get com.deepin.dde.appearance gtk-theme` + ); + break; + case "gnome": + case "gnome-classic": + theme = execSync( + `gsettings get org.gnome.desktop.interface ${this.gnomeScheme}` + ); + break; + + case "plasma": + theme = execSync( + `gsettings get org.gnome.desktop.interface color-scheme`, + ); + break; + default: + console.warn( + `NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}` + ); + break; + } + console.log(theme.toString()); + return theme.toString().toLowerCase().includes("dark"); + } catch (error) { + console.error("尝试获取主题信息失败,使用默认暗色"); + return true; + } + })() + const originalMatchMedia = window.matchMedia + window.matchMedia = function (...args) { + console.warn('----------------> matchMedia:', ...args) + const mm = originalMatchMedia.apply(this, args) + Object.defineProperty(mm, 'matches', { + get(){ + return isDark + } + }) + return mm + } class CheckDark { // 监听gsettings monitor org.gnome.desktop.interface gtk-theme monitorTheme(callback) { @@ -204,6 +251,13 @@ this.gnomeScheme, ]); break; + case "plasma": + monitor = spawn("gsettings", [ + "monitor", + "org.gnome.desktop.interface", + 'color-scheme', + ]); + break; default: console.warn( `NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}` @@ -216,9 +270,9 @@ }); monitor && monitor.stdout.on("data", (chunk) => { - // TODO: 防抖动包装 const data = chunk.toString(); - const isDark = data.toLowerCase().includes("dark"); + console.warn('Theme changed:', data) + isDark = data.toLowerCase().includes("dark"); this.callback(isDark) }); process.on("SIGTERM", (signal) => { @@ -228,37 +282,6 @@ console.error("尝试监听主题失败!", err); } } - get isDark() { - try { - const { DESKTOP_SESSION } = process.env; - console.log(DESKTOP_SESSION); - let theme = ""; - switch (DESKTOP_SESSION) { - case "deepin": - theme = execSync( - `gsettings get com.deepin.dde.appearance gtk-theme` - ); - break; - case "gnome": - case "gnome-classic": - theme = execSync( - `gsettings get org.gnome.desktop.interface ${this.gnomeScheme}` - ); - break; - - default: - console.warn( - `NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}` - ); - break; - } - console.log(theme.toString()); - return theme.toString().toLowerCase().includes("dark"); - } catch (error) { - console.error("尝试获取主题信息失败,使用默认暗色"); - return true; - } - } get gnomeScheme() { try { // 判断 Gnome-Shell 版本 from @icepie