Add a theme toggle, and fix the voice picker showing no voices
Some checks failed
CI / build-and-deploy (push) Has been cancelled

Theme toggle sits next to the settings icon. The choice starts as 'system' and
follows the OS until you press it, after which it's explicit and persisted.

An inline script in <head> stamps the resolved theme on <html> before the
first paint - resolving it from JS after load means a visible flash of dark on
the way to light. Because that attribute is always present, the stylesheet
drops its prefers-color-scheme query entirely rather than having a media query
and an explicit override fighting over the same tokens.

The voice picker was effectively empty: Chrome reports zero voices
synchronously and only fills the list when voiceschanged fires, and while the
narrator did reload them, nothing told preact to re-render - so the dropdown
kept whatever existed at construction, which was nothing. It now notifies, and
the list arrives (181 voices here).

That many voices needs shape, so they're sorted with your own language first
and offline voices ahead of network ones, then grouped into optgroups by
language. Network voices are marked as such since they're useless offline,
which for an app built to work in a field matters.

Speed and pitch are adjustable too, both fed through to every utterance.

The settings button gains a class of its own: the header now has two icon
buttons, so identifying it by .icon-button alone hits the theme toggle.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QK7PKpRVoy69Fpa32R8abb
This commit is contained in:
Jon
2026-08-02 20:17:32 +01:00
parent c3cf5ddfbf
commit 55958ae206
5 changed files with 232 additions and 30 deletions

View File

@@ -40,11 +40,15 @@ export const DEFAULT_PREFS = {
keepAwake: true,
bulbEnabled: false,
bulbSeconds: 30,
/** 'system' follows the OS until the header toggle is used. */
theme: /** @type {'system' | 'light' | 'dark'} */ ('system'),
// Toggled from the home screen; the rest live in this drawer.
voice: false,
voiceCountFrom: 5,
voiceURI: '',
voiceAnnounceFrames: false
voiceAnnounceFrames: false,
voiceRate: 1.1,
voicePitch: 1
};
/** @typedef {typeof DEFAULT_PREFS} Prefs */
@@ -95,6 +99,39 @@ function tabLabel(label) {
return label.replace(/^camera\s+/i, '') || label;
}
/**
* Bucket voices into <optgroup>s by language, keeping the order the narrator
* sorted them into (your own language first, offline voices before network).
*
* @param {SpeechSynthesisVoice[]} voices
*/
function groupVoices(voices) {
/** @type {{ lang: string, voices: SpeechSynthesisVoice[] }[]} */
let groups = [];
let byLang = new Map();
for (let voice of voices) {
let group = byLang.get(voice.lang);
if (!group) {
group = { lang: voice.lang, voices: [] };
byLang.set(voice.lang, group);
groups.push(group);
}
group.voices.push(voice);
}
return groups;
}
/**
* @param {number} value
* @param {number} min
* @param {number} max
* @param {number} fallback Used when the field is left empty (value is NaN).
*/
function clamp(value, min, max, fallback) {
if (!Number.isFinite(value)) return fallback;
return Math.min(max, Math.max(min, Math.round(value * 10) / 10));
}
/**
* @param {{ label: string, hint?: string, children?: any }} props
*/
@@ -398,7 +435,12 @@ export class SettingsDrawer extends Component {
),
h(
Row,
{ label: 'Voice' },
{
label: 'Voice',
hint: voices.length
? `${voices.length} available. Network voices won't work offline.`
: 'Loading the system voice list…'
},
h(
'select',
{
@@ -406,11 +448,56 @@ export class SettingsDrawer extends Component {
onChange: e => setPref({ voiceURI: e.currentTarget.value })
},
h('option', { value: '' }, 'Browser default'),
voices.map(v =>
h('option', { key: v.voiceURI, value: v.voiceURI }, `${v.name} (${v.lang})`)
// Grouped by language: a flat list of ~180 is unusable.
groupVoices(voices).map(group =>
h(
'optgroup',
{ key: group.lang, label: group.lang },
group.voices.map(v =>
h(
'option',
{ key: v.voiceURI, value: v.voiceURI },
v.localService ? v.name : `${v.name} (network)`
)
)
)
)
)
),
h(
Row,
{
label: 'Speed',
hint: 'Quicker means a spoken number lands nearer the second it names.'
},
h('input', {
type: 'number',
min: '0.5',
max: '2',
step: '0.1',
value: prefs.voiceRate,
onChange: e =>
setPref({
voiceRate: clamp(e.currentTarget.valueAsNumber, 0.5, 2, 1.1)
})
}),
h('span', { class: 'unit' }, '×')
),
h(
Row,
{ label: 'Pitch' },
h('input', {
type: 'number',
min: '0',
max: '2',
step: '0.1',
value: prefs.voicePitch,
onChange: e =>
setPref({
voicePitch: clamp(e.currentTarget.valueAsNumber, 0, 2, 1)
})
})
),
h(
Row,
{ label: 'Test' },