Skip to content

Commit

Permalink
Fixing window restoration
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvicenti committed Aug 8, 2023
1 parent 615ac8b commit f81129a
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions frontend/apps/electron/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {initTRPC} from '@trpc/server'
import {observable} from '@trpc/server/observable'
// import {EventEmitter} from 'events'
import superjson from 'superjson'
import {app} from 'electron'
import {BrowserWindow, Menu, MenuItem, ipcMain} from 'electron'
import {createIPCHandler} from 'electron-trpc/main'
import path from 'path'
Expand Down Expand Up @@ -56,19 +57,21 @@ export function updateGoDaemonState(state: GoDaemonState) {
})
}

const store = new Store()
const store = new Store({
name: 'AppStore',
})

type AppWindow = {
route: NavRoute
bounds: any
}

let windowsState: Record<string, AppWindow> = store.get('windows') || {}
const userData = app.getPath('userData')
console.log('App UserData: ', userData)

console.log('init windowsState', windowsState)
let windowsState: Record<string, AppWindow> = store.get('WindowState') || {}

export function openInitialWindows() {
console.log('openInitialWindows', windowsState)
if (!Object.keys(windowsState).length) {
trpc.createAppWindow({route: {key: 'home'}})
return
Expand All @@ -78,10 +81,14 @@ export function openInitialWindows() {
})
}

let isExpectingQuit = false
app.addListener('before-quit', () => {
isExpectingQuit = true
})

function setWindowsState(newWindows: Record<string, AppWindow>) {
windowsState = newWindows
store.set('windows', newWindows)
console.log('windows did update', newWindows)
store.set('WindowState', newWindows)
}

function deleteWindowState(windowId: string) {
Expand Down Expand Up @@ -247,7 +254,6 @@ export const router = t.router({
},
})
function saveWindowPosition() {
console.log('saving window position')
const bounds = browserWindow.getBounds()
updateWindowState(windowId, (window) => ({...window, bounds}))
}
Expand All @@ -261,11 +267,9 @@ export const router = t.router({
}, 200)
}
browserWindow.on('resize', (e, a) => {
console.log('resized', e, a)
saveWindowPositionDebounced()
})
browserWindow.on('moved', (e, a) => {
console.log('moved', a)
saveWindowPositionDebounced()
})
allWindows.set(windowId, browserWindow)
Expand All @@ -283,7 +287,6 @@ export const router = t.router({
browserWindow.webContents.ipc.addListener(
'windowRoute',
(info, route) => {
console.log('did window route', route)
updateWindowState(windowId, (window) => ({...window, route}))
},
)
Expand All @@ -303,7 +306,9 @@ export const router = t.router({
})

browserWindow.on('close', () => {
deleteWindowState(windowId)
if (!isExpectingQuit) {
deleteWindowState(windowId)
}
trpcHandlers.detachWindow(browserWindow)
allWindows.delete(windowId)
})
Expand Down

0 comments on commit f81129a

Please sign in to comment.