diff --git a/src/renderer/controllers/playback-controller.js b/src/renderer/controllers/playback-controller.js index b09d0fa2..717ee0b0 100644 --- a/src/renderer/controllers/playback-controller.js +++ b/src/renderer/controllers/playback-controller.js @@ -158,16 +158,18 @@ module.exports = class PlaybackController { else this.state.playing.jumpToTime = time } - preview (time, x) { - if (!Number.isFinite(time)) { - console.error('Tried to skip to a non-finite time ' + time) + // Show video preview + preview (x) { + if (!Number.isFinite(x)) { + console.error('Tried to preview a non-finite position ' + x) return console.trace() } - this.state.playing.preview = { time, x } + this.state.playing.previewXCoord = x } + // Hide video preview clearPreview () { - this.state.playing.preview = null + this.state.playing.previewXCoord = null } // Change playback speed. 1 = faster, -1 = slower diff --git a/src/renderer/main.js b/src/renderer/main.js index ec02ca2c..00738ca8 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -277,7 +277,7 @@ const dispatchHandlers = { previousTrack: () => controllers.playback().previousTrack(), skip: (time) => controllers.playback().skip(time), skipTo: (time) => controllers.playback().skipTo(time), - preview: (time, x) => controllers.playback().preview(time, x), + preview: (x) => controllers.playback().preview(x), clearPreview: () => controllers.playback().clearPreview(), changePlaybackRate: (dir) => controllers.playback().changePlaybackRate(dir), changeVolume: (delta) => controllers.playback().changeVolume(delta), diff --git a/src/renderer/pages/player-page.js b/src/renderer/pages/player-page.js index 9cb679bb..45c918e2 100644 --- a/src/renderer/pages/player-page.js +++ b/src/renderer/pages/player-page.js @@ -728,12 +728,10 @@ function renderPlayerControls (state) { // Handles a scrub hover (preview another position in the video) function handleScrubPreview (e) { + // Only show for videos if (!e.clientX || state.playing.type !== 'video') return dispatch('mediaMouseMoved') - const windowWidth = document.querySelector('body').clientWidth - const fraction = e.clientX / windowWidth - const position = fraction * state.playing.duration /* seconds */ - dispatch('preview', position, e.clientX) + dispatch('preview', e.clientX) } function clearPreview (e) { @@ -779,27 +777,36 @@ function renderPlayerControls (state) { } function renderPreview (state) { - let { preview } = state.playing - if (!preview) { - preview = { x: 0, time: 0, hide: true } - } + const { previewXCoord = null } = state.playing + + // Calculate time from x-coord as fraction of track width + const windowWidth = document.querySelector('body').clientWidth + const fraction = previewXCoord / windowWidth + const time = fraction * state.playing.duration /* seconds */ const height = 70 - let width + let width = 0 const previewEl = document.querySelector('video#preview') - if (previewEl !== null && !preview.hide) { - previewEl.currentTime = preview.time + if (previewEl !== null && previewXCoord !== null) { + previewEl.currentTime = time + + // Auto adjust width to maintain video aspect ratio width = Math.floor((previewEl.videoWidth / previewEl.videoHeight) * height) } - const windowWidth = document.querySelector('body').clientWidth - const xPos = Math.min(Math.max(preview.x - (width / 2), 5), windowWidth - width - 5) + // Center preview window on mouse cursor, + // while avoiding falling off the left or right edges + const xPos = Math.min(Math.max(previewXCoord - (width / 2), 5), windowWidth - width - 5) return (
)