Skip to content

Commit 52a32a1

Browse files
committed
🎨 Improve mobile status bar color parsing
1 parent 5504cf1 commit 52a32a1

1 file changed

Lines changed: 28 additions & 20 deletions

File tree

app/src/util/assets.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -310,32 +310,37 @@ export const setMode = (modeElementValue: number) => {
310310
/// #endif
311311
};
312312

313-
const rgba2hex = (rgba: string) => {
314-
if (rgba.startsWith("#")) {
315-
return rgba;
316-
}
317-
let a: any;
318-
const rgb: any = rgba.replace(/\s/g, "").match(/^rgba?\((\d+),(\d+),(\d+),?([^,\s)]+)?/i);
319-
const alpha = (rgb && rgb[4] || "").trim();
320-
let hex = rgb ?
321-
(rgb[1] | 1 << 8).toString(16).slice(1) +
322-
(rgb[2] | 1 << 8).toString(16).slice(1) +
323-
(rgb[3] | 1 << 8).toString(16).slice(1) : rgba;
313+
const padHex = (n: number) => (n | 256).toString(16).slice(1);
324314

325-
if (alpha !== "") {
326-
a = alpha;
327-
} else {
328-
a = 0o1;
315+
const rgbaToHex = (rgba: string) => {
316+
const rgb = rgba.replace(/\s/g, "").match(/^rgba?\((\d+),(\d+),(\d+)(?:,([^)]+))?\)$/i);
317+
if (!rgb) {
318+
return "";
319+
}
320+
const alpha = rgb[4] !== undefined ? parseFloat(rgb[4]) : 1;
321+
if (alpha === 0) {
322+
return "";
329323
}
330-
a = ((a * 255) | 1 << 8).toString(16).slice(1);
331-
hex = hex + a;
332-
return hex;
324+
return "#" +
325+
padHex(parseInt(rgb[1], 10)) +
326+
padHex(parseInt(rgb[2], 10)) +
327+
padHex(parseInt(rgb[3], 10)) +
328+
padHex(Math.round(alpha * 255));
329+
};
330+
331+
const cssVarToRgba = (varName: string) => {
332+
const probe = document.createElement("div");
333+
probe.style.display = "none";
334+
probe.style.backgroundColor = `var(${varName})`;
335+
document.documentElement.appendChild(probe);
336+
const rgba = getComputedStyle(probe).backgroundColor;
337+
probe.remove();
338+
return rgba;
333339
};
334340

335341
const updateMobileTheme = (OSTheme: string) => {
336342
if (isInMobileApp()) {
337343
setTimeout(() => {
338-
const backgroundColor = rgba2hex(getComputedStyle(document.body).getPropertyValue("--b3-theme-background").trim());
339344
let mode = window.siyuan.config.appearance.mode;
340345
if (window.siyuan.config.appearance.modeOS) {
341346
if (OSTheme === "dark") {
@@ -344,8 +349,11 @@ const updateMobileTheme = (OSTheme: string) => {
344349
mode = 0;
345350
}
346351
}
352+
const fallback = mode === 0 ? "#ffffffff" : "#1e1e1eff";
353+
const backgroundColor = rgbaToHex(cssVarToRgba("--b3-theme-background")) || fallback;
354+
// 统一传 #RRGGBBAA:iOS 按 #RRGGBBAA 解析,Android / Harmony 将 #RRGGBBAA 转为 #AARRGGBB
347355
if (isInIOS()) {
348-
window.webkit.messageHandlers.changeStatusBar.postMessage((backgroundColor || (mode === 0 ? "#fff" : "#1e1e1e")) + " " + mode);
356+
window.webkit.messageHandlers.changeStatusBar.postMessage(backgroundColor + " " + mode);
349357
} else if (isInAndroid()) {
350358
window.JSAndroid.changeStatusBarColor(backgroundColor, mode);
351359
} else if (isInHarmony()) {

0 commit comments

Comments
 (0)