fix seeking problems due to progress bar width inconsistency

This commit is contained in:
2026-03-16 23:21:00 +01:00
parent 7febb497e9
commit 1dc3f4505b

View File

@@ -102,7 +102,7 @@
}); });
function seek(event, bounds) { function seek(event, bounds) {
let x = event.pageX - bounds.left; let x = event.clientX - bounds.left;
return Math.min(Math.max(x / bounds.width, 0), 1); return Math.min(Math.max(x / bounds.width, 0), 1);
} }
@@ -144,11 +144,6 @@
} }
} }
function seekAudio(event) {
if (!songBar) return;
audio.currentTime = seek(event, songBar.getBoundingClientRect()) * duration;
}
function updateSeekVisual(event) { function updateSeekVisual(event) {
if (!songBar) return; if (!songBar) return;
pendingSeekTime = seek(event, songBar.getBoundingClientRect()) * duration; pendingSeekTime = seek(event, songBar.getBoundingClientRect()) * duration;
@@ -162,7 +157,7 @@
muted = false; muted = false;
} }
function formatSeconds(totalSeconds) { function formatSeconds(totalSeconds, forceHours = false) {
if (isNaN(totalSeconds)) return 'No Data'; if (isNaN(totalSeconds)) return 'No Data';
totalSeconds = parseInt(totalSeconds, 10); totalSeconds = parseInt(totalSeconds, 10);
var hours = Math.floor(totalSeconds / 3600); var hours = Math.floor(totalSeconds / 3600);
@@ -171,18 +166,22 @@
return [hours, minutes, seconds] return [hours, minutes, seconds]
.map((v) => (v < 10 ? '0' + v : v)) .map((v) => (v < 10 ? '0' + v : v))
.filter((v, i) => v !== '00' || i > 0) .filter((v, i) => v !== '00' || i > 0 || forceHours)
.join(':'); .join(':');
} }
function seekTooltip(event) { function seekTooltip(event) {
if (!inlineTooltip) { if (!inlineTooltip) {
let tooltipBounds = tooltip.getBoundingClientRect(); let tooltipBounds = tooltip.getBoundingClientRect();
tooltipX = Math.min(event.pageX + 10, innerWidth - tooltipBounds.width); tooltipX = Math.min(event.clientX + 10, innerWidth - tooltipBounds.width);
tooltipY = Math.min(songBar.offsetTop - 30, innerHeight - tooltipBounds.height); tooltipY = Math.min(songBar.offsetTop - 30, innerHeight - tooltipBounds.height);
} }
let bounds = songBar.getBoundingClientRect(); let bounds = songBar.getBoundingClientRect();
let seekValue = ((event.pageX - bounds.left) * duration) / bounds.width; let seekValue = ((event.clientX - bounds.left) * duration) / bounds.width;
updateTooltipText(seekValue);
}
function updateTooltipText(seekValue: number) {
if (!ctx.current) return; if (!ctx.current) return;
let trackArray = ctx.current.tracks[ctx.getSongAtTime(seekValue)]; let trackArray = ctx.current.tracks[ctx.getSongAtTime(seekValue)];
seekTrack = trackArray[1] + ' - ' + trackArray[2]; seekTrack = trackArray[1] + ' - ' + trackArray[2];
@@ -228,6 +227,8 @@
onmouseup={() => { onmouseup={() => {
if (seeking && pendingSeekTime != null) { if (seeking && pendingSeekTime != null) {
audio.currentTime = pendingSeekTime; audio.currentTime = pendingSeekTime;
currentTime = pendingSeekTime;
updateTooltipText(pendingSeekTime);
pendingSeekTime = null; pendingSeekTime = null;
} }
seeking = volumeSeeking = false; seeking = volumeSeeking = false;
@@ -252,14 +253,13 @@
bind:this={songBar} bind:this={songBar}
value={seeking && pendingSeekTime != null ? pendingSeekTime : (currentTime || 0)} value={seeking && pendingSeekTime != null ? pendingSeekTime : (currentTime || 0)}
max={duration} max={duration}
onmousedown={() => (seeking = true)} onmousedown={(e) => { seeking = true; updateSeekVisual(e); }}
onmouseenter={() => (showTooltip = true)} onmouseenter={() => (showTooltip = true)}
onmouseleave={() => (showTooltip = false)} onmouseleave={() => (showTooltip = false)}
onclick={seekAudio}
style="--primary-color:{barPrimaryColor}; --secondary-color:{barSecondaryColor}" style="--primary-color:{barPrimaryColor}; --secondary-color:{barSecondaryColor}"
class="song-progress" class="song-progress"
></progress> ></progress>
<div class="control-times">{formatSeconds(currentTime)}/{formatSeconds(duration)}</div> <div class="control-times">{formatSeconds(currentTime, duration >= 3600)}/{formatSeconds(duration)}</div>
<button <button
style="--icon-color:{iconColor}" style="--icon-color:{iconColor}"
class="material-icons" class="material-icons"
@@ -350,6 +350,9 @@
.control-times { .control-times {
margin: auto; margin: auto;
margin-right: 5px; margin-right: 5px;
font-variant-numeric: tabular-nums;
white-space: nowrap;
min-width: max-content;
} }
.tooltip { .tooltip {