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

52 lines
1.4 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};
2020-05-27 14:20:07 -07:00
use iced_native::{
image::{self, viewer},
mouse, Rectangle, 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,
image_bounds: Rectangle,
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,
bounds: image_bounds,
}),
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_clicked() {
mouse::Interaction::Grabbing
} else if is_mouse_over
&& (image_bounds.width > bounds.width
|| image_bounds.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
}
},
)
}
}