Skip to content

Commit

Permalink
Store window size and position. (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinjhuang authored Oct 14, 2024
1 parent a5693f2 commit 24a487f
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"dotenv": "^16.4.5",
"electron-log": "^5.2.0",
"electron-squirrel-startup": "^1.0.1",
"electron-store": "8.2.0",
"jest": "^29.7.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
31 changes: 27 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import { app, BrowserWindow, screen, ipcMain, Menu, MenuItem } from 'electron';
import tar from 'tar';
import log from 'electron-log/main';
import * as Sentry from '@sentry/electron/main';

import Store from 'electron-store';
import { updateElectronApp, UpdateSourceType } from 'update-electron-app';
import * as net from 'net';
import { graphics } from 'systeminformation';

import { StoreType } from './store';
log.initialize();
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
// Run this as early in the main process as possible.
if (require('electron-squirrel-startup')) app.quit();

const store = new Store<StoreType>();

updateElectronApp({
updateSource: {
type: UpdateSourceType.StaticStorage,
Expand Down Expand Up @@ -280,17 +283,26 @@ function buildMenu(): Menu {
export const createWindow = async (userResourcesPath: string): Promise<BrowserWindow> => {
const primaryDisplay = screen.getPrimaryDisplay();
const { width, height } = primaryDisplay.workAreaSize;

// Retrieve stored window size, or use default if not available
const storedWidth = store.get('windowWidth', width);
const storedHeight = store.get('windowHeight', height);
const storedX = store.get('windowX');
const storedY = store.get('windowY');

if (mainWindow) {
log.info('Main window already exists');
return mainWindow;
}
mainWindow = new BrowserWindow({
title: 'ComfyUI',
width: width,
height: height,
width: storedWidth,
height: storedHeight,
x: storedX,
y: storedY,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true, // Enable Node.js integration
nodeIntegration: true,
contextIsolation: true,
},
autoHideMenuBar: true,
Expand All @@ -302,6 +314,17 @@ export const createWindow = async (userResourcesPath: string): Promise<BrowserWi
// Returns a tray so you can set a global var to access.
SetupTray(mainWindow, userResourcesPath);

const updateBounds = () => {
const { width, height, x, y } = mainWindow.getBounds();
store.set('windowWidth', width);
store.set('windowHeight', height);
store.set('windowX', x);
store.set('windowY', y);
};

mainWindow.on('resize', updateBounds);
mainWindow.on('move', updateBounds);

mainWindow.on('close', (e: Electron.Event) => {
// Mac Only Behavior
if (process.platform === 'darwin') {
Expand Down
6 changes: 6 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type StoreType = {
windowWidth: number;
windowHeight: number;
windowX: number | undefined;
windowY: number | undefined;
};
170 changes: 168 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4718,6 +4718,20 @@ __metadata:
languageName: node
linkType: hard

"ajv-formats@npm:^2.1.1":
version: 2.1.1
resolution: "ajv-formats@npm:2.1.1"
dependencies:
ajv: "npm:^8.0.0"
peerDependencies:
ajv: ^8.0.0
peerDependenciesMeta:
ajv:
optional: true
checksum: 10c0/e43ba22e91b6a48d96224b83d260d3a3a561b42d391f8d3c6d2c1559f9aa5b253bfb306bc94bbeca1d967c014e15a6efe9a207309e95b3eaae07fcbcdc2af662
languageName: node
linkType: hard

"ajv@npm:^6.12.4":
version: 6.12.6
resolution: "ajv@npm:6.12.6"
Expand All @@ -4730,6 +4744,18 @@ __metadata:
languageName: node
linkType: hard

"ajv@npm:^8.0.0, ajv@npm:^8.6.3":
version: 8.17.1
resolution: "ajv@npm:8.17.1"
dependencies:
fast-deep-equal: "npm:^3.1.3"
fast-uri: "npm:^3.0.1"
json-schema-traverse: "npm:^1.0.0"
require-from-string: "npm:^2.0.2"
checksum: 10c0/ec3ba10a573c6b60f94639ffc53526275917a2df6810e4ab5a6b959d87459f9ef3f00d5e7865b82677cb7d21590355b34da14d1d0b9c32d75f95a187e76fff35
languageName: node
linkType: hard

"ansi-escapes@npm:^3.2.0":
version: 3.2.0
resolution: "ansi-escapes@npm:3.2.0"
Expand Down Expand Up @@ -5049,6 +5075,13 @@ __metadata:
languageName: node
linkType: hard

"atomically@npm:^1.7.0":
version: 1.7.0
resolution: "atomically@npm:1.7.0"
checksum: 10c0/31f5efd5d69474681268557af4024f9e10223bb6b39fdedb5f2e19405186c4b76284fac9f6c43c9af75013cad6437e93b7168268f5ddb7aaf1cfc5fdb415f227
languageName: node
linkType: hard

"author-regex@npm:^1.0.0":
version: 1.0.0
resolution: "author-regex@npm:1.0.0"
Expand Down Expand Up @@ -5842,6 +5875,7 @@ __metadata:
electron: "npm:31.3.1"
electron-log: "npm:^5.2.0"
electron-squirrel-startup: "npm:^1.0.1"
electron-store: "npm:8.2.0"
eslint: "npm:^8.0.1"
eslint-plugin-import: "npm:^2.25.0"
jest: "npm:^29.7.0"
Expand Down Expand Up @@ -5894,6 +5928,24 @@ __metadata:
languageName: node
linkType: hard

"conf@npm:^10.2.0":
version: 10.2.0
resolution: "conf@npm:10.2.0"
dependencies:
ajv: "npm:^8.6.3"
ajv-formats: "npm:^2.1.1"
atomically: "npm:^1.7.0"
debounce-fn: "npm:^4.0.0"
dot-prop: "npm:^6.0.1"
env-paths: "npm:^2.2.1"
json-schema-typed: "npm:^7.0.3"
onetime: "npm:^5.1.2"
pkg-up: "npm:^3.1.0"
semver: "npm:^7.3.5"
checksum: 10c0/d608d8c54ba7fad368eac640e77f2ce0334ec27cfd62ac39f44e361af8af9915eaa6c2ada81fbc25c3219273d972b4868bc752e8e2116cb6e12d35df72dc25a4
languageName: node
linkType: hard

"console-control-strings@npm:^1.1.0":
version: 1.1.0
resolution: "console-control-strings@npm:1.1.0"
Expand Down Expand Up @@ -6040,6 +6092,15 @@ __metadata:
languageName: node
linkType: hard

"debounce-fn@npm:^4.0.0":
version: 4.0.0
resolution: "debounce-fn@npm:4.0.0"
dependencies:
mimic-fn: "npm:^3.0.0"
checksum: 10c0/bcbd8eb253bdb6ee2f32759c95973c62bc479e74efbe1a44e17acfb0ea7d4bcbe615bf7e34aab80247ac08669c1ab72f7da0f384ceb7f15c18333d31d9030384
languageName: node
linkType: hard

"debug@npm:2.6.9, debug@npm:^2.2.0":
version: 2.6.9
resolution: "debug@npm:2.6.9"
Expand Down Expand Up @@ -6250,6 +6311,15 @@ __metadata:
languageName: node
linkType: hard

"dot-prop@npm:^6.0.1":
version: 6.0.1
resolution: "dot-prop@npm:6.0.1"
dependencies:
is-obj: "npm:^2.0.0"
checksum: 10c0/30e51ec6408978a6951b21e7bc4938aad01a86f2fdf779efe52330205c6bb8a8ea12f35925c2029d6dc9d1df22f916f32f828ce1e9b259b1371c580541c22b5a
languageName: node
linkType: hard

"dotenv@npm:^16.3.1, dotenv@npm:^16.4.5":
version: 16.4.5
resolution: "dotenv@npm:16.4.5"
Expand Down Expand Up @@ -6402,6 +6472,16 @@ __metadata:
languageName: node
linkType: hard

"electron-store@npm:8.2.0":
version: 8.2.0
resolution: "electron-store@npm:8.2.0"
dependencies:
conf: "npm:^10.2.0"
type-fest: "npm:^2.17.0"
checksum: 10c0/a4d19827e96ab67bf6c2a375910f51b147b23f4a0468da5cfeeb069acdfdbcd3a9f5650248a62a05aa0967149e4d1c47f2d0ba7582205e5eb38952c93b6882e1
languageName: node
linkType: hard

"electron-to-chromium@npm:^1.5.4":
version: 1.5.28
resolution: "electron-to-chromium@npm:1.5.28"
Expand Down Expand Up @@ -6499,7 +6579,7 @@ __metadata:
languageName: node
linkType: hard

"env-paths@npm:^2.2.0":
"env-paths@npm:^2.2.0, env-paths@npm:^2.2.1":
version: 2.2.1
resolution: "env-paths@npm:2.2.1"
checksum: 10c0/285325677bf00e30845e330eec32894f5105529db97496ee3f598478e50f008c5352a41a30e5e72ec9de8a542b5a570b85699cd63bd2bc646dbcb9f311d83bc4
Expand Down Expand Up @@ -7155,6 +7235,13 @@ __metadata:
languageName: node
linkType: hard

"fast-uri@npm:^3.0.1":
version: 3.0.2
resolution: "fast-uri@npm:3.0.2"
checksum: 10c0/8cdd3da7b4022a037d348d587d55caff74b7e4f862bbdd2cc35c1e6e3f97d0aedb567894d44c57ee8798d3192cceb97dcf41dbdabfa07dd2842a0474a6c6eeef
languageName: node
linkType: hard

"fast-xml-parser@npm:4.4.1":
version: 4.4.1
resolution: "fast-xml-parser@npm:4.4.1"
Expand Down Expand Up @@ -7282,6 +7369,15 @@ __metadata:
languageName: node
linkType: hard

"find-up@npm:^3.0.0":
version: 3.0.0
resolution: "find-up@npm:3.0.0"
dependencies:
locate-path: "npm:^3.0.0"
checksum: 10c0/2c2e7d0a26db858e2f624f39038c74739e38306dee42b45f404f770db357947be9d0d587f1cac72d20c114deb38aa57316e879eb0a78b17b46da7dab0a3bd6e3
languageName: node
linkType: hard

"find-up@npm:^4.0.0, find-up@npm:^4.1.0":
version: 4.1.0
resolution: "find-up@npm:4.1.0"
Expand Down Expand Up @@ -8555,6 +8651,13 @@ __metadata:
languageName: node
linkType: hard

"is-obj@npm:^2.0.0":
version: 2.0.0
resolution: "is-obj@npm:2.0.0"
checksum: 10c0/85044ed7ba8bd169e2c2af3a178cacb92a97aa75de9569d02efef7f443a824b5e153eba72b9ae3aca6f8ce81955271aa2dc7da67a8b720575d3e38104208cb4e
languageName: node
linkType: hard

"is-path-inside@npm:^3.0.3":
version: 3.0.3
resolution: "is-path-inside@npm:3.0.3"
Expand Down Expand Up @@ -9335,6 +9438,20 @@ __metadata:
languageName: node
linkType: hard

"json-schema-traverse@npm:^1.0.0":
version: 1.0.0
resolution: "json-schema-traverse@npm:1.0.0"
checksum: 10c0/71e30015d7f3d6dc1c316d6298047c8ef98a06d31ad064919976583eb61e1018a60a0067338f0f79cabc00d84af3fcc489bd48ce8a46ea165d9541ba17fb30c6
languageName: node
linkType: hard

"json-schema-typed@npm:^7.0.3":
version: 7.0.3
resolution: "json-schema-typed@npm:7.0.3"
checksum: 10c0/b4a6d984dd91f9aba72df8768c5ced99e789b8e17b55ee24afb3a687ce55b70a7b3f4360cac67939e1ff98e136ca26f3aa530635c13ef371ae5edc48b69a65f6
languageName: node
linkType: hard

"json-stable-stringify-without-jsonify@npm:^1.0.1":
version: 1.0.1
resolution: "json-stable-stringify-without-jsonify@npm:1.0.1"
Expand Down Expand Up @@ -9523,6 +9640,16 @@ __metadata:
languageName: node
linkType: hard

"locate-path@npm:^3.0.0":
version: 3.0.0
resolution: "locate-path@npm:3.0.0"
dependencies:
p-locate: "npm:^3.0.0"
path-exists: "npm:^3.0.0"
checksum: 10c0/3db394b7829a7fe2f4fbdd25d3c4689b85f003c318c5da4052c7e56eed697da8f1bce5294f685c69ff76e32cba7a33629d94396976f6d05fb7f4c755c5e2ae8b
languageName: node
linkType: hard

"locate-path@npm:^5.0.0":
version: 5.0.0
resolution: "locate-path@npm:5.0.0"
Expand Down Expand Up @@ -9861,6 +9988,13 @@ __metadata:
languageName: node
linkType: hard

"mimic-fn@npm:^3.0.0":
version: 3.1.0
resolution: "mimic-fn@npm:3.1.0"
checksum: 10c0/a07cdd8ed6490c2dff5b11f889b245d9556b80f5a653a552a651d17cff5a2d156e632d235106c2369f00cccef4071704589574cf3601bc1b1400a1f620dff067
languageName: node
linkType: hard

"mimic-response@npm:^1.0.0":
version: 1.0.1
resolution: "mimic-response@npm:1.0.1"
Expand Down Expand Up @@ -10530,7 +10664,7 @@ __metadata:
languageName: node
linkType: hard

"p-limit@npm:^2.2.0":
"p-limit@npm:^2.0.0, p-limit@npm:^2.2.0":
version: 2.3.0
resolution: "p-limit@npm:2.3.0"
dependencies:
Expand All @@ -10557,6 +10691,15 @@ __metadata:
languageName: node
linkType: hard

"p-locate@npm:^3.0.0":
version: 3.0.0
resolution: "p-locate@npm:3.0.0"
dependencies:
p-limit: "npm:^2.0.0"
checksum: 10c0/7b7f06f718f19e989ce6280ed4396fb3c34dabdee0df948376483032f9d5ec22fdf7077ec942143a75827bb85b11da72016497fc10dac1106c837ed593969ee8
languageName: node
linkType: hard

"p-locate@npm:^4.1.0":
version: 4.1.0
resolution: "p-locate@npm:4.1.0"
Expand Down Expand Up @@ -10852,6 +10995,15 @@ __metadata:
languageName: node
linkType: hard

"pkg-up@npm:^3.1.0":
version: 3.1.0
resolution: "pkg-up@npm:3.1.0"
dependencies:
find-up: "npm:^3.0.0"
checksum: 10c0/ecb60e1f8e1f611c0bdf1a0b6a474d6dfb51185567dc6f29cdef37c8d480ecba5362e006606bb290519bbb6f49526c403fabea93c3090c20368d98bb90c999ab
languageName: node
linkType: hard

"playwright-core@npm:1.47.2":
version: 1.47.2
resolution: "playwright-core@npm:1.47.2"
Expand Down Expand Up @@ -11292,6 +11444,13 @@ __metadata:
languageName: node
linkType: hard

"require-from-string@npm:^2.0.2":
version: 2.0.2
resolution: "require-from-string@npm:2.0.2"
checksum: 10c0/aaa267e0c5b022fc5fd4eef49d8285086b15f2a1c54b28240fdf03599cbd9c26049fee3eab894f2e1f6ca65e513b030a7c264201e3f005601e80c49fb2937ce2
languageName: node
linkType: hard

"require-in-the-middle@npm:^7.1.1":
version: 7.4.0
resolution: "require-in-the-middle@npm:7.4.0"
Expand Down Expand Up @@ -12648,6 +12807,13 @@ __metadata:
languageName: node
linkType: hard

"type-fest@npm:^2.17.0":
version: 2.19.0
resolution: "type-fest@npm:2.19.0"
checksum: 10c0/a5a7ecf2e654251613218c215c7493574594951c08e52ab9881c9df6a6da0aeca7528c213c622bc374b4e0cb5c443aa3ab758da4e3c959783ce884c3194e12cb
languageName: node
linkType: hard

"type-is@npm:~1.6.18":
version: 1.6.18
resolution: "type-is@npm:1.6.18"
Expand Down

0 comments on commit 24a487f

Please sign in to comment.