From 86f885c41fafe111494cc2ee93b9a7263c9322b5 Mon Sep 17 00:00:00 2001 From: smartfrigde <37928912+smartfrigde@users.noreply.github.com> Date: Thu, 16 Jun 2022 17:24:37 +0200 Subject: [PATCH] Add window state manager --- src/ipc.ts | 20 +++++++++++++++++--- src/tray.ts | 14 +++++++++++++- src/utils.ts | 31 +++++++++++++++++++++++++++++-- src/window.ts | 8 +++++++- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/ipc.ts b/src/ipc.ts index 5ad4e44..6d5af02 100644 --- a/src/ipc.ts +++ b/src/ipc.ts @@ -1,7 +1,7 @@ //ipc stuff import {app, ipcMain, shell, desktopCapturer} from "electron"; import {mainWindow} from "./window"; -import {setConfigBulk, getVersion, getConfig, setLang, getLang} from "./utils"; +import {setConfigBulk, getVersion, getConfig, setLang, getLang, getWindowState} from "./utils"; import {customTitlebar} from "./main"; import {createSettingsWindow} from "./settings/main"; export function registerIpc() { @@ -41,8 +41,22 @@ export function registerIpc() { ipcMain.on("get-app-version", (event) => { event.returnValue = getVersion(); }); - ipcMain.on("splashEnd", (event, arg) => { - mainWindow.setSize(800, 600); + ipcMain.on("splashEnd", async (event, arg) => { + try { + var width = await getWindowState("width") ?? 800; + var height= await getWindowState("height") ?? 600; + var isMaximized = await getWindowState("isMaximized") ?? false; + } catch (e) { + console.log("No window state file found. Fallbacking to default values.") + mainWindow.setSize(800, 600); + } + if (isMaximized) { + mainWindow.setSize(800, 600); //just so the whole thing doesn't cover whole screen + mainWindow.maximize() + } else { + mainWindow.setSize(width, height); + console.log("Not maximized.") + } }); ipcMain.on("restart", (event, arg) => { app.relaunch(); diff --git a/src/tray.ts b/src/tray.ts index d690914..2e22278 100644 --- a/src/tray.ts +++ b/src/tray.ts @@ -1,6 +1,6 @@ import {app, Menu, Tray} from "electron"; import {mainWindow} from "./window"; -import {getConfig} from "./utils"; +import {getConfig, setWindowState} from "./utils"; import * as path from "path"; import {createSettingsWindow} from "./settings/main"; import {platform} from "process"; @@ -22,6 +22,12 @@ app.whenReady().then(async () => { { label: "Quit ArmCord", click: function () { + let [width, height] = mainWindow.getSize() + setWindowState({ + width: width, + height: height, + isMaximized: mainWindow.isMaximized() + }) app.quit(); } } @@ -64,6 +70,12 @@ app.whenReady().then(async () => { { label: "Quit ArmCord", click: function () { + let [width, height] = mainWindow.getSize() + setWindowState({ + width: width, + height: height, + isMaximized: mainWindow.isMaximized() + }) app.quit(); } } diff --git a/src/utils.ts b/src/utils.ts index c3b5bf2..213d44b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import * as fs from "fs"; -import {app, dialog} from "electron"; +import { app, dialog } from "electron"; import path from "path"; -import {defaultMaxListeners} from "events"; +import { defaultMaxListeners } from "events"; export var firstRun: boolean; export var isSetup: boolean; export var contentPath: string; @@ -137,6 +137,33 @@ export async function getLang(object: string) { let parsed = JSON.parse(rawdata); return parsed[object]; } + +//ArmCord Window State manager +export interface WindowState { + width: number; + height: number; + isMaximized: boolean; +} +export async function setWindowState(object: WindowState) { + const userDataPath = app.getPath("userData"); + const storagePath = path.join(userDataPath, "/storage/"); + const saveFile = storagePath + "window.json"; + if (!fs.existsSync(saveFile)) { + fs.writeFileSync(saveFile, "{}", "utf-8"); + } + let toSave = JSON.stringify(object); + fs.writeFileSync(saveFile, toSave, "utf-8"); +} +export async function getWindowState(object: string) { + const userDataPath = app.getPath("userData"); + const storagePath = path.join(userDataPath, "/storage/"); + const settingsFile = storagePath + "window.json"; + let rawdata = fs.readFileSync(settingsFile, "utf-8"); + let returndata = JSON.parse(rawdata); + console.log(object + ": " + returndata[object]); + return returndata[object]; + +} //ArmCord Settings/Storage manager export interface Settings { diff --git a/src/window.ts b/src/window.ts index d523314..ce77197 100644 --- a/src/window.ts +++ b/src/window.ts @@ -4,7 +4,7 @@ // I'm sorry for this mess but I'm not sure how to fix it. import {BrowserWindow, shell, app, dialog} from "electron"; import path from "path"; -import {checkIfConfigIsBroken, firstRun, getConfig, contentPath, isSetup, setConfig, setLang} from "./utils"; +import {checkIfConfigIsBroken, firstRun, getConfig, contentPath, isSetup, setConfig, setLang, setWindowState} from "./utils"; import {registerIpc} from "./ipc"; import startServer from "./socket"; import contextMenu from "electron-context-menu"; @@ -72,6 +72,12 @@ async function doAfterDefiningTheWindow() { return callback({}); }); mainWindow.on("close", async (e) => { + let [width, height] = mainWindow.getSize() + setWindowState({ + width: width, + height: height, + isMaximized: mainWindow.isMaximized() + }) if (await getConfig("minimizeToTray")) { e.preventDefault(); mainWindow.hide();