use scheduler::Instant;
use std::time::Duration;
use crate::{App, Pixels, Point, Window};
pub(crate) const HOVER_FREEZE: Duration = Duration::from_millis(100);
pub(crate) struct HoverFreeze {
last_wheel: Option<(Instant, Point<Pixels>)>,
enabled: bool,
}
impl Default for HoverFreeze {
fn default() -> Self {
Self {
last_wheel: None,
enabled: !cfg!(test),
}
}
}
pub(crate) fn note_wheel(window: &mut Window, cx: &mut App) {
let freeze = &mut window.fast_layers.hover_freeze;
if !freeze.enabled {
return;
}
freeze.last_wheel = Some((Instant::now(), window.mouse_position));
window
.spawn(cx, async move |cx| {
cx.background_executor()
.timer(HOVER_FREEZE + Duration::from_millis(10))
.await;
cx.update(|window, _| {
if !frozen(window) {
window.refresh();
}
})
.ok();
})
.detach();
}
pub(crate) fn frozen(window: &Window) -> bool {
window
.fast_layers
.hover_freeze
.last_wheel
.is_some_and(|(at, position)| {
at.elapsed() < HOVER_FREEZE && position == window.mouse_position
})
}
pub(crate) fn hides_hover(window: &Window) -> bool {
window.last_input_was_keyboard() || (!window.invalidator.not_drawing() && frozen(window))
}
impl Window {
#[cfg(test)]
pub(crate) fn set_hover_freeze(&mut self, enabled: bool) {
self.fast_layers.hover_freeze.enabled = enabled;
self.fast_layers.hover_freeze.last_wheel = None;
}
}