fix: propagate preferred color scheme to the renderer (#22896)

* fix: do not crash if the window is closed syncronously with a nativeTheme change

* fix: propogate preferred color scheme to the renderer and keep it up to date
This commit is contained in:
Samuel Attard 2020-03-30 15:39:50 -07:00 committed by GitHub
parent 212b47a77b
commit fea3366bc7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 17 deletions

View file

@ -1,10 +1,12 @@
import { expect } from 'chai';
import { nativeTheme, systemPreferences } from 'electron';
import { nativeTheme, systemPreferences, BrowserWindow } from 'electron';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import { delay, ifdescribe } from './spec-helpers';
import { emittedOnce } from './events-helpers';
import { closeAllWindows } from './window-helpers';
describe('nativeTheme module', () => {
describe('nativeTheme.shouldUseDarkColors', () => {
@ -18,6 +20,8 @@ describe('nativeTheme module', () => {
nativeTheme.themeSource = 'system';
// Wait for any pending events to emit
await delay(20);
closeAllWindows();
});
it('is system by default', () => {
@ -62,6 +66,26 @@ describe('nativeTheme module', () => {
expect(systemPreferences.appLevelAppearance).to.equal('light');
});
});
const getPrefersColorSchemeIsDark = async (w: Electron.BrowserWindow) => {
const isDark: boolean = await w.webContents.executeJavaScript(
'matchMedia("(prefers-color-scheme: dark)").matches'
);
return isDark;
};
it('should override the result of prefers-color-scheme CSS media query', async () => {
const w = new BrowserWindow({ show: false });
await w.loadFile(path.resolve(__dirname, 'fixtures', 'blank.html'));
const originalSystemIsDark = await getPrefersColorSchemeIsDark(w);
nativeTheme.themeSource = 'dark';
expect(await getPrefersColorSchemeIsDark(w)).to.equal(true);
nativeTheme.themeSource = 'light';
expect(await getPrefersColorSchemeIsDark(w)).to.equal(false);
nativeTheme.themeSource = 'system';
expect(await getPrefersColorSchemeIsDark(w)).to.equal(originalSystemIsDark);
w.close();
});
});
describe('nativeTheme.shouldUseInvertedColorScheme', () => {