Skip to content

Commit 465af9b

Browse files
committed
refactor: 使用更简洁的主题监听方式
1 parent 322de01 commit 465af9b

File tree

3 files changed

+119
-25
lines changed

3 files changed

+119
-25
lines changed

res/scripts/hackrequire.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,118 @@
175175
}
176176
return originalExec.apply(this, [command, options, callback])
177177
}
178+
179+
{
180+
class CheckDark {
181+
// 监听gsettings monitor org.gnome.desktop.interface gtk-theme
182+
monitorTheme(callback) {
183+
try {
184+
if (this.callback) {
185+
this.callback = callback
186+
return;
187+
}
188+
this.callback = callback
189+
let monitor = null;
190+
const { DESKTOP_SESSION } = process.env;
191+
switch (DESKTOP_SESSION) {
192+
case "deepin":
193+
monitor = spawn("gsettings", [
194+
"monitor",
195+
"com.deepin.dde.appearance",
196+
"gtk-theme",
197+
]);
198+
break;
199+
case "gnome":
200+
case "gnome-classic":
201+
monitor = spawn("gsettings", [
202+
"monitor",
203+
"org.gnome.desktop.interface",
204+
this.gnomeScheme,
205+
]);
206+
break;
207+
default:
208+
console.warn(
209+
`NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}`
210+
);
211+
break;
212+
}
213+
monitor &&
214+
monitor.on("error", (err) => {
215+
console.error("monitorTheme", err);
216+
});
217+
monitor &&
218+
monitor.stdout.on("data", (chunk) => {
219+
// TODO: 防抖动包装
220+
const data = chunk.toString();
221+
const isDark = data.toLowerCase().includes("dark");
222+
this.callback(isDark)
223+
});
224+
process.on("SIGTERM", (signal) => {
225+
monitor.kill(signal);
226+
});
227+
} catch (err) {
228+
console.error("尝试监听主题失败!", err);
229+
}
230+
}
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+
}
262+
get gnomeScheme() {
263+
try {
264+
// 判断 Gnome-Shell 版本 from @icepie
265+
const gnomeVersion = execSync(`gnome-shell --version`)
266+
.toString()
267+
.replace(/[\r\n]/g, "")
268+
.split(" ");
269+
const gnomeVersionNum =
270+
gnomeVersion.length == 3 ? Number(gnomeVersion[2]) : 0;
271+
return gnomeVersionNum >= 42 ? "color-scheme" : "gtk-theme";
272+
} catch (err) {
273+
console.error("检查gnome版本失败, 使用gtk-theme", err);
274+
return "gtk-theme";
275+
}
276+
}
277+
}
278+
const checkDark = new CheckDark()
279+
const original = MediaQueryList.prototype.addEventListener
280+
MediaQueryList.prototype.addEventListener = function (...args) {
281+
console.warn('----------> MediaQueryList.addEventListener:', ...args)
282+
checkDark.monitorTheme((isDark) => {
283+
args[1]({
284+
matches: isDark
285+
})
286+
})
287+
return original.apply(this, args)
288+
}
289+
}
178290
} catch (error) {
179291
process.stderr.write(error.message);
180292
process.stderr.write(error.stack);

test/theme-check.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { execSync, spawn } = require("child_process");
33

44
class CheckDark {
55
// 监听gsettings monitor org.gnome.desktop.interface gtk-theme
6-
monitorTheme() {
6+
monitorTheme(callback) {
77
try {
88
let monitor = null;
99
const { DESKTOP_SESSION } = process.env;
@@ -37,11 +37,8 @@ class CheckDark {
3737
monitor.stdout.on("data", (chunk) => {
3838
// TODO: 防抖动包装
3939
const data = chunk.toString();
40-
const t = data.toLowerCase().includes("dark");
41-
console.log(data);
42-
console.log("dark", t);
43-
// (this._theme = t ? i.Dark : i.Light),
44-
// this._onDidThemeChange.fire(this._theme);
40+
const isDark = data.toLowerCase().includes("dark");
41+
callback(isDark)
4542
});
4643
process.on("SIGTERM", (signal) => {
4744
monitor.kill(signal);
@@ -83,7 +80,7 @@ class CheckDark {
8380
}
8481
get gnomeScheme() {
8582
try {
86-
// 判断 Gnome-Shell 版本
83+
// 判断 Gnome-Shell 版本 from @icepie
8784
const gnomeVersion = execSync(`gnome-shell --version`)
8885
.toString()
8986
.replace(/[\r\n]/g, "")
@@ -98,7 +95,9 @@ class CheckDark {
9895
}
9996
}
10097
const cd = new CheckDark();
101-
cd.monitorTheme();
98+
cd.monitorTheme((isDark) => {
99+
console.info('is dark:', isDark)
100+
});
102101
console.log(cd.isDark);
103102

104103
function original() {

tools/fix-core.sh

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,6 @@ if [[ "$WINE" != 'true' ]];then
6464
echo $timeStamp > "${package_dir}/.build_time"
6565
fi
6666

67-
# fix theme
68-
notice "fix theme"
69-
find_result=$( grep -lr "OSThemeController=" "$tmp_dir/core.wxvpkg" )
70-
echo "theme: $find_result"
71-
if [[ -n $find_result ]];then
72-
# require of child_process
73-
sed -i 's/"use strict";O/"use strict";const {execSync,spawn}=require("child_process");O/' $find_result
74-
# replace listener to monitor
75-
sed -i 's/this.registerListeners()/this.monitorTheme()/' $find_result
76-
# replace check func
77-
sed -i 's/mediaQuery.matches/isDark/' $find_result
78-
# add functions
79-
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
80-
else
81-
warn "theme位置未找到"
82-
fi
83-
8467
# pack 路径 到 文件
8568
notice "pack"
8669
node "$pack_script" "$tmp_dir/core.wxvpkg" "$package_dir/core.wxvpkg"

0 commit comments

Comments
 (0)