2022-01-23 14:41:09 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
|
|
const { screen } = require("electron");
|
|
|
|
const log = require("electron-log");
|
|
|
|
const debounce = require("lodash/debounce");
|
2023-10-23 08:43:18 +02:00
|
|
|
const Store = require("electron-store");
|
|
|
|
const store = new Store();
|
2022-01-23 14:41:09 +01:00
|
|
|
|
|
|
|
// https://stackoverflow.com/a/68627253
|
|
|
|
const windowTracker = (windowName) => {
|
|
|
|
let window, windowState;
|
|
|
|
|
|
|
|
const setBounds = () => {
|
|
|
|
// Restore from appConfig
|
2023-10-23 08:43:18 +02:00
|
|
|
if (store.has(`window.${windowName}`)) {
|
|
|
|
windowState = store.get(`window.${windowName}`);
|
2022-01-23 14:41:09 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size = screen.getPrimaryDisplay().workAreaSize;
|
|
|
|
|
|
|
|
// Default
|
|
|
|
windowState = {
|
|
|
|
x: undefined,
|
|
|
|
y: undefined,
|
|
|
|
width: size.width,
|
|
|
|
height: size.height,
|
|
|
|
isMaximized: true,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const saveState = debounce(() => {
|
2022-01-30 01:58:09 +01:00
|
|
|
if (!window || window.isDestroyed()) {
|
|
|
|
log.silly(`Saving window state failed because window is not available`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:41:09 +01:00
|
|
|
if (!windowState.isMaximized) {
|
|
|
|
windowState = window.getBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
windowState.isMaximized = window.isMaximized();
|
|
|
|
log.silly(`Saving window.${windowName} to configs`);
|
2023-10-23 08:43:18 +02:00
|
|
|
store.set(`window.${windowName}`, windowState);
|
2022-01-23 14:41:09 +01:00
|
|
|
log.silly(windowState);
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
const track = (win) => {
|
|
|
|
window = win;
|
2022-01-30 01:58:09 +01:00
|
|
|
["resize", "move", "close"].forEach((event) => {
|
2022-01-23 14:41:09 +01:00
|
|
|
win.on(event, saveState);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
setBounds();
|
|
|
|
|
|
|
|
return {
|
|
|
|
state: {
|
|
|
|
x: windowState.x,
|
|
|
|
y: windowState.y,
|
|
|
|
width: windowState.width,
|
|
|
|
height: windowState.height,
|
|
|
|
isMaximized: windowState.isMaximized,
|
|
|
|
},
|
|
|
|
track,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { windowTracker };
|