@@ -1,14 +1,3 @@
1−// Features:
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
5−//
6−// TODO:
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.
9−//
10−// Q: how do we composit nested structures? There should be sub-components/views
11−//
121// Each component declares it's own size constraints and gets fitted based on it's parent.
132// Q: how does this work with popups?
143// cursive does compositor.screen_mut().add_layer_at(pos::absolute(x, y), <component>)
@@ -36,7 +25,7 @@ pub enum EventResult {
3625}
3726
3827use helix_view::{Editor, View};
39−// shared with commands.rs
28+
4029pub struct Context<'a> {
4130 pub editor: &'a mut Editor,
4231 pub executor: &'static smol::Executor<'static>,
@@ -54,46 +43,53 @@ pub trait Component {
5443 true
5544 }
5645
46+ /// Render the component onto the provided surface.
5747 fn render(&self, area: Rect, frame: &mut Surface, ctx: &mut Context);
5848
5949 fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
6050 None
6151 }
6252
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.
58+ //
59+ // TODO: for scrolling, the scroll wrapper should place a size + offset on the Context
60+ // that way render can use it
6461 None
6562 }
6663}
6764
68−// For v1:
69−// Child views are something each view needs to handle on it's own for now, positioning and sizing
70−// options, focus tracking. In practice this is simple: we only will need special solving for
71−// splits etc
72−
73−// impl Editor {
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
78−// // -> cassowary-rs
79−
80−// // first render textarea
81−// // then render statusbar
82−// }
83−// }
84−
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
65+use anyhow::Error;
66+use std::io::stdout;
67+use tui::backend::CrosstermBackend;
68+type Terminal = crate::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
8969
9070pub struct Compositor {
9171 layers: Vec<Box<dyn Component>>,
72+ terminal: Terminal,
9273}
9374
9475impl Compositor {
95− pub fn new() -> Self {
96− Self { layers: Vec::new() }
76+ pub fn new() -> Result<Self, Error> {
77+ let backend = CrosstermBackend::new(stdout());
78+ let mut terminal = Terminal::new(backend)?;
79+ Ok(Self {
80+ layers: Vec::new(),
81+ terminal,
82+ })
83+ }
84+
85+ pub fn size(&self) -> Rect {
86+ self.terminal.size().expect("couldn't get terminal size")
87+ }
88+
89+ pub fn resize(&mut self, width: u16, height: u16) {
90+ self.terminal
91+ .resize(Rect::new(0, 0, width, height))
92+ .expect("Unable to resize terminal")
9793 }
9894
9995 pub fn push(&mut self, layer: Box<dyn Component>) {
@@ -120,10 +116,19 @@ impl Compositor {
120116 false
121117 }
122118
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();
122+
124123 for layer in &self.layers {
125124 layer.render(area, surface, cx)
126125 }
126+
127+ let pos = self
128+ .cursor_position(area, cx.editor)
129+ .map(|pos| (pos.col as u16, pos.row as u16));
130+
131+ self.terminal.draw(pos);
127132 }
128133
129134 pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {