Merge pull request #18700

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 01:07:07 +00:00
commit f989ef0f27
3 files changed
+227

No files matched your search

+42
View File
@@ -129,9 +129,47 @@ pub fn disabled_for_selection(doc: &Document, selected: &[LayerId], active: Opti
!any
}
/// Puppet Warp, Perspective Warp and Content-Aware Scale work on one layer: with two or more
/// layers selected the reference greys them (#18501). Measured on layers at the top of the stack;
/// a group among them, or layers inside a group, keep the engine's own rule.
const ONE_LAYER_DISTORTS: [&str; 3] = ["edit.puppetWarp", "edit.perspectiveWarp", "edit.contentAwareScale"];
fn several_layers_block(doc: &Document, selected: &[LayerId], id: &str) -> bool {
ONE_LAYER_DISTORTS.contains(&id)
&& selected.len() > 1
&& selected.iter().all(|l| doc.layer(*l).is_some_and(|layer| !layer.is_group()) && doc.layers.index().parent(*l).is_none())
}
/// Why a menu item that [`disabled`] greys for the layer selection is refused when it is invoked
/// anyway (a shortcut, the control channel's `ui.menu.invoke`), before any session opens. A
/// control call (`{"ui": …}`) for a Puppet Warp or Perspective Warp already open is not refused: it
/// commits, cancels or edits that session, opened on one layer, whatever is selected since.
pub fn refusal(app: &crate::PhotocraftApp, id: &str, params: &serde_json::Value) -> Option<String> {
let open = match id {
"edit.puppetWarp" => app.distort.puppet.is_some(),
"edit.perspectiveWarp" => app.distort.perspective.is_some(),
_ => false,
};
if open && params.get("ui").is_some() {
return None;
}
let st = app.session.active()?;
several_layers_block(&st.doc, st.selected_layers(), id).then(|| {
let name = match id {
"edit.puppetWarp" => "Puppet Warp",
"edit.perspectiveWarp" => "Perspective Warp",
_ => "Content-Aware Scale",
};
format!("{name} works on one layer at a time: select a single layer")
})
}
pub fn disabled(app: &crate::PhotocraftApp, id: &str) -> bool {
let Some(st) = app.session.active() else { return false };
let active = st.active_layer.and_then(|a| st.doc.layer(a));
if several_layers_block(&st.doc, st.selected_layers(), id) {
return true;
}
// The split warps edit the open Warp box's grid: with none they are greyed, also on a smart
// object with a stored warp, as the reference greys them there (#12494, #12942).
if matches!(
@@ -521,3 +559,7 @@ mod tests {
#[cfg(test)]
#[path = "warp_split_enable_tests.rs"]
mod warp_split_enable_tests;
#[cfg(test)]
#[path = "several_layers_distort_greyed_tests.rs"]
mod several_layers_distort_greyed_tests;
+4
View File
@@ -86,6 +86,10 @@ pub fn invoke(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, params: Va
if !keeps_box_drag(id) {
crate::type_box_preview::end_drag(app);
}
// An item the menu greys for the layer selection opens nothing when invoked anyway (#18501).
if let Some(why) = crate::enable_rules::refusal(app, id, &params) {
return Err(why);
}
if crate::discard_ui::intercept(app, id, &params) {
return Ok(Value::Null);
}
@@ -0,0 +1,181 @@
//! Puppet Warp, Perspective Warp and Content-Aware Scale with several layers selected (#18501):
//! greyed in the Edit menu and in `ui.menu.list`, refused when invoked anyway (no session opens,
//! nothing changes); one pixel layer keeps all three, and the cases not measured keep today's state.
use photocraft_doc::LayerId;
use serde_json::{Value, json};
use crate::PhotocraftApp;
use crate::control::{ControlRequest, Reply};
const THREE: [&str; 3] = ["edit.puppetWarp", "edit.perspectiveWarp", "edit.contentAwareScale"];
fn layer_of(v: Value) -> LayerId {
LayerId(v["layer"].as_u64().unwrap())
}
/// A 100 × 100 white document with a new pixel layer holding a block of colour, active.
fn painted_layer(app: &mut PhotocraftApp, name: &str, x: i64, color: &str) -> LayerId {
let layer = layer_of(app.run("layer.new.layer", json!({ "name": name })).unwrap());
app.run("select.rect", json!({"x": x, "y": 10, "width": 30, "height": 30})).unwrap();
app.run("edit.fill", json!({"contents": "color", "color": color})).unwrap();
app.run("select.deselect", json!({})).unwrap();
layer
}
fn app_with_two_layers() -> (Box<PhotocraftApp>, LayerId, LayerId) {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 100, "height": 100, "background": "white"})).unwrap();
app.sync_views();
let a = painted_layer(&mut app, "A", 10, "#ff0000");
let b = painted_layer(&mut app, "B", 50, "#0000ff");
(app, a, b)
}
/// The Edit menu row's state as the menu builds it.
fn in_menu(app: &PhotocraftApp, id: &str) -> bool {
let rows = crate::menus::menu_rows(app);
let row = crate::menus::items::row(&rows, id).map(|r| (r.path.clone(), r.enabled));
let (path, enabled) = row.unwrap_or_else(|| panic!("{id} is in the menu"));
assert_eq!(path.first().map(String::as_str), Some("Edit"), "{id}");
enabled
}
fn control(app: &mut PhotocraftApp, method: &str, params: Value) -> Result<Value, String> {
let ctx = app.ctx().clone();
let (request, _reply) = ControlRequest::new(method, params);
match crate::control::handle(app, &ctx, &request) {
Reply::Done(reply) if reply["ok"] == true => Ok(reply),
Reply::Done(reply) => Err(reply["error"].as_str().unwrap_or_default().to_owned()),
_ => panic!("expected an immediate reply to {method}"),
}
}
/// `enabled` of `id` in `ui.menu.list`.
fn listed(app: &mut PhotocraftApp, id: &str) -> bool {
let list = control(app, "ui.menu.list", json!({})).unwrap();
let row = list["result"].as_array().unwrap().iter().find(|row| row["id"] == id).cloned();
row.unwrap_or_else(|| panic!("{id} is listed"))["enabled"].as_bool().unwrap()
}
fn no_session(app: &PhotocraftApp) -> bool {
app.distort.puppet.is_none() && app.distort.perspective.is_none() && app.ui.transform.is_none()
}
fn history_len(app: &PhotocraftApp) -> usize {
app.session.active().unwrap().history.entries().len()
}
#[test]
fn two_selected_pixel_layers_grey_the_three_and_invoking_them_opens_nothing() {
let (mut app, a, b) = app_with_two_layers();
app.run("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
assert_eq!(app.session.active().unwrap().selected_layers().len(), 2);
for id in THREE {
assert!(!in_menu(&app, id), "{id} is greyed in the Edit menu with two layers selected");
assert!(!listed(&mut app, id), "{id} is disabled in ui.menu.list with two layers selected");
}
let steps = history_len(&app);
for id in THREE {
let refused = control(&mut app, "ui.menu.invoke", json!({ "id": id }));
let why = refused.expect_err(&format!("{id} is refused with two layers selected"));
assert!(why.contains("one layer at a time"), "{id}: {why}");
assert!(no_session(&app), "{id} opened a session");
let ctx = app.ctx().clone();
assert!(crate::menus::invoke(&mut app, &ctx, id, json!({})).is_err(), "{id} through the menu's own invoke");
assert!(no_session(&app), "{id} opened a session");
}
assert_eq!(history_len(&app), steps, "nothing changed");
assert_eq!(app.session.active().unwrap().selected_layers(), [a, b], "the selection stays");
}
#[test]
fn free_transform_and_edit_transform_stay_enabled_with_two_layers() {
let (mut app, a, b) = app_with_two_layers();
app.run("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
for id in [
"edit.freeTransform",
"edit.transform.scale",
"edit.transform.rotate",
"edit.transform.skew",
"edit.transform.distort",
"edit.transform.perspective",
"edit.transform.rotate180",
"edit.transform.rotate90Cw",
"edit.transform.rotate90Ccw",
"edit.transform.flipHorizontal",
"edit.transform.flipVertical",
] {
assert!(in_menu(&app, id), "{id} stays enabled with two layers selected");
}
}
#[test]
fn one_pixel_layer_keeps_all_three() {
let (mut app, _, b) = app_with_two_layers();
app.run("layer.select", json!({"layer": b.0})).unwrap();
for id in THREE {
assert!(in_menu(&app, id), "{id} is enabled for one pixel layer");
assert!(listed(&mut app, id), "{id} is listed enabled for one pixel layer");
}
control(&mut app, "ui.menu.invoke", json!({"id": "edit.puppetWarp"})).expect("Puppet Warp opens on one layer");
assert!(app.distort.puppet.is_some(), "the pin mesh is open");
}
#[test]
fn the_cases_not_measured_keep_todays_state() {
// Two layers inside a group.
let (mut app, a, b) = app_with_two_layers();
app.run("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
app.run("layer.groupLayers", json!({})).unwrap();
app.run("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
assert_eq!(app.session.active().unwrap().selected_layers().len(), 2);
for id in THREE {
assert!(in_menu(&app, id), "{id}: two layers inside a group keep today's state");
}
// A group and a layer, the layer active.
let (mut app, a, b) = app_with_two_layers();
app.run("layer.select", json!({"layer": a.0})).unwrap();
let group = layer_of(app.run("layer.groupLayers", json!({})).unwrap());
app.run("layer.select", json!({"layers": [group.0, b.0]})).unwrap();
app.run("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap();
assert_eq!(app.session.active().unwrap().active_layer, Some(b));
for id in THREE {
assert!(in_menu(&app, id), "{id}: a group and a layer keep today's state");
}
// A linked layer: one layer selected, its partner not.
let (mut app, a, b) = app_with_two_layers();
app.run("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
app.run("layer.linkLayers", json!({})).unwrap();
app.run("layer.select", json!({"layer": b.0})).unwrap();
for id in THREE {
assert!(in_menu(&app, id), "{id}: a linked layer keeps today's state");
}
}
/// A Puppet Warp or Perspective Warp opened on one layer still takes its control calls (commit, cancel) when two
/// layers are selected afterwards: the refusal is for opening a new session only.
#[test]
fn an_open_warp_still_commits_and_cancels_with_two_layers_selected() {
for (id, how) in [("edit.puppetWarp", "commit"), ("edit.puppetWarp", "cancel"), ("edit.perspectiveWarp", "commit"), ("edit.perspectiveWarp", "cancel")] {
let (mut app, a, b) = app_with_two_layers();
app.run("layer.select", json!({"layer": b.0})).unwrap();
control(&mut app, "ui.menu.invoke", json!({ "id": id })).unwrap_or_else(|e| panic!("{id} opens on one layer: {e}"));
assert!(!no_session(&app), "{id}: a session is open");
app.run("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
assert_eq!(app.session.active().unwrap().selected_layers().len(), 2);
let reply = control(&mut app, "ui.menu.invoke", json!({ "id": id, "params": { "ui": { how: true } } }));
reply.unwrap_or_else(|e| panic!("{id} {how} with two layers selected: {e}"));
assert!(no_session(&app), "{id}: {how} closes the session");
}
// Opening a new session with two layers selected is still refused, also with control params.
let (mut app, a, b) = app_with_two_layers();
app.run("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
for id in ["edit.puppetWarp", "edit.perspectiveWarp"] {
let refused = control(&mut app, "ui.menu.invoke", json!({ "id": id, "params": { "ui": { "commit": true } } }));
assert!(refused.is_err_and(|why| why.contains("one layer at a time")), "{id}: no session, so it is refused");
assert!(no_session(&app), "{id} opened a session");
}
}