From 0dcaa98a404649f24f210cb5f7c62ef49c79bd6a Mon Sep 17 00:00:00 2001 From: Brian Monks Date: Wed, 21 Oct 2026 12:24:43 +0200 Subject: [PATCH 1/2] UI: Control-click opens the right-click menus on macOS macOS delivers Control-click as a primary press with Control held, so no secondary_clicked()/context_menu() site saw it. ControlClick rewrites it to the secondary button in raw_input_hook, before panels and tools route clicks. Control-Option (brush resize, Liquify) and Control-Command keep the primary button; Control pressed mid-drag still overrides snapping. Tests: the normalizer's unit tests (now clearing the frame's texture delta), and a canvas test that feeds a real Control-click through the normalizer into the app: Marquee and Pen open the tool menu, Move the layer menu, Brush the Brush Preset picker, and nothing is painted. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/canvas_menu_tests.rs | 24 +++++++ crates/ui-egui/src/control_click.rs | 86 +++++++++++++++++++++++++ crates/ui-egui/src/lib.rs | 4 ++ 3 files changed, 114 insertions(+) create mode 100644 crates/ui-egui/src/control_click.rs diff --git a/crates/ui-egui/src/canvas_menu_tests.rs b/crates/ui-egui/src/canvas_menu_tests.rs index 839dc1f765c..eaf57c868eb 100644 --- a/crates/ui-egui/src/canvas_menu_tests.rs +++ b/crates/ui-egui/src/canvas_menu_tests.rs @@ -130,6 +130,30 @@ fn secondary_taps_keep_canvas_layer_menus_open() { check_reopening(Tool::RectMarquee, Modifiers::COMMAND, Popup::Layers); } +/// A macOS Control-click (control_click.rs) reaches the canvas as a right-click that still holds +/// Control: it opens the same menu as a plain right-click and paints nothing. +#[test] +fn control_clicks_open_the_right_click_menus() { + for (tool, popup) in [(Tool::RectMarquee, Popup::Tool), (Tool::Pen, Popup::Tool), (Tool::Move, Popup::Layers), (Tool::Brush, Popup::Brush)] { + let mut h = harness(tool, true, 1.0); + let at = fractional_point(&h, 1.0); + let before = document_state(&h); + let mut clicks = crate::control_click::ControlClick::default(); + h.event(Event::ModifiersChanged(Modifiers::CTRL)); + h.event(Event::PointerMoved(at)); + h.run_steps(2); + for pressed in [true, false] { + let mut events = vec![Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::CTRL }]; + clicks.normalize(&mut events, true); + h.input_mut().events.extend(events); + h.run_steps(1); + } + h.run_steps(3); + assert!(is_open(&h, popup), "{tool:?}"); + assert!(document_state(&h) == before, "{tool:?}: a Control-click paints nothing"); + } +} + #[test] fn reopened_canvas_menus_still_choose_their_actions() { let mut h = harness(Tool::RectMarquee, true, 1.0); diff --git a/crates/ui-egui/src/control_click.rs b/crates/ui-egui/src/control_click.rs new file mode 100644 index 00000000000..e02419de79a --- /dev/null +++ b/crates/ui-egui/src/control_click.rs @@ -0,0 +1,86 @@ +//! Normalize macOS Control-click before egui routes clicks to panels and canvas tools. +#[derive(Default)] +pub(crate) struct ControlClick { + secondary_down: bool, +} + +impl ControlClick { + pub fn normalize(&mut self, events: &mut [egui::Event], macos: bool) { + if !macos { + return; + } + for event in events { + match event { + egui::Event::PointerButton { button, pressed, modifiers, .. } if *button == egui::PointerButton::Primary => { + if *pressed { + // Control-Option is the existing brush resize gesture. + self.secondary_down = modifiers.ctrl && !modifiers.alt && !modifiers.command; + } + if self.secondary_down { + *button = egui::PointerButton::Secondary; + } + if !*pressed { + self.secondary_down = false; + } + } + egui::Event::WindowFocused(false) | egui::Event::PointerGone => self.secondary_down = false, + _ => {} + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use egui::{Event, Modifiers, PointerButton}; + + fn button(pressed: bool, modifiers: Modifiers) -> Event { + Event::PointerButton { pos: egui::pos2(50.0, 50.0), button: PointerButton::Primary, pressed, modifiers } + } + + #[test] + fn control_click_opens_context_menu_even_when_control_is_released_first() { + let ctx = egui::Context::default(); + let mut state = ControlClick::default(); + let mut secondary = false; + let mut primary = false; + for mut events in [vec![], vec![Event::PointerMoved(egui::pos2(50.0, 50.0)), button(true, Modifiers::CTRL)], vec![button(false, Modifiers::NONE)]] { + state.normalize(&mut events, true); + let raw = egui::RawInput { events, ..Default::default() }; + ctx.run_ui(raw, |ui| { + let r = ui.allocate_rect(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(200.0, 200.0)), egui::Sense::click()); + secondary |= r.secondary_clicked(); + primary |= r.clicked(); + r.context_menu(|ui| { + ui.label("Layer context menu"); + }); + }) + .textures_delta + .clear(); + } + assert!(secondary); + assert!(!primary); + } + + #[test] + fn other_platforms_and_existing_modifier_gestures_stay_primary() { + for (macos, mods) in + [(false, Modifiers::CTRL), (true, Modifiers::NONE), (true, Modifiers::CTRL | Modifiers::ALT), (true, Modifiers::CTRL | Modifiers::COMMAND)] + { + let mut events = vec![button(true, mods), button(false, mods)]; + ControlClick::default().normalize(&mut events, macos); + assert!(events.iter().all(|e| matches!(e, Event::PointerButton { button: PointerButton::Primary, .. }))); + } + } + + #[test] + fn focus_loss_does_not_remap_the_next_plain_click() { + let mut state = ControlClick::default(); + state.normalize(&mut [button(true, Modifiers::CTRL)], true); + state.normalize(&mut [Event::WindowFocused(false)], true); + let mut events = [button(true, Modifiers::NONE), button(false, Modifiers::NONE)]; + state.normalize(&mut events, true); + assert!(events.iter().all(|e| matches!(e, Event::PointerButton { button: PointerButton::Primary, .. }))); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 8bf2be90d75..97c5164546b 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -60,6 +60,7 @@ pub mod color_picker_ui; pub mod color_range_ui; pub mod comps_ui; pub mod control; +mod control_click; pub mod corner_radius; pub mod credits; pub mod crop_mode; @@ -894,6 +895,7 @@ pub struct PhotocraftApp { pub(crate) allow_close: bool, /// Pen pressure/tilt from the platform (see `stylus`). pub stylus: stylus::Stylus, + control_click: control_click::ControlClick, /// Run long commands and file opens as background jobs with progress and Cancel (#210; see /// `jobs_ui`). The desktop app turns it on; off (the default), everything runs inline as /// before, which tests and scripts rely on. @@ -1043,6 +1045,7 @@ impl PhotocraftApp { held_opens: Default::default(), allow_close: false, stylus: Default::default(), + control_click: Default::default(), background_jobs: false, min_frame_ms: None, last_frame_start_ms: 0.0, @@ -2094,6 +2097,7 @@ impl eframe::App for PhotocraftApp { wheel_nav::fold_legacy_pinch(ctx, raw_input); } raw_input.events.extend(self.take_synthetic_step()); + self.control_click.normalize(&mut raw_input.events, cfg!(target_os = "macos")); // A pen held still is no touch screen's press-and-hold right click (stylus.rs). let pen_down = self.stylus.pen_contacts.observe(&raw_input.events); stylus::pen_hold_is_no_right_click(ctx, pen_down); From e92c3be28dd52ef599b158a43aa65019e67c6ed7 Mon Sep 17 00:00:00 2001 From: Brian Monks Date: Wed, 21 Oct 2026 13:15:23 +0200 Subject: [PATCH 2/2] UI: keep a Control-click's secondary mapping until its release PointerGone and WindowFocused(false) cleared the mapping mid-gesture. On macOS a drag that leaves the window still delivers its release, which then arrived as a primary release, so egui kept the secondary button down. The mapping now lasts until the release; every press recomputes it, so a missed release can't remap a later plain click. Regression test: Control press, pointer exit, focus loss, re-entry and release leave no button down (failed before this change). Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/control_click.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/control_click.rs b/crates/ui-egui/src/control_click.rs index e02419de79a..289af136b66 100644 --- a/crates/ui-egui/src/control_click.rs +++ b/crates/ui-egui/src/control_click.rs @@ -23,7 +23,9 @@ impl ControlClick { self.secondary_down = false; } } - egui::Event::WindowFocused(false) | egui::Event::PointerGone => self.secondary_down = false, + // A drag keeps the mapping when the pointer leaves the window or focus is lost: the + // release still arrives and must match the secondary press egui already saw. The + // next press recomputes it, so a missed release can't remap a plain click. _ => {} } } @@ -74,6 +76,30 @@ mod tests { } } + #[test] + fn a_control_drag_out_of_the_window_and_back_releases_the_secondary_button() { + let ctx = egui::Context::default(); + let mut state = ControlClick::default(); + let frames = [ + vec![Event::PointerMoved(egui::pos2(50.0, 50.0)), button(true, Modifiers::CTRL)], + vec![Event::PointerGone], + vec![Event::WindowFocused(false)], + vec![Event::PointerMoved(egui::pos2(60.0, 60.0))], + vec![button(false, Modifiers::NONE)], + ]; + let mut down = Vec::new(); + for mut events in frames { + state.normalize(&mut events, true); + ctx.run_ui(egui::RawInput { events, ..Default::default() }, |ui| { + down.push(ui.input(|i| (i.pointer.secondary_down(), i.pointer.primary_down()))); + }) + .textures_delta + .clear(); + } + assert_eq!(down.first(), Some(&(true, false)), "the Control press is a secondary press"); + assert_eq!(down.last(), Some(&(false, false)), "the release after re-entry ends it: no stuck button"); + } + #[test] fn focus_loss_does_not_remap_the_next_plain_click() { let mut state = ControlClick::default();