Merge pull request #12993 from ljkx/feat/pen-make-mask-button

Pen options bar: Make: Selection…, Mask, Shape — Mask adds a vector mask from the path (#12904)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-10 21:42:05 +01:00
commit 6a9495c506
3 files changed
+187 -20

No files matched your search

+47 -17
View File
@@ -100,25 +100,9 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
),
),
}
// Path mode: Make Selection from the path being drawn or the active path, as Photoshop's
// options bar's "Make: Selection…" does (#5703).
if app.ui.tool_options.vector_mode == "path" {
crate::widgets::vline(ui, 22.0);
let has_path = active_path_name(app).is_some() || app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2);
if ui.add_enabled(has_path, egui::Button::new(tl!("Make Selection"))).clicked()
&& let Err(e) = crate::canvas_tool_menu::make_selection(app)
{
app.ui.status = e;
app.ui.status_error = true;
}
// Make: Shape turns the path into a shape layer with the shape fill and stroke (#10934).
let has_shape_path = super::path_ops::shape_source_path(app).is_some() || app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2);
if ui.add_enabled(has_shape_path, egui::Button::new(tl!("Make Shape"))).clicked()
&& let Err(e) = super::make_shape(app)
{
app.ui.status = e;
app.ui.status_error = true;
}
make_buttons(app, ui);
}
return true;
}
@@ -168,6 +152,48 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
true
}
/// Path mode's "Make:" row, as in Photoshop's options bar: Selection… (#5703), Mask (#12904) and
/// Shape (#10934), from the path being drawn or the active path; all greyed without one.
fn make_buttons(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
ui.label(egui::RichText::new(tl!("Make:")).color(t.text_dim).size(12.0));
let pending = app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2);
let has_path = pending || active_path_name(app).is_some();
// A draft that finishes into the targeted vector mask has no path of its own to make a mask
// from, as in the canvas menu's Create Vector Mask.
let has_mask_path = has_path && !(pending && super::path_ops::targeted_vector_mask(app).is_some());
// Make: Shape turns the path into a shape layer with the shape fill and stroke (#10934).
let has_shape_path = pending || super::path_ops::shape_source_path(app).is_some();
let selection = ui.add_enabled(has_path, egui::Button::new(tl!("Selection…"))).clicked();
let mask = ui.add_enabled(has_mask_path, egui::Button::new(tl!("Mask"))).clicked();
let shape = ui.add_enabled(has_shape_path, egui::Button::new(tl!("Shape"))).clicked();
let made = if selection {
crate::canvas_tool_menu::make_selection(app)
} else if mask {
make_mask(app)
} else if shape {
super::make_shape(app).map(|_| ())
} else {
Ok(())
};
if let Err(e) = made {
app.ui.status = e;
app.ui.status_error = true;
}
}
/// Make: Mask (#12904): a vector mask on the active layer from the path being drawn (finished
/// first, as the canvas menu's Create Vector Mask does) or the active path. The command records
/// the History step Add Vector Mask and turns the Background into a normal layer in that edit.
fn make_mask(app: &mut PhotocraftApp) -> Result<(), String> {
if app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2) && !super::pen::pen_commit(app, false) {
return Err(app.ui.status.clone());
}
let name =
active_path_name(app).ok_or_else(|| tl!("No path to make a mask from: draw one with the Pen tool or select one in the Paths panel.").to_string())?;
app.run("layer.vectorMask.fromPath", json!({"name": name})).map(|_| ())
}
/// 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"
@@ -479,3 +505,7 @@ pub fn shape_properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: photocra
}
}
}
#[cfg(test)]
#[path = "pen_make_mask_tests.rs"]
mod pen_make_mask_tests;
@@ -0,0 +1,137 @@
//! #12904: the Pen options bar's Path mode shows "Make:" followed by Selection…, Mask and Shape,
//! as in Photoshop. Mask adds a vector mask made from the path to the active layer as one History
//! step, Add Vector Mask; the Background becomes Layer 0 inside that step.
use egui::vec2;
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use photocraft_doc::Path;
use serde_json::json;
use crate::PhotocraftApp;
use crate::state::Tool;
use crate::vector_ui::{pen_down, pen_up};
fn harness() -> Harness<'static, PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
app.ui.tool = Tool::Pen;
let mut h = Harness::builder().with_size(vec2(1400.0, 400.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
if ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
ui.horizontal(|ui| super::options_bar(app, ui, Tool::Pen));
}
},
app,
);
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio);
h.run_steps(4);
h
}
/// Clicks a closed square with the Pen and returns the Work Path it made.
fn square(h: &mut Harness<'static, PhotocraftApp>) -> Path {
let app = h.state_mut();
for (x, y) in [(20.0, 20.0), (120.0, 20.0), (120.0, 120.0), (20.0, 120.0)] {
pen_down(app, x, y, false);
pen_up(app);
}
pen_down(app, 20.5, 20.5, false); // close
pen_up(app);
h.run_steps(2);
h.state().session.active().unwrap().doc.work_path.clone().expect("a work path")
}
fn steps(app: &PhotocraftApp) -> usize {
app.session.active().unwrap().history.past_len()
}
fn disabled(h: &Harness<'static, PhotocraftApp>, label: &str) -> bool {
h.get_by_label(label).accesskit_node().is_disabled()
}
#[test]
fn make_row_is_a_caption_and_three_buttons_greyed_without_a_path() {
let mut h = harness();
let x = |label: &str| h.get_by_label(label).rect().min.x;
let order = [x("Make:"), x("Selection…"), x("Mask"), x("Shape")];
assert!(order.is_sorted_by(|a, b| a < b), "Make: Selection… Mask Shape, left to right: {order:?}");
assert!(h.query_by_label("Make Selection").is_none() && h.query_by_label("Make Shape").is_none(), "the caption carries \"Make\"");
for label in ["Selection…", "Mask", "Shape"] {
assert!(disabled(&h, label), "{label}: no path yet");
}
square(&mut h);
for label in ["Selection…", "Mask", "Shape"] {
assert!(!disabled(&h, label), "{label}: the Work Path is the active path");
}
// Shape mode has no Make row.
h.state_mut().ui.tool_options.vector_mode = "shape".into();
h.run_steps(2);
assert!(h.query_by_label("Make:").is_none() && h.query_by_label("Mask").is_none());
}
#[test]
fn mask_adds_a_vector_mask_of_the_path_to_a_pixel_layer_in_one_step() {
let mut h = harness();
h.state_mut().run("layer.new.layer", json!({})).unwrap();
let drawn = square(&mut h);
let (before, layer) = (steps(h.state()), h.state().session.active().unwrap().active_layer.unwrap());
h.get_by_label("Mask").click();
h.run_steps(3);
let app = h.state();
assert!(!app.ui.status_error, "{}", app.ui.status);
let st = app.session.active().unwrap();
assert_eq!(st.active_layer, Some(layer), "the active layer is the one masked");
assert_eq!(st.doc.layer(layer).unwrap().vector_mask.as_ref().map(|m| &m.path), Some(&drawn));
assert_eq!(steps(app), before + 1, "one History step");
assert_eq!(st.history.undo_label(), Some("Add Vector Mask"));
assert_eq!(st.doc.work_path.as_ref(), Some(&drawn), "the Work Path stays");
h.state_mut().session.undo();
let st = h.state().session.active().unwrap();
assert!(st.doc.layer(layer).unwrap().vector_mask.is_none(), "one Undo takes the mask off");
}
#[test]
fn mask_on_the_background_makes_it_layer_0_in_the_same_step() {
let mut h = harness();
let drawn = square(&mut h);
let st = h.state().session.active().unwrap();
let (before, layer) = (steps(h.state()), st.active_layer.unwrap());
assert!(st.doc.is_background(layer));
h.get_by_label("Mask").click();
h.run_steps(3);
let app = h.state();
assert!(!app.ui.status_error, "{}", app.ui.status);
let st = app.session.active().unwrap();
let l = st.doc.layer(layer).unwrap();
assert!(!st.doc.is_background(layer), "no longer the Background");
assert_eq!(l.name, "Layer 0");
assert_eq!(l.vector_mask.as_ref().map(|m| &m.path), Some(&drawn));
assert_eq!(steps(app), before + 1, "one History step");
assert_eq!(st.history.undo_label(), Some("Add Vector Mask"));
h.state_mut().session.undo();
let st = h.state().session.active().unwrap();
assert!(st.doc.is_background(layer) && st.doc.layer(layer).unwrap().vector_mask.is_none(), "one Undo gives the Background back");
}
/// A path still being drawn is finished into the Work Path first, as the canvas menu's Create
/// Vector Mask does, and then masks the layer.
#[test]
fn mask_finishes_a_path_being_drawn_first() {
let mut h = harness();
h.state_mut().run("layer.new.layer", json!({})).unwrap();
for (x, y) in [(10.0, 150.0), (60.0, 150.0), (60.0, 190.0)] {
pen_down(h.state_mut(), x, y, false);
pen_up(h.state_mut());
}
h.run_steps(2);
assert!(!disabled(&h, "Mask"));
h.get_by_label("Mask").click();
h.run_steps(3);
let app = h.state();
assert!(app.ui.pen.is_none(), "the draft was finished");
let st = app.session.active().unwrap();
let mask = st.active_layer.and_then(|id| st.doc.layer(id)).and_then(|l| l.vector_mask.as_ref()).expect("a vector mask");
assert_eq!(mask.path.subpaths[0].knots.len(), 3);
assert_eq!(st.history.undo_label(), Some("Add Vector Mask"));
}
@@ -359,12 +359,12 @@ fn make_shape_turns_the_work_path_into_a_shape_layer() {
);
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio);
h.run_steps(4);
assert!(h.get_by_label("Make Shape").accesskit_node().is_disabled(), "no path yet");
assert!(h.get_by_label("Shape").accesskit_node().is_disabled(), "no path yet");
square(h.state_mut(), 20.0, 20.0, 100.0);
let drawn = work(h.state());
let (count, steps) = (layers(h.state()), h.state().session.active().unwrap().history.past_len());
h.run_steps(2);
h.get_by_label("Make Shape").click();
h.get_by_label("Shape").click();
h.run_steps(3);
let app = h.state();
assert_eq!(layers(app), count + 1);
@@ -383,7 +383,7 @@ fn make_shape_turns_the_work_path_into_a_shape_layer() {
}
let steps = h.state().session.active().unwrap().history.past_len();
h.run_steps(2);
h.get_by_label("Make Shape").click();
h.get_by_label("Shape").click();
h.run_steps(3);
let app = h.state();
assert!(app.ui.pen.is_none());