diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index 38ef019fb0e..b51c45546fe 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -4,10 +4,10 @@ //! The menu is state in `UiState::canvas_tool_menu`; [`run`] shows it on the framework's menu layer //! every pass. -use craft::behavior::IconId; +use craft::behavior::{IconId, PointerButton, PressPhase}; use craft::draw::ui::crop::{CROP_RATIOS, CROP_SIZES, CropBarWords, CropPresetChoice, CropRatio, CropSettings, CropSizeFields}; use craft::keymap::Mods; -use craft::ui::canvas::{CanvasOutput, MomentaryTool, ToolHostResponse}; +use craft::ui::canvas::{CanvasOutput, ContextRequest, ContextSource, MomentaryTool, ToolHostResponse}; use craft::ui::canvas_controls::GuidesOutput; use craft::ui::contract::overlay::{Anchor, Trigger}; use craft::ui::contract::{Bind, CommandSet, Entry}; @@ -2042,6 +2042,40 @@ pub fn entries(app: &PhotocraftApp, menu: &CanvasToolMenu) -> Vec> out } +/// Takes an ordinary Eyedropper right-click in an inactive tile and samples supported RGB content. +fn eyedropper_inactive_tile(app: &mut PhotocraftApp, canvas: &CanvasOutput, request: ContextRequest) -> bool { + use photocraft_engine::channel_cmds::ChannelTarget; + use photocraft_engine::sample_cmds::{SampleLayers, sample_color_at}; + + if request.source != ContextSource::Pointer + || request.mods != Mods::NONE + || !matches!(canvas.press.phase, PressPhase::Release { start, click: true, .. } if start.button == PointerButton::Secondary) + { + return false; + } + let Some(idx) = crate::canvas::document_area::inactive_tiled_document(app, canvas.response.id) else { return false }; + // Once the inactive tile owns the click, an unavailable sample must not open a menu for the active document. + let Some(space) = canvas.space.as_ref().filter(|space| space.view.rect.contains(request.at)) else { return true }; + let st = &app.session.documents()[idx]; + let point = space.view.to_doc(request.at); + if st.doc.mode != ColorMode::Rgb + || st.channel_view.target != ChannelTarget::Composite + || st.doc.quick_mask.is_some() + || photocraft_engine::mask_view_cmds::current(st).is_some() + { + return true; + } + let options = app.ui.tool_options.eyedropper; + let Some(source) = SampleLayers::parse(options.source.id()) else { return true }; + // Like document.sampleColor, use the document's active layer and device-pixel zoom for the sample box. + let zoom = app.ui.views.get(idx).map_or(1.0, |view| f64::from(view.zoom)); + let [r, g, b, a] = sample_color_at(&st.doc, st.active_layer, point.x, point.y, options.size.pixels(), Some(zoom), source); + if a > 0.0 { + let _ = app.run("tools.setColors", json!({"foreground": [r, g, b, 1.0]})); + } + true +} + /// Runs every pass of the canvas that owns the menu. Closes a menu whose reason is gone, opens the /// one a request asks for (a secondary press the tool did not take, the Menu key, or a guide's /// context request, or a secondary press on the Crop frame's overlay), and shows the open menu on the framework's menu layer at its press point, then @@ -2075,6 +2109,7 @@ pub(crate) fn run( None => Some(app.ui.tool), }; if let Some(tool) = tool + && !(tool == Tool::Eyedropper && request.momentary.is_none() && eyedropper_inactive_tile(app, canvas, request)) && let Some(view) = crate::canvas::document_display::active_view(app) { let doc = view.to_doc(request.at); diff --git a/crates/ui-egui/src/canvas/document_area.rs b/crates/ui-egui/src/canvas/document_area.rs index 256a1f3fc5a..f206d0aef46 100644 --- a/crates/ui-egui/src/canvas/document_area.rs +++ b/crates/ui-egui/src/canvas/document_area.rs @@ -105,6 +105,22 @@ pub(super) fn arranged_panes(app: &PhotocraftApp, rect: Rect) -> Option Option { + let windows = &app.ui.document_windows; + let tiled = matches!(windows.arrangement, Some(Arrangement::Tile | Arrangement::TileAllVertically | Arrangement::TileAllHorizontally)); + if !tiled || windows.docked.len() < 2 { + return None; + } + let active = app.session.active_index()?; + windows + .docked + .iter() + .filter_map(|group| group.shown.or_else(|| group.documents.first().copied())) + .filter_map(|doc| document_index(app, doc)) + .find(|&idx| idx != active && CanvasHost::main(idx).canvas_id() == canvas) +} + /// The arranged canvases' display and drop areas, inside any title and status bars. pub(crate) fn arranged_cells(app: &PhotocraftApp, rect: Rect) -> Option> { let mut panes = arranged_panes(app, rect)?; @@ -328,3 +344,7 @@ fn arranged(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: Rect, active: usiz #[cfg(test)] #[path = "arrange_keeps_zoom_tests.rs"] mod arrange_keeps_zoom_tests; + +#[cfg(test)] +#[path = "eyedropper_inactive_right_click_tests.rs"] +mod eyedropper_inactive_right_click_tests; diff --git a/crates/ui-egui/src/canvas/eyedropper_inactive_right_click_tests.rs b/crates/ui-egui/src/canvas/eyedropper_inactive_right_click_tests.rs new file mode 100644 index 00000000000..0c2bc9f7d28 --- /dev/null +++ b/crates/ui-egui/src/canvas/eyedropper_inactive_right_click_tests.rs @@ -0,0 +1,95 @@ +//! #17720: a secondary click samples an inactive tile without moving focus or opening a context menu. + +use craft::behavior::{PointerButton, kurbo::Point}; +use craft::color_ui::ui::SampleSource; +use craft::keymap::Mods; +use craft::ui::canvas::ToolHost; +use craft::ui::runtime::windows::WindowHost; +use egui::{Pos2, ViewportId}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::{document_display::CanvasHost, testing}; +use crate::canvas_tool_menu::MenuKind; +use crate::state::Tool; + +fn click(app: &mut PhotocraftApp, at: Pos2, button: PointerButton) { + let host = WindowHost::of(app.ctx()); + host.inject_click(ViewportId::ROOT, at, button, 1, Mods::NONE, false); + for _ in 0..8 { + testing::frame(app); + if host.injected_idle() { + return; + } + } + panic!("the tile click was not delivered"); +} + +#[test] +fn right_click_samples_the_inactive_tile_and_keeps_the_active_tiles_menus() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.background_jobs = false; + app.run("file.new", json!({"width": 800, "height": 600, "name": "A", "background": "white"})).unwrap(); + app.run("file.new", json!({"width": 400, "height": 300, "name": "B", "background": "#14b4c8"})).unwrap(); + let current = app.session.active().unwrap().active_layer.unwrap(); + app.run("layer.new.layer", json!({"name": "Orange"})).unwrap(); + app.session + .edit("Orange patch", |doc, active| { + let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + surface.fill_rect(photocraft_geom::Rect::new(100, 0, 300, 300), &[250.0 / 255.0, 120.0 / 255.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + app.run("layer.select", json!({"layer": current.0})).unwrap(); + app.run("document.activate", json!({"document": 0})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Eyedropper; + app.ui.extras.rulers = false; + let ctx = app.ctx().clone(); + crate::menus::invoke(&mut app, &ctx, "window.arrange.tileAllVertically", json!({})).unwrap(); + app.ui.views[0].zoom = 0.25; + app.ui.views[1].zoom = 0.5; + testing::shown(&mut app); + + let left = ToolHost::shown_space(&ctx, CanvasHost::main(0).canvas_id()).expect("A is shown").view; + let right = ToolHost::shown_space(&ctx, CanvasHost::main(1).canvas_id()).expect("B is shown").view; + let in_a = left.to_screen(Point::new(400.0, 300.0)); + let in_b = right.to_screen(Point::new(150.0, 150.0)); + let pasteboard = left.to_screen(Point::new(-20.0, 300.0)); + let b_pasteboard = right.to_screen(Point::new(-20.0, 150.0)); + assert!(left.rect.contains(in_a) && left.rect.contains(pasteboard)); + assert!(right.rect.contains(in_b) && !left.rect.contains(in_b)); + assert!(right.rect.contains(b_pasteboard) && !left.rect.contains(b_pasteboard)); + + click(&mut app, in_a, PointerButton::Primary); + let active = app.session.active().unwrap().doc.id; + let focus = ctx.memory(|memory| memory.focused()); + assert!(focus.is_some(), "the active tile owns keyboard focus before the secondary click"); + for (source, expected) in [(SampleSource::All, [250.0, 120.0, 0.0]), (SampleSource::Current, [20.0, 180.0, 200.0])] { + app.ui.tool_options.eyedropper.source = source; + click(&mut app, in_b, PointerButton::Secondary); + for (actual, expected) in app.session.tools.foreground.into_iter().zip(expected) { + assert!((actual * 255.0 - expected).abs() <= 1.0, "{source:?}: {actual}"); + } + assert_eq!(app.session.active().unwrap().doc.id, active); + assert_eq!(ctx.memory(|memory| memory.focused()), focus); + assert!(app.ui.canvas_tool_menu.is_none(), "B opens neither the tool menu nor A's pasteboard menu"); + } + + let swatches = (app.session.tools.foreground, app.session.tools.background); + let revisions: Vec<_> = app.session.documents().iter().map(|st| st.revision).collect(); + click(&mut app, b_pasteboard, PointerButton::Secondary); + assert!(app.ui.canvas_tool_menu.is_none(), "B's pasteboard must not open a menu for A"); + assert_eq!((app.session.tools.foreground, app.session.tools.background), swatches); + assert!(app.session.documents().iter().map(|st| st.revision).eq(revisions)); + assert_eq!(app.session.active().unwrap().doc.id, active); + assert_eq!(ctx.memory(|memory| memory.focused()), focus); + + click(&mut app, in_a, PointerButton::Secondary); + let menu = app.ui.canvas_tool_menu.take().expect("A keeps its Eyedropper menu"); + assert_eq!(menu.kind, MenuKind::Tool); + assert_eq!(menu.tool, Tool::Eyedropper); + + click(&mut app, pasteboard, PointerButton::Secondary); + assert_eq!(app.ui.canvas_tool_menu.as_ref().unwrap().kind, MenuKind::Pasteboard); +}