Lines
100 %
Functions
50 %
Branches
//! The read-only text windows: what they show, how they scroll and what they can do.
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::Widget;
use tvui::{Caps, Chrome, RowWindow, inner, render_viewer, scrollbar_parts};
use crate::event::Intent;
use crate::hits::{Surface, Target};
use crate::scroll;
use crate::tabs::seq::Seq;
use crate::view::{DrawCtx, Handled};
/// What a viewer shows; two viewers of one source are the same window.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Source {
Detail {
id: String,
},
/// The keys of one keymap scope, as the keymap had them when it was asked for.
Help {
topic: &'static str,
}
impl Source {
#[must_use]
pub fn title(&self) -> String {
match self {
Source::Detail { id } => format!("Transaction {id}"),
Source::Help { topic } => format!("Help: {topic}"),
#[derive(Debug)]
pub struct Viewer {
pub source: Source,
pub lines: Vec<String>,
pub scroll: RowWindow,
/// Counts the requests for this window's lines; only the latest reply is applied.
pub seq: Seq,
impl Viewer {
pub fn new(source: Source, lines: Vec<String>) -> Self {
Self {
source,
lines,
scroll: RowWindow::default(),
seq: Seq::default(),
self.source.title()
/// A detail is fitted to its text, so it only moves and closes; a help is the user's to size.
pub fn caps(&self) -> Caps {
match self.source {
Source::Detail { .. } => Caps::FIXED,
Source::Help { .. } => Caps::ALL,
pub fn draw(&self, buf: &mut Buffer, area: Rect, ctx: &mut DrawCtx) {
Chrome::new(ctx.theme, &ctx.title, ctx.frame).render(area, buf);
render_viewer(ctx.theme, &self.lines, area, &self.scroll, buf);
let body = inner(area);
let dims = (
self.lines.len(),
usize::from(body.height),
self.scroll.top(),
);
ctx.hits.push(body, Target::Body(Surface::Pane));
for (rect, part) in scrollbar_parts(area, dims) {
ctx.hits.push(rect, Target::Scroll(Surface::Pane, part));
pub fn handle(&mut self, intent: Intent) -> Handled {
match scroll::apply(&self.scroll, intent) {
true => Handled::Consumed(vec![]),
false => Handled::Ignored,