refactor: remove dynamic require (partial of #2)

Co-authored-by: Su-Yong <simssy2205@gmail.com>
This commit is contained in:
JellyBrick
2023-10-03 23:42:12 +09:00
parent 6eadc7f7e5
commit 6e315b9af2
24 changed files with 841 additions and 745 deletions

View File

@ -5,17 +5,46 @@ import { BrowserWindow, nativeImage } from 'electron';
import getSongControls from '../../providers/song-controls';
import registerCallback, { SongInfo } from '../../providers/song-info';
let controls: {
playPause: () => void;
next: () => void;
previous: () => void;
};
let currentSongInfo: SongInfo;
export default (win: BrowserWindow) => {
let currentSongInfo: SongInfo;
const { playPause, next, previous } = getSongControls(win);
controls = { playPause, next, previous };
const setThumbar = (win: BrowserWindow, songInfo: SongInfo) => {
// Wait for song to start before setting thumbar
if (!songInfo?.title) {
return;
}
// Win32 require full rewrite of components
win.setThumbarButtons([
{
tooltip: 'Previous',
icon: nativeImage.createFromPath(get('previous')),
click() {
previous();
},
}, {
tooltip: 'Play/Pause',
// Update icon based on play state
icon: nativeImage.createFromPath(songInfo.isPaused ? get('play') : get('pause')),
click() {
playPause();
},
}, {
tooltip: 'Next',
icon: nativeImage.createFromPath(get('next')),
click() {
next();
},
},
]);
};
// Util
const get = (kind: string) => {
return path.join(__dirname, '../../assets/media-icons-black', `${kind}.png`);
};
registerCallback((songInfo) => {
// Update currentsonginfo for win.on('show')
@ -29,39 +58,3 @@ export default (win: BrowserWindow) => {
setThumbar(win, currentSongInfo);
});
};
function setThumbar(win: BrowserWindow, songInfo: SongInfo) {
// Wait for song to start before setting thumbar
if (!songInfo?.title) {
return;
}
// Win32 require full rewrite of components
win.setThumbarButtons([
{
tooltip: 'Previous',
icon: nativeImage.createFromPath(get('previous')),
click() {
controls.previous();
},
}, {
tooltip: 'Play/Pause',
// Update icon based on play state
icon: nativeImage.createFromPath(songInfo.isPaused ? get('play') : get('pause')),
click() {
controls.playPause();
},
}, {
tooltip: 'Next',
icon: nativeImage.createFromPath(get('next')),
click() {
controls.next();
},
},
]);
}
// Util
function get(kind: string) {
return path.join(__dirname, '../../assets/media-icons-black', `${kind}.png`);
}