mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Fix filter-mask copy, named targets and mask-view switching
This commit is contained in:
1 parent
d5c0b699cb
commit
1017babd35
7 files changed
+114
-17
No files matched your search
@@ -304,15 +304,18 @@ fn copy(s: &mut Session, merged: bool) -> Result<Value> {
|
||||
Ok(json!({"bounds": [b.x0, b.y0, b.width(), b.height()]}))
|
||||
}
|
||||
|
||||
/// Copy with the layer mask targeted (`"target":"mask"`, #2928): the mask's values as a
|
||||
/// Copy the targeted layer mask (`"target":"mask"`, #2928) or smart-filter mask
|
||||
/// (`"target":"filterMask"`): the mask's values as a
|
||||
/// grayscale clip, inside the selection or the whole canvas — an untouched mask copies at its
|
||||
/// default value, so a plain Reveal All / Hide All mask transfers too. Paste then writes into
|
||||
/// another layer's mask (targeted, #1035), in this or another document.
|
||||
fn copy_mask(s: &mut Session) -> Result<Value> {
|
||||
fn copy_mask(s: &mut Session, filter: bool) -> Result<Value> {
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?;
|
||||
let canvas = d.doc.bounds();
|
||||
let id = d.active_layer.ok_or_else(|| EngineError::Other("no active layer".into()))?;
|
||||
let mask = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?.mask.as_ref().ok_or_else(|| EngineError::Other("the active layer has no layer mask".into()))?;
|
||||
let layer = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?;
|
||||
let mask = crate::mask_view_cmds::viewed_mask(layer, filter)
|
||||
.ok_or_else(|| EngineError::Other(if filter { "the active layer has no filter mask" } else { "the active layer has no layer mask" }.into()))?;
|
||||
let sel = d.doc.selection.as_ref();
|
||||
let area = sel.map_or(canvas, |m| m.content_bounds().intersect(&canvas));
|
||||
if area.is_empty() {
|
||||
@@ -344,7 +347,7 @@ fn copy_mask(s: &mut Session) -> Result<Value> {
|
||||
let b = bounds;
|
||||
let resolution = Some(d.doc.resolution_dpi);
|
||||
s.clipboard = Some(Clip { surface, bounds: b, layers: None, resolution });
|
||||
Ok(json!({"bounds": [b.x0, b.y0, b.width(), b.height()], "target": "mask"}))
|
||||
Ok(json!({"bounds": [b.x0, b.y0, b.width(), b.height()], "target": if filter { "filterMask" } else { "mask" }}))
|
||||
}
|
||||
|
||||
fn clear_selected(doc: &mut Document, id: LayerId, background: [f32; 4]) -> Result<()> {
|
||||
@@ -975,11 +978,11 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Copy",
|
||||
&["Edit"],
|
||||
Some("Cmd+C"),
|
||||
r##"{"target":"mask"? = the targeted mask, copied as grayscale (#2928)} (a targeted colour or alpha channel copies that channel, as grayscale)"##,
|
||||
r##"{"target":"mask|filterMask"? = the targeted mask, copied as grayscale (#2928)} (a targeted colour or alpha channel copies that channel, as grayscale)"##,
|
||||
can_copy,
|
||||
|s, p| {
|
||||
if crate::commands::is_mask_target(p) {
|
||||
return copy_mask(s);
|
||||
return copy_mask(s, crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::FilterMask);
|
||||
}
|
||||
match crate::channel_clip::copy(s)? {
|
||||
Some(r) => Ok(r),
|
||||
|
||||
@@ -799,7 +799,7 @@ impl Session {
|
||||
let d = self.active_mut()?;
|
||||
d.doc.layer(target)?;
|
||||
let active = d.active_layer.replace(target);
|
||||
let r = (spec.enabled)(self);
|
||||
let r = self.precondition(spec, params);
|
||||
if let Some(d) = self.active_mut() {
|
||||
d.active_layer = active;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,11 @@ fn painting_the_filter_mask_hides_the_filters_there_in_one_undo_step() {
|
||||
let original = flat(&s);
|
||||
convert(&mut s);
|
||||
// Without smart filters there is no filter mask to paint.
|
||||
let before = s.active().unwrap().doc.clone();
|
||||
let steps = s.active().unwrap().history.past_len();
|
||||
assert!(matches!(stroke(&mut s, Some("filterMask")), Err(EngineError::Disabled(..))), "depth {depth}");
|
||||
assert!(std::sync::Arc::ptr_eq(&s.active().unwrap().doc, &before));
|
||||
assert_eq!(s.active().unwrap().history.past_len(), steps);
|
||||
s.execute("filter.blur.gaussianBlur", json!({"radius": 3})).unwrap();
|
||||
let blurred = flat(&s);
|
||||
assert!(active_smart(&s).filter_mask.is_none());
|
||||
@@ -64,7 +68,9 @@ fn a_shown_filter_mask_takes_the_paint() {
|
||||
paint(&mut s);
|
||||
convert(&mut s);
|
||||
s.execute("filter.blur.gaussianBlur", json!({"radius": 3})).unwrap();
|
||||
let before = s.active().unwrap().channel_view.clone();
|
||||
assert!(s.execute("view.layerMask", json!({"mode": "gray", "mask": "filter"})).is_err(), "no filter mask yet");
|
||||
assert_eq!(s.active().unwrap().channel_view, before);
|
||||
stroke(&mut s, Some("filterMask")).unwrap();
|
||||
let v = s.execute("view.layerMask", json!({"mode": "gray", "mask": "filter"})).unwrap();
|
||||
assert_eq!(v["mode"], "gray");
|
||||
@@ -75,5 +81,50 @@ fn a_shown_filter_mask_takes_the_paint() {
|
||||
let d = s.active().unwrap();
|
||||
assert!(matches!(d.doc.layer(d.active_layer.unwrap()).unwrap().content, LayerContent::Smart(_)), "still a smart object");
|
||||
// Bad params are refused, not a panic.
|
||||
let before = s.active().unwrap().channel_view.clone();
|
||||
assert!(s.execute("view.layerMask", json!({"mask": 3})).is_err());
|
||||
assert_eq!(s.active().unwrap().channel_view, before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copying_the_filter_mask_never_copies_the_layer_mask() {
|
||||
for depth in DEPTHS {
|
||||
let mut s = session(depth);
|
||||
paint(&mut s);
|
||||
convert(&mut s);
|
||||
s.execute("filter.blur.gaussianBlur", json!({"radius": 3})).unwrap();
|
||||
stroke(&mut s, Some("filterMask")).unwrap();
|
||||
for layer_mask in [false, true] {
|
||||
if layer_mask {
|
||||
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
|
||||
}
|
||||
let before = s.active().unwrap().doc.clone();
|
||||
let steps = s.active().unwrap().history.past_len();
|
||||
let result = s.execute("edit.copy", json!({"target": "filterMask"})).unwrap();
|
||||
assert_eq!(result["target"], "filterMask");
|
||||
let clip = s.clipboard.as_ref().unwrap();
|
||||
assert_eq!(clip.surface.rgba(8, 20), [0.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(clip.surface.rgba(30, 20), [1.0; 4]);
|
||||
assert!(std::sync::Arc::ptr_eq(&s.active().unwrap().doc, &before));
|
||||
assert_eq!(s.active().unwrap().history.past_len(), steps);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_explicit_layer_checks_filter_mask_eligibility() {
|
||||
let mut s = session(8);
|
||||
paint(&mut s);
|
||||
let id = convert(&mut s);
|
||||
s.execute("filter.blur.gaussianBlur", json!({"radius": 3})).unwrap();
|
||||
s.execute("shape.create", json!({"kind": "rect", "rect": [1, 1, 20, 20], "fill": "#ff0000"})).unwrap();
|
||||
let active = s.active().unwrap().active_layer;
|
||||
s.execute("paint.cloneStamp", json!({"layer": id, "target": "filterMask", "source": [8, 8], "points": [[20, 20]], "size": 4})).unwrap();
|
||||
assert_eq!(s.active().unwrap().active_layer, active);
|
||||
// A shape has no filter mask: rejection must precede recoloring its fill.
|
||||
let before = s.active().unwrap().doc.clone();
|
||||
let steps = s.active().unwrap().history.past_len();
|
||||
assert!(s.execute("edit.fillForeground", json!({"layer": active.unwrap().0, "target": "filterMask"})).is_err());
|
||||
assert!(std::sync::Arc::ptr_eq(&s.active().unwrap().doc, &before));
|
||||
assert_eq!(s.active().unwrap().history.past_len(), steps);
|
||||
}
|
||||
@@ -754,12 +754,8 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
|
||||
app.sync_mask_targets();
|
||||
}
|
||||
if let Some(m) = filter_mask_target {
|
||||
app.ui.filter_mask_target = if m { app.session.active().and_then(|st| st.active_layer).map(|id| id.0) } else { None };
|
||||
if m {
|
||||
app.ui.mask_target = false;
|
||||
app.ui.vector_mask_target = false;
|
||||
}
|
||||
app.sync_mask_targets();
|
||||
let layer = if m { app.session.active().and_then(|st| st.active_layer).map(|id| id.0) } else { None };
|
||||
app.select_filter_mask_target(layer);
|
||||
}
|
||||
// Selection tools' options-bar mode: 0 New, 1 Add, 2 Subtract, 3 Intersect.
|
||||
if let Some(m) = selection_mode {
|
||||
|
||||
@@ -2349,6 +2349,27 @@ impl PhotocraftApp {
|
||||
colors_off && !alpha_on
|
||||
}
|
||||
|
||||
/// Explicitly targeting a filter mask ends any conflicting mask view.
|
||||
pub(crate) fn select_filter_mask_target(&mut self, layer: Option<u64>) {
|
||||
if layer.is_some() {
|
||||
if let Some(st) = self.session.active_mut() {
|
||||
if st.channel_view.layer_mask.is_some_and(|v| !v.filter || Some(v.layer.0) != layer) {
|
||||
st.channel_view.layer_mask = None;
|
||||
}
|
||||
if matches!(
|
||||
st.channel_view.target,
|
||||
photocraft_engine::channel_cmds::ChannelTarget::LayerMask | photocraft_engine::channel_cmds::ChannelTarget::VectorMask
|
||||
) {
|
||||
st.channel_view.target = photocraft_engine::channel_cmds::ChannelTarget::Composite;
|
||||
}
|
||||
}
|
||||
self.ui.mask_target = false;
|
||||
self.ui.vector_mask_target = false;
|
||||
}
|
||||
self.ui.filter_mask_target = layer;
|
||||
self.sync_mask_targets();
|
||||
}
|
||||
|
||||
/// Viewing a layer mask (#196) targets it; a vector-mask target needs a vector mask on the
|
||||
/// active layer (a shape layer's path is its content, not a mask). Targeting a mask or the
|
||||
/// pixels brings back that target's foreground/background pair, as in Photoshop (#2166).
|
||||
|
||||
@@ -2839,10 +2839,7 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
continue;
|
||||
}
|
||||
if id == "ui.filterMaskTarget" {
|
||||
app.ui.filter_mask_target = p.as_u64();
|
||||
app.ui.mask_target = false;
|
||||
app.ui.vector_mask_target = false;
|
||||
app.sync_mask_targets();
|
||||
app.select_filter_mask_target(p.as_u64());
|
||||
continue;
|
||||
}
|
||||
// Double-clicking a mask thumbnail opens its settings: show Properties.
|
||||
|
||||
@@ -457,6 +457,35 @@ mod tests {
|
||||
assert_eq!(app.ui.filter_mask_target, None, "a layer mask target replaces it");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selecting_the_filter_mask_replaces_a_layer_mask_view() {
|
||||
let mut app = PhotocraftApp::new(Session::new(), Services::default());
|
||||
app.run("file.new", json!({"width": 32, "height": 32})).unwrap();
|
||||
app.run("layer.new.layer", json!({})).unwrap();
|
||||
app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();
|
||||
app.run("layer.smartObjects.convertToSmartObject", json!({})).unwrap();
|
||||
app.run("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap();
|
||||
app.run("layer.layerMask.revealAll", json!({})).unwrap();
|
||||
app.run("view.layerMask", json!({"mode": "gray"})).unwrap();
|
||||
app.sync_mask_targets();
|
||||
assert!(app.layer_mask_targeted());
|
||||
let id = app.session.active().unwrap().active_layer.unwrap();
|
||||
app.select_filter_mask_target(Some(id.0));
|
||||
app.sync_views();
|
||||
assert!(app.filter_mask_targeted());
|
||||
assert!(!app.layer_mask_targeted());
|
||||
assert!(photocraft_engine::mask_view_cmds::current(app.session.active().unwrap()).is_none());
|
||||
app.run("paint.stroke", json!({"points": [[8, 8]], "size": 6, "hardness": 1, "color": "#000000"})).unwrap();
|
||||
let st = app.session.active().unwrap();
|
||||
let layer = st.doc.layer(id).unwrap();
|
||||
assert_eq!(layer.mask.as_ref().unwrap().value(8, 8), 1.0);
|
||||
let LayerContent::Smart(sm) = &layer.content else { panic!("smart object") };
|
||||
assert_eq!(sm.filter_mask.as_ref().unwrap().value(8, 8), 0.0);
|
||||
app.run("view.layerMask", json!({"mode": "gray", "mask": "filter"})).unwrap();
|
||||
app.select_filter_mask_target(Some(id.0));
|
||||
assert!(photocraft_engine::mask_view_cmds::current(app.session.active().unwrap()).unwrap().filter, "a matching filter-mask view stays open");
|
||||
}
|
||||
|
||||
/// A pixel layer, a shape layer and a smart object, in that order.
|
||||
fn layers() -> [Layer; 3] {
|
||||
let mut s = photocraft_engine::Session::new();
|
||||
|
||||
Reference in new issue
Block a user