Skip to content

Commit 8be903a

Browse files
committed
[Feat] Window positions are now retained on launch
1 parent 0fad608 commit 8be903a

File tree

11 files changed

+308
-70
lines changed

11 files changed

+308
-70
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ A DPS meter for Blue Protocol built with Electron, React 19, and TypeScript. Thi
7373
| Command | Description |
7474
|---------|-------------|
7575
| `npm run dev` | Start development mode with hot reload |
76-
| `npm run dev:windows` | Start development with hot reload - and utf8 output |
76+
| `npm run dev:win` | Start development with hot reload - and utf8 output |
77+
' `npm run electron:rebuild:win` | Build/Rebuild electron with the cap module |
7778
| `npm run build` | Build renderer and main process |
7879
| `npm run build:server` | Build standalone server |
7980
| `npm run build:all` | Build everything |

package-lock.json

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bpsr-meter",
3-
"version": "0.4.8",
3+
"version": "0.4.9",
44
"description": "BPSR Meter",
55
"author": "Denoder",
66
"type": "module",

src/main/constants.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,16 @@ export const keybindSettings = [
5858
"dataResetKeybind",
5959
];
6060

61-
export type WindowType = keyof typeof WINDOW_CONFIGS;
61+
export type WindowType =
62+
| "main"
63+
| "group"
64+
| "history"
65+
| "device"
66+
| "settings"
67+
| "monsters"
68+
| "update";
6269
export type WindowSize = { width: number; height: number; scale?: number };
70+
export type WindowPosition = { x: number; y: number };
6371

6472
export const globalSettings: GlobalSettings = {
6573
availableLanguages: ["en", "zh"],

src/main/index.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ import {
1414
import { electronApp, is } from "@electron-toolkit/utils";
1515
import fs from "fs";
1616
import path from "path";
17-
import { CONSTANTS, WindowType, keybindSettings } from "./constants";
17+
import {
18+
CONSTANTS,
19+
WindowType,
20+
WindowSize,
21+
keybindSettings,
22+
} from "./constants";
1823
import type { GlobalSettings } from "../types";
1924
import { SettingsManager } from "./managers/settings-manager";
2025
import { Logger } from "./logger";
@@ -245,7 +250,30 @@ class Application {
245250
},
246251
);
247252

248-
ipcMain.handle("get-saved-window-sizes", async () => {
253+
ipcMain.on(
254+
"save-window-position",
255+
async (
256+
_event: IpcMainEvent,
257+
windowType: WindowType,
258+
x: number,
259+
y: number,
260+
) => {
261+
try {
262+
await this.#windowManager.saveWindowPosition(
263+
windowType,
264+
x,
265+
y,
266+
);
267+
} catch (err) {
268+
this.#logger.error(
269+
`Error saving window position for ${windowType}`,
270+
err,
271+
);
272+
}
273+
},
274+
);
275+
276+
ipcMain.handle("get-saved-window-size", async () => {
249277
return await this.#settingsManager.getWindowSizes(
250278
this.#windowManager.lastWindowSizes,
251279
);

src/main/managers/settings-manager.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from "fs/promises";
2-
import { WindowType, WindowSize } from "../constants";
2+
import { WindowType, WindowSize, WindowPosition } from "../constants";
33
import type { GlobalSettings } from "../../types";
44

55
export class SettingsManager {
@@ -42,6 +42,29 @@ export class SettingsManager {
4242
return defaultSizes;
4343
}
4444

45+
async getWindowPositions(
46+
defaultPositions: Record<WindowType, WindowPosition>,
47+
): Promise<Record<WindowType, WindowPosition>> {
48+
try {
49+
const settings = await this.getSettings();
50+
if (settings.windowPositions) {
51+
Object.entries(settings.windowPositions).forEach(
52+
([type, pos]) => {
53+
if (type in defaultPositions) {
54+
defaultPositions[type as WindowType] =
55+
pos as WindowPosition;
56+
}
57+
},
58+
);
59+
return settings.windowPositions as Record<
60+
WindowType,
61+
WindowPosition
62+
>;
63+
}
64+
} catch {}
65+
return defaultPositions;
66+
}
67+
4568
async saveWindowSize(
4669
windowType: WindowType,
4770
size: WindowSize,
@@ -57,4 +80,20 @@ export class SettingsManager {
5780
throw new Error(`Error saving window size: ${error}`);
5881
}
5982
}
83+
84+
async saveWindowPosition(
85+
windowType: WindowType,
86+
pos: { x: number; y: number },
87+
): Promise<void> {
88+
try {
89+
const settings = await this.getSettings();
90+
settings.windowPositions = {
91+
...settings.windowPositions,
92+
[windowType]: pos,
93+
};
94+
await this.updateSettings(settings);
95+
} catch (error) {
96+
throw new Error(`Error saving window position: ${error}`);
97+
}
98+
}
6099
}

0 commit comments

Comments
 (0)