mirror of
https://github.com/Vencord/Vesktop.git
synced 2025-02-23 13:45:09 +00:00
Remember window position & size
This commit is contained in:
parent
f9ebc16656
commit
18a77d45b8
3 changed files with 92 additions and 2 deletions
|
@ -11,6 +11,7 @@ import { ensureVencordFiles } from "./utils/vencordLoader";
|
||||||
|
|
||||||
import { ICON_PATH } from "../shared/paths";
|
import { ICON_PATH } from "../shared/paths";
|
||||||
import "./ipc";
|
import "./ipc";
|
||||||
|
import { Settings } from "./settings";
|
||||||
|
|
||||||
// Make the Vencord files use our DATA_DIR
|
// Make the Vencord files use our DATA_DIR
|
||||||
process.env.VENCORD_USER_DATA_DIR = DATA_DIR;
|
process.env.VENCORD_USER_DATA_DIR = DATA_DIR;
|
||||||
|
@ -53,6 +54,10 @@ async function createWindows() {
|
||||||
mainWin.once("ready-to-show", () => {
|
mainWin.once("ready-to-show", () => {
|
||||||
splash.destroy();
|
splash.destroy();
|
||||||
mainWin!.show();
|
mainWin!.show();
|
||||||
|
|
||||||
|
if (Settings.maximized) {
|
||||||
|
mainWin!.maximize();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 { join } from "path";
|
||||||
import { ICON_PATH } from "../shared/paths";
|
import { ICON_PATH } from "../shared/paths";
|
||||||
|
import { Settings } from "./settings";
|
||||||
|
|
||||||
let isQuitting = false;
|
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() {
|
export function createMainWindow() {
|
||||||
const win = new BrowserWindow({
|
const win = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
|
@ -78,7 +125,8 @@ export function createMainWindow() {
|
||||||
devTools: true,
|
devTools: true,
|
||||||
preload: join(__dirname, "preload.js")
|
preload: join(__dirname, "preload.js")
|
||||||
},
|
},
|
||||||
icon: ICON_PATH
|
icon: ICON_PATH,
|
||||||
|
...getWindowBoundsOptions()
|
||||||
});
|
});
|
||||||
|
|
||||||
win.on("close", e => {
|
win.on("close", e => {
|
||||||
|
@ -90,6 +138,7 @@ export function createMainWindow() {
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
initWindowBoundsListeners(win);
|
||||||
initTray(win);
|
initTray(win);
|
||||||
initWindowOpenHandler(win);
|
initWindowOpenHandler(win);
|
||||||
|
|
||||||
|
|
36
src/main/settings.ts
Normal file
36
src/main/settings.ts
Normal 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;
|
||||||
|
}
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue