helix-mods/helix-term/src/compositor.rs

132 lines
4.1 KiB
Rust
Raw Normal View History

2020-12-06 03:53:58 +01:00
// Features:
// Tracks currently focused component which receives all input
// Event loop is external as opposed to cursive-rs
// Calls render on the component and translates screen coords to local component coords
//
// TODO:
// Q: where is the Application state stored? do we store it into an external static var?
// A: probably makes sense to initialize the editor into a `static Lazy<>` global var.
//
// Q: how do we composit nested structures? There should be sub-components/views
//
// Each component declares it's own size constraints and gets fitted based on it's parent.
// Q: how does this work with popups?
// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
use crossterm::event::Event;
2020-12-13 05:29:34 +01:00
use helix_core::Position;
2020-12-06 03:53:58 +01:00
use smol::Executor;
use tui::buffer::Buffer as Surface;
2020-12-13 04:23:50 +01:00
use tui::layout::Rect;
2020-12-06 03:53:58 +01:00
2020-12-21 08:23:05 +01:00
pub type Callback = Box<dyn FnOnce(&mut Compositor, &mut Editor)>;
2020-12-07 07:18:37 +01:00
// --> EventResult should have a callback that takes a context with methods like .popup(),
// .prompt() etc. That way we can abstract it from the renderer.
// Q: How does this interact with popups where we need to be able to specify the rendering of the
// popup?
// A: It could just take a textarea.
//
// If Compositor was specified in the callback that's then problematic because of
2020-12-07 07:18:37 +01:00
// Cursive-inspired
pub enum EventResult {
2020-12-07 07:18:37 +01:00
Ignored,
Consumed(Option<Callback>),
}
use helix_view::{Editor, View};
// shared with commands.rs
2020-12-13 04:23:50 +01:00
pub struct Context<'a> {
pub editor: &'a mut Editor,
2020-12-13 04:23:50 +01:00
pub executor: &'static smol::Executor<'static>,
}
pub trait Component {
2020-12-06 03:53:58 +01:00
/// Process input events, return true if handled.
fn handle_event(&mut self, event: Event, ctx: &mut Context) -> EventResult;
2020-12-06 03:53:58 +01:00
// , args: ()
/// Should redraw? Useful for saving redraw cycles if we know component didn't change.
fn should_update(&self) -> bool {
true
}
2020-12-13 04:23:50 +01:00
fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
2020-12-13 05:29:34 +01:00
2021-02-09 07:40:30 +01:00
fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
2020-12-13 05:29:34 +01:00
None
}
2020-12-06 03:53:58 +01:00
}
// For v1:
// Child views are something each view needs to handle on it's own for now, positioning and sizing
// options, focus tracking. In practice this is simple: we only will need special solving for
// splits etc
// impl Editor {
// fn render(&mut self, surface: &mut Surface, args: ()) {
// // compute x, y, w, h rects for sub-views!
// // get surface area
// // get constraints for textarea, statusbar
// // -> cassowary-rs
// // first render textarea
// // then render statusbar
// }
// }
// usecases to consider:
// - a single view with subviews (textarea + statusbar)
// - a popup panel / dialog with it's own interactions
// - an autocomplete popup that doesn't change focus
pub struct Compositor {
2020-12-06 03:53:58 +01:00
layers: Vec<Box<dyn Component>>,
}
impl Compositor {
pub fn new() -> Self {
Self { layers: Vec::new() }
}
pub fn push(&mut self, layer: Box<dyn Component>) {
self.layers.push(layer);
}
pub fn pop(&mut self) {
self.layers.pop();
}
pub fn handle_event(&mut self, event: Event, cx: &mut Context) -> bool {
2021-02-09 07:40:30 +01:00
// 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() {
match layer.handle_event(event, cx) {
EventResult::Consumed(Some(callback)) => {
2020-12-21 08:23:05 +01:00
callback(self, cx.editor);
2021-02-09 07:40:30 +01:00
return true;
}
2021-02-09 07:40:30 +01:00
EventResult::Consumed(None) => return true,
EventResult::Ignored => false,
};
2020-12-06 03:53:58 +01:00
}
false
2020-12-06 03:53:58 +01:00
}
2020-12-13 04:23:50 +01:00
pub fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
for layer in &self.layers {
layer.render(area, surface, cx)
2020-12-06 03:53:58 +01:00
}
}
2020-12-13 05:29:34 +01:00
2021-02-09 07:40:30 +01:00
pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Position {
2020-12-13 05:29:34 +01:00
for layer in self.layers.iter().rev() {
2021-02-09 07:40:30 +01:00
if let Some(pos) = layer.cursor_position(area, editor) {
2020-12-13 05:29:34 +01:00
return pos;
}
}
panic!("No layer returned a position!");
}
2020-12-06 03:53:58 +01:00
}