Merge pull request #13278 from rmsff/feat/red-eye-canvas-menu-12577

Red Eye: canvas right-click opens the layer or selection menu
This commit is contained in:
David Mann authored and GitHub committed 2026-11-11 19:57:11 +01:00
commit 78e478adda
5 files changed
+288 -40

No files matched your search

+75 -35
View File
@@ -458,24 +458,38 @@ pub const HAND_MENU: &[Row] = &[
/// the view itself.
pub const FILL_SCREEN: &str = "view.fillScreen";
/// A selection menu: the rows every selection menu shares, with `$fills` (the rows between
/// Transform Selection and Last Filter) in its own order.
macro_rules! selection_menu {
($($fills:expr),* $(,)?) => {
&[
Some(("Deselect", "select.deselect")),
Some(("Select Inverse", "select.inverse")),
Some(("Feather…", "select.modify.feather")),
Some(("Select and Mask…", "select.selectAndMask")),
None,
Some(("Save Selection…", "select.saveSelection")),
Some(("Make Work Path…", "select.toWorkPath")),
None,
Some(("Layer Via Copy", "layer.new.layerViaCopy")),
Some(("Layer Via Cut", "layer.new.layerViaCut")),
Some(("New Layer…", "layer.new.layer")),
None,
Some(("Free Transform", "edit.freeTransform")),
Some(("Transform Selection", "select.transformSelection")),
$($fills,)*
None,
Some(("Last Filter", "filter.lastFilter")),
None,
Some(("Fade…", "edit.fade")),
]
};
}
/// Photoshop's selection-tool context menu with an active selection, in its order. Its Generative
/// Fill row has no PhotoCraft command and is left out. Rows stay visible and grey out exactly like
/// their menu-bar twins.
pub const SELECTION_MENU: &[Row] = &[
Some(("Deselect", "select.deselect")),
Some(("Select Inverse", "select.inverse")),
Some(("Feather…", "select.modify.feather")),
Some(("Select and Mask…", "select.selectAndMask")),
None,
Some(("Save Selection…", "select.saveSelection")),
Some(("Make Work Path…", "select.toWorkPath")),
None,
Some(("Layer Via Copy", "layer.new.layerViaCopy")),
Some(("Layer Via Cut", "layer.new.layerViaCut")),
Some(("New Layer…", "layer.new.layer")),
None,
Some(("Free Transform", "edit.freeTransform")),
Some(("Transform Selection", "select.transformSelection")),
pub const SELECTION_MENU: &[Row] = selection_menu![
Some(("Distort", "edit.transform.distort")),
Some(("Perspective", "edit.transform.perspective")),
None,
@@ -483,10 +497,6 @@ pub const SELECTION_MENU: &[Row] = &[
Some(("Fill…", "edit.fill")),
Some(("Stroke…", "edit.stroke")),
Some(("Content-Aware Fill…", "edit.contentAwareFill")),
None,
Some(("Last Filter", "filter.lastFilter")),
None,
Some(("Fade…", "edit.fade")),
];
/// The selection tools' context menu with nothing selected: making a selection.
@@ -498,8 +508,8 @@ pub const NO_SELECTION_MENU: &[Row] = &[
Some(("Load Selection…", "select.loadSelection")),
];
/// The Count tool's canvas menu (#11772): Photoshop's generic layer menu, on a marker or off one,
/// in its order for a pixel layer without a selection. Each row is enabled as the Layers panel
/// Photoshop's generic layer menu, in its order for a pixel layer without a selection: the Count
/// tool's canvas menu on a marker or off one (#11772), and the Red Eye tool's without a selection. Each row is enabled as the Layers panel
/// menu enables it for the active layer (`menus::is_enabled`) and runs like the menu item.
pub const COUNT_MENU: &[Row] = &[
Some(("Blending Options…", "layer.layerStyle.blendingOptions")),
@@ -524,6 +534,16 @@ pub const COUNT_MENU: &[Row] = &[
/// Right-click with the Content-Aware Move tool inside an active selection (#12194).
pub const CAM_INSIDE_SELECTION_MENU: &[Row] = &[Some(("Color Range…", "select.colorRange")), Some(("Load Selection…", "select.loadSelection"))];
/// The selection menu as measured in the reference application with the Red Eye tool (#12577):
/// no Distort or Perspective, and Delete and Fill Selection after the fills.
pub const MEASURED_SELECTION_MENU: &[Row] = selection_menu![
None,
Some(("Fill…", "edit.fill")),
Some(("Stroke…", "edit.stroke")),
Some(("Content-Aware Fill…", "edit.contentAwareFill")),
Some(("Delete and Fill Selection", "edit.deleteAndFillSelection")),
];
/// The Eyedropper's copy rows (#11747).
pub const EYEDROPPER_COPY_HTML: &str = "canvasMenu.eyedropper.copyHtml";
pub const EYEDROPPER_COPY_HEX: &str = "canvasMenu.eyedropper.copyHex";
@@ -674,8 +694,31 @@ fn cam_allowed(app: &PhotocraftApp) -> bool {
app.session.active().is_some_and(|s| !matches!(s.doc.mode, ColorMode::Bitmap | ColorMode::Duotone | ColorMode::Multichannel))
}
/// Is document point `d` on the image and inside the active selection?
fn selected_at(app: &PhotocraftApp, d: [f64; 2]) -> bool {
d.iter().all(|v| v.is_finite())
&& d[0] >= 0.0
&& d[1] >= 0.0
&& app.session.active().is_some_and(|s| {
d[0] < f64::from(s.doc.size.width)
&& d[1] < f64::from(s.doc.size.height)
&& s.doc.selection.as_ref().is_some_and(|sel| sel.sample_channel(d[0].floor() as i32, d[1].floor() as i32, 0) >= 0.5)
})
}
/// Does a Red Eye right-click at `doc` open a menu (#12577)? Only where the reference application
/// was measured: an RGB document whose one layer is a pixel layer, with no selection or the pointer
/// inside it. Elsewhere it keeps opening nothing.
fn red_eye_measured(app: &PhotocraftApp, doc: Option<[f64; 2]>) -> bool {
app.session.active().is_some_and(|s| {
s.doc.mode == ColorMode::Rgb
&& matches!(s.doc.layers.as_slice(), [l] if matches!(l.content, LayerContent::Raster(_)) && l.frame.is_none())
&& (s.doc.selection.is_none() || doc.is_some_and(|d| selected_at(app, d)))
})
}
/// Tools whose plain canvas right-click opens a menu: the selection tools' actions, the
/// Content-Aware Move menu, the path menu and the Color Sampler's.
/// Content-Aware Move menu, the path menu, the Color Sampler's and the Red Eye tool's.
pub fn applies(tool: Tool) -> bool {
matches!(
tool,
@@ -697,6 +740,8 @@ pub fn applies(tool: Tool) -> bool {
// Sample Size and Copy Color (#11747).
| Tool::Eyedropper
| Tool::ContentAwareMove
// The layer or selection menu, where measured (#12577).
| Tool::RedEye
) || path_menu(tool)
}
@@ -750,6 +795,8 @@ pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] {
SAMPLER_MENU
} else if menu.tool == Tool::Count {
COUNT_MENU
} else if menu.tool == Tool::RedEye {
if menu.has_selection { MEASURED_SELECTION_MENU } else { COUNT_MENU }
} else if menu.tool == Tool::Eyedropper {
EYEDROPPER_MENU
} else if inside_selection(menu) {
@@ -1193,23 +1240,16 @@ pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2]
crate::color_hud::release(&mut app.ui);
return false;
}
if tool == Tool::RedEye && !red_eye_measured(app, doc) {
return false;
}
app.ui.brush_picker = None;
app.ui.layer_menu = None;
let has_selection = app.session.active().is_some_and(|s| s.doc.selection.is_some());
let has_path = crate::vector_ui::active_path_name(app).is_some() || app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2);
let path_name = crate::vector_ui::active_path_name(app);
let spelling = if text { doc.and_then(|d| spelling_at(app, d)) } else { None };
let inside_selection = tool == Tool::ContentAwareMove
&& doc.is_some_and(|d| {
d.iter().all(|v| v.is_finite())
&& d[0] >= 0.0
&& d[1] >= 0.0
&& app.session.active().is_some_and(|s| {
d[0] < f64::from(s.doc.size.width)
&& d[1] < f64::from(s.doc.size.height)
&& s.doc.selection.as_ref().is_some_and(|sel| sel.sample_channel(d[0].floor() as i32, d[1].floor() as i32, 0) >= 0.5)
})
});
let inside_selection = tool == Tool::ContentAwareMove && doc.is_some_and(|d| selected_at(app, d));
let sampler = if tool == Tool::ColorSampler { doc.and_then(|d| crate::analysis_ui::sampler_at(app, d)) } else { None };
let sampler_at = sampler.and_then(|i| Some((app.session.active_index()?, *app.session.active()?.color_samplers.get(i)?)));
// The Eyedropper reads the colour now, under the right-click: all layers, with the Sample
@@ -1703,10 +1743,10 @@ pub fn menu_rect_if_open(app: &PhotocraftApp, ctx: &Context) -> Option<egui::Rec
#[cfg(test)]
mod cam_menu_tests;
#[cfg(test)]
mod tests;
pub(crate) mod tests;
#[cfg(test)]
mod count_tests;
pub(crate) mod count_tests;
#[cfg(test)]
mod hand_menu_entries_tests;
@@ -18,7 +18,7 @@ fn app() -> (PhotocraftApp, egui::Context) {
(app, egui::Context::default())
}
fn call(app: &mut PhotocraftApp, ctx: &egui::Context, method: &str, params: Value) -> Value {
pub(crate) fn call(app: &mut PhotocraftApp, ctx: &egui::Context, method: &str, params: Value) -> Value {
let (req, _rx) = ControlRequest::new(method, params);
match handle(app, ctx, &req) {
Outcome::Done(v) => v,
@@ -26,13 +26,13 @@ fn call(app: &mut PhotocraftApp, ctx: &egui::Context, method: &str, params: Valu
}
}
fn right_click(app: &mut PhotocraftApp, ctx: &egui::Context, x: f64, y: f64) {
pub(crate) fn right_click(app: &mut PhotocraftApp, ctx: &egui::Context, x: f64, y: f64) {
let r = call(app, ctx, "ui.pointer", json!({"button": "secondary", "events": [{"kind": "down", "x": x, "y": y}, {"kind": "up", "x": x, "y": y}]}));
assert_eq!(r["ok"], true, "{r}");
}
/// The open menu's rows as `ui.inspect` lists them: ids, with "―" for separators.
fn listed(app: &mut PhotocraftApp, ctx: &egui::Context) -> Vec<String> {
pub(crate) fn listed(app: &mut PhotocraftApp, ctx: &egui::Context) -> Vec<String> {
let inspected = call(app, ctx, "ui.inspect", json!({}));
let entries = inspected.pointer("/result/canvasToolMenu/entries").and_then(Value::as_array).cloned().unwrap_or_default();
entries.iter().map(|e| e["id"].as_str().map_or_else(|| "―".to_string(), str::to_string)).collect()
@@ -28,7 +28,7 @@ pub(super) fn choose_open(app: &mut PhotocraftApp, command: &str) {
choose(app, &Context::default(), command);
}
pub(super) fn harness(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
pub(crate) fn harness(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
app.sync_views();
let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
@@ -45,7 +45,7 @@ pub(super) fn harness(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp>
h
}
pub(super) fn right_click(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, modifiers: Modifiers) {
pub(crate) fn right_click(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, modifiers: Modifiers) {
h.event(egui::Event::ModifiersChanged(modifiers));
h.event(egui::Event::PointerMoved(p));
h.run_steps(1);
+3
View File
@@ -1097,3 +1097,6 @@ mod background_eraser_digit_keys_tests;
mod clone_stamp_reset_tip_tests;
#[cfg(test)]
mod sponge_default_tip_tests;
#[cfg(test)]
mod red_eye_context_menu_tests;
@@ -0,0 +1,205 @@
//! #12577: a right-click on the canvas with the Red Eye tool opens the layer menu, or the
//! selection menu with the pointer inside a selection, as measured in the reference application on
//! a plain one-layer RGB document. The rows grey out like their menu-bar twins and run their
//! commands; the right-click itself corrects no red eye and adds no history state. The cases that
//! weren't measured keep opening nothing.
use egui::Modifiers;
use serde_json::{Value, json};
use crate::PhotocraftApp;
use crate::canvas_tool_menu::count_tests::{call, listed, right_click};
use crate::canvas_tool_menu::{entry_enabled, rows};
use crate::state::Tool;
/// A 64 × 48 RGB document with its one Background layer red, after a cleared selection (so
/// Reselect has one to restore). The Red Eye tool is the tool.
fn app() -> (PhotocraftApp, egui::Context) {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 64, "height": 48, "background": "white"})).unwrap();
app.run("edit.fill", json!({"contents": "color", "color": "#d01818"})).unwrap();
app.run("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap();
app.run("select.deselect", json!({})).unwrap();
app.sync_views();
app.ui.tool = Tool::RedEye;
(app, egui::Context::default())
}
/// The open menu's rows as `ui.inspect` lists them: (label, enabled), "―" for separators.
fn entries(app: &mut PhotocraftApp, ctx: &egui::Context) -> Vec<(String, bool)> {
let inspected = call(app, ctx, "ui.inspect", json!({}));
let entries = inspected.pointer("/result/canvasToolMenu/entries").and_then(Value::as_array).cloned().unwrap_or_default();
entries.iter().map(|e| (e["label"].as_str().unwrap_or("―").to_string(), e["enabled"] != false)).collect()
}
/// `want` as (label, enabled): the rows named in `grey` disabled, separators as "―".
fn expected(want: &[&str], grey: &[&str]) -> Vec<(String, bool)> {
want.iter().map(|l| (l.to_string(), *l == "―" || !grey.contains(l))).collect()
}
/// The document's revision, history depth and journal length.
fn state(app: &PhotocraftApp) -> (u64, usize, usize) {
let st = app.session.active().unwrap();
(st.revision, st.history.past_len(), app.session.journal.len())
}
const NO_SELECTION: [&str; 17] = [
"Blending Options…",
"―",
"Duplicate Layer…",
"Delete Layer",
"Group from Layers…",
"―",
"Convert to Smart Object",
"Convert to Layers",
"―",
"Rasterize Layer",
"Rasterize Layer Style",
"―",
"Free Transform",
"―",
"Color Range…",
"Load Selection…",
"Reselect",
];
/// The reference also greys Group from Layers… and Load Selection…, but their menu-bar twins are
/// live here (as in the Count tool's menu), and the rows follow the twins.
const NO_SELECTION_GREY: [&str; 5] = ["Blending Options…", "Convert to Layers", "Rasterize Layer", "Rasterize Layer Style", "Free Transform"];
const IN_SELECTION: [&str; 23] = [
"Deselect",
"Select Inverse",
"Feather…",
"Select and Mask…",
"―",
"Save Selection…",
"Make Work Path…",
"―",
"Layer Via Copy",
"Layer Via Cut",
"New Layer…",
"―",
"Free Transform",
"Transform Selection",
"―",
"Fill…",
"Stroke…",
"Content-Aware Fill…",
"Delete and Fill Selection",
"―",
"Last Filter",
"―",
"Fade…",
];
fn select(app: &mut PhotocraftApp) {
app.run("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap();
}
#[test]
fn without_a_selection_the_layer_menu_opens_in_order_with_the_reference_greys() {
let (mut app, ctx) = app();
right_click(&mut app, &ctx, 40.0, 30.0);
assert_eq!(entries(&mut app, &ctx), expected(&NO_SELECTION, &NO_SELECTION_GREY));
}
#[test]
fn inside_a_selection_the_selection_menu_opens_in_order_with_the_reference_greys() {
let (mut app, ctx) = app();
select(&mut app);
right_click(&mut app, &ctx, 20.0, 20.0);
assert_eq!(entries(&mut app, &ctx), expected(&IN_SELECTION, &["Last Filter", "Fade…"]));
}
#[test]
fn rows_grey_out_like_their_menu_bar_twins() {
for selection in [false, true] {
let (mut app, ctx) = app();
if selection {
select(&mut app);
}
right_click(&mut app, &ctx, 20.0, 20.0);
let menu = app.ui.canvas_tool_menu.clone().unwrap();
for (_, id) in rows(&menu).iter().flatten() {
assert_eq!(entry_enabled(&app, &menu, id), crate::menus::is_enabled(&app, id), "{id} (selection: {selection})");
}
}
}
#[test]
fn picking_a_row_runs_its_command() {
let (mut app, ctx) = app();
select(&mut app);
right_click(&mut app, &ctx, 20.0, 20.0);
assert_eq!(call(&mut app, &ctx, "ui.context.choose", json!({"id": "select.deselect"}))["ok"], true);
assert!(app.session.active().unwrap().doc.selection.is_none(), "Deselect deselected");
assert!(app.ui.canvas_tool_menu.is_none(), "the pick closes the menu");
right_click(&mut app, &ctx, 20.0, 20.0);
assert_eq!(call(&mut app, &ctx, "ui.context.choose", json!({"id": "select.reselect"}))["ok"], true);
assert!(app.session.active().unwrap().doc.selection.is_some(), "Reselect brought it back");
// A row the open menu doesn't have is refused.
right_click(&mut app, &ctx, 20.0, 20.0);
assert_eq!(call(&mut app, &ctx, "ui.context.choose", json!({"id": "select.colorRange"}))["ok"], false);
}
#[test]
fn the_right_click_corrects_no_red_eye_and_adds_no_history_state() {
for selection in [false, true] {
let (mut app, ctx) = app();
if selection {
select(&mut app);
}
let before = state(&app);
right_click(&mut app, &ctx, 20.0, 20.0);
assert!(app.ui.canvas_tool_menu.is_some(), "the menu is open (selection: {selection})");
assert_eq!(state(&app), before, "nothing ran (selection: {selection})");
}
}
#[test]
fn a_mouse_right_click_on_the_canvas_opens_the_layer_menu() {
let (app, _) = app();
let mut h = crate::canvas_tool_menu::tests::harness(app);
let before = state(h.state());
let center = h.state().last_canvas_rect.center();
crate::canvas_tool_menu::tests::right_click(&mut h, center, Modifiers::NONE);
let menu = h.state().ui.canvas_tool_menu.clone().expect("the menu opens");
let labels: Vec<&str> = rows(&menu).iter().map(|r| r.map_or("―", |(l, _)| l)).collect();
assert_eq!(labels, NO_SELECTION);
assert_eq!(state(h.state()), before, "no correction, no history state");
}
#[test]
fn the_cases_not_measured_keep_opening_nothing() {
type Setup = fn(&mut PhotocraftApp);
let cases: [(&str, Setup); 5] = [
("outside the selection", |app| app.run("select.rect", json!({"x": 40, "y": 30, "width": 10, "height": 10})).map(drop).unwrap()),
("a second layer", |app| app.run("layer.new.layer", json!({"name": "Ink"})).map(drop).unwrap()),
("a smart object", |app| app.run("layer.smartObjects.convertToSmartObject", json!({})).map(drop).unwrap()),
("a type layer", |app| {
let background = app.session.active().unwrap().doc.layers[0].id.0;
app.run("type.create", json!({"text": "Eyes", "size": 12, "x": 4, "y": 20})).unwrap();
app.run("layer.select", json!({"layer": background})).unwrap();
app.run("layer.delete", json!({})).unwrap();
}),
("Grayscale", |app| app.run("image.mode.grayscale", json!({})).map(drop).unwrap()),
];
for (what, setup) in cases {
let (mut app, ctx) = app();
setup(&mut app);
let before = state(&app);
right_click(&mut app, &ctx, 20.0, 20.0);
assert!(app.ui.canvas_tool_menu.is_none(), "{what}: no menu");
assert!(listed(&mut app, &ctx).is_empty(), "{what}");
assert_eq!(state(&app), before, "{what}: nothing ran");
}
}
#[test]
fn outside_the_image_with_a_selection_the_pasteboard_menu_opens_as_before() {
let (mut app, ctx) = app();
select(&mut app);
right_click(&mut app, &ctx, -10.0, 20.0);
assert!(app.ui.canvas_tool_menu.as_ref().is_some_and(|m| m.pasteboard), "the pasteboard colour menu");
}