From a7b5b365902f1e1280886c84e0b584da01cf34d5 Mon Sep 17 00:00:00 2001 From: Core Date: Fri, 7 Jan 2022 23:53:59 +0000 Subject: [PATCH] Bread has been achieved. - Window now creates and loads properly (Due to config not being created halted vue) - Foundation for app.ts created for functions to run in certain stages of the app (start/init, ready, exit) - Electron-Store created in class ConfigStore. Store instance stored in ConfigStore.store. Gets a bit compilicated and might change the class or variable name later on. --- src/main/base/app.ts | 48 ++++++++++++++++ src/main/base/store.ts | 123 +++++++++++++++++++++++++++++++++++++++++ src/main/base/win.ts | 28 +++------- src/main/index.ts | 32 ++++++----- 4 files changed, 196 insertions(+), 35 deletions(-) create mode 100644 src/main/base/app.ts create mode 100644 src/main/base/store.ts diff --git a/src/main/base/app.ts b/src/main/base/app.ts new file mode 100644 index 00000000..bed8b814 --- /dev/null +++ b/src/main/base/app.ts @@ -0,0 +1,48 @@ +import * as electron from 'electron'; + +export class App { + constructor() { + console.log('App started'); + } + + /** + * Handles all actions that occur for the app on start (Mainly commandline arguments) + * @returns {void} + */ + public start(store: any): void { + console.log('App started'); + + switch (store.get("visual.hw_acceleration")) { + default: + case "default": + electron.app.commandLine.appendSwitch('enable-accelerated-mjpeg-decode') + electron.app.commandLine.appendSwitch('enable-accelerated-video') + electron.app.commandLine.appendSwitch('disable-gpu-driver-bug-workarounds') + electron.app.commandLine.appendSwitch('ignore-gpu-blacklist') + electron.app.commandLine.appendSwitch('enable-native-gpu-memory-buffers') + electron.app.commandLine.appendSwitch('enable-accelerated-video-decode'); + electron.app.commandLine.appendSwitch('enable-gpu-rasterization'); + electron.app.commandLine.appendSwitch('enable-native-gpu-memory-buffers'); + electron.app.commandLine.appendSwitch('enable-oop-rasterization'); + break; + + case "webgpu": + console.info("WebGPU is enabled."); + electron.app.commandLine.appendSwitch('enable-unsafe-webgpu') + break; + + case "disabled": + console.info("Hardware acceleration is disabled."); + electron.app.commandLine.appendSwitch('disable-gpu') + break; + } + } + + public stop() { + console.log('App stopped'); + } + + public ready() { + console.log('App ready'); + } +} \ No newline at end of file diff --git a/src/main/base/store.ts b/src/main/base/store.ts new file mode 100644 index 00000000..1a6c887a --- /dev/null +++ b/src/main/base/store.ts @@ -0,0 +1,123 @@ +import * as Store from 'electron-store'; +import * as electron from "electron"; + +export class ConfigStore { + public store: Store | undefined; + + private defaults: any = { + "general": { + "close_behavior": 0, // 0 = close, 1 = minimize, 2 = minimize to tray + "startup_behavior": 0, // 0 = nothing, 1 = open on startup + "discord_rpc": 1, // 0 = disabled, 1 = enabled as Cider, 2 = enabled as Apple Music + "discordClearActivityOnPause": 1, // 0 = disabled, 1 = enabled + "volume": 1 + }, + "home": { + "followedArtists": [], + "favoriteItems": [] + }, + "audio": { + "quality": "990", + "seamless_audio": true, + "normalization": false, + "spatial": false, + "spatial_properties": { + "presets": [], + "gain": 0.8, + "listener_position": [0, 0, 0], + "audio_position": [0, 0, 0], + "room_dimensions": { + "width": 32, + "height": 12, + "depth": 32 + }, + "room_materials": { + "left": 'metal', + "right": 'metal', + "front": 'brick-bare', + "back": 'brick-bare', + "down": 'acoustic-ceiling-tiles', + "up": 'acoustic-ceiling-tiles', + } + } + }, + "visual": { + "theme": "", + "scrollbars": 0, // 0 = show on hover, 2 = always hide, 3 = always show + "refresh_rate": 0, + "animated_artwork": "limited", // 0 = always, 1 = limited, 2 = never + "animated_artwork_qualityLevel": 1, + "bg_artwork_rotation": false, + "hw_acceleration": "default", // default, webgpu, disabled + "window_transparency": "disabled" + }, + "lyrics": { + "enable_mxm": false, + "mxm_karaoke": false, + "mxm_language": "en", + "enable_yt": false, + }, + "lastfm": { + "enabled": false, + "scrobble_after": 30, + "auth_token": "", + "enabledRemoveFeaturingArtists": true, + "NowPlaying": "true" + }, + "advanced": { + "AudioContext": false, + } + } + private migrations: any = {} + + constructor() { + this.store = new Store({ + name: 'cider-config', + defaults: this.defaults, + migrations: this.migrations, + }); + + this.store.set(this.mergeStore(this.defaults, this.store.store)) + this.ipcHandler(this.store); + } + + /** + * Merge Configurations + * @param target The target configuration + * @param source The source configuration + */ + private mergeStore = (target: { [x: string]: any; }, source: { [x: string]: any; }) => { + // Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties + for (const key of Object.keys(source)) { + if (key.includes('migrations')) { + continue; + } + if (source[key] instanceof Object) Object.assign(source[key], this.mergeStore(target[key], source[key])) + } + // Join `target` and modified `source` + Object.assign(target || {}, source) + return target + } + + /** + * IPC Handler + */ + private ipcHandler(cfg: Store | any): void { + electron.ipcMain.handle('getStoreValue', (event, key, defaultValue) => { + return (defaultValue ? cfg.get(key, true) : cfg.get(key)); + }); + + electron.ipcMain.handle('setStoreValue', (event, key, value) => { + cfg.set(key, value); + }); + + electron.ipcMain.on('getStore', (event) => { + event.returnValue = cfg.store + }) + + electron.ipcMain.on('setStore', (event, store) => { + cfg.store = store + }) + } + +} \ No newline at end of file diff --git a/src/main/base/win.ts b/src/main/base/win.ts index de07aaff..4e19a37d 100644 --- a/src/main/base/win.ts +++ b/src/main/base/win.ts @@ -10,9 +10,15 @@ import {Stream} from "stream"; export class Win { win: any | undefined = null; - app: electron.App | undefined; + app: any | undefined = null; + store: any | undefined = null; devMode: boolean = !electron.app.isPackaged; + constructor(app: electron.App, store: any) { + this.app = app; + this.store = store; + } + private paths: any = { srcPath: path.join(__dirname, "../../src"), resourcePath: path.join(__dirname, "../../resources"), @@ -124,7 +130,7 @@ export class Win { private startWebServer(): void { const app = express(); - // TODO: app.use(express.static(path.join(this.paths.srcPath, './renderer/'))); + app.use(express.static(path.join(this.paths.srcPath, './renderer/'))); app.set("views", path.join(this.paths.srcPath, './renderer/views')); app.set("view engine", "ejs"); @@ -277,24 +283,6 @@ export class Win { return await yt.search(u) }) - electron.ipcMain.handle('getStoreValue', (event, key, defaultValue) => { - // return (defaultValue ? app.cfg.get(key, true) : app.cfg.get(key)); - return null - }); - - electron.ipcMain.handle('setStoreValue', (event, key, value) => { - // app.cfg.set(key, value); - }); - - electron.ipcMain.on('getStore', (event) => { - // event.returnValue = app.cfg.store - event.returnValue = null - }) - - electron.ipcMain.on('setStore', (event, store) => { - // app.cfg.store = store - }) - electron.ipcMain.handle('setVibrancy', (event, key, value) => { this.win.setVibrancy(value) }); diff --git a/src/main/index.ts b/src/main/index.ts index 96e4f94d..6940004c 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -12,28 +12,30 @@ import { app } from 'electron'; // import {Win} from "./base/win"; +import {ConfigStore} from "./base/store"; -const Cider = new Win() +const config = new ConfigStore(); +console.log(config) -app.on("ready", () => { - Cider.createWindow(); -}); +const Cider = new Win(app, config.store) /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * App Event Handlers * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ -// app.on('ready', () => { -// if (app.isQuiting) { app.quit(); return; } -// -// console.log('[Cider] Application is Ready. Creating Window.') -// if (!app.isPackaged) { -// console.info('[Cider] Running in development mode.') -// require('vue-devtools').install() -// } -// -// // CiderBase.Start() -// }); +app.on('ready', () => { + + + console.log('[Cider] Application is Ready. Creating Window.') + if (!app.isPackaged) { + console.info('[Cider] Running in development mode.') + require('vue-devtools').install() + } + + Cider.createWindow(); + + // CiderBase.Start() +}); // // app.on('before-quit', () => { // app.isQuiting = true;