Skip to content

Instantly share code, notes, and snippets.

@LuminescentMoon
Last active August 17, 2020 08:53
Show Gist options
  • Save LuminescentMoon/f7a180a6960d4d1b9b7e72acb1c77275 to your computer and use it in GitHub Desktop.
Save LuminescentMoon/f7a180a6960d4d1b9b7e72acb1c77275 to your computer and use it in GitHub Desktop.
// 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
}
@cmeeren
Copy link

cmeeren commented Nov 9, 2019

The isCurrentAppThemeLight function can be removed and Electron's nativeTheme.shouldUseDarkColors used instead.

@LuminescentMoon
Copy link
Author

@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.

@cmeeren
Copy link

cmeeren commented Nov 15, 2019

  • Is it possible for the reg key to not exist if nativeTheme.shouldUseDarkColors returns true?
  • 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. the isCurrentAppThemeLight implementation makes it worth it, but please let me know if this is wrong in some critical way.

@LuminescentMoon
Copy link
Author

@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.

@dougdenn
Copy link

Thank you so much for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment