-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMiniPlayer.tsx
More file actions
541 lines (519 loc) Β· 17.2 KB
/
Copy pathMiniPlayer.tsx
File metadata and controls
541 lines (519 loc) Β· 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
type PointerEvent as ReactPointerEvent,
} from "react";
import { useTranslation } from "react-i18next";
import {
Play,
Pause,
SkipBack,
SkipForward,
Heart,
Maximize2,
X,
Pin,
Repeat,
Repeat1,
Shuffle,
} from "lucide-react";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { Window as TauriWindow } from "@tauri-apps/api/window";
import { usePlayer } from "../../hooks/usePlayer";
import { Artwork } from "../common/Artwork";
import { resolveArtwork } from "../../lib/tauri/artwork";
import { dominantColor, darken, rgb } from "../../lib/dominantColor";
import { listLikedTrackIds, toggleLikeTrack } from "../../lib/tauri/track";
import { formatDuration } from "../../lib/tauri/track";
import { setMiniPlayerBounds } from "../../lib/tauri/preferences";
/**
* Spotify-style always-on-top widget. Square cover floats centered
* with a shadow; the window background takes a gradient sampled from
* the cover's dominant colour so the whole widget feels colour-aware.
*
* Hovering the cover reveals a translucent control bar (shuffle / prev
* / play / next / repeat) β the "minimal" idle state shows just the
* artwork. Title, artist and a like button live below, plus a top bar
* with always-on-top toggle, the macOS-style drag dots, and close.
*/
export function MiniPlayer() {
const { t } = useTranslation();
const {
currentTrack,
isPlaying,
togglePlayback,
next,
previous,
positionMs,
durationMs,
repeatMode,
cycleRepeatMode,
isShuffled,
toggleShuffle,
seek,
setSeeking,
activeProvider,
} = usePlayer();
const isSpotify = activeProvider === "spotify";
// ββ Like-state mirror (own webview = own React state) βββββββββββ
const [likedIds, setLikedIds] = useState<Set<number>>(new Set());
useEffect(() => {
listLikedTrackIds()
.then((ids) => setLikedIds(new Set(ids)))
.catch(() => {});
}, [currentTrack?.id]);
const isLiked = currentTrack ? likedIds.has(currentTrack.id) : false;
const handleLike = async () => {
if (!currentTrack) return;
try {
const nowLiked = await toggleLikeTrack(currentTrack.id);
setLikedIds((prev) => {
const n = new Set(prev);
if (nowLiked) n.add(currentTrack.id);
else n.delete(currentTrack.id);
return n;
});
} catch (err) {
console.error("[MiniPlayer] like failed", err);
}
};
// ββ Cover-derived background gradient βββββββββββββββββββββββββββ
const artworkUrl = useMemo(() => {
if (!currentTrack) return null;
return resolveArtwork(
{
full: currentTrack.artwork_path,
x1: currentTrack.artwork_path_1x,
x2: currentTrack.artwork_path_2x,
},
"full",
);
}, [currentTrack]);
const [bgColor, setBgColor] = useState<{ r: number; g: number; b: number }>({
r: 39,
g: 39,
b: 42,
});
useEffect(() => {
let cancelled = false;
if (!artworkUrl) {
/* eslint-disable react-hooks/set-state-in-effect */
setBgColor({ r: 39, g: 39, b: 42 });
/* eslint-enable react-hooks/set-state-in-effect */
return;
}
dominantColor(artworkUrl)
.then((c) => {
if (!cancelled) setBgColor(c);
})
.catch(() => {});
return () => {
cancelled = true;
};
}, [artworkUrl]);
const gradient = `linear-gradient(160deg, ${rgb(bgColor)} 0%, ${rgb(darken(bgColor, 0.45))} 70%, ${rgb(darken(bgColor, 0.2))} 100%)`;
// ββ Persist window bounds (position + size) βββββββββββββββββββββ
// Debounced because onMoved / onResized fire continuously while the
// user drags or resizes β without this we'd hammer SQLite at 60 Hz.
// 300 ms after the last gesture is short enough that closing the
// window with Alt-F4 still captures the final position.
useEffect(() => {
const win = getCurrentWindow();
let timer: number | null = null;
let unlistenMoved: (() => void) | null = null;
let unlistenResized: (() => void) | null = null;
const scheduleSave = () => {
if (timer != null) window.clearTimeout(timer);
timer = window.setTimeout(async () => {
try {
const scale = await win.scaleFactor();
const pos = await win.outerPosition();
const size = await win.outerSize();
await setMiniPlayerBounds({
x: pos.x / scale,
y: pos.y / scale,
width: size.width / scale,
height: size.height / scale,
});
} catch (err) {
console.error("[MiniPlayer] persist bounds failed", err);
}
}, 300);
};
win
.onMoved(scheduleSave)
.then((fn) => {
unlistenMoved = fn;
})
.catch((err) => console.error("[MiniPlayer] onMoved listen failed", err));
win
.onResized(scheduleSave)
.then((fn) => {
unlistenResized = fn;
})
.catch((err) =>
console.error("[MiniPlayer] onResized listen failed", err),
);
return () => {
if (timer != null) window.clearTimeout(timer);
unlistenMoved?.();
unlistenResized?.();
};
}, []);
// ββ Window controls (always-on-top toggle persisted; close β exit
// β we just close the mini window, the main app keeps running) β
const [pinned, setPinned] = useState(true);
const handleTogglePin = async () => {
try {
const win = getCurrentWindow();
const next = !pinned;
await win.setAlwaysOnTop(next);
setPinned(next);
} catch (err) {
console.error("[MiniPlayer] pin toggle failed", err);
}
};
const handleMaximize = async () => {
try {
const main = await TauriWindow.getByLabel("main");
if (main) {
await main.show();
await main.unminimize();
await main.setFocus();
}
await getCurrentWindow().close();
} catch (err) {
console.error("[MiniPlayer] maximize failed", err);
}
};
const handleClose = async () => {
try {
const main = await TauriWindow.getByLabel("main");
if (main) await main.show();
await getCurrentWindow().close();
} catch (err) {
console.error("[MiniPlayer] close failed", err);
}
};
const [showControls, setShowControls] = useState(false);
// ββ Interactive seek bar ββββββββββββββββββββββββββββββββββββββββ
const [dragMs, setDragMs] = useState<number | null>(null);
const trackRef = useRef<HTMLDivElement | null>(null);
const positionFromPointer = useCallback(
(clientX: number): number => {
const el = trackRef.current;
if (!el || durationMs <= 0) return 0;
const rect = el.getBoundingClientRect();
const ratio = Math.min(
Math.max((clientX - rect.left) / rect.width, 0),
1,
);
return Math.round(ratio * durationMs);
},
[durationMs],
);
const handleSeekDown = (e: ReactPointerEvent<HTMLDivElement>) => {
if (!currentTrack || durationMs <= 0) return;
e.currentTarget.setPointerCapture(e.pointerId);
setSeeking(true);
setDragMs(positionFromPointer(e.clientX));
};
const handleSeekMove = (e: ReactPointerEvent<HTMLDivElement>) => {
if (dragMs == null) return;
setDragMs(positionFromPointer(e.clientX));
};
const handleSeekUp = (e: ReactPointerEvent<HTMLDivElement>) => {
if (dragMs == null) return;
const target = dragMs;
setDragMs(null);
setSeeking(false);
e.currentTarget.releasePointerCapture(e.pointerId);
seek(target).catch(() => {});
};
const displayMs = dragMs ?? positionMs;
const progressPct = durationMs > 0 ? (displayMs / durationMs) * 100 : 0;
return (
<div
className="h-screen w-screen flex flex-col overflow-hidden text-white select-none"
style={{ background: gradient }}
>
{/* Top bar. The middle dot strip is the OS-level drag region;
everything else captures clicks normally. Splitting the
drag region this way avoids buttons fighting the move
gesture on Windows where data-tauri-drag-region on a
button-bearing parent intermittently swallows clicks. */}
<div className="flex items-stretch justify-between px-2 py-1 shrink-0">
<button
type="button"
onClick={handleTogglePin}
aria-label={t("miniPlayer.pin")}
title={t("miniPlayer.pin")}
className={`p-1 rounded-full transition-colors ${
pinned
? "text-emerald-400 hover:bg-white/10"
: "text-white/60 hover:text-white hover:bg-white/10"
}`}
>
<Pin size={12} className={pinned ? "fill-current" : ""} />
</button>
<div
data-tauri-drag-region
onMouseDown={(e) => {
// Belt-and-suspenders: data-tauri-drag-region only fires
// when the EXACT mousedown target carries the attribute,
// and pointer-events-none on children isn't enough on
// every platform (notably Windows, where it can race
// the OS hit-test). Calling startDragging explicitly
// makes the gesture deterministic regardless.
if (e.button !== 0) return;
getCurrentWindow()
.startDragging()
.catch((err) =>
console.error("[MiniPlayer] startDragging failed", err),
);
}}
className="flex-1 flex items-center justify-center gap-0.5 text-white/40 cursor-grab active:cursor-grabbing"
>
{Array.from({ length: 6 }).map((_, i) => (
<span
key={i}
className={`pointer-events-none block w-0.5 h-0.5 rounded-full bg-current${i === 3 ? " ml-1" : ""}`}
/>
))}
</div>
<div className="flex items-center gap-0.5">
<button
type="button"
onClick={handleMaximize}
aria-label={t("miniPlayer.maximize")}
title={t("miniPlayer.maximize")}
className="p-1 rounded-full text-white/60 hover:text-white hover:bg-white/10 transition-colors"
>
<Maximize2 size={12} />
</button>
<button
type="button"
onClick={handleClose}
aria-label={t("miniPlayer.close")}
title={t("miniPlayer.close")}
className="p-1 rounded-full text-white/60 hover:text-white hover:bg-white/10 transition-colors"
>
<X size={13} />
</button>
</div>
</div>
{/* Floating cover with hover overlay */}
<div className="px-3 pt-1 pb-2 flex justify-center">
<CoverWithControls
showControls={showControls}
onMouseEnter={() => setShowControls(true)}
onMouseLeave={() => setShowControls(false)}
isPlaying={isPlaying}
repeatMode={repeatMode}
isShuffled={isShuffled}
onPlayPause={togglePlayback}
onPrev={previous}
onNext={next}
onCycleRepeat={cycleRepeatMode}
onToggleShuffle={toggleShuffle}
artworkSlot={
currentTrack ? (
<Artwork
path={currentTrack.artwork_path}
path1x={currentTrack.artwork_path_1x}
path2x={currentTrack.artwork_path_2x}
size="full"
alt={currentTrack.title}
className="w-full h-full object-cover"
rounded="xl"
/>
) : (
<div className="w-full h-full rounded-2xl bg-white/10 flex items-center justify-center">
<Play size={48} className="text-white/40" />
</div>
)
}
/>
</div>
{/* Title + artist */}
<div className="px-3 pb-1.5">
<div
className="text-sm font-semibold truncate leading-tight"
title={currentTrack?.title}
>
{currentTrack?.title ?? t("miniPlayer.idle")}
</div>
<div className="flex items-center justify-between gap-2 mt-0.5">
<div
className="text-[11px] text-white/70 truncate"
title={currentTrack?.artist_name ?? undefined}
>
{currentTrack?.artist_name ?? "β"}
</div>
{/* Like button is local-library only β Spotify tracks
don't live in the WaveFlow DB so toggleLikeTrack would
fail silently on a missing row. */}
{!isSpotify && (
<button
type="button"
onClick={handleLike}
disabled={!currentTrack}
aria-label={t("miniPlayer.like")}
className="p-0.5 disabled:opacity-30 shrink-0"
>
<Heart
size={14}
className={
isLiked
? "fill-emerald-400 text-emerald-400"
: "text-white/60 hover:text-white"
}
/>
</button>
)}
</div>
</div>
{/* Interactive seek bar β Spotify-style: thin idle, thicker
on hover with timestamps revealed at both ends. */}
<div className="mt-auto px-3 pb-2 group">
<div
ref={trackRef}
onPointerDown={handleSeekDown}
onPointerMove={handleSeekMove}
onPointerUp={handleSeekUp}
onPointerCancel={handleSeekUp}
className={`relative h-1 rounded-full bg-white/20 ${currentTrack && durationMs > 0 ? "cursor-pointer" : "cursor-default"}`}
>
<div
className="absolute inset-y-0 left-0 rounded-full bg-white"
style={{ width: `${Math.min(100, progressPct)}%` }}
/>
{currentTrack && durationMs > 0 && (
<div
className="absolute top-1/2 -translate-y-1/2 w-2.5 h-2.5 rounded-full bg-white shadow opacity-0 group-hover:opacity-100 transition-opacity"
style={{ left: `calc(${Math.min(100, progressPct)}% - 5px)` }}
/>
)}
</div>
<div className="flex justify-between text-[9px] text-white/60 tabular-nums mt-1 opacity-0 group-hover:opacity-100 transition-opacity">
<span>{formatDuration(displayMs)}</span>
<span>{formatDuration(durationMs)}</span>
</div>
</div>
</div>
);
}
interface CoverWithControlsProps {
showControls: boolean;
onMouseEnter: () => void;
onMouseLeave: () => void;
isPlaying: boolean;
repeatMode: "off" | "all" | "one";
isShuffled: boolean;
onPlayPause: () => void;
onPrev: () => void;
onNext: () => void;
onCycleRepeat: () => void;
onToggleShuffle: () => void;
artworkSlot: React.ReactNode;
}
function CoverWithControls({
showControls,
onMouseEnter,
onMouseLeave,
isPlaying,
repeatMode,
isShuffled,
onPlayPause,
onPrev,
onNext,
onCycleRepeat,
onToggleShuffle,
artworkSlot,
}: CoverWithControlsProps) {
const ref = useRef<HTMLDivElement | null>(null);
return (
<div
ref={ref}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
className="relative aspect-square w-full max-w-64 rounded-xl shadow-2xl overflow-hidden"
>
{artworkSlot}
{/* Dimming layer + control bar fade in on hover. */}
<div
className={`absolute inset-0 flex items-center justify-center transition-opacity duration-150 ${
showControls ? "opacity-100 bg-black/40" : "opacity-0"
}`}
>
<div className="flex items-center gap-2">
<IconButton
onClick={onToggleShuffle}
label="shuffle"
active={isShuffled}
>
<Shuffle size={14} />
</IconButton>
<IconButton onClick={onPrev} label="previous">
<SkipBack size={16} />
</IconButton>
<button
type="button"
onClick={onPlayPause}
aria-label={isPlaying ? "pause" : "play"}
className="w-11 h-11 rounded-full bg-white text-black flex items-center justify-center hover:scale-105 transition-transform"
>
{isPlaying ? (
<Pause size={18} className="fill-current" />
) : (
<Play size={18} className="fill-current ml-0.5" />
)}
</button>
<IconButton onClick={onNext} label="next">
<SkipForward size={16} />
</IconButton>
<IconButton
onClick={onCycleRepeat}
label="repeat"
active={repeatMode !== "off"}
>
{repeatMode === "one" ? (
<Repeat1 size={14} />
) : (
<Repeat size={14} />
)}
</IconButton>
</div>
</div>
</div>
);
}
function IconButton({
onClick,
label,
active,
children,
}: {
onClick: () => void;
label: string;
active?: boolean;
children: React.ReactNode;
}) {
return (
<button
type="button"
onClick={onClick}
aria-label={label}
className={`p-2 rounded-full transition-colors ${
active
? "text-emerald-400 hover:bg-white/10"
: "text-white/80 hover:text-white hover:bg-white/10"
}`}
>
{children}
</button>
);
}