From 6dfddffc146939fe17dedfc91d7a1aa6c2820784 Mon Sep 17 00:00:00 2001 From: Brian Hursey Date: Wed, 25 Nov 2026 20:37:51 +0000 Subject: [PATCH] Show edited renders in thumbnails and paste to a filmstrip selection The grid and filmstrip only ever showed the import thumbnail, so edits and virtual copies looked untouched. Edited tiles now render client-side with the same shader as the canvas, once each, from the small cached thumbnail. The filmstrip also gains Cmd/Shift multi-select so Paste, Copy and Reset can act on several photos from the toolbar or the right-click menu. --- src/App.tsx | 4 +- src/components/Develop.tsx | 159 ++++++++++++++++++++++++----- src/components/EditedImage.tsx | 64 ++++++++++++ src/components/LibraryGrid.tsx | 11 +- src/lib/editThumbs.ts | 178 +++++++++++++++++++++++++++++++++ src/styles.css | 9 ++ 6 files changed, 392 insertions(+), 33 deletions(-) create mode 100644 src/components/EditedImage.tsx create mode 100644 src/lib/editThumbs.ts diff --git a/src/App.tsx b/src/App.tsx index 0d8b964..6c65966 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2156,7 +2156,9 @@ export default function App() { onUpdate={updatePhoto} clipboard={clipboard} onCopy={copySettings} - onPaste={() => openPaste([activePhoto.id])} + onCopyOf={copySettingsOf} + onPaste={(ids) => openPaste(ids?.length ? ids : [activePhoto.id])} + onResetSelection={resetEdits} localClipboard={localClipboard} onLocalClipboard={setLocalClipboard} onExport={openExport} diff --git a/src/components/Develop.tsx b/src/components/Develop.tsx index 9193fe2..cffa640 100644 --- a/src/components/Develop.tsx +++ b/src/components/Develop.tsx @@ -5,7 +5,6 @@ import { defaultLocalLayer, MAX_HEAL_SPOTS, MAX_LOCAL_LAYERS, - thumbTransform, } from '../lib/edits'; import { subscribeClipPreview } from '../lib/clipPreview'; import { getBitmap, getDenoisedBitmap } from '../lib/images'; @@ -42,6 +41,7 @@ import type { Snapshot, } from '../types'; import ContextMenu, { type MenuEntry } from './ContextMenu'; +import EditedImage from './EditedImage'; import CropOverlay from './CropOverlay'; import Histogram from './Histogram'; import { @@ -123,7 +123,12 @@ interface Props { onUpdate: (id: string, patch: Partial, historyLabel?: string) => void; clipboard: Edits | null; onCopy: () => void; - onPaste: () => void; + /** Copy settings from a named filmstrip photo instead of the canvas photo. */ + onCopyOf?: (id: string) => void; + /** Paste the copied settings; no ids means the photo on the canvas. */ + onPaste: (ids?: string[]) => void; + /** Reset several photos at once, without a per-photo confirmation. */ + onResetSelection?: (ids: string[]) => void; localClipboard: LocalLayer | null; onLocalClipboard: (layer: LocalLayer | null) => void; onExport: () => void; @@ -153,7 +158,9 @@ export default function Develop({ onUpdate, clipboard, onCopy, + onCopyOf, onPaste, + onResetSelection, localClipboard, onLocalClipboard, onExport, @@ -202,6 +209,10 @@ export default function Develop({ const toggleStrip = () => (mobile ? setMobileStripOpen((open) => !open) : setStripOpen((open) => !open)); const [filmMinRating, setFilmMinRating] = useState(0); const [filmFlag, setFilmFlag] = useState('any'); + // Filmstrip multi-select. The canvas keeps its own photo; this is the set a + // paste applies to, exactly like Lightroom's filmstrip selection. + const [filmSel, setFilmSel] = useState([photo.id]); + const [filmAnchor, setFilmAnchor] = useState(photo.id); const [showOriginal, setShowOriginal] = useState(false); const [histVersion, setHistVersion] = useState(0); const [error, setError] = useState(null); @@ -214,7 +225,12 @@ export default function Develop({ const [panning, setPanning] = useState(false); const [navigatorView, setNavigatorView] = useState(null); const [readout, setReadout] = useState({ shown: 100, fit: 100 }); - const [menu, setMenu] = useState<{ x: number; y: number; kind: 'canvas' | 'film' } | null>(null); + const [menu, setMenu] = useState<{ + x: number; + y: number; + kind: 'canvas' | 'film'; + ids?: string[]; + } | null>(null); const [healMode, setHealMode] = useState(false); const [brushPx, setBrushPx] = useState(56); const [healFeather, setHealFeather] = useState(50); @@ -273,6 +289,49 @@ export default function Develop({ [filmPhotos, onSelect, photo.id], ); + // The photo on the canvas is always part of the selection, so a fresh filmstrip + // starts with just that frame. Cmd/Shift clicks grow the set without moving it. + useEffect(() => { + setFilmSel([photo.id]); + setFilmAnchor(photo.id); + }, [photo.id]); + + // A cull filter can hide part of the selection; drop those frames rather than + // pasting settings into photos that are no longer on screen. + useEffect(() => { + setFilmSel((prev) => { + const visible = new Set(filmPhotos.map((item) => item.id)); + const next = prev.filter((id) => visible.has(id)); + return next.length === prev.length ? prev : next; + }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [filmMinRating, filmFlag]); + + const clickFilm = (p: PhotoRecord, e: React.MouseEvent) => { + if (e.shiftKey) { + const a = filmPhotos.findIndex((item) => item.id === filmAnchor); + const b = filmPhotos.findIndex((item) => item.id === p.id); + if (a >= 0 && b >= 0) { + const [lo, hi] = a < b ? [a, b] : [b, a]; + setFilmSel(filmPhotos.slice(lo, hi + 1).map((item) => item.id)); + } else { + setFilmSel([p.id]); + setFilmAnchor(p.id); + } + return; + } + if (e.metaKey || e.ctrlKey) { + setFilmSel((prev) => + prev.includes(p.id) ? prev.filter((id) => id !== p.id) : [...prev, p.id], + ); + setFilmAnchor(p.id); + return; + } + setFilmSel([p.id]); + setFilmAnchor(p.id); + onSelect(p.id); + }; + const moveLensSplit = (e: React.PointerEvent) => { const rect = e.currentTarget.parentElement?.getBoundingClientRect(); if (!rect || rect.width <= 0) return; @@ -1283,7 +1342,7 @@ export default function Develop({ } else if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'c') { onCopy(); } else if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'v') { - onPaste(); + onPaste(filmSel); } }; const up = (e: KeyboardEvent) => { @@ -1306,6 +1365,7 @@ export default function Develop({ navigateFilm, onCopy, onPaste, + filmSel, undo, redo, applyZoom, @@ -1456,7 +1516,7 @@ export default function Develop({ { label: 'Before / After', shortcut: '\\', onSelect: () => setShowOriginal((v) => !v) }, 'separator', { label: 'Copy settings', shortcut: '⌘C', onSelect: onCopy }, - { label: 'Paste settings', shortcut: '⌘V', onSelect: onPaste, disabled: !clipboard }, + { label: 'Paste settings', shortcut: '⌘V', onSelect: () => onPaste([photo.id]), disabled: !clipboard }, { label: 'Reset settings', onSelect: resetAll }, 'separator', ...(canPushImmich && onPushImmich @@ -1469,10 +1529,38 @@ export default function Develop({ { label: 'Delete photo…', danger: true, onSelect: onDelete }, ]; + // Right-clicking one of several selected frames acts on the whole selection; + // right-clicking outside it acts on that frame alone. + const filmMenuIds = menu?.kind === 'film' && menu.ids?.length ? menu.ids : [photo.id]; const filmMenu: MenuEntry[] = [ - { label: 'Copy settings', onSelect: onCopy }, - { label: 'Paste settings', onSelect: onPaste, disabled: !clipboard }, - { label: 'Reset settings', onSelect: resetAll }, + { + label: 'Open in Develop', + disabled: filmMenuIds.includes(photo.id), + onSelect: () => onSelect(filmMenuIds[0]), + }, + 'separator', + { + label: 'Copy settings', + onSelect: () => (onCopyOf ? onCopyOf(filmMenuIds[0]) : onCopy()), + }, + { + label: + filmMenuIds.length > 1 + ? `Paste settings to ${filmMenuIds.length} photos…` + : 'Paste settings', + onSelect: () => onPaste(filmMenuIds), + disabled: !clipboard, + }, + { + label: + filmMenuIds.length > 1 + ? `Reset adjustments on ${filmMenuIds.length} photos` + : 'Reset settings', + onSelect: () => + filmMenuIds.length > 1 && onResetSelection + ? onResetSelection(filmMenuIds) + : resetAll(), + }, 'separator', ...(canPushImmich && onPushImmich ? ([{ label: 'Send edit to Immich', onSelect: onPushImmich }] as MenuEntry[]) @@ -1525,8 +1613,17 @@ export default function Develop({ - - ))} diff --git a/src/components/EditedImage.tsx b/src/components/EditedImage.tsx new file mode 100644 index 0000000..4c2d715 --- /dev/null +++ b/src/components/EditedImage.tsx @@ -0,0 +1,64 @@ +import { useEffect, useRef, useState } from 'react'; +import { thumbTransform } from '../lib/edits'; +import { useEditedThumb } from '../lib/editThumbs'; +import type { PhotoRecord } from '../types'; + +interface Props { + photo: PhotoRecord; + /** Aspect of the tile the picture fills; the rotation transform needs it. */ + boxAspect?: number; + alt?: string; + loading?: 'lazy' | 'eager'; + draggable?: boolean; +} + +/** + * A thumbnail that follows the photo's edits. + * + * Edited photos get a freshly rendered small JPEG; everything else keeps the + * server/import thumbnail and the CSS orientation transform. Rendering starts + * only when the tile is close to the viewport. + */ +export default function EditedImage({ + photo, + boxAspect = 4 / 3, + alt, + loading = 'lazy', + draggable = false, +}: Props) { + const ref = useRef(null); + const [nearView, setNearView] = useState(false); + + useEffect(() => { + const el = ref.current; + if (!el) return; + if (typeof IntersectionObserver === 'undefined') { + setNearView(true); + return; + } + const observer = new IntersectionObserver( + (entries) => { + if (!entries.some((entry) => entry.isIntersecting)) return; + setNearView(true); + observer.disconnect(); + }, + { rootMargin: '400px' }, + ); + observer.observe(el); + return () => observer.disconnect(); + }, []); + + const edited = useEditedThumb(photo, nearView); + + return ( + {alt + ); +} diff --git a/src/components/LibraryGrid.tsx b/src/components/LibraryGrid.tsx index 11204ad..445e9cc 100644 --- a/src/components/LibraryGrid.tsx +++ b/src/components/LibraryGrid.tsx @@ -1,8 +1,9 @@ import { useEffect, useRef, useState } from 'react'; -import { COLOR_LABELS, isEdited, thumbTransform } from '../lib/edits'; +import { COLOR_LABELS, isEdited } from '../lib/edits'; import { draggedPhotoIds, endDrag, PHOTO_DRAG, startPhotoDrag } from '../lib/drag'; import type { ColorLabel, Flag, PhotoRecord } from '../types'; import Stars, { ColorSwatches, FlagButtons } from './Stars'; +import EditedImage from './EditedImage'; interface Props { photos: PhotoRecord[]; @@ -443,13 +444,7 @@ export default function LibraryGrid({ }} >
- {p.name} +
{p.flag === 'pick' && ✓} {p.flag === 'reject' && ✕} diff --git a/src/lib/editThumbs.ts b/src/lib/editThumbs.ts new file mode 100644 index 0000000..fbfdace --- /dev/null +++ b/src/lib/editThumbs.ts @@ -0,0 +1,178 @@ +import { useEffect, useState } from 'react'; +import type { Edits, PhotoRecord } from '../types'; +import { isEdited } from './edits'; +import { Renderer, rotatedBounds } from '../gl/renderer'; + +/** + * Thumbnails of edited photos. + * + * The server thumbnail is always the *undeveloped* frame, so the grid and the + * filmstrip would otherwise show the import look no matter how far a photo was + * edited. These are rendered on demand with the same shader the canvas uses, + * from the already-cached small thumbnail (not the multi-megapixel proxy), and + * kept as data URLs. One renderer does every tile: a WebGL context per picture + * would exhaust the browser's limit long before a shoot. + */ + +/** Long edge of a rendered thumb; twice the largest card, so it stays sharp. */ +const RENDER_EDGE = 384; +const CACHE_LIMIT = 700; + +const cache = new Map(); + +/** + * Edits are immutable: every change stores a new object (undo re-uses an old + * one). A per-object version number is therefore a cheap, exact cache key — far + * cheaper than serializing the whole edit for every visible card on every render. + */ +const versions = new WeakMap(); +let versionSeq = 0; + +function versionOf(edits: Edits): number { + let version = versions.get(edits); + if (version === undefined) { + version = ++versionSeq; + versions.set(edits, version); + } + return version; +} + +function keyOf(photo: PhotoRecord): string { + return `${photo.id}|${versionOf(photo.edits)}`; +} + +let renderer: Renderer | null = null; +let rendererFailed = false; + +function getRenderer(): Renderer | null { + if (renderer) return renderer; + if (rendererFailed) return null; + try { + renderer = new Renderer(document.createElement('canvas')); + } catch { + // No WebGL2: tiles quietly keep their plain thumbnails. + rendererFailed = true; + } + return renderer; +} + +/** The small picture the browser already has, decoded for the shader. */ +async function loadThumb(photo: PhotoRecord): Promise { + const src = photo.thumbUrl ?? photo.thumb; + if (!src) return null; + try { + const res = await fetch(src, { credentials: 'same-origin' }); + if (!res.ok) return null; + return await createImageBitmap(await res.blob(), { imageOrientation: 'from-image' }); + } catch { + return null; + } +} + +async function renderThumb(photo: PhotoRecord): Promise { + const r = getRenderer(); + if (!r) return null; + const bitmap = await loadThumb(photo); + if (!bitmap) return null; + try { + const crop = photo.edits.crop; + const frame = rotatedBounds(bitmap.width, bitmap.height, crop.angle, crop.quarter); + const cropW = Math.max(1, frame.w * crop.w); + const cropH = Math.max(1, frame.h * crop.h); + const scale = Math.min(2, RENDER_EDGE / Math.max(cropW, cropH)); + const width = Math.max(16, Math.round(cropW * scale)); + const height = Math.max(16, Math.round(cropH * scale)); + r.setImage(bitmap); + // `lens: null` clears the previous photo's calibration maps: this renderer + // is shared, and a stale map would warp the next thumbnail. + r.render(photo.edits, { width, height, lens: null, upscale: scale > 1.02 }); + return r.canvas.toDataURL('image/jpeg', 0.74); + } finally { + bitmap.close(); + } +} + +interface Job { + photo: PhotoRecord; + key: string; + waiters: ((url: string | null) => void)[]; +} + +const jobs: Job[] = []; +let pumping = false; + +function enqueue(photo: PhotoRecord, key: string): Promise { + return new Promise((resolve) => { + const existing = jobs.find((job) => job.key === key); + if (existing) { + existing.waiters.push(resolve); + return; + } + jobs.push({ photo, key, waiters: [resolve] }); + void pump(); + }); +} + +/** One render at a time, yielding between tiles so the UI stays responsive. */ +async function pump() { + if (pumping) return; + pumping = true; + while (jobs.length) { + const job = jobs.shift()!; + let url: string | null = null; + try { + url = await renderThumb(job.photo); + } catch { + url = null; + } + if (url) { + cache.set(job.key, url); + while (cache.size > CACHE_LIMIT) { + const first = cache.keys().next(); + if (first.done) break; + cache.delete(first.value); + } + } + for (const waiter of job.waiters) waiter(url); + await new Promise((resolve) => setTimeout(resolve, 0)); + } + pumping = false; +} + +/** + * The edited thumbnail for a photo, or null while there is none to show. + * + * Only edited photos are rendered, and only once the tile is near the viewport + * (`enabled`), so a long shoot does not pay for pictures nobody can see. Until + * a render lands the caller keeps showing the plain thumbnail. + */ +export function useEditedThumb(photo: PhotoRecord, enabled = true): string | null { + const key = isEdited(photo.edits) ? keyOf(photo) : ''; + const [src, setSrc] = useState(() => (key ? cache.get(key) ?? null : null)); + + useEffect(() => { + if (!key) { + setSrc(null); + return; + } + const hit = cache.get(key); + if (hit) { + setSrc(hit); + return; + } + if (!enabled) return; + let alive = true; + // Coalesce slider drags: only the setting you stopped on gets rendered. + const timer = window.setTimeout(() => { + void enqueue(photo, key).then((url) => { + if (alive && url) setSrc(url); + }); + }, 120); + return () => { + alive = false; + window.clearTimeout(timer); + }; + }, [key, photo, enabled]); + + return src; +} diff --git a/src/styles.css b/src/styles.css index 97dc915..f974a16 100644 --- a/src/styles.css +++ b/src/styles.css @@ -1565,6 +1565,15 @@ figcaption { box-shadow: inset 0 0 0 1px var(--accent), 0 0 0 1px var(--accent); } +/* Additional photos in a multi-select, as opposed to the one on the canvas. */ +.film.selected { + box-shadow: inset 0 0 0 2px #cfcfda; +} + +.film.selected.active { + box-shadow: inset 0 0 0 2px var(--accent), 0 0 0 1px var(--accent); +} + .film-rating { position: absolute; bottom: 2px;