From 1a89fbe612821acd055b132ec0b432b4d9f2341e Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Sat, 13 Jan 2024 09:06:41 +0900 Subject: [PATCH] fix(#1543): fix song control doesn't work (#1637) Co-authored-by: Su-Yong --- src/providers/song-controls.ts | 87 +++++++++++----------------------- src/renderer.ts | 33 +++++++++++++ 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/src/providers/song-controls.ts b/src/providers/song-controls.ts index 75af545f..3d1c7f61 100644 --- a/src/providers/song-controls.ts +++ b/src/providers/song-controls.ts @@ -1,66 +1,35 @@ // This is used for to control the songs -import { BrowserWindow } from 'electron'; - -type Modifiers = ( - | Electron.MouseInputEvent - | Electron.MouseWheelInputEvent - | Electron.KeyboardInputEvent -)['modifiers']; -export const pressKey = ( - window: BrowserWindow, - key: string, - modifiers: Modifiers = [], -) => { - window.webContents.sendInputEvent({ - type: 'keyDown', - modifiers, - keyCode: key, - }); -}; +import { BrowserWindow, ipcMain } from 'electron'; export default (win: BrowserWindow) => { const commands = { - // Playback - previous: () => pressKey(win, 'k'), - next: () => pressKey(win, 'j'), - playPause: () => pressKey(win, ';'), - like: () => pressKey(win, '+'), - dislike: () => pressKey(win, '_'), - go10sBack: () => pressKey(win, 'h'), - go10sForward: () => pressKey(win, 'l'), - go1sBack: () => pressKey(win, 'h', ['shift']), - go1sForward: () => pressKey(win, 'l', ['shift']), - shuffle: () => pressKey(win, 's'), - switchRepeat(n = 1) { - for (let i = 0; i < n; i++) { - pressKey(win, 'r'); - } - }, - // General - volumeMinus10: () => pressKey(win, '-'), - volumePlus10: () => pressKey(win, '='), - fullscreen: () => pressKey(win, 'f'), - muteUnmute: () => pressKey(win, 'm'), - maximizeMinimisePlayer: () => pressKey(win, 'q'), - // Navigation - goToHome() { - pressKey(win, 'g'); - pressKey(win, 'h'); - }, - goToLibrary() { - pressKey(win, 'g'); - pressKey(win, 'l'); - }, - goToSettings() { - pressKey(win, 'g'); - pressKey(win, ','); - }, - goToExplore() { - pressKey(win, 'g'); - pressKey(win, 'e'); - }, - search: () => pressKey(win, '/'), - showShortcuts: () => pressKey(win, '/', ['shift']), + // Playback + previous: () => win.webContents.send('ytmd:previous-video'), + next: () => win.webContents.send('ytmd:next-video'), + playPause: () => win.webContents.send('ytmd:toggle-play'), + like: () => win.webContents.send('ytmd:update-like', 'LIKE'), + dislike: () => win.webContents.send('ytmd:update-like', 'DISLIKE'), + go10sBack: () => win.webContents.send('ytmd:seek-by', -10), + go10sForward: () => win.webContents.send('ytmd:seek-by', 10), + go1sBack: () => win.webContents.send('ytmd:seek-by', -1), + go1sForward: () => win.webContents.send('ytmd:seek-by', 1), + shuffle: () => win.webContents.send('ytmd:shuffle'), + switchRepeat: (n = 1) => win.webContents.send('ytmd:switch-repeat', n), + // General + volumeMinus10: () => { + ipcMain.once('ytmd:get-volume-return', (_, volume) => { + win.webContents.send('ytmd:update-volume', volume - 10); + }); + win.webContents.send('ytmd:get-volume'); + }, + volumePlus10: () => { + ipcMain.once('ytmd:get-volume-return', (_, volume) => { + win.webContents.send('ytmd:update-volume', volume + 10); + }); + win.webContents.send('ytmd:get-volume'); + }, + fullscreen: () => win.webContents.send('ytmd:toggle-fullscreen'), + muteUnmute: () => win.webContents.send('ytmd:toggle-mute'), }; return { ...commands, diff --git a/src/renderer.ts b/src/renderer.ts index 761780c9..d5b86bea 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -37,8 +37,41 @@ interface YouTubeMusicAppElement extends HTMLElement { } async function onApiLoaded() { + window.ipcRenderer.on('ytmd:previous-video', () => { + document.querySelector('.previous-button.ytmusic-player-bar')?.click(); + }); + window.ipcRenderer.on('ytmd:next-video', () => { + document.querySelector('.next-button.ytmusic-player-bar')?.click(); + }); + window.ipcRenderer.on('ytmd:toggle-play', (_) => { + if (api?.getPlayerState() === 2) api?.playVideo(); + else api?.pauseVideo(); + }); window.ipcRenderer.on('ytmd:seek-to', (_, t: number) => api!.seekTo(t)); window.ipcRenderer.on('ytmd:seek-by', (_, t: number) => api!.seekBy(t)); + window.ipcRenderer.on('ytmd:shuffle', () => { + document.querySelector void } }>('ytmusic-player-bar')?.queue.shuffle(); + }); + window.ipcRenderer.on('ytmd:update-like', (_, status: 'LIKE' | 'DISLIKE' = 'LIKE') => { + document.querySelector void }>('#like-button-renderer')?.updateLikeStatus(status); + }); + window.ipcRenderer.on('ytmd:switch-repeat', (_, repeat = 1) => { + for (let i = 0; i < repeat; i++) { + document.querySelector void }>('ytmusic-player-bar')?.onRepeatButtonTap(); + } + }); + window.ipcRenderer.on('ytmd:update-volume', (_, volume: number) => { + document.querySelector void }>('ytmusic-player-bar')?.updateVolume(volume); + }); + window.ipcRenderer.on('ytmd:get-volume', (event) => { + event.sender.emit('ytmd:get-volume-return', api?.getVolume()); + }); + window.ipcRenderer.on('ytmd:toggle-fullscreen', (_) => { + document.querySelector void }>('ytmusic-player-bar')?.toggleFullscreen(); + }); + window.ipcRenderer.on('ytmd:toggle-mute', (_) => { + document.querySelector void }>('ytmusic-player-bar')?.onVolumeTap(); + }); const video = document.querySelector('video')!; const audioContext = new AudioContext();