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.

Browser: a project with many versions opens fast on a phone

Gabriele Lozuponebc0461c2921f
diff --git a/Cargo.lock b/Cargo.lock
index 16baf9c..89a960a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3417,6 +3417,7 @@ version = "0.10.2"
dependencies = [
"axum",
"futures-util",
+ "image",
"qls-core",
"rpassword",
"rust-embed",
diff --git a/crates/qls-core/src/remote.rs b/crates/qls-core/src/remote.rs
index f79bae6..fd253cd 100644
--- a/crates/qls-core/src/remote.rs
+++ b/crates/qls-core/src/remote.rs
@@ -266,8 +266,9 @@ pub fn browser_allowed(method: &str, path: &str) -> bool {
("GET", ["setup", "hardware" | "install" | "legacy-upscalers"]) => true,
("GET", ["enhancer", "defaults"]) => true,
("GET", ["jobs", _] | ["jobs", _, "wait"]) => true,
- ("GET", ["inputs", _] | ["inputs", _, "image" | "thumb"]) => true,
- ("GET", ["gallery", _] | ["gallery", _, "image" | "thumb" | "original"]) => true,
+ ("GET", ["inputs", _] | ["inputs", _, "image" | "thumb" | "preview"]) => true,
+ ("GET", ["gallery", _] | ["gallery", _, "image" | "thumb" | "preview" | "original"]) => true,
+ ("GET", ["projects", _, "records"]) => true,
("GET", ["luts", _, "preview"]) => true,
("GET" | "PUT" | "DELETE", ["projects", _]) => true,
("PUT", ["config"]) => true,
@@ -449,6 +450,9 @@ mod tests {
for (m, p) in [
("GET", "/status"),
("GET", "/gallery/abc/image"),
+ ("GET", "/gallery/abc/preview"),
+ ("GET", "/inputs/abc/preview"),
+ ("GET", "/projects/abc/records"),
("GET", "/events"),
("POST", "/jobs"),
("POST", "/inputs"),
@@ -478,6 +482,8 @@ mod tests {
("POST", "/remote/password"),
("DELETE", "/jobs"),
("GET", "/gallery/a/b/c"),
+ ("POST", "/gallery/abc/preview"),
+ ("PUT", "/projects/abc/records"),
] {
assert!(!browser_allowed(m, p), "{m} {p}");
}
diff --git a/crates/qls-core/src/store.rs b/crates/qls-core/src/store.rs
index 0cb00f5..c1f7715 100644
--- a/crates/qls-core/src/store.rs
+++ b/crates/qls-core/src/store.rs
@@ -253,6 +253,11 @@ impl Store {
self.data_dir.join("thumbs").join(format!("{id}.jpg"))
}
+ /// Where the browser's screen-sized copy of result `id` is kept (see [`preview`]).
+ pub fn preview_path(&self, id: &str) -> PathBuf {
+ self.data_dir.join("thumbs").join(format!("{id}.preview"))
+ }
+
/// The bundled example projects' images ([`crate::examples`]).
pub fn examples_dir(&self) -> PathBuf {
self.data_dir.join("examples")
@@ -362,6 +367,9 @@ impl Store {
let _ = std::fs::remove_file(&f);
}
let _ = std::fs::remove_file(self.thumb_path(id));
+ let preview = self.preview_path(id);
+ let _ = std::fs::remove_file(preview.with_extension("preview.jpg"));
+ let _ = std::fs::remove_file(preview.with_extension("preview.png"));
let _ = std::fs::remove_file(self.original_path(id));
Ok(())
}
@@ -445,12 +453,54 @@ impl Store {
self.data_dir.join("inputs").join(format!("{id}.thumb.jpg"))
}
+ /// Where the browser's screen-sized copy of input `id` is kept (see [`preview`]).
+ pub fn input_preview_path(&self, id: &str) -> PathBuf {
+ self.data_dir.join("inputs").join(format!("{id}.preview"))
+ }
+
/// Full-size JPEG shown in the UI for inputs the WebView cannot display (HEIC).
pub fn input_display_path(&self, id: &str) -> PathBuf {
self.data_dir.join("inputs").join(format!("{id}.display.jpg"))
}
}
+/// Long side of the screen-sized copy a browser shows on the stage. A phone's stage is about
+/// 1170 device px wide (iPhone: 390 CSS px x 3). Measured 10-24 on 3 images: 140-370 kB as JPEG
+/// q85 against 6-7 MB for a 2048x2048 PNG, about 40 ms to make.
+pub const PREVIEW_SIDE: u32 = 1280;
+
+/// The screen-sized copy of the image at `src` (at most [`PREVIEW_SIDE`] on its long side; a
+/// smaller image keeps its size), made on first request and kept at `stem` + ".jpg", or + ".png"
+/// for an image with transparency, which JPEG would lose. Returns the file and its type.
+pub fn preview(src: &Path, stem: &Path) -> AppResult<(PathBuf, &'static str)> {
+ let jpg = stem.with_extension("preview.jpg");
+ let png = stem.with_extension("preview.png");
+ if jpg.is_file() {
+ return Ok((jpg, "image/jpeg"));
+ }
+ if png.is_file() {
+ return Ok((png, "image/png"));
+ }
+ let bytes = std::fs::read(src).map_err(|e| AppError::io("read the image", src, e))?;
+ let img = crate::imaging::decode_image(&bytes)?;
+ let small = crate::imaging::thumbnail(&img.pixels, PREVIEW_SIDE);
+ let (out, data, ct) = if img.has_transparency {
+ (png, crate::imaging::encode_png(&small, &[], None, true)?, "image/png")
+ } else {
+ (jpg, crate::imaging::encode_jpeg(&small, 85)?, "image/jpeg")
+ };
+ if let Some(dir) = out.parent() {
+ let _ = std::fs::create_dir_all(dir);
+ }
+ // Two browsers may ask at once: each writes its own file, the rename is atomic.
+ let tmp = out.with_extension(format!("{}.part", uuid::Uuid::new_v4().simple()));
+ std::fs::write(&tmp, &data).map_err(|e| AppError::io("save the preview", &tmp, e))?;
+ if std::fs::rename(&tmp, &out).is_err() {
+ let _ = std::fs::remove_file(&tmp);
+ }
+ Ok((out, ct))
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -584,4 +634,41 @@ mod tests {
assert!(s.get_input("../../etc").is_err());
assert!(s.put_input(b"garbage", None, None).is_err());
}
+
+ #[test]
+ fn previews_are_screen_sized_and_kept() {
+ let dir = tempfile::tempdir().unwrap();
+ let s = Store::open(dir.path()).unwrap();
+ let big = dir.path().join("big.png");
+ std::fs::write(&big, crate::imaging::encode_png(&image::RgbaImage::from_pixel(2048, 1024, image::Rgba([9, 99, 199, 255])), &[], None, true).unwrap()).unwrap();
+ let stem = s.preview_path("g1");
+ let (p, ct) = preview(&big, &stem).unwrap();
+ assert_eq!(ct, "image/jpeg");
+ assert_eq!(p, dir.path().join("thumbs").join("g1.preview.jpg"));
+ let img = crate::imaging::decode_image(&std::fs::read(&p).unwrap()).unwrap();
+ assert_eq!((img.pixels.width(), img.pixels.height()), (PREVIEW_SIDE, PREVIEW_SIDE / 2));
+ // Made once: later requests read the kept file.
+ std::fs::remove_file(&big).unwrap();
+ assert_eq!(preview(&big, &stem).unwrap().0, p);
+ assert!(preview(&big, &s.preview_path("g2")).is_err(), "no source, no preview");
+ // A smaller image keeps its size; transparency stays (PNG).
+ let small = dir.path().join("small.png");
+ std::fs::write(&small, crate::imaging::encode_png(&image::RgbaImage::from_pixel(300, 200, image::Rgba([1, 2, 3, 255])), &[], None, true).unwrap()).unwrap();
+ let (p, ct) = preview(&small, &s.input_preview_path("abc")).unwrap();
+ assert_eq!(ct, "image/jpeg");
+ assert_eq!(crate::imaging::decode_image(&std::fs::read(&p).unwrap()).unwrap().pixels.dimensions(), (300, 200));
+ let clear = dir.path().join("clear.png");
+ std::fs::write(&clear, crate::imaging::encode_png(&image::RgbaImage::from_pixel(1600, 1600, image::Rgba([1, 2, 3, 0])), &[], None, true).unwrap()).unwrap();
+ let (p, ct) = preview(&clear, &s.preview_path("g3")).unwrap();
+ assert_eq!(ct, "image/png");
+ let img = crate::imaging::decode_image(&std::fs::read(&p).unwrap()).unwrap();
+ assert!(img.has_transparency);
+ assert_eq!(img.pixels.dimensions(), (PREVIEW_SIDE, PREVIEW_SIDE));
+ // Removing the result removes its preview.
+ let mut r = rec("g1", "p", "2026-10-24T10:00:00.000Z");
+ r.file_path = dir.path().join("out").join("g1.png").to_string_lossy().into_owned();
+ s.insert_generation(&r).unwrap();
+ s.delete_generation("g1", &dir.path().join("out")).unwrap();
+ assert!(!dir.path().join("thumbs").join("g1.preview.jpg").exists());
+ }
}
diff --git a/crates/qls-service/Cargo.toml b/crates/qls-service/Cargo.toml
index 688fb74..69757b4 100644
--- a/crates/qls-service/Cargo.toml
+++ b/crates/qls-service/Cargo.toml
@@ -27,5 +27,6 @@ tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }
uuid = { version = "1.27.0", features = ["v4"] }
[dev-dependencies]
+image = { version = "0.25.10", default-features = false, features = ["png"] }
tempfile = "3.27.0"
tower = { version = "0.5.3", features = ["util"] }
diff --git a/crates/qls-service/src/api.rs b/crates/qls-service/src/api.rs
index fea347e..f248033 100644
--- a/crates/qls-service/src/api.rs
+++ b/crates/qls-service/src/api.rs
@@ -102,10 +102,12 @@ pub fn router(state: Arc<AppState>, lan: bool) -> Router {
.route("/inputs/{id}", get(get_input))
.route("/inputs/{id}/image", get(input_image))
.route("/inputs/{id}/thumb", get(input_thumb))
+ .route("/inputs/{id}/preview", get(input_preview))
.route("/gallery", get(gallery_list))
.route("/gallery/{id}", get(gallery_get).patch(gallery_patch).delete(gallery_delete))
.route("/gallery/{id}/image", get(gallery_image))
.route("/gallery/{id}/thumb", get(gallery_thumb))
+ .route("/gallery/{id}/preview", get(gallery_preview))
.route("/gallery/{id}/original", get(gallery_original))
.route("/gallery/{id}/export", post(gallery_export))
.route("/image-info", post(image_info))
@@ -1057,6 +1059,20 @@ async fn input_thumb(State(s): St, AxPath(id): AxPath<String>) -> ApiResult<Resp
serve_file(&store.input_thumb_path(&id), Some("image/jpeg")).await
}
+/// A screen-sized copy of `src` for the browser's stage, made once (`qls_core::store::preview`).
+async fn serve_preview(src: PathBuf, stem: PathBuf) -> ApiResult<Response> {
+ let (p, ct) = tokio::task::spawn_blocking(move || qls_core::store::preview(&src, &stem)).await.map_err(AppError::internal)??;
+ serve_file(&p, Some(ct)).await
+}
+
+async fn input_preview(State(s): St, AxPath(id): AxPath<String>) -> ApiResult<Response> {
+ let store = s.engine.input_store(&id);
+ let r = store.get_input(&id)?;
+ // From the JPEG kept for display, as `input_image` serves it.
+ let src = if r.format == "heic" { store.input_display_path(&id) } else { PathBuf::from(&r.path) };
+ serve_preview(src, store.input_preview_path(&id)).await
+}
+
// ---------------------------------------------------------------------------------------
// Gallery
// ---------------------------------------------------------------------------------------
@@ -1113,6 +1129,12 @@ async fn gallery_thumb(State(s): St, AxPath(id): AxPath<String>) -> ApiResult<Re
serve_file(&p, Some("image/jpeg")).await
}
+async fn gallery_preview(State(s): St, AxPath(id): AxPath<String>) -> ApiResult<Response> {
+ let store = s.engine.gen_store(&id);
+ let r = store.get_generation(&id)?;
+ serve_preview(PathBuf::from(&r.file_path), store.preview_path(&id)).await
+}
+
#[derive(Deserialize)]
struct ExportBody {
path: String,
diff --git a/crates/qls-service/src/security_tests.rs b/crates/qls-service/src/security_tests.rs
index 40882ff..0c4669e 100644
--- a/crates/qls-service/src/security_tests.rs
+++ b/crates/qls-service/src/security_tests.rs
@@ -323,3 +323,33 @@ async fn the_network_stays_closed_without_a_password() {
assert!(!f.state.engine.config().remote_access);
assert_eq!(r.body["listening"], false);
}
+
+#[tokio::test]
+async fn a_browser_opens_a_project_with_one_request_and_previews() {
+ let f = fixture(true);
+ let a = app(&f, true, "192.168.1.50");
+ let cookie = sign_in(&a).await;
+ let c = ("cookie", cookie.as_str());
+ let img = image::RgbaImage::from_pixel(2000, 1000, image::Rgba([10, 20, 30, 255]));
+ let png = qls_core::imaging::encode_png(&img, &[], None, true).unwrap();
+ let input = f.state.engine.store.put_input(&png, Some("a.png"), None).unwrap();
+ let project = json!({ "meta": { "id": "p", "title": "t", "updatedAt": 1.0 }, "items": [
+ { "key": "i", "kind": "input", "inputId": input.id },
+ { "key": "r-gone", "kind": "gen", "genId": "gone" },
+ ] });
+ let r = send(&a, "PUT", "/api/v1/projects/p", &[c, CSRF], Some(json!({ "project": project }))).await;
+ assert_eq!(r.status, StatusCode::OK, "{:?}", r.body);
+ let r = send(&a, "GET", "/api/v1/projects/p/records", &[c], None).await;
+ assert_eq!(r.status, StatusCode::OK);
+ assert_eq!(r.body["inputs"][&input.id]["width"], 2000);
+ assert_eq!(r.body["generations"], json!({}), "a deleted version is left out");
+ assert_eq!(send(&a, "GET", "/api/v1/projects/nope/records", &[c], None).await.status, StatusCode::NOT_FOUND);
+ let r = send(&a, "GET", &format!("/api/v1/inputs/{}/preview", input.id), &[c], None).await;
+ assert_eq!(r.status, StatusCode::OK);
+ assert_eq!(r.headers[header::CONTENT_TYPE], "image/jpeg");
+ assert!(r.headers[header::CACHE_CONTROL].to_str().unwrap().contains("immutable"));
+ assert!(f.state.engine.store.input_preview_path(&input.id).with_extension("preview.jpg").is_file());
+ assert_eq!(send(&a, "GET", "/api/v1/gallery/gone/preview", &[c], None).await.status, StatusCode::NOT_FOUND);
+ // Signed out: nothing.
+ assert_eq!(send(&a, "GET", "/api/v1/projects/p/records", &[], None).await.status, StatusCode::UNAUTHORIZED);
+}
diff --git a/docs/http-api.md b/docs/http-api.md
index 86a26e4..d9a465e 100644
--- a/docs/http-api.md
+++ b/docs/http-api.md
@@ -87,6 +87,7 @@ can be read and changed; send it back as `prompt` with `"enhanced_from"` set to
| POST | `/inputs/import` | Import an image from a path on this machine: `{"path": "..."}`; `?private=true` is allowed |
| GET | `/inputs/{id}` | The stored input's record |
| GET | `/inputs/{id}/image`, `/inputs/{id}/thumb` | The image, or its thumbnail |
+| GET | `/inputs/{id}/preview` | A screen-sized copy (at most 1280 px on the long side, JPEG; PNG if it has transparency), made on first request |
| POST | `/image-info` | Size, aspect, type and embedded generation metadata of an image |
| POST | `/private/clear` | End private mode: delete every private input and result |
@@ -100,6 +101,7 @@ can be read and changed; send it back as `prompt` with `"enhanced_from"` set to
| DELETE | `/gallery/{id}` | Delete a result and its files |
| GET | `/gallery/{id}/image` | The result image |
| GET | `/gallery/{id}/thumb` | Its thumbnail |
+| GET | `/gallery/{id}/preview` | A screen-sized copy, as for inputs |
| GET | `/gallery/{id}/original` | The result before the upscaler enlarged it, when it was kept |
| POST | `/gallery/{id}/export` | Save a copy to a path: `{"path": "...", "overwrite": false, "original": false}`; the format follows the file extension |
diff --git a/src/components/Compare.tsx b/src/components/Compare.tsx
index 055af57..9750625 100644
--- a/src/components/Compare.tsx
+++ b/src/components/Compare.tsx
@@ -3,7 +3,8 @@
import { useEffect, useMemo, useRef, useState, type CSSProperties } from "react";
import { urls } from "../lib/api";
import { diffPixels, type CompareBase, type CompareMode } from "../lib/logic";
-import { setState, useStore } from "../lib/store";
+import { inBrowser } from "../lib/platform";
+import { setState, stageImage, useStore } from "../lib/store";
import type { GenerationRecord } from "../lib/types";
import { Seg, useFit } from "./ui";
import { FullscreenButton } from "./Viewer";
@@ -36,8 +37,14 @@ export function CompareFrame({ g, base, fullscreen = false, maskSrc, onFullscree
const dragging = useRef(false);
const boxRef = useRef<HTMLDivElement>(null);
const contentSize = useMemo(() => ({ width: g.width, height: g.height }), [g.width, g.height]);
- const after = urls.gen(g.id);
- const before = urls.input(base.inputId);
+ // 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);
+ const beforeWanted = useRef(!inBrowser);
+ if (!result || holding) beforeWanted.current = true;
+ const before = beforeWanted.current ? urls.input(base.inputId) : undefined;
useEffect(() => {
if (mode !== "flicker") return;
@@ -110,7 +117,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}
>
<img className="compare-img" src={after} alt={g.prompt} draggable={false} />
- {mode === "diff" && !holding && <DiffCanvas after={after} before={before} rect={base.rect} tagStyle={tag("left")} />}
+ {mode === "diff" && !holding && before && <DiffCanvas after={after} before={before} 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. */}
@@ -199,7 +206,7 @@ function DiffCanvas({ after, before, rect, tagStyle }: { after: string; before:
im.onerror = fail;
// A separate URL: the displayed <img> loads without CORS, and reusing that cached
// response for a CORS request would fail.
- im.src = `${src}&cors=1`;
+ im.src = `${src}${src.includes("?") ? "&" : "?"}cors=1`;
});
Promise.all([load(after), load(before)])
.then(([a, b]) => {
diff --git a/src/components/Stage.tsx b/src/components/Stage.tsx
index 33685fe..e3547ed 100644
--- a/src/components/Stage.tsx
+++ b/src/components/Stage.tsx
@@ -9,7 +9,7 @@ import {
} from "../lib/logic";
import { canCopyImage, native } from "../lib/native";
import {
- addAsReference, addResult, cancelJob, focusPrompt, getState, itemSource, removeSessionItem, reusePrompt, selectedItem, selectItem, sessionLabels, sessionLinks, setState, submitRequest, toastError, toastInfo, useStore, type SessionItem,
+ addAsReference, addResult, cancelJob, focusPrompt, getState, itemSource, removeSessionItem, reusePrompt, selectedItem, selectItem, sessionLabels, sessionLinks, setState, stageImage, submitRequest, toastError, toastInfo, useStore, type SessionItem,
} from "../lib/store";
import type { GenerationRecord, GenerationRequest, JobView, LutEntry, Mode, Size } from "../lib/types";
import { Cocoon, Hatch } from "./Cocoon";
@@ -500,13 +500,16 @@ function InputActions({ item, onLut }: { item: SessionItem; onLut: (lut: LutEntr
* full-screen button in its top-right corner. */
/** `fill`: enlarged to fill the frame like the card it replaces (a result); otherwise never shown
* wider than it is. */
-function PlainFrame({ src, alt, w, h, maskSrc, overlay, fill, onFullscreen }: { src: string; alt: string; w: number; h: number; maskSrc?: string; overlay?: React.ReactNode; fill?: boolean; onFullscreen: () => void }) {
+/** One image on the stage. With `source` ("gen:"/"input:"), a browser may load its preview
+ * (`stageImage`); `src` is loaded as it is. */
+function PlainFrame({ src, source, alt, w, h, maskSrc, overlay, fill, onFullscreen }: { src?: string; source?: string; alt: string; w: number; h: number; maskSrc?: string; overlay?: React.ReactNode; fill?: boolean; onFullscreen: () => void }) {
const { ref, box } = useFit(w / h);
const bw = fill ? box.w : Math.min(box.w, w);
+ const img = source ? stageImage(source, bw, bw * (h / w)) : src;
return (
<div className="stage-frame" ref={ref}>
<div className="image-box" style={{ width: bw, height: bw * (h / w) }}>
- <img className="stage-img fill" src={src} alt={alt} draggable={false} />
+ <img className="stage-img fill" src={img} alt={alt} draggable={false} />
{maskSrc && <img className="stage-mask" src={maskSrc} alt="" draggable={false} />}
{overlay}
<FullscreenButton onOpen={onFullscreen} />
@@ -954,7 +957,7 @@ export function Stage() {
{lutActive ? (
<PlainFrame
key={src}
- src={src}
+ source={source}
alt={g?.prompt ?? item.input?.original_name ?? ""}
w={w}
h={h}
@@ -966,7 +969,7 @@ export function Stage() {
) : item.gen && base ? (
<CompareFrame key={`compare-${item.gen.id}`} g={item.gen} base={base} maskSrc={maskSrc} onFullscreen={openFull} />
) : (
- <PlainFrame key={src} src={src} alt={item.gen?.prompt ?? item.input?.original_name ?? ""} w={w} h={h} maskSrc={maskSrc} fill={!!item.gen} onFullscreen={openFull} />
+ <PlainFrame key={src} source={source} alt={item.gen?.prompt ?? item.input?.original_name ?? ""} w={w} h={h} maskSrc={maskSrc} fill={!!item.gen} onFullscreen={openFull} />
)}
</Hatch>
<StageBar item={item} label={labels[item.key]} showOriginal={showOriginal} setShowOriginal={(on) => setOriginalOf(on && g ? g.id : null)} lutBar={lutBar} onLut={startLut} />
diff --git a/src/lib/api.test.ts b/src/lib/api.test.ts
index f934601..6659a9a 100644
--- a/src/lib/api.test.ts
+++ b/src/lib/api.test.ts
@@ -19,6 +19,8 @@ describe("api in a browser", () => {
configure("/api/v1", "");
expect(urls.gen("abc")).toBe("/api/v1/gallery/abc/image");
expect(urls.inputThumb("x")).toBe("/api/v1/inputs/x/thumb");
+ expect(urls.sourcePreview("gen:abc")).toBe("/api/v1/gallery/abc/preview");
+ expect(urls.sourcePreview("input:x")).toBe("/api/v1/inputs/x/preview");
expect(urls.lutPreview("warm", "gen:1", 512)).toBe("/api/v1/luts/warm/preview?source=gen%3A1&max=512");
});
diff --git a/src/lib/api.ts b/src/lib/api.ts
index eb54d15..a07ac07 100644
--- a/src/lib/api.ts
+++ b/src/lib/api.ts
@@ -194,12 +194,16 @@ export const urls = {
inputThumb: (id: string) => `${base}/inputs/${id}/thumb${tq("?")}`,
gen: (id: string) => `${base}/gallery/${id}/image${tq("?")}`,
genThumb: (id: string) => `${base}/gallery/${id}/thumb${tq("?")}`,
+ /** Screen-sized copies for a browser's stage (at most `PREVIEW_SIDE` px, logic.ts). */
+ genPreview: (id: string) => `${base}/gallery/${id}/preview${tq("?")}`,
+ inputPreview: (id: string) => `${base}/inputs/${id}/preview${tq("?")}`,
/** The result before the upscaler enlarged it (`pipeline.original_path`). */
genOriginal: (id: string) => `${base}/gallery/${id}/original${tq("?")}`,
/** Full image for a "gen:<id>" / "input:<id>" source. */
source: (s: string) => (s.startsWith("gen:") ? urls.gen(s.slice(4)) : urls.input(s.slice(6))),
/** A LUT on a "gen:"/"input:" source at full strength, at most `max` px on its long side. */
lutPreview: (name: string, source: string, max: number) => `${base}/luts/${encodeURIComponent(name)}/preview?source=${encodeURIComponent(source)}&max=${max}${tq("&")}`,
+ sourcePreview: (s: string) => (s.startsWith("gen:") ? urls.genPreview(s.slice(4)) : urls.inputPreview(s.slice(6))),
sourceThumb: (s: string) => (s.startsWith("gen:") ? urls.genThumb(s.slice(4)) : urls.inputThumb(s.slice(6))),
};
diff --git a/src/lib/logic.test.ts b/src/lib/logic.test.ts
index 73961ee..68a9301 100644
--- a/src/lib/logic.test.ts
+++ b/src/lib/logic.test.ts
@@ -3,6 +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,
} from "./logic";
import type { GenerationRecord, InstallItem, JobView, LoraItem, Plan } from "./types";
@@ -561,3 +562,36 @@ describe("page version", () => {
expect(pageOutdated("", "0.11.0", null)).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);
+ // iPhone (390 px wide, 3x): a square image fills the width, 1170 device px.
+ expect(stageFitsPreview(390, 390, 3)).toBe(true);
+ // A portrait image there is drawn taller than the preview.
+ expect(stageFitsPreview(366, 488, 3)).toBe(false);
+ // A desktop browser: fits at 1x, not on a 2x screen.
+ expect(stageFitsPreview(1000, 750, 1)).toBe(true);
+ expect(stageFitsPreview(1000, 750, 2)).toBe(false);
+ expect(stageFitsPreview(1280, 1280, 1)).toBe(true);
+ expect(stageFitsPreview(1281, 600, 1)).toBe(false);
+ // Not measured yet, or an odd devicePixelRatio.
+ expect(stageFitsPreview(0, 0, 3)).toBe(false);
+ expect(stageFitsPreview(600, 600, 0)).toBe(true);
+ });
+
+ it("drops only the versions the service knew and has no record for", () => {
+ const items = [
+ { key: "i", kind: "input", inputId: "in1" },
+ { key: "a", kind: "gen", genId: "g1" },
+ { key: "gone", kind: "gen", genId: "g2" },
+ { key: "new", kind: "gen", genId: "g3" },
+ { key: "j", kind: "job" },
+ ];
+ const records = { generations: { g1: {} }, inputs: { in1: {} } };
+ expect([...goneVersions(items, records, ["i", "a", "gone", "j"])]).toEqual(["gone"]);
+ // A deleted input is dropped too; with no copy known, nothing is dropped.
+ expect([...goneVersions(items, { generations: { g1: {}, g2: {}, g3: {} }, inputs: {} }, ["i"])]).toEqual(["i"]);
+ expect(goneVersions(items, { generations: {}, inputs: {} }, []).size).toBe(0);
+ });
+});
diff --git a/src/lib/store.ts b/src/lib/store.ts
index e4e63a5..2a96b51 100644
--- a/src/lib/store.ts
+++ b/src/lib/store.ts
@@ -8,8 +8,8 @@
import { inBrowser, isNarrow } from "./platform";
import { useSyncExternalStore } from "react";
import { markFresh } from "../components/Cocoon";
-import { api, ApiError, setPrivateInputs, subscribe } from "./api";
-import { activeLoras, buildRequest, composing, emptyComposer, goneVersions, needsPrompt, newKey, pageOutdated, persistentAdvanced, refFromGen, refFromInput, reviewRequest, starPromptDue, timelineLinks, undoTarget, versionLabels, wantsPromptReview, type CompareMode, type Composer, type Ref } from "./logic";
+import { api, ApiError, setPrivateInputs, subscribe, urls } from "./api";
+import { activeLoras, buildRequest, composing, emptyComposer, goneVersions, needsPrompt, newKey, pageOutdated, persistentAdvanced, refFromGen, refFromInput, reviewRequest, stageFitsPreview, starPromptDue, timelineLinks, undoTarget, versionLabels, wantsPromptReview, type CompareMode, type Composer, type Ref } from "./logic";
import type { AppErrorBody, GenerationRecord, GenerationRequest, InputRecord, JobView, LoraItem, ServerState, StatusResponse } from "./types";
export type Overlay = null | "gallery" | "settings" | "inpaint" | "expand" | "welcome" | "whats-new" | "prompt-tips" | "shortcuts";
@@ -742,13 +742,38 @@ export async function switchProject(id: string, selectKey?: string) {
const data = projects[id];
if (!data) return;
const token = ++switching;
- const session = await hydrate(data.items);
+ preloadStage(data, selectKey);
+ const session = await hydrate(data.items, id);
if (token !== switching) return;
const keep = (k: string | null | undefined) => (k && session.some((i) => i.key === k) ? k : null);
const selected = keep(selectKey) ?? keep(data.selected) ?? session[session.length - 1]?.key ?? null;
setState((s) => ({ projectId: id, session, selected, overlay: null, redo: [], composer: { ...composerFor(s.composer, session, selected), loras: data.loras ?? null } }));
}
+// In a browser the stage shows a screen-sized preview when it is as sharp there (logic.ts
+// stageFitsPreview). Until a stage has measured itself, a phone is assumed to fit.
+let stageFits = isNarrow();
+let preloading: HTMLImageElement | null = null;
+
+/** The image the stage loads, drawn at `w`×`h` CSS px: in a browser the preview when it fits,
+ * else (and always in the desktop app) the full image. */
+export function stageImage(source: string, w: number, h: number): string {
+ if (!inBrowser) return urls.source(source);
+ if (w > 0 && h > 0) stageFits = stageFitsPreview(w, h, window.devicePixelRatio || 1);
+ return stageFits ? urls.sourcePreview(source) : urls.source(source);
+}
+
+/** A project is being opened: its selected version's preview starts loading now, alongside
+ * its records, instead of after them (a browser only). */
+function preloadStage(data: ProjectData, selectKey?: string) {
+ if (!inBrowser || !stageFits) return;
+ const it = data.items.find((i) => i.key === selectKey) ?? data.items.find((i) => i.key === data.selected) ?? data.items.findLast((i) => i.kind !== "job");
+ const src = it && storedSource(it);
+ if (!src) return;
+ preloading = new Image();
+ preloading.src = urls.sourcePreview(src);
+}
+
/** Switches the project's LoRAs (the composer's LoRA badge); new projects start with the ones
* on by default. */
export function setProjectLoras(ids: string[]) {