LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
#323: Detail view renders at the size it is shown, not at a hidden 2560 px cap
Nathaniel Ledford14570cfdf3b1
diff --git a/crates/ui-egui/locales/ja.json b/crates/ui-egui/locales/ja.jsonindex 3e9c75560..7f679125c 100644--- a/crates/ui-egui/locales/ja.json+++ b/crates/ui-egui/locales/ja.json@@ -829,7 +829,7 @@"Keywords you add appear here; ⌥1–⌥9 apply them.": "追加したキーワードはここに表示されます(⌥1〜⌥9で適用)","Landscape": "風景","Language": "言語",- "Largest long edge the Detail view renders at; larger is sharper on big displays but slower.": "詳細表示の長辺の上限です。大きいほど鮮明になりますが、描画に時間がかかります",+ "Automatic renders the Detail view at the size it is shown, up to the photo's own pixels. Choose a size to cap it: smaller is faster.": "自動では、詳細表示を表示サイズ(写真本来の画素数まで)で描画します。サイズを選ぶと上限になります。小さいほど高速です","Last": "前回","Last view": "前回の表示","Lateral Chromatic Aberration": "倍率色収差",diff --git a/crates/ui-egui/locales/pt-br.json b/crates/ui-egui/locales/pt-br.jsonindex f5b88f4f9..56a372f55 100644--- a/crates/ui-egui/locales/pt-br.json+++ b/crates/ui-egui/locales/pt-br.json@@ -712,7 +712,7 @@"Label Set": "Conjunto de rótulos","Landscape": "Paisagem","Language": "Idioma",- "Largest long edge the Detail view renders at; larger is sharper on big displays but slower.": "Maior aresta longa em que a exibição Detalhe é renderizada; maior deixa mais nítida em telas grandes, mas mais lenta.",+ "Automatic renders the Detail view at the size it is shown, up to the photo's own pixels. Choose a size to cap it: smaller is faster.": "Automático renderiza a exibição Detalhe no tamanho em que é mostrada, até os pixels da própria foto. Escolha um tamanho para limitá-la: menor é mais rápido.","Last": "Último","Last GPU fallback": "Último fallback de GPU","Last commit": "Último commit",diff --git a/crates/ui-egui/locales/zh-hans.json b/crates/ui-egui/locales/zh-hans.jsonindex 8e7b13121..1a2ec1286 100644--- a/crates/ui-egui/locales/zh-hans.json+++ b/crates/ui-egui/locales/zh-hans.json@@ -809,7 +809,7 @@"Keywords you add appear here; ⌥1–⌥9 apply them.": "您添加的关键字会显示在此处;按 ⌥1–⌥9 即可应用。","Landscape": "横向","Language": "语言",- "Largest long edge the Detail view renders at; larger is sharper on big displays but slower.": "详细视图渲染的最大长边;在大显示器上越大越清晰,但速度更慢。",+ "Automatic renders the Detail view at the size it is shown, up to the photo's own pixels. Choose a size to cap it: smaller is faster.": "“自动”按详细视图的显示大小渲染(最高为照片自身像素)。选择一个大小可设上限:越小越快。","Last": "上次","Last view": "上次视图","Lateral Chromatic Aberration": "横向色差",diff --git a/crates/ui-egui/locales/zh-hant.json b/crates/ui-egui/locales/zh-hant.jsonindex 71a2bb5f9..57170c3f3 100644--- a/crates/ui-egui/locales/zh-hant.json+++ b/crates/ui-egui/locales/zh-hant.json@@ -809,7 +809,7 @@"Keywords you add appear here; ⌥1–⌥9 apply them.": "新增的關鍵字會顯示於此;⌥1–⌥9 可套用。","Landscape": "風景","Language": "語言",- "Largest long edge the Detail view renders at; larger is sharper on big displays but slower.": "詳細檢視預覽的最長邊尺寸;較大的尺寸在大螢幕上更清晰,但速度較慢。",+ "Automatic renders the Detail view at the size it is shown, up to the photo's own pixels. Choose a size to cap it: smaller is faster.": "「自動」會依詳細檢視的顯示大小來渲染(最高為相片本身的像素)。選擇尺寸可設定上限:越小越快。","Last": "最後","Last view": "上次檢視","Lateral Chromatic Aberration": "橫向色差",diff --git a/crates/ui-egui/src/headless.rs b/crates/ui-egui/src/headless.rsindex 4411c35ac..b803a36c8 100644--- a/crates/ui-egui/src/headless.rs+++ b/crates/ui-egui/src/headless.rs@@ -1882,8 +1882,8 @@ mod tests {h.request("ui.clickWidget", json!({"id": "button:settingsTab-performance"}), t);h.request("ui.clickWidget", json!({"id": "button:settingsCache-0"}), t);assert_eq!(h.app.session.cache_mb, 512);- h.request("ui.clickWidget", json!({"id": "button:settingsPreview-2"}), t);- assert_eq!(h.app.ui.settings.preview_edge, 3840);+ h.request("ui.clickWidget", json!({"id": "button:settingsPreview-3"}), t);+ assert_eq!(h.app.ui.settings.preview_limit, 3840);h.request("ui.clickWidget", json!({"id": "button:settingsMemory-2"}), t);assert_eq!(h.app.ui.settings.memory_mb, 1024);h.step();diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 502e31ffe..98e9367e4 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -48,6 +48,8 @@ mod tests_offline;#[cfg(test)]mod tests_panels;#[cfg(test)]+mod tests_preview_limit;+#[cfg(test)]mod tests_quit_unsaved;#[cfg(test)]mod tests_scroll;diff --git a/crates/ui-egui/src/menubar.rs b/crates/ui-egui/src/menubar.rsindex 57e0477fd..b6deccab3 100644--- a/crates/ui-egui/src/menubar.rs+++ b/crates/ui-egui/src/menubar.rs@@ -360,7 +360,7 @@ fn expanded(app: &LightcraftApp, name: &str) -> Option<Vec<MenuNode>> {vec![item("library.buildPreviews",- json!({"size": "standard", "edge": app.ui.settings.preview_edge}),+ json!({"size": "standard", "edge": app.ui.settings.standard_preview_edge()}),crate::i18n::tr_format!("Build Standard-Sized Previews ({scope})", scope = crate::i18n::tr(scope)),None,!running,diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex a4eb6f31c..7a2106517 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -959,7 +959,13 @@ pub fn run_ui_command(app: &mut LightcraftApp, id: &str, p: &Value) -> Option<Re),("GPU rendering".to_string(), if app.ui.settings.gpu { "on".into() } else { "off".into() }),("Memory budget".to_string(), mb(lightcraft_engine::memory::default_budget() as u64)),- ("Preview size".to_string(), format!("{} px", app.ui.settings.preview_edge)),+ (+ "Preview size".to_string(),+ match app.ui.settings.preview_limit {+ 0 => "automatic".to_string(),+ n => format!("{n} px"),+ },+ ),("Photos".to_string(), info["photos"].to_string()),("Albums".to_string(), info["albums"].to_string()),];diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rsindex 6c1b45500..ca74d623a 100644--- a/crates/ui-egui/src/panels/detail.rs+++ b/crates/ui-egui/src/panels/detail.rs@@ -230,7 +230,6 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {} else {24.0});- let max_edge = app.ui.settings.preview_edge.clamp(512, 8192) as f32;let native = [photo.width.max(1) as usize, photo.height.max(1) as usize];// two views (before, after): side by side or stackedlet split = matches!(app.ui.before_after, BeforeAfter::SideBySide | BeforeAfter::TopBottom);@@ -283,7 +282,8 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {let interacting = app.session.interaction.is_some();let scale = if interacting { 0.6 } else { 1.0 };// render at the final size: a click-zoom animation only changes how the result is drawn- let want = (target_rect.width().max(target_rect.height()) * ppp * scale).min(max_edge) as usize;+ let native_long = native[0].max(native[1]);+ let want = app.ui.settings.loupe_edge(target_rect.width().max(target_rect.height()) * ppp * scale, native_long);let (rw, rh) = if aspect >= 1.0 { (want, (want as f32 / aspect) as usize) } else { ((want as f32 * aspect) as usize, want) };if let Some(job) = app.session.loupe_job(id, rw.max(8), rh.max(8), !crop_tool) {let job = if interacting { job.draft() } else { job };@@ -313,7 +313,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {let nf = Frame::with_lens(np.width.max(1) as usize, np.height.max(1) as usize, &np.develop, !crop_tool, np.embedded_lens.as_ref());let na = nf.aspect() as f32;let nr = fit_rect(main_area, na, app.ui.zoom, [np.width.max(1) as usize, np.height.max(1) as usize], ppp, app.ui.pan);- let nw = (nr.width().max(nr.height()) * ppp).min(max_edge) as usize;+ let nw = app.ui.settings.loupe_edge(nr.width().max(nr.height()) * ppp, np.width.max(np.height).max(1) as usize);let (w, h) = if na >= 1.0 { (nw, (nw as f32 / na) as usize) } else { ((nw as f32 * na) as usize, nw) };if let Some(job) = app.session.loupe_job(nid, w.max(8), h.max(8), !crop_tool) {app.renderer.prefetch(Slot::Prefetch(n as u8), job, PREFETCH_PRIORITY);diff --git a/crates/ui-egui/src/panels/settings.rs b/crates/ui-egui/src/panels/settings.rsindex e16d88154..8c7206c39 100644--- a/crates/ui-egui/src/panels/settings.rs+++ b/crates/ui-egui/src/panels/settings.rs@@ -12,7 +12,7 @@ use egui::RichText;use serde_json::{Value, json};use crate::LightcraftApp;-use crate::state::{GridBadges, PREVIEW_EDGES, StartupView};+use crate::state::{GridBadges, PREVIEW_LIMITS, StartupView};use crate::theme::Tokens;use crate::widgets::register;@@ -372,11 +372,18 @@ fn performance_tab(app: &mut LightcraftApp, ui: &mut egui::Ui, t: &Tokens) {};hint(ui, t, &status);row(ui, t, crate::i18n::tr("Preview size"), |ui| {- let opts: Vec<(u32, String)> = PREVIEW_EDGES.iter().map(|e| (*e, format!("{e} px"))).collect();+ let opts: Vec<(u32, String)> =+ PREVIEW_LIMITS.iter().map(|e| (*e, if *e == 0 { crate::i18n::tr("Automatic").to_string() } else { format!("{e} px") })).collect();let opts: Vec<(u32, &str)> = opts.iter().map(|(e, l)| (*e, l.as_str())).collect();- choices(ui, "settingsPreview", &opts, &mut app.ui.settings.preview_edge);+ choices(ui, "settingsPreview", &opts, &mut app.ui.settings.preview_limit);});- hint(ui, t, crate::i18n::tr("Largest long edge the Detail view renders at; larger is sharper on big displays but slower."));+ hint(+ ui,+ t,+ crate::i18n::tr(+ "Automatic renders the Detail view at the size it is shown, up to the photo's own pixels. Choose a size to cap it: smaller is faster.",+ ),+ );row(ui, t, crate::i18n::tr("Memory for caches"), |ui| {let auto = crate::i18n::tr_format!("Automatic ({} MB)", lightcraft_engine::memory::default_budget() >> 20);let opts = [(0u32, auto.as_str()), (512, "512 MB"), (1024, "1 GB"), (2048, "2 GB"), (4096, "4 GB")];diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rsindex b57be4b9b..e24c6279f 100644--- a/crates/ui-egui/src/state.rs+++ b/crates/ui-egui/src/state.rs@@ -140,8 +140,10 @@ pub struct AppSettings {pub confirm_delete: bool,/// GPU rendering allowed (`app.gpu`).pub gpu: bool,- /// Largest long edge (pixels) the loupe renders at.- pub preview_edge: u32,+ /// Lowest long edge (pixels) the user lets the loupe render at; 0 = Automatic (the size it is+ /// drawn at, up to the photo's own pixels and [`LOUPE_EDGE_CEILING`]). Not the old+ /// `previewEdge` key: its 2560 px default was the soft-image bug (issue #323).+ pub preview_limit: u32,/// Edit in External Editor: the application ("" = the system's default for TIFF files).pub external_editor: String,/// Memory the caches may hold together, in MB (0 = automatic; `app.memoryBudget`).@@ -163,7 +165,7 @@ impl Default for AppSettings {startup_view: StartupView::Last,confirm_delete: false,gpu: true,- preview_edge: 2560,+ preview_limit: 0,external_editor: String::new(),memory_mb: 0,film_names: true,@@ -174,8 +176,29 @@ impl Default for AppSettings {}}-/// Preview sizes offered in Settings → Performance.-pub const PREVIEW_EDGES: [u32; 4] = [1600, 2560, 3840, 5120];+/// Preview size limits offered in Settings → Performance (0 = Automatic).+pub const PREVIEW_LIMITS: [u32; 5] = [0, 1600, 2560, 3840, 5120];++/// The most the loupe ever renders in one go (memory and GPU texture size), whatever the setting.+pub const LOUPE_EDGE_CEILING: u32 = 8192;++/// Long edge of Build Standard-Sized Previews when the limit is Automatic.+pub const STANDARD_PREVIEW_EDGE: u32 = 2560;++impl AppSettings {+ /// Long edge (pixels) to render the loupe at when it is drawn `wanted_px` wide on screen:+ /// that size, never above the photo's own `native_long_edge`, the user's limit or the ceiling.+ pub fn loupe_edge(&self, wanted_px: f32, native_long_edge: usize) -> usize {+ let ceiling = if self.preview_limit == 0 { LOUPE_EDGE_CEILING } else { self.preview_limit.clamp(512, LOUPE_EDGE_CEILING) } as usize;+ let wanted = if wanted_px.is_nan() { 8.0 } else { wanted_px.clamp(8.0, ceiling as f32) } as usize;+ wanted.min(native_long_edge.max(8)).min(ceiling)+ }++ /// The edge Build Standard-Sized Previews uses.+ pub fn standard_preview_edge(&self) -> u32 {+ if self.preview_limit == 0 { STANDARD_PREVIEW_EDGE } else { self.preview_limit }+ }+}/// Click-zoom ratios offered (percent): 1:1, 2:1, 3:1, 4:1, 8:1.pub const CLICK_ZOOMS: [u32; 5] = [100, 200, 300, 400, 800];@@ -685,8 +708,8 @@ impl UiState {self.brush_size = self.brush_size.clamp(0.002, 0.5);self.dialog = None;self.fullscreen = false;- if !crate::state::PREVIEW_EDGES.contains(&self.settings.preview_edge) {- self.settings.preview_edge = AppSettings::default().preview_edge;+ if !crate::state::PREVIEW_LIMITS.contains(&self.settings.preview_limit) {+ self.settings.preview_limit = AppSettings::default().preview_limit;}match self.settings.startup_view {StartupView::Last => {}diff --git a/crates/ui-egui/src/tests_preview_limit.rs b/crates/ui-egui/src/tests_preview_limit.rsnew file mode 100644index 000000000..53476886b--- /dev/null+++ b/crates/ui-egui/src/tests_preview_limit.rs@@ -0,0 +1,143 @@+//! Issue #323: the Detail view must not be softened by a hidden render-size cap. By default the+//! loupe renders at the size it is drawn at (up to the photo's own pixels and a safety ceiling);+//! "Preview size" in Settings only lowers that when the user asks for it.++use crate::state::{AppSettings, LOUPE_EDGE_CEILING, PREVIEW_LIMITS, STANDARD_PREVIEW_EDGE};++fn with_limit(preview_limit: u32) -> AppSettings {+ AppSettings { preview_limit, ..Default::default() }+}++// Given a fresh install+#[test]+fn default_is_automatic() {+ assert_eq!(AppSettings::default().preview_limit, 0);+ assert_eq!(PREVIEW_LIMITS[0], 0, "Automatic is the first choice");+}++// Given Automatic, when a 24 MP photo is shown at 1:1, then the render is the photo's own width+#[test]+fn automatic_renders_a_one_to_one_view_at_native_size() {+ assert_eq!(with_limit(0).loupe_edge(6000.0, 6000), 6000);+}++// Given Automatic, a Retina fit view of 3024 px is not held to the old 2560 px cap+#[test]+fn automatic_does_not_cap_a_fit_view_at_the_old_default() {+ assert_eq!(with_limit(0).loupe_edge(3024.0, 6000), 3024);+}++// Given Automatic, a small photo zoomed far in is not rendered above its own pixels+#[test]+fn never_renders_above_the_photos_own_pixels() {+ assert_eq!(with_limit(0).loupe_edge(8000.0, 1000), 1000);+}++// Given Automatic, a huge zoom on a huge photo stays under the memory/GPU ceiling+#[test]+fn automatic_is_bounded_by_the_ceiling() {+ assert_eq!(with_limit(0).loupe_edge(48000.0, 12000), LOUPE_EDGE_CEILING as usize);+}++// Given the user chose 1600 px, the render never exceeds it+#[test]+fn an_explicit_limit_caps_the_render() {+ assert_eq!(with_limit(1600).loupe_edge(6000.0, 6000), 1600);+ assert_eq!(with_limit(1600).loupe_edge(900.0, 6000), 900, "and does not raise a small view");+}++// Whatever the inputs (hostile or degenerate), the result is a usable size+#[test]+fn degenerate_inputs_give_a_usable_size() {+ let s = with_limit(0);+ for wanted in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -5.0, 0.0] {+ let e = s.loupe_edge(wanted, 6000);+ assert!((8..=LOUPE_EDGE_CEILING as usize).contains(&e), "{wanted}: {e}");+ }+ assert!((8..=LOUPE_EDGE_CEILING as usize).contains(&s.loupe_edge(1000.0, 0)));+ assert!(with_limit(7).loupe_edge(1000.0, 6000) >= 8, "a corrupt limit is clamped");+}++// Build Standard-Sized Previews needs a number even when the limit is Automatic+#[test]+fn standard_previews_use_the_limit_or_the_standard_size() {+ assert_eq!(with_limit(0).standard_preview_edge(), STANDARD_PREVIEW_EDGE);+ assert_eq!(with_limit(3840).standard_preview_edge(), 3840);+}++// Given a ui.json from before this setting (key previewEdge), when it loads, then the user is on+// Automatic: the old default of 2560 was the bug, and the key no longer means anything+#[test]+fn an_old_saved_preview_edge_does_not_pin_the_cap() {+ let s: AppSettings = serde_json::from_str(r#"{"previewEdge": 2560}"#).unwrap();+ assert_eq!(s.preview_limit, 0);+}++// A saved limit is kept; an unknown one (hand-edited file) falls back to Automatic+#[test]+fn saved_limits_round_trip_and_unknown_ones_are_reset() {+ let mut ui = crate::UiState::default();+ ui.settings.preview_limit = 3840;+ let back = serde_json::from_str::<crate::UiState>(&serde_json::to_string(&ui).unwrap()).unwrap().sanitized();+ assert_eq!(back.settings.preview_limit, 3840);+ ui.settings.preview_limit = 123;+ assert_eq!(ui.sanitized().settings.preview_limit, 0);+}++// ---- Behaviour in the running UI -------------------------------------------------------------++mod in_the_loupe {+ use std::time::Duration;++ use serde_json::json;++ use crate::headless::Headless;+ use crate::render::Slot;+ use crate::{LightcraftApp, Services};++ const T: Duration = Duration::from_secs(20);+ const SETTLE: Duration = Duration::from_secs(120);++ /// The demo library in a 1400×900 window, the first photo open in Detail.+ fn detail() -> (Headless, usize) {+ let services = Services { png: None, ..Default::default() };+ let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), services);+ let mut h = Headless::new(app, [1400.0, 900.0], 1.0);+ let r = h.request("ui.set", json!({"view": "detail", "right": "none", "filmstrip": false}), T);+ assert_eq!(r["ok"], true, "{r}");+ h.settle(SETTLE);+ let native = {+ let id = h.app.session.active().expect("a photo is open");+ let p = h.app.session.catalog.photo(id).expect("it is in the catalog");+ p.width.max(p.height) as usize+ };+ (h, native)+ }++ fn rendered_long_edge(h: &Headless) -> usize {+ let t = h.app.renderer.textures.get(&Slot::Main).expect("the loupe rendered");+ t.size[0].max(t.size[1])+ }++ // Given a photo larger than the old 2560 px cap, when I zoom to 1:1, then the loupe holds its pixels+ #[test]+ fn one_to_one_is_rendered_at_the_photos_own_size() {+ let (mut h, native) = detail();+ h.request("engine.execute", json!({"command": "view.zoom100"}), T);+ h.settle(SETTLE);+ let want = native.min(crate::state::LOUPE_EDGE_CEILING as usize);+ let got = rendered_long_edge(&h);+ assert!(got.abs_diff(want) <= 2, "native {native}: rendered {got}, wanted {want}");+ }++ // Given the user capped the preview at 1600 px, then 1:1 is rendered no larger than that+ #[test]+ fn an_explicit_limit_still_caps_one_to_one() {+ let (mut h, native) = detail();+ assert!(native > 1600, "the demo photo must be larger than the cap for this to mean anything (is {native})");+ h.app.ui.settings.preview_limit = 1600;+ h.request("engine.execute", json!({"command": "view.zoom100"}), T);+ h.settle(SETTLE);+ assert!(rendered_long_edge(&h) <= 1600, "rendered {}", rendered_long_edge(&h));+ }+}