Last active
August 17, 2020 08:53
-
-
Save LuminescentMoon/f7a180a6960d4d1b9b7e72acb1c77275 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Work-around for this Electron bug: https://github.com/electron/electron/issues/19468 | |
import { execSync } from 'child_process' | |
import { app } from 'electron' | |
function setWindowsAppTheme(light: boolean) { | |
try { | |
execSync( | |
`REG ADD HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize /v AppsUseLightTheme /t REG_DWORD /d ${ light ? 1 : 0 } /f`, | |
{ windowsHide: true } | |
) | |
} catch(error) { | |
throw new Error('No permission to modify AppsUseLightTheme value. Electron will hang.') | |
} | |
} | |
// Validates that reg value exists and is set to 1 | |
function isCurrentAppThemeLight(): boolean { | |
let result: string | RegExpMatchArray | null | number | |
try { | |
// Check if value exists in registry | |
result = execSync( | |
'REG QUERY HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize /v AppsUseLightTheme /t REG_DWORD', | |
{ encoding: 'utf8', windowsHide: true} | |
) | |
} catch(error) { | |
// Value doesn't exist. | |
throw new Error('Could not find registry value') | |
} | |
result = result.match(/0x\d+$/im) | |
if (result === null) | |
throw new Error('Unexpected output from reg.exe') | |
result = parseInt(result[0], 16) | |
if (isNaN(result)) | |
throw new Error('Unexpected output from reg.exe') | |
return result === 1 | |
} | |
let hasBeenCalled = false | |
export default function initDarkThemeWorkAround(): boolean { | |
if (hasBeenCalled) return false | |
hasBeenCalled = true | |
// This workaround does not apply to anything but Windows. | |
if (process.platform !== 'win32') return false | |
try { | |
if (isCurrentAppThemeLight()) return false | |
} catch(error) { | |
// Unsupported Windows version. | |
return false | |
} | |
app.on('will-finish-launching', () => { | |
setWindowsAppTheme(true) | |
}) | |
app.on('ready', () => { | |
setWindowsAppTheme(false) | |
}) | |
return true | |
} |
- Is it possible for the reg key to not exist if
nativeTheme.shouldUseDarkColors
returnstrue
? - If it is, then AFAIK this fix won't work anyway because then it won't set the theme to Light, so it's a moot point?
- In any case, is it that bad to add the reg key if it doesn't exist, given that the theme is reverted anyway? IMHO the simplicity of
nativeTheme.shouldUseDarkColors
vs. theisCurrentAppThemeLight
implementation makes it worth it, but please let me know if this is wrong in some critical way.
@cmeeren Don't know, no answer, and I rather not have this script be the source of somebody's build of Windows blue screening because Microsoft didn't do QA for random registry values existing in Windows builds where they shouldn't.
Thank you so much for this
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cmeeren isCurrentAppThemeLight is also used to check if the reg value exists so we wouldn't accidentally create a reg value instead of modifying it.