diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 5628b886fe8ce99ac038490ff0a7587c40cbfec9..9bb1c2735fceebf1a296530d7969ef25c73a2f47 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -30,8 +30,8 @@ use gpui::{ platform::CursorStyle, text_layout::{self, Line, RunStyle, TextLayoutCache}, AppContext, Axis, Border, CursorRegion, Element, ElementBox, EventContext, LayoutContext, - MouseButton, MouseButtonEvent, MouseMovedEvent, MouseRegion, MutableAppContext, PaintContext, - Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle, + Modifiers, MouseButton, MouseButtonEvent, MouseMovedEvent, MouseRegion, MutableAppContext, + PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle, }; use json::json; use language::{Bias, CursorShape, DiagnosticSeverity, OffsetUtf16, Point, Selection}; @@ -209,10 +209,14 @@ impl EditorElement { fn mouse_down( MouseButtonEvent { position, - ctrl, - alt, - shift, - cmd, + modifiers: + Modifiers { + shift, + ctrl, + alt, + cmd, + .. + }, mut click_count, .. }: MouseButtonEvent, @@ -303,8 +307,7 @@ impl EditorElement { fn mouse_dragged( view: WeakViewHandle, MouseMovedEvent { - cmd, - shift, + modifiers: Modifiers { cmd, shift, .. }, position, .. }: MouseMovedEvent, @@ -379,8 +382,7 @@ impl EditorElement { fn mouse_moved( MouseMovedEvent { - cmd, - shift, + modifiers: Modifiers { shift, cmd, .. }, position, .. }: MouseMovedEvent, diff --git a/crates/editor/src/link_go_to_definition.rs b/crates/editor/src/link_go_to_definition.rs index 705a1b276075eda3ebbf3f42b7a6e8c0664694cc..c0c66dae71d19975a188236a1984e8e1786de59c 100644 --- a/crates/editor/src/link_go_to_definition.rs +++ b/crates/editor/src/link_go_to_definition.rs @@ -358,7 +358,7 @@ fn go_to_fetched_definition_of_kind( #[cfg(test)] mod tests { use futures::StreamExt; - use gpui::{ModifiersChangedEvent, View}; + use gpui::{Modifiers, ModifiersChangedEvent, View}; use indoc::indoc; use lsp::request::{GotoDefinition, GotoTypeDefinition}; @@ -431,7 +431,10 @@ mod tests { cx.update_editor(|editor, cx| { editor.modifiers_changed( &gpui::ModifiersChangedEvent { - cmd: true, + modifiers: Modifiers { + cmd: true, + ..Default::default() + }, ..Default::default() }, cx, @@ -660,8 +663,10 @@ mod tests { cx.update_editor(|editor, cx| { editor.modifiers_changed( &ModifiersChangedEvent { - cmd: true, - ..Default::default() + modifiers: Modifiers { + cmd: true, + ..Default::default() + }, }, cx, ); diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 5cbc786b725adce8ab4dc9997990b58c4e1a5197..015a3dfe7f798c3d370074656b5d85e47469629c 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -5777,10 +5777,7 @@ mod tests { Event::MouseDown(MouseButtonEvent { position: Default::default(), button: MouseButton::Left, - ctrl: false, - alt: false, - shift: false, - cmd: false, + modifiers: Default::default(), click_count: 1, }), false, diff --git a/crates/gpui/src/platform/event.rs b/crates/gpui/src/platform/event.rs index 7e4b95c9a1385fa3d3e566a8dd5bb8779775d1cd..a309f5123fb7d22c0c3360afbb120bc506222e44 100644 --- a/crates/gpui/src/platform/event.rs +++ b/crates/gpui/src/platform/event.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use crate::{geometry::vector::Vector2F, keymap::Keystroke}; #[derive(Clone, Debug)] @@ -11,12 +13,26 @@ pub struct KeyUpEvent { pub keystroke: Keystroke, } -#[derive(Clone, Copy, Debug, Default)] -pub struct ModifiersChangedEvent { +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Modifiers { pub ctrl: bool, pub alt: bool, pub shift: bool, pub cmd: bool, + pub fun: bool, +} + +#[derive(Clone, Copy, Debug, Default)] +pub struct ModifiersChangedEvent { + pub modifiers: Modifiers, +} + +impl Deref for ModifiersChangedEvent { + type Target = Modifiers; + + fn deref(&self) -> &Self::Target { + &self.modifiers + } } /// The phase of a touch motion event. @@ -33,14 +49,19 @@ pub struct ScrollWheelEvent { pub position: Vector2F, pub delta: Vector2F, pub precise: bool, - pub ctrl: bool, - pub alt: bool, - pub shift: bool, - pub cmd: bool, + pub modifiers: Modifiers, /// If the platform supports returning the phase of a scroll wheel event, it will be stored here pub phase: Option, } +impl Deref for ScrollWheelEvent { + type Target = Modifiers; + + fn deref(&self) -> &Self::Target { + &self.modifiers + } +} + #[derive(Hash, PartialEq, Eq, Copy, Clone, Debug)] pub enum NavigationDirection { Back, @@ -83,21 +104,31 @@ impl Default for MouseButton { pub struct MouseButtonEvent { pub button: MouseButton, pub position: Vector2F, - pub ctrl: bool, - pub alt: bool, - pub shift: bool, - pub cmd: bool, + pub modifiers: Modifiers, pub click_count: usize, } +impl Deref for MouseButtonEvent { + type Target = Modifiers; + + fn deref(&self) -> &Self::Target { + &self.modifiers + } +} + #[derive(Clone, Copy, Debug, Default)] pub struct MouseMovedEvent { pub position: Vector2F, pub pressed_button: Option, - pub ctrl: bool, - pub cmd: bool, - pub alt: bool, - pub shift: bool, + pub modifiers: Modifiers, +} + +impl Deref for MouseMovedEvent { + type Target = Modifiers; + + fn deref(&self) -> &Self::Target { + &self.modifiers + } } impl MouseMovedEvent { @@ -105,10 +136,7 @@ impl MouseMovedEvent { MouseButtonEvent { position: self.position, button: self.pressed_button.unwrap_or(button), - ctrl: self.ctrl, - alt: self.alt, - shift: self.shift, - cmd: self.cmd, + modifiers: self.modifiers, click_count: 0, } } diff --git a/crates/gpui/src/platform/mac/event.rs b/crates/gpui/src/platform/mac/event.rs index ea2b492b27dec58da298f59a3b155ff4257e83dd..77d1e67caeff9f84de1e9f74a3e5a1b256dca8f1 100644 --- a/crates/gpui/src/platform/mac/event.rs +++ b/crates/gpui/src/platform/mac/event.rs @@ -2,7 +2,7 @@ use crate::{ geometry::vector::vec2f, keymap::Keystroke, platform::{Event, NavigationDirection}, - KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton, MouseButtonEvent, + KeyDownEvent, KeyUpEvent, Modifiers, ModifiersChangedEvent, MouseButton, MouseButtonEvent, MouseMovedEvent, ScrollWheelEvent, TouchPhase, }; use cocoa::{ @@ -65,6 +65,23 @@ pub fn key_to_native(key: &str) -> Cow { Cow::Owned(String::from_utf16(&[code]).unwrap()) } +unsafe fn read_modifiers(native_event: id) -> Modifiers { + let modifiers = native_event.modifierFlags(); + let ctrl = modifiers.contains(NSEventModifierFlags::NSControlKeyMask); + let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask); + let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask); + let cmd = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask); + let fun = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask); + + Modifiers { + ctrl, + alt, + shift, + cmd, + fun, + } +} + impl Event { pub unsafe fn from_native(native_event: id, window_height: Option) -> Option { let event_type = native_event.eventType(); @@ -79,20 +96,9 @@ impl Event { } match event_type { - NSEventType::NSFlagsChanged => { - let modifiers = native_event.modifierFlags(); - let ctrl = modifiers.contains(NSEventModifierFlags::NSControlKeyMask); - let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask); - let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask); - let cmd = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask); - - Some(Self::ModifiersChanged(ModifiersChangedEvent { - ctrl, - alt, - shift, - cmd, - })) - } + NSEventType::NSFlagsChanged => Some(Self::ModifiersChanged(ModifiersChangedEvent { + modifiers: read_modifiers(native_event), + })), NSEventType::NSKeyDown => Some(Self::KeyDown(KeyDownEvent { keystroke: parse_keystroke(native_event), is_held: native_event.isARepeat() == YES, @@ -112,8 +118,6 @@ impl Event { // Other mouse buttons aren't tracked currently _ => return None, }; - let modifiers = native_event.modifierFlags(); - window_height.map(|window_height| { Self::MouseDown(MouseButtonEvent { button, @@ -121,10 +125,7 @@ impl Event { native_event.locationInWindow().x as f32, window_height - native_event.locationInWindow().y as f32, ), - ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), - alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask), - shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask), - cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask), + modifiers: read_modifiers(native_event), click_count: native_event.clickCount() as usize, }) }) @@ -143,24 +144,18 @@ impl Event { }; window_height.map(|window_height| { - let modifiers = native_event.modifierFlags(); Self::MouseUp(MouseButtonEvent { button, position: vec2f( native_event.locationInWindow().x as f32, window_height - native_event.locationInWindow().y as f32, ), - ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), - alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask), - shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask), - cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask), + modifiers: read_modifiers(native_event), click_count: native_event.clickCount() as usize, }) }) } NSEventType::NSScrollWheel => window_height.map(|window_height| { - let modifiers = native_event.modifierFlags(); - let phase = match native_event.phase() { NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => { Some(TouchPhase::Started) @@ -180,10 +175,7 @@ impl Event { ), phase, precise: native_event.hasPreciseScrollingDeltas() == YES, - ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), - alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask), - shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask), - cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask), + modifiers: read_modifiers(native_event), }) }), NSEventType::NSLeftMouseDragged @@ -200,32 +192,24 @@ impl Event { }; window_height.map(|window_height| { - let modifiers = native_event.modifierFlags(); Self::MouseMoved(MouseMovedEvent { pressed_button: Some(pressed_button), position: vec2f( native_event.locationInWindow().x as f32, window_height - native_event.locationInWindow().y as f32, ), - ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), - alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask), - shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask), - cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask), + modifiers: read_modifiers(native_event), }) }) } NSEventType::NSMouseMoved => window_height.map(|window_height| { - let modifiers = native_event.modifierFlags(); Self::MouseMoved(MouseMovedEvent { position: vec2f( native_event.locationInWindow().x as f32, window_height - native_event.locationInWindow().y as f32, ), pressed_button: None, - ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask), - alt: modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask), - shift: modifiers.contains(NSEventModifierFlags::NSShiftKeyMask), - cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask), + modifiers: read_modifiers(native_event), }) }), _ => None, diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 1ce42db9ed68d3332d3bb931e0f49c58748cc09e..688c0eabcfed27a2cd344bd510ead37cc979713d 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -981,25 +981,13 @@ extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) { }) => { window_state_borrow.synthetic_drag_counter += 1; } - Event::ModifiersChanged(ModifiersChangedEvent { - ctrl, - alt, - shift, - cmd, - }) => { + Event::ModifiersChanged(ModifiersChangedEvent { modifiers }) => { // Only raise modifiers changed event when they have actually changed if let Some(Event::ModifiersChanged(ModifiersChangedEvent { - ctrl: prev_ctrl, - alt: prev_alt, - shift: prev_shift, - cmd: prev_cmd, + modifiers: prev_modifiers, })) = &window_state_borrow.previous_modifiers_changed_event { - if prev_ctrl == ctrl - && prev_alt == alt - && prev_shift == shift - && prev_cmd == cmd - { + if prev_modifiers == modifiers { return; } }