Merge pull request #475 from zed-industries/filter-input
Don't insert input in editor when control keys are pressed
This commit is contained in:
commit
882756d467
4 changed files with 34 additions and 32 deletions
|
@ -16,7 +16,6 @@ use gpui::{
|
||||||
PathBuilder,
|
PathBuilder,
|
||||||
},
|
},
|
||||||
json::{self, ToJson},
|
json::{self, ToJson},
|
||||||
keymap::Keystroke,
|
|
||||||
text_layout::{self, RunStyle, TextLayoutCache},
|
text_layout::{self, RunStyle, TextLayoutCache},
|
||||||
AppContext, Axis, Border, Element, ElementBox, Event, EventContext, LayoutContext,
|
AppContext, Axis, Border, Element, ElementBox, Event, EventContext, LayoutContext,
|
||||||
MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
|
MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewContext, WeakViewHandle,
|
||||||
|
@ -156,19 +155,15 @@ impl EditorElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn key_down(&self, chars: &str, keystroke: &Keystroke, cx: &mut EventContext) -> bool {
|
fn key_down(&self, input: Option<&str>, cx: &mut EventContext) -> bool {
|
||||||
let view = self.view.upgrade(cx.app).unwrap();
|
let view = self.view.upgrade(cx.app).unwrap();
|
||||||
|
|
||||||
if view.is_focused(cx.app) {
|
if view.is_focused(cx.app) {
|
||||||
if chars.is_empty() {
|
if let Some(input) = input {
|
||||||
false
|
cx.dispatch_action(Input(input.to_string()));
|
||||||
|
true
|
||||||
} else {
|
} else {
|
||||||
if chars.chars().any(|c| c.is_control()) || keystroke.cmd || keystroke.ctrl {
|
false
|
||||||
false
|
|
||||||
} else {
|
|
||||||
cx.dispatch_action(Input(chars.to_string()));
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -1105,9 +1100,7 @@ impl Element for EditorElement {
|
||||||
delta,
|
delta,
|
||||||
precise,
|
precise,
|
||||||
} => self.scroll(*position, *delta, *precise, layout, paint, cx),
|
} => self.scroll(*position, *delta, *precise, layout, paint, cx),
|
||||||
Event::KeyDown {
|
Event::KeyDown { input, .. } => self.key_down(input.as_deref(), cx),
|
||||||
chars, keystroke, ..
|
|
||||||
} => self.key_down(chars, keystroke, cx),
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::{geometry::vector::Vector2F, keymap::Keystroke};
|
||||||
pub enum Event {
|
pub enum Event {
|
||||||
KeyDown {
|
KeyDown {
|
||||||
keystroke: Keystroke,
|
keystroke: Keystroke,
|
||||||
chars: String,
|
input: Option<String>,
|
||||||
is_held: bool,
|
is_held: bool,
|
||||||
},
|
},
|
||||||
ScrollWheel {
|
ScrollWheel {
|
||||||
|
|
|
@ -21,11 +21,13 @@ impl Event {
|
||||||
|
|
||||||
match event_type {
|
match event_type {
|
||||||
NSEventType::NSKeyDown => {
|
NSEventType::NSKeyDown => {
|
||||||
|
let mut input = None;
|
||||||
let modifiers = native_event.modifierFlags();
|
let modifiers = native_event.modifierFlags();
|
||||||
let unmodified_chars = native_event.charactersIgnoringModifiers();
|
let unmodified_chars = CStr::from_ptr(
|
||||||
let unmodified_chars = CStr::from_ptr(unmodified_chars.UTF8String() as *mut c_char)
|
native_event.charactersIgnoringModifiers().UTF8String() as *mut c_char,
|
||||||
.to_str()
|
)
|
||||||
.unwrap();
|
.to_str()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let unmodified_chars = if let Some(first_char) = unmodified_chars.chars().next() {
|
let unmodified_chars = if let Some(first_char) = unmodified_chars.chars().next() {
|
||||||
use cocoa::appkit::*;
|
use cocoa::appkit::*;
|
||||||
|
@ -38,7 +40,10 @@ impl Event {
|
||||||
|
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
match first_char as u16 {
|
match first_char as u16 {
|
||||||
SPACE_KEY => "space",
|
SPACE_KEY => {
|
||||||
|
input = Some(" ".to_string());
|
||||||
|
"space"
|
||||||
|
}
|
||||||
BACKSPACE_KEY => "backspace",
|
BACKSPACE_KEY => "backspace",
|
||||||
ENTER_KEY => "enter",
|
ENTER_KEY => "enter",
|
||||||
ESCAPE_KEY => "escape",
|
ESCAPE_KEY => "escape",
|
||||||
|
@ -65,18 +70,22 @@ impl Event {
|
||||||
NSF11FunctionKey => "f11",
|
NSF11FunctionKey => "f11",
|
||||||
NSF12FunctionKey => "f12",
|
NSF12FunctionKey => "f12",
|
||||||
|
|
||||||
_ => unmodified_chars,
|
_ => {
|
||||||
|
input = Some(
|
||||||
|
CStr::from_ptr(
|
||||||
|
native_event.characters().UTF8String() as *mut c_char
|
||||||
|
)
|
||||||
|
.to_str()
|
||||||
|
.unwrap()
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
unmodified_chars
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
let chars = native_event.characters();
|
|
||||||
let chars = CStr::from_ptr(chars.UTF8String() as *mut c_char)
|
|
||||||
.to_str()
|
|
||||||
.unwrap()
|
|
||||||
.into();
|
|
||||||
|
|
||||||
Some(Self::KeyDown {
|
Some(Self::KeyDown {
|
||||||
keystroke: Keystroke {
|
keystroke: Keystroke {
|
||||||
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
|
ctrl: modifiers.contains(NSEventModifierFlags::NSControlKeyMask),
|
||||||
|
@ -85,7 +94,7 @@ impl Event {
|
||||||
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
|
cmd: modifiers.contains(NSEventModifierFlags::NSCommandKeyMask),
|
||||||
key: unmodified_chars.into(),
|
key: unmodified_chars.into(),
|
||||||
},
|
},
|
||||||
chars,
|
input,
|
||||||
is_held: native_event.isARepeat() == YES,
|
is_held: native_event.isARepeat() == YES,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,7 +148,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, String)>,
|
last_fresh_keydown: Option<(Keystroke, Option<String>)>,
|
||||||
layer: id,
|
layer: id,
|
||||||
traffic_light_position: Option<Vector2F>,
|
traffic_light_position: Option<Vector2F>,
|
||||||
}
|
}
|
||||||
|
@ -517,11 +517,11 @@ extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) {
|
||||||
// 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.
|
||||||
Event::KeyDown {
|
Event::KeyDown {
|
||||||
chars,
|
input,
|
||||||
keystroke,
|
keystroke,
|
||||||
is_held,
|
is_held,
|
||||||
} => {
|
} => {
|
||||||
let keydown = (keystroke.clone(), chars.clone());
|
let keydown = (keystroke.clone(), input.clone());
|
||||||
if *is_held {
|
if *is_held {
|
||||||
if window_state_borrow.last_fresh_keydown.as_ref() != Some(&keydown) {
|
if window_state_borrow.last_fresh_keydown.as_ref() != Some(&keydown) {
|
||||||
return;
|
return;
|
||||||
|
@ -558,11 +558,11 @@ extern "C" fn cancel_operation(this: &Object, _sel: Sel, _sender: id) {
|
||||||
};
|
};
|
||||||
let event = Event::KeyDown {
|
let event = Event::KeyDown {
|
||||||
keystroke: keystroke.clone(),
|
keystroke: keystroke.clone(),
|
||||||
chars: chars.clone(),
|
input: Some(chars.clone()),
|
||||||
is_held: false,
|
is_held: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
window_state_borrow.last_fresh_keydown = Some((keystroke, chars));
|
window_state_borrow.last_fresh_keydown = Some((keystroke, Some(chars)));
|
||||||
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);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue