Merge #17234 into the merge queue batch

Free Transform: grey Warp for a group box (#16731)
This commit is contained in:
David Mann committed 2026-11-27 23:49:38 +01:00
commit 326d338ea9
4 files changed
+116 -18

No files matched your search

@@ -24,7 +24,7 @@ fn type_allow(typed: bool) -> BoxAllow {
}
/// The session over the source frame `rect`: the stored settings, the interpolation default and the nudge pause.
/// `warp_ok` is false where the content cannot warp (a lone mask or channel, several layers, Content-Aware Scale).
/// `warp_ok` is false where the content cannot warp (a lone mask or channel, several layers, a group, Content-Aware Scale).
fn open_session(app: &PhotocraftApp, rect: [f64; 4], allow: BoxAllow, warp_ok: bool) -> TransformSession<SessionWarp> {
TransformSession::new(frame_of(rect))
.allow(allow.with_warp(warp_ok))
@@ -130,6 +130,8 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String>
let opacity = layer.opacity * layer.fill_opacity;
let whole = lifted.is_none();
let allow = type_allow(matches!(layer.content, LayerContent::Text(_)));
// A group doesn't warp, as several layers don't (#16731).
let warp_ok = !frame_only && !matches!(layer.content, LayerContent::Group(_));
// Reading the moving pixels and compositing the layers over them is the heavy part: hundreds
// of megapixels on a large canvas. Done on the UI thread, the window stopped answering for
// long enough that a Wayland compositor dropped it, so a large layer prepares as a job
@@ -161,7 +163,7 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String>
..OpenPreview::new(session, shown, textures, app.ui.tool)
});
app.ui.transform = Some(FreeTransform {
session: open_session(app, rect, allow, !frame_only),
session: open_session(app, rect, allow, warp_ok),
id: session,
layer: id.0,
layers: Vec::new(),
@@ -373,9 +375,38 @@ pub(super) fn moves_kind(app: &PhotocraftApp, t: &FreeTransform, kind: fn(&Layer
app.session.active().is_some_and(|st| moving_layers(t).iter().any(|id| st.doc.layer(*id).is_some_and(|l| kind(&l.content))))
}
/// Does the box move a group as one layer (not its mask, a path or a selection outline)?
pub(super) fn moves_group(app: &PhotocraftApp, t: &FreeTransform) -> bool {
t.layers.is_empty() && !t.selection && t.target.is_none() && t.path.is_none() && moves_kind(app, t, |c| matches!(c, LayerContent::Group(_)))
}
/// Why Warp refuses with several layers selected.
pub const WARP_ONE_LAYER: &str = "Warp works on one layer at a time: select a single layer";
/// Why Warp refuses on a group (#16731).
pub const WARP_GROUP: &str = "Warp doesn't work on a group";
/// Why the open box can't warp: several layers (#6501) or a group (#16731).
fn warp_refusal(app: &PhotocraftApp) -> Option<&'static str> {
let t = app.ui.transform.as_ref()?;
if !t.layers.is_empty() { Some(WARP_ONE_LAYER) } else { moves_group(app, t).then_some(WARP_GROUP) }
}
/// Would [`begin`] box the active group as one layer? Asked before it runs: a large layer's box
/// opens only when its preparing job lands, too late for [`warp_refusal`].
fn opens_group_box(app: &PhotocraftApp) -> bool {
if crate::vector_ui::free_transform_path(app).is_some() {
return false;
}
let target = crate::canvas::paint_target(app);
let Some(st) = app.session.active() else { return false };
let Some(id) = st.active_layer else { return false };
let targets = photocraft_engine::transform_cmds::box_targets(&st.doc, &photocraft_engine::transform_cmds::selection_targets(st));
targets == [id]
&& photocraft_engine::transform_cmds::lone_target(&st.doc, Some(id), &json!({ "target": target })).is_ok_and(|t| t.is_none())
&& st.doc.layer(id).is_some_and(|l| matches!(l.content, LayerContent::Group(_)))
}
/// Free Transform on a copy (⌥⌘T, no menu item): duplicates the active layer (with a selection,
/// its selected pixels, as Layer via Copy) and transforms the copy (#352).
pub fn begin_copy(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> {
@@ -688,14 +719,17 @@ pub fn begin_warp(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), St
if app.ui.transform.as_ref().is_some_and(|t| t.content_aware.is_some()) {
return Err(CAS_ONLY_SCALES.into());
}
if app.ui.transform.as_ref().is_some_and(|t| !t.layers.is_empty()) {
return Err(WARP_ONE_LAYER.into());
if let Some(why) = warp_refusal(app) {
return Err(why.into());
}
if app.ui.transform.is_none() {
if opens_group_box(app) {
return Err(WARP_GROUP.into());
}
begin(app, ctx)?;
if app.ui.transform.as_ref().is_some_and(|t| !t.layers.is_empty()) {
if let Some(why) = warp_refusal(app) {
cancel(app);
return Err(WARP_ONE_LAYER.into());
return Err(why.into());
}
}
enter_warp(app, ctx);
@@ -2,7 +2,7 @@ use craft::behavior::EditEvent;
use craft::behavior::kurbo::Point;
use craft::draw::ui::transform::{TransformEdit, TransformRequest};
use super::begin::{fold_made, moves_kind, moving_layers, rect_to_quad_affine, take_back_made};
use super::begin::{fold_made, moves_group, moves_kind, moving_layers, rect_to_quad_affine, take_back_made};
use super::{
Arc, DocId, FreeTransform, JobEvent, JobId, JobOutcome, LayerContent, LayerId, MadeLayer, OpenPreview, PhotocraftApp, SessionWarp, Several, json,
preset_for_command, swap_mode_texels,
@@ -398,6 +398,20 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> Option<bool> {
return Some(false);
}
let t = app.ui.transform.as_ref()?;
// One box over several layers doesn't warp (#6501), nor one over a group (#16731), and not
// while its commit runs either.
if matches!(
id,
"edit.transform.warp"
| "layer.smartObjects.warp"
| "edit.transform.splitWarpCrosswise"
| "edit.transform.splitWarpHorizontally"
| "edit.transform.splitWarpVertically"
| "edit.transform.removeWarpSplit"
) && (!t.layers.is_empty() || moves_group(app, t))
{
return Some(false);
}
if committing(app).is_some() {
return box_command(id).then_some(false);
}
@@ -414,17 +428,6 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> Option<bool> {
SAVE_FOR_WEB | COPY_CSS => Some(false),
// Rotate and Flip turn the box (not while warping, where the mesh is the box).
_ if preset_for_command(id).is_some() => Some(t.session.warp().is_none()),
// One box over several layers doesn't warp (#6501).
"edit.transform.warp"
| "layer.smartObjects.warp"
| "edit.transform.splitWarpCrosswise"
| "edit.transform.splitWarpHorizontally"
| "edit.transform.splitWarpVertically"
| "edit.transform.removeWarpSplit"
if !t.layers.is_empty() =>
{
Some(false)
}
// Editable type can't be distorted (#2630, as #2701 rules for the menus outside a box).
"edit.transform.distort" | "edit.transform.perspective" if moves_type(app, t) => Some(false),
_ => None,
@@ -0,0 +1,58 @@
//! #16731: a box over a group doesn't warp, as one over several layers doesn't; a box over one
//! layer of any other kind still does.
use super::*;
use crate::transform_tool::begin::WARP_GROUP;
/// The Edit › Transform rows of the Warp block.
const WARP_BLOCK: [&str; 5] = [
"edit.transform.warp",
"edit.transform.splitWarpCrosswise",
"edit.transform.splitWarpHorizontally",
"edit.transform.splitWarpVertically",
"edit.transform.removeWarpSplit",
];
/// Is the Warp row of the open box's right-click menu enabled?
fn warp_row(app: &PhotocraftApp) -> bool {
let words = TransformMenuWords::english();
open(app).session.menu(&words).iter().find(|e| matches!(e.value(), Some(TransformChoice::Warp))).map(|e| e.is_enabled()).expect("the menu has a Warp row")
}
#[test]
fn a_group_box_greys_warp_in_its_menu_and_in_edit_transform() {
let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
let ctx = app.ctx().clone();
app.session.execute("layer.new.groupFromLayers", json!({})).unwrap();
let st = app.session.active().unwrap();
assert!(st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| matches!(l.content, LayerContent::Group(_))), "the group is active");
begin(&mut app, &ctx).unwrap();
assert!(open(&app).layers.is_empty(), "a box over the group as one layer");
assert!(!warp_row(&app), "Warp is greyed in the box's menu");
for id in WARP_BLOCK {
assert!(!crate::menus::is_enabled(&app, id), "{id} is greyed while the group box is open");
}
// Asked anyway (automation), Warp refuses and leaves the box as it was.
assert_eq!(begin_warp(&mut app, &ctx), Err(WARP_GROUP.into()));
assert!(open(&app).session.warp().is_none(), "the box doesn't switch to Warp");
cancel(&mut app);
// With no box open, Warp refuses without leaving one behind.
assert_eq!(begin_warp(&mut app, &ctx), Err(WARP_GROUP.into()));
assert!(app.ui.transform.is_none(), "no box is left open");
}
#[test]
fn single_pixel_type_and_smart_object_layers_keep_warp() {
let ctx = egui::Context::default();
let pixel = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
let mut typed = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
typed.run("type.create", json!({"x": 10, "y": 40, "text": "Hi"})).unwrap();
let mut smart = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));
smart.session.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();
for (name, mut app) in [("pixel", pixel), ("type", typed), ("smart object", smart)] {
begin(&mut app, &ctx).unwrap();
assert!(warp_row(&app), "a {name} layer's box offers Warp");
assert!(crate::menus::is_enabled(&app, "edit.transform.warp"), "Edit › Transform › Warp is live over a {name} layer");
cancel(&mut app);
}
}
@@ -2219,3 +2219,6 @@ fn an_unskewed_box_shows_its_horizontal_skew_as_zero() {
#[path = "warp_remove_split_tests.rs"]
mod warp_remove_split_tests;
#[path = "group_warp_greyed_tests.rs"]
mod group_warp_greyed_tests;