Lines
100 %
Functions
50 %
Branches
//! A multi-line field: its label and a text box of [`TEXT_AREA_ROWS`] rows beside it.
use crate::draw::canvas::Offscreen;
use ratatui::layout::{Position, Rect};
use tvui::{Label, TextView, Theme};
use crate::widgets::{Editor, TEXT_AREA_ROWS};
pub(in crate::draw) struct TextArea<'a> {
label: String,
column: u16,
editor: &'a Editor,
lines: Vec<&'a str>,
cursor: (usize, usize),
focused: bool,
}
impl<'a> TextArea<'a> {
pub fn new(label: String, column: u16, editor: &'a Editor, focused: bool) -> Self {
TextArea {
label,
column,
editor,
lines: editor.text_lines(),
cursor: editor.cursor_position(),
focused,
pub fn height() -> usize {
TEXT_AREA_ROWS
pub fn width(&self) -> usize {
usize::from(self.column) + super::body::INPUT_MIN + 1
/// The text box in the value column of the field's `rect`.
pub(super) fn area(&self, rect: Rect) -> Rect {
let x = rect.x.saturating_add(self.column).min(rect.right());
let width = rect.right().saturating_sub(x).saturating_sub(1);
let height = u16::try_from(TEXT_AREA_ROWS).unwrap_or(u16::MAX);
Rect::new(x, rect.y, width, height.min(rect.height))
pub fn draw(&self, frame: &mut Offscreen, rect: Rect, theme: &Theme) {
let label = Rect::new(rect.x, rect.y, self.column.min(rect.width), 1);
frame.render_widget(Label::new(theme, &self.label, self.focused), label);
let area = self.area(rect);
let mut viewport = self.editor.viewport();
let view = TextView::new(theme, &self.lines, self.cursor, self.focused);
frame.render_stateful_widget(view, area, &mut viewport);
self.editor.set_viewport(viewport);
/// The edit position as drawn by the last [`TextArea::draw`].
pub fn cursor(&self, rect: Rect) -> Option<Position> {
let at = TextView::cursor(area, &self.lines, self.cursor, self.editor.viewport());
(area.width > 0 && area.height > 0).then_some(at)