pull/6/head
Blaž Hrastnik 4 years ago
parent 83f2c24115
commit be3c021046

@ -8,7 +8,7 @@ use helix_view::{
Document, Editor, Theme, View, Document, Editor, Theme, View,
}; };
use crate::compositor::{Component, Compositor}; use crate::compositor::{Component, Compositor, EventResult};
use log::{debug, info}; use log::{debug, info};
@ -383,7 +383,7 @@ impl EditorView {
} }
impl Component for EditorView { impl Component for EditorView {
fn handle_event(&mut self, event: Event, executor: &smol::Executor) -> bool { fn handle_event(&mut self, event: Event, executor: &smol::Executor) -> EventResult {
match event { match event {
Event::Resize(width, height) => { Event::Resize(width, height) => {
// TODO: simplistic ensure cursor in view for now // TODO: simplistic ensure cursor in view for now
@ -392,6 +392,7 @@ impl Component for EditorView {
view.size = (width, height); view.size = (width, height);
view.ensure_cursor_in_view() view.ensure_cursor_in_view()
}; };
EventResult::Consumed(None)
} }
Event::Key(event) => { Event::Key(event) => {
// if there's a prompt, it takes priority // if there's a prompt, it takes priority
@ -400,6 +401,7 @@ impl Component for EditorView {
.as_mut() .as_mut()
.unwrap() .unwrap()
.handle_input(event, &mut self.editor); .handle_input(event, &mut self.editor);
EventResult::Consumed(None)
} else if let Some(view) = self.editor.view_mut() { } else if let Some(view) = self.editor.view_mut() {
let keys = vec![event]; let keys = vec![event];
// TODO: sequences (`gg`) // TODO: sequences (`gg`)
@ -511,12 +513,13 @@ impl Component for EditorView {
} }
} }
} }
EventResult::Consumed(None)
} else {
EventResult::Ignored
} }
} }
Event::Mouse(_) => (), Event::Mouse(_) => EventResult::Ignored,
} }
true
} }
fn render(&mut self, renderer: &mut Renderer) { fn render(&mut self, renderer: &mut Renderer) {
const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@ -569,7 +572,6 @@ impl<'a> Application<'a> {
} }
fn render(&mut self) { fn render(&mut self) {
// v2:
self.renderer.surface.reset(); // reset is faster than allocating new empty surface self.renderer.surface.reset(); // reset is faster than allocating new empty surface
self.compositor.render(&mut self.renderer); // viewport, self.compositor.render(&mut self.renderer); // viewport,
self.renderer.draw_and_swap(); self.renderer.draw_and_swap();

@ -18,9 +18,17 @@ use crossterm::event::Event;
use smol::Executor; use smol::Executor;
use tui::buffer::Buffer as Surface; use tui::buffer::Buffer as Surface;
pub(crate) type Callback = Box<dyn Fn(&mut Compositor)>;
// Cursive-inspired
pub(crate) enum EventResult {
Ignored,
Consumed(Option<Callback>),
}
pub(crate) trait Component { pub(crate) trait Component {
/// Process input events, return true if handled. /// Process input events, return true if handled.
fn handle_event(&mut self, event: Event, executor: &Executor) -> bool; fn handle_event(&mut self, event: Event, executor: &Executor) -> EventResult;
// , args: () // , args: ()
/// Should redraw? Useful for saving redraw cycles if we know component didn't change. /// Should redraw? Useful for saving redraw cycles if we know component didn't change.

Loading…
Cancel
Save