Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: smooth bounded main window resize

Richie McIlroy73875355f1ad
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 2f1c4af65..c468f7382 100644
--- a/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx
+++ b/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx
@@ -197,21 +197,33 @@ const clamp = (value: number, minimum: number, maximum: number) =>
async function resizeMainWindow(expanded: boolean, animate: boolean) {
const currentWindow = getCurrentWindow();
- const [physicalSize, scaleFactor, physicalPosition, monitor] =
+ const [physicalSize, outerSize, scaleFactor, physicalPosition, monitor] =
await Promise.all([
currentWindow.innerSize(),
+ currentWindow.outerSize(),
currentWindow.scaleFactor(),
currentWindow.outerPosition().catch(() => null),
currentMonitor().catch(() => null),
]);
+ const frameWidth = Math.max(
+ 0,
+ (outerSize.width - physicalSize.width) / scaleFactor,
+ );
+ const frameHeight = Math.max(
+ 0,
+ (outerSize.height - physicalSize.height) / scaleFactor,
+ );
const preferredSize = expanded
? MAIN_WINDOW_SIZE.expanded
: MAIN_WINDOW_SIZE.compact;
const availableWidth = monitor
- ? monitor.workArea.size.width / scaleFactor - MAIN_WINDOW_SCREEN_PADDING * 2
+ ? monitor.workArea.size.width / scaleFactor -
+ frameWidth -
+ MAIN_WINDOW_SCREEN_PADDING * 2
: preferredSize.width;
const availableHeight = monitor
? monitor.workArea.size.height / scaleFactor -
+ frameHeight -
MAIN_WINDOW_SCREEN_PADDING * 2
: preferredSize.height;
const targetWidth = Math.max(
@@ -230,10 +242,64 @@ 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));
} else {
+ let pendingSize: LogicalSize | undefined;
+ let resizeWorker: Promise<void> | undefined;
+ let resizeError: unknown;
+ let resizeFailed = false;
+ const scheduleResize = (size: LogicalSize) => {
+ if (resizeFailed) return;
+ pendingSize = size;
+ if (resizeWorker) return;
+ resizeWorker = (async () => {
+ while (pendingSize) {
+ const nextSize = pendingSize;
+ pendingSize = undefined;
+ await currentWindow.setSize(nextSize);
+ }
+ })()
+ .catch((error) => {
+ resizeFailed = true;
+ resizeError = error;
+ pendingSize = undefined;
+ })
+ .finally(() => {
+ resizeWorker = undefined;
+ });
+ };
const startedAt = performance.now();
let progress = 0;
while (progress < 1) {
@@ -243,39 +309,17 @@ async function resizeMainWindow(expanded: boolean, animate: boolean) {
(performance.now() - startedAt) / MAIN_WINDOW_RESIZE_DURATION,
);
const eased = 1 - (1 - progress) ** 3;
- await currentWindow.setSize(
+ scheduleResize(
new LogicalSize(
startWidth + widthDelta * eased,
startHeight + heightDelta * eased,
),
);
}
+ await resizeWorker;
+ if (resizeFailed) throw resizeError;
}
}
-
- if (!monitor || !physicalPosition) return;
- const padding = MAIN_WINDOW_SCREEN_PADDING * scaleFactor;
- const workArea = monitor.workArea;
- const targetPhysicalWidth = targetWidth * scaleFactor;
- const targetPhysicalHeight = targetHeight * scaleFactor;
- const targetX = clamp(
- physicalPosition.x,
- workArea.position.x + padding,
- workArea.position.x + workArea.size.width - targetPhysicalWidth - padding,
- );
- const targetY = clamp(
- physicalPosition.y,
- workArea.position.y + padding,
- workArea.position.y + workArea.size.height - targetPhysicalHeight - padding,
- );
-
- if (targetX !== physicalPosition.x || targetY !== physicalPosition.y) {
- await currentWindow
- .setPosition(
- new PhysicalPosition(Math.round(targetX), Math.round(targetY)),
- )
- .catch(() => undefined);
- }
}
const recordingDeviceSettingsStore = recordingSettingsStore as unknown as {