Skip to content

Commit 0ef7e01

Browse files
committed
Dynamically read app theme color for sidebar
1 parent 90ba39c commit 0ef7e01

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

build/after-pack.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ exec "\${ELECTRON_BIN}" "\${extra_args[@]}" "$@"
114114
}
115115

116116
// Hide the native menu bar and fix black transparent background on Linux
117-
let buildDir = path.join(appOutDir, 'resources', 'app', '.vite', 'build');
117+
const buildDir = path.join(appOutDir, 'resources', 'app', '.vite', 'build');
118118
const bootstrapPath = path.join(buildDir, 'bootstrap.js');
119119
if (fs.existsSync(bootstrapPath)) {
120120
let content = fs.readFileSync(bootstrapPath, 'utf8');
121-
const inject = `(()=>{const {app,nativeTheme}=require("electron");app.on("browser-window-created",(e,w)=>{w.setMenuBarVisibility(false);w.autoHideMenuBar=true;const updateBg=()=>{try{const bg=nativeTheme.shouldUseDarkColors?"#1e1e1e":"#ffffff";w.setBackgroundColor(bg);w.webContents.insertCSS(\`* { backdrop-filter: none !important; } aside, nav, [class*="sidebar"] { background-color: \${bg} !important; }\`);}catch(err){}};w.webContents.on("dom-ready",updateBg);updateBg();nativeTheme.on("updated",updateBg);});})();`;
121+
const inject = `(()=>{const {app}=require("electron");app.on("browser-window-created",(e,w)=>{w.setMenuBarVisibility(false);w.autoHideMenuBar=true;w.webContents.on("dom-ready",()=>{w.webContents.executeJavaScript(\`(function(){const style=document.createElement('style');document.head.appendChild(style);function updateColors(){const bg=window.getComputedStyle(document.body).backgroundColor;const rgb=bg.match(/\\\d+/g);if(rgb&&rgb.length>=3){const isDark=(rgb[0]*0.299+rgb[1]*0.587+rgb[2]*0.114)<128;const sidebarBg=isDark?'#1e1e1e':'#f5f5f7';style.textContent='* { backdrop-filter: none !important; } aside, nav, [class*="sidebar"], [class*="Sidebar"] { background-color: '+sidebarBg+' !important; }';}}updateColors();setInterval(updateColors, 1000);})();\`).catch(()=>{});});});})();`;
122122
if (!content.includes('setMenuBarVisibility')) {
123123
content = content.replace('require("electron");', `require("electron");${inject}`);
124124
fs.writeFileSync(bootstrapPath, content);
125-
} else if (content.includes('const updateBg=()=>{try{w.setBackgroundColor')) {
125+
} else if (content.includes('const updateBg=()=>{try{')) {
126126
// Replace the previous injection with the new one
127-
content = content.replace(/\(\(\)=>\{const \{app,nativeTheme\}=require\("electron"\);app\.on\("browser-window-created",\(e,w\)=>\{w\.setMenuBarVisibility\(false\);w\.autoHideMenuBar=true;const updateBg=\(\)=>\{try\{w\.setBackgroundColor.*?\}\)\(\);/g, inject);
127+
content = content.replace(/\(\(\)=>\{const \{app,nativeTheme\}=require\("electron"\);app\.on\("browser-window-created",\(e,w\)=>\{w\.setMenuBarVisibility\(false\);w\.autoHideMenuBar=true;const updateBg=\(\)=>\{try\{.*?\}\)\(\);/g, inject);
128128
fs.writeFileSync(bootstrapPath, content);
129129
}
130130
}

0 commit comments

Comments
 (0)