feat(ui): Path mode for the Shape tools (part of #9453)

The Shape tools' mode dropdown offered only Shape, so a rectangle or an
ellipse could only become a shape layer. Photoshop's Shape tools have
Shape, Path and Pixels modes; Path adds the outline to the Work Path
and makes no layer.

The dropdown now offers Shape and Path (a tool option of its own,
`shape_mode`, Shape by default; the Pens keep their own mode). In Path
mode a drag adds its outline, custom shapes included, to the work path
as one more subpath, the preview shows the outline only, and the
fill/stroke options are hidden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-11-02 11:21:50 -06:00
1 parent 289dd0a874
commit 638697199c
5 files changed
+102 -43

No files matched your search

+4
View File
@@ -96,6 +96,9 @@ pub struct ToolOptions {
/// draws it as "combine"), "combine", "subtract", "intersect" or "exclude".
#[serde(default = "default_pen_path_op")]
pub pen_path_op: String,
/// Shape tools: "shape" (a shape layer) or "path" (the work path), as Photoshop's tool mode
/// (#9453). Shape by default, where the Pen's own mode defaults to Path.
pub shape_mode: String,
/// Shape tools: fill with the foreground colour, stroke width (0 = none), rectangle corner
/// radius, polygon sides, line weight.
pub shape_fill: bool,
@@ -421,6 +424,7 @@ impl Default for ToolOptions {
enhance_edge: false,
vector_mode: "path".into(),
pen_path_op: default_pen_path_op(),
shape_mode: "shape".into(),
shape_fill: true,
stroke_width: 0.0,
shape_stroke: Default::default(),
+2 -2
View File
@@ -212,7 +212,7 @@ pub fn option_keys(tool: Tool) -> &'static [&'static str] {
Tool::CurvaturePen => reset!(vector_mode, pen_path_op),
Tool::FreeformPen => reset!(vector_mode, pen_path_op, curve_fit),
Tool::Line => reset!(
vector_mode,
shape_mode,
shape_fill,
stroke_width,
shape_stroke,
@@ -228,7 +228,7 @@ pub fn option_keys(tool: Tool) -> &'static [&'static str] {
arrow_concavity,
),
Tool::Rectangle | Tool::EllipseShape | Tool::Triangle | Tool::Polygon | Tool::CustomShape => {
reset!(vector_mode, shape_fill, stroke_width, shape_stroke, shape_fill_gradient, shape_stroke_gradient, corner_radius, polygon_sides, line_weight)
reset!(shape_mode, shape_fill, stroke_width, shape_stroke, shape_fill_gradient, shape_stroke_gradient, corner_radius, polygon_sides, line_weight)
}
Tool::ColorSampler
| Tool::Ruler
+38 -30
View File
@@ -111,41 +111,49 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
}
return true;
}
let mut mode = "shape".to_string();
crate::widgets::dropdown(ui, "shape-mode", tl!("Mode"), &mut mode, &[("shape".to_string(), tl!("Shape"))], 80.0);
// Shape draws a shape layer, Path adds the outline to the work path (#9453).
let opts = [("shape".to_string(), tl!("Shape")), ("path".to_string(), tl!("Path"))];
crate::widgets::dropdown(ui, "shape-mode", tl!("Mode"), &mut app.ui.tool_options.shape_mode, &opts, 80.0);
crate::widgets::vline(ui, 22.0);
crate::shape_options::options_bar(app, ui, tool);
// Photoshop-style arrowhead settings at the right of Line options.
if tool == Tool::Line {
crate::widgets::vline(ui, 22.0);
ui.menu_image_button(crate::icons::image("settings", 18.0, t.icon), |ui| {
let o = &mut app.ui.tool_options;
ui.set_min_width(170.0);
ui.label(tl!("Arrowheads"));
ui.separator();
ui.horizontal(|ui| {
ui.checkbox(&mut o.arrow_start, tl!("Start"));
ui.checkbox(&mut o.arrow_end, tl!("End"));
});
ui.horizontal(|ui| {
ui.label(tl!("Width"));
ui.add(egui::DragValue::new(&mut o.arrow_width).range(1.0..=4096.0).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label(tl!("Length"));
ui.add(egui::DragValue::new(&mut o.arrow_length).range(1.0..=4096.0).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label(tl!("Concavity"));
ui.add(egui::DragValue::new(&mut o.arrow_concavity).range(0.0..=100.0).suffix(" %"));
});
})
.response
.on_hover_text(tl!("Arrowhead options"));
if !draws_path(&app.ui.tool_options) {
crate::shape_options::options_bar(app, ui, tool);
// Photoshop-style arrowhead settings at the right of Line options.
if tool == Tool::Line {
crate::widgets::vline(ui, 22.0);
ui.menu_image_button(crate::icons::image("settings", 18.0, t.icon), |ui| {
let o = &mut app.ui.tool_options;
ui.set_min_width(170.0);
ui.label(tl!("Arrowheads"));
ui.separator();
ui.horizontal(|ui| {
ui.checkbox(&mut o.arrow_start, tl!("Start"));
ui.checkbox(&mut o.arrow_end, tl!("End"));
});
ui.horizontal(|ui| {
ui.label(tl!("Width"));
ui.add(egui::DragValue::new(&mut o.arrow_width).range(1.0..=4096.0).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label(tl!("Length"));
ui.add(egui::DragValue::new(&mut o.arrow_length).range(1.0..=4096.0).suffix(" px"));
});
ui.horizontal(|ui| {
ui.label(tl!("Concavity"));
ui.add(egui::DragValue::new(&mut o.arrow_concavity).range(0.0..=100.0).suffix(" %"));
});
})
.response
.on_hover_text(tl!("Arrowhead options"));
}
}
true
}
/// Whether the Shape tools are in Path mode: a drag adds its outline to the work path.
pub(crate) fn draws_path(o: &crate::state::ToolOptions) -> bool {
o.shape_mode == "path"
}
/// Whether the active layer supplies the selected-shape options.
pub(super) fn selected_shape(session: &photocraft_engine::Session) -> Option<photocraft_doc::LayerId> {
let st = session.active()?;
+31 -11
View File
@@ -1,4 +1,4 @@
use super::options::solid_paint;
use super::options::{draws_path, solid_paint};
use super::overlay::path_lines;
use super::{Affine, Color32, PhotocraftApp, Tokens, Tool, ToolOptions, Value, ViewXform, json, paint_param};
@@ -100,6 +100,19 @@ pub fn shape_bounds(o: &ToolOptions, tool: Tool, start: [f64; 2], end: [f64; 2],
/// Finish a Shape-tool drag: ⇧ constrains proportions, ⌥ draws from the centre.
pub fn finish_shape(app: &mut PhotocraftApp, tool: Tool, start: [f64; 2], end: [f64; 2], mods: egui::Modifiers) {
if draws_path(&app.ui.tool_options) {
// Path mode: the outline joins the work path (one more subpath), and no layer is made.
let Some(path) = shape_outline(app, tool, start, end, mods) else { return };
let r = app.run("path.set", json!({"name": "work", "op": "combine", "path": photocraft_engine::vector_cmds::path_json(&path)}));
match r {
Ok(_) => app.ui.selected_path = Some("work".into()),
Err(e) => {
app.ui.status = e;
app.ui.status_error = true;
}
}
return;
}
let Some(mut p) = shape_geometry(&app.ui.tool_options, tool, start, end, mods) else { return };
if tool == Tool::CustomShape {
// ⇧ keeps the shape's proportions (the rect is already squared).
@@ -127,22 +140,29 @@ pub fn create_shape(app: &mut PhotocraftApp, mut geometry: Value) -> Result<Valu
app.run("shape.create", geometry)
}
/// The preview and command both use the full float tool colours, including transparent paint.
pub(super) fn shape_preview(app: &PhotocraftApp, tool: Tool, start: [f64; 2], end: [f64; 2], mods: egui::Modifiers) -> Option<photocraft_doc::ShapeLayer> {
let o = &app.ui.tool_options;
let geometry = shape_geometry(o, tool, start, end, mods)?;
let path = if tool == Tool::CustomShape {
/// The outline a Shape-tool drag draws, as a path (a custom shape fitted to the drag's box).
fn shape_outline(app: &PhotocraftApp, tool: Tool, start: [f64; 2], end: [f64; 2], mods: egui::Modifiers) -> Option<photocraft_doc::vector::Path> {
let geometry = shape_geometry(&app.ui.tool_options, tool, start, end, mods)?;
if tool == Tool::CustomShape {
let rect = geometry.get("rect").and_then(|r| serde_json::from_value::<[f64; 4]>(r.clone()).ok())?;
let groups = photocraft_engine::presets::shapes::all_groups(&app.session);
let shape = groups.iter().flat_map(|g| &g.items).find(|s| s.name == app.ui.presets_ui.shape())?;
photocraft_engine::presets::shapes::fit(&shape.path, rect, mods.shift)
Some(photocraft_engine::presets::shapes::fit(&shape.path, rect, mods.shift))
} else {
photocraft_engine::vector_cmds::shape_path(&geometry).ok()?
};
photocraft_engine::vector_cmds::shape_path(&geometry).ok()
}
}
/// The preview and command both use the full float tool colours, including transparent paint.
/// In Path mode only the outline shows, as the drag draws no layer.
pub(super) fn shape_preview(app: &PhotocraftApp, tool: Tool, start: [f64; 2], end: [f64; 2], mods: egui::Modifiers) -> Option<photocraft_doc::ShapeLayer> {
let o = &app.ui.tool_options;
let path = shape_outline(app, tool, start, end, mods)?;
let paint = !draws_path(o);
Some(photocraft_doc::ShapeLayer {
path,
fill: o.shape_fill.then(|| tool_paint(app, false)),
stroke: (o.stroke_width > 0.0).then(|| o.shape_stroke.stroke(o.stroke_width, tool_paint(app, true))),
fill: (paint && o.shape_fill).then(|| tool_paint(app, false)),
stroke: (paint && o.stroke_width > 0.0).then(|| o.shape_stroke.stroke(o.stroke_width, tool_paint(app, true))),
..Default::default()
})
}
@@ -500,6 +500,33 @@ fn shape_drag_creates_layers_with_modifiers() {
assert_eq!(app.session.active().unwrap().doc.layers.len(), 4);
}
/// #9453: in Path mode a Shape-tool drag adds its outline to the work path, one subpath per
/// drag, and makes no layer, as Photoshop's Path tool mode does. Shape stays the default.
#[test]
fn path_mode_shape_drags_add_to_the_work_path() {
let mut app = app();
assert_eq!(app.ui.tool_options.shape_mode, "shape", "Shape by default, as in Photoshop");
let layers = app.session.active().unwrap().doc.layers.len();
app.ui.tool_options.shape_mode = "path".into();
finish_shape(&mut app, Tool::EllipseShape, [20.0, 20.0], [70.0, 50.0], egui::Modifiers::NONE);
let doc = &app.session.active().unwrap().doc;
assert_eq!(doc.layers.len(), layers, "no layer");
let work = doc.work_path.as_ref().expect("a work path");
assert_eq!(work.subpaths.len(), 1);
assert!(work.subpaths[0].closed);
let b = work.control_bounds().unwrap();
assert!((b.0 - 20.0).abs() < 0.5 && (b.1 - 20.0).abs() < 0.5 && (b.2 - 70.0).abs() < 0.5 && (b.3 - 50.0).abs() < 0.5, "{b:?}");
assert_eq!(app.ui.selected_path.as_deref(), Some("work"));
finish_shape(&mut app, Tool::Rectangle, [100.0, 100.0], [140.0, 130.0], egui::Modifiers::NONE);
let doc = &app.session.active().unwrap().doc;
assert_eq!(doc.work_path.as_ref().unwrap().subpaths.len(), 2, "a second drag adds a subpath");
assert_eq!(doc.layers.len(), layers);
// Back in Shape mode a drag makes a shape layer again.
app.ui.tool_options.shape_mode = "shape".into();
finish_shape(&mut app, Tool::Rectangle, [10.0, 150.0], [60.0, 180.0], egui::Modifiers::NONE);
assert_eq!(app.session.active().unwrap().doc.layers.len(), layers + 1);
}
#[test]
fn shift_pressed_during_a_shape_drag_constrains_it() {
// #668: ⇧ only counted when held at the press; Photoshop constrains when it is pressed