summaryrefslogtreecommitdiffstats
path: root/graphics/src/widget/image/viewer.rs
blob: 0bcd09fcc096d1c731e114fad7d059e7a06f8ddc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! Zoom and pan on an image.
use crate::backend::{self, Backend};
use crate::{Primitive, Renderer};

use iced_native::image;
use iced_native::image::viewer;
use iced_native::mouse;
use iced_native::{Rectangle, Vector};

impl<B> viewer::Renderer for Renderer<B>
where
    B: Backend + backend::Image,
{
    fn draw(
        &mut self,
        state: &viewer::State,
        bounds: Rectangle,
        image_bounds: Rectangle,
        translation: Vector,
        handle: image::Handle,
        is_mouse_over: bool,
    ) -> Self::Output {
        (
            {
                Primitive::Clip {
                    bounds,
                    content: Box::new(Primitive::Translate {
                        translation,
                        content: Box::new(Primitive::Image {
                            handle,
                            bounds: image_bounds,
                        }),
                    }),
                    offset: Vector::new(0, 0),
                }
            },
            {
                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
                } else {
                    mouse::Interaction::Idle
                }
            },
        )
    }
}