Merge #16618 into the merge queue batch

Path Selection Delete removes paths, not the layer; Properties eyedropper picks
This commit is contained in:
David Mann committed 2026-11-24 12:32:14 +01:00
commit edd366190f
8 files changed
+215 -3

No files matched your search

+1
View File
@@ -1069,6 +1069,7 @@ fn documents_filesystem_path_params(id: &str, params: &str) -> bool {
| "path.addAnchor"
| "path.deleteAnchor"
| "path.deleteSegment"
| "path.deleteComponents"
| "path.mergeShapeComponents"
| "path.setOperation"
| "path.clippingPath.set"
@@ -239,6 +239,43 @@ fn delete_anchor(s: &mut Session, p: &Value) -> Result<Value> {
Ok(r)
}
/// Removes selected components, including any subpaths joined to them, in one History step.
fn delete_components(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "path.deleteComponents";
let heads = p.get("subpaths").and_then(Value::as_array).filter(|a| !a.is_empty()).ok_or_else(|| bad(CMD, "`subpaths` must list component starts"))?;
let name = p.get("name").and_then(Value::as_str).ok_or_else(|| bad(CMD, "`name` must identify a saved path or layer"))?;
if crate::vector_cmds::is_work(Some(name)) || p.get("typePath").and_then(Value::as_bool) == Some(true) {
return Err(bad(CMD, "only saved paths, shape paths and vector masks are supported"));
}
if name == "layer" {
let id = layer_id(s, p)?;
let st = s.active().ok_or_else(|| bad(CMD, "no active document"))?;
let layer = st.doc.layer(id).ok_or_else(|| bad(CMD, "no such layer"))?;
let locks = st.doc.effective_locks(id);
if locks.all || locks.pixels || (!matches!(layer.content, LayerContent::Shape(_)) && layer.vector_mask.is_none()) {
return Err(bad(CMD, "layer has no editable shape path or vector mask"));
}
}
edit_path(s, CMD, p, "Delete Selection", |path| {
let components = path.components();
if heads.len() > components.len() {
return Err("more indices than path components".into());
}
let mut remove = vec![false; path.subpaths.len()];
for value in heads {
let head = value.as_u64().and_then(|n| usize::try_from(n).ok()).ok_or("each component start must be a subpath index")?;
let range = components.binary_search_by_key(&head, |r| r.start).ok().and_then(|i| components.get(i)).ok_or("no component at that subpath index")?;
remove.get_mut(range.clone()).ok_or("component is out of bounds")?.fill(true);
}
if name == "layer" && remove.iter().all(|v| *v) {
return Err("cannot remove the last component of a shape path or vector mask".into());
}
let mut keep = remove.into_iter().map(|v| !v);
path.subpaths.retain(|_| keep.next().unwrap_or(false));
Ok(())
})
}
/// ⌫ / Delete on a segment selected with Direct Selection: the segment goes, its anchors stay.
/// Photoshop names the step Delete Anchor Point (#13203).
fn delete_segment(s: &mut Session, p: &Value) -> Result<Value> {
@@ -367,6 +404,16 @@ pub fn specs() -> Vec<CommandSpec> {
r#""tolerance":px=0.1 (bakes the components' path operations into plain combined outlines, traced from the exact coverage)"#.into(),
merge_components,
),
CommandSpec {
id: "path.deleteComponents",
label: "Delete Selection",
menu: &[],
shortcut: None,
params: r#"{"name":str|"layer" (a saved path name or a shape path or vector mask),"layer":id? (with "layer"),"subpaths":[i,…] (the first subpath of each component to delete)} → {name,path}"#,
enabled: has_doc,
run: delete_components,
journal: true,
},
spec(
"path.setOperation",
"Path Operation",
@@ -383,6 +430,31 @@ mod tests {
use crate::Session;
#[test]
fn deleting_components_rejects_invalid_indices_without_changing_history() {
let mut s = session();
let path = json!({"subpaths": [
{"closed": true, "knots": [[1,1], [30,1], [30,30], [1,30]]},
{"closed": true, "op": "join", "knots": [[5,5], [5,10], [10,10], [10,5]]},
{"closed": true, "knots": [[31,31], [35,31], [35,35], [31,35]]}
]});
s.execute("path.set", json!({"name": "Saved", "path": path})).unwrap();
let st = s.active().unwrap();
let (before, steps) = (st.doc.paths.clone(), st.history.past_len());
for p in [
json!({}),
json!({"name":"Saved","subpaths":[]}),
json!({"name":"Saved","subpaths":[0,1]}),
json!({"name":"Saved","subpaths":[-1]}),
json!({"name":"Saved","subpaths":[99999]}),
] {
assert!(s.execute("path.deleteComponents", p.clone()).is_err(), "{p}");
let st = s.active().unwrap();
assert_eq!(st.doc.paths, before, "{p}");
assert_eq!(st.history.past_len(), steps, "{p}");
}
}
fn session() -> Session {
let mut s = crate::session();
s.execute("file.new", json!({"width": 120, "height": 100, "background": "white"})).unwrap();
@@ -287,6 +287,7 @@ path.addAnchor
path.deleteAnchor
path.deleteSegment
path.mergeShapeComponents
path.deleteComponents
path.setOperation
select.quick
select.object
@@ -308,3 +308,7 @@ mod tests;
#[cfg(test)]
#[path = "path_selection_enter_escape_tests.rs"]
mod path_selection_enter_escape_tests;
#[cfg(test)]
#[path = "path_selection_delete_key_tests.rs"]
mod path_selection_delete_key_tests;
@@ -0,0 +1,92 @@
//! #13269: with Path Selection, Delete removes the selected components of a shape layer, a saved
//! path or a vector mask (or the targeted saved path when none is selected), never the layer.
use egui::{Key, Modifiers};
use serde_json::json;
use crate::PhotocraftApp;
use crate::direct_select::PathRef;
use crate::state::Tool;
const SQUARE: [[i32; 2]; 4] = [[100, 300], [300, 300], [300, 500], [100, 500]];
const TRIANGLE: [[i32; 2]; 3] = [[400, 300], [600, 300], [500, 500]];
fn app() -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap();
app
}
fn two_components() -> serde_json::Value {
json!({"subpaths": [{"closed": true, "knots": SQUARE}, {"closed": true, "knots": TRIANGLE}]})
}
fn path_selection(app: &mut PhotocraftApp) {
app.sync_views();
app.ui.tool = Tool::PathSelection;
}
fn layers(app: &PhotocraftApp) -> usize {
app.session.active().unwrap().doc.layers.len()
}
fn subpaths(path: &photocraft_doc::Path) -> usize {
path.subpaths.len()
}
fn press_delete(app: &mut PhotocraftApp) {
crate::canvas::testing::key(app, Key::Delete, Modifiers::NONE);
}
#[test]
fn delete_removes_the_selected_component_of_a_saved_path() {
let mut app = app();
app.run("path.set", json!({"name": "Path 1", "path": two_components()})).unwrap();
path_selection(&mut app);
app.ui.selected_path = Some("Path 1".into());
crate::path_components::press(&mut app, [300.0, 300.0], false);
assert_eq!(crate::path_components::selected(&app).map(|s| s.0), Some(PathRef::Saved(0)));
let n = layers(&app);
press_delete(&mut app);
let st = app.session.active().unwrap();
assert_eq!(subpaths(&st.doc.paths[0].path), 1, "the triangle stays");
assert_eq!(layers(&app), n, "the layer stays");
assert!(crate::path_components::selected(&app).is_none(), "the stale selection drops");
// With nothing selected, Delete removes the targeted saved path.
press_delete(&mut app);
assert!(app.session.active().unwrap().doc.paths.is_empty());
assert_eq!(layers(&app), n);
}
#[test]
fn delete_removes_the_selected_component_of_a_shape_layer() {
let mut app = app();
let id = app.run("shape.create", json!({"kind": "rect", "rect": [100, 300, 200, 200], "fill": "#ff0000"})).unwrap()["layer"].as_u64().unwrap();
app.run("shape.create", json!({"kind": "ellipse", "rect": [400, 300, 200, 200], "addTo": id, "op": "subtract"})).unwrap();
path_selection(&mut app);
let n = layers(&app);
crate::path_components::press(&mut app, [400.0, 400.0], false);
assert!(matches!(crate::path_components::selected(&app), Some((PathRef::Layer(l), ..)) if l == id), "the ellipse is selected");
press_delete(&mut app);
assert_eq!(layers(&app), n, "the layer stays");
let st = app.session.active().unwrap();
let photocraft_doc::LayerContent::Shape(sh) = &st.doc.layer(photocraft_doc::LayerId(id)).unwrap().content else { panic!("shape") };
assert_eq!(subpaths(&sh.path), 1, "only the ellipse went");
}
#[test]
fn delete_removes_the_selected_component_of_a_vector_mask() {
let mut app = app();
let id = app.session.active().unwrap().active_layer.unwrap().0;
app.run("layer.vectorMask.add", json!({"layer": id, "path": two_components()})).unwrap();
app.ui.vector_mask_target = true;
path_selection(&mut app);
let n = layers(&app);
crate::path_components::press(&mut app, [300.0, 300.0], false);
assert!(matches!(crate::path_components::selected(&app), Some((PathRef::Layer(l), ..)) if l == id), "the square is selected");
press_delete(&mut app);
assert_eq!(layers(&app), n, "the layer stays");
let st = app.session.active().unwrap();
let mask = st.doc.layer(photocraft_doc::LayerId(id)).unwrap().vector_mask.as_ref().expect("the mask stays");
assert_eq!(subpaths(&mask.path), 1, "the triangle stays");
}
@@ -463,6 +463,8 @@ pub fn shape_properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: photocra
let was_linked = app.ui.arrow_default == Some(target);
let mut linked = was_linked;
let recent_colors: Vec<Srgba> = crate::paint_picker::recent_colors(&app.session).into_iter().map(|c| Srgba::new(c[0], c[1], c[2], 1.0)).collect();
let sampled =
[fw::ShapePaint::Fill, fw::ShapePaint::Stroke].into_iter().find_map(|p| crate::screen_picker::take(app, ("shape-properties", p)).map(|c| (p, c)));
let groups = crate::shape_gradient_ui::ShapeGradients::groups_of(&app.session.presets().gradients, fg, bg);
let gradients = crate::shape_gradient_ui::ShapeGradients { groups, current: &app.session.presets().gradient, recent: &mut app.ui.recent_gradients, fg, bg };
let current = [sh.fill.clone(), sh.stroke.as_ref().map(|s| s.paint.clone())];
@@ -476,12 +478,13 @@ pub fn shape_properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: photocra
);
let output = ShapePanel::new(label(tl!("Shape properties")), &shown, &appearance, ppi)
.sections(true, sh.live.is_some())
.sampled(sampled)
.stroke_open(Bind::bound(&mut app.ui.shape_stroke_open))
.chips(|which, chip, chip_ui| crate::shape_paint_ui::decorate_chip(chip, which, &mut library, chip_ui))
.fallbacks(Srgba::new(fg[0], fg[1], fg[2], fg[3]), Srgba::new(bg[0], bg[1], bg[2], bg[3]))
.arrow_default(Bind::bound(&mut linked))
.show(ui);
let fw::ShapePanelOutput { events, pick_color, out_of_range, .. } = output;
let fw::ShapePanelOutput { events, pick_color, screen_pick, out_of_range, .. } = output;
let paint_gradients = [library.gradient(fw::ShapePaint::Fill), library.gradient(fw::ShapePaint::Stroke)];
let mut applied = false;
@@ -534,6 +537,9 @@ pub fn shape_properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: photocra
};
let _ = crate::shape_fill_ui::open_paint(app, which, Some(id), false, fg);
}
if let Some((which, gesture)) = screen_pick {
crate::screen_picker::hold(app, ui.ctx(), ("shape-properties", which), gesture);
}
// A Concavity typed past -50..50 % raises the reference's alert (#13258).
if let Some(alert) = out_of_range {
crate::option_cells::range_alert(app, ui, &alert);
@@ -707,3 +707,18 @@ fn path_mode_radius_leaves_the_active_shape_layer_alone() {
assert_eq!(radii(h.state()), [10.0; 4], "the shape layer keeps its radius");
assert_eq!(history(h.state()), before, "no edit");
}
/// The Properties panel's Fill popover eyedropper samples the screen and applies the colour to the layer's fill.
#[test]
fn properties_fill_eyedropper_applies_the_sampled_colour() {
let mut app = app("rgb", 8);
app.run("shape.create", json!({"rect": [20,20,80,60], "fill": "#2244aa", "stroke": null})).unwrap();
app.services.screen_capture = Some(std::sync::Arc::new(|| Box::new(craft::color_ui::ui::StillCapture::color(Srgba::rgb(1.0, 136.0 / 255.0, 0.0)))));
let mut h = harness(app, true);
chip(&h, "Fill").click();
h.run_steps(3);
h.get_by_label("Sample color from the screen").click();
h.run_steps(6);
let [r, g, b, _] = shape(h.state()).fill.as_ref().and_then(color_of).expect("a solid fill");
assert!((r - 1.0).abs() < 0.01 && (g - 136.0 / 255.0).abs() < 0.01 && b < 0.01, "the sampled orange is the fill, got {r} {g} {b}");
}
+23 -2
View File
@@ -230,12 +230,33 @@ pub(crate) fn delete_key_deletes_work_path(app: &PhotocraftApp) -> bool {
/// anchor, the segment goes on its own: its anchors stay and the subpath opens there, as in
/// Photoshop (#13203). With nothing selected, the path Direct Selection targets is deleted: a
/// saved path, the work path or a vector mask. The work path with the Pen, Freeform Pen,
/// Curvature Pen or Path Selection is [`delete_key_deletes_work_path`]. A shape layer's path
/// wasn't measured: the key keeps deleting the layer.
/// Curvature Pen or Path Selection is [`delete_key_deletes_work_path`]. With Path Selection on a
/// shape layer, a saved path or a vector mask, the selected components go
/// (`path.deleteComponents`), or with none selected the targeted saved path (#13269); the layer
/// stays. Direct Selection's shape layer path isn't measured: the key keeps deleting the layer.
pub(crate) fn delete_key_edits_path(app: &PhotocraftApp) -> Option<(&'static str, Value)> {
if delete_key_deletes_work_path(app) {
return Some(("path.delete", json!({"name": "work"})));
}
if app.ui.tool == Tool::PathSelection
&& app.ui.pen.is_none()
&& app.ui.transform.is_none()
&& app.ui.direct_selection.editing.is_none()
&& app.ui.view.shows(app.ui.view.show.target_path)
{
return match crate::path_components::selected(app) {
Some((target @ (PathRef::Layer(_) | PathRef::Saved(_)), _, heads)) => {
let mut params = target.params(app);
params["subpaths"] = json!(heads);
Some(("path.deleteComponents", params))
}
Some(_) => None,
None => {
let (i, _) = selected_saved_path_index(app)?;
Some(("path.delete", PathRef::Saved(i).params(app)))
}
};
}
let ds = &app.ui.direct_selection;
let usable = app.ui.tool == Tool::DirectSelection
&& app.ui.pen.is_none()