Remember window position & size

This commit is contained in:
Vendicated 2023-04-04 04:17:38 +02:00
parent f9ebc16656
commit 18a77d45b8
No known key found for this signature in database
GPG key ID: A1DC0CFB5615D905
3 changed files with 92 additions and 2 deletions

View file

@ -11,6 +11,7 @@ import { ensureVencordFiles } from "./utils/vencordLoader";
import { ICON_PATH } from "../shared/paths";
import "./ipc";
import { Settings } from "./settings";
// Make the Vencord files use our DATA_DIR
process.env.VENCORD_USER_DATA_DIR = DATA_DIR;
@ -53,6 +54,10 @@ async function createWindows() {
mainWin.once("ready-to-show", () => {
splash.destroy();
mainWin!.show();
if (Settings.maximized) {
mainWin!.maximize();
}
});
}

View file

@ -1,6 +1,7 @@
import { BrowserWindow, Menu, Tray, app, shell } from "electron";
import { BrowserWindow, BrowserWindowConstructorOptions, Menu, Tray, app, shell } from "electron";
import { join } from "path";
import { ICON_PATH } from "../shared/paths";
import { Settings } from "./settings";
let isQuitting = false;
@ -67,6 +68,52 @@ function initTray(win: BrowserWindow) {
});
}
function getWindowBoundsOptions() {
const options = {} as BrowserWindowConstructorOptions;
const { x, y, width, height } = Settings.windowBounds ?? {};
if (x != null && y != null) {
options.x = x;
options.y = y;
}
if (width) options.width = width;
if (height) options.height = height;
return options;
}
function initWindowBoundsListeners(win: BrowserWindow) {
win.on("maximize", () => {
Settings.maximized = true;
Settings.minimized = false;
});
win.on("minimize", () => {
Settings.minimized = true;
});
win.on("unmaximize", () => {
Settings.maximized = false;
Settings.minimized = false;
});
const saveBounds = () => {
const [width, height] = win.getSize();
const [x, y] = win.getPosition();
Settings.windowBounds = {
width,
height,
x,
y
};
};
win.on("resize", saveBounds);
win.on("move", saveBounds);
}
export function createMainWindow() {
const win = new BrowserWindow({
show: false,
@ -78,7 +125,8 @@ export function createMainWindow() {
devTools: true,
preload: join(__dirname, "preload.js")
},
icon: ICON_PATH
icon: ICON_PATH,
...getWindowBoundsOptions()
});
win.on("close", e => {
@ -90,6 +138,7 @@ export function createMainWindow() {
return false;
});
initWindowBoundsListeners(win);
initTray(win);
initWindowOpenHandler(win);

36
src/main/settings.ts Normal file
View file

@ -0,0 +1,36 @@
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { DATA_DIR } from "./constants";
const SETTINGS_FILE = join(DATA_DIR, "settings.json");
interface Settings {
maximized?: boolean;
minimized?: boolean;
windowBounds?: {
x: number;
y: number;
width: number;
height: number;
};
}
let settings = {} as Settings;
try {
const content = readFileSync(SETTINGS_FILE, "utf8");
try {
settings = JSON.parse(content);
} catch (err) {
console.error("Failed to parse settings.json:", err);
}
} catch { }
export const Settings = new Proxy(settings, {
set(target, prop, value) {
Reflect.set(target, prop, value);
writeFileSync(SETTINGS_FILE, JSON.stringify(target, null, 4));
return true;
}
});