From 4b813784234e77a38a9a4b2ae99225936e70620f Mon Sep 17 00:00:00 2001 From: Mastermindzh Date: Mon, 7 Aug 2023 19:48:29 +0200 Subject: [PATCH] fixed feature flag parsing & setting --- CHANGELOG.md | 1 + src/constants/flags.ts | 6 +++--- src/features/flags/flags.ts | 41 +++++++++++++++++++++++++++++++++++++ src/main.ts | 40 ++++++++++-------------------------- 4 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 src/features/flags/flags.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index d205ead..e9e8d59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [next] +- Fixed: config flags not being set correctly - [DEV]: - Logger is now static and will automatically call either ipcRenderer or ipcMain diff --git a/src/constants/flags.ts b/src/constants/flags.ts index 7968aff..96240ee 100644 --- a/src/constants/flags.ts +++ b/src/constants/flags.ts @@ -1,4 +1,4 @@ -export const flags: { [key: string]: { flag: string; value?: string }[] } = { - gpuRasterization: [{ flag: "enable-gpu-rasterization", value: undefined }], - disableHardwareMediaKeys: [{ flag: "disable-features", value: "HardwareMediaKeyHandling" }], +export const flags: { [key: string]: { flag: string; value?: string } } = { + gpuRasterization: { flag: "enable-gpu-rasterization", value: undefined }, + disableHardwareMediaKeys: { flag: "disable-features", value: "HardwareMediaKeyHandling" }, }; diff --git a/src/features/flags/flags.ts b/src/features/flags/flags.ts new file mode 100644 index 0000000..7b4614b --- /dev/null +++ b/src/features/flags/flags.ts @@ -0,0 +1,41 @@ +import { App } from "electron"; +import { flags } from "../../constants/flags"; +import { settings } from "../../constants/settings"; +import { settingsStore } from "../../scripts/settings"; +import { Logger } from "../logger"; + +/** + * Set default Electron flags + */ +export function setDefaultFlags(app: App) { + setFlag(app, "disable-seccomp-filter-sandbox"); +} + +/** + * Set Tidal's managed flags from the user settings + * @param app + */ +export function setManagedFlagsFromSettings(app: App) { + const flagsFromSettings = settingsStore.get(settings.flags.root); + if (flagsFromSettings) { + for (const [key, value] of Object.entries(flagsFromSettings)) { + if (value) { + const { flag, value } = flags[key]; + + Logger.log(`enabling command line option ${flag} with value ${value}`); + setFlag(app, flag, value); + } + } + } +} + +/** + * Set a single flag for Electron + * @param app app to set it on + * @param flag flag name + * @param value value to be set for the flag + */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function setFlag(app: App, flag: string, value?: any) { + app.commandLine.appendSwitch(flag, value); +} diff --git a/src/main.ts b/src/main.ts index 130a7a5..24ea755 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,9 +9,13 @@ import { session, } from "electron"; import path from "path"; -import { flags } from "./constants/flags"; import { globalEvents } from "./constants/globalEvents"; import { mediaKeys } from "./constants/mediaKeys"; +import { settings } from "./constants/settings"; +import { setDefaultFlags, setManagedFlagsFromSettings } from "./features/flags/flags"; +import { Logger } from "./features/logger"; +import { Songwhip } from "./features/songwhip/songwhip"; +import { MediaInfo } from "./models/mediaInfo"; import { initRPC, rpc, unRPC } from "./scripts/discord"; import { startExpress } from "./scripts/express"; import { updateMediaInfo } from "./scripts/mediaInfo"; @@ -20,14 +24,10 @@ import { closeSettingsWindow, createSettingsWindow, hideSettingsWindow, - showSettingsWindow, settingsStore, + showSettingsWindow, } from "./scripts/settings"; -import { settings } from "./constants/settings"; import { addTray, refreshTray } from "./scripts/tray"; -import { MediaInfo } from "./models/mediaInfo"; -import { Songwhip } from "./features/songwhip/songwhip"; -import { Logger } from "./features/logger"; const tidalUrl = "https://listen.tidal.com"; initialize(); @@ -36,26 +36,8 @@ let mainWindow: BrowserWindow; const icon = path.join(__dirname, "../assets/icon.png"); const PROTOCOL_PREFIX = "tidal"; -setFlags(); - -function setFlags() { - const flagsFromSettings = settingsStore.get(settings.flags.root); - if (flagsFromSettings) { - for (const [key, value] of Object.entries(flags)) { - if (value) { - flags[key].forEach((flag) => { - console.log(`enabling command line switch ${flag.flag} with value ${flag.value}`); - app.commandLine.appendSwitch(flag.flag, flag.value); - }); - } - } - } - - /** - * Fix Display Compositor issue. - */ - app.commandLine.appendSwitch("disable-seccomp-filter-sandbox"); -} +setDefaultFlags(app); +setManagedFlagsFromSettings(app); /** * Update the menuBarVisibility according to the store value @@ -90,8 +72,8 @@ function createWindow(options = { x: 0, y: 0, backgroundColor: "white" }) { mainWindow = new BrowserWindow({ x: options.x, y: options.y, - width: settingsStore && settingsStore.get(settings.windowBounds.width), - height: settingsStore && settingsStore.get(settings.windowBounds.height), + width: settingsStore?.get(settings.windowBounds.width), + height: settingsStore?.get(settings.windowBounds.height), icon, backgroundColor: options.backgroundColor, autoHideMenuBar: true, @@ -224,7 +206,7 @@ ipcMain.on(globalEvents.error, (event) => { }); ipcMain.handle(globalEvents.whip, async (event, url) => { - return await Songwhip.whip(url); + return Songwhip.whip(url); }); Logger.watch(ipcMain);