iced-yoda/graphics/src/widget/image/viewer.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

//! Zoom and pan on an image.
use crate::backend::{self, Backend};
2020-04-23 15:34:55 -07:00
use crate::{Primitive, Renderer};
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
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
}
},
{
if state.is_cursor_grabbed() {
mouse::Interaction::Grabbing
} else if is_mouse_over
2020-12-18 10:44:24 +01:00
&& (image_size.width > bounds.width
|| image_size.height > bounds.height)
{
mouse::Interaction::Grab
2020-04-23 15:34:55 -07:00
} else {
mouse::Interaction::Idle
2020-04-23 15:34:55 -07:00
}
},
)
}
}