Add fn modifier to modifier keys in gpui and refactor platform events to use a single modifiers struct
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use derive_more::Deref;
|
||||
|
||||
use crate::{geometry::vector::Vector2F, keymap::Keystroke};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -11,12 +13,19 @@ 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, Deref)]
|
||||
pub struct ModifiersChangedEvent {
|
||||
#[deref]
|
||||
pub modifiers: Modifiers,
|
||||
}
|
||||
|
||||
/// The phase of a touch motion event.
|
||||
@@ -28,15 +37,13 @@ pub enum TouchPhase {
|
||||
Ended,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[derive(Clone, Copy, Debug, Default, Deref)]
|
||||
pub struct ScrollWheelEvent {
|
||||
pub position: Vector2F,
|
||||
pub delta: Vector2F,
|
||||
pub precise: bool,
|
||||
pub ctrl: bool,
|
||||
pub alt: bool,
|
||||
pub shift: bool,
|
||||
pub cmd: bool,
|
||||
#[deref]
|
||||
pub modifiers: Modifiers,
|
||||
/// If the platform supports returning the phase of a scroll wheel event, it will be stored here
|
||||
pub phase: Option<TouchPhase>,
|
||||
}
|
||||
@@ -79,25 +86,21 @@ impl Default for MouseButton {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[derive(Clone, Copy, Debug, Default, Deref)]
|
||||
pub struct MouseButtonEvent {
|
||||
pub button: MouseButton,
|
||||
pub position: Vector2F,
|
||||
pub ctrl: bool,
|
||||
pub alt: bool,
|
||||
pub shift: bool,
|
||||
pub cmd: bool,
|
||||
#[deref]
|
||||
pub modifiers: Modifiers,
|
||||
pub click_count: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[derive(Clone, Copy, Debug, Default, Deref)]
|
||||
pub struct MouseMovedEvent {
|
||||
pub position: Vector2F,
|
||||
pub pressed_button: Option<MouseButton>,
|
||||
pub ctrl: bool,
|
||||
pub cmd: bool,
|
||||
pub alt: bool,
|
||||
pub shift: bool,
|
||||
#[deref]
|
||||
pub modifiers: Modifiers,
|
||||
}
|
||||
|
||||
impl MouseMovedEvent {
|
||||
@@ -105,10 +108,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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<str> {
|
||||
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<f32>) -> Option<Self> {
|
||||
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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user