Skip to content

Commit

Permalink
save window position by mai process
Browse files Browse the repository at this point in the history
  • Loading branch information
kaishuu0123 committed Nov 9, 2024
1 parent 1c1e59e commit fd7e25e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/main/db_singleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class DbSingleton {
await this.dbs.settingsDb.read()
await this.dbs.appStateDb.read()
}

public async writeAllDbs(): Promise<void> {
await this.dbs.notesDb.write()
await this.dbs.settingsDb.write()
await this.dbs.appStateDb.write()
}
}

const instance = DbSingleton.getInstance()
Expand Down
11 changes: 7 additions & 4 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import icon from '../../resources/icon.png?asset'
import { registerIpcHandles } from './ipcHandles'
import { dbInstance } from './db_singleton'

let mainWindow: BrowserWindow

function createWindow(): void {
const appStateData = dbInstance.dbs.appStateDb.data

// Create the browser window.
const mainWindow = new BrowserWindow({
mainWindow = new BrowserWindow({
width: appStateData?.windowWidth != null ? appStateData?.windowWidth : 800,
height: appStateData?.windowHeight != null ? appStateData?.windowHeight : 600,
useContentSize: true,
Expand Down Expand Up @@ -72,10 +74,10 @@ app.whenReady().then(async () => {
// IPC test
ipcMain.on('ping', () => console.log('pong'))

registerIpcHandles(ipcMain)

createWindow()

registerIpcHandles(ipcMain, mainWindow)

app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
Expand All @@ -86,7 +88,8 @@ app.whenReady().then(async () => {
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
app.on('window-all-closed', async () => {
await dbInstance.writeAllDbs()
if (process.platform !== 'darwin') {
app.quit()
}
Expand Down
14 changes: 12 additions & 2 deletions src/main/ipcHandles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { uuidv7 } from 'uuidv7'

import { AppState, Note, NoteEditorSettings } from '../types'
import { dbInstance } from './db_singleton'
import { BrowserWindow } from 'electron'

export const registerIpcHandles = (ipcMain): void => {
export const registerIpcHandles = (ipcMain, mainWindow: BrowserWindow): void => {
const { notesDb, settingsDb, appStateDb } = dbInstance.dbs

ipcMain.handle('get-notes', (): Note[] => {
Expand Down Expand Up @@ -100,7 +101,16 @@ export const registerIpcHandles = (ipcMain): void => {
ipcMain.handle(
'update-app-state',
async (_event: Electron.IpcMainInvokeEvent, appState: AppState) => {
appStateDb.data = appState
const bounds = mainWindow.getBounds()

appStateDb.data = {
...appState,
windowX: bounds.x,
windowY: bounds.y,
windowWidth: bounds.width,
windowHeight: bounds.height
}

await appStateDb.write()
}
)
Expand Down

0 comments on commit fd7e25e

Please sign in to comment.