Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

Phones: full screen no longer runs Safari out of memory

Gabriele Lozuponee6edf3d0360c
diff --git a/src/components/Compare.tsx b/src/components/Compare.tsx
index a6ce8a0..eab8105 100644
--- a/src/components/Compare.tsx
+++ b/src/components/Compare.tsx
@@ -48,14 +48,18 @@ export function CompareFrame({ g, base, fullscreen = false, maskSrc, onFullscree
};
const boxRef = useRef<HTMLDivElement>(null);
const contentSize = useMemo(() => ({ width: g.width, height: g.height }), [g.width, g.height]);
- // In a browser the Result view loads only the result, as a preview when it is as sharp
- // (stageImage); the original loads the first time a view shows it, then stays. Comparisons
- // and full screen use full-size images. The desktop app loads both at full size at once.
- const result = mode === "off" && !fullscreen;
- const after = result ? stageImage(`gen:${g.id}`, box.w, box.h) : urls.gen(g.id);
+ // In a browser both images are previews while they are as sharp as the full ones: on the
+ // stage when the box fits (stageImage), in full screen until the zoom passes the preview
+ // (`wantsFull`, then the full images stay). The original loads the first time a view shows
+ // it, then stays; Changes always compares the previews (its map is 1024 px). The desktop app
+ // loads both at full size at once.
const beforeWanted = useRef(!inBrowser);
- if (!result || holding) beforeWanted.current = true;
- const before = beforeWanted.current ? urls.input(base.inputId) : undefined;
+ if (mode !== "off" || holding) beforeWanted.current = true;
+ const images = (wantsFull: boolean) => {
+ if (!inBrowser) return { after: urls.gen(g.id), before: urls.input(base.inputId), diff: [urls.gen(g.id), urls.input(base.inputId)] };
+ const pick = (source: string) => (fullscreen ? (wantsFull ? urls.source(source) : urls.sourcePreview(source)) : stageImage(source, box.w, box.h));
+ return { after: pick(`gen:${g.id}`), before: beforeWanted.current ? pick(`input:${base.inputId}`) : undefined, diff: [urls.genPreview(g.id), urls.inputPreview(base.inputId)] };
+ };
useEffect(() => {
if (mode !== "flicker") return;
@@ -94,14 +98,16 @@ export function CompareFrame({ g, base, fullscreen = false, maskSrc, onFullscree
// In full screen the box keeps the image's own size inside ZoomPan, which scales it; `k`
// undoes that scale for the labels and the slider handle so they stay at screen size.
// While zoomed in a drag pans the image; the handle still moves the split.
- const frame = (scale: number, zoomed: boolean) => {
- const k = 1 / scale;
+ // Drawn flat (a browser, see ZoomPan) the box is laid out at its drawn size: nothing to undo.
+ const frame = (scale: number, zoomed: boolean, flat = false, wantsFull = false) => {
+ const k = flat ? 1 : 1 / scale;
+ const { after, before, diff } = images(wantsFull);
const tag = (side: "left" | "right") => ({ transform: `scale(${k})`, transformOrigin: `top ${side}` });
return (
<div
ref={boxRef}
className={`compare-box ${mode === "slider" ? "slider" : ""} ${zoomed ? "zoomed" : ""}`}
- style={fullscreen ? { width: g.width, height: g.height } : { width: box.w, height: box.h }}
+ style={fullscreen ? (flat ? { width: "100%", height: "100%" } : { width: g.width, height: g.height }) : { width: box.w, height: box.h }}
onPointerDown={(e) => {
if (e.button !== 0 || zoomed) return;
e.stopPropagation();
@@ -134,7 +140,7 @@ export function CompareFrame({ g, base, fullscreen = false, maskSrc, onFullscree
title={mode === "off" && !zoomed ? "Press and hold (or hold B) to see the original" : undefined}
>
<DevelopImg className="compare-img" src={after} placeholder={urls.genThumb(g.id)} alt={g.prompt} />
- {mode === "diff" && !holding && before && <DiffCanvas after={after} before={before} rect={base.rect} tagStyle={tag("left")} />}
+ {mode === "diff" && !holding && before && <DiffCanvas after={diff[0]} before={diff[1]} rect={base.rect} tagStyle={tag("left")} />}
{/* The area painted for the next edit, on the result (the original covers it when shown). */}
{maskSrc && <img className="stage-mask" src={maskSrc} alt="" draggable={false} />}
{/* The original stays mounted (hidden) so switching is instant. */}
@@ -182,8 +188,8 @@ export function CompareFrame({ g, base, fullscreen = false, maskSrc, onFullscree
if (fullscreen)
return (
<div className="viewer-frame">
- <ZoomPan contentSize={contentSize} resetKey={g.id} enableToggleZoom={false} minimapSrc={after}>
- {({ transform, isZoomed }) => frame(transform.scale, isZoomed)}
+ <ZoomPan contentSize={contentSize} resetKey={g.id} enableToggleZoom={false} flat={inBrowser} minimapSrc={inBrowser ? urls.genThumb(g.id) : urls.gen(g.id)}>
+ {({ transform, isZoomed, flat, wantsFull }) => frame(transform.scale, isZoomed, flat, wantsFull)}
</ZoomPan>
</div>
);
diff --git a/src/components/Gallery.tsx b/src/components/Gallery.tsx
index a351074..9636816 100644
--- a/src/components/Gallery.tsx
+++ b/src/components/Gallery.tsx
@@ -369,7 +369,7 @@ function Preview(props: {
</div>
{full && (
<Fullscreen hint="Scroll or click to zoom · Drag to pan" onClose={() => setFull(false)} nav={nav}>
- <ZoomImage key={g.id} src={urls.gen(g.id)} alt={g.prompt} />
+ <ZoomImage key={g.id} src={urls.gen(g.id)} source={`gen:${g.id}`} size={{ width: g.width, height: g.height }} alt={g.prompt} />
</Fullscreen>
)}
</div>
diff --git a/src/components/Stage.tsx b/src/components/Stage.tsx
index 7938c56..fdfa8f8 100644
--- a/src/components/Stage.tsx
+++ b/src/components/Stage.tsx
@@ -836,7 +836,8 @@ function StageFullscreen({ item, session, showOriginal, onClose }: { item: Sessi
hint = "Hold B for the original";
} else {
const src = g ? urls.gen(g.id) : urls.input(item.input!.id);
- content = <ZoomImage key={src} src={src} alt={g?.prompt ?? item.input?.original_name ?? ""} />;
+ const size = g ? { width: g.width, height: g.height } : { width: item.input!.width, height: item.input!.height };
+ content = <ZoomImage key={src} src={src} source={g ? `gen:${g.id}` : `input:${item.input!.id}`} size={size} alt={g?.prompt ?? item.input?.original_name ?? ""} />;
}
return (
<Fullscreen hint={hint} onClose={onClose} nav={nav}>
diff --git a/src/components/Viewer.tsx b/src/components/Viewer.tsx
index 0eb7c4f..2a7b235 100644
--- a/src/components/Viewer.tsx
+++ b/src/components/Viewer.tsx
@@ -4,7 +4,9 @@
import { ChevronLeft, ChevronRight, Maximize2, X } from "lucide-react";
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
+import { urls } from "../lib/api";
import { native } from "../lib/native";
+import { inBrowser } from "../lib/platform";
import { ZoomPan } from "./ZoomPan";
export const isTyping = (t: EventTarget | null) => t instanceof HTMLElement && (t.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(t.tagName));
@@ -179,7 +181,20 @@ function FullscreenShell({ children, hint, onClose, nav }: { children: ReactNode
);
}
-/** Fit to screen; wheel to zoom, drag to pan, click to toggle 100%, + / - / 0 / 1 shortcuts. */
-export function ZoomImage({ src, alt }: { src: string; alt: string }) {
- return <ZoomPan src={src} alt={alt} />;
+/** Fit to screen; wheel to zoom, drag to pan, click to toggle 100%, + / - / 0 / 1 shortcuts.
+ * With the image's `source` ("gen:…" / "input:…") and `size`, a browser shows the preview until
+ * the zoom needs the full image, and the thumbnail in the mini-map; a browser always draws it
+ * flat (see ZoomPan). The desktop app loads the full image at once. */
+export function ZoomImage({ src, alt, source, size }: { src: string; alt: string; source?: string; size?: { width: number; height: number } }) {
+ const known = inBrowser && source && size && size.width > 0 && size.height > 0;
+ return (
+ <ZoomPan
+ src={src}
+ alt={alt}
+ flat={inBrowser}
+ contentSize={known ? size : undefined}
+ previewSrc={known ? urls.sourcePreview(source) : undefined}
+ minimapSrc={known ? urls.sourceThumb(source) : undefined}
+ />
+ );
}
diff --git a/src/components/ZoomPan.tsx b/src/components/ZoomPan.tsx
index 5f37674..89b4db6 100644
--- a/src/components/ZoomPan.tsx
+++ b/src/components/ZoomPan.tsx
@@ -10,12 +10,17 @@ import {
} from "../lib/zoomMath";
import { isTyping, useMouseActive } from "./Viewer";
import { getState } from "../lib/store";
+import { viewerWantsFull } from "../lib/logic";
export interface ZoomPanContext {
transform: ZoomTransform;
isZoomed: boolean;
isPanning: boolean;
isPinching: boolean;
+ /** Children are laid out at the drawn size (`flat`), not at the content's own size. */
+ flat: boolean;
+ /** The content is drawn with more device pixels than a preview has (stays true once set). */
+ wantsFull: boolean;
zoomIn: () => void;
zoomOut: () => void;
zoomFit: () => void;
@@ -45,6 +50,16 @@ export interface ZoomPanProps {
className?: string;
/** Ref to access zoom/pan actions imperatively */
zoomRef?: React.Ref<ZoomPanHandle>;
+ /** Lay the content out at its drawn size and move it with a 2D translate, instead of at its
+ * own size scaled by a 3D transform. WebKit backs a composited layer at its untransformed
+ * size x the device scale (GraphicsLayerCA: the transform-aware scale is disabled), so a
+ * 4096 px image shrunk to a phone screen cost ~150 MB of layer per element and edits
+ * crashed Safari on an iPhone 15. Flat, the content paints into the screen-sized viewer
+ * layer. Children must fill the content box. */
+ flat?: boolean;
+ /** A screen-sized copy of `src`, shown until the zoom needs more pixels than it has (needs
+ * `contentSize`, the full image's size). */
+ previewSrc?: string;
}
export interface ZoomPanHandle {
@@ -175,9 +190,12 @@ export function ZoomPan({
resetKey,
className = "",
zoomRef,
+ flat = false,
+ previewSrc,
}: ZoomPanProps) {
const [naturalSize, setNaturalSize] = useState<Size | null>(null);
- const activeContentSize = (src && naturalSize) ? naturalSize : (contentSize ?? naturalSize);
+ // A preview's own size is not the content's.
+ const activeContentSize = (src && naturalSize && !(previewSrc && contentSize)) ? naturalSize : (contentSize ?? naturalSize);
const [mouseActive, wakeMouse] = useMouseActive(2200);
@@ -259,6 +277,27 @@ export function ZoomPan({
style,
} = zoom.containerProps;
+ // Once zoomed past what a preview can show, the full image (kept when zooming out again).
+ const full = React.useRef({ key: resetKey ?? src, on: false });
+ if (full.current.key !== (resetKey ?? src)) full.current = { key: resetKey ?? src, on: false };
+ if (zoom.fitted && activeContentSize && viewerWantsFull(activeContentSize, zoom.transform.scale, window.devicePixelRatio || 1)) full.current.on = true;
+ const wantsFull = full.current.on;
+ const shownSrc = previewSrc && contentSize && !wantsFull ? previewSrc : src;
+ const scale = zoom.transform.scale;
+ const contentStyle: React.CSSProperties = flat
+ ? {
+ width: activeContentSize ? activeContentSize.width * scale : undefined,
+ height: activeContentSize ? activeContentSize.height * scale : undefined,
+ transform: `translate(${zoom.transform.x}px, ${zoom.transform.y}px)`,
+ }
+ : {
+ width: activeContentSize?.width,
+ height: activeContentSize?.height,
+ transform: `translate3d(${zoom.transform.x}px, ${zoom.transform.y}px, 0px) scale(${scale})`,
+ transformOrigin: "0 0",
+ willChange: zoom.isPanning ? "transform" : "auto",
+ };
+
const zoomPercent = Math.round(zoom.transform.scale * 100);
const isMinimapVisible =
zoom.isZoomed &&
@@ -289,20 +328,11 @@ export function ZoomPan({
onPointerUp={onPointerUp}
onPointerCancel={onPointerCancel}
>
- <div
- className="zoom-pan-content"
- style={{
- width: activeContentSize?.width,
- height: activeContentSize?.height,
- transform: `translate3d(${zoom.transform.x}px, ${zoom.transform.y}px, 0px) scale(${zoom.transform.scale})`,
- transformOrigin: "0 0",
- willChange: zoom.isPanning ? "transform" : "auto",
- }}
- >
- {typeof children === "function" ? children(zoom) : children}
- {src && (
+ <div className="zoom-pan-content" style={contentStyle}>
+ {typeof children === "function" ? children({ ...zoom, flat, wantsFull }) : children}
+ {shownSrc && (
<img
- src={src}
+ src={shownSrc}
alt={alt}
draggable={false}
className="zoom-pan-img"
diff --git a/src/lib/logic.test.ts b/src/lib/logic.test.ts
index 68a9301..2a1663b 100644
--- a/src/lib/logic.test.ts
+++ b/src/lib/logic.test.ts
@@ -3,7 +3,7 @@ import {
activeLoras, activeOverrides, buildRequest, composing, editing, reviewRequest, wantsPromptReview, toggleLora, MAX_LORAS, jobPercent, compareBase, composerFromRecord, enhancerApplies, diffPixels, emptyComposer, expansionPixels, expansionPreset, jobLabel, jobSecondsLeft, lutInfo, moveRef, needsPrompt, normalizeQuality, persistentAdvanced, pipelineSummary,
planPipeline, remainingSeconds, sizeForWidth, timeAgo, upscaleRequest, upscaleTargets, versionLabels, type JobClock, type Ref,
neighbourAfterRemoval, pageOutdated, previewIndex, starPromptDue, STAR_PROMPT_AFTER, formatBytes, formatDuration, installItemShort, installItemText, installProgress, installStep, installTimeLeft, timelineLinks, undoTarget, aspectLabel, parseRatio, sizeForRatio, expandToRatio, expandToSize, arcLayout,
- goneVersions, PREVIEW_SIDE, stageFitsPreview,
+ goneVersions, PREVIEW_SIDE, stageFitsPreview, viewerWantsFull,
} from "./logic";
import type { GenerationRecord, InstallItem, JobView, LoraItem, Plan } from "./types";
@@ -563,6 +563,21 @@ describe("page version", () => {
});
});
+describe("full screen in a browser", () => {
+ it("keeps the preview until the zoom needs the full image", () => {
+ const square = { width: 4096, height: 4096 };
+ // Fitted to an iPhone (390 px wide, 3x): 1170 device px, the preview is enough.
+ expect(viewerWantsFull(square, 390 / 4096, 3)).toBe(false);
+ // Pinched in a little past it, or at 100 %.
+ expect(viewerWantsFull(square, 0.11, 3)).toBe(true);
+ expect(viewerWantsFull(square, 1, 3)).toBe(true);
+ // A portrait image fitted by its height already needs more than the preview.
+ expect(viewerWantsFull({ width: 1536, height: 2048 }, 520 / 2048, 3)).toBe(true);
+ // Size not known yet.
+ expect(viewerWantsFull({ width: 0, height: 0 }, 1, 3)).toBe(false);
+ });
+});
+
describe("opening a project in a browser", () => {
it("shows the preview only where it is as sharp as the full image", () => {
expect(PREVIEW_SIDE).toBe(1280);
diff --git a/src/lib/logic.ts b/src/lib/logic.ts
index 1d880c0..cdab3d4 100644
--- a/src/lib/logic.ts
+++ b/src/lib/logic.ts
@@ -835,6 +835,12 @@ export function stageFitsPreview(w: number, h: number, dpr: number): boolean {
return w > 0 && h > 0 && Math.max(w, h) * (dpr > 0 ? dpr : 1) <= PREVIEW_SIDE;
}
+/** Full screen in a browser: an image of `size` drawn at `scale` needs the full file, not the
+ * preview. Fitted to a phone it doesn't; zoomed in past ~1280 device px it does. */
+export function viewerWantsFull(size: { width: number; height: number }, scale: number, dpr: number): boolean {
+ return size.width > 0 && size.height > 0 && !stageFitsPreview(size.width * scale, size.height * scale, dpr);
+}
+
/** Versions to drop after loading a project's records in one answer: the ones the service knew
* (their key was in its copy, `known`) but sent no record for, i.e. deleted from the gallery.
* A version it did not know yet (added here, not saved) is looked up on its own instead. */
diff --git a/src/lib/useZoomPan.test.tsx b/src/lib/useZoomPan.test.tsx
index 8ae8ae8..3e8aa23 100644
--- a/src/lib/useZoomPan.test.tsx
+++ b/src/lib/useZoomPan.test.tsx
@@ -431,8 +431,14 @@ describe("CompareFrame & ZoomPan Integration", () => {
const content = container.querySelector(".zoom-pan-content") as HTMLElement;
expect(content).not.toBeNull();
- expect(content.style.width).toBe("1600px");
- expect(content.style.height).toBe("1200px");
+ // A browser lays the content out flat, at its drawn size (ZoomPan `flat`): the image's
+ // shape, with the comparison box filling it and no scale transform.
+ const w = parseFloat(content.style.width);
+ const h = parseFloat(content.style.height);
+ expect(w).toBeGreaterThan(0);
+ expect(w / h).toBeCloseTo(1600 / 1200, 5);
+ expect(content.style.transform).not.toContain("scale");
+ expect((container.querySelector(".compare-box") as HTMLElement).style.width).toBe("100%");
// Both before and after images must be mounted
const afterImg = container.querySelector(".compare-img") as HTMLImageElement;
diff --git a/src/lib/useZoomPan.ts b/src/lib/useZoomPan.ts
index 2df1cfc..33060a2 100644
--- a/src/lib/useZoomPan.ts
+++ b/src/lib/useZoomPan.ts
@@ -53,6 +53,9 @@ export interface UseZoomPanReturn {
isZoomed: boolean;
isPanning: boolean;
isPinching: boolean;
+ /** The transform has been fitted to the container at least once (before that it is a
+ * placeholder at 100 %). */
+ fitted: boolean;
canZoom: boolean;
fitTransform: ZoomTransform;
zoomIn: () => void;
@@ -97,6 +100,7 @@ export function useZoomPan(options: UseZoomPanOptions = {}): UseZoomPanReturn {
const [transform, setTransformState] = useState<ZoomTransform>({ x: 0, y: 0, scale: 1 });
const [isPanning, setIsPanning] = useState(false);
const [isPinching, setIsPinching] = useState(false);
+ const [fitted, setFitted] = useState(false);
// Track if user is currently at "fit" scale so resize preserves fit
const wasAtFitRef = useRef(true);
@@ -200,6 +204,7 @@ export function useZoomPan(options: UseZoomPanOptions = {}): UseZoomPanReturn {
if (resetKeyChanged || contentChanged) stopInertia();
wasAtFitRef.current = true;
setTransformState(fitTransform);
+ setFitted(true);
} else {
// Re-clamp current transform to new container dimensions
setTransformState((prev) => {
@@ -489,6 +494,7 @@ export function useZoomPan(options: UseZoomPanOptions = {}): UseZoomPanReturn {
isZoomed,
isPanning,
isPinching,
+ fitted,
canZoom,
fitTransform,
zoomIn,