From 135acd34323743c66cc8b8cf5b8485390ac09020 Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Tue, 29 Sep 2026 16:59:53 +0000 Subject: [PATCH 01/24] fix(feedback): shrink oversized screenshots instead of refusing them Fixes #22776 --- .../sidebar/SidebarFeedbackDialog.test.tsx | 57 ++++--- .../use-sidebar-feedback-images.test.tsx | 48 +++++- .../sidebar/use-sidebar-feedback-images.ts | 28 +++- src/renderer/src/i18n/locales/en.json | 3 +- src/renderer/src/i18n/locales/es.json | 3 +- src/renderer/src/i18n/locales/fr.json | 3 +- src/renderer/src/i18n/locales/ja.json | 3 +- src/renderer/src/i18n/locales/ko.json | 3 +- src/renderer/src/i18n/locales/zh.json | 3 +- .../lib/feedback-image-attachments.test.ts | 151 +++++++++++++++++- .../src/lib/feedback-image-attachments.ts | 126 ++++++++++----- .../src/lib/feedback-image-shrink.test.ts | 48 ++++++ src/renderer/src/lib/feedback-image-shrink.ts | 62 +++++++ 13 files changed, 460 insertions(+), 78 deletions(-) create mode 100644 src/renderer/src/lib/feedback-image-shrink.test.ts create mode 100644 src/renderer/src/lib/feedback-image-shrink.ts diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx index 61121936c404..e76590dce146 100644 --- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx +++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx @@ -235,7 +235,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) }) @@ -262,7 +263,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) mocks.submit.mockResolvedValue({ ok: true, imagesDelivered: false }) const onOpenChange = vi.fn() @@ -298,7 +300,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) mocks.submit.mockResolvedValue({ ok: true, @@ -341,7 +344,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) mocks.submit.mockResolvedValue({ ok: true, @@ -379,7 +383,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:shot' } ], - errors: [] + errors: [], + notices: [] }) const { container, unmount } = render() const input = container.querySelector('input[type="file"]') @@ -393,14 +398,16 @@ describe('SidebarFeedbackDialog image submission', () => { expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:shot') }) - it('does not consume text when the pasted image cannot be attached', () => { + it('does not consume text when the pasted image cannot be attached', async () => { mocks.readFeedbackImageFiles.mockResolvedValue({ images: [], - errors: ['huge.png is larger than 4.0 MB.'] + errors: ['huge.gif is larger than 4.0 MB.'], + notices: [] }) render() const textarea = screen.getByPlaceholderText('What could we improve?') - const file = new File(['image'], 'huge.png', { type: 'image/png' }) + // Why: PNGs over budget are shrunk; GIFs are not, so this one stays unattachable. + const file = new File(['image'], 'huge.gif', { type: 'image/gif' }) Object.defineProperty(file, 'size', { value: 4 * 1024 * 1024 + 1 }) const paste = new Event('paste', { bubbles: true, cancelable: true }) Object.defineProperty(paste, 'clipboardData', { @@ -410,7 +417,7 @@ describe('SidebarFeedbackDialog image submission', () => { fireEvent(textarea, paste) expect(paste.defaultPrevented).toBe(false) - expect(mocks.readFeedbackImageFiles).toHaveBeenCalledWith([file], 0, 0) + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledWith([file], 0, 0)) }) // Why: the byte budget, not the count, is what binds after one full-screen @@ -428,7 +435,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:full' } ], - errors: [] + errors: [], + notices: [] }) const { container } = render() const input = container.querySelector('input[type="file"]') @@ -458,7 +466,8 @@ describe('SidebarFeedbackDialog image submission', () => { previewUrl: 'blob:full' } ], - errors: [] + errors: [], + notices: [] }) const { container } = render() expect(screen.getByRole('button', { name: 'Attach' }).hasAttribute('disabled')).toBe(false) @@ -473,21 +482,26 @@ describe('SidebarFeedbackDialog image submission', () => { expect(screen.getByRole('button', { name: 'Attach' }).hasAttribute('disabled')).toBe(true) }) - it('counts committed and in-flight image bytes against the total budget', async () => { - mocks.readFeedbackImageFiles.mockResolvedValueOnce({ + it('sizes each queued batch against what the batches before it committed', async () => { + const committed = (name: string, bytes: number) => ({ images: [ { - id: 'first', - name: 'first.png', + id: name, + name: `${name}.png`, contentType: 'image/png', - bytes: 1000, + bytes, data: new Uint8Array([1]), - previewUrl: 'blob:first' + previewUrl: `blob:${name}` } ], - errors: [] - }) - mocks.readFeedbackImageFiles.mockReturnValue(new Promise(() => {})) + errors: [], + notices: [] + }) + mocks.readFeedbackImageFiles + .mockResolvedValueOnce(committed('first', 1000)) + // A shrunk image commits far fewer bytes than its file size. + .mockResolvedValueOnce(committed('second', 150)) + .mockReturnValue(new Promise(() => {})) const { container } = render() const input = container.querySelector('input[type="file"]') fireEvent.change(input!, { @@ -501,8 +515,9 @@ describe('SidebarFeedbackDialog image submission', () => { fireEvent.change(input!, { target: { files: [second] } }) fireEvent.change(input!, { target: { files: [third] } }) + await waitFor(() => expect(mocks.readFeedbackImageFiles).toHaveBeenCalledTimes(3)) expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(2, [second], 1, 1000) - expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(3, [third], 2, 1200) + expect(mocks.readFeedbackImageFiles).toHaveBeenNthCalledWith(3, [third], 2, 1150) }) it('rejects images added after submission starts instead of clearing them unsent', async () => { diff --git a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.test.tsx b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.test.tsx index 24d92b5d54d2..582688a150c1 100644 --- a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.test.tsx +++ b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.test.tsx @@ -4,6 +4,7 @@ import { act } from 'react' import { createRoot, type Root } from 'react-dom/client' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { FeedbackImageDraft } from '@/lib/feedback-image-attachments' +import { toast } from 'sonner' import { useSidebarFeedbackImages } from './use-sidebar-feedback-images' const { readFeedbackImageFiles } = vi.hoisted(() => ({ readFeedbackImageFiles: vi.fn() })) @@ -13,7 +14,7 @@ vi.mock('@/lib/feedback-image-attachments', async (importOriginal) => ({ readFeedbackImageFiles })) -vi.mock('sonner', () => ({ toast: { warning: vi.fn(), error: vi.fn() } })) +vi.mock('sonner', () => ({ toast: { info: vi.fn(), warning: vi.fn(), error: vi.fn() } })) type HookResult = ReturnType @@ -80,7 +81,7 @@ describe('useSidebarFeedbackImages', () => { }) await act(async () => { - finishFirstRead?.({ images: [draft('first', firstBytes)], errors: [] }) + finishFirstRead?.({ images: [draft('first', firstBytes)], errors: [], notices: [] }) await Promise.resolve() await Promise.resolve() // Still inside act: the first batch has committed to the ref, not to state. @@ -90,4 +91,47 @@ describe('useSidebarFeedbackImages', () => { expect(readFeedbackImageFiles).toHaveBeenNthCalledWith(2, [second], 1, firstBytes) }) + + it('tells the user when an attachment was compressed to fit', async () => { + const notice = 'shot.png was compressed from 6.4 MB to 3.1 MB to fit the attachment limit.' + readFeedbackImageFiles.mockResolvedValue({ + images: [draft('shot', 3_100_000)], + errors: [], + notices: [notice] + }) + + await act(async () => { + latest!.handleAddFiles([new File(['x'], 'shot.png', { type: 'image/png' })]) + }) + + expect(toast.info).toHaveBeenCalledWith(notice) + expect(latest!.images.map((image) => image.bytes)).toEqual([3_100_000]) + }) + + // Why: a batch still shrinking has no known size yet; reserving its raw file + // size would refuse the next screenshot even though room is left. + it('sizes a later add against the shrunk size, not the raw file size', async () => { + let finishShrink: ((value: unknown) => void) | undefined + readFeedbackImageFiles.mockReturnValueOnce( + new Promise((resolve) => { + finishShrink = resolve + }) + ) + readFeedbackImageFiles.mockReturnValue(new Promise(() => {})) + const retina = new File(['x'], 'retina.png', { type: 'image/png' }) + Object.defineProperty(retina, 'size', { value: 6_400_000 }) + const second = new File(['x'], 'second.png', { type: 'image/png' }) + + await act(async () => { + latest!.handleAddFiles([retina]) + latest!.handleAddFiles([second]) + }) + expect(readFeedbackImageFiles).toHaveBeenCalledTimes(1) + + await act(async () => { + finishShrink?.({ images: [draft('retina', 2_100_000)], errors: [], notices: [] }) + }) + + expect(readFeedbackImageFiles).toHaveBeenNthCalledWith(2, [second], 1, 2_100_000) + }) }) diff --git a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts index 641af51c1758..57bca129155a 100644 --- a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts +++ b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts @@ -8,6 +8,10 @@ import { } from '@/lib/feedback-image-attachments' import { useFeedbackImageDrop } from './use-feedback-image-drop' +function sumImageBytes(images: readonly FeedbackImageDraft[]): number { + return images.reduce((total, image) => total + image.bytes, 0) +} + export function useSidebarFeedbackImages(params: { open: boolean isSubmitting: boolean @@ -28,9 +32,13 @@ export function useSidebarFeedbackImages(params: { const [images, setImages] = useState([]) const [pendingImageReadCount, setPendingImageReadCount] = useState(0) const liveImageDraftsRef = useRef([]) - // Why: committed state lags in-flight reads, so batches still being read count - // against capacity — otherwise two quick pastes both see room for four. + // Why: the paste gate answers synchronously, so batches still queued or being + // read count against it — otherwise two quick pastes both see room for four. const pendingImageReadsRef = useRef({ count: 0, bytes: 0 }) + // Why: a shrunk image's size is unknown until it is read, so batches read one + // at a time, each sized against what the batches before it actually committed. + const readQueueRef = useRef>(undefined!) + readQueueRef.current ??= Promise.resolve() const clearImages = useCallback(() => { liveImageDraftsRef.current.forEach(releaseFeedbackImageDraft) @@ -54,7 +62,7 @@ export function useSidebarFeedbackImages(params: { const pendingReads = pendingImageReadsRef.current return { count: liveDrafts.length + pendingReads.count, - bytes: liveDrafts.reduce((total, image) => total + image.bytes, 0) + pendingReads.bytes + bytes: sumImageBytes(liveDrafts) + pendingReads.bytes } }, []) @@ -72,14 +80,17 @@ export function useSidebarFeedbackImages(params: { ) return } - const { count: existingCount, bytes: existingBytes } = getReservedImageCapacity() const pendingReads = pendingImageReadsRef.current const batchBytes = files.reduce((total, file) => total + file.size, 0) pendingReads.count += files.length pendingReads.bytes += batchBytes setPendingImageReadCount((current) => current + files.length) - void readFeedbackImageFiles(files, existingCount, existingBytes).then( - ({ images: added, errors }) => { + const read = readQueueRef.current.then(() => { + const committed = liveImageDraftsRef.current + return readFeedbackImageFiles(files, committed.length, sumImageBytes(committed)) + }) + readQueueRef.current = read.then( + ({ images: added, errors, notices }) => { pendingReads.count -= files.length pendingReads.bytes -= batchBytes if (!params.mountedRef.current) { @@ -91,7 +102,8 @@ export function useSidebarFeedbackImages(params: { liveImageDraftsRef.current = [...liveImageDraftsRef.current, ...added] setImages((existing) => [...existing, ...added]) } - // Why: never drop an attachment without telling the user. + // Why: never drop or degrade an attachment without telling the user. + notices.forEach((notice) => toast.info(notice)) errors.forEach((error) => toast.warning(error)) }, (error: unknown) => { @@ -110,7 +122,7 @@ export function useSidebarFeedbackImages(params: { } ) }, - [getReservedImageCapacity, params.isSubmitting, params.mountedRef] + [params.isSubmitting, params.mountedRef] ) const handleRemoveImage = useCallback((id: string) => { diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 8381a7aa7c8e..c5b2acf30429 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -1057,7 +1057,8 @@ "totalTooLarge": "{{fileName}} would bring the attachments over {{maxSize}} in total.", "dimensionsTooLarge": "{{fileName}} has dimensions that are too large to preview safely.", "invalidImage": "{{fileName}} is not a valid supported image.", - "additionalErrors": "{{count}} additional images could not be attached." + "additionalErrors": "{{count}} additional images could not be attached.", + "compressed": "{{fileName}} was compressed from {{originalSize}} to {{size}} to fit the attachment limit." } } }, diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index bb1ed7c6fe35..02a372395401 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -894,7 +894,8 @@ "dimensionsTooLarge": "{{fileName}} has dimensions that are too large to preview safely.", "invalidImage": "{{fileName}} is not a valid supported image.", "additionalErrors": "{{count}} additional images could not be attached.", - "totalTooLarge": "{{fileName}} superaría el total de {{maxSize}} de archivos adjuntos." + "totalTooLarge": "{{fileName}} superaría el total de {{maxSize}} de archivos adjuntos.", + "compressed": "{{fileName}} se comprimió de {{originalSize}} a {{size}} para ajustarse al límite de archivos adjuntos." } } }, diff --git a/src/renderer/src/i18n/locales/fr.json b/src/renderer/src/i18n/locales/fr.json index e0c6684b9afa..094193faebe6 100644 --- a/src/renderer/src/i18n/locales/fr.json +++ b/src/renderer/src/i18n/locales/fr.json @@ -993,7 +993,8 @@ "dimensionsTooLarge": "{{fileName}} a des dimensions trop grandes pour un aperçu sans risque.", "invalidImage": "{{fileName}} n'est pas une image valide prise en charge.", "additionalErrors": "{{count}} images supplémentaires n'ont pas pu être jointes.", - "totalTooLarge": "{{fileName}} ferait dépasser {{maxSize}} au total pour les pièces jointes." + "totalTooLarge": "{{fileName}} ferait dépasser {{maxSize}} au total pour les pièces jointes.", + "compressed": "{{fileName}} a été compressé de {{originalSize}} à {{size}} pour respecter la limite des pièces jointes." } } }, diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index 421dc263e6b8..b194b1a033a0 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -894,7 +894,8 @@ "dimensionsTooLarge": "{{fileName}} は画像サイズが大きすぎるため、安全にプレビューできません。", "invalidImage": "{{fileName}} は有効な画像として読み取れません。", "additionalErrors": "ほかに {{count}} 件の画像を添付できませんでした。", - "totalTooLarge": "{{fileName}} を追加すると添付ファイルの合計が {{maxSize}} を超えます。" + "totalTooLarge": "{{fileName}} を追加すると添付ファイルの合計が {{maxSize}} を超えます。", + "compressed": "{{fileName}} を添付ファイルの上限に収めるため、{{originalSize}} から {{size}} に圧縮しました。" } } }, diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index afcdc745bd7e..852340d76843 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -894,7 +894,8 @@ "dimensionsTooLarge": "{{fileName}} has dimensions that are too large to preview safely.", "invalidImage": "{{fileName}} is not a valid supported image.", "additionalErrors": "{{count}} additional images could not be attached.", - "totalTooLarge": "{{fileName}}을(를) 포함하면 첨부 파일 합계가 {{maxSize}}를 초과합니다." + "totalTooLarge": "{{fileName}}을(를) 포함하면 첨부 파일 합계가 {{maxSize}}를 초과합니다.", + "compressed": "{{fileName}}을(를) 첨부 파일 한도에 맞추기 위해 {{originalSize}}에서 {{size}}(으)로 압축했습니다." } } }, diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index efcacde838c9..85177f103444 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -894,7 +894,8 @@ "dimensionsTooLarge": "{{fileName}} 的尺寸过大,无法安全预览。", "invalidImage": "{{fileName}} 不是有效的受支持图片。", "additionalErrors": "还有 {{count}} 张图片无法附加。", - "totalTooLarge": "{{fileName}} 将使附件总大小超过 {{maxSize}}。" + "totalTooLarge": "{{fileName}} 将使附件总大小超过 {{maxSize}}。", + "compressed": "已将 {{fileName}} 从 {{originalSize}} 压缩到 {{size}},以符合附件大小限制。" } } }, diff --git a/src/renderer/src/lib/feedback-image-attachments.test.ts b/src/renderer/src/lib/feedback-image-attachments.test.ts index 9413d4ab26cd..632efa7f0683 100644 --- a/src/renderer/src/lib/feedback-image-attachments.test.ts +++ b/src/renderer/src/lib/feedback-image-attachments.test.ts @@ -4,12 +4,21 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { MAX_FEEDBACK_IMAGE_BYTES, MAX_FEEDBACK_IMAGE_COUNT, + MAX_FEEDBACK_IMAGE_SOURCE_BYTES, MAX_FEEDBACK_IMAGE_TOTAL_BYTES, + MIN_FEEDBACK_IMAGE_SHRINK_TARGET_BYTES, hasAttachableFeedbackImage, readFeedbackImageFiles } from './feedback-image-attachments' +const { shrinkFeedbackImage } = vi.hoisted(() => ({ shrinkFeedbackImage: vi.fn() })) + +// Why: happy-dom has no image decoder; the shrink steps are covered in their own test. +vi.mock('./feedback-image-shrink', () => ({ shrinkFeedbackImage })) + beforeEach(() => { + shrinkFeedbackImage.mockReset() + shrinkFeedbackImage.mockResolvedValue(null) let next = 0 URL.createObjectURL = vi.fn(() => `blob:feedback-${(next += 1)}`) URL.revokeObjectURL = vi.fn() @@ -34,6 +43,19 @@ function pngFile(name: string, size = 24, dimensions = { width: 1, height: 1 }): return file } +function gifFile(name: string, size: number): File { + // GIF89a, 1x1 + const file = new File([new Uint8Array([71, 73, 70, 56, 57, 97, 1, 0, 1, 0])], name, { + type: 'image/gif' + }) + Object.defineProperty(file, 'size', { value: size }) + return file +} + +function encoded(size: number, type: string): Blob { + return new Blob([new Uint8Array(size)], { type }) +} + describe('hasAttachableFeedbackImage', () => { it('is true when any file is an allow-listed type', () => { const svg = new File(['x'], 'a.svg', { type: 'image/svg+xml' }) @@ -55,10 +77,31 @@ describe('hasAttachableFeedbackImage', () => { it('is false when supported files cannot pass validation', () => { expect(hasAttachableFeedbackImage([pngFile('empty.png', 0)])).toBe(false) - expect(hasAttachableFeedbackImage([pngFile('huge.png', MAX_FEEDBACK_IMAGE_BYTES + 1)])).toBe( + expect( + hasAttachableFeedbackImage([pngFile('huge.png', MAX_FEEDBACK_IMAGE_SOURCE_BYTES + 1)]) + ).toBe(false) + expect(hasAttachableFeedbackImage([gifFile('anim.gif', MAX_FEEDBACK_IMAGE_BYTES + 1)])).toBe( false ) expect(hasAttachableFeedbackImage([pngFile('a.png')], MAX_FEEDBACK_IMAGE_COUNT)).toBe(false) + expect(hasAttachableFeedbackImage([pngFile('a.png')], 1, MAX_FEEDBACK_IMAGE_TOTAL_BYTES)).toBe( + false + ) + }) + + // Why: the paste gate must mirror the shrink rule, or a pasted screenshot that + // would have been compressed falls through to the textarea and is lost. + // Why: consuming the paste would drop co-pasted text for an image that cannot fit anyway. + it('is false when too little budget is left to shrink into', () => { + const almostSpent = MAX_FEEDBACK_IMAGE_TOTAL_BYTES - MIN_FEEDBACK_IMAGE_SHRINK_TARGET_BYTES + 1 + expect(hasAttachableFeedbackImage([pngFile('retina.png', 6_400_000)], 1, almostSpent)).toBe( + false + ) + }) + + it('is true for an oversized screenshot that can be shrunk into the space left', () => { + expect(hasAttachableFeedbackImage([pngFile('retina.png', 6_400_000)])).toBe(true) + expect(hasAttachableFeedbackImage([pngFile('second.png', 2_000_000)], 1, 3_000_000)).toBe(true) }) }) @@ -95,7 +138,111 @@ describe('readFeedbackImageFiles', () => { expect(errors.at(-1)).toBe('96 additional images could not be attached.') }) - it('reports an oversized image instead of skipping it', async () => { + it('shrinks an oversized screenshot to fit instead of refusing it', async () => { + const file = pngFile('retina.png', 6_400_000) + shrinkFeedbackImage.mockResolvedValue(encoded(3_000_000, 'image/png')) + + const { images, errors, notices } = await readFeedbackImageFiles([file], 0) + + expect(errors).toEqual([]) + expect(shrinkFeedbackImage).toHaveBeenCalledWith(file, MAX_FEEDBACK_IMAGE_BYTES) + expect(images).toHaveLength(1) + expect(images[0]).toMatchObject({ name: 'retina.png', contentType: 'image/png', bytes: 3e6 }) + expect(images[0].data.byteLength).toBe(3_000_000) + expect(notices).toEqual([ + 'retina.png was compressed from 6.1 MB to 2.9 MB to fit the attachment limit.' + ]) + }) + + it('shrinks a second screenshot into the budget the first one left', async () => { + const first = pngFile('first.png', 3_000_000) + const second = pngFile('second.png', 3_000_000) + shrinkFeedbackImage.mockResolvedValue(encoded(900_000, 'image/jpeg')) + + const { images, errors } = await readFeedbackImageFiles([first, second], 0) + + expect(errors).toEqual([]) + expect(shrinkFeedbackImage).toHaveBeenCalledTimes(1) + expect(shrinkFeedbackImage).toHaveBeenCalledWith( + second, + MAX_FEEDBACK_IMAGE_TOTAL_BYTES - 3_000_000 + ) + expect(images.map((image) => [image.name, image.contentType, image.bytes])).toEqual([ + ['first.png', 'image/png', 3_000_000], + ['second.png', 'image/jpeg', 900_000] + ]) + }) + + it('refuses without shrinking when too little budget is left to shrink into', async () => { + const almostSpent = MAX_FEEDBACK_IMAGE_TOTAL_BYTES - MIN_FEEDBACK_IMAGE_SHRINK_TARGET_BYTES + 1 + + const { images, errors } = await readFeedbackImageFiles( + [pngFile('second.png', 2_000_000)], + 1, + almostSpent + ) + + expect(shrinkFeedbackImage).not.toHaveBeenCalled() + expect(images).toEqual([]) + expect(errors).toEqual(['second.png would bring the attachments over 4.0 MB in total.']) + }) + + it('does not re-encode an image that already fits', async () => { + const { images, notices } = await readFeedbackImageFiles([pngFile('small.png', 1024)], 0) + + expect(shrinkFeedbackImage).not.toHaveBeenCalled() + expect(images[0].bytes).toBe(1024) + expect(notices).toEqual([]) + }) + + it('refuses an oversized GIF or WebP rather than flattening its animation', async () => { + const webp = new File(['x'], 'anim.webp', { type: 'image/webp' }) + Object.defineProperty(webp, 'size', { value: MAX_FEEDBACK_IMAGE_BYTES + 1 }) + + const { images, errors } = await readFeedbackImageFiles( + [gifFile('anim.gif', MAX_FEEDBACK_IMAGE_BYTES + 1), webp], + 0 + ) + + expect(shrinkFeedbackImage).not.toHaveBeenCalled() + expect(images).toEqual([]) + expect(errors).toEqual(['anim.gif is larger than 4.0 MB.', 'anim.webp is larger than 4.0 MB.']) + }) + + it('refuses an oversized animated PNG rather than flattening it', async () => { + const apng = new File([pngHeader(), new TextEncoder().encode('....acTL....IDAT')], 'anim.png', { + type: 'image/png' + }) + Object.defineProperty(apng, 'size', { value: MAX_FEEDBACK_IMAGE_BYTES + 1 }) + + const { images, errors } = await readFeedbackImageFiles([apng], 0) + + expect(shrinkFeedbackImage).not.toHaveBeenCalled() + expect(images).toEqual([]) + expect(errors).toEqual(['anim.png is larger than 4.0 MB.']) + }) + + it('refuses a file too large to read before shrinking', async () => { + const file = pngFile('enormous.png', MAX_FEEDBACK_IMAGE_SOURCE_BYTES + 1) + file.arrayBuffer = vi.fn() + + const { images, errors } = await readFeedbackImageFiles([file], 0) + + expect(file.arrayBuffer).not.toHaveBeenCalled() + expect(images).toEqual([]) + expect(errors).toEqual(['enormous.png is larger than 4.0 MB.']) + }) + + it('reports an image the browser could not decode for shrinking', async () => { + shrinkFeedbackImage.mockRejectedValue(new Error('decode failed')) + + const { images, errors } = await readFeedbackImageFiles([pngFile('retina.png', 6_400_000)], 0) + + expect(images).toEqual([]) + expect(errors).toEqual(['retina.png is larger than 4.0 MB.']) + }) + + it('reports an oversized image that could not be shrunk enough', async () => { const { images, errors } = await readFeedbackImageFiles( [pngFile('huge.png', MAX_FEEDBACK_IMAGE_BYTES + 1)], 0 diff --git a/src/renderer/src/lib/feedback-image-attachments.ts b/src/renderer/src/lib/feedback-image-attachments.ts index d444fe7950d6..5607bff81637 100644 --- a/src/renderer/src/lib/feedback-image-attachments.ts +++ b/src/renderer/src/lib/feedback-image-attachments.ts @@ -10,6 +10,7 @@ import { MAX_FEEDBACK_IMAGE_COUNT, MAX_FEEDBACK_IMAGE_TOTAL_BYTES } from '../../../shared/feedback-image-limits' +import { shrinkFeedbackImage } from './feedback-image-shrink' export { MAX_FEEDBACK_IMAGE_BYTES, @@ -26,6 +27,11 @@ export const SUPPORTED_FEEDBACK_IMAGE_TYPES = [ export const FEEDBACK_IMAGE_FILE_ACCEPT = SUPPORTED_FEEDBACK_IMAGE_TYPES.join(',') const MAX_FEEDBACK_IMAGE_DETAIL_ERRORS = 4 +// Why: an oversized image is read whole before it can be shrunk, so cap the read. +export const MAX_FEEDBACK_IMAGE_SOURCE_BYTES = 32 * 1024 * 1024 +// Why: the smallest shrink step of a full-screen capture is ~200 KB, so below +// this shrinking only burns encodes before refusing. +export const MIN_FEEDBACK_IMAGE_SHRINK_TARGET_BYTES = 64 * 1024 export type FeedbackImageDraft = { id: string @@ -41,25 +47,48 @@ function isSupportedType(contentType: string): boolean { return (SUPPORTED_FEEDBACK_IMAGE_TYPES as readonly string[]).includes(contentType) } +function feedbackImageFitBytes(remainingBytes: number): number { + return Math.min(MAX_FEEDBACK_IMAGE_BYTES, remainingBytes) +} + +// Why: APNG's acTL chunk precedes its image data; shrinking would keep only frame one. +function isAnimatedPng(data: Uint8Array): boolean { + return new TextDecoder('latin1').decode(data.subarray(0, 64 * 1024)).includes('acTL') +} + +/** Fits as-is, or can be shrunk into the space left instead of refused. */ +function canAttachWithin(file: File, fitBytes: number): boolean { + if (file.size <= fitBytes) { + return true + } + // Why: re-encoding flattens an animated GIF or WebP, losing what it was attached to show. + return ( + fitBytes >= MIN_FEEDBACK_IMAGE_SHRINK_TARGET_BYTES && + file.type !== 'image/gif' && + file.type !== 'image/webp' && + file.size <= MAX_FEEDBACK_IMAGE_SOURCE_BYTES + ) +} + /** * Whether a paste should be consumed. Extraction stays broad so unsupported * image types still reach the rejection toast, but swallowing the paste when * nothing is attachable would also discard any text riding along on the - * clipboard. Every limit readFeedbackImageFiles enforces has to be mirrored - * here, or a doomed paste eats the co-pasted text on its way to a rejection. + * clipboard. Every limit readFeedbackImageFiles checks from a file's type and + * size is mirrored here. Checks that need its bytes (dimensions, APNG, whether a + * shrink fits) cannot run synchronously, so a paste they refuse loses its text. */ export function hasAttachableFeedbackImage( files: readonly File[], existingCount = 0, existingBytes = 0 ): boolean { - const remainingBytes = Math.min( - MAX_FEEDBACK_IMAGE_BYTES, - MAX_FEEDBACK_IMAGE_TOTAL_BYTES - existingBytes - ) + const fitBytes = feedbackImageFitBytes(MAX_FEEDBACK_IMAGE_TOTAL_BYTES - existingBytes) return ( existingCount < MAX_FEEDBACK_IMAGE_COUNT && - files.some((file) => isSupportedType(file.type) && file.size > 0 && file.size <= remainingBytes) + files.some( + (file) => isSupportedType(file.type) && file.size > 0 && canAttachWithin(file, fitBytes) + ) ) } @@ -80,17 +109,18 @@ function feedbackImageDisplayName(file: File): string { } /** - * Converts picked/pasted/dropped files into drafts. Rejections come back as - * messages rather than being skipped, because silently dropping an attachment - * is the exact failure this feature exists to fix. + * Converts picked/pasted/dropped files into drafts, shrinking any that would not + * fit. Rejections and compressions come back as messages, because a silently + * dropped or degraded attachment is the exact failure this feature exists to fix. */ export async function readFeedbackImageFiles( files: readonly File[], existingCount: number, existingBytes = 0 -): Promise<{ images: FeedbackImageDraft[]; errors: string[] }> { +): Promise<{ images: FeedbackImageDraft[]; errors: string[]; notices: string[] }> { const images: FeedbackImageDraft[] = [] const errors: string[] = [] + const notices: string[] = [] let remaining = MAX_FEEDBACK_IMAGE_COUNT - existingCount let remainingBytes = MAX_FEEDBACK_IMAGE_TOTAL_BYTES - existingBytes let omittedErrorCount = 0 @@ -123,19 +153,6 @@ export async function readFeedbackImageFiles( ) continue } - if (file.size > MAX_FEEDBACK_IMAGE_BYTES) { - addError(() => - translate( - 'auto.lib.feedback.image.attachments.tooLarge', - '{{fileName}} is larger than {{maxSize}}.', - { - fileName, - maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_BYTES) - } - ) - ) - continue - } if (remaining <= 0) { addError(() => translate( @@ -146,20 +163,26 @@ export async function readFeedbackImageFiles( ) break } - if (file.size > remainingBytes) { + const addSizeError = (): void => addError(() => - translate( - 'auto.lib.feedback.image.attachments.totalTooLarge', - '{{fileName}} would bring the attachments over {{maxSize}} in total.', - { - fileName, - maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_TOTAL_BYTES) - } - ) + file.size > MAX_FEEDBACK_IMAGE_BYTES + ? translate( + 'auto.lib.feedback.image.attachments.tooLarge', + '{{fileName}} is larger than {{maxSize}}.', + { fileName, maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_BYTES) } + ) + : translate( + 'auto.lib.feedback.image.attachments.totalTooLarge', + '{{fileName}} would bring the attachments over {{maxSize}} in total.', + { fileName, maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_TOTAL_BYTES) } + ) ) + const fitBytes = feedbackImageFitBytes(remainingBytes) + if (!canAttachWithin(file, fitBytes)) { + addSizeError() continue } - const data = new Uint8Array(await file.arrayBuffer()) + let data = new Uint8Array(await file.arrayBuffer()) try { assertRasterImagePreviewWithinLimits(data, file.type) } catch (error) { @@ -185,17 +208,42 @@ export async function readFeedbackImageFiles( } throw error } + let image: Blob = file + if (file.size > fitBytes) { + // Why: runs after the dimension check above, which bounds the decode. + const shrunk = + file.type === 'image/png' && isAnimatedPng(data) + ? null + : await shrinkFeedbackImage(file, fitBytes).catch(() => null) + if (!shrunk) { + addSizeError() + continue + } + image = shrunk + data = new Uint8Array(await shrunk.arrayBuffer()) + notices.push( + translate( + 'auto.lib.feedback.image.attachments.compressed', + '{{fileName}} was compressed from {{originalSize}} to {{size}} to fit the attachment limit.', + { + fileName, + originalSize: formatFeedbackImageSize(file.size), + size: formatFeedbackImageSize(shrunk.size) + } + ) + ) + } remaining -= 1 - remainingBytes -= file.size + remainingBytes -= image.size images.push({ // Why: crypto.randomUUID is undefined in non-secure browser contexts (LAN // web client over plain HTTP); createBrowserUuid falls back safely. id: `${file.name}-${file.size}-${createBrowserUuid()}`, name: fileName, - contentType: file.type, - bytes: file.size, + contentType: image.type, + bytes: image.size, data, - previewUrl: URL.createObjectURL(file) + previewUrl: URL.createObjectURL(image) }) } } catch (error) { @@ -215,7 +263,7 @@ export async function readFeedbackImageFiles( ) } - return { images, errors } + return { images, errors, notices } } export function extractImageFilesFromDataTransfer(data: DataTransfer | null): File[] { diff --git a/src/renderer/src/lib/feedback-image-shrink.test.ts b/src/renderer/src/lib/feedback-image-shrink.test.ts new file mode 100644 index 000000000000..41f5d5a00284 --- /dev/null +++ b/src/renderer/src/lib/feedback-image-shrink.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from 'vitest' +import { shrinkFeedbackImageWithin, type FeedbackImageShrinkStep } from './feedback-image-shrink' + +function encoderBySize(sizeFor: (step: FeedbackImageShrinkStep) => number | null): { + encode: (step: FeedbackImageShrinkStep) => Promise + tried: FeedbackImageShrinkStep[] +} { + const tried: FeedbackImageShrinkStep[] = [] + return { + tried, + encode: async (step) => { + tried.push(step) + const size = sizeFor(step) + return size === null ? null : new Blob([new Uint8Array(size)], { type: step.contentType }) + } + } +} + +describe('shrinkFeedbackImageWithin', () => { + it('stops at the first encoding that fits', async () => { + const { encode, tried } = encoderBySize((step) => + step.contentType === 'image/png' && step.scale === 0.5 ? 900 : 5000 + ) + + const blob = await shrinkFeedbackImageWithin(encode, 1000) + + expect(blob?.size).toBe(900) + expect(blob?.type).toBe('image/png') + expect(tried.at(-1)).toEqual({ scale: 0.5, contentType: 'image/png' }) + }) + + // Why: screenshots are mostly text, so every PNG size is tried before any JPEG. + it('falls back to JPEG only after every PNG size is too big', async () => { + const { encode, tried } = encoderBySize((step) => + step.contentType === 'image/jpeg' ? 800 : 5000 + ) + + const blob = await shrinkFeedbackImageWithin(encode, 1000) + + expect(blob?.type).toBe('image/jpeg') + expect(tried.map((step) => step.contentType)).toEqual(['image/png', 'image/png', 'image/jpeg']) + }) + + it('returns null when nothing fits or the browser cannot encode', async () => { + expect(await shrinkFeedbackImageWithin(encoderBySize(() => 5000).encode, 1000)).toBeNull() + expect(await shrinkFeedbackImageWithin(encoderBySize(() => null).encode, 1000)).toBeNull() + }) +}) diff --git a/src/renderer/src/lib/feedback-image-shrink.ts b/src/renderer/src/lib/feedback-image-shrink.ts new file mode 100644 index 000000000000..9ec37664e504 --- /dev/null +++ b/src/renderer/src/lib/feedback-image-shrink.ts @@ -0,0 +1,62 @@ +export type FeedbackImageShrinkStep = { + scale: number + contentType: 'image/png' | 'image/jpeg' +} + +// Why: screenshots are mostly text, which JPEG smears, so every PNG size is +// tried before the first lossy one. Retina captures stay readable at 0.5. +const FEEDBACK_IMAGE_SHRINK_STEPS: readonly FeedbackImageShrinkStep[] = [ + { scale: 0.75, contentType: 'image/png' }, + { scale: 0.5, contentType: 'image/png' }, + { scale: 1, contentType: 'image/jpeg' }, + { scale: 0.75, contentType: 'image/jpeg' }, + { scale: 0.5, contentType: 'image/jpeg' }, + { scale: 0.25, contentType: 'image/jpeg' } +] + +const FEEDBACK_IMAGE_JPEG_QUALITY = 0.85 + +/** Returns the first step's encoding that fits, or null when none does. */ +export async function shrinkFeedbackImageWithin( + // Encodes the decoded image at one step; null when the browser cannot encode it. + encode: (step: FeedbackImageShrinkStep) => Promise, + maxBytes: number +): Promise { + for (const step of FEEDBACK_IMAGE_SHRINK_STEPS) { + const blob = await encode(step) + if (blob && blob.size <= maxBytes) { + return blob + } + } + return null +} + +function encodeBitmap(bitmap: ImageBitmap, step: FeedbackImageShrinkStep): Promise { + const canvas = document.createElement('canvas') + canvas.width = Math.max(1, Math.floor(bitmap.width * step.scale)) + canvas.height = Math.max(1, Math.floor(bitmap.height * step.scale)) + const context = canvas.getContext('2d') + if (!context) { + return Promise.resolve(null) + } + if (step.contentType === 'image/jpeg') { + // Why: JPEG has no alpha, and transparent pixels would otherwise encode black. + context.fillStyle = '#fff' + context.fillRect(0, 0, canvas.width, canvas.height) + } + context.drawImage(bitmap, 0, 0, canvas.width, canvas.height) + // Why: toBlob encodes off the renderer thread; toDataURL would freeze the dialog. + return new Promise((resolve) => + canvas.toBlob(resolve, step.contentType, FEEDBACK_IMAGE_JPEG_QUALITY) + ) +} + +/** Decodes once and re-encodes smaller until it fits; null when nothing fits. */ +export async function shrinkFeedbackImage(image: Blob, maxBytes: number): Promise { + const bitmap = await createImageBitmap(image) + try { + return await shrinkFeedbackImageWithin((step) => encodeBitmap(bitmap, step), maxBytes) + } finally { + bitmap.close() + } +} From ad6e3532cd4a08bf772d80d4ce3ac022b48a44ad Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 30 Sep 2026 23:59:41 -0700 Subject: [PATCH 02/24] fix(feedback): release each shrink canvas and skip the PNG ladder for a JPEG Two defects in the attach-time shrink ladder: - Every step allocated a fresh full-size canvas and left it for the garbage collector, so a 32-megapixel source could keep six of them alive at once. Past the renderer's canvas memory budget Chromium hands back a blank canvas, which would upload as a blank screenshot with nothing to notice it. Each step's canvas is now released once toBlob has answered. - A JPEG source ran two full-size PNG encodes first. A PNG of decoded JPEG noise is many times larger than the source it has to undercut, so those steps could only ever burn time and a large buffer before the first JPEG attempt. A lossy source now starts at the JPEG steps. --- .../src/lib/feedback-image-shrink.test.ts | 45 ++++++++++-- src/renderer/src/lib/feedback-image-shrink.ts | 71 +++++++++++++------ 2 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/renderer/src/lib/feedback-image-shrink.test.ts b/src/renderer/src/lib/feedback-image-shrink.test.ts index 41f5d5a00284..7a302ec24bea 100644 --- a/src/renderer/src/lib/feedback-image-shrink.test.ts +++ b/src/renderer/src/lib/feedback-image-shrink.test.ts @@ -1,5 +1,9 @@ import { describe, expect, it } from 'vitest' -import { shrinkFeedbackImageWithin, type FeedbackImageShrinkStep } from './feedback-image-shrink' +import { + feedbackImageShrinkSteps, + shrinkFeedbackImageWithin, + type FeedbackImageShrinkStep +} from './feedback-image-shrink' function encoderBySize(sizeFor: (step: FeedbackImageShrinkStep) => number | null): { encode: (step: FeedbackImageShrinkStep) => Promise @@ -16,13 +20,40 @@ function encoderBySize(sizeFor: (step: FeedbackImageShrinkStep) => number | null } } +const pngSteps = feedbackImageShrinkSteps('image/png') + +describe('feedbackImageShrinkSteps', () => { + it('tries every PNG size before the first lossy one for a PNG source', () => { + expect(pngSteps.map((step) => step.contentType)).toEqual([ + 'image/png', + 'image/png', + 'image/jpeg', + 'image/jpeg', + 'image/jpeg', + 'image/jpeg' + ]) + }) + + // Why: a PNG of decoded JPEG noise is larger than the source it must undercut, + // so those two full-size encodes could only ever waste time and memory. + it('skips the PNG sizes for an already-lossy JPEG source', () => { + expect( + feedbackImageShrinkSteps('image/jpeg').every((step) => step.contentType === 'image/jpeg') + ).toBe(true) + }) + + it('keeps every step a downscale or a same-size re-encode', () => { + expect(pngSteps.every((step) => step.scale > 0 && step.scale <= 1)).toBe(true) + }) +}) + describe('shrinkFeedbackImageWithin', () => { it('stops at the first encoding that fits', async () => { const { encode, tried } = encoderBySize((step) => step.contentType === 'image/png' && step.scale === 0.5 ? 900 : 5000 ) - const blob = await shrinkFeedbackImageWithin(encode, 1000) + const blob = await shrinkFeedbackImageWithin(pngSteps, encode, 1000) expect(blob?.size).toBe(900) expect(blob?.type).toBe('image/png') @@ -35,14 +66,18 @@ describe('shrinkFeedbackImageWithin', () => { step.contentType === 'image/jpeg' ? 800 : 5000 ) - const blob = await shrinkFeedbackImageWithin(encode, 1000) + const blob = await shrinkFeedbackImageWithin(pngSteps, encode, 1000) expect(blob?.type).toBe('image/jpeg') expect(tried.map((step) => step.contentType)).toEqual(['image/png', 'image/png', 'image/jpeg']) }) it('returns null when nothing fits or the browser cannot encode', async () => { - expect(await shrinkFeedbackImageWithin(encoderBySize(() => 5000).encode, 1000)).toBeNull() - expect(await shrinkFeedbackImageWithin(encoderBySize(() => null).encode, 1000)).toBeNull() + expect( + await shrinkFeedbackImageWithin(pngSteps, encoderBySize(() => 5000).encode, 1000) + ).toBeNull() + expect( + await shrinkFeedbackImageWithin(pngSteps, encoderBySize(() => null).encode, 1000) + ).toBeNull() }) }) diff --git a/src/renderer/src/lib/feedback-image-shrink.ts b/src/renderer/src/lib/feedback-image-shrink.ts index 9ec37664e504..b2c9c867b553 100644 --- a/src/renderer/src/lib/feedback-image-shrink.ts +++ b/src/renderer/src/lib/feedback-image-shrink.ts @@ -5,9 +5,12 @@ export type FeedbackImageShrinkStep = { // Why: screenshots are mostly text, which JPEG smears, so every PNG size is // tried before the first lossy one. Retina captures stay readable at 0.5. -const FEEDBACK_IMAGE_SHRINK_STEPS: readonly FeedbackImageShrinkStep[] = [ +const FEEDBACK_IMAGE_PNG_SHRINK_STEPS: readonly FeedbackImageShrinkStep[] = [ { scale: 0.75, contentType: 'image/png' }, - { scale: 0.5, contentType: 'image/png' }, + { scale: 0.5, contentType: 'image/png' } +] + +const FEEDBACK_IMAGE_JPEG_SHRINK_STEPS: readonly FeedbackImageShrinkStep[] = [ { scale: 1, contentType: 'image/jpeg' }, { scale: 0.75, contentType: 'image/jpeg' }, { scale: 0.5, contentType: 'image/jpeg' }, @@ -16,13 +19,25 @@ const FEEDBACK_IMAGE_SHRINK_STEPS: readonly FeedbackImageShrinkStep[] = [ const FEEDBACK_IMAGE_JPEG_QUALITY = 0.85 +/** + * Why: a PNG of an already-lossy source re-encodes JPEG noise losslessly, so it + * runs many times larger than the source it has to undercut — a multi-megapixel + * photo would burn two full-size PNG encodes and their buffers to never fit. + */ +export function feedbackImageShrinkSteps(sourceType: string): readonly FeedbackImageShrinkStep[] { + return sourceType === 'image/jpeg' + ? FEEDBACK_IMAGE_JPEG_SHRINK_STEPS + : [...FEEDBACK_IMAGE_PNG_SHRINK_STEPS, ...FEEDBACK_IMAGE_JPEG_SHRINK_STEPS] +} + /** Returns the first step's encoding that fits, or null when none does. */ export async function shrinkFeedbackImageWithin( + steps: readonly FeedbackImageShrinkStep[], // Encodes the decoded image at one step; null when the browser cannot encode it. encode: (step: FeedbackImageShrinkStep) => Promise, maxBytes: number ): Promise { - for (const step of FEEDBACK_IMAGE_SHRINK_STEPS) { + for (const step of steps) { const blob = await encode(step) if (blob && blob.size <= maxBytes) { return blob @@ -31,31 +46,47 @@ export async function shrinkFeedbackImageWithin( return null } -function encodeBitmap(bitmap: ImageBitmap, step: FeedbackImageShrinkStep): Promise { +async function encodeBitmap( + bitmap: ImageBitmap, + step: FeedbackImageShrinkStep +): Promise { const canvas = document.createElement('canvas') - canvas.width = Math.max(1, Math.floor(bitmap.width * step.scale)) - canvas.height = Math.max(1, Math.floor(bitmap.height * step.scale)) - const context = canvas.getContext('2d') - if (!context) { - return Promise.resolve(null) - } - if (step.contentType === 'image/jpeg') { - // Why: JPEG has no alpha, and transparent pixels would otherwise encode black. - context.fillStyle = '#fff' - context.fillRect(0, 0, canvas.width, canvas.height) + try { + canvas.width = Math.max(1, Math.floor(bitmap.width * step.scale)) + canvas.height = Math.max(1, Math.floor(bitmap.height * step.scale)) + const context = canvas.getContext('2d') + if (!context) { + return null + } + if (step.contentType === 'image/jpeg') { + // Why: JPEG has no alpha, and transparent pixels would otherwise encode black. + context.fillStyle = '#fff' + context.fillRect(0, 0, canvas.width, canvas.height) + } + context.drawImage(bitmap, 0, 0, canvas.width, canvas.height) + // Why: toBlob encodes off the renderer thread; toDataURL would freeze the dialog. + return await new Promise((resolve) => + canvas.toBlob(resolve, step.contentType, FEEDBACK_IMAGE_JPEG_QUALITY) + ) + } finally { + // Why: toBlob snapshots the backing store, so once it has answered the step's + // canvas can go. Holding six full-size ones until GC runs risks tripping the + // renderer's canvas memory budget, which hands back a blank canvas — and a + // blank screenshot would upload without anything noticing. + canvas.width = 0 + canvas.height = 0 } - context.drawImage(bitmap, 0, 0, canvas.width, canvas.height) - // Why: toBlob encodes off the renderer thread; toDataURL would freeze the dialog. - return new Promise((resolve) => - canvas.toBlob(resolve, step.contentType, FEEDBACK_IMAGE_JPEG_QUALITY) - ) } /** Decodes once and re-encodes smaller until it fits; null when nothing fits. */ export async function shrinkFeedbackImage(image: Blob, maxBytes: number): Promise { const bitmap = await createImageBitmap(image) try { - return await shrinkFeedbackImageWithin((step) => encodeBitmap(bitmap, step), maxBytes) + return await shrinkFeedbackImageWithin( + feedbackImageShrinkSteps(image.type), + (step) => encodeBitmap(bitmap, step), + maxBytes + ) } finally { bitmap.close() } From 87386fd40f8e6b5107cd10fd4439f0ecd8c5eec5 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 30 Sep 2026 23:59:44 -0700 Subject: [PATCH 03/24] fix(feedback): keep the image read queue usable after a settle callback throws MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The queue tail is what the next batch awaits. A throw in either settle callback left it rejected, so every later attach short-circuited into "Could not read the attached images" without reading anything — and the rejection went unhandled. --- .../src/components/sidebar/use-sidebar-feedback-images.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts index 57bca129155a..cbc6bd349d25 100644 --- a/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts +++ b/src/renderer/src/components/sidebar/use-sidebar-feedback-images.ts @@ -89,7 +89,7 @@ export function useSidebarFeedbackImages(params: { const committed = liveImageDraftsRef.current return readFeedbackImageFiles(files, committed.length, sumImageBytes(committed)) }) - readQueueRef.current = read.then( + const settled = read.then( ({ images: added, errors, notices }) => { pendingReads.count -= files.length pendingReads.bytes -= batchBytes @@ -121,6 +121,11 @@ export function useSidebarFeedbackImages(params: { } } ) + // Why: the tail is what the next batch waits on, so a throw inside either + // callback would leave it rejected and report every later attach as unreadable. + readQueueRef.current = settled.catch((error: unknown) => { + console.error('Failed to settle a feedback image batch:', error) + }) }, [params.isSubmitting, params.mountedRef] ) From 5109254993c069f829d6476169ad60e887a38862 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 30 Sep 2026 23:59:46 -0700 Subject: [PATCH 04/24] feat(feedback): say when an attachment is still being prepared MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Shrinking a full-screen capture takes long enough that the gap between picking the file and its thumbnail appearing reads as a dropped attachment: the only other signal was the Send button quietly staying disabled. The attachment hint now reads "Preparing attachments…" while a batch is in flight. --- .../sidebar/SidebarFeedbackDialog.test.tsx | 3 +++ .../sidebar/SidebarFeedbackDialog.tsx | 1 + .../SidebarFeedbackImageAttachments.tsx | 26 +++++++++++++------ src/renderer/src/i18n/locales/en.json | 1 + src/renderer/src/i18n/locales/es.json | 1 + src/renderer/src/i18n/locales/fr.json | 1 + src/renderer/src/i18n/locales/ja.json | 1 + src/renderer/src/i18n/locales/ko.json | 1 + src/renderer/src/i18n/locales/zh.json | 1 + 9 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx index e76590dce146..c74149d3062a 100644 --- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx +++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.test.tsx @@ -222,6 +222,8 @@ describe('SidebarFeedbackDialog image submission', () => { expect((send as HTMLButtonElement).disabled).toBe(true) fireEvent.click(send) expect(mocks.submit).not.toHaveBeenCalled() + // Why: a shrink can run for a while, and no thumbnail yet reads as a dropped file. + expect(screen.getByText('Preparing attachments…')).not.toBeNull() await act(async () => { finishRead?.({ @@ -241,6 +243,7 @@ describe('SidebarFeedbackDialog image submission', () => { }) await waitFor(() => expect((send as HTMLButtonElement).disabled).toBe(false)) + expect(screen.queryByText('Preparing attachments…')).toBeNull() const remove = screen.getByRole('button', { name: 'Remove shot.png' }) expect(remove.dataset.slot).toBe('button') expect(remove.dataset.size).toBe('icon-xs') diff --git a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx index 9224d3f85bd5..deb113a1ebb8 100644 --- a/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx +++ b/src/renderer/src/components/sidebar/SidebarFeedbackDialog.tsx @@ -352,6 +352,7 @@ export function SidebarFeedbackDialog({ void @@ -21,6 +23,7 @@ type SidebarFeedbackImageAttachmentsProps = { export function SidebarFeedbackImageAttachments({ images, + pendingCount, disabled, isDragActive, onAddFiles, @@ -43,14 +46,21 @@ export function SidebarFeedbackImageAttachments({ >
- {translate( - 'auto.components.sidebar.SidebarFeedbackImageAttachments.screenshotsHint', - 'Attach up to {{count}} screenshots, {{maxSize}} total', - { - count: MAX_FEEDBACK_IMAGE_COUNT, - maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_TOTAL_BYTES) - } - )} + {/* Why: shrinking an oversized screenshot is slow enough that a silent gap + between the pick and the thumbnail reads as a dropped attachment. */} + {pendingCount > 0 + ? translate( + 'auto.components.sidebar.SidebarFeedbackImageAttachments.preparing', + 'Preparing attachments…' + ) + : translate( + 'auto.components.sidebar.SidebarFeedbackImageAttachments.screenshotsHint', + 'Attach up to {{count}} screenshots, {{maxSize}} total', + { + count: MAX_FEEDBACK_IMAGE_COUNT, + maxSize: formatFeedbackImageSize(MAX_FEEDBACK_IMAGE_TOTAL_BYTES) + } + )}