Skip to content

Commit

Permalink
feat: using the scroll wheel to set the volume
Browse files Browse the repository at this point in the history
  • Loading branch information
nini22P committed Sep 17, 2024
1 parent ffd80b9 commit 464d07d
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions src/pages/Player/VolumeControl.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { IconButton, Popover, Box, Slider } from '@mui/material'
import { IconButton, Popover, Box, Slider, Tooltip } from '@mui/material'
import { useState } from 'react'
import VolumeUpIcon from '@mui/icons-material/VolumeUp'
import VolumeDownIcon from '@mui/icons-material/VolumeDown'
import VolumeOffIcon from '@mui/icons-material/VolumeOff'
import useUiStore from '@/store/useUiStore'
import { useWheel } from '@use-gesture/react'

export default function VolumeControl() {

Expand All @@ -22,23 +23,45 @@ export default function VolumeControl() {
const [volumeAnchorEl, setVolumeAnchorEl] = useState<HTMLElement | null>(null)
const volumeOpen = Boolean(volumeAnchorEl)

const bind = useWheel((state) => {
const _volume: number = Number((volume - state.movement[1] / 100).toFixed(0))
updateVolume(Math.min(Math.max(_volume, 0), 100))
})

return (
<div>
<IconButton onClick={(event: React.MouseEvent<HTMLButtonElement>) => setVolumeAnchorEl(event.currentTarget)} >
{
volume === 0
? <VolumeOffIcon />
: volume < 50
? <VolumeDownIcon/>
: <VolumeUpIcon />
}
</IconButton>
<div {...bind()}>
<Tooltip
title={volume}
placement='top'
slotProps={{
popper: {
modifiers: [
{
name: 'offset',
options: {
offset: [0, -10],
},
},
],
},
}}
>
<IconButton onClick={(event: React.MouseEvent<HTMLButtonElement>) => setVolumeAnchorEl(event.currentTarget)} >
{
volume === 0
? <VolumeOffIcon />
: volume < 50
? <VolumeDownIcon />
: <VolumeUpIcon />
}
</IconButton>
</Tooltip>
<Popover
open={volumeOpen}
onClose={() => setVolumeAnchorEl(null)}
anchorEl={volumeAnchorEl}
anchorOrigin={{
vertical: audioViewIsShow ? 'bottom': 'top',
vertical: audioViewIsShow ? 'bottom' : 'top',
horizontal: 'center',
}}
transformOrigin={{
Expand All @@ -52,8 +75,9 @@ export default function VolumeControl() {
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
padding: '1rem 0.5rem 1rem 0.5rem',
gap: '1em',
padding: '1rem 0.75rem 1.75rem 0.75rem',
gap: '1.5em',
minHeight: '240px',
}}
>
{volume}
Expand All @@ -65,7 +89,7 @@ export default function VolumeControl() {
max={100}
onChange={(_, value) => updateVolume(value as number)}
sx={{
minHeight: '160px',
flexGrow: 1,
}}
/>
</Box>
Expand Down

0 comments on commit 464d07d

Please sign in to comment.