Skip to content

Private AI meeting notes that stay on your computer. No meeting bot. Free and open source for Mac, Windows and Linux. An alternative to Granola, Otter and Fireflies.

Window: Yogurt opens at the size and place it was left at (and maximized if it was), back to the default size when that place is on a screen no longer there

christianmat1b93858c697d
diff --git a/app/src/main/index.ts b/app/src/main/index.ts
index f2b8134c..6e148a31 100644
--- a/app/src/main/index.ts
+++ b/app/src/main/index.ts
@@ -1,3 +1,4 @@
+import { loadWindowState, placeWindow, saveWindowState } from './windowState'
import { trustSystemCertificates } from './systemCerts'
import { createSavedTranscriptions } from './savedTranscription'
import { transcriptionBusy } from '../shared/transcription'
@@ -18,7 +19,7 @@ import { isAppRoute } from '../shared/routes'
import { resumesNote } from '../shared/agendaNote'
import { relaunchOptions, stripAppImageEnv } from './relaunch'
import { capabilities, micListHelper, platformOf, supportedKinds } from '../shared/platform'
-import { app, BrowserWindow, dialog, ipcMain, nativeTheme, Notification, protocol, safeStorage, shell } from 'electron'
+import { app, BrowserWindow, dialog, ipcMain, nativeTheme, Notification, protocol, safeStorage, screen, shell } from 'electron'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import os from 'node:os'
import { basename, join } from 'node:path'
@@ -130,8 +131,13 @@ let updateWidgetVisibility = () => {}
let recordingPillChanged = (_on: boolean) => {}
let sendWidgetTranscript = (_channel: string, _value: unknown) => {}
function createWindow() {
+ // the size and place it was left at (windowState.ts); test runs (fake mode) always start at the default, so one
+ // test's resize never carries into the next
+ const remember = !isFake()
+ const stateFile = join(userDataDir(), 'window.json')
+ const place = placeWindow(remember ? loadWindowState(stateFile) : null, screen.getAllDisplays().map((d) => d.workArea), { width: 1100, height: 760, minWidth: 720, minHeight: 520 })
win = new BrowserWindow({
- width: 1100, height: 760, minWidth: 720, minHeight: 520,
+ x: place.x, y: place.y, width: place.width, height: place.height, minWidth: 720, minHeight: 520,
title: 'Yogurt',
...windowChrome(process.platform, nativeTheme.shouldUseDarkColors),
show: false,
@@ -147,6 +153,16 @@ function createWindow() {
nativeTheme.on('updated', recolor)
win.on('closed', () => nativeTheme.off('updated', recolor))
}
+ if (place.maximized) win.maximize()
+ if (remember) {
+ // saved a moment after a move or resize settles, and whenever the window is hidden or closed
+ const w = win
+ let timer: NodeJS.Timeout | undefined
+ const save = () => { clearTimeout(timer); if (!w.isDestroyed() && !w.isFullScreen()) saveWindowState(stateFile, { ...w.getNormalBounds(), maximized: w.isMaximized() }) }
+ const later = () => { clearTimeout(timer); timer = setTimeout(save, 500) }
+ w.on('resize', later); w.on('move', later)
+ w.on('hide', save); w.on('close', save)
+ }
win.webContents.setWindowOpenHandler(({ url }) => { openExternally(url); return { action: 'deny' } })
win.webContents.on('will-navigate', (e, url) => {
const ownPage = process.env.ELECTRON_RENDERER_URL ? url.startsWith(process.env.ELECTRON_RENDERER_URL) : url === pathToFileURL(join(__dirname, '../renderer/index.html')).toString()
diff --git a/app/src/main/windowState.ts b/app/src/main/windowState.ts
new file mode 100644
index 00000000..36723e5d
--- /dev/null
+++ b/app/src/main/windowState.ts
@@ -0,0 +1,45 @@
+import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
+import { dirname } from 'node:path'
+
+/**
+ * The main window opens the size and place it was left at, so it never has to be resized on every start. A place no
+ * screen shows any more (a monitor unplugged) keeps the size and lets the system centre it.
+ */
+export type WindowState = { x: number; y: number; width: number; height: number; maximized?: boolean }
+type Area = { x: number; y: number; width: number; height: number }
+type Defaults = { width: number; height: number; minWidth: number; minHeight: number }
+export type Placement = { x?: number; y?: number; width: number; height: number; maximized?: boolean }
+
+const num = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v)
+
+export function loadWindowState(file: string): WindowState | null {
+ try {
+ const s = JSON.parse(readFileSync(file, 'utf8'))
+ if (!s || ![s.x, s.y, s.width, s.height].every(num)) return null
+ return { x: s.x, y: s.y, width: s.width, height: s.height, maximized: s.maximized === true }
+ } catch { return null }
+}
+
+export function saveWindowState(file: string, s: WindowState) {
+ try {
+ mkdirSync(dirname(file), { recursive: true })
+ // written whole and swapped in, so a crash mid-write never leaves half a file
+ writeFileSync(`${file}.tmp`, JSON.stringify(s))
+ renameSync(`${file}.tmp`, file)
+ } catch (e) { console.warn('could not save the window size', e) }
+}
+
+/** where the window opens: the saved place if a screen still shows enough of it, the saved size within the limits */
+export function placeWindow(saved: WindowState | null, screens: Area[], d: Defaults): Placement {
+ if (!saved) return { width: d.width, height: d.height }
+ const biggest = screens.reduce((a, s) => (s.width * s.height > a.width * a.height ? s : a), screens[0] ?? { x: 0, y: 0, width: d.width, height: d.height })
+ const width = Math.min(Math.max(saved.width, d.minWidth), biggest.width)
+ const height = Math.min(Math.max(saved.height, d.minHeight), biggest.height)
+ // a screen must show the window's top strip (where it is dragged from), at least 100 points of it
+ const seen = screens.some((s) => {
+ const left = Math.max(s.x, saved.x), right = Math.min(s.x + s.width, saved.x + width)
+ return right - left >= 100 && saved.y >= s.y && saved.y < s.y + s.height - 40
+ })
+ const maximized = saved.maximized ? { maximized: true } : {}
+ return seen ? { x: saved.x, y: saved.y, width, height, ...maximized } : { width, height, ...maximized }
+}
diff --git a/app/test/windowState.test.ts b/app/test/windowState.test.ts
new file mode 100644
index 00000000..cf66102b
--- /dev/null
+++ b/app/test/windowState.test.ts
@@ -0,0 +1,40 @@
+import { afterEach, describe, expect, it } from 'vitest'
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { loadWindowState, placeWindow, saveWindowState } from '../src/main/windowState'
+
+const dirs: string[] = []
+afterEach(() => dirs.splice(0).forEach((d) => rmSync(d, { recursive: true, force: true })))
+const file = () => { const d = mkdtempSync(join(tmpdir(), 'yogurt-window-')); dirs.push(d); return join(d, 'window.json') }
+
+const screen = { x: 0, y: 25, width: 1512, height: 920 }
+const defaults = { width: 1100, height: 760, minWidth: 720, minHeight: 520 }
+
+describe('the main window opens where it was left', () => {
+ it('no saved size: the default size, centred by the system', () => {
+ expect(placeWindow(null, [screen], defaults)).toEqual({ width: 1100, height: 760 })
+ })
+ it('a saved size and place on a screen: both come back, and maximized too', () => {
+ const saved = { x: 100, y: 80, width: 1300, height: 820, maximized: true }
+ expect(placeWindow(saved, [screen], defaults)).toEqual({ x: 100, y: 80, width: 1300, height: 820, maximized: true })
+ })
+ it('a place no screen shows any more (a monitor unplugged): the saved size, centred', () => {
+ expect(placeWindow({ x: 3000, y: 100, width: 1300, height: 820 }, [screen], defaults)).toEqual({ width: 1300, height: 820 })
+ })
+ it('never smaller than the minimum or bigger than the screen', () => {
+ expect(placeWindow({ x: 10, y: 30, width: 300, height: 200 }, [screen], defaults)).toMatchObject({ width: 720, height: 520 })
+ expect(placeWindow({ x: 0, y: 25, width: 4000, height: 3000 }, [screen], defaults)).toMatchObject({ width: 1512, height: 920 })
+ })
+ it('saves and reads back; a broken or missing file reads as nothing', () => {
+ const f = file()
+ expect(loadWindowState(f)).toBeNull()
+ saveWindowState(f, { x: 1, y: 30, width: 900, height: 600, maximized: false })
+ expect(loadWindowState(f)).toEqual({ x: 1, y: 30, width: 900, height: 600, maximized: false })
+ expect(JSON.parse(readFileSync(f, 'utf8')).width).toBe(900)
+ writeFileSync(f, '{not json')
+ expect(loadWindowState(f)).toBeNull()
+ writeFileSync(f, JSON.stringify({ width: 'wide' }))
+ expect(loadWindowState(f)).toBeNull()
+ })
+})