Remove input from KeyDownEvent

This commit is contained in:
Antonio Scandurra 2022-07-21 14:29:27 +02:00
parent f170582c26
commit 101a0663d3
6 changed files with 16 additions and 79 deletions

View file

@ -24,7 +24,7 @@ use gpui::{
json::{self, ToJson}, json::{self, ToJson},
platform::CursorStyle, platform::CursorStyle,
text_layout::{self, Line, RunStyle, TextLayoutCache}, text_layout::{self, Line, RunStyle, TextLayoutCache},
AppContext, Axis, Border, CursorRegion, Element, ElementBox, Event, EventContext, KeyDownEvent, AppContext, Axis, Border, CursorRegion, Element, ElementBox, Event, EventContext,
LayoutContext, ModifiersChangedEvent, MouseButton, MouseEvent, MouseMovedEvent, LayoutContext, ModifiersChangedEvent, MouseButton, MouseEvent, MouseMovedEvent,
MutableAppContext, PaintContext, Quad, Scene, ScrollWheelEvent, SizeConstraint, ViewContext, MutableAppContext, PaintContext, Quad, Scene, ScrollWheelEvent, SizeConstraint, ViewContext,
WeakViewHandle, WeakViewHandle,
@ -278,21 +278,6 @@ impl EditorElement {
true true
} }
fn key_down(&self, input: Option<&str>, cx: &mut EventContext) -> bool {
let view = self.view.upgrade(cx.app).unwrap();
if view.is_focused(cx.app) {
if let Some(input) = input {
cx.dispatch_action(Input(input.to_string()));
true
} else {
false
}
} else {
false
}
}
fn modifiers_changed(&self, cmd: bool, cx: &mut EventContext) -> bool { fn modifiers_changed(&self, cmd: bool, cx: &mut EventContext) -> bool {
cx.dispatch_action(CmdChanged { cmd_down: cmd }); cx.dispatch_action(CmdChanged { cmd_down: cmd });
false false

View file

@ -141,13 +141,7 @@ impl<'a> EditorTestContext<'a> {
pub fn simulate_keystroke(&mut self, keystroke_text: &str) { pub fn simulate_keystroke(&mut self, keystroke_text: &str) {
let keystroke = Keystroke::parse(keystroke_text).unwrap(); let keystroke = Keystroke::parse(keystroke_text).unwrap();
let input = if keystroke.modified() { self.cx.dispatch_keystroke(self.window_id, keystroke, false);
None
} else {
Some(keystroke.key.clone())
};
self.cx
.dispatch_keystroke(self.window_id, keystroke, input, false);
} }
pub fn simulate_keystrokes<const COUNT: usize>(&mut self, keystroke_texts: [&str; COUNT]) { pub fn simulate_keystrokes<const COUNT: usize>(&mut self, keystroke_texts: [&str; COUNT]) {

View file

@ -498,13 +498,7 @@ impl TestAppContext {
self.cx.borrow_mut().dispatch_global_action(action); self.cx.borrow_mut().dispatch_global_action(action);
} }
pub fn dispatch_keystroke( pub fn dispatch_keystroke(&mut self, window_id: usize, keystroke: Keystroke, is_held: bool) {
&mut self,
window_id: usize,
keystroke: Keystroke,
input: Option<String>,
is_held: bool,
) {
self.cx.borrow_mut().update(|cx| { self.cx.borrow_mut().update(|cx| {
let presenter = cx let presenter = cx
.presenters_and_platform_windows .presenters_and_platform_windows
@ -515,14 +509,9 @@ impl TestAppContext {
let dispatch_path = presenter.borrow().dispatch_path(cx.as_ref()); let dispatch_path = presenter.borrow().dispatch_path(cx.as_ref());
if !cx.dispatch_keystroke(window_id, dispatch_path, &keystroke) { if !cx.dispatch_keystroke(window_id, dispatch_path, &keystroke) {
presenter.borrow_mut().dispatch_event( presenter
Event::KeyDown(KeyDownEvent { .borrow_mut()
keystroke, .dispatch_event(Event::KeyDown(KeyDownEvent { keystroke, is_held }), cx);
input,
is_held,
}),
cx,
);
} }
}); });
} }

View file

@ -3,14 +3,12 @@ use crate::{geometry::vector::Vector2F, keymap::Keystroke};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct KeyDownEvent { pub struct KeyDownEvent {
pub keystroke: Keystroke, pub keystroke: Keystroke,
pub input: Option<String>,
pub is_held: bool, pub is_held: bool,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct KeyUpEvent { pub struct KeyUpEvent {
pub keystroke: Keystroke, pub keystroke: Keystroke,
pub input: Option<String>,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View file

@ -83,10 +83,8 @@ impl Event {
let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask); let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask);
let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask); let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask);
let cmd = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask); let cmd = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask);
let function = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask);
let (unmodified_chars, input) = get_key_text(native_event, cmd, ctrl, function)?;
let unmodified_chars = get_key_text(native_event)?;
Some(Self::KeyDown(KeyDownEvent { Some(Self::KeyDown(KeyDownEvent {
keystroke: Keystroke { keystroke: Keystroke {
ctrl, ctrl,
@ -95,7 +93,6 @@ impl Event {
cmd, cmd,
key: unmodified_chars.into(), key: unmodified_chars.into(),
}, },
input,
is_held: native_event.isARepeat() == YES, is_held: native_event.isARepeat() == YES,
})) }))
} }
@ -105,10 +102,7 @@ impl Event {
let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask); let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask);
let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask); let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask);
let cmd = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask); let cmd = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask);
let function = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask); let unmodified_chars = get_key_text(native_event)?;
let (unmodified_chars, input) = get_key_text(native_event, cmd, ctrl, function)?;
Some(Self::KeyUp(KeyUpEvent { Some(Self::KeyUp(KeyUpEvent {
keystroke: Keystroke { keystroke: Keystroke {
ctrl, ctrl,
@ -117,7 +111,6 @@ impl Event {
cmd, cmd,
key: unmodified_chars.into(), key: unmodified_chars.into(),
}, },
input,
})) }))
} }
NSEventType::NSLeftMouseDown NSEventType::NSLeftMouseDown
@ -238,27 +231,18 @@ impl Event {
} }
} }
unsafe fn get_key_text( unsafe fn get_key_text(native_event: id) -> Option<&'static str> {
native_event: id,
cmd: bool,
ctrl: bool,
function: bool,
) -> Option<(&'static str, Option<String>)> {
let unmodified_chars = let unmodified_chars =
CStr::from_ptr(native_event.charactersIgnoringModifiers().UTF8String() as *mut c_char) CStr::from_ptr(native_event.charactersIgnoringModifiers().UTF8String() as *mut c_char)
.to_str() .to_str()
.unwrap(); .unwrap();
let mut input = None;
let first_char = unmodified_chars.chars().next()?; let first_char = unmodified_chars.chars().next()?;
use cocoa::appkit::*; use cocoa::appkit::*;
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
let unmodified_chars = match first_char as u16 { let unmodified_chars = match first_char as u16 {
SPACE_KEY => { SPACE_KEY => "space",
input = Some(" ".to_string());
"space"
}
BACKSPACE_KEY => "backspace", BACKSPACE_KEY => "backspace",
ENTER_KEY | NUMPAD_ENTER_KEY => "enter", ENTER_KEY | NUMPAD_ENTER_KEY => "enter",
ESCAPE_KEY => "escape", ESCAPE_KEY => "escape",
@ -284,19 +268,8 @@ unsafe fn get_key_text(
NSF10FunctionKey => "f10", NSF10FunctionKey => "f10",
NSF11FunctionKey => "f11", NSF11FunctionKey => "f11",
NSF12FunctionKey => "f12", NSF12FunctionKey => "f12",
_ => unmodified_chars,
_ => {
if !cmd && !ctrl && !function {
input = Some(
CStr::from_ptr(native_event.characters().UTF8String() as *mut c_char)
.to_str()
.unwrap()
.into(),
);
}
unmodified_chars
}
}; };
Some((unmodified_chars, input)) Some(unmodified_chars)
} }

