PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Gradient options bar: the swatch covers its checkerboard at half-pixel offsets (fixes #4577) (#4616)
Marco Arnulfo9716b2b319bd
diff --git a/crates/ui-egui/src/gradient_ui.rs b/crates/ui-egui/src/gradient_ui.rsindex 5fd87393d..5590c13f2 100644--- a/crates/ui-egui/src/gradient_ui.rs+++ b/crates/ui-egui/src/gradient_ui.rs@@ -13,6 +13,7 @@use std::sync::Arc;+use egui::emath::GuiRounding;use egui::{Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2};use photocraft_compose::gradient_fill as gf;use photocraft_doc::{Document, Fill, GradientStyle, Layer, LayerContent, LayerId};@@ -508,6 +509,9 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform/// A ramp of RGBA stops over a checkerboard in `r`.fn paint_ramp(p: &egui::Painter, r: Rect, sample: impl Fn(f32) -> [f32; 4]) {+ // The tessellator snaps the checker's rects to pixels but not the mesh: snap both alike, or a+ // half-pixel offset leaves a row of checker showing under the ramp (#4577).+ let r = r.round_to_pixels(p.pixels_per_point());widgets::checker(p, r, 5.0);let n = 64;let mut mesh = egui::Mesh::default();@@ -1390,4 +1394,42 @@ mod tests {assert!(active_gradient(&app).is_none(), "the canvas widget skips a hidden layer");assert!(selected_gradient(&app).is_some(), "the editor still edits it");}++ /// #4577: the tessellator snaps the checkerboard's rects to whole pixels but leaves the ramp's+ /// mesh where it was laid out, so at a half-pixel offset the ramp stopped a pixel short of the+ /// checker and a row of it showed under the gradient. The ramp must cover the checker exactly.+ #[test]+ fn the_ramp_covers_its_checkerboard_at_half_pixel_offsets() {+ let ctx = egui::Context::default();+ for ppp in [1.0, 2.0] {+ ctx.set_pixels_per_point(ppp);+ let r = Rect::from_min_size(pos2(10.0 + 0.5 / ppp, 7.0 + 0.5 / ppp), vec2(96.0, 20.0));+ // The new scale applies from the next frame.+ ctx.run_ui(egui::RawInput::default(), |_| {}).textures_delta.clear();+ let mut painted_at = 0.0;+ let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {+ painted_at = ui.ctx().pixels_per_point();+ paint_ramp(ui.painter(), r, |u| [u, u, u, 1.0]);+ });+ out.textures_delta.clear();+ assert_eq!(painted_at, ppp);+ let checker = out+ .shapes+ .iter()+ .find_map(|s| match &s.shape {+ egui::Shape::Rect(rs) if rs.fill == Color32::from_gray(250) => Some(rs.rect),+ _ => None,+ })+ .expect("the checkerboard's base rect");+ let ramp = out+ .shapes+ .iter()+ .find_map(|s| match &s.shape {+ egui::Shape::Mesh(m) => Some(m.calc_bounds()),+ _ => None,+ })+ .expect("the ramp mesh");+ assert_eq!(ramp, checker.round_to_pixels(ppp), "{ppp}x: the ramp covers every pixel of the checker");+ }+ }}