mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-07 04:20:12 +02:00
Add media keys
This commit is contained in:
@@ -3,6 +3,7 @@ import MpvAPI from 'node-mpv';
|
||||
import { PlayerData } from '../../../../renderer/store';
|
||||
import { getMainWindow } from '../../../main';
|
||||
import { store } from '../settings/index';
|
||||
import './media-keys';
|
||||
|
||||
declare module 'node-mpv';
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/* eslint-disable promise/always-return */
|
||||
import { BrowserWindow, globalShortcut } from 'electron';
|
||||
|
||||
export const enableMediaKeys = (window: BrowserWindow | null) => {
|
||||
globalShortcut.register('MediaStop', () => {
|
||||
window?.webContents.send('renderer-player-stop');
|
||||
});
|
||||
|
||||
globalShortcut.register('MediaPlayPause', () => {
|
||||
window?.webContents.send('renderer-player-play-pause');
|
||||
});
|
||||
|
||||
globalShortcut.register('MediaNextTrack', () => {
|
||||
window?.webContents.send('renderer-player-next');
|
||||
});
|
||||
|
||||
globalShortcut.register('MediaPreviousTrack', () => {
|
||||
window?.webContents.send('renderer-player-previous');
|
||||
});
|
||||
};
|
||||
|
||||
export const disableMediaKeys = () => {
|
||||
globalShortcut.unregister('MediaStop');
|
||||
globalShortcut.unregister('MediaPlayPause');
|
||||
globalShortcut.unregister('MediaNextTrack');
|
||||
globalShortcut.unregister('MediaPreviousTrack');
|
||||
};
|
||||
+21
-1
@@ -9,9 +9,14 @@
|
||||
* `./src/main.js` using webpack. This gives us some performance wins.
|
||||
*/
|
||||
import path from 'path';
|
||||
import { app, BrowserWindow, shell, ipcMain } from 'electron';
|
||||
import { app, BrowserWindow, shell, ipcMain, globalShortcut } from 'electron';
|
||||
import log from 'electron-log';
|
||||
import { autoUpdater } from 'electron-updater';
|
||||
import {
|
||||
disableMediaKeys,
|
||||
enableMediaKeys,
|
||||
} from './features/core/player/media-keys';
|
||||
import { store } from './features/core/settings/index';
|
||||
import MenuBuilder from './menu';
|
||||
import { resolveHtmlPath } from './utils';
|
||||
import './features';
|
||||
@@ -105,6 +110,20 @@ const createWindow = async () => {
|
||||
app.exit(0);
|
||||
});
|
||||
|
||||
ipcMain.on('global-media-keys-enable', () => {
|
||||
enableMediaKeys(mainWindow);
|
||||
});
|
||||
|
||||
ipcMain.on('global-media-keys-disable', () => {
|
||||
disableMediaKeys();
|
||||
});
|
||||
|
||||
const globalMediaKeysEnabled = store.get('global_media_hotkeys') as boolean;
|
||||
|
||||
if (globalMediaKeysEnabled) {
|
||||
enableMediaKeys(mainWindow);
|
||||
}
|
||||
|
||||
mainWindow.loadURL(resolveHtmlPath('index.html'));
|
||||
|
||||
mainWindow.on('ready-to-show', () => {
|
||||
@@ -152,6 +171,7 @@ export const getMainWindow = () => {
|
||||
app.on('window-all-closed', () => {
|
||||
// Respect the OSX convention of having the application in memory even
|
||||
// after all windows have been closed
|
||||
globalShortcut.unregisterAll();
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,12 @@ contextBridge.exposeInMainWorld('electron', {
|
||||
PLAYER_CURRENT_TIME() {
|
||||
ipcRenderer.send('player-current-time');
|
||||
},
|
||||
PLAYER_MEDIA_KEYS_DISABLE() {
|
||||
ipcRenderer.send('global-media-keys-disable');
|
||||
},
|
||||
PLAYER_MEDIA_KEYS_ENABLE() {
|
||||
ipcRenderer.send('global-media-keys-enable');
|
||||
},
|
||||
PLAYER_MUTE() {
|
||||
ipcRenderer.send('player-mute');
|
||||
},
|
||||
@@ -55,12 +61,23 @@ contextBridge.exposeInMainWorld('electron', {
|
||||
) {
|
||||
ipcRenderer.on('renderer-player-current-time', cb);
|
||||
},
|
||||
RENDERER_PLAYER_NEXT(cb: (event: IpcRendererEvent, data: any) => void) {
|
||||
ipcRenderer.on('renderer-player-next', cb);
|
||||
},
|
||||
RENDERER_PLAYER_PAUSE(cb: (event: IpcRendererEvent, data: any) => void) {
|
||||
ipcRenderer.on('renderer-player-pause', cb);
|
||||
},
|
||||
RENDERER_PLAYER_PLAY(cb: (event: IpcRendererEvent, data: any) => void) {
|
||||
ipcRenderer.on('renderer-player-play', cb);
|
||||
},
|
||||
RENDERER_PLAYER_PLAY_PAUSE(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
) {
|
||||
ipcRenderer.on('renderer-player-play-pause', cb);
|
||||
},
|
||||
RENDERER_PLAYER_PREVIOUS(cb: (event: IpcRendererEvent, data: any) => void) {
|
||||
ipcRenderer.on('renderer-player-previous', cb);
|
||||
},
|
||||
RENDERER_PLAYER_STOP(cb: (event: IpcRendererEvent, data: any) => void) {
|
||||
ipcRenderer.on('renderer-player-stop', cb);
|
||||
},
|
||||
|
||||
Vendored
+12
@@ -5,8 +5,11 @@ declare global {
|
||||
interface Window {
|
||||
electron: {
|
||||
ipcRenderer: {
|
||||
APP_RESTART(): void;
|
||||
PLAYER_AUTO_NEXT(data: PlayerData): void;
|
||||
PLAYER_CURRENT_TIME(): void;
|
||||
PLAYER_MEDIA_KEYS_DISABLE(): void;
|
||||
PLAYER_MEDIA_KEYS_ENABLE(): void;
|
||||
PLAYER_MUTE(): void;
|
||||
PLAYER_NEXT(): void;
|
||||
PLAYER_PAUSE(): void;
|
||||
@@ -24,12 +27,21 @@ declare global {
|
||||
RENDERER_PLAYER_CURRENT_TIME(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
): void;
|
||||
RENDERER_PLAYER_NEXT(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
): void;
|
||||
RENDERER_PLAYER_PAUSE(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
): void;
|
||||
RENDERER_PLAYER_PLAY(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
): void;
|
||||
RENDERER_PLAYER_PLAY_PAUSE(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
): void;
|
||||
RENDERER_PLAYER_PREVIOUS(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
): void;
|
||||
RENDERER_PLAYER_STOP(
|
||||
cb: (event: IpcRendererEvent, data: any) => void
|
||||
): void;
|
||||
|
||||
Reference in New Issue
Block a user