diff --git a/src/main/index.ts b/src/main/index.ts index 342cd70..c3fea0d 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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(); + } }); } diff --git a/src/main/mainWindow.ts b/src/main/mainWindow.ts index f540fac..6d02549 100644 --- a/src/main/mainWindow.ts +++ b/src/main/mainWindow.ts @@ -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); diff --git a/src/main/settings.ts b/src/main/settings.ts new file mode 100644 index 0000000..318b058 --- /dev/null +++ b/src/main/settings.ts @@ -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; + } +});