Merge pull request #13153 from rmsff/fix/clone-stamp-no-source-alert-12609

Clone Stamp: a press without a source point shows the reference's alert
This commit is contained in:
David Mann authored and GitHub committed 2026-11-11 19:18:28 +01:00
commit f69cb2056e
6 files changed
+142 -19

No files matched your search

@@ -9,7 +9,7 @@ use super::*;
use crate::canvas::{ToolEvent, tool_event};
/// A 64 × 64 document with a painted layer active, black foreground, `tool`.
pub(super) fn app(tool: Tool) -> PhotocraftApp {
pub(in crate::tools) fn app(tool: Tool) -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
app.run("layer.new.layer", json!({})).unwrap();
@@ -24,22 +24,22 @@ pub(super) fn active(app: &PhotocraftApp) -> u64 {
app.session.active().unwrap().active_layer.unwrap().0
}
pub(super) fn steps(app: &PhotocraftApp) -> usize {
pub(in crate::tools) fn steps(app: &PhotocraftApp) -> usize {
app.session.active().unwrap().history.past_len()
}
/// A press, a short drag and the release with the active tool.
pub(super) fn stroke(app: &mut PhotocraftApp) {
pub(in crate::tools) fn stroke(app: &mut PhotocraftApp) {
tool_event(app, ToolEvent::Down { x: 20.0, y: 32.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(app, ToolEvent::Move { x: 44.0, y: 32.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(app, ToolEvent::Up { x: 44.0, y: 32.0 }, Modifiers::NONE);
}
pub(super) fn alerts(app: &PhotocraftApp) -> Vec<String> {
pub(in crate::tools) fn alerts(app: &PhotocraftApp) -> Vec<String> {
app.ui.dialogs.iter().filter(|d| d.kind == DialogKind::Error).filter_map(|d| d.fields.get("message").and_then(Value::as_str).map(str::to_string)).collect()
}
pub(super) fn refused(app: &mut PhotocraftApp, want: &str) {
pub(in crate::tools) fn refused(app: &mut PhotocraftApp, want: &str) {
let before = steps(app);
stroke(app);
assert_eq!(alerts(app), [want], "the alert, once");
+6 -1
View File
@@ -544,7 +544,12 @@ pub(super) fn tool_event_with_view(app: &mut PhotocraftApp, ev: ToolEvent, mods:
if !sets_source && press_refused(app, tool) {
return;
}
if !sets_source && (crate::rasterize_prompt::intercept(app, tool, x, y, pressure) || refuse_group(app, tool)) {
// The Clone Stamp's no-source alert opens at the press, after any rasterize prompt.
if !sets_source
&& (crate::rasterize_prompt::intercept(app, tool, x, y, pressure)
|| refuse_group(app, tool)
|| crate::retouch_ui::clone_stamp_without_source(app, tool))
{
return;
}
// ⌘ with a selection tool is the Move tool for the drag: outside the selection (or
@@ -0,0 +1,95 @@
//! #12609: a Clone Stamp press before any Alt-click opens the reference application's alert with
//! OK at the press, instead of a status-bar line, and paints nothing. The Healing Brush keeps its
//! own behaviour.
use egui::Modifiers;
use serde_json::json;
use crate::PhotocraftApp;
use crate::canvas::{ToolEvent, tool_event};
use crate::control::{ControlRequest, Outcome, handle};
use crate::state::{DialogKind, Tool};
use crate::tools::brush_refusal::brush_refusal_alert_tests::{alerts, app, refused, steps, stroke};
const MESSAGE: &str = if cfg!(target_os = "macos") {
"Could not use the clone stamp tool because the area to clone has not been defined (Option-click to define a source point)."
} else {
"Could not use the clone stamp tool because the area to clone has not been defined (Alt-click to define a source point)."
};
/// The active layer's pixels.
fn pixels(app: &PhotocraftApp) -> Vec<f32> {
let st = app.session.active().unwrap();
st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().read_region(st.doc.bounds())
}
#[test]
fn a_stroke_without_a_source_alerts_once_and_paints_nothing() {
let mut app = app(Tool::CloneStamp);
let before = pixels(&app);
refused(&mut app, MESSAGE);
assert!(!app.ui.status_error, "no status-bar error as well: {}", app.ui.status);
assert!(pixels(&app) == before, "after OK the document is unchanged");
}
#[test]
fn the_alert_opens_at_the_press_and_the_drag_and_release_add_none() {
let mut app = app(Tool::CloneStamp);
tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 32.0, pressure: 1.0 }, Modifiers::NONE);
assert_eq!(alerts(&app), [MESSAGE], "at the press, before any move");
assert!(app.drag.is_none() && app.live_stroke.is_none(), "the press starts no stroke");
// Closed before the drag, so an alert from the move or the release would show.
let id = app.ui.dialogs.iter().find(|d| d.kind == DialogKind::Error).map(|d| d.id).unwrap();
crate::dialogs::confirm(&mut app, id).unwrap();
tool_event(&mut app, ToolEvent::Move { x: 44.0, y: 32.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(&mut app, ToolEvent::Up { x: 44.0, y: 32.0 }, Modifiers::NONE);
assert!(alerts(&app).is_empty(), "neither the drag nor the release alerts");
}
#[test]
fn a_second_press_while_the_alert_is_up_adds_none() {
let mut app = app(Tool::CloneStamp);
stroke(&mut app);
stroke(&mut app);
assert_eq!(alerts(&app), [MESSAGE]);
}
#[test]
fn the_alert_is_listed_and_confirmed_over_the_control_channel() {
let ctx = egui::Context::default();
let mut app = app(Tool::CloneStamp);
stroke(&mut app);
let (inspect, _rx) = ControlRequest::new("ui.inspect", json!({}));
let Outcome::Done(inspect) = handle(&mut app, &ctx, &inspect) else { panic!("ui.inspect replies at once") };
let dialog = &inspect["result"]["dialogs"][0];
assert_eq!(dialog["fields"]["message"], MESSAGE, "{inspect}");
let (confirm, _rx) = ControlRequest::new("ui.dialog.confirm", json!({"dialog": dialog["id"]}));
assert!(matches!(handle(&mut app, &ctx, &confirm), Outcome::Done(v) if v["ok"] == true));
assert!(alerts(&app).is_empty());
}
#[test]
fn after_an_alt_click_the_clone_stamp_paints_without_an_alert() {
let mut app = app(Tool::CloneStamp);
// A white band around the source on the red layer, so the clone has something to copy.
app.run("select.rect", json!({"x": 0, "y": 0, "width": 64, "height": 20})).unwrap();
app.run("edit.fill", json!({"color": "#ffffff"})).unwrap();
app.run("select.deselect", json!({})).unwrap();
tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, Modifiers::ALT);
tool_event(&mut app, ToolEvent::Up { x: 10.0, y: 10.0 }, Modifiers::ALT);
let before = steps(&app);
stroke(&mut app);
assert!(alerts(&app).is_empty());
assert_eq!(steps(&app), before + 1, "one Clone Stamp state");
let st = app.session.active().unwrap();
let on_stroke = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(32, 32);
assert!(on_stroke[1] > 0.5, "the stroke copies the white source onto the red: {on_stroke:?}");
}
#[test]
fn the_healing_brush_keeps_its_status_line() {
let mut app = app(Tool::Healing);
stroke(&mut app);
assert!(alerts(&app).is_empty());
assert!(app.ui.status_error && app.ui.status.contains("source point to clone from"), "{}", app.ui.status);
}
+1 -1
View File
@@ -90,7 +90,7 @@ pub fn prompt(app: &mut PhotocraftApp, message: &str) {
#[cfg(test)]
#[path = "../brush_refusal_alert_tests.rs"]
mod brush_refusal_alert_tests;
pub(super) mod brush_refusal_alert_tests;
#[cfg(test)]
#[path = "../sharpen_refusal_alert_tests.rs"]
+26 -3
View File
@@ -198,6 +198,21 @@ pub(crate) fn stroke_seed(points: &[[f64; 3]]) -> u64 {
photocraft_engine::paint::rng::seed_from_bytes(first.as_bytes())
}
/// A Clone Stamp press with no source point yet: the reference application's alert, opened at
/// the press (#12609) and once while it is up. Returns whether it refused the stroke.
pub(crate) fn clone_stamp_without_source(app: &mut PhotocraftApp, tool: Tool) -> bool {
if tool != Tool::CloneStamp || clone_params(app).is_some() {
return false;
}
let message = if cfg!(target_os = "macos") {
tl!("Could not use the clone stamp tool because the area to clone has not been defined (Option-click to define a source point).")
} else {
tl!("Could not use the clone stamp tool because the area to clone has not been defined (Alt-click to define a source point).")
};
open_alert_once(app, message);
true
}
/// Finish a stroke with a retouching tool. Returns false if `tool` isn't one.
pub fn finish_stroke(app: &mut PhotocraftApp, tool: Tool, points: &[[f64; 3]], mods: egui::Modifiers, line: bool) -> bool {
finish_stroke_with(app, tool, points, mods, line, None)
@@ -224,6 +239,9 @@ pub(crate) fn finish_stroke_with(app: &mut PhotocraftApp, tool: Tool, points: &[
return true;
}
let Some(p) = clone_params(app) else {
if clone_stamp_without_source(app, tool) {
return true;
}
// Option-click on the Mac, Alt-click on Windows.
app.ui.status = if cfg!(target_os = "macos") {
tl!("Option-click to define a source point to clone from")
@@ -783,7 +801,8 @@ fn cam_amount_slider(ui: &mut egui::Ui, name: &str, value: &mut f32, lo: f32, hi
});
}
fn open_cam_range_alert(app: &mut PhotocraftApp, message: &str) {
/// Opens the alert with `message` (English, translated here) unless it is already up.
fn open_alert_once(app: &mut PhotocraftApp, message: &str) {
let message = tl!(message).to_owned();
let shown =
app.ui.dialogs.iter().any(|d| d.kind == crate::state::DialogKind::Error && d.fields.get("message").and_then(|m| m.as_str()) == Some(message.as_str()));
@@ -807,11 +826,11 @@ fn content_aware_move_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
opt(ui, tl!("Structure:"));
if cam_integer_popup(ui, "cam-structure", "Structure", &mut app.ui.tool_options.cam_structure, 1.0, 7.0, STRUCTURE_TIP) {
open_cam_range_alert(app, STRUCTURE_RANGE_ALERT);
open_alert_once(app, STRUCTURE_RANGE_ALERT);
}
opt(ui, tl!("Color:"));
if cam_integer_popup(ui, "cam-color", "Color", &mut app.ui.tool_options.cam_color, 0.0, 10.0, COLOR_TIP) {
open_cam_range_alert(app, COLOR_RANGE_ALERT);
open_alert_once(app, COLOR_RANGE_ALERT);
}
crate::widgets::checkbox(ui, &mut app.ui.tool_options.sample_all.content_aware_move, tl!("Sample All Layers"))
.on_hover_text(tl!("Enable to remix all layers"));
@@ -1207,3 +1226,7 @@ mod sponge_flow_field_tests;
#[cfg(test)]
#[path = "../healing_indexed_alert_tests.rs"]
mod healing_indexed_alert_tests;
#[cfg(test)]
#[path = "../clone_stamp_no_source_alert_tests.rs"]
mod clone_stamp_no_source_alert_tests;
+9 -9
View File
@@ -225,15 +225,15 @@ fn paint_bucket_mode_reaches_the_fill() {
#[test]
fn clone_without_a_source_names_the_platform_modifier() {
// Option-click on the Mac, Alt-click elsewhere (#251).
for tool in [Tool::CloneStamp, Tool::Healing] {
let mut app = app();
app.ui.tool = tool;
assert!(finish_stroke(&mut app, tool, &[[10.0, 30.0, 1.0], [50.0, 30.0, 1.0]], egui::Modifiers::NONE, false));
assert!(app.ui.status_error, "{tool:?}");
let want = if cfg!(target_os = "macos") { "Option-click" } else { "Alt-click" };
assert!(app.ui.status.starts_with(want), "{tool:?}: {}", app.ui.status);
}
// Option-click on the Mac, Alt-click elsewhere (#251). The Clone Stamp says it in an alert
// (#12609, clone_stamp_no_source_alert_tests.rs).
let tool = Tool::Healing;
let mut app = app();
app.ui.tool = tool;
assert!(finish_stroke(&mut app, tool, &[[10.0, 30.0, 1.0], [50.0, 30.0, 1.0]], egui::Modifiers::NONE, false));
assert!(app.ui.status_error);
let want = if cfg!(target_os = "macos") { "Option-click" } else { "Alt-click" };
assert!(app.ui.status.starts_with(want), "{}", app.ui.status);
}
#[test]