The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 21:00 UTC
fix(desktop): a maximized window reopens on the display it was last used on (#25833)
Julius Marminge committed
Nov 11, 2026 at 16:07 UTC
cacdbb216316ab7cc91cb81c7ce1198f3a52e3d2
2 files changed
+225
-22
apps/desktop/src/window/DesktopWindow.test.ts
+138
-5
@@ -1023,11 +1023,75 @@ describe("DesktopWindow", () => {
1023
assert.deepEqual(mainWindowBoundsUpdates, [{ x: 220, y: 140, width: 1380, height: 920 }]);
1024
assert.deepEqual(mainWindowMaximizedUpdates, [true]);
1025
assert.equal(fakeWindow.getNormalBounds.mock.calls.length, 1);
1026
- assert.equal(fakeWindow.getBounds.mock.calls.length, 0);
1026
}).pipe(Effect.provide(layer));
1027
}),
1028
);
1029
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
+
1095
it.effect("preserves unrestorable bounds until the user changes the window", () =>
1096
Effect.gen(function* () {
1097
const fakeWindow = makeFakeBrowserWindow();
@@ -1071,8 +1135,7 @@ describe("DesktopWindow", () => {
1135
it.effect("flushes normal bounds when minimized before the debounce completes", () =>
1136
Effect.gen(function* () {
1137
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 });
1139
const createCount = yield* Ref.make(0);
1140
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
1141
const mainWindowBoundsUpdates: DesktopAppSettings.DesktopWindowBounds[] = [];
@@ -1093,17 +1156,87 @@ describe("DesktopWindow", () => {
1156
}
1157
resize();
1158
yield* TestClock.adjust(250);
1159
+ // Windows reports a minimized window at -32000.
1160
fakeWindow.isMinimized.mockReturnValue(true);
1161
+ fakeWindow.getBounds.mockReturnValue({ x: -32_000, y: -32_000, width: 160, height: 28 });
1162
1163
yield* desktopWindow.flushMainWindowBounds;
1164
+ resize();
1165
+ yield* TestClock.adjust(500);
1166
+ yield* Effect.promise(() => Promise.resolve());
1167
1168
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);
1169
}).pipe(Effect.provide(layer));
1170
}),
1171
);
1172
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
+
1240
it.effect("maps macOS horizontal swipes to available history navigation", () =>
1241
Effect.gen(function* () {
1242
const fakeWindow = makeFakeBrowserWindow();
apps/desktop/src/window/DesktopWindow.ts
+87
-17
@@ -183,6 +183,58 @@ function windowFitsWithinDisplay(
183
);
184
}
185
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
+
238
function windowBoundsEqual(
239
left: DesktopAppSettings.DesktopWindowBounds,
240
right: DesktopAppSettings.DesktopWindowBounds,
@@ -458,33 +510,52 @@ export const make = Effect.gen(function* () {
510
let boundsPersistFiber: Fiber.Fiber<void, never> | undefined;
511
let pendingBoundsPersistFiber: Fiber.Fiber<void, never> | undefined;
512
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 = () => {
518
if (window.isDestroyed()) {
519
return null;
520
}
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();
527
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
+ )
534
: window.getBounds();
469
- return DesktopAppSettings.normalizeMainWindowBounds({
535
+ const persistableBounds = DesktopAppSettings.normalizeMainWindowBounds({
536
x: Math.round(bounds.x),
537
y: Math.round(bounds.y),
538
width: Math.round(bounds.width),
539
height: Math.round(bounds.height),
540
});
541
+ lastPersistableState =
542
+ persistableBounds === null ? null : { bounds: persistableBounds, isMaximized };
543
+ return lastPersistableState;
544
};
476
- const fallbackWindowBounds = boundsPersistenceEnabled ? null : readPersistableBounds();
545
+ const fallbackWindowBounds = boundsPersistenceEnabled
546
+ ? null
547
+ : (readPersistableState()?.bounds ?? null);
548
const fallbackWindowMaximized = persistedSettings.mainWindowMaximized;
549
const persistCurrentBounds = (): Fiber.Fiber<void, never> | undefined => {
550
if (!boundsPersistenceEnabled) {
551
return pendingBoundsPersistFiber;
552
}
482
- const bounds = readPersistableBounds();
483
- if (bounds === null) {
553
+ const state = readPersistableState();
554
+ if (state === null) {
555
return pendingBoundsPersistFiber;
556
}
557
pendingBoundsPersistFiber = runFork(
487
- desktopSettings.setMainWindowBounds(bounds, window.isMaximized()).pipe(
558
+ desktopSettings.setMainWindowBounds(state.bounds, state.isMaximized).pipe(
559
Effect.asVoid,
560
Effect.catch((error) =>
561
logWindowWarning("failed to persist main window bounds", {
@@ -496,16 +567,15 @@ export const make = Effect.gen(function* () {
567
return pendingBoundsPersistFiber;
568
};
569
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 ||
574
(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;
579
}
580
boundsPersistenceEnabled = true;
581
if (boundsPersistFiber !== undefined) {