on master help sign in
ui: Move terminal into compositor, redo required_size hints.
commit5ea610c41d58c8e63178200db7b723d016318f67 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 8 Mar 2021, 17:00 (6 years ago) parent05aa0d6
7 files changed+85−76
helix-term/src/application.rs+6−18 helix-term/src/commands.rs+4−1 helix-term/src/compositor.rs+42−37 helix-term/src/ui/markdown.rs+8−9 helix-term/src/ui/menu.rs+7−2 helix-term/src/ui/popup.rs+14−7 helix-term/src/ui/text.rs+4−2
helix-term/src/application.rs+6−18
@@ -23,23 +23,19 @@ use crossterm::{
2323 execute, terminal,
2424};
2525
26−use tui::{backend::CrosstermBackend, layout::Rect};
27−
28−type Terminal = crate::terminal::Terminal<CrosstermBackend<std::io::Stdout>>;
26+use tui::layout::Rect;
2927
3028pub struct Application {
3129 compositor: Compositor,
3230 editor: Editor,
33− terminal: Terminal,
3431
3532 executor: &'static smol::Executor<'static>,
3633}
3734
3835impl Application {
3936 pub fn new(mut args: Args, executor: &'static smol::Executor<'static>) -> Result<Self, Error> {
40− let backend = CrosstermBackend::new(stdout());
41− let mut terminal = Terminal::new(backend)?;
42− let size = terminal.size()?;
37+ let mut compositor = Compositor::new()?;
38+ let size = compositor.size();
4339 let mut editor = Editor::new(size);
4440
4541 let files = args.values_of_t::<PathBuf>("files").unwrap();
@@ -47,12 +43,10 @@ impl Application {
4743 editor.open(file, executor)?;
4844 }
4945
50− let mut compositor = Compositor::new();
5146 compositor.push(Box::new(ui::EditorView::new()));
5247
5348 let mut app = Self {
5449 editor,
55− terminal,
5650 compositor,
5751
5852 executor,
@@ -64,17 +58,11 @@ impl Application {
6458 fn render(&mut self) {
6559 let executor = &self.executor;
6660 let editor = &mut self.editor;
67− let compositor = &self.compositor;
61+ let compositor = &mut self.compositor;
6862
6963 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));
7664
77− self.terminal.draw(pos);
65+ compositor.render(&mut cx);
7866 }
7967
8068 pub async fn event_loop(&mut self) {
@@ -107,7 +95,7 @@ impl Application {
10795 // Handle key events
10896 let should_redraw = match event {
10997 Some(Ok(Event::Resize(width, height))) => {
110− self.terminal.resize(Rect::new(0, 0, width, height));
98+ self.compositor.resize(width, height);
11199
112100 self.compositor
113101 .handle_event(Event::Resize(width, height), &mut cx)
helix-term/src/commands.rs+4−1
@@ -39,9 +39,12 @@ impl<'a> Context<'a> {
3939 }
4040
4141 /// 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>) {
4343 self.callback = Some(Box::new(
4444 |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));
4548 compositor.push(component);
4649 },
4750 ));
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−//
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> {
helix-term/src/ui/markdown.rs+8−9
@@ -59,16 +59,14 @@ impl Component for Markdown {
5959 Event::End(tag) => {
6060 tags.pop();
6161 match tag {
62− Tag::Heading(_) | Tag::Paragraph => {
63− // whenever paragraph closes, new line
62+ Tag::Heading(_)
63+ | Tag::Paragraph
64+ | Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
65+ // whenever code block or paragraph closes, new line
6466 let spans = std::mem::replace(&mut spans, Vec::new());
6567 lines.push(Spans::from(spans));
6668 lines.push(Spans::default());
6769 }
68− Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
69− let spans = std::mem::replace(&mut spans, Vec::new());
70− lines.push(Spans::from(spans));
71− }
7270 _ => (),
7371 }
7472 }
@@ -117,14 +115,15 @@ impl Component for Markdown {
117115 let par = Paragraph::new(contents).wrap(Wrap { trim: false });
118116 // .scroll(x, y) offsets
119117
120− // padding on all sides
121118 let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2);
122119 par.render(area, surface);
123120 }
124121
125− fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
122+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
126123 let contents = tui::text::Text::from(self.contents.clone());
127− Some((contents.width(), contents.height()))
124+ let width = std::cmp::min(contents.width() as u16, viewport.0);
125+ let height = std::cmp::min(contents.height() as u16, viewport.1);
126+ Some((width, height))
128127 }
129128}
130129
helix-term/src/ui/menu.rs+7−2
@@ -149,16 +149,21 @@ impl<T> Component for Menu<T> {
149149 EventResult::Ignored
150150 }
151151
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+
153155 const MAX: usize = 5;
154156 let height = std::cmp::min(self.options.len(), MAX);
155− Some((30, height))
157+ let height = std::cmp::min(height, viewport.1 as usize);
158+
159+ Some((width as u16, height as u16))
156160 }
157161
158162 fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
159163 let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender
160164 let selected = Style::default().fg(Color::Rgb(255, 255, 255));
161165
166+ // TODO: instead of a cell, all these numbers should be precomputed in handle_event + init
162167 let mut scroll = self.scroll.get();
163168 let len = self.options.len();
164169
helix-term/src/ui/popup.rs+14−7
@@ -18,6 +18,7 @@ use helix_view::Editor;
1818pub struct Popup {
1919 contents: Box<dyn Component>,
2020 position: Option<Position>,
21+ size: (u16, u16),
2122}
2223
2324impl Popup {
@@ -27,6 +28,7 @@ impl Popup {
2728 Self {
2829 contents,
2930 position: None,
31+ size: (0, 0),
3032 }
3133 }
3234
@@ -68,6 +70,17 @@ impl Component for Popup {
6870 // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll.
6971 }
7072
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+
7184 fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) {
7285 use tui::text::Text;
7386 use tui::widgets::{Paragraph, Widget, Wrap};
@@ -77,13 +90,7 @@ impl Component for Popup {
7790 .or_else(|| cx.editor.cursor_position())
7891 .unwrap_or_default();
7992
80− let (width, height) = self
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;
93+ let (width, height) = self.size;
8794
8895 // -- make sure frame doesn't stick out of bounds
8996 let mut rel_x = position.col as u16;
helix-term/src/ui/text.rs+4−2
@@ -33,8 +33,10 @@ impl Component for Text {
3333 par.render(area, surface);
3434 }
3535
36− fn size_hint(&self, area: Rect) -> Option<(usize, usize)> {
36+ fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
3737 let contents = tui::text::Text::from(self.contents.clone());
38− Some((contents.width(), contents.height()))
38+ let width = std::cmp::min(contents.width() as u16, viewport.0);
39+ let height = std::cmp::min(contents.height() as u16, viewport.1);
40+ Some((width, height))
3941 }
4042}