diff options
author | Blaž Hrastnik | 2021-03-08 08:00:32 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-03-08 08:00:32 +0000 |
commit | 5ea610c41d58c8e63178200db7b723d016318f67 (patch) | |
tree | c76d8b76d5cda802ca9a0fc36137e5ddbed281b5 /helix-term/src/ui | |
parent | 05aa0d6991820625189c7e367a6d033682ef4e4e (diff) |
ui: Move terminal into compositor, redo required_size hints.
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r-- | helix-term/src/ui/markdown.rs | 17 | ||||
-rw-r--r-- | helix-term/src/ui/menu.rs | 9 | ||||
-rw-r--r-- | helix-term/src/ui/popup.rs | 21 | ||||
-rw-r--r-- | helix-term/src/ui/text.rs | 6 |
4 files changed, 33 insertions, 20 deletions
diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs index 8456ef74..976d00fa 100644 --- a/helix-term/src/ui/markdown.rs +++ b/helix-term/src/ui/markdown.rs @@ -59,16 +59,14 @@ impl Component for Markdown { Event::End(tag) => { tags.pop(); match tag { - Tag::Heading(_) | Tag::Paragraph => { - // whenever paragraph closes, new line + Tag::Heading(_) + | Tag::Paragraph + | Tag::CodeBlock(CodeBlockKind::Fenced(_)) => { + // whenever code block or paragraph closes, new line let spans = std::mem::replace(&mut spans, Vec::new()); lines.push(Spans::from(spans)); lines.push(Spans::default()); } - Tag::CodeBlock(CodeBlockKind::Fenced(_)) => { - let spans = std::mem::replace(&mut spans, Vec::new()); - lines.push(Spans::from(spans)); - } _ => (), } } @@ -117,14 +115,15 @@ impl Component for Markdown { let par = Paragraph::new(contents).wrap(Wrap { trim: false }); // .scroll(x, y) offsets - // padding on all sides let area = Rect::new(area.x + 1, area.y + 1, area.width - 2, area.height - 2); par.render(area, surface); } - fn size_hint(&self, area: Rect) -> Option<(usize, usize)> { + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { let contents = tui::text::Text::from(self.contents.clone()); - Some((contents.width(), contents.height())) + let width = std::cmp::min(contents.width() as u16, viewport.0); + let height = std::cmp::min(contents.height() as u16, viewport.1); + Some((width, height)) } } diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 9f0e79be..c129420d 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -149,16 +149,21 @@ impl<T> Component for Menu<T> { EventResult::Ignored } - fn size_hint(&self, area: Rect) -> Option<(usize, usize)> { + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { + let width = std::cmp::min(30, viewport.0); + const MAX: usize = 5; let height = std::cmp::min(self.options.len(), MAX); - Some((30, height)) + let height = std::cmp::min(height, viewport.1 as usize); + + Some((width as u16, height as u16)) } fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) { let style = Style::default().fg(Color::Rgb(164, 160, 232)); // lavender let selected = Style::default().fg(Color::Rgb(255, 255, 255)); + // TODO: instead of a cell, all these numbers should be precomputed in handle_event + init let mut scroll = self.scroll.get(); let len = self.options.len(); diff --git a/helix-term/src/ui/popup.rs b/helix-term/src/ui/popup.rs index 625ee2b3..4ca70c50 100644 --- a/helix-term/src/ui/popup.rs +++ b/helix-term/src/ui/popup.rs @@ -18,6 +18,7 @@ use helix_view::Editor; pub struct Popup { contents: Box<dyn Component>, position: Option<Position>, + size: (u16, u16), } impl Popup { @@ -27,6 +28,7 @@ impl Popup { Self { contents, position: None, + size: (0, 0), } } @@ -68,6 +70,17 @@ impl Component for Popup { // tab/enter/ctrl-k or whatever will confirm the selection/ ctrl-n/ctrl-p for scroll. } + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { + let (width, height) = self + .contents + .required_size((120, 26)) // max width, max height + .expect("Component needs required_size implemented in order to be embedded in a popup"); + + self.size = (width, height); + + Some(self.size) + } + fn render(&self, viewport: Rect, surface: &mut Surface, cx: &mut Context) { use tui::text::Text; use tui::widgets::{Paragraph, Widget, Wrap}; @@ -77,13 +90,7 @@ impl Component for Popup { .or_else(|| cx.editor.cursor_position()) .unwrap_or_default(); - let (width, height) = self - .contents - .size_hint(viewport) - .expect("Component needs size_hint implemented in order to be embedded in a popup"); - - let width = width.min(120) as u16; - let height = height.min(26) as u16; + let (width, height) = self.size; // -- make sure frame doesn't stick out of bounds let mut rel_x = position.col as u16; diff --git a/helix-term/src/ui/text.rs b/helix-term/src/ui/text.rs index 133cdd25..9db4d3bc 100644 --- a/helix-term/src/ui/text.rs +++ b/helix-term/src/ui/text.rs @@ -33,8 +33,10 @@ impl Component for Text { par.render(area, surface); } - fn size_hint(&self, area: Rect) -> Option<(usize, usize)> { + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { let contents = tui::text::Text::from(self.contents.clone()); - Some((contents.width(), contents.height())) + let width = std::cmp::min(contents.width() as u16, viewport.0); + let height = std::cmp::min(contents.height() as u16, viewport.1); + Some((width, height)) } } |