use std::time::{Duration, Instant};
use crate::editor::RAW_REVEAL_DELAY;
use super::App;
pub(super) const SCROLL_QUIESCE: Duration = Duration::from_millis(150);
pub(super) const MIN_FRAME_INTERVAL: Duration = Duration::from_millis(16);
pub(super) const RESIZE_QUIESCE: Duration = Duration::from_millis(80);
pub(super) fn is_scrolling_within(last_scroll_at: Option<Instant>, quiesce: Duration) -> bool {
last_scroll_at.is_some_and(|t| t.elapsed() < quiesce)
}
impl App {
pub(super) fn mark_scrolling(&mut self) {
self.last_scroll_at = Some(Instant::now());
}
pub(super) fn is_scrolling(&self) -> bool {
is_scrolling_within(self.last_scroll_at, SCROLL_QUIESCE)
}
pub(super) fn next_deadline(&self, now: Instant) -> Option<Instant> {
let mut earliest: Option<Instant> = None;
let mut push = |candidate: Option<Instant>| {
if let Some(c) = candidate.filter(|&c| c > now) {
earliest = Some(earliest.map_or(c, |e: Instant| e.min(c)));
}
};
push(
self.editor
.cursor_block_entered_at
.map(|t| t + RAW_REVEAL_DELAY),
);
push(self.last_scroll_at.map(|t| t + SCROLL_QUIESCE));
push(self.resize_quiesce_at);
push(self.transient_deadline());
push(self.editor.cursor_blink.next_toggle());
push(self.editor.yank_flash_deadline());
push(self.autosave_deadline());
push(self.section_jump_deadline());
push(self.diff_advance_deadline());
push(self.search_advance_deadline());
push(self.modal_stack.next_deadline());
earliest
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_scrolling_is_false_when_never_scrolled() {
assert!(!is_scrolling_within(None, SCROLL_QUIESCE));
}
#[test]
fn is_scrolling_is_true_right_after_mark() {
let now = Instant::now();
assert!(is_scrolling_within(Some(now), SCROLL_QUIESCE));
}
#[test]
fn is_scrolling_is_false_after_quiesce_elapsed() {
let now = Instant::now();
std::thread::sleep(Duration::from_millis(20));
assert!(!is_scrolling_within(Some(now), Duration::from_millis(5)));
}
#[test]
fn is_scrolling_is_true_within_a_short_window() {
let now = Instant::now();
assert!(is_scrolling_within(
Some(now),
Duration::from_millis(10_000)
));
}
}