electron-store base for volume storing, fixes to window max/min handling

This commit is contained in:
Core 2021-12-16 17:17:18 +00:00
parent f193299996
commit 3fcf6f9faf
3 changed files with 55 additions and 20 deletions

View file

@ -4,6 +4,13 @@ const {app} = require('electron');
// Creating the Application Window and Calling all the Functions // Creating the Application Window and Calling all the Functions
function CreateWindow() { function CreateWindow() {
if (app.isQuiting) { app.quit(); return; } if (app.isQuiting) { app.quit(); return; }
// store
const Store = require("electron-store");
app.cfg = new Store({
defaults: {volume: 1},
});
/** CIDER **/ /** CIDER **/
const ciderwin = require("./resources/functions/cider-base") const ciderwin = require("./resources/functions/cider-base")
app.win = ciderwin app.win = ciderwin

View file

@ -275,6 +275,10 @@ const app = new Vue({
} }
} }
// Set the volume
ipcRenderer.invoke('getStoreValue', 'volume').then((value) => {
self.mk.volume = value
})
// load cached library // load cached library
if (localStorage.getItem("librarySongs") != null) { if (localStorage.getItem("librarySongs") != null) {
@ -396,6 +400,10 @@ const app = new Vue({
} }
}) })
this.mk.addEventListener(MusicKit.Events.playbackVolumeDidChange, (_a) => {
ipcRenderer.invoke('setStoreValue', 'volume', this.mk.volume)
})
this.apiCall('https://api.music.apple.com/v1/me/library/playlists', res => { this.apiCall('https://api.music.apple.com/v1/me/library/playlists', res => {
self.playlists.listing = res.data self.playlists.listing = res.data
}) })

View file

@ -1,4 +1,4 @@
const {BrowserWindow, ipcMain, shell} = require("electron") const {BrowserWindow, ipcMain, shell, app} = require("electron")
const {join} = require("path") const {join} = require("path")
const getPort = require("get-port"); const getPort = require("get-port");
const express = require("express"); const express = require("express");
@ -43,7 +43,10 @@ const CiderBase = {
} }
} }
CiderBase.InitWebServer() CiderBase.InitWebServer()
// Create the BrowserWindow
if (process.platform === "darwin" || process.platform === "linux") { if (process.platform === "darwin" || process.platform === "linux") {
win = new BrowserWindow(options) win = new BrowserWindow(options)
} else { } else {
@ -51,17 +54,6 @@ const CiderBase = {
win = new BrowserWindow(options) win = new BrowserWindow(options)
} }
win.webContents.setWindowOpenHandler(({url}) => {
if (url.includes("apple") || url.includes("localhost")) {
return { action: "allow"}
}
shell.openExternal(url).catch(() => {})
return {
action: 'deny'
}
})
// intercept "https://js-cdn.music.apple.com/hls.js/2.141.0/hls.js/hls.js" and redirect to local file "./apple-hls.js" instead // intercept "https://js-cdn.music.apple.com/hls.js/2.141.0/hls.js/hls.js" and redirect to local file "./apple-hls.js" instead
win.webContents.session.webRequest.onBeforeRequest( win.webContents.session.webRequest.onBeforeRequest(
{ {
@ -95,18 +87,31 @@ const CiderBase = {
}) })
// IPC stuff (listeners) // IPC stuff (listeners)
ipcMain.on('close', () => { // listen for close event ipcMain.on('close', () => { // listen for close event
win.close(); win.close();
}) })
ipcMain.handle('getStoreValue', (event, key, defaultValue) => {
return (defaultValue ? app.cfg.get(key, true) : app.cfg.get(key));
});
ipcMain.handle('setStoreValue', (event, key, value) => {
app.cfg.set(key, value);
});
ipcMain.on('getStore', (event) => {
event.returnValue = app.cfg.store
})
ipcMain.on('setStore', (event, store) => {
app.cfg.store = store
})
ipcMain.on('maximize', () => { // listen for maximize event ipcMain.on('maximize', () => { // listen for maximize event
if (win.maximizable) { if (win.isMaximized()) {
win.maximize(); win.unmaximize()
win.maximizable = false;
} else { } else {
win.unmaximize(); win.maximize()
win.maximizable = true;
} }
}) })
@ -115,13 +120,13 @@ const CiderBase = {
}) })
if (process.platform === "win32") { if (process.platform === "win32") {
var WND_STATE = { let WND_STATE = {
MINIMIZED: 0, MINIMIZED: 0,
NORMAL: 1, NORMAL: 1,
MAXIMIZED: 2, MAXIMIZED: 2,
FULL_SCREEN: 3 FULL_SCREEN: 3
} }
var wndState = WND_STATE.NORMAL let wndState = WND_STATE.NORMAL
win.on("resize", (_event) => { win.on("resize", (_event) => {
const isMaximized = win.isMaximized() const isMaximized = win.isMaximized()
@ -141,13 +146,27 @@ const CiderBase = {
} }
}) })
} }
// Set window Handler
win.webContents.setWindowOpenHandler(({url}) => {
if (url.includes("apple") || url.includes("localhost")) {
return { action: "allow"}
}
shell.openExternal(url).catch(() => {})
return {
action: 'deny'
}
})
return win return win
}, },
EnvironmentVariables: { EnvironmentVariables: {
"env": { "env": {
platform: os.platform() platform: os.platform()
} }
}, },
async InitWebServer() { async InitWebServer() {
const webRemotePort = await getPort({port : 9000}); const webRemotePort = await getPort({port : 9000});
const webapp = express(); const webapp = express();
@ -164,6 +183,7 @@ const CiderBase = {
console.log(`Web Remote listening on port ${webRemotePort}`); console.log(`Web Remote listening on port ${webRemotePort}`);
}); });
}, },
} }
module.exports = CiderBase; module.exports = CiderBase;