Add window state manager

This commit is contained in:
smartfrigde 2022-06-16 17:24:37 +02:00
parent dfa1665b19
commit 86f885c41f
4 changed files with 66 additions and 7 deletions

View file

@ -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) => {
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();

View file

@ -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();
}
}

View file

@ -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 {

View file

@ -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();