Skip to content

Commit

Permalink
Merge pull request #5585 from scampower3/lyrics-auto-scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
thornbill authored Jun 25, 2024
2 parents f30343c + 7b765b7 commit cf0d207
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/controllers/lyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@ import { appRouter } from '../components/router/appRouter';
import layoutManager from 'components/layoutManager';
import { playbackManager } from '../components/playback/playbackmanager';
import ServerConnections from '../components/ServerConnections';
import scrollManager from 'components/scrollManager';
import focusManager from 'components/focusManager';

import keyboardNavigation from 'scripts/keyboardNavigation';
import globalize from '../scripts/globalize';
import LibraryMenu from '../scripts/libraryMenu';
import Events from '../utils/events.ts';

import '../styles/lyrics.scss';
import { AutoScroll } from './lyrics.types';

let currentPlayer;
let currentItem;

let savedLyrics;
let isDynamicLyric = false;
let autoScroll = AutoScroll.Instant;

function dynamicLyricHtmlReducer(htmlAccumulator, lyric, index) {
if (layoutManager.tv) {
Expand Down Expand Up @@ -69,6 +74,12 @@ export default function (view) {
if (lyric) {
lyric.classList.remove('pastLyric');
lyric.classList.remove('futureLyric');
if (autoScroll !== AutoScroll.NoScroll) {
// instant scroll is used when the view is first loaded
scrollManager.scrollToElement(lyric, autoScroll === AutoScroll.Smooth);
focusManager.focus(lyric);
autoScroll = AutoScroll.Smooth;
}
}
}

Expand Down Expand Up @@ -178,6 +189,7 @@ export default function (view) {
}

function onLyricClick(lyricTime) {
autoScroll = AutoScroll.Smooth;
playbackManager.seek(lyricTime);
if (playbackManager.paused()) {
playbackManager.playPause(currentPlayer);
Expand Down Expand Up @@ -234,8 +246,23 @@ export default function (view) {
}
}

function onWheelOrTouchMove() {
autoScroll = AutoScroll.NoScroll;
}

function onKeyDown(e) {
const key = keyboardNavigation.getKeyName(e);
if (key === 'ArrowUp' || key === 'ArrowDown') {
autoScroll = AutoScroll.NoScroll;
}
}

view.addEventListener('viewshow', function () {
Events.on(playbackManager, 'playerchange', onPlayerChange);
autoScroll = AutoScroll.Instant;
document.addEventListener('wheel', onWheelOrTouchMove);
document.addEventListener('touchmove', onWheelOrTouchMove);
document.addEventListener('keydown', onKeyDown);
try {
onLoad();
} catch (e) {
Expand All @@ -245,6 +272,9 @@ export default function (view) {

view.addEventListener('viewbeforehide', function () {
Events.off(playbackManager, 'playerchange', onPlayerChange);
document.removeEventListener('wheel', onWheelOrTouchMove);
document.removeEventListener('touchmove', onWheelOrTouchMove);
document.removeEventListener('keydown', onKeyDown);
releaseCurrentPlayer();
});
}
5 changes: 5 additions & 0 deletions src/controllers/lyrics.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum AutoScroll {
NoScroll = 0,
Smooth = 1,
Instant = 2
}

0 comments on commit cf0d207

Please sign in to comment.