diff --git a/crates/ui-egui/src/canvas/cursor_policy.rs b/crates/ui-egui/src/canvas/cursor_policy.rs index deefb27b95..01163ce5a1 100644 --- a/crates/ui-egui/src/canvas/cursor_policy.rs +++ b/crates/ui-egui/src/canvas/cursor_policy.rs @@ -102,15 +102,16 @@ pub(crate) fn alt_eyedropper(tool: Tool) -> bool { /// Shape tools whose Alt Eyedropper also updates the tool's Fill. pub(super) fn shape_alt_sets_fill(tool: Tool) -> bool { - matches!(tool, Tool::Triangle | Tool::CustomShape | Tool::Rectangle | Tool::EllipseShape | Tool::Polygon) + matches!(tool, Tool::Triangle | Tool::CustomShape | Tool::Rectangle | Tool::EllipseShape | Tool::Polygon | Tool::Star) } -/// Whether an Alt or Shift+Alt press samples the foreground and Fill for the Triangle, Custom Shape, -/// Rectangle, Ellipse and Polygon tools (#13364, #15856, #13333, #13224, #13317). Sampling requires Shape mode, no active shape layer, and a sampleable pixel. -/// Control+Alt is excluded here, as in [`alt_samples`]. +/// Whether an Alt press samples the foreground and Fill for a shape tool. Sampling requires Shape +/// mode, no active shape layer, and a sampleable pixel. The Star keeps Shift+Alt for drawing a +/// constrained shape from the centre. Control+Alt is excluded, as in [`alt_samples`]. pub(crate) fn shape_alt_samples(app: &mut PhotocraftApp, mods: Mods, x: f64, y: f64) -> bool { mods.alt && !mods.ctrl + && (!mods.shift || app.ui.tool != Tool::Star) && shape_alt_sets_fill(app.ui.tool) && app.ui.tool_options.shape_mode == craft::draw::ui::VectorMode::Shape && crate::vector_ui::active_shape_path(app).is_none() diff --git a/crates/ui-egui/src/tools/vector_ui/host.rs b/crates/ui-egui/src/tools/vector_ui/host.rs index 23c7ad6c5d..725ed0c4e1 100644 --- a/crates/ui-egui/src/tools/vector_ui/host.rs +++ b/crates/ui-egui/src/tools/vector_ui/host.rs @@ -483,3 +483,7 @@ fn resume(app: &mut PhotocraftApp, key: u64, knot: KnotRef) { // On the Work Path that press is the step "Pick-up Path", and the path goes on being drawn live (#12890). pen::pick_up_live_path(app); } + +#[cfg(test)] +#[path = "star_alt_eyedropper_tests.rs"] +mod star_alt_eyedropper_tests; diff --git a/crates/ui-egui/src/tools/vector_ui/star_alt_eyedropper_tests.rs b/crates/ui-egui/src/tools/vector_ui/star_alt_eyedropper_tests.rs new file mode 100644 index 0000000000..723e670cd1 --- /dev/null +++ b/crates/ui-egui/src/tools/vector_ui/star_alt_eyedropper_tests.rs @@ -0,0 +1,109 @@ +//! #16875: an Option press can hand the Star tool's gesture to the eyedropper. + +use craft::draw::ui::VectorMode; +use egui::Modifiers; +use photocraft_doc::{Color, Fill, LayerContent}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, testing, tool_event}; +use crate::state::Tool; + +const ON_RED: [f64; 2] = [200.0, 175.0]; +const ON_WHITE: [f64; 2] = [500.0, 350.0]; +const START_ON_WHITE: [f64; 2] = [400.0, 300.0]; + +fn app() -> Box { + 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.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 150})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.run("tools.setColors", json!({"foreground": "#000000"})).unwrap(); + app.ui.tool = Tool::Star; + app.ui.tool_options.shape_mode = VectorMode::Shape; + app.sync_views(); + app +} + +fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], press: Modifiers, rest: Modifiers) { + tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, press); + tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, rest); + tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, rest); +} + +fn layers_and_steps(app: &PhotocraftApp) -> (usize, usize) { + let state = app.session.active().unwrap(); + (state.doc.layers.len(), state.history.past_len()) +} + +fn foreground(app: &PhotocraftApp) -> [u8; 3] { + let c = app.session.tools.foreground; + [0, 1, 2].map(|i| (c[i] * 255.0).round() as u8) +} + +#[test] +fn star_alt_drag_samples_the_release_pixel_for_the_foreground_and_fill() { + for release in [Modifiers::ALT, Modifiers::NONE] { + let mut app = app(); + app.ui.tool_options.shape_fill = false; + let before = layers_and_steps(&app); + + tool_event(&mut app, ToolEvent::Down { x: ON_RED[0], y: ON_RED[1], pressure: 1.0 }, Modifiers::ALT); + tool_event(&mut app, ToolEvent::Move { x: 250.0, y: 200.0, pressure: 1.0 }, Modifiers::ALT); + assert_eq!(foreground(&app), [255, 0, 0], "red at the last move"); + tool_event(&mut app, ToolEvent::Up { x: ON_WHITE[0], y: ON_WHITE[1] }, release); + + assert_eq!(foreground(&app), [255, 255, 255], "white at release"); + assert!(app.ui.tool_options.shape_fill, "the Fill swatch is solid"); + assert_eq!(layers_and_steps(&app), before, "sampling leaves layers and history alone"); + assert!(!testing::in_gesture(&app)); + + drag(&mut app, START_ON_WHITE, ON_WHITE, Modifiers::NONE, Modifiers::NONE); + let state = app.session.active().unwrap(); + let layer = state.doc.layer(state.active_layer.unwrap()).unwrap(); + assert!(matches!(&layer.content, LayerContent::Shape(shape) if shape.fill == Some(Fill::Solid(Color::WHITE)))); + } +} + +#[test] +fn star_alt_click_samples_without_opening_create_star() { + let mut app = app(); + let before = layers_and_steps(&app); + testing::click(&mut app, ON_RED, Modifiers::ALT); + assert_eq!(foreground(&app), [255, 0, 0]); + assert_eq!(layers_and_steps(&app), before); + assert!(app.framework.dialogs.is_empty()); +} + +#[test] +fn star_alt_drag_with_a_star_layer_active_still_draws() { + let mut app = app(); + drag(&mut app, START_ON_WHITE, ON_WHITE, Modifiers::NONE, Modifiers::NONE); + let (layers, steps) = layers_and_steps(&app); + drag(&mut app, ON_RED, [280.0, 230.0], Modifiers::ALT, Modifiers::ALT); + assert_eq!(foreground(&app), [0, 0, 0]); + assert_eq!(layers_and_steps(&app), (layers + 1, steps + 1)); +} + +#[test] +fn star_alt_pressed_after_the_drag_begins_still_draws() { + let mut app = app(); + let (layers, steps) = layers_and_steps(&app); + drag(&mut app, START_ON_WHITE, ON_WHITE, Modifiers::NONE, Modifiers::ALT); + assert_eq!(foreground(&app), [0, 0, 0]); + assert_eq!(layers_and_steps(&app), (layers + 1, steps + 1)); +} + +#[test] +fn star_alt_drag_in_path_and_pixels_modes_keeps_drawing() { + for (mode, new_layers) in [(VectorMode::Path, 0), (VectorMode::Pixels, 1)] { + let mut app = app(); + app.ui.tool_options.shape_mode = mode; + let (layers, steps) = layers_and_steps(&app); + drag(&mut app, START_ON_WHITE, ON_WHITE, Modifiers::ALT, Modifiers::ALT); + assert_eq!(foreground(&app), [0, 0, 0], "{mode:?}"); + assert_eq!(layers_and_steps(&app), (layers + new_layers, steps + 1), "{mode:?}"); + } +}