Skip to main content

verso/ui/
reader_view.rs

1use crate::reader::page::Page;
2use ratatui::{
3    layout::{Alignment, Rect},
4    style::{Color, Style},
5    text::{Line, Span as TuiSpan},
6    widgets::{Paragraph, Wrap},
7    Frame,
8};
9
10pub struct ReaderView<'a> {
11    pub page: Option<&'a Page>,
12    pub column_width: u16,
13    pub theme: &'a str,
14}
15
16impl<'a> ReaderView<'a> {
17    pub fn render(&self, f: &mut Frame, area: Rect) {
18        let bg = if self.theme == "light" {
19            Color::White
20        } else {
21            Color::Reset
22        };
23        let fg = if self.theme == "light" {
24            Color::Black
25        } else {
26            Color::Gray
27        };
28
29        let left_pad = area.width.saturating_sub(self.column_width) / 2;
30        let text_area = Rect {
31            x: area.x + left_pad,
32            y: area.y,
33            width: self.column_width.min(area.width),
34            height: area.height,
35        };
36
37        let lines: Vec<Line> = match self.page {
38            Some(p) => p
39                .rows
40                .iter()
41                .map(|r| {
42                    Line::from(vec![TuiSpan::styled(
43                        r.text.clone(),
44                        Style::default().fg(fg).bg(bg),
45                    )])
46                })
47                .collect(),
48            None => vec![Line::from("…paginating")],
49        };
50        let para = Paragraph::new(lines)
51            .alignment(Alignment::Left)
52            .wrap(Wrap { trim: false });
53        f.render_widget(para, text_area);
54    }
55}