Skip to content

Commit cac9370

Browse files
committed
Merge fix/pet-stays-on-external-display: keep pet on its current display
2 parents 2ff3535 + eb621e4 commit cac9370

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

apps/desktop/src/display.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ export function getDefaultPetInitialPosition(size: WindowSize = defaultPetWindow
2626
};
2727
}
2828

29-
export function clampToPrimaryWorkArea(position: Point, size: WindowSize = defaultPetWindowSize): Point {
30-
const { workArea } = screen.getPrimaryDisplay();
29+
export function clampToVisibleWorkArea(position: Point, size: WindowSize = defaultPetWindowSize): Point {
30+
// Clamp to the display the pet currently lives on (the one nearest its centre),
31+
// not the primary display — otherwise a pet on an external monitor gets yanked
32+
// back to the built-in screen whenever its position is read, saved, or restored.
33+
const centre = { x: position.x + size.width / 2, y: position.y + size.height / 2 };
34+
const { workArea } = screen.getDisplayNearestPoint(centre);
3135
const minX = workArea.x;
3236
const minY = workArea.y;
3337
const maxX = workArea.x + Math.max(0, workArea.width - size.width);

apps/desktop/src/pet-window.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from "node:path";
44
import { pathToFileURL } from "node:url";
55

66
import { getAppStateSnapshot, markPetBroken, type PetScaleValue } from "./app-state.js";
7-
import { clampToPrimaryWorkArea, defaultPetWindowSize, getDefaultPetInitialPosition, type Point } from "./display.js";
7+
import { clampToVisibleWorkArea, defaultPetWindowSize, getDefaultPetInitialPosition, type Point } from "./display.js";
88
import { builtInPet } from "./built-in-pet.js";
99
import { getInstalledPetDir } from "./pet-paths.js";
1010
import type { OpenPetsReaction } from "./local-ipc-protocol.js";
@@ -582,12 +582,12 @@ function tryUpdateLoadedPetContent(window: BrowserWindow, render: PetContentRend
582582
}
583583

584584
export function getSafeDefaultPetPosition(position: Point | undefined): Point {
585-
return clampToPrimaryWorkArea(position ?? getDefaultPetInitialPosition(), defaultPetWindowSize);
585+
return clampToVisibleWorkArea(position ?? getDefaultPetInitialPosition(), defaultPetWindowSize);
586586
}
587587

588588
export function readWindowPosition(window: BrowserWindow): Point {
589589
const [x, y] = window.getPosition();
590-
return clampToPrimaryWorkArea({ x, y }, defaultPetWindowSize);
590+
return clampToVisibleWorkArea({ x, y }, defaultPetWindowSize);
591591
}
592592

593593
function applyLinuxPetWindowShape(window: BrowserWindow, scale: PetScaleValue, hasBubble: boolean): void {

0 commit comments

Comments
 (0)