Handle touchpad scroll events

This commit is contained in:
Héctor Ramón Jiménez 2019-10-29 19:00:46 +01:00
parent 29588f604a
commit bd5d871eb6
4 changed files with 46 additions and 11 deletions

View file

@ -3,4 +3,4 @@ mod button;
mod event; mod event;
pub use button::Button; pub use button::Button;
pub use event::Event; pub use event::{Event, ScrollDelta};

View file

@ -34,11 +34,22 @@ pub enum Event {
}, },
/// The mouse wheel was scrolled. /// The mouse wheel was scrolled.
WheelScrolled { WheelScrolled { delta: ScrollDelta },
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ScrollDelta {
Lines {
/// The number of horizontal lines scrolled /// The number of horizontal lines scrolled
delta_x: f32, x: f32,
/// The number of vertical lines scrolled /// The number of vertical lines scrolled
delta_y: f32, y: f32,
},
Pixels {
/// The number of horizontal pixels scrolled
x: f32,
/// The number of vertical pixels scrolled
y: f32,
}, },
} }

View file

@ -64,11 +64,16 @@ where
// TODO: Event capture. Nested scrollables should capture scroll events. // TODO: Event capture. Nested scrollables should capture scroll events.
if is_mouse_over { if is_mouse_over {
match event { match event {
Event::Mouse(mouse::Event::WheelScrolled { Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
delta_y, .. match delta {
}) => { mouse::ScrollDelta::Lines { y, .. } => {
// TODO: Configurable speed (?) // TODO: Configurable speed (?)
self.state.scroll(delta_y * 15.0, bounds, content_bounds); self.state.scroll(y * 15.0, bounds, content_bounds);
}
mouse::ScrollDelta::Pixels { y, .. } => {
self.state.scroll(y, bounds, content_bounds);
}
}
} }
_ => {} _ => {}
} }

View file

@ -123,6 +123,7 @@ pub trait Application {
.. ..
} => match window_event { } => match window_event {
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
// TODO: Remove when renderer supports HiDPI
let physical_position = let physical_position =
position.to_physical(window.hidpi_factor()); position.to_physical(window.hidpi_factor());
@ -143,10 +144,28 @@ pub trait Application {
delta_y, delta_y,
) => { ) => {
events.push(Event::Mouse( events.push(Event::Mouse(
mouse::Event::WheelScrolled { delta_x, delta_y }, mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Lines {
x: delta_x,
y: delta_y,
},
},
));
}
winit::event::MouseScrollDelta::PixelDelta(position) => {
// TODO: Remove when renderer supports HiDPI
let physical_position =
position.to_physical(window.hidpi_factor());
events.push(Event::Mouse(
mouse::Event::WheelScrolled {
delta: mouse::ScrollDelta::Pixels {
x: physical_position.x as f32,
y: physical_position.y as f32,
},
},
)); ));
} }
_ => {}
}, },
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit; *control_flow = ControlFlow::Exit;