Skip to content

Commit a7fe61d

Browse files
fix(desktop): clip annotation captures to visible targets
1 parent 9ee664e commit a7fe61d

2 files changed

Lines changed: 10 additions & 17 deletions

File tree

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,11 @@ const normalizeCaptureRect = (value: unknown): PreviewAnnotationRect | null => {
270270
) {
271271
return null;
272272
}
273-
return {
274-
x: Math.floor(x),
275-
y: Math.floor(y),
276-
width: Math.max(1, Math.ceil(width)),
277-
height: Math.max(1, Math.ceil(height)),
278-
};
273+
return { x, y, width, height };
279274
};
280275

281-
/**
282-
* Crops a viewport capture to a picked annotation. `cropRect` is in CSS px;
283-
* the capture's pixels are CSS px × the guest's `devicePixelRatio`.
284-
*/
276+
// capturePage returns a 1x NativeImage containing device pixels, so crop() uses
277+
// CSS coordinates scaled by the guest's devicePixelRatio (including page zoom).
285278
const cropAnnotationScreenshot = (
286279
image: Electron.NativeImage,
287280
cropRect: PreviewAnnotationRect | null,
@@ -291,17 +284,17 @@ const cropAnnotationScreenshot = (
291284
const full = image.getSize();
292285
if (full.width <= 0 || full.height <= 0) return null;
293286
const scale = Number.isFinite(devicePixelRatio) && devicePixelRatio > 0 ? devicePixelRatio : 1;
294-
// Padding and unions can overlap the viewport without any selected target.
295-
// Check the individual unpadded bounds before accepting even a full capture.
296-
// Zero-size bounds stay eligible: a stroked SVG line reports zero height.
287+
// Padding or a union of offscreen targets can overlap an empty viewport area.
288+
// A zero-width/height SVG line can still paint at the top or left edge.
297289
if (
298290
targetRects.length > 0 &&
299291
!targetRects.some(
300292
(rect) =>
293+
(rect.width > 0 || rect.height > 0) &&
301294
rect.x * scale < full.width &&
302295
rect.y * scale < full.height &&
303-
(rect.x + rect.width) * scale > 0 &&
304-
(rect.y + rect.height) * scale > 0,
296+
(rect.width === 0 ? rect.x >= 0 : rect.x + rect.width > 0) &&
297+
(rect.height === 0 ? rect.y >= 0 : rect.y + rect.height > 0),
305298
)
306299
) {
307300
return null;
@@ -328,6 +321,7 @@ const cropAnnotationScreenshot = (
328321
height: bottom - y,
329322
});
330323
const size = cropped.getSize();
324+
if (size.width <= 0 || size.height <= 0) return null;
331325
return { dataUrl: cropped.toDataURL(), width: size.width, height: size.height, cropRect };
332326
};
333327

‎apps/desktop/src/preview/PickPreload.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ function unionRects(
252252
const top = Math.min(...rects.map((rect) => rect.y));
253253
const right = Math.max(...rects.map((rect) => rect.x + rect.width));
254254
const bottom = Math.max(...rects.map((rect) => rect.y + rect.height));
255-
// Keep offscreen bounds intact; main intersects them with the captured
256-
// viewport instead of moving an invisible selection onto unrelated pixels.
255+
// Main clips to the captured viewport; clipping here would lose offscreen bounds.
257256
return {
258257
x: left - padding,
259258
y: top - padding,

0 commit comments

Comments
 (0)