mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
With two or more layers selected the command is disabled and a call without a layer is refused with the reason; a call that names its layer converts that layer as before.
134 lines
4.8 KiB
Rust
134 lines
4.8 KiB
Rust
//! Layer › New › Background from Layer with several layers selected (#17306).
|
||
//!
|
||
//! Measured in the reference: with two layers selected the menu item is greyed. With one layer it
|
||
//! is live for pixel, type, smart object, fill and shape layers and greyed for groups and
|
||
//! adjustment layers. A script or action that names the layer (`"layer": id`) converts that
|
||
//! layer, whatever is selected.
|
||
|
||
use photocraft_doc::LayerId;
|
||
use photocraft_engine::Session;
|
||
use serde_json::{Value, json};
|
||
|
||
const CMD: &str = "layer.new.backgroundFromLayer";
|
||
|
||
/// A document without a Background: a transparent "Layer 1" plus A and B on top.
|
||
fn no_background() -> (Session, [LayerId; 3]) {
|
||
let mut s = Session::new();
|
||
s.execute("file.new", json!({"width": 16, "height": 12, "background": "transparent"})).unwrap();
|
||
let base = s.active().unwrap().active_layer.unwrap();
|
||
let a = new_layer(&mut s, "A");
|
||
let b = new_layer(&mut s, "B");
|
||
(s, [base, a, b])
|
||
}
|
||
|
||
fn new_layer(s: &mut Session, name: &str) -> LayerId {
|
||
LayerId(s.execute("layer.new.layer", json!({"name": name})).unwrap()["layer"].as_u64().unwrap())
|
||
}
|
||
|
||
fn ids(r: &Value) -> LayerId {
|
||
LayerId(r["layer"].as_u64().unwrap())
|
||
}
|
||
|
||
fn order(s: &Session) -> Vec<LayerId> {
|
||
s.active().unwrap().doc.layers.iter().map(|l| l.id).collect()
|
||
}
|
||
|
||
fn background(s: &Session) -> Option<LayerId> {
|
||
s.active().unwrap().doc.background().map(|l| l.id)
|
||
}
|
||
|
||
#[test]
|
||
fn two_selected_layers_grey_the_command_and_a_call_without_layer_is_refused() {
|
||
let (mut s, [_, a, b]) = no_background();
|
||
s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
|
||
assert_eq!(s.active().unwrap().selected_layers().len(), 2);
|
||
assert!(!s.is_enabled(CMD), "greyed with two layers selected");
|
||
let why = s.disabled_reason(CMD).unwrap();
|
||
assert!(why.contains("layers selected"), "the reason names the multi-selection: {why}");
|
||
|
||
let before = order(&s);
|
||
let revision = s.active().unwrap().revision;
|
||
let err = s.execute(CMD, json!({})).unwrap_err().to_string();
|
||
assert!(err.contains("layers selected"), "the error names the reason: {err}");
|
||
assert_eq!(order(&s), before, "no layer moved");
|
||
assert_eq!(background(&s), None, "no Background made");
|
||
assert_eq!(s.active().unwrap().revision, revision, "the document did not change");
|
||
|
||
// Three layers selected: greyed too.
|
||
let (mut s, [base, a, b]) = no_background();
|
||
s.execute("layer.select", json!({"layers": [base.0, a.0, b.0]})).unwrap();
|
||
assert!(!s.is_enabled(CMD));
|
||
assert!(s.execute(CMD, json!({"layer": null})).is_err(), "a null layer is the selection too");
|
||
}
|
||
|
||
#[test]
|
||
fn an_explicit_layer_converts_that_layer_with_several_selected() {
|
||
// The named layer is one of the selected ones.
|
||
let (mut s, [_, a, b]) = no_background();
|
||
s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
|
||
let r = s.execute(CMD, json!({"layer": a.0})).unwrap();
|
||
assert_eq!(ids(&r), a);
|
||
assert_eq!(background(&s), Some(a), "A is the Background");
|
||
assert_eq!(order(&s)[0], a, "at the bottom");
|
||
|
||
// The named layer is outside the selection.
|
||
let (mut s, [base, a, b]) = no_background();
|
||
s.execute("layer.select", json!({"layers": [a.0, b.0]})).unwrap();
|
||
s.execute(CMD, json!({"layer": base.0})).unwrap();
|
||
assert_eq!(background(&s), Some(base));
|
||
}
|
||
|
||
#[test]
|
||
fn one_selected_layer_keeps_todays_rules() {
|
||
let live = |make: &dyn Fn(&mut Session)| {
|
||
let (mut s, _) = no_background();
|
||
make(&mut s);
|
||
assert_eq!(s.active().unwrap().selected_layers().len(), 1);
|
||
s.is_enabled(CMD)
|
||
};
|
||
assert!(live(&|_| {}), "pixel layer");
|
||
assert!(
|
||
live(&|s| {
|
||
s.execute("type.create", json!({"x": 2, "y": 10, "text": "I", "size": 8})).unwrap();
|
||
}),
|
||
"type layer"
|
||
);
|
||
assert!(
|
||
live(&|s| {
|
||
s.execute("edit.fill", json!({"contents": "black"})).unwrap();
|
||
s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();
|
||
}),
|
||
"smart object"
|
||
);
|
||
assert!(
|
||
live(&|s| {
|
||
s.execute("layer.newFillLayer.solidColor", json!({"color": "#4080c0"})).unwrap();
|
||
}),
|
||
"solid colour fill layer"
|
||
);
|
||
assert!(
|
||
live(&|s| {
|
||
s.execute("shape.create", json!({"kind": "rect", "rect": [2, 2, 6, 6]})).unwrap();
|
||
}),
|
||
"shape layer"
|
||
);
|
||
assert!(
|
||
!live(&|s| {
|
||
s.execute("layer.new.group", json!({})).unwrap();
|
||
}),
|
||
"group"
|
||
);
|
||
assert!(
|
||
!live(&|s| {
|
||
s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap();
|
||
}),
|
||
"adjustment layer"
|
||
);
|
||
|
||
// And one selected pixel layer converts as before.
|
||
let (mut s, [_, a, _]) = no_background();
|
||
s.execute("layer.select", json!({"layer": a.0})).unwrap();
|
||
s.execute(CMD, json!({})).unwrap();
|
||
assert_eq!(background(&s), Some(a));
|
||
}
|