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

Fix scroll to seek/change volume/playback rate changing too fast with touchpad #6666

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
import {
addKeyboardShortcutToActionTitle,
showToast,
writeFileWithPicker
writeFileWithPicker,
throttle,
} from '../../helpers/utils'

/** @typedef {import('../../helpers/sponsorblock').SponsorBlockCategory} SponsorBlockCategory */
Expand Down Expand Up @@ -904,13 +905,13 @@ export default defineComponent({

if (event.ctrlKey || event.metaKey) {
if (videoPlaybackRateMouseScroll.value) {
mouseScrollPlaybackRate(event)
mouseScrollPlaybackRateHandler(event)
}
} else {
if (videoVolumeMouseScroll.value) {
mouseScrollVolume(event)
mouseScrollVolumeHandler(event)
} else if (videoSkipMouseScroll.value) {
mouseScrollSkip(event)
mouseScrollSkipHandler(event)
}
}
}
Expand Down Expand Up @@ -947,7 +948,7 @@ export default defineComponent({
}

// make scrolling over volume slider change the volume
container.value.querySelector('.shaka-volume-bar').addEventListener('wheel', mouseScrollVolume)
container.value.querySelector('.shaka-volume-bar').addEventListener('wheel', mouseScrollVolumeHandler)

// title overlay when the video is fullscreened
// placing this inside the controls container so that we can fade it in and out at the same time as the controls
Expand Down Expand Up @@ -1925,56 +1926,79 @@ export default defineComponent({

// #region mouse scroll handlers

const mouseScrollThrottleWaitMs = 100

/**
* @param {WheelEvent} event
*/
function mouseScrollPlaybackRate(event) {
event.preventDefault()

if ((event.deltaY < 0 || event.deltaX > 0)) {
changePlayBackRate(0.05)
} else if ((event.deltaY > 0 || event.deltaX < 0)) {
changePlayBackRate(-0.05)
}
}
const mouseScrollPlaybackRateThrottle = throttle(mouseScrollPlaybackRate, mouseScrollThrottleWaitMs)
/**
* @param {WheelEvent} event
*/
function mouseScrollPlaybackRateHandler(event) {
event.preventDefault()

mouseScrollPlaybackRateThrottle(event)
}

/**
* @param {WheelEvent} event
*/
function mouseScrollSkip(event) {
if ((event.deltaY < 0 || event.deltaX > 0)) {
seekBySeconds(defaultSkipInterval.value * video.value.playbackRate, true)
} else if ((event.deltaY > 0 || event.deltaX < 0)) {
seekBySeconds(-defaultSkipInterval.value * video.value.playbackRate, true)
}
}
const mouseScrollSkipThrottle = throttle(mouseScrollSkip, mouseScrollThrottleWaitMs)
/**
* @param {WheelEvent} event
*/
function mouseScrollSkipHandler(event) {
if (canSeek()) {
event.preventDefault()

mouseScrollSkipThrottle(event)
}
}

/**
* @param {WheelEvent} event
*/
function mouseScrollVolume(event) {
const video_ = video.value

if (video_.muted && (event.deltaY < 0 || event.deltaX > 0)) {
video_.muted = false
video_.volume = 0
}

if (!video_.muted) {
if ((event.deltaY < 0 || event.deltaX > 0)) {
seekBySeconds(defaultSkipInterval.value * video.value.playbackRate, true)
changeVolume(0.05)
} else if ((event.deltaY > 0 || event.deltaX < 0)) {
seekBySeconds(-defaultSkipInterval.value * video.value.playbackRate, true)
changeVolume(-0.05)
}
}
}

const mouseScrollVolumeThrottle = throttle(mouseScrollVolume, mouseScrollThrottleWaitMs)
/**
* @param {WheelEvent} event
*/
function mouseScrollVolume(event) {
function mouseScrollVolumeHandler(event) {
if (!event.ctrlKey && !event.metaKey) {
event.preventDefault()
event.stopPropagation()

const video_ = video.value

if (video_.muted && (event.deltaY < 0 || event.deltaX > 0)) {
video_.muted = false
video_.volume = 0
}

if (!video_.muted) {
if ((event.deltaY < 0 || event.deltaX > 0)) {
changeVolume(0.05)
} else if ((event.deltaY > 0 || event.deltaX < 0)) {
changeVolume(-0.05)
}
}
mouseScrollVolumeThrottle(event)
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/renderer/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,3 +1052,28 @@ export function debounce(func, wait) {
}, wait)
}
}

/**
* @template {Function} T
* @param {T} func
* @param {number} wait
* @returns {T}
*/
export function throttle(func, wait) {
let isWaiting

// Using a fully fledged function here instead of an arrow function
// so that we can get `this` and pass it onto the original function.
// Vue components using the options API use `this` alot.
return function (...args) {
const context = this
if (!isWaiting) {
func.apply(context, args)

isWaiting = true
setTimeout(() => {
isWaiting = false
}, wait)
}
}
}