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

Add lyrics auto scroll #5585

Merged
merged 10 commits into from
Jun 25, 2024
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
}
Loading