Merge pull request #13910 from ljkx/feat/13364-triangle-alt-eyedropper

Triangle tool: Alt at the press picks the foreground and Fill colour (#13364)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-13 22:49:45 +01:00
commit dc0f56b22e
2 files changed
+222 -6

No files matched your search

+47 -6
View File
@@ -287,20 +287,23 @@ pub(crate) fn paint_bucket_cursor(app: &mut PhotocraftApp, painter: &egui::Paint
/// Decided when the press starts, so ⌥ pressed or released mid-stroke never switches between
/// painting and sampling.
pub(super) fn alt_eyedropper(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool {
if matches!(ev, ToolEvent::Down { .. }) {
if let ToolEvent::Down { x, y, .. } = ev {
let physical_right = app.services.right_button_down.as_ref().is_some_and(|f| f());
app.alt_sampling = !physical_right && !app.secondary_erase && alt_samples(app.active_tool(), mods);
app.alt_sampling = !physical_right && !app.secondary_erase && (alt_samples(app.active_tool(), mods) || shape_alt_samples(app, mods, x, y));
}
if !app.alt_sampling {
return false;
}
// The Triangle tool's ⇧⌥ at the press samples as ⌥ does, and the release point's colour is the
// one it keeps (#13364).
let triangle = app.active_tool() == Tool::Triangle;
match ev {
// ⇧⌥-click adds a colour sampler at the clicked pixel instead, as the Color Sampler tool
// places one: no foreground change, nothing painted (#11890). A refused point (the
// document's limit) reports in the status bar as that tool's does; off the canvas, as that
// tool, it places none and says nothing. A press on an existing point wasn't measured, so
// it samples the foreground as before.
ToolEvent::Down { x, y, .. } if mods.shift && !on_sampler(app, x, y) => {
ToolEvent::Down { x, y, .. } if mods.shift && !triangle && !on_sampler(app, x, y) => {
let size = app.session.active().map(|d| d.doc.size);
let inside = size.is_some_and(|s| (0.0..f64::from(s.width)).contains(&x) && (0.0..f64::from(s.height)).contains(&y));
if inside && let Err(e) = app.run("view.colorSamplers.add", json!({"x": x, "y": y})) {
@@ -312,14 +315,42 @@ pub(super) fn alt_eyedropper(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui:
}
}
// While ⇧⌥ stay held after that press, moves sample nothing.
ToolEvent::Move { .. } if mods.shift => {}
ToolEvent::Move { .. } if mods.shift && !triangle => {}
ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } if triangle => sample_shape_fill(app, x, y),
// Without ⌥: the sample sets the foreground colour, whatever the Eyedropper would do.
ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } => sample_eyedropper(app, x, y, egui::Modifiers::NONE),
ToolEvent::Up { .. } => app.alt_sampling = false,
ToolEvent::Up { x, y } => {
if triangle {
sample_shape_fill(app, x, y);
}
app.alt_sampling = false;
}
}
true
}
/// The Triangle tool samples with ⌥ held at the press at (x, y) only in Shape mode and with no
/// shape layer active (#13364): with one, ⌥ subtracts from it. Path or Pixels mode and a press
/// with nothing to sample (off the image, over transparency) weren't measured and keep drawing
/// from the centre. Control+⌥ stays the brush-resize drag, as in [`alt_samples`].
fn shape_alt_samples(app: &mut PhotocraftApp, mods: egui::Modifiers, x: f64, y: f64) -> bool {
mods.alt
&& !mods.ctrl
&& app.active_tool() == Tool::Triangle
&& app.ui.tool_options.shape_mode == "shape"
&& crate::vector_ui::active_shape_path(app).is_none()
&& eyedropper_color(app, x, y).is_some()
}
/// The Triangle tool's ⌥ sample: the colour at (x, y) becomes the foreground and the tool's Fill, a
/// solid fill, which follows the foreground (#13364). Where nothing is sampled, both stay.
fn sample_shape_fill(app: &mut PhotocraftApp, x: f64, y: f64) {
if sampled_eyedropper(app, x, y, egui::Modifiers::NONE) {
app.ui.tool_options.shape_fill = true;
app.ui.tool_options.shape_fill_gradient = None;
}
}
/// Is document point (x, y) on a colour sampler, within the Color Sampler tool's reach of one (7
/// screen pixels)?
fn on_sampler(app: &PhotocraftApp, x: f64, y: f64) -> bool {
@@ -327,6 +358,10 @@ fn on_sampler(app: &PhotocraftApp, x: f64, y: f64) -> bool {
app.session.active().is_some_and(|d| d.color_samplers.iter().any(|q| (q[0] - x).hypot(q[1] - y) <= tol))
}
#[cfg(test)]
#[path = "triangle_alt_eyedropper_tests.rs"]
mod triangle_alt_eyedropper_tests;
#[cfg(test)]
#[path = "../brush_shift_alt_sampler_tests.rs"]
mod brush_shift_alt_sampler_tests;
@@ -334,6 +369,11 @@ mod brush_shift_alt_sampler_tests;
/// Sample the image or targeted raster mask at (x, y) using the configured Sample Size.
/// Write a valid sample to the foreground swatch, or to the background swatch with Alt.
pub(super) fn sample_eyedropper(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers) {
sampled_eyedropper(app, x, y, mods);
}
/// [`sample_eyedropper`], true when it set a swatch.
fn sampled_eyedropper(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers) -> bool {
let rgb = if let Some((surface, canvas)) = eyedropper_mask_source(app) {
sample_layer_mask(surface, canvas, x, y, app.ui.tool_options.eyedropper_size)
} else {
@@ -341,8 +381,9 @@ pub(super) fn sample_eyedropper(app: &mut PhotocraftApp, x: f64, y: f64, mods: e
};
if let Some(rgb) = rgb {
let key = if mods.alt { "background" } else { "foreground" };
let _ = app.run("tools.setColors", json!({ key: [rgb[0],rgb[1],rgb[2],1.0] }));
return app.run("tools.setColors", json!({ key: [rgb[0],rgb[1],rgb[2],1.0] })).is_ok();
}
false
}
/// Only the Eyedropper's Current Layer and Current & Below modes pick the targeted raster mask.
@@ -0,0 +1,175 @@
//! #13364: with the Triangle tool, ⌥ (Alt) held at the press with no shape layer active picks the
//! colour under the pointer as the foreground and the tool's Fill instead of drawing or opening
//! Create Triangle; ⇧⌥ does the same. ⌥ pressed only after the drag started still draws from the
//! centre, and with a shape layer active ⌥ at the press does not sample.
use egui::Modifiers;
use photocraft_doc::{Color, Fill, LayerContent};
use serde_json::json;
use super::shape_alt_samples;
use crate::PhotocraftApp;
use crate::canvas::{ToolEvent, tool_event};
use crate::state::Tool;
const RED: [u8; 3] = [220, 30, 30];
const BLACK: [u8; 3] = [0, 0, 0];
const WHITE: [u8; 3] = [255, 255, 255];
/// A point on the red layer, and one on the white Background only.
const ON_RED: [f64; 2] = [200.0, 175.0];
const ON_WHITE: [f64; 2] = [500.0, 350.0];
/// The issue's test document: 800 × 600, white Background, a red (220,30,30) pixel layer at
/// 100,100 to 300,250 (active), black foreground, the Triangle tool in Shape mode.
fn app() -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap();
app.run("layer.new.layer", json!({})).unwrap();
app.run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 150})).unwrap();
app.run("edit.fill", json!({"color": "#dc1e1e"})).unwrap();
app.run("select.deselect", json!({})).unwrap();
app.run("tools.setColors", json!({"foreground": "#000000"})).unwrap();
app.sync_views();
app.ui.tool = Tool::Triangle;
app
}
/// What a press could change: history states, layers, open dialogs and colour samplers.
fn document_state(app: &PhotocraftApp) -> (usize, usize, usize, usize) {
let st = app.session.active().unwrap();
(st.history.past_len(), st.doc.layers.len(), app.ui.dialogs.len(), st.color_samplers.len())
}
/// The foreground colour in 8-bit RGB.
fn foreground(app: &PhotocraftApp) -> [u8; 3] {
let c = app.session.tools.foreground;
[0, 1, 2].map(|i| (c[i] * 255.0).round() as u8)
}
/// The tool's Fill colour: a solid Fill follows the foreground (`vector_ui::shapes::tool_paint`).
fn tool_fill(app: &PhotocraftApp) -> [u8; 3] {
let o = &app.ui.tool_options;
assert!(o.shape_fill && o.shape_fill_gradient.is_none(), "a solid Fill");
foreground(app)
}
/// Press at `from` with `press` held, move to `to` with `rest`, release there with `rest`.
fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], press: Modifiers, rest: Modifiers) {
tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, press);
tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, rest);
tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, rest);
}
/// The active layer's Fill colour in 8-bit RGB, which must be a shape layer with a solid Fill.
fn active_shape_fill(app: &PhotocraftApp) -> [u8; 3] {
let st = app.session.active().unwrap();
let layer = st.doc.layer(st.active_layer.unwrap()).unwrap();
let LayerContent::Shape(shape) = &layer.content else { panic!("a shape layer, not {:?}", layer.name) };
let Some(Fill::Solid(c)) = &shape.fill else { panic!("a solid Fill, not {:?}", shape.fill) };
let [r, g, b, _] = c.to_rgba8();
[r, g, b]
}
#[test]
fn alt_click_on_the_red_layer_picks_it_as_foreground_and_fill() {
for press in [Modifiers::ALT, Modifiers::SHIFT | Modifiers::ALT] {
let mut app = app();
let before = document_state(&app);
drag(&mut app, ON_RED, ON_RED, press, press);
assert_eq!(foreground(&app), RED, "{press:?}: foreground #DC1E1E");
assert_eq!(tool_fill(&app), RED, "{press:?}: Fill 220,30,30");
assert_eq!(document_state(&app), before, "{press:?}: no History state, layer, dialog or colour sampler");
assert!(!app.alt_sampling, "{press:?}: the release ends the sampling");
// The next triangle is drawn in that Fill.
drag(&mut app, [400.0, 300.0], [600.0, 400.0], Modifiers::NONE, Modifiers::NONE);
assert_eq!(active_shape_fill(&app), RED, "{press:?}: the next triangle's Fill");
}
}
#[test]
fn an_alt_drag_keeps_the_colour_under_the_release_point() {
for press in [Modifiers::ALT, Modifiers::SHIFT | Modifiers::ALT] {
let mut app = app();
let before = document_state(&app);
// The last move is still on red; the release is on white.
tool_event(&mut app, ToolEvent::Down { x: ON_RED[0], y: ON_RED[1], pressure: 1.0 }, press);
tool_event(&mut app, ToolEvent::Move { x: 250.0, y: 200.0, pressure: 1.0 }, press);
assert_eq!(foreground(&app), RED, "{press:?}: the drag samples as it goes");
tool_event(&mut app, ToolEvent::Up { x: ON_WHITE[0], y: ON_WHITE[1] }, press);
assert_eq!(foreground(&app), WHITE, "{press:?}: foreground");
assert_eq!(tool_fill(&app), WHITE, "{press:?}: Fill");
assert_eq!(document_state(&app), before, "{press:?}: no shape and no History state");
}
}
#[test]
fn a_gradient_or_no_fill_becomes_the_sampled_solid_fill() {
let gradient = Fill::Gradient {
stops: vec![(0.0, Color::BLACK), (1.0, Color::WHITE)],
angle: 90.0,
scale: 1.0,
style: Default::default(),
reverse: false,
opacity_stops: Vec::new(),
midpoints: Vec::new(),
offset: (0.0, 0.0),
dither: false,
align: true,
};
for (fill, fill_gradient) in [(true, Some(gradient)), (false, None)] {
let mut app = app();
app.ui.tool_options.shape_fill = fill;
app.ui.tool_options.shape_fill_gradient = fill_gradient;
drag(&mut app, ON_RED, ON_RED, Modifiers::ALT, Modifiers::ALT);
assert_eq!(tool_fill(&app), RED, "fill {fill}");
}
}
#[test]
fn alt_pressed_after_the_drag_started_still_draws_from_the_centre() {
let mut app = app();
let layers = document_state(&app).1;
drag(&mut app, [400.0, 300.0], [500.0, 350.0], Modifiers::NONE, Modifiers::ALT);
assert_eq!(foreground(&app), BLACK, "nothing sampled");
assert_eq!(document_state(&app).1, layers + 1, "a triangle was drawn");
let st = app.session.active().unwrap();
let layer = st.doc.layer(st.active_layer.unwrap()).unwrap();
let LayerContent::Shape(shape) = &layer.content else { panic!("a shape layer") };
let anchors: Vec<[f64; 2]> = shape.path.subpaths[0].knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect();
assert_eq!(anchors, [[400.0, 250.0], [500.0, 350.0], [300.0, 350.0]], "centred on the press");
}
#[test]
fn a_shape_layer_active_does_not_sample() {
let mut app = app();
drag(&mut app, [400.0, 300.0], [600.0, 400.0], Modifiers::NONE, Modifiers::NONE);
app.run("tools.setColors", json!({"foreground": "#000000"})).unwrap();
assert!(!shape_alt_samples(&mut app, Modifiers::ALT, ON_RED[0], ON_RED[1]), "⌥ is left to the subtract");
tool_event(&mut app, ToolEvent::Down { x: ON_RED[0], y: ON_RED[1], pressure: 1.0 }, Modifiers::ALT);
assert!(!app.alt_sampling);
tool_event(&mut app, ToolEvent::Up { x: ON_RED[0], y: ON_RED[1] }, Modifiers::ALT);
assert_eq!(foreground(&app), BLACK);
}
#[test]
fn unmeasured_cases_keep_drawing() {
let mut app = app();
assert!(shape_alt_samples(&mut app, Modifiers::ALT, ON_RED[0], ON_RED[1]));
assert!(shape_alt_samples(&mut app, Modifiers::SHIFT | Modifiers::ALT, ON_RED[0], ON_RED[1]));
assert!(!shape_alt_samples(&mut app, Modifiers::NONE, ON_RED[0], ON_RED[1]));
assert!(!shape_alt_samples(&mut app, Modifiers::ALT | Modifiers::CTRL, ON_RED[0], ON_RED[1]), "Control+⌥ is the brush resize");
assert!(!shape_alt_samples(&mut app, Modifiers::ALT, -40.0, -40.0), "nothing to sample off the image");
for mode in ["path", "pixels"] {
app.ui.tool_options.shape_mode = mode.into();
assert!(!shape_alt_samples(&mut app, Modifiers::ALT, ON_RED[0], ON_RED[1]), "{mode} mode wasn't measured");
}
app.ui.tool_options.shape_mode = "shape".into();
app.ui.tool = Tool::Rectangle;
assert!(!shape_alt_samples(&mut app, Modifiers::ALT, ON_RED[0], ON_RED[1]), "the other shape tools are their own items");
// Off the image the press draws from the centre, as before.
app.ui.tool = Tool::Triangle;
let layers = document_state(&app).1;
drag(&mut app, [-40.0, -40.0], [60.0, 60.0], Modifiers::ALT, Modifiers::ALT);
assert_eq!(foreground(&app), BLACK, "nothing sampled");
assert_eq!(document_state(&app).1, layers + 1, "a triangle was drawn");
}