Repository navigation
Replace text toggle with an icon button + settings pop-up #15
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
Changes from all commits
59a2761
a6a66fd
d1e3c57
2f29ec8
98c4f8e
0956351
15ae540
d66277a
16529fa
72f69fb
e8d1aec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -140,126 +140,255 @@ | |||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Inject toggle button into player controls | ||||||||||||||||||||
| function _metInjectButton() { | ||||||||||||||||||||
| // v3: mount the metronome controls into the host's stable plugin-control | ||||||||||||||||||||
| // slot instead of anchoring to #btn-lyrics inside #player-controls (that | ||||||||||||||||||||
| // transport bar auto-hides in v3, making it an unreliable anchor). | ||||||||||||||||||||
| const hostUi = _metGetHostUi(); | ||||||||||||||||||||
| const isV3 = !!(hostUi && hostUi.uiVersion === 'v3'); | ||||||||||||||||||||
| let slot = null; | ||||||||||||||||||||
| if (isV3 && hostUi.ui && typeof hostUi.ui.playerControlSlot === 'function') { | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| const _s = hostUi.ui.playerControlSlot(); | ||||||||||||||||||||
| if (_s && typeof _s.appendChild === 'function' && typeof _s.insertBefore === 'function') slot = _s; | ||||||||||||||||||||
| } catch (_e) { /* host slot API failure -> fall back to legacy container */ } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| const controls = slot || document.getElementById('player-controls'); | ||||||||||||||||||||
| if (!controls) return; | ||||||||||||||||||||
| const existingBtn = document.getElementById('btn-metronome'); | ||||||||||||||||||||
| if (existingBtn) { | ||||||||||||||||||||
| // Gate reuse on `aria-haspopup` rather than just the id existing: a | ||||||||||||||||||||
| // pre-consolidation page (the old classic-text-toggle-plus-icon pair) | ||||||||||||||||||||
| // also used id="btn-metronome" for its text-only toggle button, which | ||||||||||||||||||||
| // has none of the new single button's markup. Reusing that element | ||||||||||||||||||||
| // as-is would rebind the popover-opening handler onto a stale text | ||||||||||||||||||||
| // button that still reads "Metronome" instead of showing the icon. | ||||||||||||||||||||
| const existingPopover = document.getElementById('met-popover'); | ||||||||||||||||||||
| const isConsolidatedBtn = !!(existingBtn && existingPopover && | ||||||||||||||||||||
| typeof existingBtn.getAttribute === 'function' && | ||||||||||||||||||||
| existingBtn.getAttribute('aria-haspopup') === 'true'); | ||||||||||||||||||||
| if (isConsolidatedBtn) { | ||||||||||||||||||||
| const existingEnabledCheck = document.getElementById('met-enabled-check'); | ||||||||||||||||||||
| const existingSlider = document.getElementById('met-volume'); | ||||||||||||||||||||
| const existingFlashCheck = document.getElementById('met-flash-check'); | ||||||||||||||||||||
| const existingSubdivSel = document.getElementById('met-subdiv'); | ||||||||||||||||||||
| const existingCountInCheck = document.getElementById('met-count-in-check'); | ||||||||||||||||||||
| existingBtn.onclick = _metToggle; | ||||||||||||||||||||
| existingBtn.onclick = _metTogglePopover; | ||||||||||||||||||||
| if (existingEnabledCheck) _metBindEnabledCheck(existingEnabledCheck); | ||||||||||||||||||||
| if (existingSlider) _metBindVolumeSlider(existingSlider); | ||||||||||||||||||||
| if (existingFlashCheck) _metBindFlashCheck(existingFlashCheck); | ||||||||||||||||||||
| if (existingSubdivSel) _metBindSubdivSelect(existingSubdivSel); | ||||||||||||||||||||
| if (existingCountInCheck) _metBindCountInCheck(existingCountInCheck); | ||||||||||||||||||||
| _metInstallPopoverDismissHook(); | ||||||||||||||||||||
| _metSyncUi(); | ||||||||||||||||||||
| return; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Stale DOM: a pre-consolidation classic toggle (+ its separate settings | ||||||||||||||||||||
| // icon), or an interrupted prior injection. Drop whatever's left rather | ||||||||||||||||||||
| // than appending a duplicate alongside it below, which would leave | ||||||||||||||||||||
| // _metSyncUi() updating the first DOM match while a second, orphaned | ||||||||||||||||||||
| // control goes stale. | ||||||||||||||||||||
| const staleWrap = document.getElementById('met-wrap'); | ||||||||||||||||||||
| if (staleWrap) { | ||||||||||||||||||||
| staleWrap.remove(); | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| if (existingBtn) existingBtn.remove(); | ||||||||||||||||||||
| const staleSettingsBtn = document.getElementById('btn-metronome-settings'); | ||||||||||||||||||||
| if (staleSettingsBtn) staleSettingsBtn.remove(); | ||||||||||||||||||||
| // The released 1.0.1 build appended the volume/flash/subdivision/ | ||||||||||||||||||||
| // count-in controls directly to #player-controls (no #met-wrap), | ||||||||||||||||||||
| // and _metSyncUi no longer manages those ids — drop whichever | ||||||||||||||||||||
| // survived a live upgrade so they don't strand dead chrome in the | ||||||||||||||||||||
| // controls bar next to the fresh popover-backed control set. | ||||||||||||||||||||
| ['met-volume', 'met-vol-label', 'met-flash-label', 'met-subdiv', 'met-count-in-label'] | ||||||||||||||||||||
| .forEach((id) => { | ||||||||||||||||||||
| const legacy = document.getElementById(id); | ||||||||||||||||||||
| if (legacy) legacy.remove(); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const lyricsBtn = document.getElementById('btn-lyrics'); | ||||||||||||||||||||
| // In the v3 slot we always append in order rather than anchoring to a | ||||||||||||||||||||
| // node that may not even be a child of the slot. | ||||||||||||||||||||
| const insertBefore = isV3 ? null : (lyricsBtn?.nextSibling || controls.querySelector('button:last-child')); | ||||||||||||||||||||
| const insert = (el) => { | ||||||||||||||||||||
| if (insertBefore && insertBefore.parentNode === controls) controls.insertBefore(el, insertBefore); | ||||||||||||||||||||
| else controls.appendChild(el); | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Single consolidated button (design feedback on issue #12: a separate | ||||||||||||||||||||
| // text toggle next to the icon was repetitive). Clicking it opens the | ||||||||||||||||||||
| // settings pop-up, whose "Enabled" checkbox is now the only on/off | ||||||||||||||||||||
| // control — the button itself still reflects enabled state visually | ||||||||||||||||||||
| // via the met-btn--active class (see _metSyncUi). | ||||||||||||||||||||
| const wrap = document.createElement('div'); | ||||||||||||||||||||
| wrap.id = 'met-wrap'; | ||||||||||||||||||||
| wrap.className = 'met-wrap'; | ||||||||||||||||||||
| insert(wrap); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const btn = document.createElement('button'); | ||||||||||||||||||||
| btn.id = 'btn-metronome'; | ||||||||||||||||||||
| btn.className = 'met-btn'; | ||||||||||||||||||||
| btn.textContent = 'Metronome'; | ||||||||||||||||||||
| btn.title = 'Toggle metronome click'; | ||||||||||||||||||||
| btn.onclick = _metToggle; | ||||||||||||||||||||
| insert(btn); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| btn.innerHTML = _MET_ICON_SVG; | ||||||||||||||||||||
|
Check warning on line 232 in screen.js
|
||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 HIGH RISK Avoid assigning raw strings to innerHTML. Even with trusted constants, this pattern bypasses security mitigations and is flagged as a high-risk vulnerability. Use DOMParser to safely convert the SVG string into a DOM node: See Issue in Codacy |
||||||||||||||||||||
| btn.title = 'Metronome settings'; | ||||||||||||||||||||
| btn.setAttribute('aria-label', 'Metronome settings'); | ||||||||||||||||||||
|
cchacs marked this conversation as resolved.
Resolved
|
||||||||||||||||||||
| btn.setAttribute('aria-haspopup', 'true'); | ||||||||||||||||||||
| btn.setAttribute('aria-expanded', 'false'); | ||||||||||||||||||||
| btn.setAttribute('aria-controls', 'met-popover'); | ||||||||||||||||||||
| btn.onclick = _metTogglePopover; | ||||||||||||||||||||
| wrap.appendChild(btn); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const popover = document.createElement('div'); | ||||||||||||||||||||
| popover.id = 'met-popover'; | ||||||||||||||||||||
| popover.className = 'met-popover met-hidden'; | ||||||||||||||||||||
| // Listen on `wrap`, not `popover`: opening the popover doesn't move | ||||||||||||||||||||
| // focus into it, so an Escape pressed immediately after the click that | ||||||||||||||||||||
| // opened it still targets the settings button — a listener scoped to | ||||||||||||||||||||
| // the popover alone would never see that keydown. `wrap` is an | ||||||||||||||||||||
| // ancestor of both the button and the popover, so it catches Escape | ||||||||||||||||||||
| // from either. | ||||||||||||||||||||
| wrap.addEventListener('keydown', function(e) { | ||||||||||||||||||||
| if (e.key === 'Escape' || e.key === 'Esc') { _metClosePopover(); btn.focus(); } | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
Comment on lines
+250
to
+252
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ LOW RISK Suggestion: The Escape key logic should be conditional on the popover being open to prevent unintentional focus shifts.
Suggested change
|
||||||||||||||||||||
| wrap.appendChild(popover); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const enabledLabel = document.createElement('label'); | ||||||||||||||||||||
| enabledLabel.id = 'met-enabled-label'; | ||||||||||||||||||||
| enabledLabel.className = 'met-toggle'; | ||||||||||||||||||||
| const enabledCheck = document.createElement('input'); | ||||||||||||||||||||
| enabledCheck.type = 'checkbox'; | ||||||||||||||||||||
| enabledCheck.id = 'met-enabled-check'; | ||||||||||||||||||||
| enabledCheck.className = 'met-checkbox'; | ||||||||||||||||||||
| enabledLabel.appendChild(enabledCheck); | ||||||||||||||||||||
| enabledLabel.appendChild(document.createTextNode(' Enabled')); | ||||||||||||||||||||
| popover.appendChild(enabledLabel); | ||||||||||||||||||||
| _metBindEnabledCheck(enabledCheck); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const volRow = document.createElement('div'); | ||||||||||||||||||||
| volRow.className = 'met-row'; | ||||||||||||||||||||
| const slider = document.createElement('input'); | ||||||||||||||||||||
| slider.type = 'range'; | ||||||||||||||||||||
| slider.id = 'met-volume'; | ||||||||||||||||||||
| slider.min = '0'; | ||||||||||||||||||||
| slider.max = '100'; | ||||||||||||||||||||
| slider.className = 'met-range met-hidden'; | ||||||||||||||||||||
| slider.className = 'met-range'; | ||||||||||||||||||||
| _metBindVolumeSlider(slider); | ||||||||||||||||||||
| insert(slider); | ||||||||||||||||||||
| volRow.appendChild(slider); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const label = document.createElement('span'); | ||||||||||||||||||||
| label.id = 'met-vol-label'; | ||||||||||||||||||||
| label.className = 'met-label met-hidden'; | ||||||||||||||||||||
| label.className = 'met-label'; | ||||||||||||||||||||
| label.textContent = `${Math.round(_metSettings.volume * 100)}%`; | ||||||||||||||||||||
| insert(label); | ||||||||||||||||||||
| volRow.appendChild(label); | ||||||||||||||||||||
| popover.appendChild(volRow); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const flashLabel = document.createElement('label'); | ||||||||||||||||||||
| flashLabel.id = 'met-flash-label'; | ||||||||||||||||||||
| flashLabel.className = 'met-toggle met-hidden'; | ||||||||||||||||||||
| flashLabel.className = 'met-toggle'; | ||||||||||||||||||||
| const flashCheck = document.createElement('input'); | ||||||||||||||||||||
| flashCheck.type = 'checkbox'; | ||||||||||||||||||||
| flashCheck.id = 'met-flash-check'; | ||||||||||||||||||||
| flashCheck.className = 'met-checkbox'; | ||||||||||||||||||||
| flashLabel.appendChild(flashCheck); | ||||||||||||||||||||
| flashLabel.appendChild(document.createTextNode(' Flash')); | ||||||||||||||||||||
| insert(flashLabel); | ||||||||||||||||||||
| popover.appendChild(flashLabel); | ||||||||||||||||||||
| _metBindFlashCheck(flashCheck); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const subdivSel = document.createElement('select'); | ||||||||||||||||||||
| subdivSel.id = 'met-subdiv'; | ||||||||||||||||||||
| subdivSel.className = 'met-select met-hidden'; | ||||||||||||||||||||
| subdivSel.className = 'met-select'; | ||||||||||||||||||||
| subdivSel.title = 'Subdivision clicks'; | ||||||||||||||||||||
| [['none', 'Beats only'], ['eighth', '8th notes'], ['triplet', 'Triplets']].forEach(([val, text]) => { | ||||||||||||||||||||
| const opt = document.createElement('option'); | ||||||||||||||||||||
| opt.value = val; | ||||||||||||||||||||
| opt.textContent = text; | ||||||||||||||||||||
| subdivSel.appendChild(opt); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| insert(subdivSel); | ||||||||||||||||||||
| popover.appendChild(subdivSel); | ||||||||||||||||||||
| _metBindSubdivSelect(subdivSel); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const countInLabel = document.createElement('label'); | ||||||||||||||||||||
| countInLabel.id = 'met-count-in-label'; | ||||||||||||||||||||
| countInLabel.className = 'met-toggle met-hidden'; | ||||||||||||||||||||
| countInLabel.className = 'met-toggle'; | ||||||||||||||||||||
| countInLabel.title = 'Show 4-3-2-1 countdown before the first beat'; | ||||||||||||||||||||
| const countInCheck = document.createElement('input'); | ||||||||||||||||||||
| countInCheck.type = 'checkbox'; | ||||||||||||||||||||
| countInCheck.id = 'met-count-in-check'; | ||||||||||||||||||||
| countInCheck.className = 'met-checkbox'; | ||||||||||||||||||||
| countInLabel.appendChild(countInCheck); | ||||||||||||||||||||
| countInLabel.appendChild(document.createTextNode(' Count-in')); | ||||||||||||||||||||
| insert(countInLabel); | ||||||||||||||||||||
| popover.appendChild(countInLabel); | ||||||||||||||||||||
| _metBindCountInCheck(countInCheck); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| _metInstallPopoverDismissHook(); | ||||||||||||||||||||
| _metSyncUi(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Classic metronome triangle-with-pendulum glyph (issue #12: "should have | ||||||||||||||||||||
| // the classic metronome shape"). | ||||||||||||||||||||
| const _MET_ICON_SVG = | ||||||||||||||||||||
| '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" ' + | ||||||||||||||||||||
| 'xmlns="http://www.w3.org/2000/svg" aria-hidden="true">' + | ||||||||||||||||||||
| '<path d="M8 21h8l-2.5-15h-3L8 21z" stroke="currentColor" stroke-width="1.6" ' + | ||||||||||||||||||||
| 'stroke-linejoin="round"/>' + | ||||||||||||||||||||
| '<line x1="12" y1="19" x2="15.5" y2="7" stroke="currentColor" stroke-width="1.6" ' + | ||||||||||||||||||||
| 'stroke-linecap="round"/>' + | ||||||||||||||||||||
| '<circle cx="14.4" cy="11" r="1.3" fill="currentColor"/>' + | ||||||||||||||||||||
| '</svg>'; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| function _metBindEnabledCheck(check) { | ||||||||||||||||||||
| if (check._metEnabledListener) check.removeEventListener('change', check._metEnabledListener); | ||||||||||||||||||||
| check.checked = !!_metSettings.enabled; | ||||||||||||||||||||
| check._metEnabledListener = function() { _metToggle(); }; | ||||||||||||||||||||
| check.addEventListener('change', check._metEnabledListener); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| function _metSetPopoverOpen(open) { | ||||||||||||||||||||
| const popover = document.getElementById('met-popover'); | ||||||||||||||||||||
| const btn = document.getElementById('btn-metronome'); | ||||||||||||||||||||
| if (!popover) return; | ||||||||||||||||||||
| popover.classList.toggle('met-hidden', !open); | ||||||||||||||||||||
| if (btn) btn.setAttribute('aria-expanded', open ? 'true' : 'false'); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| function _metTogglePopover(e) { | ||||||||||||||||||||
| if (e && typeof e.stopPropagation === 'function') e.stopPropagation(); | ||||||||||||||||||||
| const popover = document.getElementById('met-popover'); | ||||||||||||||||||||
| if (!popover) return; | ||||||||||||||||||||
| _metSetPopoverOpen(popover.classList.contains('met-hidden')); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| function _metClosePopover() { | ||||||||||||||||||||
| _metSetPopoverOpen(false); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Closes the settings pop-up on an outside click. Installed once per page | ||||||||||||||||||||
| // (guarded like the other document-level hooks in this file). | ||||||||||||||||||||
| const MET_POPOVER_DISMISS_INSTALLED_KEY = '__slopsmithMetronomePopoverDismissInstalled'; | ||||||||||||||||||||
| function _metInstallPopoverDismissHook() { | ||||||||||||||||||||
| if (typeof document === 'undefined' || typeof document.addEventListener !== 'function') return; | ||||||||||||||||||||
| if (window[MET_POPOVER_DISMISS_INSTALLED_KEY]) return; | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Use direct property access on the window object instead of square-bracket notation with a variable to avoid potential object injection risks: |
||||||||||||||||||||
| document.addEventListener('click', function(e) { | ||||||||||||||||||||
| const wrap = document.getElementById('met-wrap'); | ||||||||||||||||||||
| const popover = document.getElementById('met-popover'); | ||||||||||||||||||||
| if (!wrap || !popover) return; | ||||||||||||||||||||
| if (typeof wrap.contains === 'function' && wrap.contains(e.target)) return; | ||||||||||||||||||||
| _metClosePopover(); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| window[MET_POPOVER_DISMISS_INSTALLED_KEY] = true; | ||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Avoid assigning to dynamic properties on the global object. Use a literal name to prevent potential security warnings: |
||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| function _metSyncUi() { | ||||||||||||||||||||
| const enabled = _metSettings.enabled; | ||||||||||||||||||||
| const btn = document.getElementById('btn-metronome'); | ||||||||||||||||||||
| const slider = document.getElementById('met-volume'); | ||||||||||||||||||||
| const label = document.getElementById('met-vol-label'); | ||||||||||||||||||||
| const flashLabel = document.getElementById('met-flash-label'); | ||||||||||||||||||||
| const subdivSel = document.getElementById('met-subdiv'); | ||||||||||||||||||||
| const countInLabel = document.getElementById('met-count-in-label'); | ||||||||||||||||||||
| const enabledCheck = document.getElementById('met-enabled-check'); | ||||||||||||||||||||
| if (btn) { | ||||||||||||||||||||
| btn.className = enabled ? 'met-btn met-btn--active' : 'met-btn'; | ||||||||||||||||||||
| btn.textContent = enabled ? 'Metronome ✓' : 'Metronome'; | ||||||||||||||||||||
| const stateLabel = enabled ? 'Metronome settings (on)' : 'Metronome settings (off)'; | ||||||||||||||||||||
| btn.title = stateLabel; | ||||||||||||||||||||
| btn.setAttribute('aria-label', stateLabel); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (slider) slider.classList.toggle('met-hidden', !enabled); | ||||||||||||||||||||
| if (label) label.classList.toggle('met-hidden', !enabled); | ||||||||||||||||||||
| if (flashLabel) flashLabel.classList.toggle('met-hidden', !enabled); | ||||||||||||||||||||
| if (subdivSel) subdivSel.classList.toggle('met-hidden', !enabled); | ||||||||||||||||||||
| if (countInLabel) countInLabel.classList.toggle('met-hidden', !enabled); | ||||||||||||||||||||
| if (enabledCheck) enabledCheck.checked = enabled; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| function _metToggle() { | ||||||||||||||||||||
|
|
@@ -329,101 +458,101 @@ | |||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Main tick — called from a polling loop | ||||||||||||||||||||
| function _metTick() { | ||||||||||||||||||||
| const currentHighway = _metGetHighway(); | ||||||||||||||||||||
| if ( | ||||||||||||||||||||
| !currentHighway || | ||||||||||||||||||||
| typeof currentHighway.getBeats !== 'function' || | ||||||||||||||||||||
| typeof currentHighway.getTime !== 'function' | ||||||||||||||||||||
| ) { | ||||||||||||||||||||
| return; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (!_metSettings.enabled) { | ||||||||||||||||||||
| _metState.flashAlpha = 0; | ||||||||||||||||||||
| return; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| const beats = currentHighway.getBeats(); | ||||||||||||||||||||
| const t = currentHighway.getTime(); | ||||||||||||||||||||
| if (!beats || beats.length === 0) return; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Find the current beat (the most recent beat <= current time) | ||||||||||||||||||||
| let lo = 0, hi = beats.length; | ||||||||||||||||||||
| while (lo < hi) { | ||||||||||||||||||||
| const mid = (lo + hi) >> 1; | ||||||||||||||||||||
| if (beats[mid].time <= t) lo = mid + 1; | ||||||||||||||||||||
| else hi = mid; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| const idx = lo - 1; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Reset subdivision state when we enter a new beat interval | ||||||||||||||||||||
| if (idx !== _metState.lastBeatIdx) { | ||||||||||||||||||||
| _metState.lastSubdivInBeat = -1; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if (idx < 0 || idx === _metState.lastBeatIdx) { | ||||||||||||||||||||
| // Fade out flash | ||||||||||||||||||||
| _metState.flashAlpha *= 0.85; | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| // Only trigger if we're close to the beat (within 50ms) to avoid | ||||||||||||||||||||
| // catching up on seeks | ||||||||||||||||||||
| const beatTime = beats[idx].time; | ||||||||||||||||||||
| if (Math.abs(t - beatTime) > 0.05) { | ||||||||||||||||||||
| _metState.lastBeatIdx = idx; | ||||||||||||||||||||
| _metState.flashAlpha *= 0.85; | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| _metState.lastBeatIdx = idx; | ||||||||||||||||||||
| const isMeasure = beats[idx].measure >= 0; | ||||||||||||||||||||
| _metClick(isMeasure ? 'high' : 'mid'); | ||||||||||||||||||||
| _metFlash(isMeasure ? 0.35 : 0.15); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Check subdivisions within the current beat interval. | ||||||||||||||||||||
| // Subdivision times are interpolated between this beat and the next, so | ||||||||||||||||||||
| // they naturally follow any playback-speed change applied by the host. | ||||||||||||||||||||
| const subdivMode = _metSettings.subdivision || 'none'; | ||||||||||||||||||||
| if (idx >= 0 && subdivMode !== 'none' && idx + 1 < beats.length) { | ||||||||||||||||||||
| const beatStart = beats[idx].time; | ||||||||||||||||||||
| const dt = beats[idx + 1].time - beatStart; | ||||||||||||||||||||
| const subdivTimes = subdivMode === 'eighth' | ||||||||||||||||||||
| ? [beatStart + dt * 0.5] | ||||||||||||||||||||
| : [beatStart + dt / 3, beatStart + dt * 2 / 3]; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for (let s = 0; s < subdivTimes.length; s++) { | ||||||||||||||||||||
| if (s <= _metState.lastSubdivInBeat) continue; | ||||||||||||||||||||
| if (t < subdivTimes[s]) break; // ordered ascending; nothing further is due yet | ||||||||||||||||||||
| _metState.lastSubdivInBeat = s; | ||||||||||||||||||||
| if (Math.abs(t - subdivTimes[s]) <= 0.05) { | ||||||||||||||||||||
| _metClick('low'); | ||||||||||||||||||||
| // Dim flash for subdivision — only raise alpha, never lower a beat flash in progress | ||||||||||||||||||||
| if (_metSettings.flashEnabled && _metState.flashAlpha < 0.07) { | ||||||||||||||||||||
| _metState.flashAlpha = 0.07; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Count-in overlay: show 4-3-2-1 before the first beat | ||||||||||||||||||||
| if (_metSettings.countInEnabled && beats && beats.length >= 2) { | ||||||||||||||||||||
| const firstBeatTime = beats[0].time; | ||||||||||||||||||||
| const beatInterval = beats[1].time - beats[0].time; | ||||||||||||||||||||
| const remaining = firstBeatTime - t; | ||||||||||||||||||||
| if (remaining > 0.01 && beatInterval > 0.01) { | ||||||||||||||||||||
| const count = Math.ceil((remaining + 0.001) / beatInterval); | ||||||||||||||||||||
| if (count >= 1 && count <= 4) { | ||||||||||||||||||||
| // alpha: 1.0 at the start of each count beat, fades to ~0.3 before the next | ||||||||||||||||||||
| const beatPhase = ((remaining + 0.001) % beatInterval) / beatInterval; | ||||||||||||||||||||
| _metUpdateCountIn(count, 0.3 + 0.7 * (1 - beatPhase)); | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| _metClearCountIn(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| _metClearCountIn(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } else if (_metCountInEl) { | ||||||||||||||||||||
| _metClearCountIn(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const TICK_INTERVAL_ID_KEY = 'slopsmithMetronomeTickIntervalId'; | ||||||||||||||||||||
| const VISIBILITY_CHANGE_HOOKS_INSTALLED_KEY = '__slopsmithMetronomeVisibilityChangeHooksInstalled'; | ||||||||||||||||||||
|
|
@@ -491,6 +620,8 @@ | |||||||||||||||||||
| module.exports = { | ||||||||||||||||||||
| _metSettings, _metState, _metClick, _metFlash, _metBindVolumeSlider, | ||||||||||||||||||||
| _metBindFlashCheck, _metBindSubdivSelect, _metBindCountInCheck, | ||||||||||||||||||||
| _metBindEnabledCheck, _metTogglePopover, _metClosePopover, _metSetPopoverOpen, | ||||||||||||||||||||
| _metInstallPopoverDismissHook, | ||||||||||||||||||||
| _metInjectButton, _metSyncUi, _metToggle, _metSetVolume, _metSaveSettings, | ||||||||||||||||||||
| _metGetHighway, _metEnsureDrawHookInstalled, _metTick, | ||||||||||||||||||||
| _metUpdateCountIn, _metClearCountIn, | ||||||||||||||||||||
|
|
||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.