Ctrl+T on a selected path transforms the path (#1871)

With Path Selection or Direct Selection and a path, Edit > Free Transform (and the Transform
items) frame the path's anchors and handles and commit one path.transform step instead of
moving pixels; it works on the Background too. Distort and Perspective, which an affine path
transform can't follow, report an error. Fixes #1770.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
AlonandClaude Opus 5.5 authored and GitHub committed 2026-10-09 13:04:08 -04:00
1 parent a4a2583280
commit 9ba843290e
5 files changed
+170 -3

No files matched your search

+5
View File
@@ -69,6 +69,11 @@ pub fn disabled_for(doc: &Document, active: Option<&Layer>, id: &str) -> bool {
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));
// Free Transform Path works on the Background too: it moves the path, not the pixels.
let transform = id == "edit.freeTransform" || (id.starts_with("edit.transform.") && id != "edit.transform.again");
if transform && crate::vector_ui::free_transform_path(app).is_some() {
return false;
}
disabled_for(&st.doc, active, id)
}
+4
View File
@@ -700,6 +700,10 @@ pub struct TransformSession {
/// the Quick Mask by itself (`None`: the layer, with its linked masks).
#[serde(default)]
pub target: Option<serde_json::Value>,
/// Free Transform Path: `path.transform`'s `name` (and `layer`) when the box moves a
/// path's anchors and handles instead of pixels.
#[serde(default)]
pub path: Option<serde_json::Value>,
/// The layer was made for this session (⌥⌘T's copy, #352; a file dropped on the canvas), so
/// Cancel takes it back and OK folds it into one history step with the transform.
#[serde(default)]
+143 -2
View File
@@ -66,6 +66,8 @@ pub struct TransformPreview {
steps: Steps,
/// The tool the session began with: picking another one applies the transform.
tool: Tool,
/// Free Transform Path: the path as it was, drawn through the box instead of pixels.
path: Option<photocraft_doc::vector::Path>,
}
/// Grab radius of the box's handles, in screen points. Generous, so a corner is easy to catch;
@@ -131,6 +133,9 @@ fn corners(r: [f64; 4]) -> [[f64; 2]; 4] {
/// Start Free Transform on the active layer (or its selected pixels), or on the targeted unlinked
/// layer mask, alpha channel or Quick Mask.
pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> {
if let Some((params, path)) = crate::vector_ui::free_transform_path(app) {
return begin_path(app, ctx, params, path);
}
let target = crate::canvas::paint_target(app);
let st = app.session.active().ok_or("no document")?;
let doc = st.doc.clone();
@@ -179,6 +184,7 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String>
split_quick: false,
steps: Steps::default(),
tool: app.ui.tool,
path: None,
});
app.ui.transform = Some(TransformSession {
session,
@@ -190,6 +196,7 @@ pub fn begin(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String>
warp: None,
selection: false,
target: None,
path: None,
made: None,
mode: Default::default(),
});
@@ -313,6 +320,7 @@ fn begin_lone(
split_quick: false,
steps: Steps::default(),
tool: app.ui.tool,
path: None,
});
app.ui.transform = Some(TransformSession {
session,
@@ -324,6 +332,7 @@ fn begin_lone(
warp: None,
selection: false,
target: Some(target),
path: None,
made: None,
mode: Default::default(),
});
@@ -331,6 +340,69 @@ fn begin_lone(
Ok(())
}
/// Edit › Free Transform Path: the box frames the path's anchors and handles and draws the
/// path through it; OK commits one `path.transform` step.
fn begin_path(app: &mut PhotocraftApp, ctx: &egui::Context, params: serde_json::Value, path: photocraft_doc::vector::Path) -> Result<(), String> {
let (x0, y0, x1, y1) = path.control_bounds().ok_or("the path has no points to transform")?;
let st = app.session.active().ok_or("no document")?;
let doc = st.doc.clone();
let layer = st.active_layer.or_else(|| doc.layers.first().map(|l| l.id)).ok_or("no layer")?;
crate::type_tool::commit(app);
// A straight horizontal or vertical path still gets a box to turn.
let rect = [x0, y0, x1.max(x0 + 1.0), y1.max(y0 + 1.0)];
let session = app.ui.alloc_id();
let texture =
crate::transform_tex::PreviewTextures::new(ctx, format!("transform-{session}"), egui::ColorImage::new([1, 1], vec![Color32::TRANSPARENT]), [1.0, 1.0]);
app.transform_preview = Some(TransformPreview {
session,
doc,
texture,
opacity: 1.0,
gesture: None,
warp_drag: None,
split_tool: None,
split_pointer: None,
split_placing: false,
split_quick: false,
steps: Steps::default(),
tool: app.ui.tool,
path: Some(path),
});
app.ui.transform = Some(TransformSession {
session,
layer: layer.0,
rect,
quad: corners(rect),
pivot: [(rect[0] + rect[2]) / 2.0, (rect[1] + rect[3]) / 2.0],
interpolation: "bicubic".into(),
warp: None,
selection: false,
target: None,
path: Some(params),
made: None,
mode: Default::default(),
});
start_steps(app);
Ok(())
}
/// The affine map taking `rect`'s corners to `quad`'s; `None` when the box was distorted out of a
/// parallelogram (Distort, Perspective), which an affine path transform can't follow.
fn rect_to_quad_affine(r: [f64; 4], q: [[f64; 2]; 4]) -> Option<photocraft_geom::Affine> {
let (w, h) = (r[2] - r[0], r[3] - r[1]);
if w <= 0.0 || h <= 0.0 {
return None;
}
let tol = 1e-3 * (1.0 + w.max(h));
if (q[0][0] + q[2][0] - q[1][0] - q[3][0]).abs() > tol || (q[0][1] + q[2][1] - q[1][1] - q[3][1]).abs() > tol {
return None;
}
let (a, b) = ((q[1][0] - q[0][0]) / w, (q[1][1] - q[0][1]) / w);
let (c, d) = ((q[3][0] - q[0][0]) / h, (q[3][1] - q[0][1]) / h);
let m = [a, b, c, d, q[0][0] - a * r[0] - c * r[1], q[0][1] - b * r[0] - d * r[1]];
m.iter().all(|v| v.is_finite()).then_some(photocraft_geom::Affine { m })
}
/// Start Select › Transform Selection: the same box over the selection's bounds, previewing the
/// selection mask (the marching ants are hidden meanwhile); commits `select.transformSelection`.
pub fn begin_selection(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> {
@@ -372,6 +444,7 @@ pub fn begin_selection(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(
split_quick: false,
steps: Steps::default(),
tool: app.ui.tool,
path: None,
});
app.ui.transform = Some(TransformSession {
session,
@@ -383,6 +456,7 @@ pub fn begin_selection(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(
warp: None,
selection: true,
target: None,
path: None,
made: None,
mode: Default::default(),
});
@@ -410,8 +484,8 @@ pub fn enter_warp(app: &mut PhotocraftApp) {
let existing =
app.session.active().and_then(|d| d.doc.layer(LayerId(app.ui.transform.as_ref()?.layer)).and_then(photocraft_engine::warp_cmds::smart_warp_doc_space));
let Some(t) = app.ui.transform.as_mut() else { return };
// Warp moves layers only: a lone mask or channel keeps the box.
if t.warp.is_some() || t.target.is_some() {
// Warp moves layers only: a lone mask, channel or path keeps the box.
if t.warp.is_some() || t.target.is_some() || t.path.is_some() {
return;
}
if t.quad == corners(t.rect)
@@ -526,6 +600,21 @@ pub fn commit(app: &mut PhotocraftApp) {
}
return;
}
if let Some(target) = t.path {
if t.warp.is_some() || t.quad == corners(t.rect) {
return;
}
let Some(a) = rect_to_quad_affine(t.rect, t.quad) else {
app.ui.status = "Distort and Perspective can't be applied to a path".into();
return;
};
let mut p = target;
p["matrix"] = json!(a.m);
if let Err(e) = app.run("path.transform", p) {
app.ui.status = e;
}
return;
}
let made = t.made;
let r = if let Some(w) = &t.warp {
if w.is_identity() {
@@ -1225,6 +1314,17 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform
painter.add(mesh);
}
let accent = crate::theme::Tokens::get(painter.ctx()).accent;
if let (Some(path), Some(h)) = (&pv.path, Homography::rect_to_quad(t.rect, t.quad)) {
crate::vector_ui::draw_outline(
painter,
path,
&|q| {
let (x, y) = h.apply(q[0], q[1]);
xf.to_screen(x as f32, y as f32)
},
accent,
);
}
let pts: Vec<Pos2> = t.quad.iter().map(|q| scr(*q)).collect();
painter.add(egui::Shape::closed_line(pts.clone(), Stroke::new(1.0, accent)));
let mids: Vec<Pos2> = (0..4).map(|i| pts[i].lerp(pts[(i + 1) % 4], 0.5)).collect();
@@ -1674,6 +1774,7 @@ mod tests {
warp: None,
selection: false,
target: None,
path: None,
made: None,
mode: Default::default(),
}
@@ -2665,4 +2766,44 @@ mod tests {
assert_eq!(app.ui.transform.as_ref().unwrap().target, None);
assert_eq!(app.ui.transform.as_ref().unwrap().rect, [8.0, 8.0, 24.0, 24.0]);
}
/// ⌘T with Path Selection and a path transforms the path, not the layer's pixels, as one
/// undoable step; it works on the Background too.
#[test]
fn free_transform_moves_the_selected_path_not_pixels() {
let ctx = egui::Context::default();
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap();
app.sync_views();
app.run("path.set", json!({"name": "work", "path": {"subpaths": [{"closed": true, "knots": [[10, 10], [30, 10], [30, 20], [10, 20]]}]}})).unwrap();
app.ui.tool = Tool::PathSelection;
assert!(crate::menus::is_enabled(&app, "edit.freeTransform"), "a path is transformable on the Background");
let pixels = app.session.active().unwrap().doc.layers[0].surface().cloned();
let steps = app.session.active().unwrap().history.past_len();
crate::menus::invoke(&mut app, &ctx, "edit.freeTransform", json!({})).unwrap();
let t = app.ui.transform.as_mut().unwrap();
assert_eq!(t.rect, [10.0, 10.0, 30.0, 20.0], "the box frames the path");
assert_eq!(t.path, Some(json!({"name": "work"})));
// Twice as wide and 5 px down.
t.quad = [[10.0, 15.0], [50.0, 15.0], [50.0, 25.0], [10.0, 25.0]];
commit(&mut app);
let st = app.session.active().unwrap();
assert_eq!(st.history.past_len(), steps + 1, "one step");
let wp = st.doc.work_path.as_ref().unwrap();
let anchors: Vec<[f64; 2]> = wp.subpaths[0].knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect();
assert_eq!(anchors, vec![[10.0, 15.0], [50.0, 15.0], [50.0, 25.0], [10.0, 25.0]]);
assert_eq!(st.doc.layers[0].surface().cloned(), pixels, "the pixels stay put");
// A box pulled out of a parallelogram (Distort) can't be applied to a path: an error, no step.
crate::menus::invoke(&mut app, &ctx, "edit.freeTransform", json!({})).unwrap();
app.ui.transform.as_mut().unwrap().quad[2] = [70.0, 40.0];
commit(&mut app);
assert_eq!(app.session.active().unwrap().history.past_len(), steps + 1);
assert!(!app.ui.status.is_empty());
// Without a path tool, ⌘T is the pixel transform again.
app.ui.tool = Tool::Move;
app.run("layer.new.layer", json!({})).unwrap();
assert!(crate::vector_ui::free_transform_path(&app).is_none());
}
}
+1
View File
@@ -122,6 +122,7 @@ pub(crate) fn frame(app: &mut PhotocraftApp) -> Option<TransformSession> {
warp: None,
selection: false,
target: None,
path: None,
made: None,
mode: Default::default(),
})
+17 -1
View File
@@ -315,6 +315,21 @@ fn target_path(app: &PhotocraftApp) -> Option<(PathTarget, Path)> {
app.session.active()?.doc.work_path.clone().map(|p| (PathTarget::Work, p))
}
/// Edit › Free Transform Path (⌘T with Path Selection or Direct Selection): the path the
/// box transforms (the one Path Selection edits) and `path.transform`'s params that name it.
pub(crate) fn free_transform_path(app: &PhotocraftApp) -> Option<(Value, Path)> {
if !matches!(app.ui.tool, Tool::PathSelection | Tool::DirectSelection) {
return None;
}
let (target, path) = target_path(app)?;
path.control_bounds()?;
let params = match target {
PathTarget::Shape(id) | PathTarget::VectorMask(id) => json!({"name": "layer", "layer": id}),
PathTarget::Work => json!({"name": "work"}),
};
Some((params, path))
}
pub fn path_selection_finish(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {
let (dx, dy) = (end[0] - start[0], end[1] - start[1]);
if dx.abs() + dy.abs() < 0.5 {
@@ -408,7 +423,8 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform
}
};
// View › Show › Target Path (under Extras) hides the paths; the Pen's path in progress stays.
let paths = app.ui.view.shows(app.ui.view.show.target_path);
// Free Transform Path draws the path through its box instead (transform_tool).
let paths = app.ui.view.shows(app.ui.view.show.target_path) && !app.ui.transform.as_ref().is_some_and(|t| t.path.is_some());
if crate::direct_select::shows(app, painter.ctx().input(|i| i.modifiers)) {
// Direct Selection (or the Pen with ⌘/Ctrl held) draws the paths it edits (#790).
crate::direct_select::draw_overlay(app, painter, &to_scr, accent, paths);