2020-12-18 10:15:30 +01:00
|
|
|
//! Zoom and pan on an image.
|
|
|
|
|
use crate::backend::{self, Backend};
|
2020-04-23 15:34:55 -07:00
|
|
|
use crate::{Primitive, Renderer};
|
2020-12-18 10:15:30 +01:00
|
|
|
|
2020-12-18 10:18:39 +01:00
|
|
|
use iced_native::image;
|
|
|
|
|
use iced_native::image::viewer;
|
|
|
|
|
use iced_native::mouse;
|
2020-12-18 10:44:24 +01:00
|
|
|
use iced_native::{Rectangle, Size, Vector};
|
2020-04-23 15:34:55 -07:00
|
|
|
|
2020-12-18 10:15:30 +01:00
|
|
|
impl<B> viewer::Renderer for Renderer<B>
|
|
|
|
|
where
|
|
|
|
|
B: Backend + backend::Image,
|
|
|
|
|
{
|
2020-04-23 15:34:55 -07:00
|
|
|
fn draw(
|
|
|
|
|
&mut self,
|
2020-05-27 14:20:07 -07:00
|
|
|
state: &viewer::State,
|
2020-04-23 15:34:55 -07:00
|
|
|
bounds: Rectangle,
|
2020-12-18 10:44:24 +01:00
|
|
|
image_size: Size,
|
2020-05-27 13:39:26 -07:00
|
|
|
translation: Vector,
|
2020-04-23 15:34:55 -07:00
|
|
|
handle: image::Handle,
|
|
|
|
|
is_mouse_over: bool,
|
|
|
|
|
) -> Self::Output {
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
Primitive::Clip {
|
|
|
|
|
bounds,
|
2020-05-27 13:39:26 -07:00
|
|
|
content: Box::new(Primitive::Translate {
|
|
|
|
|
translation,
|
|
|
|
|
content: Box::new(Primitive::Image {
|
|
|
|
|
handle,
|
2020-12-18 10:44:24 +01:00
|
|
|
bounds: Rectangle {
|
|
|
|
|
x: bounds.x,
|
|
|
|
|
y: bounds.y,
|
|
|
|
|
..Rectangle::with_size(image_size)
|
|
|
|
|
},
|
2020-05-27 13:39:26 -07:00
|
|
|
}),
|
2020-04-23 15:34:55 -07:00
|
|
|
}),
|
2020-05-27 13:39:26 -07:00
|
|
|
offset: Vector::new(0, 0),
|
2020-04-23 15:34:55 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2020-12-18 11:04:07 +01:00
|
|
|
if state.is_cursor_grabbed() {
|
2020-05-14 11:54:05 -07:00
|
|
|
mouse::Interaction::Grabbing
|
2020-04-23 16:22:53 -07:00
|
|
|
} else if is_mouse_over
|
2020-12-18 10:44:24 +01:00
|
|
|
&& (image_size.width > bounds.width
|
|
|
|
|
|| image_size.height > bounds.height)
|
2020-04-23 16:22:53 -07:00
|
|
|
{
|
2020-05-14 11:54:05 -07:00
|
|
|
mouse::Interaction::Grab
|
2020-04-23 15:34:55 -07:00
|
|
|
} else {
|
2020-05-14 11:54:05 -07:00
|
|
|
mouse::Interaction::Idle
|
2020-04-23 15:34:55 -07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|