Select current playback rate in playback rate selector (#1902)

* Select current playback rate in playback rate selector

* More concise implementation

Co-authored-by: PikachuEXE <pikachuexe@gmail.com>

* Scroll to the selected element instead of focusing it

Co-authored-by: PikachuEXE <pikachuexe@gmail.com>
This commit is contained in:
absidue 2021-11-30 22:08:33 +00:00 committed by GitHub
parent 842dc93231
commit 82b223ea97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 0 deletions

View File

@ -322,6 +322,36 @@ export default Vue.extend({
this.player.removeChild('BigPlayButton') this.player.removeChild('BigPlayButton')
} }
// Makes the playback rate menu focus the current item on mouse hover
// or the closest item if the playback rate is between two items
// which is likely to be the case when the playback rate is changed by scrolling
const playbackRateMenuButton = this.player.controlBar.getChild('playbackRateMenuButton')
playbackRateMenuButton.on(playbackRateMenuButton.menuButton_, 'mouseenter', () => {
const playbackRate = this.player.playbackRate()
const rates = this.player.playbackRates()
// iterate through the items in reverse order as the highest is displayed first
// `slice` must be used as `reverse` does reversing in place
const targetPlaybackRateMenuItemIndex = rates.slice().reverse().findIndex((rate) => {
return rate === playbackRate || rate < playbackRate
})
// center the selected item in the middle of the visible area
// the first and last items will never be in the center so it can be skipped for them
if (targetPlaybackRateMenuItemIndex !== 0 && targetPlaybackRateMenuItemIndex !== rates.length - 1) {
const playbackRateMenu = playbackRateMenuButton.menu
const menuElement = playbackRateMenu.contentEl()
const itemHeight = playbackRateMenu.children()[targetPlaybackRateMenuItemIndex].contentEl().clientHeight
// clientHeight is the height of the visible part of an element
const centerOfVisibleArea = (menuElement.clientHeight - itemHeight) / 2
const menuScrollOffset = (itemHeight * targetPlaybackRateMenuItemIndex) - centerOfVisibleArea
menuElement.scrollTo({ top: menuScrollOffset })
}
})
if (this.storyboardSrc !== '') { if (this.storyboardSrc !== '') {
this.player.vttThumbnails({ this.player.vttThumbnails({
src: this.storyboardSrc, src: this.storyboardSrc,