Let one mask be built from more than one selection, and paint into it

A mask the model draws arrives approximately right — stopping inside a
shoulder, leaking into the hair — and FR-DEV-3's edge controls move the
*whole* boundary, so no value of feather or dilation fixes two errors that
go opposite ways. What fixes them is a second selection joined to the first,
and a layer that held exactly one source had nowhere to put one. The brush
the core has had all along was reachable from no control in the application.

A layer is now an ordered list of parts. Each names a source and how it
joins the mask before it — added to it, or taken out of it — and carries its
own edge treatment, because a model's soft coverage and a stroke painted
where it stopped short do not want the same feather. Invert and opacity stay
on the layer, where the composed shader already reads them.

The sidecar grows `[part]` blocks and nothing else. A layer of one part
writes exactly the bytes it always did; a mask block with no part blocks
after it reads back as one part; and a stroke, a join or a source this build
cannot read costs that part rather than the layer. So every sidecar in every
library still parses to the edit it always was.

On the device the parts fold into the layer's one slice, so eight layers
still cost eight channels: union is a `max` blend and subtraction is the
erase blend the brush already used. A part is drawn into a scratch texture
before it is joined, and that is not incidental — an erase stroke means a
hole in *that part*, not a hole in the mask, and drawn straight onto the
accumulator it would punch through the subject underneath. A layer of one
part skips all of it and takes the path it always took.

In the interface: a part list under the selected layer with a chip saying
which way each joins, Add and Subtract beside it, a Select/Paint/Erase strip
with the brush's size, hardness and flow, and a drag on the photograph that
paints. Pressing Paint on a mask that cannot hold a stroke joins a part that
can, rather than explaining that a subject is not a brush. A whole stroke is
one step in the history.

