From 292ce6ab55a3c18636fe599cfbddc20ebc32cea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hartmut=20N=C3=B6renberg?= <55043719+hnoerenberg@users.noreply.github.com> Date: Tue, 3 Nov 2026 16:12:16 +0100 Subject: [PATCH] fix(ui): Minimum and Maximum preview at the scale they apply (#10474) (#10524) * fix(ui): Minimum and Maximum preview at the scale they apply (#10474) Filter > Other > Minimum... and Maximum... were missing from FULL_RESOLUTION_PREVIEW, so on any document large enough to be proxied their live preview was not what OK writes. preview_factor keeps the largest proxy factor at which no pixel-sized parameter falls below its command's own minimum, and for these two commands that minimum is 0.2 (engine/src/filters/validation.rs:90). A typed radius of 1.5 px therefore allows k = 4, and scale_preview_params divides it to 0.375 px. Both kernels then measure that in whole pixels and return the crop untouched when it reads 0: Squareness is `let r = radius.max(0.0).round() as i32;` (algo/src/other.rs:83) and Roundness is `let r = radius.max(0.0); let ri = r.floor() as i32;` (other.rs:23-24). So the dialog showed the untouched document while OK applied a real 1.5 px (Squareness) or 1 px (Roundness) effect - the same failure mode Dust & Scratches already has in that list for (#3265). The repo already asserts this rule for the filters that are listed, in fixed_size_filters_preview_at_the_scale_they_apply; Minimum and Maximum simply were not in it. Fail-first: four rows (1.5 px in both Preserve modes, both filters) added to that test failed with the production change stashed - `assert_eq!(k, 1, "filter.other.minimum")` reported left: 4, right: 1 - and pass with it. filter_dialog group 177 pass / 0 fail; full cargo test -p photocraft-ui-egui --lib 4440 pass / 22 fail, a strict subset of the 24 failures a clean 422ca7be checkout gives in this environment (the i18n, font-picker, type-tool and layer-row tests that need a CRAFT_FONTS_DIR checkout this machine has no access to); nothing new failed. clippy --all-targets -D warnings clean, fmt --check clean, cargo xtask layers OK (31 crates), cargo xtask wasm ok. Not touched, on purpose: the kernel math, the 0.2..500 radius range, and the fractional-radius question in #10474. Min/max over an integer lattice cannot represent a fractional radius at all, and the two Preserve options already snap at different thresholds (0.5 px for Squareness, 1.0 px for Roundness). Picking graded output or one shared rule needs reference captures, and nothing in docs/ records the reference behaviour today; guessing would put the guess in the tests as the oracle. The question stays open on the issue. No doc row either: #2063 and #3265 appear only in code comments, not in any docs table. Co-Authored-By: Claude Opus 5.5 * fix(ui): Minimum and Maximum keep one proxy pixel of radius, not a full-size preview (#10474) Previewing every radius at full size fixed the identity preview but made a large radius (up to 500 px, Roundness is O(radius) per pixel) cost seconds to minutes per tick on an ordinary photo. The bug only appears when radius / k drops under one kernel pixel, so cap k at floor(radius): 1.5 px still previews at k = 1, while 50 px keeps the 1/4 proxy. Co-Authored-By: Claude Opus 5.5 * fix(ui): Squareness previews its radius within 1 px of what OK applies (#10474) Capping k at floor(radius) kept a proxy pixel of radius, but Squareness rounds the proxy radius: 6 px at k = 4 is round(1.5) = 2 proxy px, an 8 px preview for a 6 px result. Take the largest k whose rounded proxy radius, scaled back, is within 1 px of round(radius) (k = 3 for 6 px and 50 px); Roundness reads its disc from the unrounded radius and keeps the floor(radius) cap. The radius is clamped to the command's 0.2-500 range first, as the engine does. Co-Authored-By: Claude Opus 5.5 * fix(ui): the canvas keeps the last preview frame when a drag changes the proxy factor (#10474) Minimum and Maximum's factor now follows the radius, so a radius drag crossing a whole pixel changed FilterPreviewKey::k, and the display gate (same_target, which includes k) dropped the held frame: the canvas showed the unfiltered document until a frame for the new factor landed. The frame is drawn at its own k (textures read preview.key.k), so the gate now forgives the factor alone (same_target_at_any_scale); publishing and freshness keep the strict checks. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/ui-egui/src/canvas/filter_preview.rs | 5 ++-- crates/ui-egui/src/dialogs/filter_dialog.rs | 25 ++++++++++++++++++- .../src/dialogs/filter_dialog/tests.rs | 22 ++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/canvas/filter_preview.rs b/crates/ui-egui/src/canvas/filter_preview.rs index a7d3f8dc94..0144390b46 100644 --- a/crates/ui-egui/src/canvas/filter_preview.rs +++ b/crates/ui-egui/src/canvas/filter_preview.rs @@ -135,8 +135,9 @@ pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &e ctx.request_repaint_after(std::time::Duration::from_millis(16)); } let preview = app.filter_preview.as_ref()?; - // Keep the last preview during a drag, but never one from another source or dialog. - if !preview.key.same_target(&request) { + // Keep the last preview during a drag, but never one from another source or dialog. It is + // drawn at its own factor, so a drag that changes the factor keeps it too (#10474). + if !preview.key.same_target_at_any_scale(&request) { return None; } let result = preview.result.as_ref()?; diff --git a/crates/ui-egui/src/dialogs/filter_dialog.rs b/crates/ui-egui/src/dialogs/filter_dialog.rs index 0f87c26faf..08ce0f6ab1 100644 --- a/crates/ui-egui/src/dialogs/filter_dialog.rs +++ b/crates/ui-egui/src/dialogs/filter_dialog.rs @@ -276,6 +276,22 @@ pub fn preview_factor(command: &str, params: &Value, k: u32) -> u32 { }, _ => k, }); + // Minimum and Maximum measure the radius in whole pixels (Squareness rounds it, Roundness + // floors it, and 0 is the identity), so at k = 4 a typed 1.5 px became 0.375 = 0 proxy px and + // the preview showed the untouched document while OK applied a real effect (#10474). Keep at + // least one proxy pixel of radius, and for Squareness the largest k whose rounded proxy radius, + // scaled back, is within 1 px of the one OK applies (6 px at k = 4 rounded to 2 proxy px, 8 px). + // A large radius still previews on a proxy: at full size Roundness costs O(radius) per pixel. + if matches!(command, "filter.other.minimum" | "filter.other.maximum") { + let Some(r) = params.get("radius").and_then(Value::as_f64).filter(|r| r.is_finite()) else { return k }; + let r = r.clamp(0.2, 500.0); + let square = params.get("preserve").and_then(Value::as_str) != Some("roundness"); + let fits = |d: u32| { + let d = f64::from(d); + d <= r.floor() && (!square || ((r / d).round() * d - r.round()).abs() <= 1.0) + }; + return (1..=k).rev().find(|&d| fits(d)).unwrap_or(1); + } // Mosaic rounds its cells to whole pixels: 10 px cells at k = 4 became 3 proxy px = 12 px. // Divide the cell size evenly instead. match params.get("cellSize").and_then(Value::as_f64) { @@ -1353,13 +1369,20 @@ impl FilterPreviewKey { /// parameters moved on. A reopened dialog has a new id, a different command or a different /// document, and a committed document a new revision, so neither survives this test. pub fn same_target(&self, other: &Self) -> bool { + self.k == other.k && self.same_target_at_any_scale(other) + } + + /// [`Self::same_target`] at any proxy factor: whether the canvas may keep showing this frame, + /// drawn at its own `k`, while the factor moves under a drag. A parameter can set the factor + /// (Minimum and Maximum's radius, Mosaic's cell size, #10474), and the frame for the old one is + /// still the closest thing to the result until the new one lands. + pub fn same_target_at_any_scale(&self, other: &Self) -> bool { self.doc == other.doc && self.revision == other.revision && self.dialog == other.dialog && self.active == other.active && self.target == other.target && self.command == other.command - && self.k == other.k } } diff --git a/crates/ui-egui/src/dialogs/filter_dialog/tests.rs b/crates/ui-egui/src/dialogs/filter_dialog/tests.rs index 10f5a073a1..c5013adc25 100644 --- a/crates/ui-egui/src/dialogs/filter_dialog/tests.rs +++ b/crates/ui-egui/src/dialogs/filter_dialog/tests.rs @@ -133,6 +133,8 @@ fn same_target_ignores_parameters_but_nothing_else() { break_it(&mut other); assert!(!other.same_target(&base), "{other:?} must not pass as {base:?}"); assert!(!base.same_target(&other)); + // The canvas's display gate forgives the factor alone (#10474), nothing else. + assert_eq!(other.same_target_at_any_scale(&base), other.k != base.k, "{other:?}"); } } @@ -966,6 +968,13 @@ fn fixed_size_filters_preview_at_the_scale_they_apply() { ("filter.noise.dustAndScratches", json!({"radius": 4.0, "threshold": 0.0})), ("filter.noise.reduceNoise", json!({})), ("filter.other.custom", json!({"kernel": edge})), + // #10474: Minimum and Maximum work on whole-pixel radii (`round()` / `floor()`), so at + // k = 4 a typed 1.5 px radius previewed as 0.375 px = 0 px: the proxy showed the untouched + // document while OK writes a real 1.5 / 1 px effect. A radius under 2 px previews at k = 1. + ("filter.other.minimum", json!({"radius": 1.5, "preserve": "squareness"})), + ("filter.other.minimum", json!({"radius": 1.5, "preserve": "roundness"})), + ("filter.other.maximum", json!({"radius": 1.5, "preserve": "squareness"})), + ("filter.other.maximum", json!({"radius": 1.5, "preserve": "roundness"})), ("filter.video.deInterlace", json!({})), ("filter.stylize.traceContour", json!({})), ("filter.stylize.wind", json!({"method": "blast"})), @@ -1038,6 +1047,19 @@ fn proxy_previews_scale_only_lengths() { assert_eq!(preview_factor("filter.pixelate.mosaic", &json!({"cellSize": "x"}), 4), 4); let (k, diff) = error("filter.pixelate.mosaic", json!({"cellSize": 10.0})); assert!(k == 2 && diff < 0.01, "Mosaic: {diff}"); + // Minimum and Maximum keep at least one proxy pixel of radius (#10474), and a large radius + // keeps a proxy instead of a full-size preview that costs minutes at radius 500. Squareness + // also keeps its rounded radius within 1 px of OK's: 6 px is 2 proxy px at k = 3 (6 px), not + // at k = 4 (8 px); 50 px is 17 at k = 3 (51 px), not 13 at k = 4 (52 px). + let factors = |cmd: &str, preserve: &str| { + [0.2, 1.5, 2.5, 3.9, 6.0, 50.0, 500.0, 9999.0].map(|radius| preview_factor(cmd, &json!({"radius": radius, "preserve": preserve}), 4)) + }; + for cmd in ["filter.other.minimum", "filter.other.maximum"] { + assert_eq!(factors(cmd, "squareness"), [1, 1, 2, 3, 3, 3, 4, 4], "{cmd} squareness"); + assert_eq!(factors(cmd, "roundness"), [1, 1, 2, 3, 4, 4, 4, 4], "{cmd} roundness"); + assert_eq!(preview_factor(cmd, &json!({"radius": 6.0}), 4), 3, "{cmd}: Squareness is the default"); + assert_eq!(preview_factor(cmd, &json!({"radius": "x"}), 4), 4, "{cmd}: a junk radius keeps the proxy"); + } // Tree's and Picture Frame's sizes are relative, not pixels (and not shown as "px"). assert!(!is_pixel_param_of("filter.render.tree", "size") && !is_pixel_param_of("filter.render.pictureFrame", "size")); assert!(is_pixel_param_of("filter.stylize.extrude", "size"));