Skip to content

Commit c6eda98

Browse files
committed
fix: window position
1 parent 6604f1f commit c6eda98

File tree

6 files changed

+71
-55
lines changed

6 files changed

+71
-55
lines changed

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"clsx": "^2.1.0",
3030
"electron-log": "5.1.1",
3131
"electron-store": "^8.1.0",
32-
"electron-window-state": "^5.0.3",
3332
"get-installed-apps": "^1.1.0",
3433
"lodash": "^4.17.21",
3534
"react": "^18.2.0",

src/main/app.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { app, BrowserWindow, nativeTheme, shell } from 'electron';
22
import path from 'path';
33

44
import log from 'electron-log';
5-
import electronWindowState from 'electron-window-state';
65
import { updateElectronApp } from 'update-electron-app';
76

87
import './ipcs';
98
import { updateEditorsAndShells } from './libs/integrations/integrations';
9+
import { loadWindowState, saveBounds } from './libs/window';
1010

1111
log.initialize({ preload: true, spyRendererConsole: false });
1212

@@ -24,30 +24,19 @@ if (isDev) {
2424
}
2525

2626
const createWindow = (): void => {
27-
const mainWindowState = electronWindowState({
28-
defaultHeight: 600,
29-
defaultWidth: isDev ? 1426 : 800
30-
});
31-
3227
const mainWindow = new BrowserWindow({
3328
backgroundColor: nativeTheme.shouldUseDarkColors ? '#141414' : '#ffffff',
34-
35-
height: mainWindowState.height,
3629
minHeight: 600,
3730
minWidth: 800,
38-
show: isDev ? false : true,
31+
show: false,
3932
titleBarStyle: 'hidden',
4033
trafficLightPosition: { x: 15, y: 17 },
4134
webPreferences: {
4235
preload: path.join(__dirname, 'preload.js')
4336
},
44-
width: mainWindowState.width,
45-
x: mainWindowState.x,
46-
y: mainWindowState.y
37+
...loadWindowState()
4738
});
4839

49-
mainWindowState.manage(mainWindow);
50-
5140
// all external links should open in default browser
5241
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
5342
shell.openExternal(url);
@@ -63,12 +52,22 @@ const createWindow = (): void => {
6352

6453
updateEditorsAndShells(mainWindow);
6554

66-
if (isDev) {
55+
mainWindow.on('close', () => {
56+
if (!isDev && mainWindow.webContents.isDevToolsOpened()) return;
57+
saveBounds(mainWindow);
58+
});
59+
60+
mainWindow.once('ready-to-show', () => {
61+
if (!isDev) {
62+
mainWindow.show();
63+
return;
64+
}
65+
6766
setTimeout(() => {
6867
mainWindow.showInactive();
6968
mainWindow.webContents.openDevTools();
7069
}, 500);
71-
}
70+
});
7271

7372
// CSP interceptor
7473
// mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
@@ -102,7 +101,7 @@ app.on('activate', () => {
102101
// for applications and their menu bar to stay active until the user quits
103102
// explicitly with Cmd + Q.
104103
app.on('window-all-closed', () => {
105-
// if (process.platform !== 'darwin') {
106-
app.quit();
107-
// }
104+
if (process.platform !== 'darwin') {
105+
app.quit();
106+
}
108107
});

src/main/libs/window.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { screen, type Rectangle, type BrowserWindow } from 'electron';
2+
3+
import { settings } from '../../main/settings';
4+
5+
const getArea = (bounds: Rectangle) => {
6+
return screen.getDisplayMatching(bounds).workArea;
7+
};
8+
9+
const isSizeValid = (bounds: Rectangle) => {
10+
const area = getArea(bounds);
11+
return bounds.width <= area.width && bounds.height <= area.height;
12+
};
13+
14+
const isPositionValid = (bounds: Rectangle) => {
15+
const area = getArea(bounds);
16+
17+
return (
18+
bounds.x >= area.x &&
19+
bounds.y >= area.y &&
20+
bounds.x + bounds.width <= area.x + area.width &&
21+
bounds.y + bounds.height <= area.y + area.height
22+
);
23+
};
24+
25+
export const loadWindowState = () => {
26+
const bounds = settings.get('windowBounds');
27+
28+
const positionValid = isPositionValid(bounds);
29+
const sizeValid = isSizeValid(bounds);
30+
31+
if (!positionValid || !sizeValid) {
32+
const area = getArea(bounds);
33+
bounds.x = area.x + (area.width - bounds.width) / 2;
34+
bounds.y = area.y + (area.height - bounds.height) / 2;
35+
}
36+
37+
return bounds;
38+
};
39+
40+
export const saveBounds = (mainWindow: BrowserWindow) => {
41+
settings.set('windowBounds', mainWindow.getBounds());
42+
};

src/main/settings.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ export const settings = new Store<Settings>({
2828
groupAliases: [],
2929
projects: [],
3030
selectedGroups: [],
31-
themeSource: 'system'
31+
themeSource: 'system',
32+
windowBounds: {
33+
height: 600,
34+
width: isDev ? 1426 : 800,
35+
x: 0,
36+
y: 0
37+
}
3238
},
3339
migrations: {},
3440
name: 'devkitty.settings'

src/types/settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nativeTheme } from 'electron';
1+
import { Rectangle, nativeTheme } from 'electron';
22

33
import { Group, Groups } from 'types';
44

@@ -12,4 +12,5 @@ export type Settings = {
1212
projects: Projects;
1313
selectedGroups: Group['id'][];
1414
themeSource: typeof nativeTheme.themeSource;
15+
windowBounds: Rectangle;
1516
};

0 commit comments

Comments
 (0)