mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Name the Transform Again history step (#17860)
This commit is contained in:
1 parent
d06a8fca82
commit
f55005ce12
4 files changed
+88
-5
No files matched your search
@@ -887,7 +887,8 @@ fn toggle_last_state(s: &mut Session) -> Result<Value> {
|
||||
/// Edit › Transform › Again: replay the last `edit.transform` on the selected layers (#6501).
|
||||
fn transform_again(s: &mut Session) -> Result<Value> {
|
||||
let ids = s.active().map(crate::transform_cmds::selection_targets).unwrap_or_default();
|
||||
let p = transform_again_params(s, &ids)?;
|
||||
let mut p = transform_again_params(s, &ids)?;
|
||||
p["stepName"] = json!(crate::transform_cmds::TRANSFORM_AGAIN);
|
||||
s.execute("edit.transform", p)
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ fn has_layer(s: &Session) -> std::result::Result<(), String> {
|
||||
|
||||
/// The History step name of a shape-tool bounding box scale (`edit.transform` `stepName`).
|
||||
pub const TRANSFORM_PATH: &str = "Transform Path";
|
||||
pub(crate) const TRANSFORM_AGAIN: &str = "Transform Again";
|
||||
|
||||
fn bad(msg: impl Into<String>) -> EngineError {
|
||||
EngineError::BadParams { cmd: "edit.transform".into(), msg: msg.into() }
|
||||
@@ -858,12 +859,12 @@ fn with_panel_target(s: &Session, p: &Value) -> Value {
|
||||
}
|
||||
|
||||
fn transform(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
// The shape tools' bounding box scales live shapes with this command and names the step as
|
||||
// the reference does. Passed in, not renamed after: the edit may land later as a job.
|
||||
// Pass the history label to the edit job so it is used when the transform lands.
|
||||
let step_name = match p.get("stepName") {
|
||||
None | Some(Value::Null) => "Free Transform",
|
||||
Some(Value::String(n)) if n == TRANSFORM_PATH => TRANSFORM_PATH,
|
||||
Some(_) => return Err(bad("`stepName` must be \"Transform Path\"")),
|
||||
Some(Value::String(n)) if n == TRANSFORM_AGAIN => TRANSFORM_AGAIN,
|
||||
Some(_) => return Err(bad("`stepName` must be \"Transform Path\" or \"Transform Again\"")),
|
||||
};
|
||||
let p = &with_panel_target(s, p);
|
||||
let st = s.active().ok_or(EngineError::NoDocument)?;
|
||||
@@ -1037,7 +1038,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
label: "Free Transform",
|
||||
menu: &[],
|
||||
shortcut: None,
|
||||
params: r##"{"layer":id?,"layers":[id,…]? (several layers moved by one box, one step; default with neither = the selected layers, as Photoshop's Free Transform with several selected; refused as locked when the Background is among several),"rect":[x0,y0,x1,y1]? (source frame; default = the layers' content ∩ selection),"quad":[[x,y]×4]? (where the frame's corners go, clockwise from top-left),"matrix":[a,b,c,d,e,f]? (affine alternative),"scaleX":%?,"scaleY":%?,"rotate":deg?,"dx":px?,"dy":px?,"reference":"center|topLeft|top|topRight|left|right|bottomLeft|bottom|bottomRight"|[x,y]="center" (relative alternative: scale and rotate about this point, then move),"interpolation":"bicubic|bilinear|nearest|bicubicSharper|bicubicSmoother|bicubicAutomatic"="bicubic" (Photoshop's bicubic variants render as Bicubic),"stepName":"Transform Path"? (History step name; the shape tools' bounding box sends it; default "Free Transform"; any other value is refused),"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target (an unlinked mask, an alpha channel or the Quick Mask transforms alone; a linked mask moves with its layer)}"##,
|
||||
params: r##"{"layer":id?,"layers":[id,…]? (several layers moved by one box, one step; default with neither = the selected layers, as Photoshop's Free Transform with several selected; refused as locked when the Background is among several),"rect":[x0,y0,x1,y1]? (source frame; default = the layers' content ∩ selection),"quad":[[x,y]×4]? (where the frame's corners go, clockwise from top-left),"matrix":[a,b,c,d,e,f]? (affine alternative),"scaleX":%?,"scaleY":%?,"rotate":deg?,"dx":px?,"dy":px?,"reference":"center|topLeft|top|topRight|left|right|bottomLeft|bottom|bottomRight"|[x,y]="center" (relative alternative: scale and rotate about this point, then move),"interpolation":"bicubic|bilinear|nearest|bicubicSharper|bicubicSmoother|bicubicAutomatic"="bicubic" (Photoshop's bicubic variants render as Bicubic),"stepName":"Transform Path|Transform Again"? (History step name; default "Free Transform"),"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target (an unlinked mask, an alpha channel or the Quick Mask transforms alone; a linked mask moves with its layer)}"##,
|
||||
enabled: has_layer,
|
||||
journal: true,
|
||||
run: transform,
|
||||
|
||||
@@ -193,6 +193,7 @@ mod smart_rerender_fallback;
|
||||
mod style_and_align;
|
||||
mod svg_smart_object;
|
||||
mod tone_target_picker;
|
||||
mod transform_again_history_name_tests;
|
||||
mod transform_budget_unclamped;
|
||||
mod type_format_targets;
|
||||
mod type_postscript_font;
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
use photocraft_doc::LayerId;
|
||||
use photocraft_engine::Session;
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::json;
|
||||
|
||||
fn skewed_document(layer_count: usize) -> (Session, Vec<LayerId>) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 1000, "height": 700})).unwrap();
|
||||
let mut layers = Vec::new();
|
||||
for (name, rect, color) in
|
||||
[("A", Rect::new(100, 100, 300, 300), [1.0, 0.0, 0.0, 1.0]), ("B", Rect::new(500, 300, 800, 500), [0.0, 0.0, 1.0, 1.0])].into_iter().take(layer_count)
|
||||
{
|
||||
let made = s.execute("layer.new.layer", json!({"name": name})).unwrap();
|
||||
let id = LayerId(made["layer"].as_u64().unwrap());
|
||||
s.edit("Paint rectangle", |doc, _| {
|
||||
doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(rect, &color);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
layers.push(id);
|
||||
}
|
||||
s.execute("layer.select", json!({"layer": layers[0].0})).unwrap();
|
||||
for id in &layers[1..] {
|
||||
s.execute("layer.select", json!({"layer": id.0, "mode": "add"})).unwrap();
|
||||
}
|
||||
s.execute("edit.transform", json!({"rect": [100, 100, 800, 500], "quad": [[200, 100], [900, 100], [800, 500], [100, 500]], "interpolation": "nearest"}))
|
||||
.unwrap();
|
||||
(s, layers)
|
||||
}
|
||||
|
||||
fn bounds(s: &Session, layers: &[LayerId]) -> Vec<Rect> {
|
||||
let doc = &s.active().unwrap().doc;
|
||||
layers.iter().map(|id| doc.layer(*id).unwrap().surface().unwrap().content_bounds()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transform_again_adds_one_named_undo_step_for_single_and_multiple_layers() {
|
||||
for layer_count in [1, 2] {
|
||||
let (mut s, layers) = skewed_document(layer_count);
|
||||
let state = s.active().unwrap();
|
||||
let before = state.doc.clone();
|
||||
let previous_steps = state.history.past_len();
|
||||
assert_eq!(state.history.undo_label(), Some("Free Transform"));
|
||||
|
||||
s.execute("edit.transform.again", json!({})).unwrap();
|
||||
let state = s.active().unwrap();
|
||||
assert_eq!(state.history.past_len(), previous_steps + 1);
|
||||
assert_eq!(state.history.undo_label(), Some("Transform Again"));
|
||||
assert_eq!(bounds(&s, &layers), [Rect::new(200, 100, 500, 300), Rect::new(500, 300, 900, 500)][..layer_count]);
|
||||
|
||||
assert_eq!(s.execute("history.undo", json!({})).unwrap(), json!(true));
|
||||
let state = s.active().unwrap();
|
||||
assert_eq!(state.doc, before);
|
||||
assert_eq!(state.history.past_len(), previous_steps);
|
||||
assert_eq!(state.history.redo_label(), Some("Transform Again"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recorded_transform_again_plays_the_concrete_transform_once() {
|
||||
let (mut s, layers) = skewed_document(2);
|
||||
s.execute("prefs.set", json!({"path": "actions.createSingleHistoryState", "value": false})).unwrap();
|
||||
s.execute("actions.record", json!({"name": "Repeat skew"})).unwrap();
|
||||
s.execute("edit.transform.again", json!({})).unwrap();
|
||||
s.execute("actions.stop", json!({})).unwrap();
|
||||
let repeated_bounds = bounds(&s, &layers);
|
||||
let recorded = s.execute("actions.get", json!({"action": "Repeat skew"})).unwrap();
|
||||
let [step] = recorded["steps"].as_array().unwrap().as_slice() else { panic!("expected a single recorded step: {recorded}") };
|
||||
assert_eq!(step[0], "edit.transform");
|
||||
|
||||
assert_eq!(s.execute("history.undo", json!({})).unwrap(), json!(true));
|
||||
let before = s.active().unwrap().history.past_len();
|
||||
let played = s.execute("actions.play", json!({"action": "Repeat skew"})).unwrap();
|
||||
assert_eq!(played["ran"], 1, "{played}");
|
||||
assert!(played.get("failed").is_none(), "{played}");
|
||||
assert_eq!(bounds(&s, &layers), repeated_bounds);
|
||||
let state = s.active().unwrap();
|
||||
assert_eq!(state.history.past_len(), before + 1);
|
||||
assert_eq!(state.history.undo_label(), Some("Transform Again"));
|
||||
}
|
||||
Reference in new issue
Block a user