Lines
98.84 %
Functions
100 %
Branches
//! A dialog sits where it was moved to, hangs off the left, right and bottom at full size, and
//! shows only what the desktop does.
use crate::app::App;
use crate::event::{Intent, apply};
use crate::form::Form;
use crate::hits::{Hits, Slot, Target};
use crate::modal::{ConfirmAction, Modal};
use crate::overlay::Placement;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::buffer::Buffer;
use ratatui::layout::{Position, Rect};
use tvui::Place;
const SIZE: (u16, u16) = (80, 24);
struct Drawn {
buffer: Buffer,
hits: Hits,
cursor: Option<Position>,
}
fn drawn(app: &App) -> Drawn {
let mut terminal = Terminal::new(TestBackend::new(SIZE.0, SIZE.1)).expect("test terminal");
let mut hits = Hits::default();
terminal
.draw(|frame| crate::draw::draw(frame, app, &mut hits))
.expect("the frame draws");
let backend = terminal.backend();
Drawn {
buffer: backend.buffer().clone(),
hits,
cursor: backend.cursor_visible().then(|| backend.cursor_position()),
fn confirm() -> App {
let mut app = crate::test_support::app();
app.overlays.push(Modal::Confirm {
prompt: "Discard changes?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
app
fn slot(app: &App) -> Slot {
drawn(app).hits.dialog().expect("a dialog was drawn")
/// Puts the dialog on top with its origin at `x` and `y`.
fn put_at(app: &mut App, x: i32, y: i32) -> Place {
let auto = slot(app).auto;
let place = Place {
x,
y,
..Place::from(auto)
};
app.overlays.set_placement(Placement::of(place, auto));
place
fn glyph(drawn: &Drawn, x: u16, y: u16) -> &str {
drawn.buffer[(x, y)].symbol()
fn title(drawn: &Drawn) -> Rect {
let found = drawn
.hits
.entries()
.into_iter()
.rfind(|(_, t)| *t == Target::Title);
found.map(|(rect, _)| rect).expect("a title row was drawn")
/// What the dialog drew from its top row `y` down, not the window under it.
fn dialog_hits(drawn: &Drawn, y: u16) -> Vec<(Rect, Target)> {
let entries = drawn.hits.entries();
let from = entries.iter().rposition(|(_, t)| *t == Target::Block);
let hits = entries.into_iter().skip(from.unwrap_or(0) + 1);
hits.filter(|(rect, t)| rect.y >= y && !matches!(t, Target::Hint(_)))
.collect()
#[test]
fn a_dialog_opens_centred_and_a_moved_one_keeps_its_offset() {
let mut app = confirm();
let auto = slot(&app).auto;
let first = drawn(&app);
assert_eq!(title(&first).x, auto.x);
put_at(&mut app, i32::from(auto.x) + 3, i32::from(auto.y) + 1);
let moved = drawn(&app);
assert_eq!(title(&moved).x, auto.x + 3);
assert_eq!(title(&moved).y, auto.y + 1);
assert_eq!(glyph(&moved, auto.x + 3, auto.y + 1), "╔");
assert_eq!(glyph(&moved, auto.x, auto.y), " ");
fn a_dialog_off_the_right_edge_is_clipped_to_the_cells_the_screen_shows() {
let host = slot(&app).host;
let at = i32::from(host.right()) - 4;
put_at(&mut app, at, i32::from(host.y) + 2);
let frame = drawn(&app);
let (x, y) = (host.right() - 4, host.y + 2);
assert_eq!(glyph(&frame, x, y), "╔");
assert_eq!(glyph(&frame, x + 3, y), "■");
assert_eq!(title(&frame), Rect::new(x, y, 4, 1));
let dialog = dialog_hits(&frame, y);
assert!(!dialog.is_empty());
for (rect, _) in dialog {
assert!(
rect.right() <= SIZE.0 && rect.bottom() <= host.bottom(),
"{rect:?}"
);
frame
.iter()
.all(|(_, t)| !matches!(t, Target::Button(_)))
fn a_dialog_off_the_left_edge_shows_its_right_cells() {
let slot = slot(&app);
let width = i32::from(slot.auto.width);
put_at(&mut app, 3 - width, i32::from(slot.host.y) + 2);
let y = slot.host.y + 2;
assert_eq!(glyph(&frame, 2, y), "╗");
assert_eq!(glyph(&frame, 0, y), "═");
assert_eq!(title(&frame), Rect::new(0, y, 3, 1));
dialog_hits(&frame, y)
.all(|(_, t)| *t != Target::Close)
fn a_dialog_off_the_bottom_leaves_the_status_line_alone() {
let inside = drawn(&app);
put_at(
&mut app,
i32::from(slot.auto.x),
i32::from(slot.host.bottom()) - 2,
let status = slot.host.bottom();
let row = |drawn: &Drawn| -> String { (0..SIZE.0).map(|x| glyph(drawn, x, status)).collect() };
assert_eq!(row(&frame), row(&inside));
assert_eq!(glyph(&frame, slot.auto.x, status - 2), "╔");
assert_eq!(glyph(&frame, slot.auto.x, status - 1), "║");
fn typed_form() -> App {
let mut form = Form::config_set();
form.focus = 1;
app.overlays.push(Modal::Form(form));
fn the_form_cursor_follows_a_moved_dialog_and_hides_when_its_cell_is_clipped() {
let mut app = typed_form();
let Some(home) = drawn(&app).cursor else {
panic!("a focused text field shows the cursor");
let (x, y) = (i32::from(slot.auto.x) + 2, i32::from(slot.auto.y) + 1);
put_at(&mut app, x, y);
assert_eq!(
drawn(&app).cursor,
Some(Position::new(home.x + 2, home.y + 1))
put_at(&mut app, i32::from(slot.host.right()) - 1, y);
assert_eq!(drawn(&app).cursor, None);
put_at(&mut app, 1 - i32::from(slot.auto.width), y);
fn only_a_confirm_has_its_own_size_as_the_least() {
let confirm = slot(&app);
assert_eq!(confirm.min, (confirm.auto.width, confirm.auto.height));
apply(&mut app, Intent::CloseTopmost);
crate::test_support::open_draft(&mut app);
let help = slot(&app);
assert_ne!(help.min, (help.auto.width, help.auto.height));
fn a_moved_dialog_keeps_its_corner_when_its_content_grows() {
let mut form = Form::transaction_create();
form.focus = 3;
put_at(&mut app, i32::from(auto.x) + 2, i32::from(auto.y) + 1);
let before = title(&drawn(&app));
if let Some(Modal::Form(form)) = app.overlays.top_mut()
&& let Some(crate::widgets::Widget::Splits(splits)) =
form.fields.get_mut(3).map(|f| &mut f.widget)
{
(0..4).for_each(|_| splits.add_row());
assert_ne!(slot(&app).auto.height, auto.height);
let after = title(&drawn(&app));
assert_eq!((after.x, after.y), (before.x, before.y));
fn a_dialog_in_size_move_mode_has_the_dragging_frame_and_keeps_the_cursor() {
app.hits = drawn(&app).hits;
apply(&mut app, Intent::SizeWindow);
assert_eq!(glyph(&frame, auto.x, auto.y), "┌");
assert!(frame.cursor.is_some());
apply(&mut app, Intent::DragDone);
assert_eq!(glyph(&drawn(&app), auto.x, auto.y), "╔");