Lines
100 %
Functions
Branches
//! Real dialogs draw the Turbo Vision buttons: a shadow, a pressed look, their own placement.
use crate::app::{App, Face, Owner};
use crate::form::Form;
use crate::hits::{Hits, Target};
use crate::modal::{ConfirmAction, Modal};
use crate::test_support::draw_frame;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use tvui::Theme;
const PRESETS: [&str; 2] = ["dn", "turbo"];
fn confirm(preset: &str) -> App {
let mut app = crate::test_support::app();
app.theme = Theme::preset(preset).expect("the preset parses");
app.overlays.push(Modal::Confirm {
prompt: "Discard changes?".to_string(),
action: ConfirmAction::DiscardForm,
focus: None,
});
app
}
fn form(preset: &str) -> App {
app.overlays.push(Modal::Form(Form::config_set()));
fn rects(hits: &Hits) -> Vec<Rect> {
hits.entries()
.iter()
.filter(|(_, target)| matches!(target, Target::Button(_)))
.map(|(rect, _)| *rect)
.collect()
fn row(buffer: &Buffer, y: u16) -> String {
(0..buffer.area.width)
.map(|x| buffer[(x, y)].symbol())
#[test]
fn a_confirm_and_a_form_draw_two_row_buttons_with_a_shadow_in_both_presets() {
for preset in PRESETS {
for app in [confirm(preset), form(preset)] {
let (buffer, hits) = draw_frame(&app, 80, 24);
let buttons = rects(&hits);
assert_eq!(buttons.len(), 2, "{preset}");
for rect in buttons {
assert_eq!(rect.height, 1, "{preset}");
assert_eq!(buffer[(rect.right(), rect.y)].symbol(), "\u{2584}");
let lower = row(&buffer, rect.y + 1);
let under: String = lower
.chars()
.skip(usize::from(rect.x) + 1)
.take(8)
.collect();
assert_eq!(under, "\u{2580}".repeat(8), "{preset}");
fn a_message_centres_its_buttons_and_a_form_puts_them_at_the_bottom_right() {
let (_, hits) = draw_frame(&confirm("dn"), 80, 24);
let dialog = dialog_inner(&draw_frame(&confirm("dn"), 80, 24).0);
let left = buttons[0].x - 1 - dialog.x;
let right = dialog.right() - buttons[1].right() - 1;
assert!(left.abs_diff(right) <= 1, "{left} {right}");
let (buffer, hits) = draw_frame(&form("dn"), 80, 24);
let dialog = dialog_inner(&buffer);
assert_eq!(buttons[1].right() + 2, dialog.right());
assert_eq!(buttons[1].x - 1 - (buttons[0].right() + 1), 2);
/// The inside of the dialog: between the first `╔` and the last `╝`.
fn dialog_inner(buffer: &Buffer) -> Rect {
let corners = |symbol: &str| -> Vec<_> {
let all = buffer.area.positions();
all.filter(|p| buffer[*p].symbol() == symbol).collect()
};
let (first, last) = (corners("╔")[0], *corners("╝").last().expect("a corner"));
Rect::new(
first.x + 1,
first.y + 1,
last.x - first.x - 1,
last.y - first.y - 1,
)
fn a_held_button_is_drawn_down_in_a_confirm_and_a_form() {
for mut app in [confirm("dn"), form("dn")] {
let (_, hits) = draw_frame(&app, 80, 24);
let held = rects(&hits)[0];
app.hold_press(Owner::Layer, Face::Button(0), held);
assert_eq!(rects(&hits)[0], held);
assert_eq!(buffer[(held.right(), held.y)].symbol(), " ");
assert_eq!(buffer[(held.x + 2, held.y + 1)].symbol(), " ");
let other = rects(&hits)[1];
assert_eq!(buffer[(other.right(), other.y)].symbol(), "\u{2584}");