The edge controls now shape the part that is selected rather than the layer,
which is the one behaviour change to an existing control: with a correction
selected, the feather slider softens the correction and leaves the model's
mask alone.
This commit is contained in:
2026-09-07 20:00:40 +02:00
parent 9ede23073d
commit df741a8a49
20 changed files with 2998 additions and 654 deletions
+265 -1
View File
@@ -30,7 +30,9 @@ use slint::{ComponentHandle as _, Model as _, ModelRc, VecModel};
use crate::develop::{Abandon, DevelopSession, RefinedInstance, Segmented, SessionId};
use crate::segmentation;
use crate::{sync_rows, AppWindow, CategoryRow, GradientHandle, MaskRow, ParamRow, SubjectRow};
use crate::{
sync_rows, AppWindow, CategoryRow, GradientHandle, MaskRow, ParamRow, PartRow, SubjectRow,
};
/// What the adjust panel's heading says when the controls are global.
///
@@ -238,6 +240,31 @@ pub(crate) fn sync(window: &AppWindow, session: &Rc<RefCell<Option<DevelopSessio
.collect();
window.set_category_rows(ModelRc::new(VecModel::from(categories)));
// TRACES: FR-DEV-19a
// Only the selected layer's parts, and only when exactly one is selected:
// a stroke has one target, and so does the edge the sliders shape.
let parts: Vec<PartRow> = match s.active_masks() {
[id] => s
.mask_parts(id)
.into_iter()
.enumerate()
.map(|(i, (part_id, label, join))| PartRow {
id: part_id.into(),
label: label.into(),
join: join as i32,
selected: i == s.active_part(),
base: i == 0,
})
.collect(),
_ => Vec::new(),
};
window.set_mask_parts(ModelRc::new(VecModel::from(parts)));
let (radius, hardness, flow) = s.brush();
window.set_brush_radius(radius);
window.set_brush_hardness(hardness);
window.set_brush_flow(flow);
window.set_segmented(s.has_segmentation());
window.set_adjust_scope(scope_label(s).into());
sync_handles(window, s);
@@ -723,6 +750,141 @@ pub(crate) fn wire(
}
// --- adding layers ------------------------------------------------------
// TRACES: FR-DEV-19b
// Painting. Three handlers for one gesture, and the middle one runs per
// pointer report — so it does the least it can: map the point, record it,
// redraw. No sync, because nothing the panel shows changes while a finger
// is down, and rebuilding eight rows a frame is how a brush starts to lag.
{
let weak = window.as_weak();
let session = session.clone();
let redraw = redraw.clone();
window.on_paint_began(move |x, y| {
let Some(w) = weak.upgrade() else { return };
let erase = w.get_mask_tool() == 2;
if let Some(s) = session.borrow_mut().as_mut() {
s.begin_mask_stroke(x, y, erase);
}
redraw(&w);
});
}
{
let weak = window.as_weak();
let session = session.clone();
let redraw = redraw.clone();
window.on_paint_moved(move |x, y| {
let Some(w) = weak.upgrade() else { return };
if let Some(s) = session.borrow_mut().as_mut() {
s.extend_mask_stroke(x, y);
}
redraw(&w);
});
}
{
let weak = window.as_weak();
let session = session.clone();
let redraw = redraw.clone();
let rows = rows.clone();
window.on_paint_ended(move || {
let Some(w) = weak.upgrade() else { return };
if let Some(s) = session.borrow_mut().as_mut() {
s.end_mask_stroke();
}
// Now the panel is worth rebuilding: the gesture may have joined a
// part to the mask, and the history has a step in it.
sync(&w, &session);
sync_rows(&w, &rows, &session);
redraw(&w);
});
}
{
let weak = window.as_weak();
let session = session.clone();
window.on_brush_changed(move |radius, hardness, flow| {
let Some(w) = weak.upgrade() else { return };
if let Some(s) = session.borrow_mut().as_mut() {
s.set_brush(radius, hardness, flow);
}
sync(&w, &session);
});
}
{
let weak = window.as_weak();
let session = session.clone();
window.on_mask_tool_picked(move |_tool| {
// The tool itself lives in the interface — it arms a gesture and
// changes no pixel — so nothing is set here. What this does is
// re-sync, because arming the brush is what makes the parts of a
// mask worth showing.
let Some(w) = weak.upgrade() else { return };
sync(&w, &session);
});
}
// TRACES: FR-DEV-19a
{
let weak = window.as_weak();
let session = session.clone();
let redraw = redraw.clone();
let rows = rows.clone();
window.on_mask_part_selected(move |_id, index| {
let Some(w) = weak.upgrade() else { return };
if let Some(s) = session.borrow_mut().as_mut() {
s.set_active_part(index.max(0) as usize);
}
sync(&w, &session);
sync_rows(&w, &rows, &session);
redraw(&w);
});
}
{
let weak = window.as_weak();
let session = session.clone();
let redraw = redraw.clone();
let rows = rows.clone();
window.on_mask_part_added(move |id, join| {
let Some(w) = weak.upgrade() else { return };
if let Some(s) = session.borrow_mut().as_mut() {
s.add_mask_part(&id, join.max(0) as usize);
}
// Armed, because adding an empty painted part and leaving the
// pointer in "select" would look like the button did nothing —
// the part covers no pixels until something is painted into it.
if w.get_mask_tool() == 0 {
w.set_mask_tool(1);
}
sync(&w, &session);
sync_rows(&w, &rows, &session);
redraw(&w);
});
}
{
let weak = window.as_weak();
let session = session.clone();
let redraw = redraw.clone();
let rows = rows.clone();
window.on_mask_part_removed(move |id, index| {
let Some(w) = weak.upgrade() else { return };
if let Some(s) = session.borrow_mut().as_mut() {
s.remove_mask_part(&id, index.max(0) as usize);
}
sync(&w, &session);
sync_rows(&w, &rows, &session);
redraw(&w);
});
}
{
let weak = window.as_weak();
let session = session.clone();
let redraw = redraw.clone();
window.on_mask_part_join_picked(move |id, index, join| {
let Some(w) = weak.upgrade() else { return };
if let Some(s) = session.borrow_mut().as_mut() {
s.set_mask_part_join(&id, index.max(0) as usize, join.max(0) as usize);
}
sync(&w, &session);
redraw(&w);
});
}
{
let weak = window.as_weak();
let session = session.clone();
@@ -948,6 +1110,108 @@ mod tests {
DevelopSession::open_rgb(&ctx, &rgba, 32, 32, dr_types::Orientation::NORMAL).ok()
}
/// TRACES: FR-DEV-19b
/// Pressing Paint on a mask the model made must not answer with a
/// refusal about a subject not being a brush. The correction the
/// photographer is about to make *is* a new part, so it is made.
#[test]
fn painting_on_a_mask_that_cannot_hold_a_stroke_joins_a_part_that_can() {
let Some(mut s) = session() else {
eprintln!("no adapter; skipping");
return;
};
let id = s.add_gradient_mask(true).expect("radial");
s.set_active_mask(Some(&id));
assert_eq!(s.mask_parts(&id).len(), 1, "one selection to begin with");
assert!(s.begin_mask_stroke(0.5, 0.5, false), "the stroke started");
s.extend_mask_stroke(0.55, 0.5);
s.end_mask_stroke();
let parts = s.mask_parts(&id);
assert_eq!(parts.len(), 2, "a painted part was joined");
assert_eq!(parts[1].1, "brush");
assert_eq!(s.active_part(), 1, "and the tools point at it");
}
/// A stroke is one decision. Recording per pointer report would make
/// taking a mark back cost as many presses as making it did.
#[test]
fn a_whole_stroke_is_one_step_in_the_history() {
let Some(mut s) = session() else {
eprintln!("no adapter; skipping");
return;
};
let id = s.add_gradient_mask(true).expect("radial");
s.set_active_mask(Some(&id));
let before = s.history_rows().len();
assert!(s.begin_mask_stroke(0.4, 0.4, false));
for i in 0..12 {
s.extend_mask_stroke(0.4 + i as f32 * 0.02, 0.5);
}
s.end_mask_stroke();
// Two: joining the part is its own step, and the stroke is the other.
assert_eq!(
s.history_rows().len(),
before + 2,
"a stroke recorded more than one step"
);
}
/// A gesture the window cancels leaves nothing behind — including in the
/// history, because as far as the photographer is concerned nothing
/// happened.
#[test]
fn a_cancelled_stroke_leaves_no_mark() {
let Some(mut s) = session() else {
eprintln!("no adapter; skipping");
return;
};
let id = s.add_gradient_mask(true).expect("radial");
s.set_active_mask(Some(&id));
assert!(s.begin_mask_stroke(0.4, 0.4, false));
s.extend_mask_stroke(0.5, 0.5);
let steps = s.history_rows().len();
s.cancel_mask_stroke();
assert_eq!(s.history_rows().len(), steps, "nothing was recorded");
assert!(
s.masks()
.get(&id)
.expect("layer")
.part(1)
.expect("part")
.strokes()
.is_empty(),
"and the abandoned stroke is not in the mask"
);
}
/// TRACES: FR-DEV-19a
/// The edge controls point at the part that is selected, so that dragging
/// the feather while a correction is selected does not silently soften the
/// model's mask underneath it.
#[test]
fn the_edge_controls_shape_the_selected_part() {
let Some(mut s) = session() else {
eprintln!("no adapter; skipping");
return;
};
let id = s.add_gradient_mask(true).expect("radial");
s.set_active_mask(Some(&id));
s.set_mask_feather(&id, 0.03);
let index = s.add_mask_part(&id, 0).expect("a part was joined");
assert_eq!(s.active_part(), index);
s.set_mask_feather(&id, 0.0);
assert_eq!(s.mask_feather(&id), 0.0, "the selected part was shaped");
s.set_active_part(0);
assert_eq!(s.mask_feather(&id), 0.03, "and the base was left alone");
}
/// TRACES: FR-DEV-3 | FR-UI-1
/// The fault this pass exists for, stated as a test.
///