Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: keep main window position after resize

itsanishjainc27bf6bff1d6
diff --git a/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx b/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx
index 7e71765ed..b349eadb6 100644
--- a/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx
+++ b/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx
@@ -120,6 +120,7 @@ import TargetDropdownButton from "./TargetDropdownButton";
import TargetMenuGrid from "./TargetMenuGrid";
import TargetTypeButton from "./TargetTypeButton";
import useRequestPermission from "./useRequestPermission";
+import { getPostResizeWindowPosition } from "./window-geometry";
const MAIN_WINDOW_SIZE = {
compact: { width: 330, height: 395 },
@@ -205,9 +206,6 @@ const listScreenshotsQuery = queryOptions<ScreenshotWithPath[]>({
const nextAnimationFrame = () =>
new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
-const clamp = (value: number, minimum: number, maximum: number) =>
- Math.min(Math.max(value, minimum), maximum);
-
async function resizeMainWindow(expanded: boolean, animate: boolean) {
const currentWindow = getCurrentWindow();
const [physicalSize, outerSize, scaleFactor, physicalPosition, monitor] =
@@ -255,36 +253,6 @@ async function resizeMainWindow(expanded: boolean, animate: boolean) {
"(prefers-reduced-motion: reduce)",
).matches;
- if (monitor && physicalPosition) {
- const padding = MAIN_WINDOW_SCREEN_PADDING * scaleFactor;
- const workArea = monitor.workArea;
- const targetPhysicalWidth = (targetWidth + frameWidth) * scaleFactor;
- const targetPhysicalHeight = (targetHeight + frameHeight) * scaleFactor;
- const minimumX = workArea.position.x + padding;
- const minimumY = workArea.position.y + padding;
- const maximumX = Math.max(
- minimumX,
- workArea.position.x + workArea.size.width - targetPhysicalWidth - padding,
- );
- const maximumY = Math.max(
- minimumY,
- workArea.position.y +
- workArea.size.height -
- targetPhysicalHeight -
- padding,
- );
- const targetX = clamp(physicalPosition.x, minimumX, maximumX);
- const targetY = clamp(physicalPosition.y, minimumY, maximumY);
-
- if (targetX !== physicalPosition.x || targetY !== physicalPosition.y) {
- await currentWindow
- .setPosition(
- new PhysicalPosition(Math.round(targetX), Math.round(targetY)),
- )
- .catch(() => undefined);
- }
- }
-
if (Math.abs(widthDelta) > 0.5 || Math.abs(heightDelta) > 0.5) {
if (!animate || reduceMotion) {
await currentWindow.setSize(new LogicalSize(targetWidth, targetHeight));
@@ -333,6 +301,36 @@ async function resizeMainWindow(expanded: boolean, animate: boolean) {
if (resizeFailed) throw resizeError;
}
}
+
+ if (monitor && physicalPosition) {
+ // AppKit keeps the bottom edge fixed while growing an NSWindow, so restore
+ // the intended top-left position only after the resize finishes.
+ const positionAfterResize = await currentWindow
+ .outerPosition()
+ .catch(() => null);
+ if (!positionAfterResize) return;
+
+ const targetPosition = getPostResizeWindowPosition(
+ physicalPosition,
+ positionAfterResize,
+ {
+ width: (targetWidth + frameWidth) * scaleFactor,
+ height: (targetHeight + frameHeight) * scaleFactor,
+ },
+ monitor.workArea,
+ MAIN_WINDOW_SCREEN_PADDING * scaleFactor,
+ );
+ if (!targetPosition) return;
+
+ await currentWindow
+ .setPosition(
+ new PhysicalPosition(
+ Math.round(targetPosition.x),
+ Math.round(targetPosition.y),
+ ),
+ )
+ .catch(() => undefined);
+ }
}
const recordingDeviceSettingsStore = recordingSettingsStore as unknown as {
diff --git a/apps/desktop/src/routes/(window-chrome)/new-main/window-geometry.test.ts b/apps/desktop/src/routes/(window-chrome)/new-main/window-geometry.test.ts
new file mode 100644
index 000000000..9e0285663
--- /dev/null
+++ b/apps/desktop/src/routes/(window-chrome)/new-main/window-geometry.test.ts
@@ -0,0 +1,48 @@
+import { describe, expect, it } from "vitest";
+import { getPostResizeWindowPosition } from "./window-geometry";
+
+const builtInDisplayWorkArea = {
+ position: { x: 0, y: 0 },
+ size: { width: 1352, height: 848 },
+};
+
+describe("main window geometry", () => {
+ it("restores the top edge after a macOS resize moves it above the display", () => {
+ expect(
+ getPostResizeWindowPosition(
+ { x: 376, y: 100 },
+ { x: 376, y: -165 },
+ { width: 600, height: 660 },
+ builtInDisplayWorkArea,
+ 12,
+ ),
+ ).toEqual({ x: 376, y: 100 });
+ });
+
+ it("keeps the resized window inside a display with a negative origin", () => {
+ expect(
+ getPostResizeWindowPosition(
+ { x: 1700, y: -400 },
+ { x: 1700, y: -400 },
+ { width: 600, height: 660 },
+ {
+ position: { x: 1352, y: -274 },
+ size: { width: 2048, height: 1152 },
+ },
+ 12,
+ ),
+ ).toEqual({ x: 1700, y: -262 });
+ });
+
+ it("does not move a window that stayed at its intended position", () => {
+ expect(
+ getPostResizeWindowPosition(
+ { x: 376, y: 100 },
+ { x: 376, y: 100 },
+ { width: 600, height: 660 },
+ builtInDisplayWorkArea,
+ 12,
+ ),
+ ).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/routes/(window-chrome)/new-main/window-geometry.ts b/apps/desktop/src/routes/(window-chrome)/new-main/window-geometry.ts
new file mode 100644
index 000000000..1f73d9237
--- /dev/null
+++ b/apps/desktop/src/routes/(window-chrome)/new-main/window-geometry.ts
@@ -0,0 +1,49 @@
+type Point = {
+ x: number;
+ y: number;
+};
+
+type Size = {
+ width: number;
+ height: number;
+};
+
+type Rectangle = {
+ position: Point;
+ size: Size;
+};
+
+const clamp = (value: number, minimum: number, maximum: number) =>
+ Math.min(Math.max(value, minimum), maximum);
+
+export function getPostResizeWindowPosition(
+ positionBeforeResize: Point,
+ positionAfterResize: Point,
+ windowSize: Size,
+ workArea: Rectangle,
+ padding: number,
+) {
+ const minimumX = workArea.position.x + padding;
+ const minimumY = workArea.position.y + padding;
+ const maximumX = Math.max(
+ minimumX,
+ workArea.position.x + workArea.size.width - windowSize.width - padding,
+ );
+ const maximumY = Math.max(
+ minimumY,
+ workArea.position.y + workArea.size.height - windowSize.height - padding,
+ );
+ const targetPosition = {
+ x: clamp(positionBeforeResize.x, minimumX, maximumX),
+ y: clamp(positionBeforeResize.y, minimumY, maximumY),
+ };
+
+ if (
+ targetPosition.x === positionAfterResize.x &&
+ targetPosition.y === positionAfterResize.y
+ ) {
+ return null;
+ }
+
+ return targetPosition;
+}