Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/ui/text.rs')
| -rw-r--r-- | helix-view/src/ui/text.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/helix-view/src/ui/text.rs b/helix-view/src/ui/text.rs new file mode 100644 index 00000000..52082080 --- /dev/null +++ b/helix-view/src/ui/text.rs @@ -0,0 +1,68 @@ +use crate::compositor::{self, Component, RenderContext}; +use crate::graphics::Rect; + +pub struct Text { + pub(crate) contents: tui::text::Text<'static>, + size: (u16, u16), + viewport: (u16, u16), +} + +impl Text { + pub fn new(contents: String) -> Self { + Self { + contents: tui::text::Text::from(contents), + size: (0, 0), + viewport: (0, 0), + } + } +} + +impl From<tui::text::Text<'static>> for Text { + fn from(contents: tui::text::Text<'static>) -> Self { + Self { + contents, + size: (0, 0), + viewport: (0, 0), + } + } +} + +#[cfg(feature = "term")] +impl compositor::term::Render for Text { + fn render(&mut self, area: Rect, cx: &mut RenderContext<'_>) { + use tui::widgets::{Paragraph, Widget, Wrap}; + + let par = Paragraph::new(self.contents.clone()).wrap(Wrap { trim: false }); + // .scroll(x, y) offsets + + par.render(area, cx.surface); + } +} + +impl Component for Text { + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { + if viewport != self.viewport { + let width = std::cmp::min(self.contents.width() as u16, viewport.0); + let height = std::cmp::min(self.contents.height() as u16, viewport.1); + self.size = (width, height); + self.viewport = viewport; + } + Some(self.size) + } +} + +pub fn required_size(text: &tui::text::Text, max_text_width: u16) -> (u16, u16) { + let mut text_width = 0; + let mut height = 0; + for content in &text.lines { + height += 1; + let content_width = content.width() as u16; + if content_width > max_text_width { + text_width = max_text_width; + height += content_width / max_text_width; + } else if content_width > text_width { + text_width = content_width; + } + } + (text_width, height) +} |