From 699ed268e6c44e6ce63d27e23cc150c80e9b13c0 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Sun, 30 Oct 2022 01:37:05 -0700 Subject: [PATCH] Set MPV path from local settings file --- src/main/features/core/index.ts | 1 + src/main/features/core/player/index.ts | 14 +++----------- src/main/features/core/settings/index.ts | 12 +++++++----- src/main/main.ts | 5 +++++ src/main/preload.ts | 5 ++++- src/renderer/features/settings/index.ts | 7 ++++++- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/main/features/core/index.ts b/src/main/features/core/index.ts index 7f8e5b630..c273d1a5b 100644 --- a/src/main/features/core/index.ts +++ b/src/main/features/core/index.ts @@ -1 +1,2 @@ import './player'; +import './settings'; diff --git a/src/main/features/core/player/index.ts b/src/main/features/core/player/index.ts index 52269a543..7eaf17059 100644 --- a/src/main/features/core/player/index.ts +++ b/src/main/features/core/player/index.ts @@ -2,25 +2,17 @@ import { ipcMain } from 'electron'; import MpvAPI from 'node-mpv'; import { PlayerData } from '../../../../renderer/store'; import { getMainWindow } from '../../../main'; +import { store } from '../settings/index'; declare module 'node-mpv'; -// const BINARY_PATH = getMainWindow() -// ?.webContents.executeJavaScript('localStorage.getItem("mpv_binary");', true) -// .then((result) => { -// return result; -// }) -// .catch((err) => { -// console.log(err); -// }); +const BINARY_PATH = store.get('mpv_path') as string; const mpv = new MpvAPI( { audio_only: true, auto_restart: true, - binary: 'C:/ProgramData/chocolatey/lib/mpv.install/tools/mpv.exe', - // binary: BINARY_PATH, - + binary: BINARY_PATH || '', time_update: 1, }, ['--gapless-audio=yes', '--prefetch-playlist'] diff --git a/src/main/features/core/settings/index.ts b/src/main/features/core/settings/index.ts index f6fda43f0..39795564b 100644 --- a/src/main/features/core/settings/index.ts +++ b/src/main/features/core/settings/index.ts @@ -1,13 +1,15 @@ import { ipcMain } from 'electron'; -import settings from 'electron-settings'; +import Store from 'electron-store'; -ipcMain.on('settings-get', async (__event, data: { property: string }) => { - return settings.getSync(data.property); +export const store = new Store(); + +ipcMain.handle('settings-get', (_event, data: { property: string }) => { + return store.get(`${data.property}`); }); ipcMain.on( 'settings-set', - async (__event, data: { property: string; value: any }) => { - return settings.setSync(data.property, data.value); + (__event, data: { property: string; value: any }) => { + store.set(`${data.property}`, data.value); } ); diff --git a/src/main/main.ts b/src/main/main.ts index d3ea9f3f8..92a22342a 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -100,6 +100,11 @@ const createWindow = async () => { mainWindow?.close(); }); + ipcMain.on('app-restart', () => { + app.relaunch(); + app.exit(0); + }); + mainWindow.loadURL(resolveHtmlPath('index.html')); mainWindow.on('ready-to-show', () => { diff --git a/src/main/preload.ts b/src/main/preload.ts index 58e00c1da..df35a11cb 100644 --- a/src/main/preload.ts +++ b/src/main/preload.ts @@ -3,6 +3,9 @@ import { PlayerData } from '../renderer/store'; contextBridge.exposeInMainWorld('electron', { ipcRenderer: { + APP_RESTART() { + ipcRenderer.send('app-restart'); + }, PLAYER_AUTO_NEXT(data: PlayerData) { ipcRenderer.send('player-auto-next', data); }, @@ -62,7 +65,7 @@ contextBridge.exposeInMainWorld('electron', { ipcRenderer.on('renderer-player-stop', cb); }, SETTINGS_GET(data: { property: string }) { - ipcRenderer.send('settings-get', data); + return ipcRenderer.invoke('settings-get', data); }, SETTINGS_SET(data: { property: string; value: any }) { ipcRenderer.send('settings-set', data); diff --git a/src/renderer/features/settings/index.ts b/src/renderer/features/settings/index.ts index 3d1c67e61..4ba111359 100644 --- a/src/renderer/features/settings/index.ts +++ b/src/renderer/features/settings/index.ts @@ -1,6 +1,6 @@ import isElectron from 'is-electron'; -export * from './hooks/useDefaultSettings'; +export * from './hooks/use-default-settings'; const ipc = isElectron() ? window.electron.ipcRenderer : null; @@ -10,7 +10,12 @@ const set = (property: string, value: any) => { ipc?.SETTINGS_SET({ property, value }); }; +const restart = () => { + ipc?.APP_RESTART(); +}; + export const settings = { get, + restart, set, };