diff --git a/res/scripts/hackrequire.js b/res/scripts/hackrequire.js index e962ad9..e8a3ec1 100644 --- a/res/scripts/hackrequire.js +++ b/res/scripts/hackrequire.js @@ -175,6 +175,118 @@ } return originalExec.apply(this, [command, options, callback]) } + + { + class CheckDark { + // 监听gsettings monitor org.gnome.desktop.interface gtk-theme + monitorTheme(callback) { + try { + if (this.callback) { + this.callback = callback + return; + } + this.callback = callback + let monitor = null; + const { DESKTOP_SESSION } = process.env; + switch (DESKTOP_SESSION) { + case "deepin": + monitor = spawn("gsettings", [ + "monitor", + "com.deepin.dde.appearance", + "gtk-theme", + ]); + break; + case "gnome": + case "gnome-classic": + monitor = spawn("gsettings", [ + "monitor", + "org.gnome.desktop.interface", + this.gnomeScheme, + ]); + break; + default: + console.warn( + `NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}` + ); + break; + } + monitor && + monitor.on("error", (err) => { + console.error("monitorTheme", err); + }); + monitor && + monitor.stdout.on("data", (chunk) => { + // TODO: 防抖动包装 + const data = chunk.toString(); + const isDark = data.toLowerCase().includes("dark"); + this.callback(isDark) + }); + process.on("SIGTERM", (signal) => { + monitor.kill(signal); + }); + } catch (err) { + 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 + const gnomeVersion = execSync(`gnome-shell --version`) + .toString() + .replace(/[\r\n]/g, "") + .split(" "); + const gnomeVersionNum = + gnomeVersion.length == 3 ? Number(gnomeVersion[2]) : 0; + return gnomeVersionNum >= 42 ? "color-scheme" : "gtk-theme"; + } catch (err) { + console.error("检查gnome版本失败, 使用gtk-theme", err); + return "gtk-theme"; + } + } + } + const checkDark = new CheckDark() + const original = MediaQueryList.prototype.addEventListener + MediaQueryList.prototype.addEventListener = function (...args) { + console.warn('----------> MediaQueryList.addEventListener:', ...args) + checkDark.monitorTheme((isDark) => { + args[1]({ + matches: isDark + }) + }) + return original.apply(this, args) + } + } } catch (error) { process.stderr.write(error.message); process.stderr.write(error.stack); diff --git a/test/theme-check.js b/test/theme-check.js index b148282..fb30e6d 100644 --- a/test/theme-check.js +++ b/test/theme-check.js @@ -3,7 +3,7 @@ const { execSync, spawn } = require("child_process"); class CheckDark { // 监听gsettings monitor org.gnome.desktop.interface gtk-theme - monitorTheme() { + monitorTheme(callback) { try { let monitor = null; const { DESKTOP_SESSION } = process.env; @@ -37,11 +37,8 @@ class CheckDark { monitor.stdout.on("data", (chunk) => { // TODO: 防抖动包装 const data = chunk.toString(); - const t = data.toLowerCase().includes("dark"); - console.log(data); - console.log("dark", t); - // (this._theme = t ? i.Dark : i.Light), - // this._onDidThemeChange.fire(this._theme); + const isDark = data.toLowerCase().includes("dark"); + callback(isDark) }); process.on("SIGTERM", (signal) => { monitor.kill(signal); @@ -83,7 +80,7 @@ class CheckDark { } get gnomeScheme() { try { - // 判断 Gnome-Shell 版本 + // 判断 Gnome-Shell 版本 from @icepie const gnomeVersion = execSync(`gnome-shell --version`) .toString() .replace(/[\r\n]/g, "") @@ -98,7 +95,9 @@ class CheckDark { } } const cd = new CheckDark(); -cd.monitorTheme(); +cd.monitorTheme((isDark) => { + console.info('is dark:', isDark) +}); console.log(cd.isDark); function original() { diff --git a/tools/fix-core.sh b/tools/fix-core.sh index 924699a..6d675a7 100755 --- a/tools/fix-core.sh +++ b/tools/fix-core.sh @@ -64,23 +64,6 @@ if [[ "$WINE" != 'true' ]];then echo $timeStamp > "${package_dir}/.build_time" fi -# fix theme -notice "fix theme" -find_result=$( grep -lr "OSThemeController=" "$tmp_dir/core.wxvpkg" ) -echo "theme: $find_result" -if [[ -n $find_result ]];then - # require of child_process - sed -i 's/"use strict";O/"use strict";const {execSync,spawn}=require("child_process");O/' $find_result - # replace listener to monitor - sed -i 's/this.registerListeners()/this.monitorTheme()/' $find_result - # replace check func - sed -i 's/mediaQuery.matches/isDark/' $find_result - # add functions - sed -i 's#}getDefaultTheme#}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:break}return theme.includes("dark");}catch(err){console.error("尝试获取主题信息失败,使用默认暗色",err);return true;}}get gnomeScheme(){try{const gnomeVersion=execSync(`gnome-shell --version`).toString().replace(/[\\r\\n]/g,"").split(" ");const gnomeVersionNum=gnomeVersion.length==3?Number(gnomeVersion[2]):0;return gnomeVersionNum>=42?"color-scheme":"gtk-theme";}catch(err){console.error("检查gnome版本失败, 使用gtk-theme", err);return "gtk-theme";}}monitorTheme(){try{let monitor=null;const{DESKTOP_SESSION}=process.env;switch(DESKTOP_SESSION){case"deepin":monitor=spawn("gsettings",["monitor","com.deepin.dde.appearance","gtk-theme",]);break;case"gnome":case"gnome-classic":monitor=spawn("gsettings",["monitor","org.gnome.desktop.interface",this.gnomeScheme,]);break;default:console.warn(`NOT SUPPORTED!!!DESKTOP_SESSION:${DESKTOP_SESSION}`);break}monitor\&\&monitor.on("error",(err)=>{console.error("monitorTheme",err)});monitor\&\&monitor.stdout.on("data",e.debounce((chunk)=>{const data=chunk.toString();const t=data.toLowerCase().includes("dark");(this._theme=t?i.Dark:i.Light),this._onDidThemeChange.fire(this._theme)},400));process.on("SIGTERM",(signal)=>{monitor.kill(signal);});}catch(err){console.error("尝试监听主题失败!", err);}}getDefaultTheme#' $find_result -else - warn "theme位置未找到" -fi - # pack 路径 到 文件 notice "pack" node "$pack_script" "$tmp_dir/core.wxvpkg" "$package_dir/core.wxvpkg"