| 1 | use std::time::Instant; |
| 2 | |
| 3 | use ratatui::{ |
| 4 | layout::Rect, |
| 5 | style::Style, |
| 6 | widgets::{Block, BorderType, Borders}, |
| 7 | }; |
| 8 | |
| 9 | pub const SIDE_BORDERS: Borders = Borders::LEFT.union(Borders::RIGHT); |
| 10 | pub const AUTOHIDE_TIMEOUT_MILLISECONDS: u64 = 5000; // 5 seconds to autohide |
| 11 | |
| 12 | /// Determine whether a graph x-label should be hidden. |
| 13 | pub fn should_hide_x_label( |
| 14 | always_hide_time: bool, autohide_time: bool, timer: &mut Option<Instant>, draw_loc: Rect, |
| 15 | ) -> bool { |
| 16 | const TIME_LABEL_HEIGHT_LIMIT: u16 = 7; |
| 17 | |
| 18 | if always_hide_time || (autohide_time && timer.is_none()) { |
| 19 | true |
| 20 | } else if let Some(time) = timer { |
| 21 | if Instant::now().duration_since(*time).as_millis() < AUTOHIDE_TIMEOUT_MILLISECONDS.into() { |
| 22 | false |
| 23 | } else { |
| 24 | *timer = None; |
| 25 | true |
| 26 | } |
| 27 | } else { |
| 28 | draw_loc.height < TIME_LABEL_HEIGHT_LIMIT |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Return a widget block. |
| 33 | pub fn widget_block( |
| 34 | is_basic: bool, is_selected: bool, border_type: BorderType, widget_style: Style, |
| 35 | ) -> Block<'static> { |
| 36 | if is_basic { |
| 37 | if is_selected { |
| 38 | Block::default() |
| 39 | .border_type(border_type) |
| 40 | .style(widget_style) |
| 41 | .borders(SIDE_BORDERS) |
| 42 | } else { |
| 43 | Block::default().style(widget_style) |
| 44 | } |
| 45 | } else { |
| 46 | Block::default() |
| 47 | .border_type(border_type) |
| 48 | .style(widget_style) |
| 49 | .borders(Borders::all()) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Return a dialog block. |
| 54 | pub fn dialog_block(border_type: BorderType, border_style: Style) -> Block<'static> { |
| 55 | Block::default() |
| 56 | .border_type(border_type) |
| 57 | .border_style(border_style) |
| 58 | .borders(Borders::all()) |
| 59 | } |
| 60 | |
| 61 | #[cfg(test)] |
| 62 | mod test { |
| 63 | |
| 64 | use super::*; |
| 65 | |
| 66 | #[test] |
| 67 | fn test_should_hide_x_label() { |
| 68 | use std::time::{Duration, Instant}; |
| 69 | |
| 70 | use ratatui::layout::Rect; |
| 71 | |
| 72 | let rect = Rect::new(0, 0, 10, 10); |
| 73 | let small_rect = Rect::new(0, 0, 10, 6); |
| 74 | |
| 75 | let mut under_timer = Some(Instant::now()); |
| 76 | let mut over_timer = |
| 77 | Instant::now().checked_sub(Duration::from_millis(AUTOHIDE_TIMEOUT_MILLISECONDS + 100)); |
| 78 | |
| 79 | assert!(should_hide_x_label(true, false, &mut None, rect)); |
| 80 | assert!(should_hide_x_label(false, true, &mut None, rect)); |
| 81 | assert!(should_hide_x_label(false, false, &mut None, small_rect)); |
| 82 | |
| 83 | assert!(!should_hide_x_label( |
| 84 | false, |
| 85 | true, |
| 86 | &mut under_timer, |
| 87 | small_rect |
| 88 | )); |
| 89 | assert!(under_timer.is_some()); |
| 90 | |
| 91 | assert!(should_hide_x_label( |
| 92 | false, |
| 93 | true, |
| 94 | &mut over_timer, |
| 95 | small_rect |
| 96 | )); |
| 97 | assert!(over_timer.is_none()); |
| 98 | } |
| 99 | |
| 100 | /// This test exists because previously, [`SIDE_BORDERS`] was set |
| 101 | /// incorrectly after I moved from tui-rs to ratatui. |
| 102 | #[test] |
| 103 | fn assert_side_border_bits_match() { |
| 104 | assert_eq!( |
| 105 | SIDE_BORDERS, |
| 106 | Borders::ALL.difference(Borders::TOP.union(Borders::BOTTOM)) |
| 107 | ) |
| 108 | } |
| 109 | } |