diff --git a/changelog.d/fixed-dashboard-tracker-sounds-held-by-autoplay.md b/changelog.d/fixed-dashboard-tracker-sounds-held-by-autoplay.md new file mode 100644 index 0000000000..6ce124317e --- /dev/null +++ b/changelog.d/fixed-dashboard-tracker-sounds-held-by-autoplay.md @@ -0,0 +1 @@ +- Hive activity monitor sounds no longer go silent after a reload: when the browser's autoplay policy holds the audio engine back (every reload — including the dashboard's automatic reload on a new hive version — starts without user activation), the 🔊 chip turns amber and a one-time toast with an **Enable sounds** action says a click or key press on the page is needed, instead of each zap/buzz being dropped silently. Gesture plays (▶ Play, turning sound on) now start their voices inside the click itself so Safari unlocks the engine, a `resume()` that settles before the state flips waits for `statechange` rather than counting as blocked, and priming also listens for click/touchend. diff --git a/src/docs/dashboard-sections.md b/src/docs/dashboard-sections.md index 3ea34a22ef..a888a184f8 100644 --- a/src/docs/dashboard-sections.md +++ b/src/docs/dashboard-sections.md @@ -646,9 +646,9 @@ Fed by `GET /api/stage-tracker`. Every issue and pull request follows the same p **Group.** The **Group** control next to **Sort** switches between **None** (one flat list, the default) and **Repo**. Grouped by repo, rows sit under a sticky header per repository showing the `owner/repo`, its row count, how many of those rows need attention, and a mini distribution of rows across the pipeline columns; the primary repo's group comes first, then groups by row count. The chosen **Sort** still orders rows within each group. A header's chevron collapses that group to its header alone; collapsed repos are remembered across reloads (`hive.tracker.group.collapsed`), as is the Group choice itself (`hive.tracker.group`), which is also available in the settings modal. Group headers are keyed by repo so a refresh keeps them in place while rows move, and a header leaves only after the last row in its group has played its exit animation. -**Sounds.** The 🔇/🔊 chip at the right end of the toolbar turns on two optional, browser-made sounds: a "bug zapper" that plays when a pull request already on the list merges while you have the page open (never on first load, never for issues, once per pull request, at most two zaps per refresh, silent while the tab is hidden), and a short bee-like buzz when an issue or pull request first appears on the list (never on first load, once per item, at most two buzzes per refresh, same hidden-tab rule). Both are off by default and remembered per browser; switching them on plays each sound once so you know what to listen for. They fire for every repo the hive governs — there is no primary-repo scoping in the detection. +**Sounds.** The 🔇/🔊 chip at the right end of the toolbar turns on two optional, browser-made sounds: a "bug zapper" that plays when a pull request already on the list merges while you have the page open (never on first load, never for issues, once per pull request, at most two zaps per refresh, silent while the tab is hidden), and a short bee-like buzz when an issue or pull request first appears on the list (never on first load, once per item, at most two buzzes per refresh, same hidden-tab rule). Both are off by default and remembered per browser; switching them on plays each sound once so you know what to listen for. They fire for every repo the hive governs — there is no primary-repo scoping in the detection. Browsers only let a page make sound after you have clicked or pressed a key on it, and that permission does not survive a reload — including the dashboard's own automatic reload when a new hive version is published. If sound is on but the browser is still holding the audio engine back, the 🔊 chip turns amber (hover it for the reason) and, once per page load, a toast offers an **Enable sounds** button; pressing it, or any click or key press on the page, releases the engine and the chip returns to normal. A zap or buzz that fell in that window is skipped (the toast says so) rather than queued to burst out later. -**Sound settings (⚙ on the card header).** The gear on the Hive activity monitor card opens an in-app settings modal with a **Play sounds** master toggle (the same preference as the toolbar chip), a **Volume** slider (0–100, applied to the built-in sounds and to your own files alike, remembered per browser), and one row per event — **Open sound** (a new issue or pull request appears) and **Merge sound** (a pull request merges). Each row shows what currently plays ("Built-in (bee buzz)" / "Built-in (zapper)" or your file's name), a **▶ Play** preview (the button reads *Playing…* while the sound runs; if the browser refuses to start audio, a toast says so rather than failing silently), a **Choose WAV…** picker that accepts `.wav` or any other audio your browser can decode (up to 4 MB; larger files are refused with a toast), and **Reset to built-in**. Uploaded files are kept in this browser only (IndexedDB) and decoded with the Web Audio API; if a stored file cannot be decoded the built-in sound plays instead and the row says so. **▶ Play both** auditions the merge sound followed by the open sound. Previews start the browser's audio engine inside the click itself and wait for it to come up (about a second at most) before playing, so they work in Safari and on a page that has never made a sound; an automatic zap or buzz on an engine the browser still holds back waits only briefly and otherwise skips rather than queueing up to burst out on your next click, and the next click or key press re-arms it. +**Sound settings (⚙ on the card header).** The gear on the Hive activity monitor card opens an in-app settings modal with a **Play sounds** master toggle (the same preference as the toolbar chip), a **Volume** slider (0–100, applied to the built-in sounds and to your own files alike, remembered per browser), and one row per event — **Open sound** (a new issue or pull request appears) and **Merge sound** (a pull request merges). Each row shows what currently plays ("Built-in (bee buzz)" / "Built-in (zapper)" or your file's name), a **▶ Play** preview (the button reads *Playing…* while the sound runs; if the browser refuses to start audio, a toast says so rather than failing silently), a **Choose WAV…** picker that accepts `.wav` or any other audio your browser can decode (up to 4 MB; larger files are refused with a toast), and **Reset to built-in**. Uploaded files are kept in this browser only (IndexedDB) and decoded with the Web Audio API; if a stored file cannot be decoded the built-in sound plays instead and the row says so. **▶ Play both** auditions the merge sound followed by the open sound. Previews start the browser's audio engine and schedule their sound inside the click itself (Safari only unlocks audio for work done synchronously in a user gesture) and otherwise wait for the engine to report it is running (about a second at most), so they work in Safari and on a page that has never made a sound; an automatic zap or buzz on an engine the browser still holds back waits only briefly and otherwise skips rather than queueing up to burst out on your next click, and the next click or key press re-arms it. **Movement animation (⚙ on the card header).** When a refresh moves an item's lit chevron to a different column — forward (advance) or backward (regress) — the row plays a short, subtle three-beat sequence so the eye can see where it went: first the chevron it is *leaving* flashes the active highlight twice (two ~140 ms pulses with a short gap), then the chosen effect travels from that chevron to the new one, and finally the new chevron lands lit — the whole sequence takes about a second. Pick the travel effect in the same settings modal, under **Movement animation** below the sound settings: **None**, **A Sliding puck** (a highlight glides from the old chevron to the new one and settles), **B Ripple** (each chevron between old and new lights in turn), **C Ghost crossfade** (the old highlight fades out while the new one fades in with a slight slide), **D Comet** (a streak with a bright head travels the strip and lands on the destination), **E Destination bump** (the destination chevron scales briefly with a fading direction arrow), or **F Colour wash** (default — a tinted sweep flows across the intermediate chevrons). Each option has a *Preview* button that plays the full flash → move → land sequence on a sample row inside the dialog. The choice is remembered per browser (`localStorage` key `hiveStageTrackerMotion`). Items that stay put never move; a row that leaves the list (merged, closed, or filtered away) collapses and fades out over `--ham-move-exit-ms` and a row that arrives fades in over `--ham-move-enter-ms`; nothing animates on first load, and at most twelve rows animate per refresh (a filter or page change that turns over more rows than that redraws at once). The list is reconciled row by row on every refresh rather than rebuilt, so a move keeps playing while the hive keeps publishing. When the operating system asks for reduced motion (`prefers-reduced-motion: reduce`), every style falls back to a single soft flash followed by a plain crossfade and departing rows only fade, regardless of the setting; **None** disables the flash, the effect and the row fades entirely. Durations, the flash timing (`--ham-move-flash-ms`, `--ham-move-flash-gap-ms`, `--ham-move-flash-count`) and tints come from the `--ham-move-*` tokens in `tokens.css`. diff --git a/src/pkg/dashboard/stage_tracker_merge_sound_test.go b/src/pkg/dashboard/stage_tracker_merge_sound_test.go index 56e42c6605..ebad7a4415 100644 --- a/src/pkg/dashboard/stage_tracker_merge_sound_test.go +++ b/src/pkg/dashboard/stage_tracker_merge_sound_test.go @@ -126,7 +126,7 @@ func TestStageTrackerMergeSoundWiring(t *testing.T) { `window.AudioContext || window.webkitAudioContext`, `if (ctx.state === 'running') return stageTrackerScheduleVoices(ctx, voice, count, gain, startOffsetS);`, `stageTrackerResumeContext(ctx, STAGE_TRACKER_AUTO_RESUME_MS).then(`, - `() => stageTrackerSoundPrime());`, + `() => stageTrackerSoundHeldNotice(true));`, `high.type = 'highpass';`, `data[i] = snap + sizzle * env;`, `ctx.createBufferSource()`, @@ -504,6 +504,7 @@ func TestStageTrackerSoundSelection(t *testing.T) { "function stageTrackerZapVoice(ctx, out, at) { out.voices.push('zap'); }", "function stageTrackerBuzzVoice(ctx, out, at) { out.voices.push('buzz'); }", "function stageTrackerSoundPrime() {}", + "function stageTrackerSoundHeldNotice() {} function stageTrackerSoundHeldClear() {}", "function stageTrackerSoundSyncButton() {}", "function stageTrackerRenderSettings() {}", "function stageTrackerPlayTest() {}", diff --git a/src/pkg/dashboard/stage_tracker_sound_resume_test.go b/src/pkg/dashboard/stage_tracker_sound_resume_test.go index 6604809b79..813a81920c 100644 --- a/src/pkg/dashboard/stage_tracker_sound_resume_test.go +++ b/src/pkg/dashboard/stage_tracker_sound_resume_test.go @@ -38,6 +38,27 @@ func TestStageTrackerSoundResumeWiring(t *testing.T) { t.Errorf("%s must await stageTrackerResumeContext", fn) } } + // A blocked automatic play must surface the autoplay affordance (which + // re-arms priming) rather than only re-arming priming silently. + if pv := jsFunc(t, html, "stageTrackerPlayVoices"); !strings.Contains(pv, "stageTrackerSoundHeldNotice(true)") { + t.Errorf("stageTrackerPlayVoices must call stageTrackerSoundHeldNotice(true) when the context stays blocked:\n%s", pv) + } + // Gesture plays schedule inside the gesture (WebKit unlock) and discard a + // context the browser refused so the queued voices never burst later. + if pg := jsFunc(t, html, "stageTrackerPlayOnGesture"); !strings.Contains(pg, "const scheduled = !!schedule(ctx);") || !strings.Contains(pg, "stageTrackerDiscardContext(ctx)") { + t.Errorf("stageTrackerPlayOnGesture must schedule synchronously inside the gesture and discard a refused context:\n%s", pg) + } + for _, want := range []string{"'pointerdown'", "'keydown'", "'click'", "'touchend'"} { + if !strings.Contains(jsConstLine(t, html, "STAGE_TRACKER_PRIME_EVENTS"), want) { + t.Errorf("STAGE_TRACKER_PRIME_EVENTS must arm %s", want) + } + } + if !strings.Contains(html, `.ham-sound.ham-sound-blocked`) { + t.Errorf("index.html missing the .ham-sound-blocked chip style") + } + if init := jsFunc(t, html, "stageTrackerSoundInit"); !strings.Contains(init, "stageTrackerHasUserActivation(window.navigator)") || !strings.Contains(init, "stageTrackerSoundHeldNotice(false)") { + t.Errorf("stageTrackerSoundInit must show the held-back affordance on a document without user activation:\n%s", init) + } num := func(name string) int { m := regexp.MustCompile(`const ` + name + ` = (\d+);`).FindStringSubmatch(html) if m == nil { @@ -62,7 +83,12 @@ func TestStageTrackerSoundResumeWiring(t *testing.T) { // "Playing…" meanwhile; an automatic play on a context that resumes quickly // plays exactly once; one that never resumes plays nothing and re-arms // priming; a rejected resume() toasts; 'interrupted' behaves like suspended; -// a context that is already running schedules synchronously. +// a context that is already running schedules synchronously. Also: a +// gesture play starts its voices inside the gesture (WebKit unlock), a +// resume() that resolves before the state flips waits for statechange, a +// refused context is discarded with its queued voices, and a blocked +// automatic play (or a reload with sound on) shows the one-time "Enable +// sounds" affordance that resumes the context from its own click. func TestStageTrackerSoundResumeAsync(t *testing.T) { if _, err := exec.LookPath("node"); err != nil { t.Skip("node not on PATH — async resume paths were NOT executed by this run") @@ -70,7 +96,9 @@ func TestStageTrackerSoundResumeAsync(t *testing.T) { html := indexHTML(t) script := strings.Join([]string{ "const assert = require('assert');", - "const window = {}; const document = { hidden: false, listeners: {}, addEventListener(n, f) { (this.listeners[n] = this.listeners[n] || []).push(f); }, removeEventListener(n, f) { this.listeners[n] = (this.listeners[n] || []).filter(g => g !== f); } };", + "const chip = { textContent: '🔇', attrs: {}, classes: new Set(), setAttribute(k, v) { this.attrs[k] = v; }, getAttribute(k) { return this.attrs[k]; }, removeAttribute(k) { delete this.attrs[k]; }, classList: { add: c => chip.classes.add(c), remove: c => chip.classes.delete(c), contains: c => chip.classes.has(c) } };", + "const window = { navigator: { userActivation: { hasBeenActive: false } } }; const document = { hidden: false, listeners: {}, addEventListener(n, f) { (this.listeners[n] = this.listeners[n] || []).push(f); }, removeEventListener(n, f) { this.listeners[n] = (this.listeners[n] || []).filter(g => g !== f); }, getElementById(id) { return id === 'activity-monitor-sound' ? chip : null; } };", + "const localStorage = { store: {}, getItem(k) { return k in this.store ? this.store[k] : null; }, setItem(k, v) { this.store[k] = String(v); } };", jsConstLine(t, html, "STAGE_TRACKER_SOUND_MAX_PLAYS"), jsConstLine(t, html, "STAGE_TRACKER_SOUND_OFFSET_S"), jsConstLine(t, html, "STAGE_TRACKER_SOUND_MASTER_GAIN"), @@ -81,6 +109,15 @@ func TestStageTrackerSoundResumeAsync(t *testing.T) { jsConstLine(t, html, "STAGE_TRACKER_SOUND_BLOCKED_MSG"), jsConstLine(t, html, "STAGE_TRACKER_SOUND_UNSUPPORTED_MSG"), jsConstLine(t, html, "STAGE_TRACKER_SOUND_PLAYING_LABEL"), + jsConstLine(t, html, "STAGE_TRACKER_SOUND_HELD_MSG"), + jsConstLine(t, html, "STAGE_TRACKER_SOUND_HELD_SKIPPED_MSG"), + jsConstLine(t, html, "STAGE_TRACKER_SOUND_HELD_ACTION"), + jsConstLine(t, html, "STAGE_TRACKER_SOUND_HELD_TOAST_MS"), + jsConstLine(t, html, "STAGE_TRACKER_SOUND_HELD_CLASS"), + jsConstLine(t, html, "STAGE_TRACKER_SOUND_HELD_TITLE"), + jsConstLine(t, html, "STAGE_TRACKER_PRIME_EVENTS"), + jsConstLine(t, html, "STAGE_TRACKER_SOUND_KEY"), + jsConstLine(t, html, "STAGE_TRACKER_VOLUME_KEY"), jsConstLine(t, html, "STAGE_TRACKER_ZAP_S"), jsConstLine(t, html, "STAGE_TRACKER_BUZZ_S"), // Short windows keep the fixture fast; the real values are pinned by @@ -88,7 +125,13 @@ func TestStageTrackerSoundResumeAsync(t *testing.T) { "const STAGE_TRACKER_GESTURE_RESUME_MS = 80; const STAGE_TRACKER_AUTO_RESUME_MS = 40;", jsFunc(t, html, "stageTrackerAudioContext"), jsFunc(t, html, "stageTrackerResumeContext"), + jsFunc(t, html, "stageTrackerDiscardContext"), jsFunc(t, html, "stageTrackerSoundPrime"), + jsFunc(t, html, "stageTrackerHasUserActivation"), + jsFunc(t, html, "stageTrackerSoundHeldNotice"), + jsFunc(t, html, "stageTrackerSoundHeldClear"), + jsFunc(t, html, "stageTrackerSoundSyncButton"), + jsFunc(t, html, "stageTrackerSoundInit"), jsFunc(t, html, "stageTrackerScheduleVoices"), jsFunc(t, html, "stageTrackerPlayVoices"), jsFunc(t, html, "stageTrackerPlayOnGesture"), @@ -107,48 +150,62 @@ func TestStageTrackerSoundResumeAsync(t *testing.T) { "function stageTrackerBuzzVoice(ctx, out, at) { out.voices.push('buzz@' + at); }", "function stageTrackerDecodeClips() { if (decodeThrows) throw new Error('decode exploded'); }", "let decodeThrows = false;", - "const toasts = []; function showToast(m, type) { toasts.push({ m, type }); }", + "const toasts = []; function showToast(m, type, opts) { toasts.push({ m, type, opts: opts || {} }); }", + "function stageTrackerClipsLoad() { return Promise.resolve(); } function stageTrackerRenderSettings() {}", "const STAGE_TRACKER_SOUND_KINDS = { open: { label: 'Open sound' }, merge: { label: 'Merge sound' } };", - "var _stageTrackerAudioCtx = null; var _stageTrackerSoundOn = true; var _stageTrackerSoundPrimed = false;", + "var _stageTrackerAudioCtx = null; var _stageTrackerSoundOn = true; var _stageTrackerSoundPrimed = false; var _stageTrackerSoundHeldShown = false;", "var _stageTrackerVolume = STAGE_TRACKER_VOLUME_DEFAULT; var _stageTrackerClips = { open: null, merge: null };", }, "\n") + ` // Fake AudioContext: resume() is asynchronous and the test decides when (or // whether) it settles. 'mode' is 'later' (resolve after resumeDelayMs), +// 'stale' (resolve at once with the state still 'suspended', then flip to +// 'running' and fire statechange after resumeDelayMs — WebKit's ordering), // 'never', or 'reject'. Voices land in the master gain it hands out. const gains = []; -let created = 0; +let created = 0, closed = 0; let mode = 'later', resumeDelayMs = 5; class FakeAudioContext { - constructor() { created++; this.state = 'suspended'; this.currentTime = 0; this.sampleRate = 48000; this.destination = {}; this.resumes = 0; } - createGain() { const g = { gain: { value: 1 }, voices: [], connect() {} }; gains.push(g); return g; } + constructor() { created++; this.state = 'suspended'; this.currentTime = 0; this.sampleRate = 48000; this.destination = {}; this.resumes = 0; this.stateListeners = []; } + addEventListener(n, f) { if (n === 'statechange') this.stateListeners.push(f); } + removeEventListener(n, f) { this.stateListeners = this.stateListeners.filter(g => g !== f); } + close() { closed++; this.state = 'closed'; return Promise.resolve(); } + createGain() { const g = { gain: { value: 1 }, voices: [], ctx: this, connect() {} }; gains.push(g); return g; } createBufferSource() { return { buffer: null, connect(out) { this.out = out; }, start(at) { this.out.voices.push('clip@' + at); } }; } resume() { this.resumes++; if (mode === 'reject') return Promise.reject(new Error('NotAllowedError')); if (mode === 'never') return new Promise(() => {}); + if (mode === 'stale') { setTimeout(() => { this.state = 'running'; this.stateListeners.slice().forEach(f => f()); }, resumeDelayMs); return Promise.resolve(); } return new Promise(resolve => setTimeout(() => { this.state = 'running'; resolve(); }, resumeDelayMs)); } } window.AudioContext = FakeAudioContext; const sleep = ms => new Promise(r => setTimeout(r, ms)); -const voices = () => gains.flatMap(g => g.voices); +// Voices on a context that is still open — a discarded (closed) context's +// voices can never sound, so they do not count. +const voices = () => gains.filter(g => g.ctx.state !== 'closed').flatMap(g => g.voices); const fakeButton = () => ({ textContent: '▶ Play', attrs: {}, classes: new Set(), setAttribute(k, v) { this.attrs[k] = v; }, removeAttribute(k) { delete this.attrs[k]; }, classList: { add: c => {}, remove: c => {} } }); -const reset = () => { gains.length = 0; toasts.length = 0; _stageTrackerAudioCtx = null; _stageTrackerSoundPrimed = false; document.listeners = {}; }; +const reset = () => { gains.length = 0; toasts.length = 0; closed = 0; _stageTrackerAudioCtx = null; _stageTrackerSoundPrimed = false; _stageTrackerSoundHeldShown = false; _stageTrackerSoundOn = true; chip.classes.clear(); chip.attrs = {}; document.listeners = {}; }; +const heldToasts = () => toasts.filter(t => t.m.indexOf(STAGE_TRACKER_SOUND_HELD_MSG) === 0); (async () => { - // (a) Preview inside a gesture on a suspended context: nothing plays - // synchronously, the button reads "Playing…", and the voice is scheduled - // once resume() resolves. The context is created inside the handler. + // (a) Preview inside a gesture on a suspended context: resume() is called + // and the voice is scheduled synchronously inside the gesture (WebKit + // unlocks the context only for a resume()/start() made while processing + // the gesture), the button reads "Playing…" until the context runs, and + // the preview resolves true once it does. The context is created inside + // the handler. reset(); const btn = fakeButton(); const p = stageTrackerPreviewSound('merge', btn); assert.strictEqual(created, 1, 'context created inside the gesture handler'); assert.strictEqual(_stageTrackerAudioCtx.state, 'suspended', 'state is still stale-suspended synchronously'); - assert.deepStrictEqual(voices(), [], 'nothing scheduled before resume() settles'); + assert.strictEqual(_stageTrackerAudioCtx.resumes, 1, 'resume() called synchronously inside the gesture'); + assert.deepStrictEqual(voices(), ['zap@0'], 'the voice is started synchronously inside the gesture'); assert.strictEqual(btn.textContent, STAGE_TRACKER_SOUND_PLAYING_LABEL); assert.strictEqual(btn.attrs['aria-busy'], 'true'); - assert.strictEqual(await p, true, 'preview resolves true once scheduled'); - assert.deepStrictEqual(voices(), ['zap@0'], 'the preview voice plays after resume() resolves'); + assert.strictEqual(await p, true, 'preview resolves true once the context runs'); + assert.deepStrictEqual(voices(), ['zap@0'], 'no second voice after resume() resolves'); assert.deepStrictEqual(toasts, [], 'no toast on success'); await sleep(Math.ceil(STAGE_TRACKER_ZAP_S * 1000) + 10); assert.strictEqual(btn.textContent, '▶ Play', 'label restored after the sound length'); @@ -175,6 +232,8 @@ const reset = () => { gains.length = 0; toasts.length = 0; _stageTrackerAudioCtx assert.strictEqual(voices().length, STAGE_TRACKER_SOUND_MAX_PLAYS, 'scheduled once resume() resolved, capped'); assert.strictEqual(gains.length, 1, 'exactly one play, not a burst'); assert.strictEqual(_stageTrackerSoundPrimed, false, 'priming not re-armed when the resume succeeded'); + assert.deepStrictEqual(heldToasts(), [], 'no held-back toast when the resume succeeded'); + assert.ok(!chip.classes.has(STAGE_TRACKER_SOUND_HELD_CLASS), 'chip not marked held when the resume succeeded'); await sleep(STAGE_TRACKER_AUTO_RESUME_MS + 10); assert.strictEqual(gains.length, 1, 'the timed-out branch did not fire after the resolved one'); @@ -186,22 +245,52 @@ const reset = () => { gains.length = 0; toasts.length = 0; _stageTrackerAudioCtx await sleep(STAGE_TRACKER_AUTO_RESUME_MS + 10); assert.deepStrictEqual(voices(), [], 'nothing queued for a later burst'); assert.strictEqual(_stageTrackerSoundPrimed, true, 'priming re-armed'); - assert.strictEqual((document.listeners.pointerdown || []).length, 1); - assert.strictEqual((document.listeners.keydown || []).length, 1); + for (const n of ['pointerdown', 'keydown', 'click', 'touchend']) assert.strictEqual((document.listeners[n] || []).length, 1, n + ' armed'); + // The dropped play is not silent: the chip goes amber and one toast with + // an "Enable sounds" action explains the autoplay policy. + assert.ok(chip.classes.has(STAGE_TRACKER_SOUND_HELD_CLASS), 'chip marked held'); + assert.strictEqual(chip.attrs.title, STAGE_TRACKER_SOUND_HELD_TITLE); + assert.strictEqual(heldToasts().length, 1, 'one held-back toast'); + assert.strictEqual(heldToasts()[0].m, STAGE_TRACKER_SOUND_HELD_MSG + STAGE_TRACKER_SOUND_HELD_SKIPPED_MSG); + assert.strictEqual(heldToasts()[0].type, 'warning'); + assert.strictEqual(heldToasts()[0].opts.actionLabel, STAGE_TRACKER_SOUND_HELD_ACTION); + assert.strictEqual(typeof heldToasts()[0].opts.onAction, 'function'); + // A second dropped play re-marks the chip but never toasts again. + assert.strictEqual(stageTrackerPlayZap(1), false); + await sleep(STAGE_TRACKER_AUTO_RESUME_MS + 10); + assert.strictEqual(heldToasts().length, 1, 'the toast is shown once per page load'); // A gesture whose resume() does not settle keeps the listeners armed. document.listeners.pointerdown[0](); await sleep(STAGE_TRACKER_GESTURE_RESUME_MS + 10); assert.strictEqual(_stageTrackerSoundPrimed, true, 'still primed after a blocked gesture'); assert.strictEqual(document.listeners.pointerdown.length, 1, 'listeners kept (not once:true) until a resume succeeds'); - // The next gesture succeeds: it resumes the context and disarms. + assert.ok(chip.classes.has(STAGE_TRACKER_SOUND_HELD_CLASS), 'chip still held after a blocked gesture'); + // The next gesture (a click this time) succeeds: it resumes the context, + // disarms and clears the affordance. mode = 'later'; - document.listeners.pointerdown[0](); + document.listeners.click[0](); await sleep(resumeDelayMs + 10); assert.strictEqual(_stageTrackerAudioCtx.state, 'running'); assert.strictEqual(_stageTrackerSoundPrimed, false, 'disarmed after a successful resume'); - assert.strictEqual(document.listeners.pointerdown.length, 0); - assert.strictEqual(document.listeners.keydown.length, 0); + for (const n of ['pointerdown', 'keydown', 'click', 'touchend']) assert.strictEqual(document.listeners[n].length, 0, n + ' disarmed'); assert.deepStrictEqual(voices(), [], 'priming itself never plays'); + assert.ok(!chip.classes.has(STAGE_TRACKER_SOUND_HELD_CLASS), 'chip cleared once the context runs'); + assert.strictEqual(chip.attrs.title, 'Sound on (merge zap + new-item buzz) — turn off', 'chip title restored'); + + // The toast's "Enable sounds" action is itself the gesture: it resumes + // the context and clears the affordance without playing anything. + reset(); + mode = 'never'; + assert.strictEqual(stageTrackerPlayBuzz(1), false); + await sleep(STAGE_TRACKER_AUTO_RESUME_MS + 10); + assert.strictEqual(heldToasts().length, 1); + mode = 'later'; + heldToasts()[0].opts.onAction(); + await sleep(resumeDelayMs + 10); + assert.strictEqual(_stageTrackerAudioCtx.state, 'running'); + assert.ok(!chip.classes.has(STAGE_TRACKER_SOUND_HELD_CLASS), 'action clears the chip'); + assert.deepStrictEqual(voices(), [], 'the action plays nothing'); + assert.strictEqual(toasts.length, 1, 'no blocked toast from the action'); // (d) resume() rejects: the preview toasts and resolves false; label // restored at once. An automatic play re-arms priming instead of toasting. @@ -214,10 +303,13 @@ const reset = () => { gains.length = 0; toasts.length = 0; _stageTrackerAudioCtx assert.strictEqual(toasts[0].type, 'warning'); await sleep(1); assert.strictEqual(rb.textContent, '▶ Play', 'label restored after a blocked preview'); - assert.deepStrictEqual(voices(), []); + assert.strictEqual(closed, 1, 'the refused context is closed'); + assert.strictEqual(_stageTrackerAudioCtx, null, 'and dropped, so the next play starts fresh'); + assert.deepStrictEqual(voices(), [], 'the voice queued inside the gesture went with it'); assert.strictEqual(stageTrackerPlayZap(1), false); await sleep(5); - assert.strictEqual(toasts.length, 1, 'automatic plays never toast'); + assert.strictEqual(toasts.length, 2, 'an automatic play never shows the blocked toast, only the held-back one'); + assert.strictEqual(heldToasts().length, 1); assert.strictEqual(_stageTrackerSoundPrimed, true); // Gesture that times out (resume() pending past the gesture window) toasts too. @@ -232,7 +324,7 @@ const reset = () => { gains.length = 0; toasts.length = 0; _stageTrackerAudioCtx mode = 'later'; stageTrackerAudioContext().state = 'interrupted'; const ip = stageTrackerPreviewSound('merge', null); - assert.deepStrictEqual(voices(), []); + assert.deepStrictEqual(voices(), ['zap@0'], 'scheduled inside the gesture'); assert.strictEqual(await ip, true); assert.deepStrictEqual(voices(), ['zap@0']); reset(); @@ -241,14 +333,58 @@ const reset = () => { gains.length = 0; toasts.length = 0; _stageTrackerAudioCtx await sleep(resumeDelayMs + 10); assert.deepStrictEqual(voices(), ['buzz@0'], 'automatic play after an interrupted context resumes'); - // A resume() that resolves while the state stays blocked counts as blocked. + // (f) WebKit ordering: resume() resolves while the state still reads + // 'suspended' and statechange flips it a beat later. That is not a + // blocked context — the play waits for the statechange and succeeds. + reset(); + mode = 'stale'; + assert.strictEqual(await stageTrackerPreviewSound('merge', null), true); + assert.deepStrictEqual(toasts, [], 'a stale-but-resuming context never toasts'); + assert.deepStrictEqual(voices(), ['zap@0']); + assert.strictEqual(_stageTrackerAudioCtx.stateListeners.length, 0, 'statechange listener removed once settled'); + reset(); + assert.strictEqual(stageTrackerPlayBuzz(1), false); + await sleep(resumeDelayMs + 10); + assert.deepStrictEqual(voices(), ['buzz@0'], 'automatic play on a stale-resolving context plays once it runs'); + assert.deepStrictEqual(heldToasts(), []); + // A resume() that resolves and a state that never flips is still blocked, + // on the deadline. reset(); + mode = 'later'; const stuck = stageTrackerAudioContext(); stuck.resume = () => Promise.resolve(); assert.strictEqual(await stageTrackerPreviewSound('merge', null), false); assert.strictEqual(toasts.length, 1); assert.deepStrictEqual(voices(), []); + // (g) Sound left on across a reload: on a document without user + // activation the affordance shows at init, before any event is dropped, + // and does not when the document has been interacted with. + reset(); + localStorage.store[STAGE_TRACKER_SOUND_KEY] = '1'; + window.navigator.userActivation.hasBeenActive = false; + stageTrackerSoundInit(); + assert.strictEqual(_stageTrackerSoundOn, true); + assert.strictEqual(heldToasts().length, 1, 'held-back toast at init without activation'); + assert.strictEqual(heldToasts()[0].m, STAGE_TRACKER_SOUND_HELD_MSG, 'no "skipped" suffix before anything was dropped'); + assert.ok(chip.classes.has(STAGE_TRACKER_SOUND_HELD_CLASS)); + assert.strictEqual(_stageTrackerSoundPrimed, true, 'priming armed by the notice'); + reset(); + window.navigator.userActivation.hasBeenActive = true; + stageTrackerSoundInit(); + assert.deepStrictEqual(heldToasts(), [], 'no toast when the document has user activation'); + assert.ok(!chip.classes.has(STAGE_TRACKER_SOUND_HELD_CLASS)); + assert.strictEqual(_stageTrackerSoundPrimed, true, 'priming still armed for a browser that wants a gesture anyway'); + reset(); + localStorage.store[STAGE_TRACKER_SOUND_KEY] = '0'; + window.navigator.userActivation.hasBeenActive = false; + stageTrackerSoundInit(); + assert.strictEqual(_stageTrackerSoundOn, false); + assert.deepStrictEqual(heldToasts(), [], 'sound off: nothing to hold back, no toast'); + assert.strictEqual(stageTrackerHasUserActivation({}), false, 'no userActivation API counts as no activation'); + assert.strictEqual(stageTrackerHasUserActivation(undefined), false); + _stageTrackerSoundOn = true; + // Clip decoding that throws cannot abort context creation. reset(); decodeThrows = true; diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 44d9018869..9b41d09395 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1338,6 +1338,8 @@ /* Sound toggle (merge zap + new-item buzz): a speaker chip at the toolbar's far end; pressed = on. */ .ham-sound { margin-left: auto; padding: var(--sp-1) var(--sp-2); min-width: 2em; line-height: 1; } .ham-sound[aria-pressed="true"] { color: var(--green); border-color: var(--green); } + /* Sound on but the browser holds audio back until a click/key on the page (autoplay policy). */ + .ham-sound.ham-sound-blocked, .ham-sound[aria-pressed="true"].ham-sound-blocked { color: var(--amber); border-color: var(--amber); } /* Settings modal rows: one per sound — label, current clip, actions. */ .ham-sound-row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); padding-top: var(--sp-2); border-top: 1px solid var(--border); font-size: var(--fs-xs); } .ham-sound-row-label { color: var(--text); font-weight: 600; flex: 1 1 14rem; } @@ -16471,7 +16473,17 @@

All labels

AudioContext is created lazily on a user gesture and resumed when suspended; a context the autoplay policy still holds suspended at play time re-arms the gesture listeners instead of queueing stale - voices. */ + voices. A reload (the SPA reloads itself whenever the hive deploys + a new version) is a fresh document with no user activation, so a + context created for the first automatic play after it is suspended + and every zap/buzz is dropped until the user clicks or presses a + key on the page: the chip turns amber and a one-time toast with an + "Enable sounds" action says so, instead of failing silently. Gesture + plays (▶ Play, turning sound on) start their voices inside the + gesture itself alongside resume() — WebKit unlocks the context on + either, and only inside the gesture — and a resume() that resolves + before the state has flipped waits for statechange rather than + counting as blocked. */ const STAGE_TRACKER_SOUND_KEY = 'hive.tracker.sound'; const STAGE_TRACKER_SOUND_MAX_PLAYS = 2; const STAGE_TRACKER_SOUND_OFFSET_S = 0.26; @@ -16516,6 +16528,19 @@

All labels

const STAGE_TRACKER_SOUND_BLOCKED_MSG = 'The browser blocked audio for this page — click ▶ Play again, or allow sound/auto-play for this site in the browser\'s site settings.'; const STAGE_TRACKER_SOUND_UNSUPPORTED_MSG = 'This browser has no Web Audio support, so the tracker sounds cannot play.'; const STAGE_TRACKER_SOUND_PLAYING_LABEL = 'Playing…'; + // Autoplay-policy affordance: shown once per page load when sound is on + // but the browser holds the audio engine back until a click or key press + // on this page (every reload — including the automatic one on a new hive + // version — starts without user activation). + const STAGE_TRACKER_SOUND_HELD_MSG = 'Tracker sounds are on, but the browser holds audio back until you click or press a key on this page.'; + const STAGE_TRACKER_SOUND_HELD_SKIPPED_MSG = ' A zap or buzz was just skipped.'; + const STAGE_TRACKER_SOUND_HELD_ACTION = 'Enable sounds'; + const STAGE_TRACKER_SOUND_HELD_TOAST_MS = 60000; + const STAGE_TRACKER_SOUND_HELD_CLASS = 'ham-sound-blocked'; + const STAGE_TRACKER_SOUND_HELD_TITLE = 'Sound on, but held back by the browser until you click or press a key on this page'; + // Gestures the autoplay policies count as user activation: WebKit does + // not treat every pointerdown as one, so click and touchend are armed too. + const STAGE_TRACKER_PRIME_EVENTS = Object.freeze(['pointerdown', 'keydown', 'click', 'touchend']); const STAGE_TRACKER_SOUND_KINDS = Object.freeze({ open: { label: 'Open sound', builtin: 'Built-in (bee buzz)', event: 'a new issue or pull request appears' }, merge: { label: 'Merge sound', builtin: 'Built-in (zapper)', event: 'a pull request merges' }, @@ -16527,6 +16552,7 @@

All labels

var _stageTrackerAudioCtx = null; var _stageTrackerSoundOn = false; var _stageTrackerSoundPrimed = false; + var _stageTrackerSoundHeldShown = false; var _stageTrackerVolume = STAGE_TRACKER_VOLUME_DEFAULT; // kind → { name, bytes (ArrayBuffer), buffer (AudioBuffer|null), failed } var _stageTrackerClips = { open: null, merge: null }; @@ -16634,26 +16660,41 @@

All labels

// Resolves with ctx once it is running. Any other state ('suspended', // Safari's 'interrupted', …) gets a resume(); the promise rejects when - // resume() throws or rejects, resolves without the context running, or - // does not settle within timeoutMs. + // resume() throws or rejects, or when the context is not running within + // timeoutMs. A resume() that resolves while the state still reads + // blocked is not a verdict — WebKit can settle the promise a beat before + // the state flips — so the context's statechange is awaited until the + // deadline. function stageTrackerResumeContext(ctx, timeoutMs) { if (!ctx) return Promise.reject(new Error('no AudioContext')); if (ctx.state === 'running') return Promise.resolve(ctx); return new Promise((resolve, reject) => { let settled = false; + const canListen = typeof ctx.addEventListener === 'function'; + const onState = () => { if (ctx.state === 'running') finish(true); }; const finish = (ok, err) => { if (settled) return; settled = true; clearTimeout(timer); + if (canListen) ctx.removeEventListener('statechange', onState); if (ok) resolve(ctx); else reject(err || new Error('AudioContext did not resume')); }; const timer = setTimeout(() => finish(false, new Error('AudioContext resume timed out')), timeoutMs); + if (canListen) ctx.addEventListener('statechange', onState); let p; try { p = ctx.resume(); } catch (e) { p = Promise.reject(e); } - Promise.resolve(p).then(() => finish(ctx.state === 'running'), e => finish(false, e)); + Promise.resolve(p).then(() => { if (ctx.state === 'running') finish(true); }, e => finish(false, e)); }); } + // Drops the context so voices queued on it can never burst out later; + // the next play creates a fresh one. Decoded clips are plain AudioBuffers + // and survive. + function stageTrackerDiscardContext(ctx) { + if (_stageTrackerAudioCtx === ctx) _stageTrackerAudioCtx = null; + try { const p = ctx && typeof ctx.close === 'function' && ctx.close(); if (p && typeof p.catch === 'function') p.catch(() => {}); } catch {} + } + // Prime the context on a gesture after load when sound was left on in a // previous visit, so a merge that lands before the user clicks anything // in the tracker can still play (autoplay policy). The listeners stay @@ -16666,8 +16707,7 @@

All labels

let inFlight = false; const disarm = () => { _stageTrackerSoundPrimed = false; - document.removeEventListener('pointerdown', prime); - document.removeEventListener('keydown', prime); + STAGE_TRACKER_PRIME_EVENTS.forEach(n => document.removeEventListener(n, prime)); }; const prime = () => { if (!_stageTrackerSoundOn) { disarm(); return; } @@ -16675,10 +16715,43 @@

All labels

const ctx = stageTrackerAudioContext(); if (!ctx) { disarm(); return; } inFlight = true; - stageTrackerResumeContext(ctx, STAGE_TRACKER_GESTURE_RESUME_MS).then(disarm, () => { inFlight = false; }); + stageTrackerResumeContext(ctx, STAGE_TRACKER_GESTURE_RESUME_MS).then(() => { disarm(); stageTrackerSoundHeldClear(); }, () => { inFlight = false; }); }; - document.addEventListener('pointerdown', prime, { passive: true }); - document.addEventListener('keydown', prime, { passive: true }); + STAGE_TRACKER_PRIME_EVENTS.forEach(n => document.addEventListener(n, prime, { passive: true })); + } + + // Pure: whether this document has had the user activation the autoplay + // policies want before an AudioContext may run outside a gesture. A + // browser without navigator.userActivation is assumed not to have it. + function stageTrackerHasUserActivation(nav) { + const ua = nav && nav.userActivation; + return !!(ua && ua.hasBeenActive); + } + + // The autoplay-policy affordance: the chip turns amber and, once per + // page load, a toast with an "Enable sounds" action explains that a + // click or key press on the page is needed. The action runs inside the + // click, so it is itself the gesture that resumes the context. + function stageTrackerSoundHeldNotice(skipped) { + const b = document.getElementById('activity-monitor-sound'); + if (b) { + b.classList.add(STAGE_TRACKER_SOUND_HELD_CLASS); + b.setAttribute('title', STAGE_TRACKER_SOUND_HELD_TITLE); + } + stageTrackerSoundPrime(); + if (_stageTrackerSoundHeldShown) return; + _stageTrackerSoundHeldShown = true; + showToast(STAGE_TRACKER_SOUND_HELD_MSG + (skipped ? STAGE_TRACKER_SOUND_HELD_SKIPPED_MSG : ''), 'warning', { + autoDismissMs: STAGE_TRACKER_SOUND_HELD_TOAST_MS, + actionLabel: STAGE_TRACKER_SOUND_HELD_ACTION, + onAction: () => { stageTrackerPlayOnGesture(() => true).then(ok => { if (ok) stageTrackerSoundHeldClear(); }); }, + }); + } + function stageTrackerSoundHeldClear() { + const b = document.getElementById('activity-monitor-sound'); + if (!b) return; + b.classList.remove(STAGE_TRACKER_SOUND_HELD_CLASS); + stageTrackerSoundSyncButton(); } // Baked sample by sample like the reference render: the snap is a raw @@ -16756,7 +16829,8 @@

