1
//! The read-only text windows: what they show, how they scroll and what they can do.
2

            
3
use ratatui::buffer::Buffer;
4
use ratatui::layout::Rect;
5
use ratatui::widgets::Widget;
6
use tvui::{Caps, Chrome, RowWindow, inner, render_viewer, scrollbar_parts};
7

            
8
use crate::event::Intent;
9
use crate::hits::{Surface, Target};
10
use crate::scroll;
11
use crate::tabs::seq::Seq;
12
use crate::view::{DrawCtx, Handled};
13

            
14
/// What a viewer shows; two viewers of one source are the same window.
15
#[derive(Debug, Clone, PartialEq, Eq)]
16
pub enum Source {
17
    Detail {
18
        id: String,
19
    },
20
    /// The keys of one keymap scope, as the keymap had them when it was asked for.
21
    Help {
22
        topic: &'static str,
23
    },
24
}
25

            
26
impl Source {
27
    #[must_use]
28
43
    pub fn title(&self) -> String {
29
43
        match self {
30
27
            Source::Detail { id } => format!("Transaction {id}"),
31
16
            Source::Help { topic } => format!("Help: {topic}"),
32
        }
33
43
    }
34
}
35

            
36
#[derive(Debug)]
37
pub struct Viewer {
38
    pub source: Source,
39
    pub lines: Vec<String>,
40
    pub scroll: RowWindow,
41
    /// Counts the requests for this window's lines; only the latest reply is applied.
42
    pub seq: Seq,
43
}
44

            
45
impl Viewer {
46
    #[must_use]
47
77
    pub fn new(source: Source, lines: Vec<String>) -> Self {
48
77
        Self {
49
77
            source,
50
77
            lines,
51
77
            scroll: RowWindow::default(),
52
77
            seq: Seq::default(),
53
77
        }
54
77
    }
55

            
56
    #[must_use]
57
41
    pub fn title(&self) -> String {
58
41
        self.source.title()
59
41
    }
60

            
61
    /// A detail is fitted to its text, so it only moves and closes; a help is the user's to size.
62
    #[must_use]
63
90
    pub fn caps(&self) -> Caps {
64
90
        match self.source {
65
55
            Source::Detail { .. } => Caps::FIXED,
66
35
            Source::Help { .. } => Caps::ALL,
67
        }
68
90
    }
69

            
70
14
    pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
71
14
        Chrome::new(ctx.theme, &ctx.title, ctx.frame).render(area, buf);
72
14
        render_viewer(ctx.theme, &self.lines, area, &self.scroll, buf);
73
14
        let body = inner(area);
74
14
        let dims = (
75
14
            self.lines.len(),
76
14
            usize::from(body.height),
77
14
            self.scroll.top(),
78
14
        );
79
14
        ctx.hits.push(body, Target::Body(Surface::Pane));
80
14
        for (rect, part) in scrollbar_parts(area, dims) {
81
52
            ctx.hits.push(rect, Target::Scroll(Surface::Pane, part));
82
        }
83
14
    }
84

            
85
78
    pub fn handle(&mut self, intent: Intent) -> Handled {
86
78
        match scroll::apply(&self.scroll, intent) {
87
74
            true => Handled::Consumed(vec![]),
88
4
            false => Handled::Ignored,
89
        }
90
78
    }
91
}