Rename starting_cursor_pos to cursor_grabbed_at in image::Viewer

This commit is contained in:
Héctor Ramón Jiménez 2020-12-18 11:04:07 +01:00
parent 8245a11766
commit 43ef85ae5c
2 changed files with 11 additions and 12 deletions

View File

@ -39,7 +39,7 @@ where
} }
}, },
{ {
if state.is_cursor_clicked() { if state.is_cursor_grabbed() {
mouse::Interaction::Grabbing mouse::Interaction::Grabbing
} else if is_mouse_over } else if is_mouse_over
&& (image_size.width > bounds.width && (image_size.width > bounds.width

View File

@ -268,14 +268,14 @@ where
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
if is_mouse_over => if is_mouse_over =>
{ {
self.state.starting_cursor_pos = Some(cursor_position); self.state.cursor_grabbed_at = Some(cursor_position);
self.state.starting_offset = self.state.current_offset; self.state.starting_offset = self.state.current_offset;
} }
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => { Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
self.state.starting_cursor_pos = None self.state.cursor_grabbed_at = None
} }
Event::Mouse(mouse::Event::CursorMoved { position }) Event::Mouse(mouse::Event::CursorMoved { position })
if self.state.is_cursor_clicked() => if self.state.is_cursor_grabbed() =>
{ {
let image_size = self.image_size(renderer, bounds.size()); let image_size = self.image_size(renderer, bounds.size());
@ -343,7 +343,7 @@ pub struct State {
scale: f32, scale: f32,
starting_offset: Vector, starting_offset: Vector,
current_offset: Vector, current_offset: Vector,
starting_cursor_pos: Option<Point>, cursor_grabbed_at: Option<Point>,
} }
impl Default for State { impl Default for State {
@ -352,7 +352,7 @@ impl Default for State {
scale: 1.0, scale: 1.0,
starting_offset: Vector::default(), starting_offset: Vector::default(),
current_offset: Vector::default(), current_offset: Vector::default(),
starting_cursor_pos: None, cursor_grabbed_at: None,
} }
} }
} }
@ -377,8 +377,8 @@ impl State {
let hidden_height = let hidden_height =
(image_size.height - bounds.height / 2.0).max(0.0).round(); (image_size.height - bounds.height / 2.0).max(0.0).round();
let delta_x = x - self.starting_cursor_pos.unwrap().x; let delta_x = x - self.cursor_grabbed_at.unwrap().x;
let delta_y = y - self.starting_cursor_pos.unwrap().y; let delta_y = y - self.cursor_grabbed_at.unwrap().y;
if bounds.width < image_size.width { if bounds.width < image_size.width {
self.current_offset.x = (self.starting_offset.x - delta_x) self.current_offset.x = (self.starting_offset.x - delta_x)
@ -411,13 +411,12 @@ impl State {
) )
} }
/// Returns if the left mouse button is still held down since clicking inside /// Returns if the cursor is currently grabbed by the [`Viewer`].
/// the [`Viewer`].
/// ///
/// [`Viewer`]: struct.Viewer.html /// [`Viewer`]: struct.Viewer.html
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn is_cursor_clicked(&self) -> bool { pub fn is_cursor_grabbed(&self) -> bool {
self.starting_cursor_pos.is_some() self.cursor_grabbed_at.is_some()
} }
} }