2021-07-01 19:41:20 +02:00
|
|
|
use crate::compositor::{Component, Context};
|
2021-06-25 05:58:15 +02:00
|
|
|
use tui::buffer::Buffer as Surface;
|
2021-03-02 10:24:24 +01:00
|
|
|
|
2021-07-01 19:41:20 +02:00
|
|
|
use helix_view::graphics::Rect;
|
2021-03-02 10:24:24 +01:00
|
|
|
|
|
|
|
pub struct Text {
|
2022-02-20 06:44:44 +01:00
|
|
|
pub(crate) contents: tui::text::Text<'static>,
|
2021-09-09 05:35:14 +02:00
|
|
|
size: (u16, u16),
|
|
|
|
viewport: (u16, u16),
|
2021-03-02 10:24:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Text {
|
|
|
|
pub fn new(contents: String) -> Self {
|
2021-12-09 03:28:53 +01:00
|
|
|
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 {
|
2021-09-09 05:35:14 +02:00
|
|
|
Self {
|
|
|
|
contents,
|
|
|
|
size: (0, 0),
|
|
|
|
viewport: (0, 0),
|
|
|
|
}
|
2021-03-02 10:24:24 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-09 03:28:53 +01:00
|
|
|
|
2021-03-02 10:24:24 +01:00
|
|
|
impl Component for Text {
|
2021-08-12 09:00:42 +02:00
|
|
|
fn render(&mut self, area: Rect, surface: &mut Surface, _cx: &mut Context) {
|
2021-03-02 10:24:24 +01:00
|
|
|
use tui::widgets::{Paragraph, Widget, Wrap};
|
|
|
|
|
2021-12-09 03:28:53 +01:00
|
|
|
let par = Paragraph::new(self.contents.clone()).wrap(Wrap { trim: false });
|
2021-03-02 10:24:24 +01:00
|
|
|
// .scroll(x, y) offsets
|
|
|
|
|
|
|
|
par.render(area, surface);
|
|
|
|
}
|
|
|
|
|
2021-03-08 09:00:32 +01:00
|
|
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
2021-09-09 05:35:14 +02:00
|
|
|
if viewport != self.viewport {
|
2021-12-09 03:28:53 +01:00
|
|
|
let width = std::cmp::min(self.contents.width() as u16, viewport.0);
|
|
|
|
let height = std::cmp::min(self.contents.height() as u16, viewport.1);
|
2021-09-09 05:35:14 +02:00
|
|
|
self.size = (width, height);
|
|
|
|
self.viewport = viewport;
|
|
|
|
}
|
|
|
|
Some(self.size)
|
2021-03-02 10:24:24 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 06:44:44 +01:00
|
|
|
|
|
|
|
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;
|
2023-03-03 07:50:26 +01:00
|
|
|
height += content_width.checked_div(max_text_width).unwrap_or(0);
|
2022-02-20 06:44:44 +01:00
|
|
|
} else if content_width > text_width {
|
|
|
|
text_width = content_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(text_width, height)
|
|
|
|
}
|