mirror of
https://github.com/antebudimir/feishin.git
synced 2025-12-31 18:13:31 +00:00
Add files
This commit is contained in:
commit
e87c814068
266 changed files with 63938 additions and 0 deletions
2
src/main/features/core/index.ts
Normal file
2
src/main/features/core/index.ts
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
import './player';
|
||||
import './settings';
|
||||
161
src/main/features/core/player/index.ts
Normal file
161
src/main/features/core/player/index.ts
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
import { ipcMain } from 'electron';
|
||||
import uniq from 'lodash/uniq';
|
||||
import MpvAPI from 'node-mpv';
|
||||
import { store } from '/@/main/features/core/settings';
|
||||
import { getMainWindow } from '/@/main/main';
|
||||
import { PlayerData } from '/@/renderer/store';
|
||||
|
||||
declare module 'node-mpv';
|
||||
|
||||
const BINARY_PATH = store.get('mpv_path') as string | undefined;
|
||||
const MPV_PARAMETERS = store.get('mpv_parameters') as Array<string> | undefined;
|
||||
const DEFAULT_MPV_PARAMETERS = () => {
|
||||
const parameters = [];
|
||||
if (
|
||||
!MPV_PARAMETERS?.includes('--gapless-audio=weak') ||
|
||||
!MPV_PARAMETERS?.includes('--gapless-audio=no') ||
|
||||
!MPV_PARAMETERS?.includes('--gapless-audio=yes') ||
|
||||
!MPV_PARAMETERS?.includes('--gapless-audio')
|
||||
) {
|
||||
parameters.push('--gapless-audio=yes');
|
||||
}
|
||||
|
||||
if (
|
||||
!MPV_PARAMETERS?.includes('--prefetch-playlist=no') ||
|
||||
!MPV_PARAMETERS?.includes('--prefetch-playlist=yes') ||
|
||||
!MPV_PARAMETERS?.includes('--prefetch-playlist')
|
||||
) {
|
||||
parameters.push('--prefetch-playlist=yes');
|
||||
}
|
||||
|
||||
return parameters;
|
||||
};
|
||||
|
||||
const mpv = new MpvAPI(
|
||||
{
|
||||
audio_only: true,
|
||||
auto_restart: true,
|
||||
binary: BINARY_PATH || '',
|
||||
time_update: 1,
|
||||
},
|
||||
MPV_PARAMETERS
|
||||
? uniq([...DEFAULT_MPV_PARAMETERS(), ...MPV_PARAMETERS])
|
||||
: DEFAULT_MPV_PARAMETERS(),
|
||||
);
|
||||
|
||||
mpv.start().catch((error) => {
|
||||
console.log('error', error);
|
||||
});
|
||||
|
||||
mpv.on('status', (status) => {
|
||||
if (status.property === 'playlist-pos') {
|
||||
if (status.value !== 0) {
|
||||
getMainWindow()?.webContents.send('renderer-player-auto-next');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is playing
|
||||
mpv.on('resumed', () => {
|
||||
getMainWindow()?.webContents.send('renderer-player-play');
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is stopped
|
||||
mpv.on('stopped', () => {
|
||||
getMainWindow()?.webContents.send('renderer-player-stop');
|
||||
});
|
||||
|
||||
// Automatically updates the play button when the player is paused
|
||||
mpv.on('paused', () => {
|
||||
getMainWindow()?.webContents.send('renderer-player-pause');
|
||||
});
|
||||
|
||||
mpv.on('quit', () => {
|
||||
console.log('mpv quit');
|
||||
});
|
||||
|
||||
// Event output every interval set by time_update, used to update the current time
|
||||
mpv.on('timeposition', (time: number) => {
|
||||
getMainWindow()?.webContents.send('renderer-player-current-time', time);
|
||||
});
|
||||
|
||||
// Starts the player
|
||||
ipcMain.on('player-play', async () => {
|
||||
await mpv.play();
|
||||
});
|
||||
|
||||
// Pauses the player
|
||||
ipcMain.on('player-pause', async () => {
|
||||
await mpv.pause();
|
||||
});
|
||||
|
||||
// Stops the player
|
||||
ipcMain.on('player-stop', async () => {
|
||||
await mpv.stop();
|
||||
});
|
||||
|
||||
// Goes to the next track in the playlist
|
||||
ipcMain.on('player-next', async () => {
|
||||
await mpv.next();
|
||||
});
|
||||
|
||||
// Goes to the previous track in the playlist
|
||||
ipcMain.on('player-previous', async () => {
|
||||
await mpv.prev();
|
||||
});
|
||||
|
||||
// Seeks forward or backward by the given amount of seconds
|
||||
ipcMain.on('player-seek', async (_event, time: number) => {
|
||||
await mpv.seek(time);
|
||||
});
|
||||
|
||||
// Seeks to the given time in seconds
|
||||
ipcMain.on('player-seek-to', async (_event, time: number) => {
|
||||
await mpv.goToPosition(time);
|
||||
});
|
||||
|
||||
// Sets the queue in position 0 and 1 to the given data. Used when manually starting a song or using the next/prev buttons
|
||||
ipcMain.on('player-set-queue', async (_event, data: PlayerData) => {
|
||||
if (data.queue.current) {
|
||||
await mpv.load(data.queue.current.streamUrl, 'replace');
|
||||
}
|
||||
|
||||
if (data.queue.next) {
|
||||
await mpv.load(data.queue.next.streamUrl, 'append');
|
||||
}
|
||||
});
|
||||
|
||||
// Replaces the queue in position 1 to the given data
|
||||
ipcMain.on('player-set-queue-next', async (_event, data: PlayerData) => {
|
||||
const size = await mpv.getPlaylistSize();
|
||||
|
||||
if (size > 1) {
|
||||
await mpv.playlistRemove(1);
|
||||
}
|
||||
|
||||
if (data.queue.next) {
|
||||
await mpv.load(data.queue.next.streamUrl, 'append');
|
||||
}
|
||||
});
|
||||
|
||||
// Sets the next song in the queue when reaching the end of the queue
|
||||
ipcMain.on('player-auto-next', async (_event, data: PlayerData) => {
|
||||
// Always keep the current song as position 0 in the mpv queue
|
||||
// This allows us to easily set update the next song in the queue without
|
||||
// disturbing the currently playing song
|
||||
await mpv.playlistRemove(0);
|
||||
|
||||
if (data.queue.next) {
|
||||
await mpv.load(data.queue.next.streamUrl, 'append');
|
||||
}
|
||||
});
|
||||
|
||||
// Sets the volume to the given value (0-100)
|
||||
ipcMain.on('player-volume', async (_event, value: number) => {
|
||||
mpv.volume(value);
|
||||
});
|
||||
|
||||
// Toggles the mute status
|
||||
ipcMain.on('player-mute', async () => {
|
||||
mpv.mute();
|
||||
});
|
||||
27
src/main/features/core/player/media-keys.ts
Normal file
27
src/main/features/core/player/media-keys.ts
Normal file
|
|
@ -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');
|
||||
};
|
||||
12
src/main/features/core/settings/index.ts
Normal file
12
src/main/features/core/settings/index.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { ipcMain } from 'electron';
|
||||
import Store from 'electron-store';
|
||||
|
||||
export const store = new Store();
|
||||
|
||||
ipcMain.handle('settings-get', (_event, data: { property: string }) => {
|
||||
return store.get(`${data.property}`);
|
||||
});
|
||||
|
||||
ipcMain.on('settings-set', (__event, data: { property: string; value: any }) => {
|
||||
store.set(`${data.property}`, data.value);
|
||||
});
|
||||
0
src/main/features/darwin/index.ts
Normal file
0
src/main/features/darwin/index.ts
Normal file
4
src/main/features/index.ts
Normal file
4
src/main/features/index.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import './core';
|
||||
|
||||
// eslint-disable-next-line import/no-dynamic-require
|
||||
require(`./${process.platform}`);
|
||||
0
src/main/features/linux/index.ts
Normal file
0
src/main/features/linux/index.ts
Normal file
0
src/main/features/win32/index.ts
Normal file
0
src/main/features/win32/index.ts
Normal file
187
src/main/main.ts
Normal file
187
src/main/main.ts
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
/* eslint global-require: off, no-console: off, promise/always-return: off */
|
||||
|
||||
/**
|
||||
* This module executes inside of electron's main process. You can start
|
||||
* electron renderer process from here and communicate with the other processes
|
||||
* through IPC.
|
||||
*
|
||||
* When running `npm run build` or `npm run build:main`, this file is compiled to
|
||||
* `./src/main.js` using webpack. This gives us some performance wins.
|
||||
*/
|
||||
import path from 'path';
|
||||
import { app, BrowserWindow, shell, ipcMain, globalShortcut } from 'electron';
|
||||
import electronLocalShortcut from 'electron-localshortcut';
|
||||
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';
|
||||
|
||||
export default class AppUpdater {
|
||||
constructor() {
|
||||
log.transports.file.level = 'info';
|
||||
autoUpdater.logger = log;
|
||||
autoUpdater.checkForUpdatesAndNotify();
|
||||
}
|
||||
}
|
||||
|
||||
let mainWindow: BrowserWindow | null = null;
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
const sourceMapSupport = require('source-map-support');
|
||||
sourceMapSupport.install();
|
||||
}
|
||||
|
||||
const isDevelopment = process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true';
|
||||
|
||||
if (isDevelopment) {
|
||||
require('electron-debug')();
|
||||
}
|
||||
|
||||
const installExtensions = async () => {
|
||||
const installer = require('electron-devtools-installer');
|
||||
const forceDownload = !!process.env.UPGRADE_EXTENSIONS;
|
||||
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS'];
|
||||
|
||||
return installer
|
||||
.default(
|
||||
extensions.map((name) => installer[name]),
|
||||
forceDownload,
|
||||
)
|
||||
.catch(console.log);
|
||||
};
|
||||
|
||||
const createWindow = async () => {
|
||||
if (isDevelopment) {
|
||||
await installExtensions();
|
||||
}
|
||||
|
||||
const RESOURCES_PATH = app.isPackaged
|
||||
? path.join(process.resourcesPath, 'assets')
|
||||
: path.join(__dirname, '../../assets');
|
||||
|
||||
const getAssetPath = (...paths: string[]): string => {
|
||||
return path.join(RESOURCES_PATH, ...paths);
|
||||
};
|
||||
|
||||
mainWindow = new BrowserWindow({
|
||||
frame: false,
|
||||
height: 900,
|
||||
icon: getAssetPath('icon.png'),
|
||||
minHeight: 600,
|
||||
minWidth: 640,
|
||||
show: false,
|
||||
webPreferences: {
|
||||
backgroundThrottling: false,
|
||||
contextIsolation: true,
|
||||
devTools: true,
|
||||
nodeIntegration: true,
|
||||
preload: app.isPackaged
|
||||
? path.join(__dirname, 'preload.js')
|
||||
: path.join(__dirname, '../../.erb/dll/preload.js'),
|
||||
},
|
||||
width: 1440,
|
||||
});
|
||||
|
||||
electronLocalShortcut.register(mainWindow, 'Ctrl+Shift+I', () => {
|
||||
mainWindow?.webContents.openDevTools();
|
||||
});
|
||||
|
||||
ipcMain.on('window-maximize', () => {
|
||||
mainWindow?.maximize();
|
||||
});
|
||||
|
||||
ipcMain.on('window-unmaximize', () => {
|
||||
mainWindow?.unmaximize();
|
||||
});
|
||||
|
||||
ipcMain.on('window-minimize', () => {
|
||||
mainWindow?.minimize();
|
||||
});
|
||||
|
||||
ipcMain.on('window-close', () => {
|
||||
mainWindow?.close();
|
||||
});
|
||||
|
||||
ipcMain.on('app-restart', () => {
|
||||
app.relaunch();
|
||||
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', () => {
|
||||
if (!mainWindow) {
|
||||
throw new Error('"mainWindow" is not defined');
|
||||
}
|
||||
if (process.env.START_MINIMIZED) {
|
||||
mainWindow.minimize();
|
||||
} else {
|
||||
mainWindow.show();
|
||||
}
|
||||
});
|
||||
|
||||
mainWindow.on('closed', () => {
|
||||
mainWindow = null;
|
||||
});
|
||||
|
||||
const menuBuilder = new MenuBuilder(mainWindow);
|
||||
menuBuilder.buildMenu();
|
||||
|
||||
// Open urls in the user's browser
|
||||
mainWindow.webContents.setWindowOpenHandler((edata) => {
|
||||
shell.openExternal(edata.url);
|
||||
return { action: 'deny' };
|
||||
});
|
||||
|
||||
// Remove this if your app does not use auto updates
|
||||
// eslint-disable-next-line
|
||||
new AppUpdater();
|
||||
};
|
||||
|
||||
/**
|
||||
* Add event listeners...
|
||||
*/
|
||||
|
||||
app.commandLine.appendSwitch('disable-features', 'HardwareMediaKeyHandling,MediaSessionService');
|
||||
|
||||
export const getMainWindow = () => {
|
||||
return mainWindow;
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
app
|
||||
.whenReady()
|
||||
.then(() => {
|
||||
createWindow();
|
||||
app.on('activate', () => {
|
||||
// On macOS it's common to re-create a window in the app when the
|
||||
// dock icon is clicked and there are no other windows open.
|
||||
if (mainWindow === null) createWindow();
|
||||
});
|
||||
})
|
||||
.catch(console.log);
|
||||
269
src/main/menu.ts
Normal file
269
src/main/menu.ts
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
import { app, Menu, shell, BrowserWindow, MenuItemConstructorOptions } from 'electron';
|
||||
|
||||
interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
|
||||
selector?: string;
|
||||
submenu?: DarwinMenuItemConstructorOptions[] | Menu;
|
||||
}
|
||||
|
||||
export default class MenuBuilder {
|
||||
mainWindow: BrowserWindow;
|
||||
|
||||
constructor(mainWindow: BrowserWindow) {
|
||||
this.mainWindow = mainWindow;
|
||||
}
|
||||
|
||||
buildMenu(): Menu {
|
||||
if (process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true') {
|
||||
this.setupDevelopmentEnvironment();
|
||||
}
|
||||
|
||||
const template =
|
||||
process.platform === 'darwin' ? this.buildDarwinTemplate() : this.buildDefaultTemplate();
|
||||
|
||||
const menu = Menu.buildFromTemplate(template);
|
||||
Menu.setApplicationMenu(menu);
|
||||
|
||||
return menu;
|
||||
}
|
||||
|
||||
setupDevelopmentEnvironment(): void {
|
||||
this.mainWindow.webContents.on('context-menu', (_, props) => {
|
||||
const { x, y } = props;
|
||||
|
||||
Menu.buildFromTemplate([
|
||||
{
|
||||
click: () => {
|
||||
this.mainWindow.webContents.inspectElement(x, y);
|
||||
},
|
||||
label: 'Inspect element',
|
||||
},
|
||||
]).popup({ window: this.mainWindow });
|
||||
});
|
||||
}
|
||||
|
||||
buildDarwinTemplate(): MenuItemConstructorOptions[] {
|
||||
const subMenuAbout: DarwinMenuItemConstructorOptions = {
|
||||
label: 'Electron',
|
||||
submenu: [
|
||||
{
|
||||
label: 'About ElectronReact',
|
||||
selector: 'orderFrontStandardAboutPanel:',
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{ label: 'Services', submenu: [] },
|
||||
{ type: 'separator' },
|
||||
{
|
||||
accelerator: 'Command+H',
|
||||
label: 'Hide ElectronReact',
|
||||
selector: 'hide:',
|
||||
},
|
||||
{
|
||||
accelerator: 'Command+Shift+H',
|
||||
label: 'Hide Others',
|
||||
selector: 'hideOtherApplications:',
|
||||
},
|
||||
{ label: 'Show All', selector: 'unhideAllApplications:' },
|
||||
{ type: 'separator' },
|
||||
{
|
||||
accelerator: 'Command+Q',
|
||||
click: () => {
|
||||
app.quit();
|
||||
},
|
||||
label: 'Quit',
|
||||
},
|
||||
],
|
||||
};
|
||||
const subMenuEdit: DarwinMenuItemConstructorOptions = {
|
||||
label: 'Edit',
|
||||
submenu: [
|
||||
{ accelerator: 'Command+Z', label: 'Undo', selector: 'undo:' },
|
||||
{ accelerator: 'Shift+Command+Z', label: 'Redo', selector: 'redo:' },
|
||||
{ type: 'separator' },
|
||||
{ accelerator: 'Command+X', label: 'Cut', selector: 'cut:' },
|
||||
{ accelerator: 'Command+C', label: 'Copy', selector: 'copy:' },
|
||||
{ accelerator: 'Command+V', label: 'Paste', selector: 'paste:' },
|
||||
{
|
||||
accelerator: 'Command+A',
|
||||
label: 'Select All',
|
||||
selector: 'selectAll:',
|
||||
},
|
||||
],
|
||||
};
|
||||
const subMenuViewDev: MenuItemConstructorOptions = {
|
||||
label: 'View',
|
||||
submenu: [
|
||||
{
|
||||
accelerator: 'Command+R',
|
||||
click: () => {
|
||||
this.mainWindow.webContents.reload();
|
||||
},
|
||||
label: 'Reload',
|
||||
},
|
||||
{
|
||||
accelerator: 'Ctrl+Command+F',
|
||||
click: () => {
|
||||
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
|
||||
},
|
||||
label: 'Toggle Full Screen',
|
||||
},
|
||||
{
|
||||
accelerator: 'Alt+Command+I',
|
||||
click: () => {
|
||||
this.mainWindow.webContents.toggleDevTools();
|
||||
},
|
||||
label: 'Toggle Developer Tools',
|
||||
},
|
||||
],
|
||||
};
|
||||
const subMenuViewProd: MenuItemConstructorOptions = {
|
||||
label: 'View',
|
||||
submenu: [
|
||||
{
|
||||
accelerator: 'Ctrl+Command+F',
|
||||
click: () => {
|
||||
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
|
||||
},
|
||||
label: 'Toggle Full Screen',
|
||||
},
|
||||
],
|
||||
};
|
||||
const subMenuWindow: DarwinMenuItemConstructorOptions = {
|
||||
label: 'Window',
|
||||
submenu: [
|
||||
{
|
||||
accelerator: 'Command+M',
|
||||
label: 'Minimize',
|
||||
selector: 'performMiniaturize:',
|
||||
},
|
||||
{ accelerator: 'Command+W', label: 'Close', selector: 'performClose:' },
|
||||
{ type: 'separator' },
|
||||
{ label: 'Bring All to Front', selector: 'arrangeInFront:' },
|
||||
],
|
||||
};
|
||||
const subMenuHelp: MenuItemConstructorOptions = {
|
||||
label: 'Help',
|
||||
submenu: [
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://electronjs.org');
|
||||
},
|
||||
label: 'Learn More',
|
||||
},
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://github.com/electron/electron/tree/main/docs#readme');
|
||||
},
|
||||
label: 'Documentation',
|
||||
},
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://www.electronjs.org/community');
|
||||
},
|
||||
label: 'Community Discussions',
|
||||
},
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://github.com/electron/electron/issues');
|
||||
},
|
||||
label: 'Search Issues',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const subMenuView =
|
||||
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true'
|
||||
? subMenuViewDev
|
||||
: subMenuViewProd;
|
||||
|
||||
return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow, subMenuHelp];
|
||||
}
|
||||
|
||||
buildDefaultTemplate() {
|
||||
const templateDefault = [
|
||||
{
|
||||
label: '&File',
|
||||
submenu: [
|
||||
{
|
||||
accelerator: 'Ctrl+O',
|
||||
label: '&Open',
|
||||
},
|
||||
{
|
||||
accelerator: 'Ctrl+W',
|
||||
click: () => {
|
||||
this.mainWindow.close();
|
||||
},
|
||||
label: '&Close',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
label: '&View',
|
||||
submenu:
|
||||
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true'
|
||||
? [
|
||||
{
|
||||
accelerator: 'Ctrl+R',
|
||||
click: () => {
|
||||
this.mainWindow.webContents.reload();
|
||||
},
|
||||
label: '&Reload',
|
||||
},
|
||||
{
|
||||
accelerator: 'F11',
|
||||
click: () => {
|
||||
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
|
||||
},
|
||||
label: 'Toggle &Full Screen',
|
||||
},
|
||||
{
|
||||
accelerator: 'Alt+Ctrl+I',
|
||||
click: () => {
|
||||
this.mainWindow.webContents.toggleDevTools();
|
||||
},
|
||||
label: 'Toggle &Developer Tools',
|
||||
},
|
||||
]
|
||||
: [
|
||||
{
|
||||
accelerator: 'F11',
|
||||
click: () => {
|
||||
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
|
||||
},
|
||||
label: 'Toggle &Full Screen',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
label: 'Help',
|
||||
submenu: [
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://electronjs.org');
|
||||
},
|
||||
label: 'Learn More',
|
||||
},
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://github.com/electron/electron/tree/main/docs#readme');
|
||||
},
|
||||
label: 'Documentation',
|
||||
},
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://www.electronjs.org/community');
|
||||
},
|
||||
label: 'Community Discussions',
|
||||
},
|
||||
{
|
||||
click() {
|
||||
shell.openExternal('https://github.com/electron/electron/issues');
|
||||
},
|
||||
label: 'Search Issues',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
return templateDefault;
|
||||
}
|
||||
}
|
||||
109
src/main/preload.ts
Normal file
109
src/main/preload.ts
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
|
||||
import { PlayerData } from '../renderer/store';
|
||||
import { browser } from './preload/browser';
|
||||
import { ipc } from './preload/ipc';
|
||||
import { localSettings } from './preload/local-settings';
|
||||
import { mpvPlayer, mpvPlayerListener } from './preload/mpv-player';
|
||||
|
||||
contextBridge.exposeInMainWorld('electron', {
|
||||
browser,
|
||||
ipc,
|
||||
ipcRenderer: {
|
||||
APP_RESTART() {
|
||||
ipcRenderer.send('app-restart');
|
||||
},
|
||||
PLAYER_AUTO_NEXT(data: PlayerData) {
|
||||
ipcRenderer.send('player-auto-next', data);
|
||||
},
|
||||
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');
|
||||
},
|
||||
PLAYER_NEXT() {
|
||||
ipcRenderer.send('player-next');
|
||||
},
|
||||
PLAYER_PAUSE() {
|
||||
ipcRenderer.send('player-pause');
|
||||
},
|
||||
PLAYER_PLAY() {
|
||||
ipcRenderer.send('player-play');
|
||||
},
|
||||
PLAYER_PREVIOUS() {
|
||||
ipcRenderer.send('player-previous');
|
||||
},
|
||||
PLAYER_SEEK(seconds: number) {
|
||||
ipcRenderer.send('player-seek', seconds);
|
||||
},
|
||||
PLAYER_SEEK_TO(seconds: number) {
|
||||
ipcRenderer.send('player-seek-to', seconds);
|
||||
},
|
||||
PLAYER_SET_QUEUE(data: PlayerData) {
|
||||
ipcRenderer.send('player-set-queue', data);
|
||||
},
|
||||
PLAYER_SET_QUEUE_NEXT(data: PlayerData) {
|
||||
ipcRenderer.send('player-set-queue-next', data);
|
||||
},
|
||||
PLAYER_STOP() {
|
||||
ipcRenderer.send('player-stop');
|
||||
},
|
||||
PLAYER_VOLUME(value: number) {
|
||||
ipcRenderer.send('player-volume', value);
|
||||
},
|
||||
RENDERER_PLAYER_AUTO_NEXT(cb: (event: IpcRendererEvent, data: any) => void) {
|
||||
ipcRenderer.on('renderer-player-auto-next', cb);
|
||||
},
|
||||
RENDERER_PLAYER_CURRENT_TIME(cb: (event: IpcRendererEvent, data: any) => void) {
|
||||
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);
|
||||
},
|
||||
SETTINGS_GET(data: { property: string }) {
|
||||
return ipcRenderer.invoke('settings-get', data);
|
||||
},
|
||||
SETTINGS_SET(data: { property: string; value: any }) {
|
||||
ipcRenderer.send('settings-set', data);
|
||||
},
|
||||
removeAllListeners(value: string) {
|
||||
ipcRenderer.removeAllListeners(value);
|
||||
},
|
||||
windowClose() {
|
||||
ipcRenderer.send('window-close');
|
||||
},
|
||||
windowMaximize() {
|
||||
ipcRenderer.send('window-maximize');
|
||||
},
|
||||
windowMinimize() {
|
||||
ipcRenderer.send('window-minimize');
|
||||
},
|
||||
windowUnmaximize() {
|
||||
ipcRenderer.send('window-unmaximize');
|
||||
},
|
||||
},
|
||||
localSettings,
|
||||
mpvPlayer,
|
||||
mpvPlayerListener,
|
||||
});
|
||||
21
src/main/preload/browser.ts
Normal file
21
src/main/preload/browser.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import { ipcRenderer } from 'electron';
|
||||
|
||||
const exit = () => {
|
||||
ipcRenderer.send('window-close');
|
||||
};
|
||||
const maximize = () => {
|
||||
ipcRenderer.send('window-maximize');
|
||||
};
|
||||
const minimize = () => {
|
||||
ipcRenderer.send('window-minimize');
|
||||
};
|
||||
const unmaximize = () => {
|
||||
ipcRenderer.send('window-unmaximize');
|
||||
};
|
||||
|
||||
export const browser = {
|
||||
exit,
|
||||
maximize,
|
||||
minimize,
|
||||
unmaximize,
|
||||
};
|
||||
9
src/main/preload/ipc.ts
Normal file
9
src/main/preload/ipc.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { ipcRenderer } from 'electron';
|
||||
|
||||
const removeAllListeners = (channel: string) => {
|
||||
ipcRenderer.removeAllListeners(channel);
|
||||
};
|
||||
|
||||
export const ipc = {
|
||||
removeAllListeners,
|
||||
};
|
||||
33
src/main/preload/local-settings.ts
Normal file
33
src/main/preload/local-settings.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { app, ipcRenderer } from 'electron';
|
||||
import Store from 'electron-store';
|
||||
|
||||
const store = new Store();
|
||||
|
||||
const set = (property: string, value: string | Record<string, unknown> | boolean | string[]) => {
|
||||
store.set(`${property}`, value);
|
||||
};
|
||||
|
||||
const get = (property: string) => {
|
||||
return store.get(`${property}`);
|
||||
};
|
||||
|
||||
const restart = () => {
|
||||
app.relaunch();
|
||||
app.exit(0);
|
||||
};
|
||||
|
||||
const enableMediaKeys = () => {
|
||||
ipcRenderer.send('global-media-keys-enable');
|
||||
};
|
||||
|
||||
const disableMediaKeys = () => {
|
||||
ipcRenderer.send('global-media-keys-disable');
|
||||
};
|
||||
|
||||
export const localSettings = {
|
||||
disableMediaKeys,
|
||||
enableMediaKeys,
|
||||
get,
|
||||
restart,
|
||||
set,
|
||||
};
|
||||
102
src/main/preload/mpv-player.ts
Normal file
102
src/main/preload/mpv-player.ts
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import { ipcRenderer, IpcRendererEvent } from 'electron';
|
||||
import { PlayerData } from '/@/renderer/store';
|
||||
|
||||
const autoNext = (data: PlayerData) => {
|
||||
ipcRenderer.send('player-auto-next', data);
|
||||
};
|
||||
|
||||
const currentTime = () => {
|
||||
ipcRenderer.send('player-current-time');
|
||||
};
|
||||
const mute = () => {
|
||||
ipcRenderer.send('player-mute');
|
||||
};
|
||||
const next = () => {
|
||||
ipcRenderer.send('player-next');
|
||||
};
|
||||
const pause = () => {
|
||||
ipcRenderer.send('player-pause');
|
||||
};
|
||||
const play = () => {
|
||||
ipcRenderer.send('player-play');
|
||||
};
|
||||
const previous = () => {
|
||||
ipcRenderer.send('player-previous');
|
||||
};
|
||||
const seek = (seconds: number) => {
|
||||
ipcRenderer.send('player-seek', seconds);
|
||||
};
|
||||
const seekTo = (seconds: number) => {
|
||||
ipcRenderer.send('player-seek-to', seconds);
|
||||
};
|
||||
const setQueue = (data: PlayerData) => {
|
||||
ipcRenderer.send('player-set-queue', data);
|
||||
};
|
||||
const setQueueNext = (data: PlayerData) => {
|
||||
ipcRenderer.send('player-set-queue-next', data);
|
||||
};
|
||||
const stop = () => {
|
||||
ipcRenderer.send('player-stop');
|
||||
};
|
||||
const volume = (value: number) => {
|
||||
ipcRenderer.send('player-volume', value);
|
||||
};
|
||||
|
||||
const rendererAutoNext = (cb: (event: IpcRendererEvent, data: PlayerData) => void) => {
|
||||
ipcRenderer.on('renderer-player-auto-next', cb);
|
||||
};
|
||||
|
||||
const rendererCurrentTime = (cb: (event: IpcRendererEvent, data: number) => void) => {
|
||||
ipcRenderer.on('renderer-player-current-time', cb);
|
||||
};
|
||||
|
||||
const rendererNext = (cb: (event: IpcRendererEvent, data: PlayerData) => void) => {
|
||||
ipcRenderer.on('renderer-player-next', cb);
|
||||
};
|
||||
|
||||
const rendererPause = (cb: (event: IpcRendererEvent, data: PlayerData) => void) => {
|
||||
ipcRenderer.on('renderer-player-pause', cb);
|
||||
};
|
||||
|
||||
const rendererPlay = (cb: (event: IpcRendererEvent, data: PlayerData) => void) => {
|
||||
ipcRenderer.on('renderer-player-play', cb);
|
||||
};
|
||||
|
||||
const rendererPlayPause = (cb: (event: IpcRendererEvent, data: PlayerData) => void) => {
|
||||
ipcRenderer.on('renderer-player-play-pause', cb);
|
||||
};
|
||||
|
||||
const rendererPrevious = (cb: (event: IpcRendererEvent, data: PlayerData) => void) => {
|
||||
ipcRenderer.on('renderer-player-previous', cb);
|
||||
};
|
||||
|
||||
const rendererStop = (cb: (event: IpcRendererEvent, data: PlayerData) => void) => {
|
||||
ipcRenderer.on('renderer-player-stop', cb);
|
||||
};
|
||||
|
||||
export const mpvPlayer = {
|
||||
autoNext,
|
||||
currentTime,
|
||||
mute,
|
||||
next,
|
||||
pause,
|
||||
play,
|
||||
previous,
|
||||
seek,
|
||||
seekTo,
|
||||
setQueue,
|
||||
setQueueNext,
|
||||
stop,
|
||||
volume,
|
||||
};
|
||||
|
||||
export const mpvPlayerListener = {
|
||||
rendererAutoNext,
|
||||
rendererCurrentTime,
|
||||
rendererNext,
|
||||
rendererPause,
|
||||
rendererPlay,
|
||||
rendererPlayPause,
|
||||
rendererPrevious,
|
||||
rendererStop,
|
||||
};
|
||||
31
src/main/utils.ts
Normal file
31
src/main/utils.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* eslint import/prefer-default-export: off, import/no-mutable-exports: off */
|
||||
import path from 'path';
|
||||
import process from 'process';
|
||||
import { URL } from 'url';
|
||||
|
||||
export let resolveHtmlPath: (htmlFileName: string) => string;
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
const port = process.env.PORT || 4343;
|
||||
resolveHtmlPath = (htmlFileName: string) => {
|
||||
const url = new URL(`http://localhost:${port}`);
|
||||
url.pathname = htmlFileName;
|
||||
return url.href;
|
||||
};
|
||||
} else {
|
||||
resolveHtmlPath = (htmlFileName: string) => {
|
||||
return `file://${path.resolve(__dirname, '../renderer/', htmlFileName)}`;
|
||||
};
|
||||
}
|
||||
|
||||
export const isMacOS = () => {
|
||||
return process.platform === 'darwin';
|
||||
};
|
||||
|
||||
export const isWindows = () => {
|
||||
return process.platform === 'win32';
|
||||
};
|
||||
|
||||
export const isLinux = () => {
|
||||
return process.platform === 'linux';
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue