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