diff --git a/src/app.rs b/src/app.rs index 13b7af5..0f76789 100644 --- a/src/app.rs +++ b/src/app.rs @@ -75,6 +75,8 @@ pub struct App { text: Option, font_tried: bool, pointer: (f64, f64), + /// Whether the pointer is over the window, as far as its last event said. + pointer_in: bool, drag: Option<(f64, f64)>, pub playing: bool, pub anim_due: Option, @@ -149,6 +151,7 @@ impl App { text: None, font_tried: false, pointer: (0.0, 0.0), + pointer_in: false, drag: None, playing: true, anim_due: None, @@ -705,10 +708,37 @@ impl App { // Input. + /// Takes the pointer's place over the window, and says whether it moved. Hyprland + /// sends the pointer's place again, unmoved, when what's under it changes: the bar + /// hiding, or the pointer itself hiding. Taken as motion, that brought both straight + /// back, so a playing video never lost them. Coming back over the window counts as a + /// move even at the same place. + /// + /// Kept for every view, so a pointer that left over the grid still counts as coming + /// back once a video shows again. Events that aren't the pointer's count as moved. + pub(super) fn pointer_moved(&mut self, ev: &Input) -> bool { + match *ev { + Input::Move { x, y } => { + let p = (x * self.scale, y * self.scale); + let moved = !self.pointer_in || p != self.pointer; + (self.pointer, self.pointer_in) = (p, true); + moved + } + Input::Leave => { + self.pointer_in = false; + true + } + _ => true, + } + } + pub fn input(&mut self, wl: &mut Wl, ev: Input) { + let moved = self.pointer_moved(&ev); if self.media.is_some() && self.grid.is_none() { - if let Input::Move { x, y } = ev { - self.pointer = (x * self.scale, y * self.scale); + if !moved { + // Not motion, but the bar may have come or gone under the pointer. + self.refresh_hover(); + return; } if self.media_pointer(wl, &ev) { return; diff --git a/src/app/player_ui.rs b/src/app/player_ui.rs index a472829..184eb4e 100644 --- a/src/app/player_ui.rs +++ b/src/app/player_ui.rs @@ -343,6 +343,17 @@ impl App { self.a11y_dirty = true; } + /// Highlights the control under the pointer, if the bar is shown, without counting as + /// motion: for when the bar came or went while the pointer stayed put. + pub(super) fn refresh_hover(&mut self) { + let (x, y) = (self.pointer.0 as i32, self.pointer.1 as i32); + let hover = if self.controls.shown { self.bar_layout().and_then(|l| l.hit(x, y)) } else { None }; + if hover != self.controls.hover { + self.controls.hover = hover; + self.overlay_bar(); + } + } + /// The pointer, over a video or song. True if it was the player's. pub(super) fn media_pointer(&mut self, wl: &mut Wl, ev: &Input) -> bool { if self.media.is_none() || self.grid.is_some() { @@ -779,6 +790,26 @@ mod tests { a } + #[test] + fn the_pointer_coming_again_to_the_same_place_is_not_motion() { + let t = Instant::now(); + let mut a = playing("clip.mp4", false, false); + a.scale = 2.0; + let at = |x, y| Input::Move { x, y }; + assert!(a.pointer_moved(&at(50.0, 40.0)), "coming over the window counts"); + assert_eq!(a.pointer, (100.0, 80.0), "kept in buffer pixels"); + a.poke_controls(t); + a.expire_controls(t + motion::CONTROLS_HIDE); + assert!(a.pointer_hidden(), "hidden with the bar"); + // Hyprland repeats the place when the bar or the pointer hides. + assert!(!a.pointer_moved(&at(50.0, 40.0)), "the same place again isn't a move"); + a.refresh_hover(); + assert!(a.pointer_hidden() && !a.controls.shown, "so both stay hidden"); + assert!(a.pointer_moved(&at(50.5, 40.0)), "a real move is"); + assert!(a.pointer_moved(&Input::Leave)); + assert!(a.pointer_moved(&at(50.5, 40.0)), "back over the window counts, even at the same place"); + } + #[test] fn the_pointer_hides_with_the_bar_while_a_video_plays_and_motion_brings_it_back() { let t = Instant::now();