The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
import { type BrowserWindow, ipcMain, type IpcMainEvent, type Rectangle, screen } from 'electron'
import type { WindowSizeMode } from './window-size-types'
import { windowSize } from './window-state'
type SizeRequestListener = (event: Pick<IpcMainEvent, 'sender'>, mode: WindowSizeMode) => void
/** The Electron surface window sizing uses: the size IPC and the display under a window. */
export interface WindowSizingHost {
onSizeRequest: (listener: SizeRequestListener) => void
workAreaOf: (bounds: Rectangle) => Rectangle
}
const electronHost: WindowSizingHost = {
onSizeRequest: listener => ipcMain.on('hermes:window:size', listener),
workAreaOf: bounds => screen.getDisplayMatching(bounds).workArea
}
interface WindowSizingOptions {
host?: WindowSizingHost
mainWindow: () => BrowserWindow | null
}
// 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'>
const appSized = new WeakMap<PlacedWindow, Rectangle>()
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 bounds = win.getNormalBounds()
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
// size and never shrinks one the user already made bigger.
function sizedBounds(mode: WindowSizeMode, bounds: Rectangle, workArea: Rectangle): Rectangle | null {
const target = windowSize(mode, workArea)
if (mode === 'onboarding') {
return centeredBounds(workArea, target.width, target.height)
}
const width = Math.max(bounds.width, target.width)
const height = Math.max(bounds.height, target.height)
return width === bounds.width && height === bounds.height ? null : centeredBounds(workArea, width, height)
}
// The bounds each window had when onboarding sized it. Normal puts them back, so an intro replayed
// from Settings leaves an existing user's window where it was.
const beforeOnboarding = new WeakMap<BrowserWindow, Rectangle>()
export function registerWindowSizing({ host = electronHost, mainWindow }: WindowSizingOptions): void {
host.onSizeRequest((event, mode) => {
const win = mainWindow()
if (!win || win.isDestroyed() || event.sender !== win.webContents) {
return
}
if (mode !== 'normal' && mode !== 'onboarding') {
return
}
const before = beforeOnboarding.get(win)
beforeOnboarding.delete(win)
if (win.isMaximized() || win.isFullScreen()) {
return
}
const bounds = win.getBounds()
if (mode === 'onboarding') {
beforeOnboarding.set(win, bounds)
} else if (before) {
// The user's own placement, not one this module chose: window-state.json saves it.
win.setBounds(before, true)
return
}
const next = sizedBounds(mode, bounds, host.workAreaOf(bounds))
if (next) {
markAppSized(win, next)
win.setBounds(next, true)
}
})
}
function centeredBounds(workArea: Rectangle, width: number, height: number): Rectangle {
return {
height,
width,
x: Math.round(workArea.x + (workArea.width - width) / 2),
y: Math.round(workArea.y + (workArea.height - height) / 2)
}
}