Skip to content

The agent that grows with you

fix(desktop): clamp pet overlay drags and persist the clamped spot

brooklyn!75fef3739a00
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 8b6bae8fefd..02d93d3d7fc 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -321,8 +321,7 @@ import { LEGACY_OAUTH_PARTITION, resolveOauthPartition } from './oauth-partition
import { mintGatewayWsTicket as mintOauthGatewayWsTicket, requestWithOauthFallback } from './oauth-rest-request'
import { wireOauthSessionResponse } from './oauth-session-response'
import { createParentStartMarkerResolver, parentWatchdogEnv } from './parent-process-identity'
-import { resolvePetOverlayBounds } from './pet-overlay'
-import { registerPetOverlayIpc } from './pet-overlay-ipc'
+import { placePetOverlay, registerPetOverlayIpc } from './pet-overlay-ipc'
import {
pendingNotice as pendingPluginCompatNotice,
recordDismissed as recordPluginCompatDismissed
@@ -14438,9 +14437,10 @@ function closePetOverlay() {
// second overlay while the first is still on screen (see petOverlayClosing).
}
-// Re-home the popped-out pet after a display change: if the overlay still sits
-// on a connected display it stays put; otherwise it is re-centered on the main
-// window's display (see resolvePetOverlayBounds). The corrected spot is pushed
+// Re-home the popped-out pet after a display change: an overlay that still
+// fits its display stays put, one pushed past a work-area edge is clamped back
+// in, and one on no display is re-centered on the main window's display (see
+// resolvePetOverlayBounds). The corrected spot is pushed
// back to the renderer via the existing 'bounds' control channel, which it
// persists for the next pop-out/restart.
function rehomePetOverlay() {
@@ -14448,18 +14448,8 @@ function rehomePetOverlay() {
return
}
- let anchor = null
-
- try {
- if (mainWindow && !mainWindow.isDestroyed()) {
- anchor = mainWindow.getContentBounds()
- }
- } catch {
- // Resolve falls back to the primary display when the anchor is unknown.
- }
-
const current = petOverlayWindow.getBounds()
- const resolved = resolvePetOverlayBounds(current, screen.getAllDisplays(), anchor)
+ const resolved = placePetOverlay(current, mainWindow)
if (!resolved) {
return
diff --git a/apps/desktop/electron/pet-overlay-ipc.ts b/apps/desktop/electron/pet-overlay-ipc.ts
index e2ed8546edf..dfe12c2a84a 100644
--- a/apps/desktop/electron/pet-overlay-ipc.ts
+++ b/apps/desktop/electron/pet-overlay-ipc.ts
@@ -11,6 +11,23 @@ export interface PetOverlayIpcDeps {
closePetOverlay: () => void
}
+// Where the overlay may actually sit: `bounds` kept wholly on a connected
+// display, or re-centered on the main window's display when it is on none
+// (see resolvePetOverlayBounds). Null for garbage bounds.
+export function placePetOverlay(bounds, mainWindow: BrowserWindow | null) {
+ let anchor = null
+
+ try {
+ if (mainWindow && !mainWindow.isDestroyed()) {
+ anchor = mainWindow.getContentBounds()
+ }
+ } catch {
+ // Resolve falls back to the primary display when the anchor is unknown.
+ }
+
+ return resolvePetOverlayBounds(bounds, screen.getAllDisplays(), anchor)
+}
+
export function registerPetOverlayIpc({
getMainWindow,
getPetOverlayWindow,
@@ -42,25 +59,12 @@ export function registerPetOverlayIpc({
// Fall back to raw bounds if the window geometry is unavailable.
}
- // A remembered/dragged spot is only trusted while it still lands on a
- // connected display — otherwise the transparent, non-activating overlay
- // would open off-screen (e.g. saved on an external monitor that has since
- // been unplugged) and the pet would be unfindable. Re-center on the main
- // window's display instead, and echo the corrected bounds so the renderer
- // persists the on-screen spot (self-healing).
- if (screenBounds) {
- let anchor = null
-
- try {
- if (mainWindow && !mainWindow.isDestroyed()) {
- anchor = mainWindow.getContentBounds()
- }
- } catch {
- // Resolve falls back to the primary display when the anchor is unknown.
- }
-
- screenBounds = resolvePetOverlayBounds(screenBounds, screen.getAllDisplays(), anchor) ?? screenBounds
- }
+ // Both paths land wholly inside a display's work area: a fresh pop-out near
+ // the window edge or a spot saved on a since-unplugged monitor would
+ // otherwise open the transparent, non-activating overlay (mostly)
+ // off-screen where the pet can't be found. The corrected bounds are echoed
+ // so the renderer persists the on-screen spot (self-healing).
+ screenBounds = placePetOverlay(screenBounds, mainWindow) ?? screenBounds
openPetOverlay(screenBounds)
@@ -84,9 +88,14 @@ export function registerPetOverlayIpc({
return
}
+ const next = placePetOverlay(bounds, getMainWindow())
+
+ if (!next) {
+ return
+ }
+
const win = petOverlayWindow
- const width = Math.max(80, Math.round(bounds.width))
- const height = Math.max(80, Math.round(bounds.height))
+ const { width, height } = next
const [curW, curH] = win.getSize()
const resizing = width !== curW || height !== curH
@@ -94,7 +103,7 @@ export function registerPetOverlayIpc({
win.setResizable(true)
}
- win.setBounds({ x: Math.round(bounds.x), y: Math.round(bounds.y), width, height })
+ win.setBounds(next)
if (resizing) {
win.setResizable(false)
@@ -168,6 +177,12 @@ export function registerPetOverlayIpc({
mainWindow.focus()
}
+ // A drag/resize end reports the rect the overlay asked for; persist where
+ // set-bounds actually put it (clamped on-screen), not the raw request.
+ if (payload && payload.type === 'bounds') {
+ payload = { ...payload, bounds: placePetOverlay(payload.bounds, mainWindow) ?? payload.bounds }
+ }
+
mainWindow.webContents.send('hermes:pet-overlay:control', payload)
})
}