use std::{path::PathBuf, time::Duration};
use winit::{event::WindowEvent, window::Window};
use super::cursor;
use crate::{
app::{Tree, events::DraggedPaths},
time::Instant,
};
pub(super) const POLL_INTERVAL: Duration = Duration::from_millis(16);
pub(super) const HOVER_TIMEOUT: Duration = Duration::from_secs(60);
pub(super) const BUTTONS_UP_GRACE: Duration = Duration::from_millis(250);
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct PointerProbe {
pub(super) position: Option<(f32, f32)>,
pub(super) any_button_pressed: Option<bool>,
}
impl PointerProbe {
fn read(window: Option<&Window>) -> Self {
Self {
position: window.and_then(cursor::position_in_window),
any_button_pressed: cursor::any_mouse_button_pressed(),
}
}
}
#[derive(Debug, PartialEq, Eq)]
enum FileEvent {
Hovered(PathBuf),
Cancelled,
Dropped(PathBuf),
}
pub(super) struct FileDragShell {
events: Vec<FileEvent>,
hovering: bool,
last_poll: Instant,
last_event: Instant,
buttons_up_since: Option<Instant>,
last_position: Option<(f32, f32)>,
}
impl Default for FileDragShell {
fn default() -> Self {
let now = Instant::now();
Self {
events: Vec::new(),
hovering: false,
last_poll: now,
last_event: now,
buttons_up_since: None,
last_position: None,
}
}
}
impl FileDragShell {
pub(super) fn on_event(&mut self, event: &WindowEvent) -> bool {
let event = match event {
WindowEvent::HoveredFile(path) => FileEvent::Hovered(path.clone()),
WindowEvent::DroppedFile(path) => FileEvent::Dropped(path.clone()),
WindowEvent::HoveredFileCancelled => FileEvent::Cancelled,
_ => return false,
};
self.events.push(event);
true
}
pub(super) fn has_pending(&self) -> bool {
!self.events.is_empty()
}
pub(super) fn next_poll(&self) -> Option<Instant> {
self.hovering.then(|| self.last_poll + POLL_INTERVAL)
}
pub(super) fn flush(&mut self, tree: &mut Tree, window: Option<&Window>, fallback: (f64, f64)) {
self.flush_at(tree, Instant::now(), &|| PointerProbe::read(window), fallback);
}
fn flush_at(&mut self, tree: &mut Tree, now: Instant, probe: &dyn Fn() -> PointerProbe, fallback: (f64, f64)) {
let events = std::mem::take(&mut self.events);
if !events.is_empty() {
self.last_event = now;
self.buttons_up_since = None;
}
let mut events = events.into_iter().peekable();
while let Some(event) = events.next() {
match event {
FileEvent::Cancelled => {
self.hovering = false;
tree.file_drag_leave();
}
FileEvent::Hovered(path) => {
let mut paths = vec![path];
while let Some(FileEvent::Hovered(_)) = events.peek() {
if let Some(FileEvent::Hovered(path)) = events.next() {
paths.push(path);
}
}
let (x, y) = self.position(probe(), fallback);
self.hovering = true;
self.last_poll = now;
tree.file_drag_enter(DraggedPaths::from_paths(paths), x, y);
}
FileEvent::Dropped(path) => {
let mut paths = vec![path];
while let Some(FileEvent::Dropped(_)) = events.peek() {
if let Some(FileEvent::Dropped(path)) = events.next() {
paths.push(path);
}
}
let (x, y) = self.position(probe(), fallback);
self.hovering = false;
tree.file_drop(DraggedPaths::from_paths(paths), x, y);
}
}
}
if !self.hovering {
return;
}
if now.duration_since(self.last_poll) >= POLL_INTERVAL {
self.last_poll = now;
let probed = probe();
if probed.any_button_pressed == Some(false) {
let since = *self.buttons_up_since.get_or_insert(now);
if now.duration_since(since) >= BUTTONS_UP_GRACE {
self.hovering = false;
self.buttons_up_since = None;
tree.file_drag_leave();
return;
}
} else {
self.buttons_up_since = None;
}
let moved_from = self.last_position;
let position = self.position(probed, fallback);
if moved_from != Some(position) {
self.last_event = now;
tree.file_drag_over(position.0, position.1);
}
}
if now.duration_since(self.last_event) >= HOVER_TIMEOUT {
self.hovering = false;
tree.file_drag_leave();
}
}
fn position(&mut self, probe: PointerProbe, fallback: (f64, f64)) -> (f32, f32) {
let position = probe.position.unwrap_or((fallback.0 as f32, fallback.1 as f32));
self.last_position = Some(position);
position
}
}
#[cfg(test)]
mod tests {
use std::sync::{Arc, Mutex};
use super::*;
use crate::{
app::{
App,
events::{FileDragEvent, FileDropEvent},
},
components::Row,
};
type Log = Arc<Mutex<Vec<String>>>;
fn target(log: &Log) -> Tree {
let mut tree = Tree::new();
tree.resize(200, 200);
let (enter, over, leave, drop) = (log.clone(), log.clone(), log.clone(), log.clone());
tree.set_root(
Row::new()
.width(100.0)
.height(100.0)
.on_file_drag_enter(move |event: FileDragEvent| {
enter.lock().unwrap().push(format!("enter {}", event.paths.len()));
})
.on_file_drag_over(move |event: FileDragEvent| {
over.lock().unwrap().push(format!("over {},{}", event.x, event.y));
})
.on_file_drag_leave(move || leave.lock().unwrap().push("leave".into()))
.on_file_drop(move |event: FileDropEvent| {
drop.lock().unwrap().push(format!("drop {}", event.paths.len()));
}),
);
tree.pass_headless(&mut App::new());
tree
}
fn hovered(shell: &mut FileDragShell, paths: &[&str]) {
for path in paths {
assert!(shell.on_event(&WindowEvent::HoveredFile(PathBuf::from(path))));
}
}
fn dropped(shell: &mut FileDragShell, paths: &[&str]) {
for path in paths {
assert!(shell.on_event(&WindowEvent::DroppedFile(PathBuf::from(path))));
}
}
fn at(x: f32, y: f32) -> PointerProbe {
PointerProbe {
position: Some((x, y)),
any_button_pressed: None,
}
}
#[test]
fn a_burst_of_hovered_files_enters_once_and_the_pointer_is_polled_while_it_moves() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
let position = Arc::new(Mutex::new((10.0_f32, 10.0_f32)));
let read = {
let position = position.clone();
move || {
let (x, y) = *position.lock().unwrap();
at(x, y)
}
};
let start = Instant::now();
hovered(&mut shell, &["C:/a.png", "C:/b.png"]);
assert!(shell.has_pending());
shell.flush_at(&mut tree, start, &read, (0.0, 0.0));
assert!(!shell.has_pending());
assert_eq!(shell.next_poll(), Some(start + POLL_INTERVAL));
assert_eq!(log.lock().unwrap().as_slice(), ["enter 2", "over 10,10"]);
shell.flush_at(&mut tree, start + POLL_INTERVAL / 2, &read, (0.0, 0.0));
shell.flush_at(&mut tree, start + POLL_INTERVAL, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().len(), 2, "no over without a move");
*position.lock().unwrap() = (20.0, 30.0);
shell.flush_at(&mut tree, start + POLL_INTERVAL * 2, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().last().map(String::as_str), Some("over 20,30"));
assert_eq!(shell.next_poll(), Some(start + POLL_INTERVAL * 3));
}
#[test]
fn enter_over_and_leave_follow_the_events_and_a_cancel_ends_the_polling() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
let read = || at(10.0, 10.0);
let start = Instant::now();
hovered(&mut shell, &["C:/a.png"]);
shell.flush_at(&mut tree, start, &read, (0.0, 0.0));
assert!(shell.next_poll().is_some());
assert!(shell.on_event(&WindowEvent::HoveredFileCancelled));
shell.flush_at(&mut tree, start + POLL_INTERVAL, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().as_slice(), ["enter 1", "over 10,10", "leave"]);
assert_eq!(shell.next_poll(), None, "no polling after the cancel");
assert!(!tree.has_file_drag());
}
#[test]
fn a_drop_delivers_all_its_paths_and_ends_the_polling() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
let read = || at(10.0, 10.0);
let start = Instant::now();
hovered(&mut shell, &["C:/a.png", "C:/b.png"]);
shell.flush_at(&mut tree, start, &read, (0.0, 0.0));
dropped(&mut shell, &["C:/a.png", "C:/b.png"]);
shell.flush_at(&mut tree, start + POLL_INTERVAL, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().as_slice(), ["enter 2", "over 10,10", "drop 2"]);
assert_eq!(shell.next_poll(), None);
assert!(!tree.has_file_drag());
}
#[test]
fn events_of_one_turn_are_delivered_in_arrival_order() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
let read = || at(10.0, 10.0);
let start = Instant::now();
hovered(&mut shell, &["C:/a.png"]);
assert!(shell.on_event(&WindowEvent::HoveredFileCancelled));
shell.flush_at(&mut tree, start, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().as_slice(), ["enter 1", "over 10,10", "leave"]);
assert_eq!(shell.next_poll(), None, "hover then cancel: not hovering");
assert!(!tree.has_file_drag());
log.lock().unwrap().clear();
assert!(shell.on_event(&WindowEvent::HoveredFileCancelled));
hovered(&mut shell, &["C:/b.png"]);
shell.flush_at(&mut tree, start + POLL_INTERVAL, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().as_slice(), ["enter 1", "over 10,10"]);
assert!(shell.next_poll().is_some(), "cancel then hover: hovering");
assert!(tree.has_file_drag());
}
#[test]
fn a_motionless_hover_without_a_drop_or_a_cancel_times_out_as_a_leave() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
let position = Arc::new(Mutex::new((10.0_f32, 10.0_f32)));
let read = {
let position = position.clone();
move || {
let (x, y) = *position.lock().unwrap();
at(x, y)
}
};
let start = Instant::now();
hovered(&mut shell, &["C:/a.png"]);
shell.flush_at(&mut tree, start, &read, (0.0, 0.0));
*position.lock().unwrap() = (12.0, 10.0);
shell.flush_at(&mut tree, start + HOVER_TIMEOUT - POLL_INTERVAL, &read, (0.0, 0.0));
assert!(shell.next_poll().is_some(), "still hovering: it moved");
shell.flush_at(&mut tree, start + HOVER_TIMEOUT + POLL_INTERVAL, &read, (0.0, 0.0));
assert!(shell.next_poll().is_some(), "the timeout counts from the last motion");
assert_eq!(log.lock().unwrap().last().map(String::as_str), Some("over 12,10"));
let moved_at = start + HOVER_TIMEOUT - POLL_INTERVAL;
shell.flush_at(&mut tree, moved_at + HOVER_TIMEOUT, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().last().map(String::as_str), Some("leave"));
assert_eq!(shell.next_poll(), None);
assert!(!tree.has_file_drag());
dropped(&mut shell, &["C:/a.png"]);
shell.flush_at(&mut tree, moved_at + HOVER_TIMEOUT + POLL_INTERVAL, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().last().map(String::as_str), Some("drop 1"));
}
fn button_probe() -> (Arc<Mutex<Option<bool>>>, impl Fn() -> PointerProbe) {
let pressed = Arc::new(Mutex::new(Some(true)));
let read = {
let pressed = pressed.clone();
move || PointerProbe {
position: Some((10.0, 10.0)),
any_button_pressed: *pressed.lock().unwrap(),
}
};
(pressed, read)
}
#[test]
fn a_hover_ends_once_no_mouse_button_has_been_pressed_for_the_grace_period() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
let (pressed, read) = button_probe();
let start = Instant::now();
hovered(&mut shell, &["C:/a.png"]);
shell.flush_at(&mut tree, start, &read, (0.0, 0.0));
shell.flush_at(&mut tree, start + POLL_INTERVAL, &read, (0.0, 0.0));
assert!(shell.next_poll().is_some(), "a pressed button keeps the hover");
*pressed.lock().unwrap() = Some(false);
let released_at = start + POLL_INTERVAL * 2;
shell.flush_at(&mut tree, released_at, &read, (0.0, 0.0));
shell.flush_at(
&mut tree,
released_at + BUTTONS_UP_GRACE - POLL_INTERVAL,
&read,
(0.0, 0.0),
);
assert_eq!(
log.lock().unwrap().as_slice(),
["enter 1", "over 10,10"],
"no leave within the grace period"
);
assert!(shell.next_poll().is_some());
*pressed.lock().unwrap() = Some(true);
shell.flush_at(&mut tree, released_at + BUTTONS_UP_GRACE, &read, (0.0, 0.0));
*pressed.lock().unwrap() = Some(false);
let released_again_at = released_at + BUTTONS_UP_GRACE + POLL_INTERVAL;
shell.flush_at(&mut tree, released_again_at, &read, (0.0, 0.0));
shell.flush_at(
&mut tree,
released_again_at + BUTTONS_UP_GRACE - POLL_INTERVAL,
&read,
(0.0, 0.0),
);
assert!(
shell.next_poll().is_some(),
"the grace period counts from the last press"
);
shell.flush_at(&mut tree, released_again_at + BUTTONS_UP_GRACE, &read, (0.0, 0.0));
assert_eq!(log.lock().unwrap().as_slice(), ["enter 1", "over 10,10", "leave"]);
assert_eq!(shell.next_poll(), None);
assert!(!tree.has_file_drag());
}
#[test]
fn a_drop_within_the_grace_period_after_the_release_continues_the_session() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
let (pressed, read) = button_probe();
let start = Instant::now();
hovered(&mut shell, &["C:/a.png"]);
shell.flush_at(&mut tree, start, &read, (0.0, 0.0));
*pressed.lock().unwrap() = Some(false);
shell.flush_at(&mut tree, start + POLL_INTERVAL, &read, (0.0, 0.0));
shell.flush_at(&mut tree, start + POLL_INTERVAL * 2, &read, (0.0, 0.0));
dropped(&mut shell, &["C:/a.png"]);
shell.flush_at(&mut tree, start + POLL_INTERVAL * 3, &read, (0.0, 0.0));
assert_eq!(
log.lock().unwrap().as_slice(),
["enter 1", "over 10,10", "drop 1"],
"no leave, no second enter"
);
assert_eq!(shell.next_poll(), None);
assert!(!tree.has_file_drag());
}
#[test]
fn the_last_pointer_position_the_window_saw_stands_in_where_the_os_gives_none() {
let log = Log::default();
let mut tree = target(&log);
let mut shell = FileDragShell::default();
hovered(&mut shell, &["C:/a.png"]);
shell.flush_at(
&mut tree,
Instant::now(),
&|| PointerProbe {
position: None,
any_button_pressed: None,
},
(40.0, 50.0),
);
assert_eq!(log.lock().unwrap().as_slice(), ["enter 1", "over 40,50"]);
}
}