on master help sign in
ignore Enter keypress when menu has no selection (#1704)
39f7ba3Michael Davis
helix-term/src/ui/menu.rs+7−5
@@ -1,5 +1,5 @@
1use crate::{
1use crate::{
2− compositor::{Component, Compositor, Context, EventResult},
2+ compositor::{Callback, Component, Compositor, Context, EventResult},
3 ctrl, key, shift,
3 ctrl, key, shift,
4};
4};
5use crossterm::event::Event;
5use crossterm::event::Event;
@@ -205,16 +205,16 @@ impl<T: Item + 'static> Component for Menu<T> {
205 _ => return EventResult::Ignored(None),
205 _ => return EventResult::Ignored(None),
206 };
206 };
207
207
208− let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| {
208+ let close_fn: Option<Callback> = Some(Box::new(|compositor: &mut Compositor, _| {
209 // remove the layer
209 // remove the layer
210 compositor.pop();
210 compositor.pop();
211− })));
211+ }));
212
212
213 match event.into() {
213 match event.into() {
214 // esc or ctrl-c aborts the completion and closes the menu
214 // esc or ctrl-c aborts the completion and closes the menu
215 key!(Esc) | ctrl!('c') => {
215 key!(Esc) | ctrl!('c') => {
216 (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
216 (self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort);
217− return close_fn;
217+ return EventResult::Consumed(close_fn);
218 }
218 }
219 // arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc)
219 // arrow up/ctrl-p/shift-tab prev completion choice (including updating the doc)
220 shift!(Tab) | key!(Up) | ctrl!('p') | ctrl!('k') => {
220 shift!(Tab) | key!(Up) | ctrl!('p') | ctrl!('k') => {
@@ -231,8 +231,10 @@ impl<T: Item + 'static> Component for Menu<T> {
231 key!(Enter) => {
231 key!(Enter) => {
232 if let Some(selection) = self.selection() {
232 if let Some(selection) = self.selection() {
233 (self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate);
233 (self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate);
234+ return EventResult::Consumed(close_fn);
235+ } else {
236+ return EventResult::Ignored(close_fn);
234 }
237 }
235− return close_fn;
236 }
238 }
237 // KeyEvent {
239 // KeyEvent {
238 // code: KeyCode::Char(c),
240 // code: KeyCode::Char(c),