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
31 changes: 31 additions & 0 deletions src/controllers/lyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ 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 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 { AutoScrollType } from './lyrics.types';

let currentPlayer;
let currentItem;

let savedLyrics;
let isDynamicLyric = false;
let autoScroll = AutoScrollType.Smooth;
scampower3 marked this conversation as resolved.
Show resolved Hide resolved

function dynamicLyricHtmlReducer(htmlAccumulator, lyric, index) {
if (layoutManager.tv) {
Expand Down Expand Up @@ -69,6 +73,14 @@ export default function (view) {
if (lyric) {
lyric.classList.remove('pastLyric');
lyric.classList.remove('futureLyric');
if (autoScroll === AutoScrollType.Smooth) {
scrollManager.scrollToElement(lyric, true);
dmitrylyzo marked this conversation as resolved.
Show resolved Hide resolved
}
if (autoScroll === AutoScrollType.Instant) {
// instant scroll is used when the view is first loaded
scrollManager.scrollToElement(lyric, false);
autoScroll = AutoScrollType.Smooth;
}
scampower3 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

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

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

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

view.addEventListener('viewshow', function () {
Events.on(playbackManager, 'playerchange', onPlayerChange);
autoScroll = AutoScrollType.Instant;
document.addEventListener('wheel', onWheelOrTouchMove);
document.addEventListener('touchmove', onWheelOrTouchMove);
document.addEventListener('keydown', onKeyDown);
try {
onLoad();
} catch (e) {
Expand All @@ -245,6 +273,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 AutoScrollType {
scampower3 marked this conversation as resolved.
Show resolved Hide resolved
NoScroll = 0,
Smooth = 1,
Instant = 2
}
Loading