Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close popups automatically #1285

Merged
Merged
32 changes: 24 additions & 8 deletions helix-term/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Context)>;

// Cursive-inspired
pub enum EventResult {
Ignored,
Ignored(Option<Callback>),
Consumed(Option<Callback>),
}

Expand All @@ -36,7 +36,7 @@ pub struct Context<'a> {
pub trait Component: Any + AnyComponent {
/// Process input events, return true if handled.
fn handle_event(&mut self, _event: Event, _ctx: &mut Context) -> EventResult {
EventResult::Ignored
EventResult::Ignored(None)
}
// , args: ()

Expand Down Expand Up @@ -136,19 +136,35 @@ impl Compositor {
keys.push(key.into());
}

let mut callbacks = Vec::new();
let mut consumed = false;

// propagate events through the layers until we either find a layer that consumes it or we
// run out of layers (event bubbling)
for layer in self.layers.iter_mut().rev() {
bram209 marked this conversation as resolved.
Show resolved Hide resolved
bram209 marked this conversation as resolved.
Show resolved Hide resolved
match layer.handle_event(event, cx) {
consumed = match layer.handle_event(event, cx) {
EventResult::Consumed(Some(callback)) => {
callback(self, cx);
return true;
callbacks.push(callback);
true
}
EventResult::Consumed(None) => true,
bram209 marked this conversation as resolved.
Show resolved Hide resolved
EventResult::Ignored(Some(callback)) => {
callbacks.push(callback);
false
}
EventResult::Consumed(None) => return true,
EventResult::Ignored => false,
EventResult::Ignored(None) => false,
};

if consumed {
break;
}
}
false

for callback in callbacks {
callback(self, cx)
}

consumed
}

pub fn render(&mut self, cx: &mut Context) {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl Component for Completion {
code: KeyCode::Esc, ..
}) = event
{
return EventResult::Ignored;
return EventResult::Ignored(None);
}
self.popup.handle_event(event, cx)
}
Expand Down
18 changes: 9 additions & 9 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ impl EditorView {
return EventResult::Consumed(None);
}

EventResult::Ignored
EventResult::Ignored(None)
}

MouseEvent {
Expand All @@ -820,7 +820,7 @@ impl EditorView {

let pos = match view.pos_at_screen_coords(doc, row, column) {
Some(pos) => pos,
None => return EventResult::Ignored,
None => return EventResult::Ignored(None),
};

let mut selection = doc.selection(view.id).clone();
Expand Down Expand Up @@ -851,7 +851,7 @@ impl EditorView {

match result {
Some(view_id) => cxt.editor.tree.focus = view_id,
None => return EventResult::Ignored,
None => return EventResult::Ignored(None),
}

let offset = cxt.editor.config.scroll_lines.abs() as usize;
Expand All @@ -867,14 +867,14 @@ impl EditorView {
..
} => {
if !cxt.editor.config.middle_click_paste {
return EventResult::Ignored;
return EventResult::Ignored(None);
}

let (view, doc) = current!(cxt.editor);
let range = doc.selection(view.id).primary();

if range.to() - range.from() <= 1 {
return EventResult::Ignored;
return EventResult::Ignored(None);
}

commands::MappableCommand::yank_main_selection_to_primary_clipboard.execute(cxt);
Expand All @@ -891,7 +891,7 @@ impl EditorView {
} => {
let editor = &mut cxt.editor;
if !editor.config.middle_click_paste {
return EventResult::Ignored;
return EventResult::Ignored(None);
}

if modifiers == crossterm::event::KeyModifiers::ALT {
Expand All @@ -914,10 +914,10 @@ impl EditorView {
return EventResult::Consumed(None);
}

EventResult::Ignored
EventResult::Ignored(None)
}

_ => EventResult::Ignored,
_ => EventResult::Ignored(None),
}
}
}
Expand Down Expand Up @@ -1003,7 +1003,7 @@ impl Component for EditorView {
// if the command consumed the last view, skip the render.
// on the next loop cycle the Application will then terminate.
if cxt.editor.should_close() {
return EventResult::Ignored;
return EventResult::Ignored(None);
}

let (view, doc) = current!(cxt.editor);
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl<T: Item + 'static> Component for Menu<T> {
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
let event = match event {
Event::Key(event) => event,
_ => return EventResult::Ignored,
_ => return EventResult::Ignored(None),
};

let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| {
Expand Down Expand Up @@ -237,7 +237,7 @@ impl<T: Item + 'static> Component for Menu<T> {
// for some events, we want to process them but send ignore, specifically all input except
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
// EventResult::Consumed(None)
EventResult::Ignored
EventResult::Ignored(None)
}

fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl<T: 'static> Component for Picker<T> {
let key_event = match event {
Event::Key(event) => event,
Event::Resize(..) => return EventResult::Consumed(None),
_ => return EventResult::Ignored,
_ => return EventResult::Ignored(None),
};

let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| {
Expand Down
22 changes: 15 additions & 7 deletions helix-term/src/ui/popup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
compositor::{Component, Compositor, Context, EventResult},
compositor::{Callback, Component, Context, EventResult},
ctrl, key,
};
use crossterm::event::Event;
Expand Down Expand Up @@ -95,19 +95,19 @@ impl<T: Component> Component for Popup<T> {
Event::Key(event) => event,
Event::Resize(_, _) => {
// TODO: calculate inner area, call component's handle_event with that area
return EventResult::Ignored;
return EventResult::Ignored(None);
}
_ => return EventResult::Ignored,
_ => return EventResult::Ignored(None),
};

let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| {
let close_fn: Callback = Box::new(|compositor, _| {
// remove the layer
compositor.pop();
})));
});

match key.into() {
// esc or ctrl-c aborts the completion and closes the menu
key!(Esc) | ctrl!('c') => close_fn,
key!(Esc) | ctrl!('c') => EventResult::Consumed(Some(close_fn)),
ctrl!('d') => {
self.scroll(self.size.1 as usize / 2, true);
EventResult::Consumed(None)
Expand All @@ -116,7 +116,15 @@ impl<T: Component> Component for Popup<T> {
self.scroll(self.size.1 as usize / 2, false);
EventResult::Consumed(None)
}
_ => self.contents.handle_event(event, cx),
_ => {
let contents_event_result = self.contents.handle_event(event, cx);

if let EventResult::Ignored(None) = contents_event_result {
bram209 marked this conversation as resolved.
Show resolved Hide resolved
EventResult::Ignored(Some(close_fn))
} else {
contents_event_result
}
}
}
// for some events, we want to process them but send ignore, specifically all input except
// tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl Component for Prompt {
let event = match event {
Event::Key(event) => event,
Event::Resize(..) => return EventResult::Consumed(None),
_ => return EventResult::Ignored,
_ => return EventResult::Ignored(None),
};

let close_fn = EventResult::Consumed(Some(Box::new(|compositor: &mut Compositor, _| {
Expand Down