All labels

// Automatic play (a merge or new item landed): schedules at once on a // running context. A context the autoplay policy still holds gets one // short resume() window; voices that would otherwise burst out together - // on the next gesture are dropped and gesture priming is re-armed instead. + // on the next gesture are dropped, gesture priming is re-armed and the + // held-back affordance is shown instead. // Returns true only when the voices were scheduled synchronously. function stageTrackerPlayVoices(voice, count, gain, startOffsetS) { const ctx = stageTrackerAudioContext(); @@ -16764,22 +16838,28 @@

All labels

if (ctx.state === 'running') return stageTrackerScheduleVoices(ctx, voice, count, gain, startOffsetS); stageTrackerResumeContext(ctx, STAGE_TRACKER_AUTO_RESUME_MS).then( () => stageTrackerScheduleVoices(ctx, voice, count, gain, startOffsetS), - () => stageTrackerSoundPrime()); + () => stageTrackerSoundHeldNotice(true)); return false; } // Gesture play (▶ Play, ▶ Play both, turning sound on): must be called - // from the gesture handler itself. Creates the context there, waits for - // resume() and then calls schedule(ctx). Resolves true when scheduled; - // when the browser refuses, toasts why and resolves false — a preview - // never fails silently. + // from the gesture handler itself. Creates the context there and, when + // it is not yet running, calls resume() and schedule(ctx) synchronously + // inside the gesture — WebKit only unlocks a context for a resume() or a + // node start() made while processing the gesture, and a promise + // continuation is no longer that. Resolves true once the context runs; + // when the browser refuses, the context (and the voices queued on it) + // is discarded, a toast says why and it resolves false — a preview never + // fails silently. function stageTrackerPlayOnGesture(schedule) { const ctx = stageTrackerAudioContext(); if (!ctx) { showToast(STAGE_TRACKER_SOUND_UNSUPPORTED_MSG, 'error'); return Promise.resolve(false); } if (ctx.state === 'running') return Promise.resolve(!!schedule(ctx)); - return stageTrackerResumeContext(ctx, STAGE_TRACKER_GESTURE_RESUME_MS).then( - () => !!schedule(ctx), - () => { showToast(STAGE_TRACKER_SOUND_BLOCKED_MSG, 'warning'); return false; }); + const resumed = stageTrackerResumeContext(ctx, STAGE_TRACKER_GESTURE_RESUME_MS); + const scheduled = !!schedule(ctx); + return resumed.then( + () => { stageTrackerSoundHeldClear(); return scheduled; }, + () => { stageTrackerDiscardContext(ctx); showToast(STAGE_TRACKER_SOUND_BLOCKED_MSG, 'warning'); return false; }); } // Marks btn as playing for the life of promise plus lengthS of audio, @@ -17009,7 +17089,7 @@

