Canvas and Decal say where to click after the image is chosen (#4359) (#4396)

* claim: #4359

* Canvas and Decal say where to click after the image is chosen (#4359)

After picking an image the placement tool started silently, so it looked
as if nothing had happened. Show the tool's placement hint as a toast while
it waits for the click (translated), and end it once the image is placed.

* Canvas: include placement hint in viewport obstacles (#4359)
This commit is contained in:
raykholo
2026-10-29 06:17:35 +00:00
committed by GitHub
parent d77b0434dd
commit c688054db0
5 changed files with 109 additions and 4 deletions
@@ -0,0 +1,26 @@
{
"en": {
"canvas.decal_place_hint": "Click a face to place the decal",
"canvas.place_hint": "Click a plane or face to place the canvas"
},
"ar": {
"canvas.decal_place_hint": "انقر على وجه لوضع الملصق",
"canvas.place_hint": "انقر على مستوى أو وجه لوضع الصورة المرجعية"
},
"he": {
"canvas.decal_place_hint": "יש ללחוץ על פאה כדי למקם את המדבקה",
"canvas.place_hint": "יש ללחוץ על מישור או פאה כדי למקם את תמונת הייחוס"
},
"ja": {
"canvas.decal_place_hint": "面をクリックしてデカールを配置",
"canvas.place_hint": "平面または面をクリックしてキャンバスを配置"
},
"vi": {
"canvas.decal_place_hint": "Nhấp vào một mặt để đặt hình dán",
"canvas.place_hint": "Nhấp vào mặt phẳng hoặc mặt để đặt ảnh tham chiếu"
},
"zh": {
"canvas.decal_place_hint": "单击一个面以放置贴花",
"canvas.place_hint": "单击平面或面以放置画布"
}
}
+64
View File
@@ -504,4 +504,68 @@ mod tests {
assert!(plane.normal().z.abs() > 0.99 && (plane.origin.z - 40.0).abs() < 1e-6, "{} lies on {plane:?}", canvas.name);
}
}
fn placement_hint_painted(h: &mut crate::scenario::Harness, hint: &str) -> bool {
fn contains(shape: &egui::Shape, hint: &str) -> bool {
match shape {
egui::Shape::Text(text) => text.galley.job.text == hint,
egui::Shape::Vec(shapes) => shapes.iter().any(|shape| contains(shape, hint)),
_ => false,
}
}
let out = h.ctx.run_ui(
egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1600.0, 1000.0))), ..Default::default() },
|ui| h.app.ui(ui),
);
out.shapes.iter().any(|shape| contains(&shape.shape, hint))
}
fn image_placement(command: &str) -> (crate::scenario::Harness, &'static str) {
let mut h = crate::scenario::Harness::new();
h.app.home.open = false;
if command == "canvas.decal" {
h.app.run("solid.box", json!({"length":10,"width":10,"height":10})).unwrap();
h.app.fit_view();
} else {
h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
}
let path = h.dir.join("image.png");
let png = solidcraft_engine::render::png(1, 1, vec![255, 0, 0, 255]).unwrap();
std::fs::write(&path, png).unwrap();
let answer = picker(&mut h.app, false);
h.app.start(command);
h.frames(2);
assert!(h.app.tool.is_none());
finish(&mut h.app, &answer, Some(path.to_string_lossy().into_owned()));
assert_eq!(h.app.tool.as_ref().map(|tool| tool.cmd), Some(command));
let hint = if command == "canvas.decal" { "Click a face to place the decal" } else { "Click a plane or face to place the canvas" };
assert!(placement_hint_painted(&mut h, hint), "{command}: placement instruction is visible");
// An expired ordinary status must not dismiss the active placement instruction.
h.app.status = Some(("Old status".into(), crate::now_ms() - 7000.0, false));
assert!(placement_hint_painted(&mut h, hint));
(h, hint)
}
#[test]
fn image_placement_hint_clears_after_placement() {
for command in ["canvas.insert", "canvas.decal"] {
let (mut h, hint) = image_placement(command);
let point = if command == "canvas.decal" { json!([5, 5, 10]) } else { json!([4, 3, 0]) };
h.step(&json!({"click":{"world":point}})).unwrap();
assert_eq!(h.app.session.doc.canvases.len(), 1, "{command}: {:?}", h.app.status);
assert!(h.app.tool.is_none());
assert!(!placement_hint_painted(&mut h, hint));
}
}
#[test]
fn image_placement_hint_clears_on_escape() {
for command in ["canvas.insert", "canvas.decal"] {
let (mut h, hint) = image_placement(command);
h.step(&json!({"key":"Escape"})).unwrap();
assert!(h.app.tool.is_none());
assert!(h.app.session.doc.canvases.is_empty());
assert!(!placement_hint_painted(&mut h, hint));
}
}
}
+7 -2
View File
@@ -637,6 +637,11 @@ pub fn tool_for(id: &str) -> Option<Tool> {
})
}
/// A successful image placement consumes the selected file and ends its tool.
pub fn image_placement_finished(tool: &Tool) -> bool {
matches!(tool.cmd, "canvas.insert" | "canvas.decal") && IMAGE.with(|image| image.borrow().is_none())
}
pub fn hint(id: &str) -> Option<String> {
Some(
match id {
@@ -650,8 +655,8 @@ pub fn hint(id: &str) -> Option<String> {
"inspect.center_of_mass" => "Center of mass of all bodies; click a body for its own",
"inspect.isocurve" => "Isocurves: click faces",
"inspect.continuity" => "Continuity: click an edge for its numbers (the button again turns the colours off)",
"canvas.insert" => "Canvas: click a plane or a planar face where the image's centre goes",
"canvas.decal" => "Decal: click a planar face where the image's centre goes",
"canvas.insert" => crate::i18n::tr("canvas.place_hint"),
"canvas.decal" => crate::i18n::tr("canvas.decal_place_hint"),
"sketch.trim" => "Trim: click the piece of a curve to remove, or drag across the pieces to remove",
"sketch.extend" => "Extend: click a curve near the end to extend (Shift: click two curves to extend both to where they meet)",
"sketch.break" => "Break: click a curve where it should split",
+1 -1
View File
@@ -450,7 +450,7 @@ pub fn on_click(app: &mut SolveApp, proj: &Proj, pos: Pos2, shift: bool) {
}
}
}
if !crate::sketch_text::editing(app) {
if !crate::sketch_text::editing(app) && !crate::sketch_tools::image_placement_finished(&tool) {
app.tool = Some(tool);
}
}
+11 -1
View File
@@ -4836,9 +4836,12 @@ const STATUS_TOAST_MS: f64 = 6000.0;
/// A single visual pattern for ongoing work, the existing six-second status, the 3D mouse
/// calibration offer and the stand-up note, stacked from the bottom of the free view. Returns the toasts' rects.
pub(crate) fn transient_messages(app: &mut SolveApp, ui: &egui::Ui, viewport: Rect) -> Vec<Rect> {
let placement_hint =
app.tool.as_ref().filter(|tool| matches!(tool.cmd, "canvas.insert" | "canvas.decal")).map(|tool| crate::tools::hint(tool.cmd));
// Nothing to show or time: an idle window skips the layout. The offer is checked too, as its clock
// runs from the frames `due` sees.
if app.status.is_none()
if placement_hint.is_none()
&& app.status.is_none()
&& app.computing_since().is_none()
&& app.agent_computing().is_none()
&& crate::space_mouse_offer::offerable(app).is_none()
@@ -4894,6 +4897,13 @@ pub(crate) fn transient_messages(app: &mut SolveApp, ui: &egui::Ui, viewport: Re
ui.ctx().request_repaint_after(std::time::Duration::from_millis(500));
}
}
if let Some(hint) = placement_hint
&& let Some(r) = paint_toast(ui, free, hint, false, false)
{
crate::scenario::publish_rect("toast:placement", r);
obstacles.push(r);
free.max.y = r.top() - t.space_8;
}
if let Some((msg, at, error)) = app.status.clone() {
let shown_ms = crate::now_ms() - at;
if shown_ms < STATUS_TOAST_MS {