Add a theme toggle, and fix the voice picker showing no voices
Some checks failed
CI / build-and-deploy (push) Has been cancelled
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:
@@ -36,6 +36,15 @@ export class Narrator {
|
||||
announceFrames = false;
|
||||
/** Empty means the browser's default voice. */
|
||||
voiceURI = '';
|
||||
rate = 1.1;
|
||||
pitch = 1;
|
||||
/**
|
||||
* Called when the voice list changes, so the UI can re-render. Chrome
|
||||
* reports zero voices synchronously and only fills the list later, so
|
||||
* without this the picker is stuck on whatever existed at construction.
|
||||
* @type {(() => void) | undefined}
|
||||
*/
|
||||
onVoicesChanged;
|
||||
|
||||
/** @type {number | null} */
|
||||
#lastSecond = null;
|
||||
@@ -49,11 +58,27 @@ export class Narrator {
|
||||
if (!supportsSpeech) return;
|
||||
this.#loadVoices();
|
||||
// Voices arrive asynchronously, and on some platforms only after this fires.
|
||||
speechSynthesis.onvoiceschanged = () => this.#loadVoices();
|
||||
speechSynthesis.onvoiceschanged = () => {
|
||||
this.#loadVoices();
|
||||
this.onVoicesChanged?.();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Chrome hands back ~180 voices in no useful order. Sort them so the ones
|
||||
* you'd actually pick are near the top: your own language first, then
|
||||
* offline voices ahead of network ones - the latter are useless in the field.
|
||||
*/
|
||||
#loadVoices() {
|
||||
this.#voices = speechSynthesis.getVoices();
|
||||
let primary = (navigator.language || 'en').split('-')[0].toLowerCase();
|
||||
let rank = v => (v.lang.toLowerCase().startsWith(primary) ? 0 : 1);
|
||||
this.#voices = speechSynthesis.getVoices().slice().sort(
|
||||
(a, b) =>
|
||||
rank(a) - rank(b) ||
|
||||
a.lang.localeCompare(b.lang) ||
|
||||
Number(b.localService) - Number(a.localService) ||
|
||||
a.name.localeCompare(b.name)
|
||||
);
|
||||
}
|
||||
|
||||
get voices() {
|
||||
@@ -72,8 +97,10 @@ export class Narrator {
|
||||
let utterance = new SpeechSynthesisUtterance(text);
|
||||
let voice = this.#voices.find(v => v.voiceURI === this.voiceURI);
|
||||
if (voice) utterance.voice = voice;
|
||||
// Slightly quick, so a spoken "three" lands nearer the second it names.
|
||||
utterance.rate = 1.1;
|
||||
// Default is slightly quick, so a spoken "three" lands nearer the second
|
||||
// it names.
|
||||
utterance.rate = this.rate;
|
||||
utterance.pitch = this.pitch;
|
||||
speechSynthesis.speak(utterance);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user