From 52b3381693c57eaa8bc9cf81eadd512afc167608 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sun, 22 Nov 2026 23:18:37 +0000 Subject: [PATCH] PaintBar: show the Mixer Brush load as a colour, image or checkerboard MixerValues::load_color becomes current_load: MixerLoad (Clean, Color, Image). The load swatch shows the sampled tip image after an Alt-click and a checkerboard for a clean brush, through Swatch's fills. MixerValues is no longer Copy. Restores storytold/photocraft#11953. --- crates/draw/src/ui/mod.rs | 3 +- crates/draw/src/ui/paint_bar.rs | 34 +++++++++++++----- crates/draw/src/ui/paint_bar/stories.rs | 20 +++++++++-- crates/draw/tests/it/paint_bar.rs | 46 ++++++++++++++++++++++--- 4 files changed, 86 insertions(+), 17 deletions(-) diff --git a/crates/draw/src/ui/mod.rs b/crates/draw/src/ui/mod.rs index cff0a6c14..735a55f75 100644 --- a/crates/draw/src/ui/mod.rs +++ b/crates/draw/src/ui/mod.rs @@ -86,7 +86,8 @@ pub use measure::{ MIN_LINE, RulerBar, RulerBarOutput, RulerLine, RulerOverlay, RulerPass, RulerTool, RulerWords, box_distances, ruler_line_form, show_ruler, }; pub use paint_bar::{ - EraserMode, MIXER_COMBINATIONS, MIXER_GROUP_STARTS, MixerValues, PaintBar, PaintBarOutput, PaintBarWords, PaintTool, PaintValues, Smoothing, + EraserMode, MIXER_COMBINATIONS, MIXER_GROUP_STARTS, MixerLoad, MixerValues, PaintBar, PaintBarOutput, PaintBarWords, PaintTool, PaintValues, + Smoothing, }; pub use paths::{PathKind, PathRow, PathsCommands, PathsEvent, PathsFooterButton, PathsOutput, PathsPanel, PathsWords}; pub use pattern::{PatternColors, PatternFillDialog, PatternFillEditor, PatternFillOptions, PatternFillOutput, PatternSwatch, PatternWords}; diff --git a/crates/draw/src/ui/paint_bar.rs b/crates/draw/src/ui/paint_bar.rs index c589e2e61..870b80016 100644 --- a/crates/draw/src/ui/paint_bar.rs +++ b/crates/draw/src/ui/paint_bar.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use craft_behavior::{EditEvent, EditEvents, IconId, Mixed, Value}; use craft_theme::Srgba; use craft_ui::buttons::{IconButton, ToggleButton}; -use craft_ui::choice::{Checkbox, Swatch}; +use craft_ui::choice::{Checkbox, Swatch, SwatchFill, SwatchImage}; use craft_ui::contract::{ Bind, BodyCx, Entry, Gap, LabelKind, PopupKind, Props as _, Remember as _, Show as _, ShowContainer as _, SideLabel, Trigger, clicked, }; @@ -111,8 +111,21 @@ impl Default for Smoothing { } } +/// What the Mixer Brush holds: the paint its next stroke starts with. +#[derive(Clone, Debug, Default, PartialEq, craft_ui::contract::MemoProps)] +#[non_exhaustive] +pub enum MixerLoad { + /// Nothing: the brush is clean. + #[default] + Clean, + /// One colour. + Color(Srgba), + /// The tip image an Alt-click sampled. + Image(SwatchImage), +} + /// The Mixer Brush's values. -#[derive(Clone, Copy, Debug, PartialEq, craft_ui::contract::MemoProps)] +#[derive(Clone, Debug, PartialEq, craft_ui::contract::MemoProps)] #[non_exhaustive] pub struct MixerValues { /// Load the brush after each stroke. @@ -131,8 +144,8 @@ pub struct MixerValues { pub sample_all_layers: bool, /// The index into [`MIXER_COMBINATIONS`]. pub combination: usize, - /// The colour the brush is loaded with, if any. - pub load_color: Option, + /// What the brush holds, which the load swatch shows. + pub current_load: MixerLoad, /// Only solid colours load. pub solid_colors_only: bool, } @@ -149,7 +162,7 @@ impl Default for MixerValues { flow: 1.0, sample_all_layers: false, combination: 0, - load_color: None, + current_load: MixerLoad::Clean, solid_colors_only: false, } } @@ -442,12 +455,15 @@ impl Pass<'_, M> { cx.memory::().0 = open; } - /// The Mixer Brush's load swatch and its menu. + /// The Mixer Brush's load swatch, showing what the brush holds (a checkerboard when it is clean), and its menu. fn mixer_load(&mut self, ui: &mut Ui, words: &PaintBarWords) { - let color = self.values.mixer.load_color; + let fill = match &self.values.mixer.current_load { + MixerLoad::Clean => SwatchFill::Empty, + MixerLoad::Color(color) => SwatchFill::Color(Mixed::Same(*color)), + MixerLoad::Image(image) => SwatchFill::Image(image.clone()), + }; let solid = self.values.mixer.solid_colors_only; - // `Swatch` has no empty face, so a brush with no load shows the mixed face. - let swatch = Swatch::new(words.load_swatch.clone(), color.map_or(Mixed::Mixed, Mixed::Same)).show(ui).response; + let swatch = Swatch::with_fill(words.load_swatch.clone(), fill).show(ui).response; let (mut load, mut clean, mut next_solid) = (false, false, None); let _ = Menu::new(words.load_swatch.clone()).opened_by(&swatch, Trigger::Click).show(ui, |ui, _| { load = MenuItem::new(words.load_brush.clone()).show(ui).ran; diff --git a/crates/draw/src/ui/paint_bar/stories.rs b/crates/draw/src/ui/paint_bar/stories.rs index c2e206b25..754849cee 100644 --- a/crates/draw/src/ui/paint_bar/stories.rs +++ b/crates/draw/src/ui/paint_bar/stories.rs @@ -1,14 +1,20 @@ //! Stories of the paint bar, one per tool and Eraser mode. -use super::{EraserMode, PaintBar, PaintTool, PaintValues}; +use super::{EraserMode, MixerLoad, PaintBar, PaintTool, PaintValues}; use craft_ui::bars::{Bar, BarKind}; +use craft_ui::choice::SwatchImage; use craft_ui::contract::{Entry, ShowContainer as _}; +use craft_ui::testing::pictures::sample_picture; use craft_ui::testing::{Drive, StoryData, StoryOutput, story}; use egui::Ui; -/// The bar of `tool` in a bar strip, over values the story keeps. +/// The bar of `tool` in a bar strip, over values the story keeps, starting from `start`. fn show(ui: &mut Ui, data: &mut StoryData, tool: PaintTool, eraser: EraserMode) -> StoryOutput { - let state = data.value("state", (PaintValues::new(0u32), eraser)); + show_from(ui, data, tool, eraser, PaintValues::new(0u32)) +} + +fn show_from(ui: &mut Ui, data: &mut StoryData, tool: PaintTool, eraser: EraserMode, start: PaintValues) -> StoryOutput { + let state = data.value("state", (start, eraser)); let (values, eraser_mode) = (&mut state.0, &mut state.1); let modes = [Entry::new(0u32, "Normal"), Entry::new(1u32, "Multiply"), Entry::new(2u32, "Screen")]; Bar::new("Options", BarKind::Options).show(ui, |ui, _| { @@ -45,3 +51,11 @@ fn eraser_block_mode(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { fn mixer_brush(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { show(ui, data, PaintTool::MixerBrush, EraserMode::Brush) } + +/// The Mixer Brush after an Alt-click: the load swatch shows the sampled tip image. +#[story(PaintBar<'static, u32>, covers = [], drive = Drive::Never)] +fn mixer_brush_sampled(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let mut start = PaintValues::new(0u32); + start.mixer.current_load = MixerLoad::Image(SwatchImage::new(sample_picture(11, [64, 64]))); + show_from(ui, data, PaintTool::MixerBrush, EraserMode::Brush, start) +} diff --git a/crates/draw/tests/it/paint_bar.rs b/crates/draw/tests/it/paint_bar.rs index 192179e4c..1ec55b56c 100644 --- a/crates/draw/tests/it/paint_bar.rs +++ b/crates/draw/tests/it/paint_bar.rs @@ -1,17 +1,19 @@ -//! `PaintBar`: which members each tool disables, the smoothing options, the gestures, the combinations list and a -//! click on the load swatch menu. +//! `PaintBar`: which members each tool disables, the smoothing options, the gestures, the combinations list, what the +//! load swatch shows and a click on its menu. use std::cell::RefCell; use std::rc::Rc; use craft_behavior::EditEvent; -use craft_draw::ui::{EraserMode, MIXER_COMBINATIONS, PaintBar, PaintTool, PaintValues}; +use craft_draw::ui::{EraserMode, MIXER_COMBINATIONS, MixerLoad, PaintBar, PaintTool, PaintValues}; +use craft_theme::Srgba; use craft_ui::bars::{Bar, BarKind}; +use craft_ui::choice::SwatchImage; use craft_ui::contract::{Entry, ShowContainer as _}; use craft_ui::testing::focus::AccessActionKind; use craft_ui::testing::pointer::{moved, press, release}; use craft_ui::testing::{Fixture, Room, trial}; -use egui::{Key, Modifiers, Pos2}; +use egui::{Color32, ColorImage, Key, Modifiers, Pos2, Shape, TextureId}; struct Host { values: PaintValues, @@ -150,3 +152,39 @@ fn a_click_on_the_load_swatch_menus_clean_brush_row_reports_it_once() { assert_eq!(host.borrow().cleans, 1, "one pass: {one_pass}"); } } + +/// Every shape of the frame, groups opened. +fn flat(shapes: Vec) -> Vec { + shapes.into_iter().flat_map(|shape| if let Shape::Vec(inner) = shape { flat(inner) } else { vec![shape] }).collect() +} + +/// What the load swatch shows: its accessible value and the pictures painted in it (the checkerboard, a sampled image). +fn load_swatch(fixture: &mut Fixture) -> (Option, usize) { + let swatch = fixture.by_label("Current brush load").expect("the bar shows the load swatch"); + let value = fixture.tree().value_of(swatch.node).map(str::to_owned); + let well = swatch.rect.expand(0.5); + let mut pictures: Vec = flat(fixture.frame_shapes()) + .iter() + .filter(|shape| well.contains_rect(shape.visual_bounding_rect())) + .map(Shape::texture_id) + .filter(|texture| *texture != TextureId::default()) + .collect(); + pictures.sort_by_key(|texture| format!("{texture:?}")); + pictures.dedup(); + (value, pictures.len()) +} + +/// The load swatch shows what the brush holds: a clean brush the checkerboard alone, a colour that colour, a sampled +/// tip image that picture over the checkerboard. +#[trial] +fn the_load_swatch_shows_each_mixer_load() { + let green = Srgba::rgb(0.0, 1.0, 0.0); + let tip = SwatchImage::new(ColorImage::filled([64, 64], Color32::from_rgb(0, 0, 255))); + let cases = + [(MixerLoad::Clean, Some("Empty".to_owned()), 1), (MixerLoad::Color(green), Some(green.to_hex()), 0), (MixerLoad::Image(tip), None, 2)]; + for (load, value, pictures) in cases { + let shown = load.clone(); + let (mut fixture, _) = harness(PaintTool::MixerBrush, EraserMode::Brush, move |v| v.mixer.current_load = shown); + assert_eq!(load_swatch(&mut fixture), (value, pictures), "{load:?}"); + } +}