From 61543d99b35fb7a4ed988b101234200e2bcfceb2 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Tue, 24 Jul 2018 23:09:09 -0700 Subject: [PATCH] Fix event priority in ScrollView --- src/vec.rs | 9 +++++++++ src/views/scroll_view.rs | 34 +++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/vec.rs b/src/vec.rs index ba29d44..bba029e 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -180,6 +180,15 @@ impl XY { } } +impl<'a, T> From<&'a XY> for XY +where + T: Clone, +{ + fn from(t: &'a XY) -> Self { + t.clone() + } +} + // Anything that can become XY can also become XY impl From for XY where diff --git a/src/views/scroll_view.rs b/src/views/scroll_view.rs index 651a731..cb0b968 100644 --- a/src/views/scroll_view.rs +++ b/src/views/scroll_view.rs @@ -449,11 +449,35 @@ where fn on_event(&mut self, event: Event) -> EventResult { // Relativize event accorging to the offset let mut relative_event = event.clone(); - // eprintln!("Mouse = {:?}", relative_event); - if let Some(pos) = relative_event.mouse_position_mut() { - *pos = *pos + self.offset; - } - match self.inner.on_event(relative_event) { + + // Should the event be treated inside, by the inner view? + let inside = if let Event::Mouse { + ref mut position, + ref offset, + .. + } = relative_event + { + // For mouse events, check if it falls inside the available area + let inside = position + .checked_sub(offset) + .map(|p| p.fits_in(self.available_size())) + .unwrap_or(false); + *position = *position + self.offset; + inside + } else { + // For key events, assume it's inside by default. + true + }; + + let result = if inside { + // If the event is inside, give it to the child. + self.inner.on_event(relative_event) + } else { + // Otherwise, pretend it wasn't there. + EventResult::Ignored + }; + + match result { EventResult::Ignored => { // If it's an arrow, try to scroll in the given direction. // If it's a mouse scroll, try to scroll as well.