PhotoCraft: An open-source, photo editor written by scratch in pure Rust
UI: Control-click opens the right-click menus on macOS
Brian Monks0dcaa98a4046
diff --git a/crates/ui-egui/src/canvas_menu_tests.rs b/crates/ui-egui/src/canvas_menu_tests.rsindex 839dc1f76..eaf57c868 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.rsnew file mode 100644index 000000000..e02419de7--- /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.rsindex 8bf2be90d..97c516454 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);