diff --git a/test/env b/test/env deleted file mode 100755 index 6f0e172..0000000 --- a/test/env +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env node -console.log(process.version) \ No newline at end of file diff --git a/test/fix-core.sh b/test/fix-core.sh new file mode 100755 index 0000000..4b29f9c --- /dev/null +++ b/test/fix-core.sh @@ -0,0 +1,16 @@ +#!/bin/bash +root_dir=$(cd `dirname $0`/.. && pwd -P) + +package_dir="$root_dir/package.nw" +tmp_dir="$root_dir/package.nw" +mkdir -p $tmp_dir +unpack_script="$root_dir/tools/wxvpkg_unpack.js" +pack_script="$root_dir/tools/wxvpkg_pack.js" + +find_result=$( grep -lr "OSThemeController=" "$tmp_dir/core.wxvpkg.ext" ) +echo "theme: $find_result" +sed -i 's/"use strict";O/"use strict";const {execSync,spawn}=require("child_process");O/' $find_result +sed -i 's/this.registerListeners()/this.monitorTheme()/' $find_result +sed -i 's/}registerListeners/}monitorTheme(){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","gtk-theme",]);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.includes("dark");console.warn(data);console.warn("dark",t);(this._theme=t?i.Dark:i.Light),this._onDidThemeChange.fire(this._theme)},400))}registerListeners/' $find_result +sed -i 's/mediaQuery.matches/isDark/' $find_result +sed -i 's/}getDefaultTheme/}get isDark(){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 gtk-theme`);break;default:break}return theme.includes("dark")}getDefaultTheme/' $find_result \ No newline at end of file diff --git a/test/fix-wcc-node b/test/fix-wcc-node.sh similarity index 100% rename from test/fix-wcc-node rename to test/fix-wcc-node.sh diff --git a/test/theme-check.js b/test/theme-check.js index 576b1b6..579b91a 100644 --- a/test/theme-check.js +++ b/test/theme-check.js @@ -35,7 +35,7 @@ class CheckDark { }); monitor && monitor.stdout.on("data", (chunk) => { - // TODO: 防抖动包装 + // TODO: 防抖动包装 const data = chunk.toString(); const t = data.includes("dark"); console.log(data); @@ -68,12 +68,13 @@ class CheckDark { } } const cd = new CheckDark(); -console.log(cd.isDark); cd.monitorTheme(); +console.log(cd.isDark); function original() { !(function (require, directRequire) { "use strict"; + const { execSync, spawn } = require("child_process"); Object.defineProperty(exports, "__esModule", { value: !0 }), (exports.OSThemeController = exports.OSTheme = void 0); const e = require("lodash"), @@ -98,7 +99,7 @@ function original() { (this._theme = void 0), (this._isEnabled = void 0), (this._mediaQuery2 = void 0), - this.registerListeners(); + this.monitorTheme(); } static get shared() { return ( @@ -108,6 +109,47 @@ function original() { get onDidThemeChange() { return this._onDidThemeChange.event; } + monitorTheme() { + 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", + "gtk-theme", + ]); + 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) => { + // 防抖动包装 防抖动, 该函数会从上一次被调用后,延迟 400 毫秒后调用 本方法 + const data = chunk.toString(); + const t = data.includes("dark"); + console.warn(data); + console.warn("dark", t); + (this._theme = t ? i.Dark : i.Light), + this._onDidThemeChange.fire(this._theme); + }, 400)); + } registerListeners() { var e, t; this.isEnabled() && @@ -143,8 +185,8 @@ function original() { } tryGetCurrentTheme() { if (this.isEnabled()) { - // return this.isDark ? i.Dark : i.Light; - return this.mediaQuery.matches ? i.Dark : i.Light; + return this.isDark ? i.Dark : i.Light; + // return this.mediaQuery.matches ? i.Dark : i.Light; } return i.Unknown; } @@ -169,28 +211,28 @@ function original() { this._mediaQuery2 ); } - get isDark() { - 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 gtk-theme` - ); - break; - - default: - break; - } - return theme.includes("dark"); - } + get isDark() { + 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 gtk-theme` + ); + break; + + default: + break; + } + return theme.includes("dark"); + } getDefaultTheme() { return i.Dark; } diff --git a/tools/fix-core.sh b/tools/fix-core.sh index 9150a54..e6577bf 100755 --- a/tools/fix-core.sh +++ b/tools/fix-core.sh @@ -69,6 +69,15 @@ if [[ $NO_WINE == 'true' ]];then echo $timeStamp > "${package_dir}/.build_time" fi +# fix theme +find_result=$( grep -lr "OSThemeController=" "$tmp_dir/core.wxvpkg" ) +echo "theme: $find_result" +sed -i 's/"use strict";O/"use strict";const {execSync,spawn}=require("child_process");O/' $find_result +sed -i 's/this.registerListeners()/this.monitorTheme()/' $find_result +sed -i 's/}registerListeners/}monitorTheme(){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","gtk-theme",]);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.includes("dark");console.warn(data);console.warn("dark",t);(this._theme=t?i.Dark:i.Light),this._onDidThemeChange.fire(this._theme)},400))}registerListeners/' $find_result +sed -i 's/mediaQuery.matches/isDark/' $find_result +sed -i 's/}getDefaultTheme/}get isDark(){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 gtk-theme`);break;default:break}return theme.includes("dark")}getDefaultTheme/' $find_result + # pack 路径 到 文件 echo "pack" node "$pack_script" "$tmp_dir/core.wxvpkg" "$package_dir/core.wxvpkg"