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 {
|
|
|
|
contents: String,
|
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-09-09 05:35:14 +02:00
|
|
|
Self {
|
|
|
|
contents,
|
|
|
|
size: (0, 0),
|
|
|
|
viewport: (0, 0),
|
|
|
|
}
|
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};
|
|
|
|
let contents = tui::text::Text::from(self.contents.clone());
|
|
|
|
|
|
|
|
let par = Paragraph::new(contents).wrap(Wrap { trim: false });
|
|
|
|
// .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 {
|
|
|
|
let contents = tui::text::Text::from(self.contents.clone());
|
|
|
|
let width = std::cmp::min(contents.width() as u16, viewport.0);
|
|
|
|
let height = std::cmp::min(contents.height() as u16, viewport.1);
|
|
|
|
self.size = (width, height);
|
|
|
|
self.viewport = viewport;
|
|
|
|
}
|
|
|
|
Some(self.size)
|
2021-03-02 10:24:24 +01:00
|
|
|
}
|
|
|
|
}
|