View file

@ -279,7 +279,7 @@ struct WindowState {
scene_to_render: Option<Scene>, scene_to_render: Option<Scene>,
renderer: Renderer, renderer: Renderer,
command_queue: metal::CommandQueue, command_queue: metal::CommandQueue,
last_fresh_keydown: Option<(Keystroke, Option<String>)>, last_fresh_keydown: Option<Keystroke>,
layer: id, layer: id,
traffic_light_position: Option<Vector2F>, traffic_light_position: Option<Vector2F>,
previous_modifiers_changed_event: Option<Event>, previous_modifiers_changed_event: Option<Event>,
@ -699,7 +699,7 @@ extern "C" fn handle_key_equivalent(this: &Object, _: Sel, native_event: id) ->
if let Some(event) = event { if let Some(event) = event {
window_state_borrow.pending_keydown_event = match event { window_state_borrow.pending_keydown_event = match event {
Event::KeyDown(event) => { Event::KeyDown(event) => {
let keydown = (event.keystroke.clone(), event.input.clone()); let keydown = event.keystroke.clone();
// Ignore events from held-down keys after some of the initially-pressed keys // Ignore events from held-down keys after some of the initially-pressed keys
// were released. // were released.
if event.is_held { if event.is_held {
@ -812,21 +812,19 @@ extern "C" fn cancel_operation(this: &Object, _sel: Sel, _sender: id) {
let window_state = unsafe { get_window_state(this) }; let window_state = unsafe { get_window_state(this) };
let mut window_state_borrow = window_state.as_ref().borrow_mut(); let mut window_state_borrow = window_state.as_ref().borrow_mut();
let chars = ".".to_string();
let keystroke = Keystroke { let keystroke = Keystroke {
cmd: true, cmd: true,
ctrl: false, ctrl: false,
alt: false, alt: false,
shift: false, shift: false,
key: chars.clone(), key: ".".into(),
}; };
let event = Event::KeyDown(KeyDownEvent { let event = Event::KeyDown(KeyDownEvent {
keystroke: keystroke.clone(), keystroke: keystroke.clone(),
input: Some(chars.clone()),
is_held: false, is_held: false,
}); });
window_state_borrow.last_fresh_keydown = Some((keystroke, Some(chars))); window_state_borrow.last_fresh_keydown = Some(keystroke);
if let Some(mut callback) = window_state_borrow.event_callback.take() { if let Some(mut callback) = window_state_borrow.event_callback.take() {
drop(window_state_borrow); drop(window_state_borrow);
callback(event); callback(event);