refactor(np): add triangle volume slider to now playing panel

This commit is contained in:
2025-10-04 15:01:19 -04:00
parent 480aa5859b
commit 7c64818db1
2 changed files with 283 additions and 78 deletions

View File

@@ -2,6 +2,7 @@
import { playback } from '$lib/stores/playback';
import { audioPlayer } from '$lib/services/audioPlayer';
import LyricsDisplay from '$lib/components/LyricsDisplay.svelte';
import TriangleVolumeSlider from '$lib/components/TriangleVolumeSlider.svelte';
function handlePlayPause() {
playback.togglePlayPause();
@@ -22,9 +23,7 @@
playback.setCurrentTime(time);
}
function handleVolumeChange(e: Event) {
const target = e.target as HTMLInputElement;
const volume = parseFloat(target.value);
function handleVolumeChange(volume: number) {
playback.setVolume(volume);
audioPlayer.setVolume(volume);
}
@@ -83,7 +82,6 @@
{:else}
<div class="track-title">No track playing</div>
{/if}
</div>
<div class="progress-section">
<span class="time-display">{formatTime($playback.currentTime)}</span>
@@ -104,21 +102,10 @@
</div>
<span class="time-display">{formatTime($playback.duration)}</span>
</div>
</div>
<div class="volume-section">
<img src="/icons/volume.svg" alt="Volume" class="volume-icon" />
<div class="is-vertical volume-slider-container">
<input
type="range"
min="0"
max="1"
step="0.01"
value={$playback.volume}
oninput={handleVolumeChange}
class="has-box-indicator"
/>
</div>
<span class="volume-percent">{Math.round($playback.volume * 100)}%</span>
<TriangleVolumeSlider bind:value={$playback.volume} onchange={handleVolumeChange} />
</div>
<LyricsDisplay lrcPath={$playback.lrcPath} currentTime={$playback.currentTime} />
@@ -142,38 +129,14 @@
}
.control-button {
background: transparent;
border: none;
box-shadow: none;
padding: 4px;
min-width: auto;
min-height: auto;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
.control-button:hover:not(:disabled) {
background: light-dark(silver, #2b2b2b);
box-shadow: inset -1px -1px light-dark(#0a0a0a, #000),
inset 1px 1px light-dark(#fff, #525252),
inset -2px -2px light-dark(grey, #232323),
inset 2px 2px light-dark(#dfdfdf, #363636);
}
.control-button:active:not(:disabled) {
box-shadow: inset -1px -1px light-dark(#fff, #525252),
inset 1px 1px light-dark(#0a0a0a, #000),
inset -2px -2px light-dark(#dfdfdf, #363636),
inset 2px 2px light-dark(grey, #232323);
}
.control-button:disabled {
opacity: 0.4;
cursor: not-allowed;
}
.control-button img {
width: 24px;
height: 24px;
@@ -184,10 +147,18 @@
padding: 6px;
}
.play-pause img {
width: 28px;
height: 28px;
}
.track-info {
flex: 1;
min-width: 0;
overflow: hidden;
display: flex;
flex-direction: column;
gap: 4px;
}
.track-title {
@@ -209,8 +180,7 @@
display: flex;
align-items: center;
gap: 8px;
flex: 2;
min-width: 200px;
min-width: 0;
}
.time-display {
@@ -248,28 +218,9 @@
}
.volume-section {
display: flex;
align-items: center;
gap: 6px;
flex-shrink: 0;
}
.volume-icon {
width: 20px;
height: 20px;
filter: light-dark(none, invert(1));
}
.volume-slider-container {
width: 80px;
}
.volume-percent {
font-family: monospace;
font-size: 10px;
min-width: 35px;
}
/* Responsive: hide lyrics on medium widths */
@media (max-width: 1000px) {
.now-playing :global(.lyrics-display) {

View File

@@ -0,0 +1,254 @@
<script lang="ts">
interface Props {
value: number; // 0-1
onchange?: (value: number) => void;
}
let { value = $bindable(0.75), onchange }: Props = $props();
let volume = $state(value * 100); // Internal state as 0-100
let isDragging = $state(false);
let dragOffset = $state(0);
let triangleContainer: HTMLDivElement;
let volumeThumb: HTMLDivElement;
const triangleWidth = 60;
// Sync external value (0-1) with internal volume (0-100)
$effect(() => {
volume = value * 100;
});
function updateVolume(percentage: number) {
volume = Math.max(0, Math.min(100, percentage));
const newValue = volume / 100;
value = newValue;
if (onchange) {
onchange(newValue);
}
}
function getVolumeFromPosition(clientX: number, useOffset: boolean = false): number {
const rect = triangleContainer.getBoundingClientRect();
let x = clientX - rect.left;
if (useOffset) {
x = x - dragOffset;
}
const percentage = (x / rect.width) * 100;
return percentage;
}
function handleThumbMouseDown(e: MouseEvent) {
isDragging = true;
const rect = triangleContainer.getBoundingClientRect();
const clickX = e.clientX - rect.left;
const currentThumbPosition = (volume / 100) * rect.width;
dragOffset = clickX - currentThumbPosition;
e.stopPropagation();
e.preventDefault();
}
function handleContainerMouseDown(e: MouseEvent) {
if (e.target !== volumeThumb) {
dragOffset = 0;
const newVolume = getVolumeFromPosition(e.clientX);
updateVolume(newVolume);
isDragging = true;
}
}
function handleMouseMove(e: MouseEvent) {
if (isDragging) {
const newVolume = getVolumeFromPosition(e.clientX, true);
updateVolume(newVolume);
}
}
function handleMouseUp() {
isDragging = false;
dragOffset = 0;
}
function handleThumbTouchStart(e: TouchEvent) {
isDragging = true;
const touch = e.touches[0];
const rect = triangleContainer.getBoundingClientRect();
const currentThumbPosition = (volume / 100) * rect.width;
dragOffset = (touch.clientX - rect.left) - currentThumbPosition;
e.preventDefault();
}
function handleContainerTouchStart(e: TouchEvent) {
if (e.target !== volumeThumb) {
const touch = e.touches[0];
dragOffset = 0;
const newVolume = getVolumeFromPosition(touch.clientX, false);
updateVolume(newVolume);
isDragging = true;
}
e.preventDefault();
}
function handleTouchMove(e: TouchEvent) {
if (isDragging) {
const touch = e.touches[0];
const newVolume = getVolumeFromPosition(touch.clientX, true);
updateVolume(newVolume);
}
}
function handleTouchEnd() {
isDragging = false;
}
function handleDecrease() {
updateVolume(volume - 5);
}
function handleIncrease() {
updateVolume(volume + 5);
}
const position = $derived((volume / 100) * triangleWidth);
const rightEdge = $derived(volume);
</script>
<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} ontouchmove={handleTouchMove} ontouchend={handleTouchEnd} />
<div class="volume-container">
<button class="volume-icons" onclick={handleDecrease} aria-label="Decrease volume"></button>
<div class="triangle-wrapper">
<div
class="triangle-container"
bind:this={triangleContainer}
onmousedown={handleContainerMouseDown}
ontouchstart={handleContainerTouchStart}
role="slider"
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow={Math.round(value * 100)}
tabindex="0"
>
<div class="triangle-bg"></div>
<div
class="triangle-fill"
style="clip-path: inset(0 {100 - rightEdge}% 0 0);"
></div>
<div
class="volume-thumb"
style="left: {position}px;"
bind:this={volumeThumb}
onmousedown={handleThumbMouseDown}
ontouchstart={handleThumbTouchStart}
>
<div class="volume-thumb-inner"></div>
</div>
</div>
</div>
<button class="volume-icons" onclick={handleIncrease} aria-label="Increase volume">+</button>
</div>
<style>
.volume-container {
display: flex;
align-items: center;
gap: 8px;
}
.volume-icons {
font-size: 12px;
color: light-dark(#000, #fff);
user-select: none;
padding: 2px 6px;
min-width: auto;
min-height: auto;
font-weight: bold;
}
.triangle-wrapper {
position: relative;
width: 60px;
height: 16px;
flex: 1;
max-width: 60px;
}
.triangle-container {
position: relative;
width: 60px;
height: 100%;
cursor: pointer;
}
.triangle-bg {
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 0;
border-left: 60px solid transparent;
border-right: 0 solid transparent;
border-bottom: 16px solid light-dark(#808080, #525252);
}
.triangle-fill {
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 0;
border-left: 60px solid transparent;
border-right: 0 solid transparent;
border-bottom: 16px solid light-dark(#000080, #0066cc);
transition: clip-path 0.05s ease-out;
}
.volume-thumb {
position: absolute;
bottom: -2px;
width: 8px;
height: 16px;
background: light-dark(#c0c0c0, #2b2b2b);
border-top: 1px solid light-dark(#ffffff, #525252);
border-left: 1px solid light-dark(#ffffff, #525252);
border-right: 1px solid light-dark(#000000, #000);
border-bottom: 1px solid light-dark(#000000, #000);
box-shadow: inset 1px 1px 0 light-dark(#dfdfdf, #363636),
inset -1px -1px 0 light-dark(#808080, #232323);
cursor: grab;
transform: translateX(-4px);
z-index: 10;
}
.volume-thumb:active {
cursor: grabbing;
border-top: 1px solid light-dark(#000000, #000);
border-left: 1px solid light-dark(#000000, #000);
border-right: 1px solid light-dark(#ffffff, #525252);
border-bottom: 1px solid light-dark(#ffffff, #525252);
}
.volume-thumb-inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 4px;
height: 10px;
background: linear-gradient(
to right,
light-dark(#dfdfdf, #363636) 0%,
light-dark(#c0c0c0, #2b2b2b) 50%,
light-dark(#808080, #232323) 100%
);
}
</style>