Skip to content

The agent that grows with you

fix(desktop): a window moved after onboarding reopens where the user put it

alt-glitch117ce0b69c30
diff --git a/apps/desktop/electron/window-growth.test.ts b/apps/desktop/electron/window-growth.test.ts
new file mode 100644
index 00000000000..83aced6cb6d
--- /dev/null
+++ b/apps/desktop/electron/window-growth.test.ts
@@ -0,0 +1,42 @@
+/**
+ * window-state.json skips a window still at the bounds window sizing gave it,
+ * but a user who moves or resizes it afterwards has placed it, and that
+ * placement must be saved.
+ */
+import type { Rectangle } from 'electron'
+import { describe, expect, it } from 'vitest'
+
+import { isAppSized, markAppSized } from './window-growth'
+
+function placedWindow(initial: Rectangle) {
+ let bounds = { ...initial }
+
+ return {
+ getNormalBounds: () => ({ ...bounds }),
+ isMaximized: () => false,
+ moveTo: (x: number, y: number) => {
+ bounds = { ...bounds, x, y }
+ }
+ }
+}
+
+const GIVEN: Rectangle = { height: 642, width: 602, x: 500, y: 200 }
+
+describe('isAppSized', () => {
+ it('is true while the window sits at the bounds sizing gave it', () => {
+ const win = placedWindow(GIVEN)
+
+ markAppSized(win, GIVEN)
+
+ expect(isAppSized(win)).toBe(true)
+ })
+
+ it('is false once the user moves the window without resizing it', () => {
+ const win = placedWindow(GIVEN)
+
+ markAppSized(win, GIVEN)
+ win.moveTo(40, 60)
+
+ expect(isAppSized(win)).toBe(false)
+ })
+})
diff --git a/apps/desktop/electron/window-growth.ts b/apps/desktop/electron/window-growth.ts
index 56524cd7b88..b287b201149 100644
--- a/apps/desktop/electron/window-growth.ts
+++ b/apps/desktop/electron/window-growth.ts
@@ -8,21 +8,28 @@ interface WindowSizingOptions {
mainWindow: () => BrowserWindow | null
}
-// The size this module last gave each window. window-state.json skips a window
-// still at it: an app-chosen size is not where the user left the window, and a
-// saved onboarding size reopened the app as a 602x642 chat.
-const appSized = new WeakMap<BrowserWindow, { height: number; width: number }>()
+// The bounds this module last gave each window. window-state.json skips a
+// window still at them: app-chosen bounds are not where the user left the
+// window, and a saved onboarding size reopened the app as a 602x642 chat. A
+// user move or resize leaves them, so that placement is saved.
+type PlacedWindow = Pick<BrowserWindow, 'getNormalBounds' | 'isMaximized'>
-export function isAppSized(win: BrowserWindow): boolean {
- const size = appSized.get(win)
+const appSized = new WeakMap<PlacedWindow, Rectangle>()
- if (!size || win.isMaximized()) {
+export function markAppSized(win: PlacedWindow, bounds: Rectangle): void {
+ appSized.set(win, bounds)
+}
+
+export function isAppSized(win: PlacedWindow): boolean {
+ const given = appSized.get(win)
+
+ if (!given || win.isMaximized()) {
return false
}
- const { height, width } = win.getNormalBounds()
+ const bounds = win.getNormalBounds()
- return Math.abs(width - size.width) <= 1 && Math.abs(height - size.height) <= 1
+ return (['x', 'y', 'width', 'height'] as const).every(key => Math.abs(bounds[key] - given[key]) <= 1)
}
// Onboarding sets the chat size outright. Normal grows each axis to the normal
@@ -56,7 +63,7 @@ export function registerWindowSizing({ enabled, mainWindow }: WindowSizingOption
const next = sizedBounds(mode, bounds, screen.getDisplayMatching(bounds).workArea)
if (next) {
- appSized.set(win, { height: next.height, width: next.width })
+ markAppSized(win, next)
win.setBounds(next, true)
}
})