| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#; |
| 2 | |
| 3 | use ratatuirust-analyzer cargo ratatui 0.30.2 crate/::{ |
| 4 | layoutrust-analyzer cargo ratatui-core 0.1.2 layout/::Rectrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#, |
| 5 | stylerust-analyzer cargo ratatui-core 0.1.2 style/::Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 6 | widgetsrust-analyzer cargo ratatui 0.30.2 widgets/::{Blockrust-analyzer cargo ratatui-widgets 0.3.2 block/Block#, BorderTyperust-analyzer cargo ratatui-widgets 0.3.2 borders/BorderType#, Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#}, |
| 7 | }; |
| 8 | |
| 9 | pub const SIDE_BORDERSrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/SIDE_BORDERS.: Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders# = Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#::LEFTrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]LEFT..unionrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]union().(Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#::RIGHTrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]RIGHT.); |
| 10 | pub const AUTOHIDE_TIMEOUT_MILLISECONDSrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/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_labelrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/should_hide_x_label().( |
| 14 | always_hide_time: bool, autohide_time: bool, timer: &mut Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#>, draw_loc: Rectrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#, |
| 15 | ) -> bool { |
| 16 | const TIME_LABEL_HEIGHT_LIMITrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/TIME_LABEL_HEIGHT_LIMIT.: u16 = 7; |
| 17 | |
| 18 | if always_hide_time || (autohide_time && timer.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()) { |
| 19 | true |
| 20 | } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(time) = timer { |
| 21 | if Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().().duration_sincerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]duration_since().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().time).as_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_millis().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]lt().AUTOHIDE_TIMEOUT_MILLISECONDSrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/AUTOHIDE_TIMEOUT_MILLISECONDS..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() { |
| 22 | false |
| 23 | } else { |
| 24 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().timer = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 25 | true |
| 26 | } |
| 27 | } else { |
| 28 | draw_loc.heightrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#height. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialOrd<Self>`]lt().TIME_LABEL_HEIGHT_LIMITrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/TIME_LABEL_HEIGHT_LIMIT. |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Return a widget block. |
| 33 | pub fn widget_blockrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/widget_block().( |
| 34 | is_basic: bool, is_selected: bool, border_type: BorderTyperust-analyzer cargo ratatui-widgets 0.3.2 borders/BorderType#, widget_style: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#, |
| 35 | ) -> Blockrust-analyzer cargo ratatui-widgets 0.3.2 block/Block#<'static> { |
| 36 | if is_basic { |
| 37 | if is_selected { |
| 38 | Blockrust-analyzer cargo ratatui-widgets 0.3.2 block/Block#::defaultrust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`][Default]default().() |
| 39 | .border_typerust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]border_type().(border_type) |
| 40 | .stylerust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]style().(widget_style) |
| 41 | .bordersrust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]borders().(SIDE_BORDERSrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/SIDE_BORDERS.) |
| 42 | } else { |
| 43 | Blockrust-analyzer cargo ratatui-widgets 0.3.2 block/Block#::defaultrust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`][Default]default().().stylerust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]style().(widget_style) |
| 44 | } |
| 45 | } else { |
| 46 | Blockrust-analyzer cargo ratatui-widgets 0.3.2 block/Block#::defaultrust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`][Default]default().() |
| 47 | .border_typerust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]border_type().(border_type) |
| 48 | .stylerust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]style().(widget_style) |
| 49 | .bordersrust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]borders().(Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#::allrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]all().()) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Return a dialog block. |
| 54 | pub fn dialog_blockrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/dialog_block().(border_type: BorderTyperust-analyzer cargo ratatui-widgets 0.3.2 borders/BorderType#, border_style: Stylerust-analyzer cargo ratatui-core 0.1.2 style/Style#) -> Blockrust-analyzer cargo ratatui-widgets 0.3.2 block/Block#<'static> { |
| 55 | Blockrust-analyzer cargo ratatui-widgets 0.3.2 block/Block#::defaultrust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`][Default]default().() |
| 56 | .border_typerust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]border_type().(border_type) |
| 57 | .border_stylerust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]border_style().(border_style) |
| 58 | .bordersrust-analyzer cargo ratatui-widgets 0.3.2 block/impl#[`Block<'a>`]borders().(Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#::allrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]all().()) |
| 59 | } |
| 60 | |
| 61 | #[cfg(test)] |
| 62 | mod testrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/test/ { |
| 63 | |
| 64 | use superrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/::*; |
| 65 | |
| 66 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 67 | fn test_should_hide_x_labelrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/test/test_should_hide_x_label().() { |
| 68 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::{Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#, Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#}; |
| 69 | |
| 70 | use ratatuirust-analyzer cargo ratatui 0.30.2 crate/::layoutrust-analyzer cargo ratatui-core 0.1.2 layout/::Rectrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#; |
| 71 | |
| 72 | let rect = Rectrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#::newrust-analyzer cargo ratatui-core 0.1.2 layout/rect/impl#[Rect]new().(0, 0, 10, 10); |
| 73 | let small_rect = Rectrust-analyzer cargo ratatui-core 0.1.2 layout/rect/Rect#::newrust-analyzer cargo ratatui-core 0.1.2 layout/rect/impl#[Rect]new().(0, 0, 10, 6); |
| 74 | |
| 75 | let mut under_timer = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().()); |
| 76 | let mut over_timer = |
| 77 | Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().().checked_subrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]checked_sub().(Durationrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/Duration#::from_millisrust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]from_millis().(AUTOHIDE_TIMEOUT_MILLISECONDSrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/AUTOHIDE_TIMEOUT_MILLISECONDS. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().100)); |
| 78 | |
| 79 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(should_hide_x_labelrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/should_hide_x_label().(true, false, &mut Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, rect)); |
| 80 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(should_hide_x_labelrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/should_hide_x_label().(false, true, &mut Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, rect)); |
| 81 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(should_hide_x_labelrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/should_hide_x_label().(false, false, &mut Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, small_rect)); |
| 82 | |
| 83 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().should_hide_x_labelrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/should_hide_x_label().( |
| 84 | false, |
| 85 | true, |
| 86 | &mut under_timer, |
| 87 | small_rect |
| 88 | )); |
| 89 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(under_timer.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()); |
| 90 | |
| 91 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(should_hide_x_labelrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/should_hide_x_label().( |
| 92 | false, |
| 93 | true, |
| 94 | &mut over_timer, |
| 95 | small_rect |
| 96 | )); |
| 97 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(over_timer.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]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 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 103 | fn assert_side_border_bits_matchrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/test/assert_side_border_bits_match().() { |
| 104 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 105 | SIDE_BORDERSrust-analyzer cargo bottom 0.14.9 canvas/drawing_utils/SIDE_BORDERS., |
| 106 | Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#::ALLrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]ALL..differencerust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]difference().(Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#::TOPrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]TOP..unionrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]union().(Bordersrust-analyzer cargo ratatui-widgets 0.3.2 borders/Borders#::BOTTOMrust-analyzer cargo ratatui-widgets 0.3.2 borders/impl#[Borders]BOTTOM.)) |
| 107 | ) |
| 108 | } |
| 109 | } |