Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(desktop): save and restore window state #1852

Merged
merged 6 commits into from
Feb 10, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata' // Required by TypoORM.
;('use strict')
import { app, protocol, BrowserWindow, ipcMain, shell, Menu } from 'electron'
import { app, protocol, BrowserWindow, ipcMain, shell, Menu, screen } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
import { quitAndRenameLogger } from './utils/logger'
Expand Down Expand Up @@ -185,6 +185,22 @@ async function createWindow() {
backgroundColor: theme === 'dark' ? '#232323' : theme === 'night' ? '#212328' : '#ffffff',
icon: `${__static}/app.ico`,
})

// Restore window state
const savedBounds = electronStore.get('bounds')
if (savedBounds !== undefined) {
const screenArea = screen.getDisplayMatching(savedBounds).workArea
// Check if the window is within the bounds of the screen
if (
savedBounds.x >= screenArea.x &&
savedBounds.x <= screenArea.x + screenArea.width &&
savedBounds.y >= screenArea.y &&
savedBounds.y <= screenArea.y + screenArea.height
) {
win.setBounds(savedBounds)
}
}

// Theme change
onSystemThemeChanged(async (theme) => {
// @ts-ignore
Expand All @@ -210,7 +226,11 @@ async function createWindow() {
win.loadURL('app://./index.html')
}

win.on('closed', () => {
win.on('close', () => {
if (win) {
// Save window bounds before closing
electronStore.set('bounds', win.getBounds())
}
win = null
console.log('App closed')
beforeAppQuit()
Expand Down