@@ -1,14 +1,3 @@
2−// Tracks currently focused component which receives all input
3−// Event loop is external as opposed to cursive-rs
4−// Calls render on the component and translates screen coords to local component coords
7−// Q: where is the Application state stored? do we store it into an external static var?
8−// A: probably makes sense to initialize the editor into a `static Lazy<>` global var.
10−// Q: how do we composit nested structures? There should be sub-components/views
12// Each component declares it's own size constraints and gets fitted based on it's parent.
1// Each component declares it's own size constraints and gets fitted based on it's parent.
13// Q: how does this work with popups?
2// Q: how does this work with popups?
14// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
3// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
@@ -36,7 +25,7 @@ pub enum EventResult {
38use helix_view::{Editor, View};
27use helix_view::{Editor, View};
39−// shared with commands.rs
28+
40pub struct Context<'a> {
29pub struct Context<'a> {
41 pub editor: &'a mut Editor,
30 pub editor: &'a mut Editor,
42 pub executor: &'static smol::Executor<'static>,
31 pub executor: &'static smol::Executor<'static>,
@@ -54,46 +43,53 @@ pub trait Component {
46+ /// Render the component onto the provided surface.
57 fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
47 fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
59 fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
49 fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
63− fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
53+ /// May be used by the parent component to compute the child area.
54+ /// viewport is the maximum allowed area, and the child should stay within those bounds.
55+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
56+ // TODO: the compositor should trigger this on push_layer too so that we can use it as an
57+ // initializer there too.
59+ // TODO: for scrolling, the scroll wrapper should place a size + offset on the Context
60+ // that way render can use it
68−// For v1:
65+use anyhow::Error;
69−// Child views are something each view needs to handle on it's own for now, positioning and sizing
66+use std::io::stdout;
70−// options, focus tracking. In practice this is simple: we only will need special solving for
67+use tui::backend::CrosstermBackend;
71−// splits etc
68+type Terminal = crate::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
74−// fn render(&mut self, surface: &mut Surface, args: ()) {
75−// // compute x, y, w, h rects for sub-views!
76−// // get surface area
77−// // get constraints for textarea, statusbar
80−// // first render textarea
81−// // then render statusbar
85−// usecases to consider:
86−// - a single view with subviews (textarea + statusbar)
87−// - a popup panel / dialog with it's own interactions
88−// - an autocomplete popup that doesn't change focus
90pub struct Compositor {
70pub struct Compositor {
91 layers: Vec<Box<dyn Component>>,
71 layers: Vec<Box<dyn Component>>,
94impl Compositor {
75impl Compositor {
95− pub fn new() -> Self {
76+ pub fn new() -> Result<Self, Error> {
77+ let backend = CrosstermBackend::new(stdout());
78+ let mut terminal = Terminal::new(backend)?;
96− Self { layers: Vec::new() }
80+ layers: Vec::new(),
85+ pub fn size(&self) -> Rect {
86+ self.terminal.size().expect("couldn't get terminal size")
89+ pub fn resize(&mut self, width: u16, height: u16) {
91+ .resize(Rect::new(0, 0, width, height))
92+ .expect("Unable to resize terminal")
99 pub fn push(&mut self, layer: Box<dyn Component>) {
95 pub fn push(&mut self, layer: Box<dyn Component>) {
@@ -120,10 +116,19 @@ impl Compositor {
123− pub fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
119+ pub fn render(&mut self, cx: &mut Context) {
120+ let area = self.size();
121+ let surface = self.terminal.current_buffer_mut();
124 for layer in &self.layers {
123 for layer in &self.layers {
125 layer.render(area, surface, cx)
124 layer.render(area, surface, cx)
128+ .cursor_position(area, cx.editor)
129+ .map(|pos| (pos.col as u16, pos.row as u16));
131+ self.terminal.draw(pos);
129 pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
134 pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {