summaryrefslogtreecommitdiffstats
path: root/native/src/widget/image/viewer.rs
blob: af6d960ba5b08c52353b360e043886ed44ab7779 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Zoom and pan on an image.
use crate::{
    image, layout, mouse, Clipboard, Element, Event, Hasher, Layout, Length,
    Point, Rectangle, Size, Vector, Widget,
};

use std::{f32, hash::Hash, u32};

/// A widget that can display an image with the ability to zoom in/out and pan.
#[allow(missing_debug_implementations)]
pub struct Viewer<'a> {
    state: &'a mut State,
    padding: u16,
    width: Length,
    height: Length,
    max_width: u32,
    max_height: u32,
    min_scale: f32,
    max_scale: f32,
    scale_pct: f32,
    handle: image::Handle,
}

impl<'a> Viewer<'a> {
    /// Creates a new [`Viewer`] with the given [`State`] and [`Handle`].
    ///
    /// [`Viewer`]: struct.Viewer.html
    /// [`State`]: struct.State.html
    /// [`Handle`]: ../../image/struct.Handle.html
    pub fn new(state: &'a mut State, handle: image::Handle) -> Self {
        Viewer {
            state,
            padding: 0,
            width: Length::Shrink,
            height: Length::Shrink,
            max_width: u32::MAX,
            max_height: u32::MAX,
            min_scale: 0.25,
            max_scale: 10.0,
            scale_pct: 0.10,
            handle,
        }
    }

    /// Sets the padding of the [`Viewer`].
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn padding(mut self, units: u16) -> Self {
        self.padding = units;
        self
    }

    /// Sets the width of the [`Viewer`].
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    /// Sets the height of the [`Viewer`].
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn height(mut self, height: Length) -> Self {
        self.height = height;
        self
    }

    /// Sets the max width of the [`Viewer`].
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn max_width(mut self, max_width: u32) -> Self {
        self.max_width = max_width;
        self
    }

    /// Sets the max height of the [`Viewer`].
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn max_height(mut self, max_height: u32) -> Self {
        self.max_height = max_height;
        self
    }

    /// Sets the max scale applied to the image of the [`Viewer`].
    ///
    /// Default is `10.0`
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn max_scale(mut self, max_scale: f32) -> Self {
        self.max_scale = max_scale;
        self
    }

    /// Sets the min scale applied to the image of the [`Viewer`].
    ///
    /// Default is `0.25`
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn min_scale(mut self, min_scale: f32) -> Self {
        self.min_scale = min_scale;
        self
    }

    /// Sets the percentage the image of the [`Viewer`] will be scaled by
    /// when zoomed in / out.
    ///
    /// Default is `0.10`
    ///
    /// [`Viewer`]: struct.Viewer.html
    pub fn scale_pct(mut self, scale_pct: f32) -> Self {
        self.scale_pct = scale_pct;
        self
    }

    /// Returns the bounds of the underlying image, given the bounds of
    /// the [`Viewer`]. Scaling will be applied and original aspect ratio
    /// will be respected.
    ///
    /// [`Viewer`]: struct.Viewer.html
    fn image_bounds<Renderer>(
        &self,
        renderer: &Renderer,
        bounds: Rectangle,
    ) -> Rectangle
    where
        Renderer: self::Renderer + image::Renderer,
    {
        let (width, height) = renderer.dimensions(&self.handle);

        let dimensions = {
            let dimensions = (width as f32, height as f32);

            let width_ratio = bounds.width / dimensions.0;
            let height_ratio = bounds.height / dimensions.1;

            let ratio = width_ratio.min(height_ratio);

            let scale = self.state.scale.unwrap_or(1.0);

            if ratio < 1.0 {
                (dimensions.0 * ratio * scale, dimensions.1 * ratio * scale)
            } else {
                (dimensions.0 * scale, dimensions.1 * scale)
            }
        };

        Rectangle {
            x: bounds.x,
            y: bounds.y,
            width: dimensions.0,
            height: dimensions.1,
        }
    }

    /// Cursor position relative to the [`Viewer`] bounds.
    ///
    /// [`Viewer`]: struct.Viewer.html
    fn relative_cursor_position(
        &self,
        mut absolute_position: Point,
        bounds: Rectangle,
    ) -> Point {
        absolute_position.x -= bounds.x;
        absolute_position.y -= bounds.y;
        absolute_position
    }

    /// Center point relative to the [`Viewer`] bounds.
    ///
    /// [`Viewer`]: struct.Viewer.html
    fn relative_center(&self, bounds: Rectangle) -> Point {
        let mut center = bounds.center();
        center.x -= bounds.x;
        center.y -= bounds.y;
        center
    }
}

impl<'a, Message, Renderer> Widget<Message, Renderer> for Viewer<'a>
where
    Renderer: self::Renderer + image::Renderer,
{
    fn width(&self) -> Length {
        self.width
    }

    fn height(&self) -> Length {
        self.height
    }

    fn layout(
        &self,
        _renderer: &Renderer,
        limits: &layout::Limits,
    ) -> layout::Node {
        let padding = f32::from(self.padding);

        let limits = limits
            .max_width(self.max_width)
            .max_height(self.max_height)
            .width(self.width)
            .height(self.height)
            .pad(padding);

        let size = limits.resolve(Size::INFINITY);

        layout::Node::new(size)
    }

    fn on_event(
        &mut self,
        event: Event,
        layout: Layout<'_>,
        cursor_position: Point,
        _messages: &mut Vec<Message>,
        renderer: &Renderer,
        _clipboard: Option<&dyn Clipboard>,
    ) {
        let bounds = layout.bounds();
        let is_mouse_over = bounds.contains(cursor_position);

        if is_mouse_over {
            match event {
                Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
                    match delta {
                        mouse::ScrollDelta::Lines { y, .. }
                        | mouse::ScrollDelta::Pixels { y, .. } => {
                            let previous_scale =
                                self.state.scale.unwrap_or(1.0);

                            if y < 0.0 && previous_scale > self.min_scale
                                || y > 0.0 && previous_scale < self.max_scale
                            {
                                self.state.scale = Some(
                                    (if y > 0.0 {
                                        self.state.scale.unwrap_or(1.0)
                                            * (1.0 + self.scale_pct)
                                    } else {
                                        self.state.scale.unwrap_or(1.0)
                                            / (1.0 + self.scale_pct)
                                    })
                                    .max(self.min_scale)
                                    .min(self.max_scale),
                                );

                                let image_bounds =
                                    self.image_bounds(renderer, bounds);

                                let factor = self.state.scale.unwrap()
                                    / previous_scale
                                    - 1.0;

                                let cursor_to_center =
                                    self.relative_cursor_position(
                                        cursor_position,
                                        bounds,
                                    ) - self.relative_center(bounds);

                                let adjustment = cursor_to_center * factor
                                    + self.state.current_offset * factor;

                                self.state.current_offset = Vector::new(
                                    if image_bounds.width > bounds.width {
                                        self.state.current_offset.x
                                            + adjustment.x
                                    } else {
                                        0.0
                                    },
                                    if image_bounds.height > bounds.height {
                                        self.state.current_offset.y
                                            + adjustment.y
                                    } else {
                                        0.0
                                    },
                                );
                            }
                        }
                    }
                }
                Event::Mouse(mouse::Event::ButtonPressed(button)) => {
                    if button == mouse::Button::Left {
                        self.state.starting_cursor_pos = Some(cursor_position);

                        self.state.starting_offset = self.state.current_offset;
                    }
                }
                Event::Mouse(mouse::Event::ButtonReleased(button)) => {
                    if button == mouse::Button::Left {
                        self.state.starting_cursor_pos = None
                    }
                }
                Event::Mouse(mouse::Event::CursorMoved { x, y }) => {
                    if self.state.is_cursor_clicked() {
                        let image_bounds = self.image_bounds(renderer, bounds);

                        self.state.pan(x, y, bounds, image_bounds);
                    }
                }
                _ => {}
            }
        } else if let Event::Mouse(mouse::Event::ButtonReleased(button)) = event
        {
            if button == mouse::Button::Left {
                self.state.starting_cursor_pos = None;
            }
        }
    }

    fn draw(
        &self,
        renderer: &mut Renderer,
        _defaults: &Renderer::Defaults,
        layout: Layout<'_>,
        cursor_position: Point,
    ) -> Renderer::Output {
        let bounds = layout.bounds();

        let image_bounds = self.image_bounds(renderer, bounds);

        let translation = {
            let image_top_left = Vector::new(
                bounds.width / 2.0 - image_bounds.width / 2.0,
                bounds.height / 2.0 - image_bounds.height / 2.0,
            );

            image_top_left - self.state.offset(bounds, image_bounds)
        };

        let is_mouse_over = bounds.contains(cursor_position);

        self::Renderer::draw(
            renderer,
            &self.state,
            bounds,
            image_bounds,
            translation,
            self.handle.clone(),
            is_mouse_over,
        )
    }

    fn hash_layout(&self, state: &mut Hasher) {
        struct Marker;
        std::any::TypeId::of::<Marker>().hash(state);

        self.width.hash(state);
        self.height.hash(state);
        self.max_width.hash(state);
        self.max_height.hash(state);
        self.padding.hash(state);

        self.handle.hash(state);
    }
}

/// The local state of a [`Viewer`].
///
/// [`Viewer`]: struct.Viewer.html
#[derive(Debug, Clone, Copy, Default)]
pub struct State {
    scale: Option<f32>,
    starting_offset: Vector,
    current_offset: Vector,
    starting_cursor_pos: Option<Point>,
}

impl State {
    /// Creates a new [`State`].
    ///
    /// [`State`]: struct.State.html
    pub fn new() -> Self {
        State::default()
    }

    /// Apply a panning offset to the current [`State`], given the bounds of
    /// the [`Viewer`] and its image.
    ///
    /// [`Viewer`]: struct.Viewer.html
    /// [`State`]: struct.State.html
    fn pan(
        &mut self,
        x: f32,
        y: f32,
        bounds: Rectangle,
        image_bounds: Rectangle,
    ) {
        let hidden_width = ((image_bounds.width - bounds.width) as f32 / 2.0)
            .max(0.0)
            .round();
        let hidden_height = ((image_bounds.height - bounds.height) as f32
            / 2.0)
            .max(0.0)
            .round();

        let delta_x = x - self.starting_cursor_pos.unwrap().x;
        let delta_y = y - self.starting_cursor_pos.unwrap().y;

        if bounds.width < image_bounds.width {
            self.current_offset.x = (self.starting_offset.x - delta_x)
                .min(hidden_width)
                .max(-1.0 * hidden_width);
        }

        if bounds.height < image_bounds.height {
            self.current_offset.y = (self.starting_offset.y - delta_y)
                .min(hidden_height)
                .max(-1.0 * hidden_height);
        }
    }

    /// Returns the current offset of the [`State`], given the bounds
    /// of the [`Viewer`] and its image.
    ///
    /// [`Viewer`]: struct.Viewer.html
    /// [`State`]: struct.State.html
    fn offset(&self, bounds: Rectangle, image_bounds: Rectangle) -> Vector {
        let hidden_width = ((image_bounds.width - bounds.width) as f32 / 2.0)
            .max(0.0)
            .round();
        let hidden_height = ((image_bounds.height - bounds.height) as f32
            / 2.0)
            .max(0.0)
            .round();

        Vector::new(
            self.current_offset
                .x
                .min(hidden_width)
                .max(-1.0 * hidden_width),
            self.current_offset
                .y
                .min(hidden_height)
                .max(-1.0 * hidden_height),
        )
    }

    /// Returns if the left mouse button is still held down since clicking inside
    /// the [`Viewer`].
    ///
    /// [`Viewer`]: struct.Viewer.html
    /// [`State`]: struct.State.html
    pub fn is_cursor_clicked(&self) -> bool {
        self.starting_cursor_pos.is_some()
    }
}

/// The renderer of an [`Viewer`].
///
/// Your [renderer] will need to implement this trait before being
/// able to use a [`Viewer`] in your user interface.
///
/// [`Viewer`]: struct.Viewer.html
/// [renderer]: ../../../renderer/index.html
pub trait Renderer: crate::Renderer + Sized {
    /// Draws the [`Viewer`].
    ///
    /// It receives:
    /// - the [`State`] of the [`Viewer`]
    /// - the bounds of the [`Viewer`] widget
    /// - the bounds of the scaled [`Viewer`] image
    /// - the translation of the clipped image
    /// - the [`Handle`] to the underlying image
    /// - whether the mouse is over the [`Viewer`] or not
    ///
    /// [`Viewer`]: struct.Viewer.html
    /// [`State`]: struct.State.html
    /// [`Handle`]: ../../image/struct.Handle.html
    fn draw(
        &mut self,
        state: &State,
        bounds: Rectangle,
        image_bounds: Rectangle,
        translation: Vector,
        handle: image::Handle,
        is_mouse_over: bool,
    ) -> Self::Output;
}

impl<'a, Message, Renderer> From<Viewer<'a>> for Element<'a, Message, Renderer>
where
    Renderer: 'a + self::Renderer + image::Renderer,
    Message: 'a,
{
    fn from(viewer: Viewer<'a>) -> Element<'a, Message, Renderer> {
        Element::new(viewer)
    }
}