Repository navigation
Add System / Light / Dark theme selection #5468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PlainSeer
wants to merge
2
commits into
springfall2008:main
Choose a base branch
from
PlainSeer:feature/system-theme-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Behaviour tests for the generated theme scripts, using only Node.js built-ins. | ||
| // Run directly: node apps/predbat/tests/test_web_theme.cjs [python executable] | ||
| const assert = require('node:assert/strict'); | ||
| const { execFileSync } = require('node:child_process'); | ||
| const path = require('node:path'); | ||
| const vm = require('node:vm'); | ||
|
|
||
| const header = execFileSync(process.argv[2] || 'python3', ['-c', | ||
| "from web_helper import get_header_html; print(get_header_html('Test', False, './dash', [], 'v1.0', ''))" | ||
| ], { cwd: path.resolve(__dirname, '..'), encoding: 'utf8' }); | ||
| const scripts = [...header.matchAll(/<script>([\s\S]*?)<\/script>/g)] | ||
| .map(match => match[1]).filter(script => /function (getDarkModePreference|applyDarkMode)/.test(script)); | ||
| assert.equal(scripts.length, 2); | ||
|
|
||
| function classList() { | ||
| const values = new Set(); | ||
| return { add: value => values.add(value), remove: value => values.delete(value), contains: value => values.has(value) }; | ||
| } | ||
|
|
||
| function page(stored, dark, legacy = false, mediaAvailable = true) { | ||
| const storage = new Map(stored === null ? [] : [['darkMode', stored]]); | ||
| const selector = { value: 'system' }; | ||
| const logo = { src: '', getAttribute: name => name === 'data-dark-src' ? 'dark.png' : 'light.png' }; | ||
| let listener; | ||
| let reloads = 0; | ||
| const media = { matches: dark }; | ||
| media[legacy ? 'addListener' : 'addEventListener'] = (...args) => { listener = args.at(-1); }; | ||
| const document = { | ||
| documentElement: { classList: classList() }, body: { classList: classList() }, | ||
| addEventListener: () => {}, | ||
| getElementById: id => id === 'theme-mode' ? selector : id === 'logo-image' ? logo : null | ||
| }; | ||
| const window = mediaAvailable ? { matchMedia: () => media } : {}; | ||
| const context = vm.createContext({ document, window, | ||
| localStorage: { getItem: key => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, String(value)), removeItem: key => storage.delete(key) }, | ||
| location: { reload: () => { reloads++; } } | ||
| }); | ||
| scripts.forEach(script => vm.runInContext(script, context)); | ||
| const preCssDark = document.documentElement.classList.contains('dark-mode'); | ||
| window.onload(); | ||
| return { context, document, storage, selector, logo, preCssDark, reloads: () => reloads, | ||
| osChange: value => { media.matches = value; listener(); } }; | ||
| } | ||
|
|
||
| function checkAppearance(page, dark, mode) { | ||
| assert.equal(page.document.documentElement.classList.contains('dark-mode'), dark); | ||
| assert.equal(page.document.body.classList.contains('dark-mode'), dark); | ||
| assert.equal(page.logo.src, dark ? 'dark.png' : 'light.png'); | ||
| assert.equal(page.selector.value, mode); | ||
| } | ||
|
|
||
| for (const legacy of [false, true]) { | ||
| for (const stored of [null, 'true', 'false']) { | ||
| for (const osDark of [false, true]) { | ||
| const current = page(stored, osDark, legacy); | ||
| const mode = stored === null ? 'system' : stored === 'true' ? 'dark' : 'light'; | ||
| const expected = stored === null ? osDark : stored === 'true'; | ||
| assert.equal(current.preCssDark, expected); | ||
| checkAppearance(current, expected, mode); | ||
| current.osChange(!osDark); | ||
| checkAppearance(current, stored === null ? !osDark : expected, mode); | ||
| assert.equal(current.storage.get('darkMode') ?? null, stored); | ||
| assert.equal(current.reloads(), 0); | ||
|
|
||
| for (const choice of ['light', 'dark', 'system']) { | ||
| current.context.setThemeMode(choice); | ||
| const saved = current.storage.get('darkMode') ?? null; | ||
| assert.equal(saved, choice === 'system' ? null : choice === 'dark' ? 'true' : 'false'); | ||
| const reloaded = page(saved, osDark, legacy); | ||
| checkAppearance(reloaded, choice === 'system' ? osDark : choice === 'dark', choice); | ||
| reloaded.osChange(!osDark); | ||
| checkAppearance(reloaded, choice === 'system' ? !osDark : choice === 'dark', choice); | ||
| } | ||
| assert.equal(current.reloads(), 3); | ||
| } | ||
| } | ||
| } | ||
| checkAppearance(page(null, false, false, false), false, 'system'); | ||
| checkAppearance(page('true', false, false, false), true, 'dark'); | ||
| console.log('PASS: theme choices, persistence, pre-CSS appearance, logo, live OS changes, legacy listener and missing matchMedia'); | ||
|
|
||
| // Run the actual generated Editor storage listener with an already-open editor. | ||
| const editorJs = execFileSync(process.argv[2] || 'python3', ['-c', | ||
| 'from web_helper import get_editor_js; print(get_editor_js())' | ||
| ], { cwd: path.resolve(__dirname, '..'), encoding: 'utf8' }); | ||
| const storageListener = editorJs.match(/window.addEventListener\('storage', function\(e\) \{[\s\S]*?\n \}\);/); | ||
| assert.ok(storageListener, 'Editor storage listener must exist'); | ||
| const editorPage = page('true', true); | ||
| const wrapper = { style: { backgroundColor: '#2d2d2d' } }; | ||
| const gutter = { style: {} }; | ||
| const tooltip = { style: {} }; | ||
| let editorTheme = 'monokai'; | ||
| let lintRuns = 0; | ||
| let refreshes = 0; | ||
| let onStorage; | ||
| editorPage.context.window.addEventListener = (event, callback) => { | ||
| assert.equal(event, 'storage'); | ||
| onStorage = callback; | ||
| }; | ||
| editorPage.document.querySelectorAll = selector => selector === '.CodeMirror-gutters' ? [gutter] : [tooltip]; | ||
| editorPage.context.editor = { | ||
| setOption: (option, value) => { assert.equal(option, 'theme'); editorTheme = value; }, | ||
| getWrapperElement: () => wrapper, | ||
| performLint: () => { lintRuns++; }, | ||
| refresh: () => { refreshes++; } | ||
| }; | ||
| vm.runInContext(storageListener[0], editorPage.context); | ||
| checkAppearance(editorPage, true, 'dark'); | ||
| // Another tab selects System: shared storage changes, then the Editor receives the event. | ||
| const otherTab = page('true', true); | ||
| otherTab.context.localStorage = editorPage.context.localStorage; | ||
| otherTab.context.setThemeMode('system'); | ||
| assert.equal(editorPage.context.localStorage.getItem('darkMode'), null); | ||
| onStorage({ key: 'darkMode', oldValue: 'true', newValue: null }); | ||
| assert.equal(editorTheme, 'monokai'); | ||
| assert.equal(wrapper.style.backgroundColor, '#2d2d2d'); | ||
| assert.equal(gutter.style.backgroundColor, '#2d2d2d'); | ||
| assert.equal(tooltip.style.backgroundColor, '#2d2d2d'); | ||
| assert.equal(lintRuns, 1); | ||
| assert.equal(refreshes, 1); | ||
| assert.equal(editorPage.reloads(), 0); | ||
| assert.equal(otherTab.reloads(), 1); | ||
| console.log('PASS: clearing Dark override in another tab keeps the open Editor dark on a Dark OS'); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in e7499e2: the Editor storage listener now uses getDarkModePreference(). Added a regression test confirming that clearing a saved Dark override from another tab keeps the open Editor dark when the OS appearance is Dark. The test fails before the fix and passes afterward; the related web tests and quality checks also pass.