on master help sign in
ui: Move terminal into compositor, redo required_size hints.
5ea610cBlaž Hrastnik
helix-term/src/application.rs+6−18
@@ -23,23 +23,19 @@ use crossterm::{
23 execute, terminal,
23 execute, terminal,
24};
24};
25
25
26−use tui::{backend::CrosstermBackend, layout::Rect};
26+use tui::layout::Rect;
27−
28−type Terminal = crate::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
29
27
30pub struct Application {
28pub struct Application {
31 compositor: Compositor,
29 compositor: Compositor,
32 editor: Editor,
30 editor: Editor,
33− terminal: Terminal,
34
31
35 executor: &'static smol::Executor<'static>,
32 executor: &'static smol::Executor<'static>,
36}
33}
37
34
38impl Application {
35impl Application {
39 pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
36 pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
40− let backend = CrosstermBackend::new(stdout());
37+ let mut compositor = Compositor::new()?;
41− let mut terminal = Terminal::new(backend)?;
42− let size = terminal.size()?;
38+ let size = compositor.size();
43 let mut editor = Editor::new(size);
39 let mut editor = Editor::new(size);
44
40
45 let files = args.values_of_t::<PathBuf>("files").unwrap();
41 let files = args.values_of_t::<PathBuf>("files").unwrap();
@@ -47,12 +43,10 @@ impl Application {
47 editor.open(file, executor)?;
43 editor.open(file, executor)?;
48 }
44 }
49
45
50− let mut compositor = Compositor::new();
51 compositor.push(Box::new(ui::EditorView::new()));
46 compositor.push(Box::new(ui::EditorView::new()));
52
47
53 let mut app = Self {
48 let mut app = Self {
54 editor,
49 editor,
55− terminal,
56 compositor,
50 compositor,
57
51
58 executor,
52 executor,
@@ -64,17 +58,11 @@ impl Application {
64 fn render(&mut self) {
58 fn render(&mut self) {
65 let executor = &self.executor;
59 let executor = &self.executor;
66 let editor = &mut self.editor;
60 let editor = &mut self.editor;
67− let compositor = &self.compositor;
61+ let compositor = &mut self.compositor;
68
62
69 let mut cx = crate::compositor::Context { editor, executor };
63 let mut cx = crate::compositor::Context { editor, executor };
70− let area = self.terminal.size().unwrap();
71−
72− compositor.render(area, self.terminal.current_buffer_mut(), &mut cx);
73− let pos = compositor
74− .cursor_position(area, &editor)
75− .map(|pos| (pos.col as u16, pos.row as u16));
76
64
77− self.terminal.draw(pos);
65+ compositor.render(&mut cx);
78 }
66 }
79
67
80 pub async fn event_loop(&mut self) {
68 pub async fn event_loop(&mut self) {
@@ -107,7 +95,7 @@ impl Application {
107 // Handle key events
95 // Handle key events
108 let should_redraw = match event {
96 let should_redraw = match event {
109 Some(Ok(Event::Resize(width, height))) => {
97 Some(Ok(Event::Resize(width, height))) => {
110− self.terminal.resize(Rect::new(0, 0, width, height));
98+ self.compositor.resize(width, height);
111
99
112 self.compositor
100 self.compositor
113 .handle_event(Event::Resize(width, height), &mut cx)
101 .handle_event(Event::Resize(width, height), &mut cx)
helix-term/src/commands.rs+4−1
@@ -39,9 +39,12 @@ impl<'a> Context<'a> {
39 }
39 }
40
40
41 /// Push a new component onto the compositor.
41 /// Push a new component onto the compositor.
42− pub fn push_layer(&mut self, component: Box<dyn crate::compositor::Component>) {
42+ pub fn push_layer(&mut self, mut component: Box<dyn crate::compositor::Component>) {
43 self.callback = Some(Box::new(
43 self.callback = Some(Box::new(
44 |compositor: &mut Compositor, editor: &mut Editor| {
44 |compositor: &mut Compositor, editor: &mut Editor| {
45+ let size = compositor.size();
46+ // trigger required_size on init
47+ component.required_size((size.width, size.height));
45 compositor.push(component);
48 compositor.push(component);
46 },
49 },
47 ));
50 ));
helix-term/src/compositor.rs+42−37
@@ -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−//
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 {
36}
25}
37
26
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 {
54 true
43 true
55 }
44 }
56
45
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);
58
48
59 fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
49 fn cursor_position(&self, area: Rect, ctx: &Editor) -> Option<Position> {
60 None
50 None
61 }
51 }
62
52
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
64 None
61 None
65 }
62 }
66}
63}
67
64
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>>;
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
89
69
90pub struct Compositor {
70pub struct Compositor {
91 layers: Vec<Box<dyn Component>>,
71 layers: Vec<Box<dyn Component>>,
72+ terminal: Terminal,
92}
73}
93
74
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)?;
79+ Ok(Self {
96− Self { layers: Vec::new() }
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")
97 }
93 }
98
94
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 {
120 false
116 false
121 }
117 }
122
118
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+
124 for layer in &self.layers {
123 for layer in &self.layers {
125 layer.render(area, surface, cx)
124 layer.render(area, surface, cx)
126 }
125 }
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);
127 }
132 }
128
133
129 pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
134 pub fn cursor_position(&self, area: Rect, editor: &Editor) -> Option<Position> {
helix-term/src/ui/markdown.rs+8−9
@@ -59,16 +59,14 @@ impl Component for Markdown {
59 Event::End(tag) => {
59 Event::End(tag) => {
60 tags.pop();
60 tags.pop();
61 match tag {
61 match tag {
62+ Tag::Heading(_)
62− Tag::Heading(_) | Tag::Paragraph => {
63+ | Tag::Paragraph
64+ | Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
63− // whenever paragraph closes, new line
65+ // whenever code block or paragraph closes, new line
64 let spans = std::mem::replace(&mut spans, Vec::new());
66 let spans = std::mem::replace(&mut spans, Vec::new());
65 lines.push(Spans::from(spans));
67 lines.push(Spans::from(spans));
66 lines.push(Spans::default());
68 lines.push(Spans::default());
67 }
69 }
68− Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
69− let spans = std::mem::replace(&mut spans, Vec::new());
70− lines.push(Spans::from(spans));
71− }
72 _ => (),
70 _ => (),
73 }
71 }
74 }
72 }
@@ -117,14 +115,15 @@ impl Component for Markdown {
117 let par = Paragraph::new(contents).wrap(Wrap { trim: false });
115 let par = Paragraph::new(contents).wrap(Wrap { trim: false });
118 // .scroll(x, y) offsets
116 // .scroll(x, y) offsets
119
117
120− // padding on all sides
121 let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
118 let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
122 par.render(area, surface);
119 par.render(area, surface);
123 }
120 }
124
121
125− fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
122+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
126 let contents = tui::text::Text::from(self.contents.clone());
123 let contents = tui::text::Text::from(self.contents.clone());
124+ let width = std::cmp::min(contents.width() as u16, viewport.0);
125+ let height = std::cmp::min(contents.height() as u16, viewport.1);
127− Some((contents.width(), contents.height()))
126+ Some((width, height))
128 }
127 }
129}
128}
130
129
helix-term/src/ui/menu.rs+7−2
@@ -149,16 +149,21 @@ impl<T> Component for Menu<T> {
149 EventResult::Ignored
149 EventResult::Ignored
150 }
150 }
151
151
152− fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
152+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
153+ let width = std::cmp::min(30, viewport.0);
154+
153 const MAX: usize = 5;
155 const MAX: usize = 5;
154 let height = std::cmp::min(self.options.len(), MAX);
156 let height = std::cmp::min(self.options.len(), MAX);
157+ let height = std::cmp::min(height, viewport.1 as usize);
158+
155− Some((30, height))
159+ Some((width as u16, height as u16))
156 }
160 }
157
161
158 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
162 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
159 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
163 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
160 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
164 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
161
165
166+ // TODO: instead of a cell, all these numbers should be precomputed in handle_event + init
162 let mut scroll = self.scroll.get();
167 let mut scroll = self.scroll.get();
163 let len = self.options.len();
168 let len = self.options.len();
164
169
helix-term/src/ui/popup.rs+14−7
@@ -18,6 +18,7 @@ use helix_view::Editor;
18pub struct Popup {
18pub struct Popup {
19 contents: Box<dyn Component>,
19 contents: Box<dyn Component>,
20 position: Option<Position>,
20 position: Option<Position>,
21+ size: (u16, u16),
21}
22}
22
23
23impl Popup {
24impl Popup {
@@ -27,6 +28,7 @@ impl Popup {
27 Self {
28 Self {
28 contents,
29 contents,
29 position: None,
30 position: None,
31+ size: (0, 0),
30 }
32 }
31 }
33 }
32
34
@@ -68,6 +70,17 @@ impl Component for Popup {
68 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
70 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
69 }
71 }
70
72
73+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
74+ let (width, height) = self
75+ .contents
76+ .required_size((120, 26)) // max width, max height
77+ .expect("Component needs required_size implemented in order to be embedded in a popup");
78+
79+ self.size = (width, height);
80+
81+ Some(self.size)
82+ }
83+
71 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
84 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
72 use tui::text::Text;
85 use tui::text::Text;
73 use tui::widgets::{Paragraph, Widget, Wrap};
86 use tui::widgets::{Paragraph, Widget, Wrap};
@@ -77,13 +90,7 @@ impl Component for Popup {
77 .or_else(|| cx.editor.cursor_position())
90 .or_else(|| cx.editor.cursor_position())
78 .unwrap_or_default();
91 .unwrap_or_default();
79
92
80− let (width, height) = self
93+ let (width, height) = self.size;
81− .contents
82− .size_hint(viewport)
83− .expect("Component needs size_hint implemented in order to be embedded in a popup");
84−
85− let width = width.min(120) as u16;
86− let height = height.min(26) as u16;
87
94
88 // -- make sure frame doesn't stick out of bounds
95 // -- make sure frame doesn't stick out of bounds
89 let mut rel_x = position.col as u16;
96 let mut rel_x = position.col as u16;
helix-term/src/ui/text.rs+4−2
@@ -33,8 +33,10 @@ impl Component for Text {
33 par.render(area, surface);
33 par.render(area, surface);
34 }
34 }
35
35
36− fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
36+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
37 let contents = tui::text::Text::from(self.contents.clone());
37 let contents = tui::text::Text::from(self.contents.clone());
38+ let width = std::cmp::min(contents.width() as u16, viewport.0);
39+ let height = std::cmp::min(contents.height() as u16, viewport.1);
38− Some((contents.width(), contents.height()))
40+ Some((width, height))
39 }
41 }
40}
42}