Skip to content
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

Extract track progress in separate component + new player hooks #712

Merged
merged 1 commit into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/renderer/components/Header/Header.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}

.header {
border-bottom: 1px solid var(--border-color);
border-bottom: solid 1px var(--border-color);
background-color: var(--header-bg);
color: var(--header-color);
padding: 0 10px;
Expand Down Expand Up @@ -57,3 +57,12 @@
min-width: 0;
max-width: 600px;
}

.header__trackProgress {
-webkit-app-region: no-drag;
z-index: 1000;
position: absolute;
bottom: 0;
right: 0;
left: 0;
}
2 changes: 1 addition & 1 deletion src/renderer/components/PlayingBar/PlayingBar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

.playingBar__cover {
flex-shrink: 0;
width: 49px;
width: 48px;
overflow: hidden;
}

Expand Down
64 changes: 1 addition & 63 deletions src/renderer/components/PlayingBarInfo/PlayingBarInfo.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
display: flex;
flex-direction: column;
justify-content: flex-end;
padding: 0 6px;
padding: 0 4px;
}

.playingBar__info__metas {
Expand Down Expand Up @@ -39,65 +39,3 @@
font-variant-numeric: tabular-nums;
white-space: nowrap;
}

.playingBar__info__progress {
position: relative;
}

.progressTooltip {
position: absolute;
background-color: var(--background);
border: 1px solid var(--border-color);
font-size: 10px;
padding: 2px 5px;
bottom: 10px;
z-index: 1;
transform: translateX(-11px);

&::before,
&::after {
content: '';
position: absolute;
width: 0;
height: 0;
border-style: solid;
border-color: transparent;
border-bottom: 0;
}

/* Stroke */
&::before {
top: 16px;
left: 5px;
border-top-color: var(--border-color);
border-width: 6px;
}

/* Fill */
&::after {
top: 16px;
left: 6px;
border-top-color: var(--background);
border-width: 5px;
}
}

.progress {
-webkit-app-region: no-drag;
background-color: var(--progress-bg);
outline: none;
height: 5px;
margin-bottom: 0;
margin-top: 3px;
cursor: pointer;

.progressBar {
background-color: var(--main-color);
pointer-events: none;
transition: none;
transform-origin: left;
will-change: transform;
width: 100%;
height: 100%;
}
}
132 changes: 4 additions & 128 deletions src/renderer/components/PlayingBarInfo/PlayingBarInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';

import * as utils from '../../lib/utils';
import { TrackModel, Repeat } from '../../../shared/types/museeks';
import { usePlayerAPI } from '../../stores/usePlayerStore';
import player from '../../lib/player';
import TrackProgress from '../TrackProgress/TrackProgress';
import usePlayingTrackCurrentTime from '../../hooks/usePlayingTrackCurrentTime';

import styles from './PlayingBarInfo.module.css';

Expand All @@ -15,100 +13,7 @@ type Props = {

export default function PlayingBarInfo(props: Props) {
const { trackPlaying } = props;

const playingBar = useRef<HTMLDivElement>(null);

const [elapsed, setElapsed] = useState(0);
const [duration, setDuration] = useState<number | null>(null);
const [x, setX] = useState<number | null>(null);
const [dragging, setDragging] = useState(false);

const playerAPI = usePlayerAPI();

const tick = useCallback(() => {
setElapsed(player.getCurrentTime());
}, []);

const jumpAudioTo = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
setDragging(true);

if (playingBar.current) {
const parent = playingBar.current.offsetParent as HTMLDivElement;
const percent =
((e.pageX - (playingBar.current.offsetLeft + parent.offsetLeft)) /
playingBar.current.offsetWidth) *
100;

const to = (percent * trackPlaying.duration) / 100;

playerAPI.jumpTo(to);
}
},
[playingBar, trackPlaying, playerAPI],
);

const dragOver = useCallback(
(e: MouseEvent) => {
// Check if a currentTime update is needed
if (dragging) {
if (playingBar.current) {
const playingBarRect = playingBar.current.getBoundingClientRect();

const barWidth = playingBar.current.offsetWidth;
const offsetX = Math.min(
Math.max(0, e.pageX - playingBarRect.left),
barWidth,
);

const percent = (offsetX / barWidth) * 100;

const to = (percent * trackPlaying.duration) / 100;

playerAPI.jumpTo(to);
}
}
},
[playingBar, dragging, trackPlaying, playerAPI],
);

const dragEnd = useCallback(() => {
setDragging(false);
}, []);

const showTooltip = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
const { offsetX } = e.nativeEvent;
const barWidth = e.currentTarget.offsetWidth;

const percent = (offsetX / barWidth) * 100;

const time = (percent * trackPlaying.duration) / 100;

setDuration(time);
setX(percent);
},
[trackPlaying],
);

const hideTooltip = useCallback(() => {
setDuration(null);
setX(null);
}, []);

useEffect(() => {
player.getAudio().addEventListener('timeupdate', tick);

window.addEventListener('mousemove', dragOver);
window.addEventListener('mouseup', dragEnd);

return () => {
player.getAudio().removeEventListener('timeupdate', tick);

window.removeEventListener('mousemove', dragOver);
window.removeEventListener('mouseup', dragEnd);
};
}, [dragEnd, dragOver, tick]);
const elapsed = usePlayingTrackCurrentTime();

return (
<div className={styles.playingBar__info}>
Expand All @@ -129,36 +34,7 @@ export default function PlayingBarInfo(props: Props) {
{utils.parseDuration(trackPlaying.duration)}
</div>
</div>
<div className={styles.playingBar__info__progress} ref={playingBar}>
<div
className={styles.progressTooltip}
hidden={duration === null}
style={{ left: `${x}%` }}
>
{utils.parseDuration(duration)}
</div>
<div
className={styles.progress}
role="progressbar"
tabIndex={0}
onMouseDown={jumpAudioTo}
onMouseMove={showTooltip}
onMouseLeave={hideTooltip}
>
<div
className={styles.progressBar}
style={
elapsed / trackPlaying.duration <= 1
? {
transform: `scale3d(${
elapsed / trackPlaying.duration
}, 1, 1)`,
}
: { display: 'none' }
}
/>
</div>
</div>
<TrackProgress trackPlaying={trackPlaying} />
</div>
);
}
66 changes: 66 additions & 0 deletions src/renderer/components/TrackProgress/TrackProgress.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
.trackRoot {
-webkit-app-region: no-drag;
position: relative;
display: flex;
align-items: center;
user-select: none;
touch-action: none;
height: 5px;

/* the track progress is too close to the metadata, but using margin would
* push the whole section up */
transform: translateY(3px);
}

.trackProgress {
display: block;
width: 100%;
height: 100%;
background-color: var(--progress-bg);
box-shadow: inset 0 0 0 1px var(--border-color);
}

.trackRange {
position: absolute;
height: 100%;
background-color: var(--main-color);
box-shadow: inset 0 0 0 1px rgba(0 0 0 / 0.2);
}

.progressTooltip {
position: absolute;
background-color: var(--background);
border: 1px solid var(--border-color);
font-size: 10px;
padding: 2px 5px;
bottom: 10px;
z-index: 1;
transform: translateX(-11px);

&::before,
&::after {
content: '';
position: absolute;
width: 0;
height: 0;
border-style: solid;
border-color: transparent;
border-bottom: 0;
}

/* Stroke */
&::before {
top: 16px;
left: 5px;
border-top-color: var(--border-color);
border-width: 6px;
}

/* Fill */
&::after {
top: 15px;
left: 6px;
border-top-color: var(--background);
border-width: 5px;
}
}
Loading