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 new file mode 100644 index 0000000..579b91a --- /dev/null +++ b/test/theme-check.js @@ -0,0 +1,241 @@ +#!/usr/bin/env node +const { execSync, spawn } = require("child_process"); + +class CheckDark { + // 监听gsettings monitor org.gnome.desktop.interface gtk-theme + 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", (chunk) => { + // TODO: 防抖动包装 + const data = chunk.toString(); + const t = data.includes("dark"); + console.log(data); + console.log("dark", t); + // (this._theme = t ? i.Dark : i.Light), + // this._onDidThemeChange.fire(this._theme); + }); + } + 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"); + } +} +const cd = new CheckDark(); +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"), + t = require("./e45a1736f0cff12ccd223e3f4dd75808.js"); + var i; + !(function (e) { + (e[(e.Dark = 0)] = "Dark"), + (e[(e.Light = 1)] = "Light"), + (e[(e.Unknown = 2)] = "Unknown"); + })((i = exports.OSTheme || (exports.OSTheme = {}))); + const r = process.versions.nw || "0.0.0"; + exports.OSThemeController = class { + constructor() { + (this._onDidThemeChange = new t.EasyEmitter()), + // debounce, 防抖动, 该函数会从上一次被调用后,延迟 400 毫秒后调用 func 方法 + (this.onMediaQueryChange = e.debounce((e) => { + console.warn("onMediaQueryChange", e); + const t = e.matches; + (this._theme = t ? i.Dark : i.Light), + this._onDidThemeChange.fire(this._theme); + }, 400)), + (this._theme = void 0), + (this._isEnabled = void 0), + (this._mediaQuery2 = void 0), + this.monitorTheme(); + } + static get shared() { + return ( + this._shared || (this._shared = new this()), this._shared + ); + } + 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() && + (null === (e = this.mediaQuery) || + void 0 === e || + e.addEventListener("change", this.onMediaQueryChange), + null === (t = this.mediaQuery2) || + void 0 === t || + t.addEventListener("change", this.onMediaQueryChange)); + } + getCurrentTheme() { + console.warn("getCurrentTheme"); + return ( + this._theme || (this._theme = this.tryGetCurrentTheme()), + this._theme + ); + } + isEnabled() { + var e; + if (void 0 === this._isEnabled) { + const i = t.compareVersion("0.44.6", r) <= 0, + n = + "not all" !== + (null === + (e = window.matchMedia( + "(prefers-color-scheme)" + )) || void 0 === e + ? void 0 + : e.media); + this._isEnabled = i && n; + } + return this._isEnabled; + } + tryGetCurrentTheme() { + if (this.isEnabled()) { + return this.isDark ? i.Dark : i.Light; + // return this.mediaQuery.matches ? i.Dark : i.Light; + } + return i.Unknown; + } + get mediaQuery() { + return ( + this._mediaQuery || + (this._mediaQuery = window.matchMedia( + "(prefers-color-scheme: dark)" + )), + this._mediaQuery + ); + } + get mediaQuery2() { + return ( + void 0 === this._mediaQuery2 && + (global.contentWindow && global.contentWindow !== window + ? (this._mediaQuery2 = + global.contentWindow.matchMedia( + "(prefers-color-scheme: dark)" + )) + : (this._mediaQuery2 = null)), + 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"); + } + getDefaultTheme() { + return i.Dark; + } + }; + })(require("licia/lazyImport")(require), require); +} 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"