Pan image::Viewer even if the cursor is out of bounds

This commit is contained in:
Héctor Ramón Jiménez 2020-12-18 10:47:29 +01:00
parent 21b10dc103
commit add167d6a0

View File

@ -215,14 +215,14 @@ where
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
if is_mouse_over {
match event {
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
Event::Mouse(mouse::Event::WheelScrolled { delta })
if is_mouse_over =>
{
match delta {
mouse::ScrollDelta::Lines { y, .. }
| mouse::ScrollDelta::Pixels { y, .. } => {
let previous_scale =
self.state.scale.unwrap_or(1.0);
let previous_scale = self.state.scale.unwrap_or(1.0);
if y < 0.0 && previous_scale > self.min_scale
|| y > 0.0 && previous_scale < self.max_scale
@ -246,8 +246,7 @@ where
/ previous_scale
- 1.0;
let cursor_to_center =
relative_cursor_position(
let cursor_to_center = relative_cursor_position(
cursor_position,
bounds,
) - relative_center(bounds);
@ -257,14 +256,12 @@ where
self.state.current_offset = Vector::new(
if image_size.width > bounds.width {
self.state.current_offset.x
+ adjustment.x
self.state.current_offset.x + adjustment.x
} else {
0.0
},
if image_size.height > bounds.height {
self.state.current_offset.y
+ adjustment.y
self.state.current_offset.y + adjustment.y
} else {
0.0
},
@ -273,7 +270,9 @@ where
}
}
}
Event::Mouse(mouse::Event::ButtonPressed(button)) => {
Event::Mouse(mouse::Event::ButtonPressed(button))
if is_mouse_over =>
{
if button == mouse::Button::Left {
self.state.starting_cursor_pos = Some(cursor_position);
@ -287,21 +286,13 @@ where
}
Event::Mouse(mouse::Event::CursorMoved { position }) => {
if self.state.is_cursor_clicked() {
let image_size =
self.image_size(renderer, bounds.size());
let image_size = self.image_size(renderer, bounds.size());
self.state
.pan(position.x, position.y, bounds, image_size);
self.state.pan(position.x, position.y, bounds, image_size);
}
}
_ => {}
}
} else if let Event::Mouse(mouse::Event::ButtonReleased(button)) = event
{
if button == mouse::Button::Left {
self.state.starting_cursor_pos = None;
}
}
event::Status::Ignored
}