Skip to content

Commit cacdbb2

Browse files
fix(desktop): a maximized window reopens on the display it was last used on (pingdotgg#25833)
1 parent d749cfd commit cacdbb2

2 files changed

Lines changed: 225 additions & 22 deletions

File tree

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,11 +1023,75 @@ describe("DesktopWindow", () => {
10231023
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 220, y: 140, width: 1380, height: 920 }]);
10241024
assert.deepEqual(mainWindowMaximizedUpdates, [true]);
10251025
assert.equal(fakeWindow.getNormalBounds.mock.calls.length, 1);
1026-
assert.equal(fakeWindow.getBounds.mock.calls.length, 0);
10271026
}).pipe(Effect.provide(layer));
10281027
}),
10291028
);
10301029

1030+
it.effect.each([
1031+
{ normal: "on display 1", width: 1920, height: 1080, x: 220 },
1032+
{ normal: "on display 1", width: 1280, height: 720, x: 220 },
1033+
{ normal: "straddling both displays", width: 1920, height: 1080, x: 1800 },
1034+
])(
1035+
"persists bounds on the display a maximized window was moved to ($width x $height, normal $normal)",
1036+
({ width, height, x }) =>
1037+
Effect.gen(function* () {
1038+
const displayOne = { x: 0, y: 0, width: 1920, height: 1080 };
1039+
const displayTwo = { x: 1920, y: 0, width, height };
1040+
const getAllDisplays = vi.mocked(Electron.screen.getAllDisplays);
1041+
getAllDisplays.mockReturnValue([
1042+
{ bounds: displayOne, workArea: displayOne },
1043+
{ bounds: displayTwo, workArea: displayTwo },
1044+
] as unknown as Electron.Display[]);
1045+
const fakeWindow = makeFakeBrowserWindow();
1046+
fakeWindow.isMaximized.mockReturnValue(true);
1047+
fakeWindow.getBounds.mockReturnValue(displayTwo);
1048+
fakeWindow.getNormalBounds.mockReturnValue({ x, y: 140, width: 1380, height: 920 });
1049+
const createCount = yield* Ref.make(0);
1050+
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
1051+
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
1052+
const mainWindowMaximizedUpdates: boolean[] = [];
1053+
const layer = layerTest({
1054+
window: fakeWindow.window,
1055+
createCount,
1056+
mainWindow,
1057+
mainWindowBoundsUpdates,
1058+
mainWindowMaximizedUpdates,
1059+
});
1060+
1061+
yield* Effect.gen(function* () {
1062+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
1063+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
1064+
1065+
const move = fakeWindow.windowListeners.get("move");
1066+
if (!move) {
1067+
return yield* Effect.die("window move listener was not registered");
1068+
}
1069+
move();
1070+
yield* TestClock.adjust(500);
1071+
yield* Effect.promise(() => Promise.resolve());
1072+
1073+
const persisted = mainWindowBoundsUpdates.at(-1);
1074+
assert.isDefined(persisted, "maximized window bounds were not persisted");
1075+
const persistedDisplay = [displayOne, displayTwo].findIndex(
1076+
(display) =>
1077+
persisted.x >= display.x &&
1078+
persisted.y >= display.y &&
1079+
persisted.x + persisted.width <= display.x + display.width &&
1080+
persisted.y + persisted.height <= display.y + display.height,
1081+
);
1082+
assert.equal(
1083+
persistedDisplay,
1084+
1,
1085+
`window maximized on display 2 persisted bounds ${JSON.stringify(persisted)}, which restore on display ${persistedDisplay + 1}`,
1086+
);
1087+
assert.deepEqual(mainWindowMaximizedUpdates, [true]);
1088+
}).pipe(
1089+
Effect.provide(layer),
1090+
Effect.ensuring(Effect.sync(() => getAllDisplays.mockReset())),
1091+
);
1092+
}),
1093+
);
1094+
10311095
it.effect("preserves unrestorable bounds until the user changes the window", () =>
10321096
Effect.gen(function* () {
10331097
const fakeWindow = makeFakeBrowserWindow();
@@ -1071,8 +1135,7 @@ describe("DesktopWindow", () => {
10711135
it.effect("flushes normal bounds when minimized before the debounce completes", () =>
10721136
Effect.gen(function* () {
10731137
const fakeWindow = makeFakeBrowserWindow();
1074-
fakeWindow.getBounds.mockReturnValue({ x: -32_000, y: -32_000, width: 160, height: 28 });
1075-
fakeWindow.getNormalBounds.mockReturnValue({ x: 180, y: 120, width: 1440, height: 960 });
1138+
fakeWindow.getBounds.mockReturnValue({ x: 180, y: 120, width: 1440, height: 960 });
10761139
const createCount = yield* Ref.make(0);
10771140
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
10781141
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
@@ -1093,17 +1156,87 @@ describe("DesktopWindow", () => {
10931156
}
10941157
resize();
10951158
yield* TestClock.adjust(250);
1159+
// Windows reports a minimized window at -32000.
10961160
fakeWindow.isMinimized.mockReturnValue(true);
1161+
fakeWindow.getBounds.mockReturnValue({ x: -32_000, y: -32_000, width: 160, height: 28 });
10971162

10981163
yield* desktopWindow.flushMainWindowBounds;
1164+
resize();
1165+
yield* TestClock.adjust(500);
1166+
yield* Effect.promise(() => Promise.resolve());
10991167

11001168
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 180, y: 120, width: 1440, height: 960 }]);
1101-
assert.equal(fakeWindow.getBounds.mock.calls.length, 0);
1102-
assert.equal(fakeWindow.getNormalBounds.mock.calls.length, 1);
11031169
}).pipe(Effect.provide(layer));
11041170
}),
11051171
);
11061172

1173+
it.effect.each([
1174+
{ window: "normal", write: "flush" },
1175+
{ window: "normal", write: "debounce" },
1176+
{ window: "maximized", write: "flush" },
1177+
{ window: "maximized", write: "debounce" },
1178+
] as const)("persists a $window window change minimized before its $write", (input) =>
1179+
Effect.gen(function* () {
1180+
const displayOne = { x: 0, y: 0, width: 1920, height: 1080 };
1181+
const displayTwo = { x: 1920, y: 0, width: 1920, height: 1080 };
1182+
const getAllDisplays = vi.mocked(Electron.screen.getAllDisplays);
1183+
getAllDisplays.mockReturnValue([
1184+
{ bounds: displayOne, workArea: displayOne },
1185+
{ bounds: displayTwo, workArea: displayTwo },
1186+
] as unknown as Electron.Display[]);
1187+
const maximized = input.window === "maximized";
1188+
const fakeWindow = makeFakeBrowserWindow();
1189+
fakeWindow.isMaximized.mockReturnValue(maximized);
1190+
fakeWindow.getBounds.mockReturnValue(
1191+
maximized ? displayTwo : { x: 220, y: 140, width: 1380, height: 920 },
1192+
);
1193+
fakeWindow.getNormalBounds.mockReturnValue({ x: 220, y: 140, width: 1380, height: 920 });
1194+
const createCount = yield* Ref.make(0);
1195+
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
1196+
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
1197+
const mainWindowMaximizedUpdates: boolean[] = [];
1198+
const layer = layerTest({
1199+
window: fakeWindow.window,
1200+
createCount,
1201+
mainWindow,
1202+
mainWindowBoundsUpdates,
1203+
mainWindowMaximizedUpdates,
1204+
});
1205+
1206+
yield* Effect.gen(function* () {
1207+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
1208+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
1209+
1210+
const move = fakeWindow.windowListeners.get("move");
1211+
if (!move) {
1212+
return yield* Effect.die("window move listener was not registered");
1213+
}
1214+
move();
1215+
yield* TestClock.adjust(250);
1216+
// Minimized windows report stale state: macOS unmaximized at the pre-move frame.
1217+
fakeWindow.isMinimized.mockReturnValue(true);
1218+
fakeWindow.isMaximized.mockReturnValue(false);
1219+
fakeWindow.getBounds.mockReturnValue({ x: 100, y: 100, width: 1100, height: 780 });
1220+
fakeWindow.getNormalBounds.mockReturnValue({ x: 100, y: 100, width: 1100, height: 780 });
1221+
if (input.write === "debounce") {
1222+
yield* TestClock.adjust(250);
1223+
yield* Effect.promise(() => Promise.resolve());
1224+
}
1225+
yield* desktopWindow.flushMainWindowBounds;
1226+
1227+
assert.deepEqual(mainWindowBoundsUpdates, [
1228+
maximized
1229+
? { x: 2140, y: 140, width: 1380, height: 920 }
1230+
: { x: 220, y: 140, width: 1380, height: 920 },
1231+
]);
1232+
assert.deepEqual(mainWindowMaximizedUpdates, [maximized]);
1233+
}).pipe(
1234+
Effect.provide(layer),
1235+
Effect.ensuring(Effect.sync(() => getAllDisplays.mockReset())),
1236+
);
1237+
}),
1238+
);
1239+
11071240
it.effect("maps macOS horizontal swipes to available history navigation", () =>
11081241
Effect.gen(function* () {
11091242
const fakeWindow = makeFakeBrowserWindow();

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 87 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,58 @@ function windowFitsWithinDisplay(
183183
);
184184
}
185185

186+
function overlapArea(left: DisplayBounds, right: DisplayBounds): number {
187+
const width = Math.min(left.x + left.width, right.x + right.width) - Math.max(left.x, right.x);
188+
const height = Math.min(left.y + left.height, right.y + right.height) - Math.max(left.y, right.y);
189+
return width > 0 && height > 0 ? width * height : 0;
190+
}
191+
192+
function displayMatching(
193+
bounds: DisplayBounds,
194+
displays: readonly DisplayBounds[],
195+
): DisplayBounds | undefined {
196+
let match: DisplayBounds | undefined;
197+
let matchArea = 0;
198+
for (const display of displays) {
199+
const area = overlapArea(bounds, display);
200+
if (area > matchArea) {
201+
match = display;
202+
matchArea = area;
203+
}
204+
}
205+
return match;
206+
}
207+
208+
// Normal bounds of a maximized window can sit on, or straddle into, another display than the window.
209+
function normalBoundsOnWindowDisplay(
210+
normalBounds: DisplayBounds,
211+
windowBounds: DisplayBounds,
212+
displays: readonly DisplayBounds[],
213+
): DisplayBounds {
214+
const target = displayMatching(windowBounds, displays);
215+
if (target === undefined || windowFitsWithinDisplay(normalBounds, target)) {
216+
return normalBounds;
217+
}
218+
const source = displayMatching(normalBounds, displays) ?? target;
219+
const width = Math.min(normalBounds.width, target.width);
220+
const height = Math.min(normalBounds.height, target.height);
221+
const clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);
222+
return {
223+
x: clamp(target.x + normalBounds.x - source.x, target.x, target.x + target.width - width),
224+
y: clamp(target.y + normalBounds.y - source.y, target.y, target.y + target.height - height),
225+
width,
226+
height,
227+
};
228+
}
229+
230+
function readDisplayBoundsOrEmpty(): readonly DisplayBounds[] {
231+
try {
232+
return Electron.screen.getAllDisplays().map((display) => display.bounds);
233+
} catch {
234+
return [];
235+
}
236+
}
237+
186238
function windowBoundsEqual(
187239
left: DesktopAppSettings.DesktopWindowBounds,
188240
right: DesktopAppSettings.DesktopWindowBounds,
@@ -458,33 +510,52 @@ export const make = Effect.gen(function* () {
458510
let boundsPersistFiber: Fiber.Fiber<void, never> | undefined;
459511
let pendingBoundsPersistFiber: Fiber.Fiber<void, never> | undefined;
460512
let boundsPersistenceEnabled = persistedBounds === null || restoredPersistedBounds;
461-
const readPersistableBounds = (): DesktopAppSettings.DesktopWindowBounds | null => {
513+
let lastPersistableState: {
514+
readonly bounds: DesktopAppSettings.DesktopWindowBounds;
515+
readonly isMaximized: boolean;
516+
} | null = null;
517+
const readPersistableState = () => {
462518
if (window.isDestroyed()) {
463519
return null;
464520
}
521+
// Minimized windows report stale state on some platforms (macOS: not maximized, the
522+
// pre-move normal frame; Windows: -32000), so they keep the state seen before minimizing.
523+
if (window.isMinimized()) {
524+
return lastPersistableState;
525+
}
526+
const isMaximized = window.isMaximized();
465527
const bounds =
466-
window.isFullScreen() || window.isMaximized() || window.isMinimized()
467-
? window.getNormalBounds()
528+
window.isFullScreen() || isMaximized
529+
? normalBoundsOnWindowDisplay(
530+
window.getNormalBounds(),
531+
window.getBounds(),
532+
readDisplayBoundsOrEmpty(),
533+
)
468534
: window.getBounds();
469-
return DesktopAppSettings.normalizeMainWindowBounds({
535+
const persistableBounds = DesktopAppSettings.normalizeMainWindowBounds({
470536
x: Math.round(bounds.x),
471537
y: Math.round(bounds.y),
472538
width: Math.round(bounds.width),
473539
height: Math.round(bounds.height),
474540
});
541+
lastPersistableState =
542+
persistableBounds === null ? null : { bounds: persistableBounds, isMaximized };
543+
return lastPersistableState;
475544
};
476-
const fallbackWindowBounds = boundsPersistenceEnabled ? null : readPersistableBounds();
545+
const fallbackWindowBounds = boundsPersistenceEnabled
546+
? null
547+
: (readPersistableState()?.bounds ?? null);
477548
const fallbackWindowMaximized = persistedSettings.mainWindowMaximized;
478549
const persistCurrentBounds = (): Fiber.Fiber<void, never> | undefined => {
479550
if (!boundsPersistenceEnabled) {
480551
return pendingBoundsPersistFiber;
481552
}
482-
const bounds = readPersistableBounds();
483-
if (bounds === null) {
553+
const state = readPersistableState();
554+
if (state === null) {
484555
return pendingBoundsPersistFiber;
485556
}
486557
pendingBoundsPersistFiber = runFork(
487-
desktopSettings.setMainWindowBounds(bounds, window.isMaximized()).pipe(
558+
desktopSettings.setMainWindowBounds(state.bounds, state.isMaximized).pipe(
488559
Effect.asVoid,
489560
Effect.catch((error) =>
490561
logWindowWarning("failed to persist main window bounds", {
@@ -496,16 +567,15 @@ export const make = Effect.gen(function* () {
496567
return pendingBoundsPersistFiber;
497568
};
498569
const scheduleBoundsPersist = () => {
499-
if (!boundsPersistenceEnabled) {
500-
const currentBounds = readPersistableBounds();
501-
if (
502-
currentBounds === null ||
570+
const state = readPersistableState();
571+
if (
572+
!boundsPersistenceEnabled &&
573+
(state === null ||
503574
(fallbackWindowBounds !== null &&
504-
windowBoundsEqual(currentBounds, fallbackWindowBounds) &&
505-
window.isMaximized() === fallbackWindowMaximized)
506-
) {
507-
return;
508-
}
575+
windowBoundsEqual(state.bounds, fallbackWindowBounds) &&
576+
state.isMaximized === fallbackWindowMaximized))
577+
) {
578+
return;
509579
}
510580
boundsPersistenceEnabled = true;
511581
if (boundsPersistFiber !== undefined) {

0 commit comments

Comments
 (0)