mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge PR #8221: UI: Control-click opens the right-click menus on macOS
This commit is contained in:
commit
a2fb0d8157
3 files changed
+140
No files matched your search
@@ -141,6 +141,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);
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
//! 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;
|
||||
}
|
||||
}
|
||||
// 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.
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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 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();
|
||||
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, .. })));
|
||||
}
|
||||
}
|
||||
@@ -62,6 +62,7 @@ pub mod color_range_ui;
|
||||
pub mod color_table_ui;
|
||||
pub mod comps_ui;
|
||||
pub mod control;
|
||||
mod control_click;
|
||||
pub mod corner_radius;
|
||||
pub mod credits;
|
||||
pub mod crop_mode;
|
||||
@@ -925,6 +926,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.
|
||||
@@ -1081,6 +1083,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,
|
||||
@@ -2173,6 +2176,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);
|
||||
|
||||
Reference in new issue
Block a user