From 427aadecb1410687d30dacd22e339445c86a17c9 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 23:17:23 -0600 Subject: [PATCH 1/4] Mixer Brush: the load swatch shows a checkerboard or the sampled tip image (#11953) The Current Brush Load swatch now shows what the reservoir holds: the loaded colour after Load Brush, the 64 x 64 sampled tip image after an Alt+click (over the usual transparency checker), and a checkerboard when the brush is clean. The image's texture is uploaded again only when the load changes. --- crates/ui-egui/src/mixer_load_swatch_tests.rs | 121 ++++++++++++++++++ crates/ui-egui/src/panels/brush_controls.rs | 56 ++++++-- 2 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 crates/ui-egui/src/mixer_load_swatch_tests.rs diff --git a/crates/ui-egui/src/mixer_load_swatch_tests.rs b/crates/ui-egui/src/mixer_load_swatch_tests.rs new file mode 100644 index 0000000000..4e15f6d916 --- /dev/null +++ b/crates/ui-egui/src/mixer_load_swatch_tests.rs @@ -0,0 +1,121 @@ +//! The Mixer Brush's Current Brush Load swatch shows what the reservoir holds (#11953): a solid +//! colour after Load Brush, the sampled 64 × 64 tip image after an ⌥-click (an edge between green +//! and blue shows green on the left and blue on the right), and a checkerboard after Clean Brush. + +use egui::{Modifiers, Rect, Shape, TextureId, vec2}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::json; + +use super::mixer_load_image; +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; +const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; + +/// A pixel layer green left of x 100 and blue from there on, with the Mixer Brush active. +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 100, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 0, "y": 0, "width": 100, "height": 100})).unwrap(); + app.run("edit.fill", json!({"contents": "color", "color": GREEN})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 0, "width": 100, "height": 100})).unwrap(); + app.run("edit.fill", json!({"contents": "color", "color": BLUE})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.run("tools.setColors", json!({"foreground": "#ff0000", "background": "#ffffff"})).unwrap(); + app.ui.tool = Tool::MixerBrush; + crate::paint_mouse::sync_tool_brush(&mut app); + app.run("tools.setBrush", json!({"brush": {"size": 40, "hardness": 1.0}})).unwrap(); + app +} + +fn harness() -> Harness<'static, PhotocraftApp> { + let mut h = Harness::builder().with_size(vec2(1600.0, 120.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::panels::options_bar(app, ui); + }, + app(), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio); + h.run_steps(4); + h +} + +fn flatten<'a>(shape: &'a Shape, out: &mut Vec<&'a Shape>) { + if let Shape::Vec(v) = shape { + v.iter().for_each(|s| flatten(s, out)); + } else { + out.push(shape); + } +} + +/// What the swatch draws this frame: the fill colours of its rectangles and the textures of its +/// images, inside the swatch's chip. +fn swatch(h: &Harness<'static, PhotocraftApp>) -> (Vec, Vec) { + let r = h.get_by_label("Current Brush Load").rect(); + let chip = Rect::from_min_size(egui::pos2(r.left() + 4.0, r.center().y - 8.0), vec2(16.0, 16.0)).expand(0.5); + let (mut fills, mut textures) = (Vec::new(), Vec::new()); + for clipped in &h.output().shapes { + let mut all = Vec::new(); + flatten(&clipped.shape, &mut all); + for s in all { + match s { + Shape::Rect(rs) if chip.contains_rect(rs.rect) => fills.push(rs.fill), + Shape::Mesh(m) if m.texture_id != TextureId::default() && chip.contains_rect(m.calc_bounds()) => textures.push(m.texture_id), + _ => {} + } + } + } + (fills, textures) +} + +fn has_fonts(h: &Harness<'static, PhotocraftApp>) -> bool { + h.ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) +} + +#[test] +fn the_sampled_image_keeps_its_left_and_right() { + let load: Vec<[f32; 4]> = (0..64 * 64).map(|i| if i % 64 < 32 { GREEN } else { BLUE }).collect(); + let image = mixer_load_image(&load); + assert_eq!(image.size, [64, 64]); + assert_eq!(image[(10, 32)], egui::Color32::from_rgb(0, 255, 0)); + assert_eq!(image[(54, 32)], egui::Color32::from_rgb(0, 0, 255)); +} + +#[test] +fn the_swatch_shows_the_load_the_tip_image_or_a_checkerboard() { + let mut h = harness(); + if !has_fonts(&h) { + return; + } + // Load Brush: one solid red, no image. + h.state_mut().run("brush.mixer.load", json!({})).unwrap(); + h.run_steps(3); + let (fills, textures) = swatch(&h); + assert!(fills.contains(&egui::Color32::from_rgb(255, 0, 0)) && textures.is_empty(), "{fills:?} {textures:?}"); + + // ⌥-click on the green/blue edge: the sampled tip image, green left and blue right. + tool_event(h.state_mut(), ToolEvent::Down { x: 100.0, y: 50.0, pressure: 1.0 }, Modifiers::ALT); + tool_event(h.state_mut(), ToolEvent::Up { x: 100.0, y: 50.0 }, Modifiers::ALT); + h.run_steps(3); + let load = h.state().session.tools.mixer.spatial.clone().expect("the ⌥-click loads the tip image"); + let (_, textures) = swatch(&h); + assert_eq!(textures.len(), 1, "the swatch draws the tip image"); + let image = mixer_load_image(&load); + let (left, right) = (image[(16, 32)], image[(48, 32)]); + assert!(left.g() > 200 && left.b() < 50, "left of the image {left:?}"); + assert!(right.b() > 200 && right.g() < 50, "right of the image {right:?}"); + + // Clean Brush: a checkerboard of two greys, no image. + h.state_mut().run("brush.mixer.clean", json!({})).unwrap(); + h.run_steps(3); + let (fills, textures) = swatch(&h); + let greys: std::collections::BTreeSet<_> = fills.iter().filter(|c| c.r() == c.g() && c.g() == c.b() && c.r() > 150).map(|c| c.r()).collect(); + assert!(greys.len() >= 2 && textures.is_empty(), "checkerboard {fills:?} {textures:?}"); +} diff --git a/crates/ui-egui/src/panels/brush_controls.rs b/crates/ui-egui/src/panels/brush_controls.rs index ddd45f38ba..9c58d0927a 100644 --- a/crates/ui-egui/src/panels/brush_controls.rs +++ b/crates/ui-egui/src/panels/brush_controls.rs @@ -64,8 +64,36 @@ pub(super) fn smoothing_options(ui: &mut egui::Ui, b: &mut photocraft_engine::Br }); } -/// Mixer Brush: the Current Brush Load swatch shows the paint the next stroke starts with (an -/// empty swatch when the brush is clean). Its menu holds Load Brush and Clean Brush. +/// The sampled tip image of a Mixer Brush load (64 × 64 straight RGBA cells, row by row) as the +/// Current Brush Load swatch draws it. +pub(crate) fn mixer_load_image(load: &[[f32; 4]]) -> egui::ColorImage { + let bytes: Vec = load.iter().flat_map(|c| c.map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)).collect(); + egui::ColorImage::from_rgba_unmultiplied([64, 64], &bytes) +} + +/// The swatch's texture of `load`, uploaded again only when the load changes. +fn mixer_load_texture(ctx: &egui::Context, load: &[[f32; 4]]) -> egui::TextureHandle { + use std::hash::{Hash, Hasher}; + let image = mixer_load_image(load); + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + image.as_raw().hash(&mut hasher); + let hash = hasher.finish(); + let key = egui::Id::unique("mixer-load-swatch-image"); + if let Some((_, tex)) = ctx.data(|d| d.get_temp::<(u64, egui::TextureHandle)>(key)).filter(|(h, _)| *h == hash) { + return tex; + } + let tex = ctx.load_texture("mixer-load-swatch", image, egui::TextureOptions::LINEAR); + ctx.data_mut(|d| d.insert_temp(key, (hash, tex.clone()))); + tex +} + +#[cfg(test)] +#[path = "../mixer_load_swatch_tests.rs"] +mod mixer_load_swatch_tests; + +/// Mixer Brush: the Current Brush Load swatch shows the paint the next stroke starts with: the +/// loaded colour, the sampled tip image after an ⌥-click, or a transparency checkerboard when the +/// brush is clean. Its menu holds Load Brush and Clean Brush. pub(super) fn mixer_load_swatch(app: &mut PhotocraftApp, ui: &mut egui::Ui) { let t = Tokens::get(ui.ctx()); let (r, _) = ui.allocate_exact_size(vec2(40.0, 26.0), Sense::hover()); @@ -75,14 +103,22 @@ pub(super) fn mixer_load_swatch(app: &mut PhotocraftApp, ui: &mut egui::Ui) { } let chip = Rect::from_min_size(pos2(r.left() + 4.0, r.center().y - 8.0), vec2(16.0, 16.0)); let p = ui.painter(); - match app.session.tools.mixer.next_load(app.session.tools.foreground) { - Some(c) => { - p.rect_filled(chip, 2.0, c32(c)); - } - None => { - p.rect_filled(chip, 2.0, t.field); - p.line_segment([chip.left_bottom(), chip.right_top()], Stroke::new(1.0, t.text_faint)); - } + // What the reservoir holds (#11953): the loaded colour, the sampled tip image of an ⌥-click + // (kept while the foreground stays), or a transparency checkerboard when the brush is clean. + let mixer = &app.session.tools.mixer; + let foreground = app.session.tools.foreground; + match mixer.next_load(foreground) { + Some(c) => match mixer.spatial.as_deref().filter(|load| load.len() == 64 * 64 && mixer.color == Some(foreground)) { + Some(load) => { + widgets::checker(p, chip, 4.0); + let tex = mixer_load_texture(ui.ctx(), load); + p.image(tex.id(), chip, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE); + } + None => { + p.rect_filled(chip, 2.0, c32(c)); + } + }, + None => widgets::checker(p, chip, 4.0), } p.rect_stroke(chip, 2.0, Stroke::new(1.0, t.field_border), StrokeKind::Outside); icons::paint(ui, Rect::from_center_size(pos2(r.right() - 9.0, r.center().y), vec2(10.0, 10.0)), "chevron-down", 9.0, t.text_faint); From d5246412294ab087c81c3107b07725f9e54a18c7 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 8 Nov 2026 12:37:48 -0600 Subject: [PATCH 2/4] Mixer Brush load swatch tests: the sampled image keeps its top and bottom rows, its alpha and clamps out-of-range values (eliets2 review 5818990460 on mixer_load_swatch_tests.rs) --- crates/ui-egui/src/mixer_load_swatch_tests.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/mixer_load_swatch_tests.rs b/crates/ui-egui/src/mixer_load_swatch_tests.rs index 4e15f6d916..6b58687b4f 100644 --- a/crates/ui-egui/src/mixer_load_swatch_tests.rs +++ b/crates/ui-egui/src/mixer_load_swatch_tests.rs @@ -80,12 +80,18 @@ fn has_fonts(h: &Harness<'static, PhotocraftApp>) -> bool { } #[test] -fn the_sampled_image_keeps_its_left_and_right() { +fn the_sampled_image_keeps_its_left_right_top_and_bottom() { let load: Vec<[f32; 4]> = (0..64 * 64).map(|i| if i % 64 < 32 { GREEN } else { BLUE }).collect(); let image = mixer_load_image(&load); assert_eq!(image.size, [64, 64]); assert_eq!(image[(10, 32)], egui::Color32::from_rgb(0, 255, 0)); assert_eq!(image[(54, 32)], egui::Color32::from_rgb(0, 0, 255)); + // Row 0 is the tip's top, as `load_at` reads it; alpha is kept and values past 0..1 clamp. + let load: Vec<[f32; 4]> = (0..64 * 64).map(|i| if i < 32 * 64 { [1.0, 0.0, 0.0, 0.5] } else { [1.5, -0.2, 0.0, 1.0] }).collect(); + let image = mixer_load_image(&load); + let (top, bottom) = (image[(32, 10)], image[(32, 54)]); + assert_eq!((top.r() > 0, top.g(), top.b(), top.a()), (true, 0, 0, 128), "top of the image {top:?}"); + assert_eq!(bottom, egui::Color32::from_rgb(255, 0, 0), "bottom of the image"); } #[test] From 88b467095902d78e3c3b87e00ecc871317b571e9 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 8 Nov 2026 12:37:49 -0600 Subject: [PATCH 3/4] Mixer Brush load swatch tests: the swatch draws the sampled load's own texture, keeps it while the load stays and uploads a new one when it changes (eliets2 review 5818990460 on brush_controls.rs mixer_load_texture) --- crates/ui-egui/src/mixer_load_swatch_tests.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/mixer_load_swatch_tests.rs b/crates/ui-egui/src/mixer_load_swatch_tests.rs index 6b58687b4f..f23d9bf7bc 100644 --- a/crates/ui-egui/src/mixer_load_swatch_tests.rs +++ b/crates/ui-egui/src/mixer_load_swatch_tests.rs @@ -7,7 +7,7 @@ use egui_kittest::Harness; use egui_kittest::kittest::Queryable; use serde_json::json; -use super::mixer_load_image; +use super::{mixer_load_image, mixer_load_texture}; use crate::PhotocraftApp; use crate::canvas::{ToolEvent, tool_event}; use crate::state::Tool; @@ -112,11 +112,22 @@ fn the_swatch_shows_the_load_the_tip_image_or_a_checkerboard() { h.run_steps(3); let load = h.state().session.tools.mixer.spatial.clone().expect("the ⌥-click loads the tip image"); let (_, textures) = swatch(&h); - assert_eq!(textures.len(), 1, "the swatch draws the tip image"); + let sampled = mixer_load_texture(&h.ctx, &load).id(); + assert_eq!(textures, [sampled], "the swatch draws the texture of the sampled load"); let image = mixer_load_image(&load); let (left, right) = (image[(16, 32)], image[(48, 32)]); assert!(left.g() > 200 && left.b() < 50, "left of the image {left:?}"); assert!(right.b() > 200 && right.g() < 50, "right of the image {right:?}"); + h.run_steps(3); + assert_eq!(swatch(&h).1, [sampled], "an unchanged load keeps its texture"); + + // ⌥-click in the green: a new load, uploaded again. + tool_event(h.state_mut(), ToolEvent::Down { x: 30.0, y: 50.0, pressure: 1.0 }, Modifiers::ALT); + tool_event(h.state_mut(), ToolEvent::Up { x: 30.0, y: 50.0 }, Modifiers::ALT); + h.run_steps(3); + let green = h.state().session.tools.mixer.spatial.clone().expect("the second ⌥-click loads the tip image"); + let (_, textures) = swatch(&h); + assert!(textures != [sampled] && textures == [mixer_load_texture(&h.ctx, &green).id()], "the new load's texture {textures:?}"); // Clean Brush: a checkerboard of two greys, no image. h.state_mut().run("brush.mixer.clean", json!({})).unwrap(); From 06498888f8cbcae85d06fbbfd5cac8f8d8c1f3a6 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 8 Nov 2026 12:37:49 -0600 Subject: [PATCH 4/4] Mixer Brush load swatch tests: a new foreground after an Alt+click shows its colour, not the sampled image (eliets2 review 5818990460 on mixer_load_swatch_tests.rs) --- crates/ui-egui/src/mixer_load_swatch_tests.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/ui-egui/src/mixer_load_swatch_tests.rs b/crates/ui-egui/src/mixer_load_swatch_tests.rs index f23d9bf7bc..7885e52cbc 100644 --- a/crates/ui-egui/src/mixer_load_swatch_tests.rs +++ b/crates/ui-egui/src/mixer_load_swatch_tests.rs @@ -129,6 +129,12 @@ fn the_swatch_shows_the_load_the_tip_image_or_a_checkerboard() { let (_, textures) = swatch(&h); assert!(textures != [sampled] && textures == [mixer_load_texture(&h.ctx, &green).id()], "the new load's texture {textures:?}"); + // A new foreground: the next stroke loads it, so the swatch shows that colour, not the image. + h.state_mut().run("tools.setColors", json!({"foreground": "#ffff00"})).unwrap(); + h.run_steps(3); + let (fills, textures) = swatch(&h); + assert!(fills.contains(&egui::Color32::from_rgb(255, 255, 0)) && textures.is_empty(), "the new foreground {fills:?} {textures:?}"); + // Clean Brush: a checkerboard of two greys, no image. h.state_mut().run("brush.mixer.clean", json!({})).unwrap(); h.run_steps(3);