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},
platform::CursorStyle,
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,
MutableAppContext, PaintContext, Quad, Scene, ScrollWheelEvent, SizeConstraint, ViewContext,
WeakViewHandle,
@ -278,21 +278,6 @@ impl EditorElement {
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 {
cx.dispatch_action(CmdChanged { cmd_down: cmd });
false

View file

@ -141,13 +141,7 @@ impl<'a> EditorTestContext<'a> {
pub fn simulate_keystroke(&mut self, keystroke_text: &str) {
let keystroke = Keystroke::parse(keystroke_text).unwrap();
let input = if keystroke.modified() {
None
} else {
Some(keystroke.key.clone())
};
self.cx
.dispatch_keystroke(self.window_id, keystroke, input, false);
self.cx.dispatch_keystroke(self.window_id, keystroke, false);
}
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);
}
pub fn dispatch_keystroke(
&mut self,
window_id: usize,
keystroke: Keystroke,
input: Option<String>,
is_held: bool,
) {
pub fn dispatch_keystroke(&mut self, window_id: usize, keystroke: Keystroke, is_held: bool) {
self.cx.borrow_mut().update(|cx| {
let presenter = cx
.presenters_and_platform_windows
@ -515,14 +509,9 @@ impl TestAppContext {
let dispatch_path = presenter.borrow().dispatch_path(cx.as_ref());
if !cx.dispatch_keystroke(window_id, dispatch_path, &keystroke) {
presenter.borrow_mut().dispatch_event(
Event::KeyDown(KeyDownEvent {
keystroke,
input,
is_held,
}),
cx,
);
presenter
.borrow_mut()
.dispatch_event(Event::KeyDown(KeyDownEvent { keystroke, is_held }), cx);
}
});
}

View file

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

View file

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

View file

@ -279,7 +279,7 @@ struct WindowState {
scene_to_render: Option<Scene>,
renderer: Renderer,
command_queue: metal::CommandQueue,
last_fresh_keydown: Option<(Keystroke, Option<String>)>,
last_fresh_keydown: Option<Keystroke>,
layer: id,
traffic_light_position: Option<Vector2F>,
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 {
window_state_borrow.pending_keydown_event = match 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
// were released.
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 mut window_state_borrow = window_state.as_ref().borrow_mut();
let chars = ".".to_string();
let keystroke = Keystroke {
cmd: true,
ctrl: false,
alt: false,
shift: false,
key: chars.clone(),
key: ".".into(),
};
let event = Event::KeyDown(KeyDownEvent {
keystroke: keystroke.clone(),
input: Some(chars.clone()),
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() {
drop(window_state_borrow);
callback(event);