-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New plugin: Taskbar Progress #4057
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { createPlugin } from '@/utils'; | ||
| import { registerCallback, type SongInfo } from '@/providers/song-info'; | ||
| import { t } from '@/i18n'; | ||
|
|
||
| export default createPlugin({ | ||
| name: () => t('plugins.taskbar-progress.name'), | ||
| description: () => t('plugins.taskbar-progress.description'), | ||
| restartNeeded: true, | ||
| config: { enabled: false }, | ||
|
|
||
| backend({ window }) { | ||
| let lastSongInfo: SongInfo | null = null; | ||
| let progressInterval: ReturnType<typeof setInterval> | null = null; | ||
|
|
||
| const updateProgressBar = (songInfo: SongInfo) => { | ||
| if ( | ||
| !songInfo?.title || | ||
| typeof songInfo.elapsedSeconds !== 'number' || | ||
| !songInfo.songDuration | ||
| ) { | ||
| return; | ||
| } | ||
|
Comment on lines
+16
to
+22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'd be better to use zod here |
||
|
|
||
| if ( | ||
| !lastSongInfo || | ||
| songInfo.title !== lastSongInfo.title || | ||
| songInfo.elapsedSeconds !== lastSongInfo.elapsedSeconds || | ||
| songInfo.isPaused !== lastSongInfo.isPaused | ||
| ) { | ||
| lastSongInfo = songInfo; | ||
| } | ||
|
|
||
| const progress = songInfo.elapsedSeconds / songInfo.songDuration; | ||
| window.setProgressBar(progress, { | ||
| mode: songInfo.isPaused ? 'paused' : 'normal', | ||
| }); | ||
| }; | ||
|
|
||
| const startProgressInterval = (songInfo: SongInfo) => { | ||
| stopProgressInterval(); | ||
| if (!songInfo.isPaused) { | ||
| progressInterval = setInterval(() => { | ||
| if ( | ||
| lastSongInfo && | ||
| !lastSongInfo.isPaused && | ||
| typeof lastSongInfo.elapsedSeconds === 'number' | ||
| ) { | ||
| updateProgressBar({ | ||
| ...lastSongInfo, | ||
| elapsedSeconds: lastSongInfo.elapsedSeconds + 1, | ||
| }); | ||
| } | ||
| }, 1000); | ||
|
Comment on lines
+42
to
+53
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an interval is not guaranteed to run on a precise manner, could you maybe refactor this to not depend on the interval, and maybe calculate the elapsedSeconds using an external variable? e.g. const intervalStart = performance.now();
setInterval(() => {
const elapsedSeconds = (performance.now() - intervalStart) / 1000;
// ...
}, 1000) |
||
| } | ||
| }; | ||
|
|
||
| const stopProgressInterval = () => { | ||
| if (progressInterval) { | ||
| clearInterval(progressInterval); | ||
| progressInterval = null; | ||
| } | ||
| }; | ||
|
|
||
| registerCallback((songInfo) => { | ||
| if (!songInfo?.title) return; | ||
| updateProgressBar(songInfo); | ||
| if (songInfo.isPaused) { | ||
| stopProgressInterval(); | ||
| } else { | ||
| startProgressInterval(songInfo); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| stopProgressInterval(); | ||
| window.setProgressBar(-1); | ||
| }; | ||
| }, | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please properly implement the cleanup logic required to turn this into
restartNeeded: false, you just need to cleanup some intervalsyou cannot unregister the song info callback, but you can remove all side-effects when the plugin is disabled