1
//! The button a key holds drawn down for [`KEY_PRESS`] before it acts.
2

            
3
use std::time::{Duration, Instant};
4

            
5
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
6

            
7
use super::App;
8
use super::hold::{Grip, Owner};
9
use crate::event::{Intent, apply};
10
use crate::focus::{FocusTarget, current_focus};
11

            
12
/// How long a key leaves a button drawn down before it acts, as in Turbo Vision.
13
pub(crate) const KEY_PRESS: Duration = Duration::from_millis(100);
14

            
15
/// A button a key holds down until `due`, when `intent` acts.
16
#[derive(Debug, Clone, Copy)]
17
pub(crate) struct KeyPress {
18
    index: usize,
19
    due: Instant,
20
    intent: Intent,
21
}
22

            
23
impl App {
24
    /// The key press, if the dialog it began on is still the top one.
25
7733
    fn live_key_press(&self) -> Option<KeyPress> {
26
7733
        match self.live_hold()?.grip {
27
233
            Grip::Key(press) => Some(press),
28
282
            _ => None,
29
        }
30
7733
    }
31

            
32
117
    pub(crate) fn hold_button_by_key(&mut self, index: usize, intent: Intent, now: Instant) {
33
117
        let press = KeyPress {
34
117
            index,
35
117
            due: now + KEY_PRESS,
36
117
            intent,
37
117
        };
38
117
        self.begin_hold(Owner::Layer, Grip::Key(press));
39
117
    }
40

            
41
    /// The button a key holds drawn down.
42
734
    pub(crate) fn key_pressed_button(&self) -> Option<usize> {
43
734
        self.live_key_press().map(|press| press.index)
44
734
    }
45

            
46
    /// Lets a key's press act now, without waiting for its time; whether it did. A press whose
47
    /// dialog is gone is dropped.
48
6999
    pub(crate) fn flush_key_press(&mut self) -> bool {
49
6999
        let live = self.live_key_press();
50
6999
        self.hold.take_if(|hold| matches!(hold.grip, Grip::Key(_)));
51
6999
        let Some(press) = live else {
52
6883
            return false;
53
        };
54
116
        apply(self, press.intent);
55
116
        true
56
6999
    }
57

            
58
    /// Lets a key's press act once its time is up.
59
610
    pub(crate) fn tick(&mut self, now: Instant) {
60
610
        if self.next_deadline(now) == Some(Duration::ZERO) {
61
22
            self.flush_key_press();
62
588
        }
63
610
    }
64

            
65
    /// The time left until the loop has something to do besides waiting for input.
66
1239
    pub(crate) fn next_deadline(&self, now: Instant) -> Option<Duration> {
67
1239
        match self.hold?.grip {
68
246
            Grip::Key(press) => Some(press.due.saturating_duration_since(now)),
69
8
            _ => None,
70
        }
71
1239
    }
72
}
73

            
74
/// The button the key presses and what it does: the one `Press` presses, or the one whose
75
/// hotkey letter the key types and whose intent the key is bound to.
76
3220
pub(crate) fn button_of_key(app: &App, key: KeyEvent, intent: Intent) -> Option<(usize, Intent)> {
77
3220
    if current_focus(app) != FocusTarget::Overlay {
78
1614
        return None;
79
1606
    }
80
1606
    let modal = app.overlays.top()?;
81
1606
    let specs = modal.button_specs();
82
1606
    let index = match intent {
83
72
        Intent::Press => modal.press_button()?,
84
1534
        _ => hotkey_button(specs, key, intent)?,
85
    };
86
115
    specs.get(index).map(|&(_, spec)| (index, spec))
87
3220
}
88

            
89
1534
fn hotkey_button(specs: &[(&str, Intent)], key: KeyEvent, intent: Intent) -> Option<usize> {
90
1534
    let KeyCode::Char(typed) = key.code else {
91
723
        return None;
92
    };
93
811
    if key
94
811
        .modifiers
95
811
        .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT)
96
    {
97
173
        return None;
98
638
    }
99
1490
    specs.iter().position(|&(label, spec)| {
100
1490
        spec == intent && tvui::hotkey(label).is_some_and(|c| c.eq_ignore_ascii_case(&typed))
101
1490
    })
102
1534
}