diff --git a/crates/ui-egui/src/menu_keys.rs b/crates/ui-egui/src/menu_keys.rs index 8a819fe319..84abde9323 100644 --- a/crates/ui-egui/src/menu_keys.rs +++ b/crates/ui-egui/src/menu_keys.rs @@ -42,6 +42,9 @@ pub struct Keys { alt_down: bool, /// Alt went down and nothing else was pressed since: releasing it is a tap. alt_tap: bool, + /// The mouse buttons that are down, one bit each, from the button events in order (the + /// frame's pointer state is its end state, which a whole click within one frame doesn't show). + buttons_held: u8, /// A menu was just opened from the keyboard: light its first row once it is drawn. first_row: bool, } @@ -251,18 +254,34 @@ impl Keys { match e { Event::Key { key: Key::AltLeft | Key::AltRight, pressed: true, .. } => { self.alt_down = true; - self.alt_tap = true; + // Alt pressed while a mouse button is down is a modifier for that drag + // (Free Transform's scale-from-centre), never a tap (#8113). + self.alt_tap = self.buttons_held == 0; } Event::Key { key: Key::AltLeft | Key::AltRight, pressed: false, .. } => { self.alt_down = false; tap |= std::mem::take(&mut self.alt_tap); } - // Alt+wheel (zoom, brush size…) or Alt going to the desktop is no tap either. - Event::Key { pressed: true, .. } - | Event::Text(_) - | Event::PointerButton { pressed: true, .. } - | Event::MouseWheel { .. } - | Event::WindowFocused(false) => self.alt_tap = false, + // A click or the end of a drag while Alt is held is no tap. It also cancels a tap + // taken earlier in this frame: a whole drag can arrive in one frame (press, Alt + // down, Alt up, release). + Event::PointerButton { button, pressed, .. } => { + let bit = 1u8 << (*button as u8); + if *pressed { + self.buttons_held |= bit; + } else { + self.buttons_held &= !bit; + } + self.alt_tap = false; + tap = false; + } + // Alt going to the desktop is no tap, and a button let go out there is up. + Event::WindowFocused(false) => { + self.alt_tap = false; + self.buttons_held = 0; + } + // Alt+wheel (zoom, brush size…) is no tap either. + Event::Key { pressed: true, .. } | Event::Text(_) | Event::MouseWheel { .. } => self.alt_tap = false, _ => {} } } diff --git a/crates/ui-egui/src/menu_keys_tests.rs b/crates/ui-egui/src/menu_keys_tests.rs index af1bb84d46..fc72648769 100644 --- a/crates/ui-egui/src/menu_keys_tests.rs +++ b/crates/ui-egui/src/menu_keys_tests.rs @@ -356,3 +356,59 @@ fn alt_held_through_a_shortcut_does_not_put_the_keyboard_on_the_bar() { alt_up(&mut h); assert_eq!(Keys::current(&h.ctx).focus, Some(0), "a lone tap still focuses the bar"); } + +/// #8113: Alt pressed during a drag (Free Transform's scale from the centre) is a modifier for the +/// drag, not a tap: with the mouse button let go before or after Alt, the keyboard stays where +/// it was, so Enter commits the transform instead of opening File. A lone tap still works. +#[test] +fn alt_held_during_a_drag_is_no_tap() { + let mut h = bar(); + let at = egui::pos2(640.0, 400.0); + let button = |h: &mut Harness<'static, PhotocraftApp>, pressed: bool, mods: egui::Modifiers| { + h.event_modifiers(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: mods }, mods); + h.run_steps(2); + }; + for mouse_up_first in [true, false] { + h.hover_at(at); + button(&mut h, true, egui::Modifiers::NONE); + h.key_down(egui::Key::AltLeft); + h.run_steps(2); + if mouse_up_first { + button(&mut h, false, egui::Modifiers::ALT); + h.key_up(egui::Key::AltLeft); + } else { + h.key_up(egui::Key::AltLeft); + h.run_steps(2); + button(&mut h, false, egui::Modifiers::NONE); + } + h.run_steps(3); + assert_eq!(Keys::current(&h.ctx).focus, None, "mouse up first: {mouse_up_first}"); + key(&mut h, egui::Key::Enter); + assert!(!is_open(&h.ctx), "Enter opens no menu (mouse up first: {mouse_up_first})"); + } + // The whole gesture within one frame: press, Alt down, Alt up, release. + let (alt, none) = (egui::Modifiers::ALT, egui::Modifiers::NONE); + h.event_modifiers(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: true, modifiers: none }, none); + h.event_modifiers(egui::Event::Key { key: egui::Key::AltLeft, physical_key: None, pressed: true, repeat: false, modifiers: alt }, alt); + h.event_modifiers(egui::Event::Key { key: egui::Key::AltLeft, physical_key: None, pressed: false, repeat: false, modifiers: none }, none); + h.event_modifiers(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: none }, none); + h.run_steps(3); + assert_eq!(Keys::current(&h.ctx).focus, None, "one frame"); + // Two buttons: the right one let go while the left is still down. Alt pressed then is still + // pressed during the left-button drag. + let btn = |h: &mut Harness<'static, PhotocraftApp>, button: egui::PointerButton, pressed: bool| { + h.event_modifiers(egui::Event::PointerButton { pos: at, button, pressed, modifiers: none }, none); + h.run_steps(2); + }; + btn(&mut h, egui::PointerButton::Primary, true); + btn(&mut h, egui::PointerButton::Secondary, true); + btn(&mut h, egui::PointerButton::Secondary, false); + h.key_down(egui::Key::AltLeft); + h.run_steps(2); + h.key_up(egui::Key::AltLeft); + h.run_steps(3); + assert_eq!(Keys::current(&h.ctx).focus, None, "the left button is still down"); + btn(&mut h, egui::PointerButton::Primary, false); + alt_tap(&mut h); + assert!(Keys::current(&h.ctx).focus.is_some(), "a lone tap still focuses the bar"); +}