All labels

function stageTrackerSetSoundOn(on) { _stageTrackerSoundOn = !!on; try { localStorage.setItem(STAGE_TRACKER_SOUND_KEY, _stageTrackerSoundOn ? '1' : '0'); } catch {} - stageTrackerSoundSyncButton(); + stageTrackerSoundHeldClear(); stageTrackerRenderSettings(); if (_stageTrackerSoundOn) stageTrackerPlayTest(); } @@ -17073,7 +17153,12 @@

All labels

_stageTrackerVolume = v === null ? STAGE_TRACKER_VOLUME_DEFAULT : Math.max(0, Math.min(Number(v) || 0, STAGE_TRACKER_VOLUME_MAX)); } catch { _stageTrackerVolume = STAGE_TRACKER_VOLUME_DEFAULT; } stageTrackerSoundSyncButton(); - if (_stageTrackerSoundOn) stageTrackerSoundPrime(); + // Sound left on from a previous visit on a document nobody has clicked + // yet (every reload, including the automatic one on a new hive + // version): the browser will hold the audio engine back, so say so + // now rather than after the first zap has been dropped. + if (_stageTrackerSoundOn && !stageTrackerHasUserActivation(window.navigator)) stageTrackerSoundHeldNotice(false); + else if (_stageTrackerSoundOn) stageTrackerSoundPrime(); stageTrackerClipsLoad().then(() => { stageTrackerDecodeClips(); stageTrackerRenderSettings(); }); }