1use ratatui::buffer::Buffer;
4use ratatui::layout::Rect;
5use ratatui::widgets::Widget;
6use tvui::{Caps, Chrome, RowWindow, inner, render_viewer, scrollbar_parts};
7
8use crate::event::Intent;
9use crate::hits::{Surface, Target};
10use crate::scroll;
11use crate::tabs::seq::Seq;
12use crate::view::{DrawCtx, Handled};
13
14#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum Source {
17 Detail {
18 id: String,
19 },
20 Help {
22 topic: &'static str,
23 },
24}
25
26impl Source {
27 #[must_use]
28 pub fn title(&self) -> String {
29 match self {
30 Source::Detail { id } => format!("Transaction {id}"),
31 Source::Help { topic } => format!("Help: {topic}"),
32 }
33 }
34}
35
36#[derive(Debug)]
37pub struct Viewer {
38 pub source: Source,
39 pub lines: Vec<String>,
40 pub scroll: RowWindow,
41 pub seq: Seq,
43}
44
45impl Viewer {
46 #[must_use]
47 pub fn new(source: Source, lines: Vec<String>) -> Self {
48 Self {
49 source,
50 lines,
51 scroll: RowWindow::default(),
52 seq: Seq::default(),
53 }
54 }
55
56 #[must_use]
57 pub fn title(&self) -> String {
58 self.source.title()
59 }
60
61 #[must_use]
63 pub fn caps(&self) -> Caps {
64 match self.source {
65 Source::Detail { .. } => Caps::FIXED,
66 Source::Help { .. } => Caps::ALL,
67 }
68 }
69
70 pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
71 Chrome::new(ctx.theme, &ctx.title, ctx.frame).render(area, buf);
72 render_viewer(ctx.theme, &self.lines, area, &self.scroll, buf);
73 let body = inner(area);
74 let dims = (
75 self.lines.len(),
76 usize::from(body.height),
77 self.scroll.top(),
78 );
79 ctx.hits.push(body, Target::Body(Surface::Pane));
80 for (rect, part) in scrollbar_parts(area, dims) {
81 ctx.hits.push(rect, Target::Scroll(Surface::Pane, part));
82 }
83 }
84
85 pub fn handle(&mut self, intent: Intent) -> Handled {
86 match scroll::apply(&self.scroll, intent) {
87 true => Handled::Consumed(vec![]),
88 false => Handled::Ignored,
89 }
90 }
91}