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

471 lines
14 KiB
Rust
Raw Normal View History

2020-04-23 15:34:55 -07:00
//! Zoom and pan on an image.
use crate::event::{self, Event};
use crate::image;
use crate::layout;
use crate::mouse;
2020-04-23 15:34:55 -07:00
use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector,
Widget,
2020-04-23 15:34:55 -07:00
};
use std::{f32, hash::Hash, u32};
2020-05-27 14:16:38 -07:00
/// A frame that displays an image with the ability to zoom in/out and pan.
2020-04-23 15:34:55 -07:00
#[allow(missing_debug_implementations)]
2020-05-26 17:15:55 -07:00
pub struct Viewer<'a> {
2020-04-23 15:34:55 -07:00
state: &'a mut State,
padding: u16,
width: Length,
height: Length,
max_width: u32,
max_height: u32,
2020-05-27 13:39:26 -07:00
min_scale: f32,
max_scale: f32,
scale_step: f32,
2020-04-23 15:34:55 -07:00
handle: image::Handle,
}
2020-05-26 17:15:55 -07:00
impl<'a> Viewer<'a> {
/// Creates a new [`Viewer`] with the given [`State`] and [`Handle`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
/// [`State`]: struct.State.html
2020-05-26 17:15:55 -07:00
/// [`Handle`]: ../../image/struct.Handle.html
2020-04-23 15:34:55 -07:00
pub fn new(state: &'a mut State, handle: image::Handle) -> Self {
2020-05-26 17:15:55 -07:00
Viewer {
2020-04-23 15:34:55 -07:00
state,
padding: 0,
width: Length::Shrink,
height: Length::Shrink,
max_width: u32::MAX,
max_height: u32::MAX,
2020-05-27 13:39:26 -07:00
min_scale: 0.25,
max_scale: 10.0,
scale_step: 0.10,
2020-04-23 15:34:55 -07:00
handle,
}
}
2020-05-26 17:15:55 -07:00
/// Sets the padding of the [`Viewer`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
pub fn padding(mut self, units: u16) -> Self {
self.padding = units;
self
}
2020-05-26 17:15:55 -07:00
/// Sets the width of the [`Viewer`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
pub fn width(mut self, width: Length) -> Self {
self.width = width;
self
}
2020-05-26 17:15:55 -07:00
/// Sets the height of the [`Viewer`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
pub fn height(mut self, height: Length) -> Self {
self.height = height;
self
}
2020-05-26 17:15:55 -07:00
/// Sets the max width of the [`Viewer`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
pub fn max_width(mut self, max_width: u32) -> Self {
self.max_width = max_width;
self
}
2020-05-26 17:15:55 -07:00
/// Sets the max height of the [`Viewer`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
pub fn max_height(mut self, max_height: u32) -> Self {
self.max_height = max_height;
self
}
2020-05-27 13:39:26 -07:00
/// 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_step(mut self, scale_step: f32) -> Self {
self.scale_step = scale_step;
2020-05-27 13:39:26 -07:00
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
2020-12-18 10:44:24 +01:00
fn image_size<Renderer>(&self, renderer: &Renderer, bounds: Size) -> Size
2020-05-27 13:39:26 -07:00
where
Renderer: self::Renderer + image::Renderer,
{
let (width, height) = renderer.dimensions(&self.handle);
let (width, height) = {
2020-05-27 13:39:26 -07:00
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;
2020-05-27 13:39:26 -07:00
if ratio < 1.0 {
(dimensions.0 * ratio * scale, dimensions.1 * ratio * scale)
} else {
(dimensions.0 * scale, dimensions.1 * scale)
}
};
2020-12-18 10:44:24 +01:00
Size::new(width, height)
2020-05-27 13:39:26 -07:00
}
}
2020-05-27 13:39:26 -07:00
/// Cursor position relative to the [`Viewer`] bounds.
///
/// [`Viewer`]: struct.Viewer.html
fn relative_cursor_position(
absolute_position: Point,
bounds: Rectangle,
) -> Point {
absolute_position - Vector::new(bounds.x, bounds.y)
}
2020-05-27 13:39:26 -07:00
/// Center point relative to the [`Viewer`] bounds.
///
/// [`Viewer`]: struct.Viewer.html
fn relative_center(bounds: Rectangle) -> Point {
bounds.center() - Vector::new(bounds.x, bounds.y)
2020-04-23 15:34:55 -07:00
}
2020-05-26 17:15:55 -07:00
impl<'a, Message, Renderer> Widget<Message, Renderer> for Viewer<'a>
2020-04-23 15:34:55 -07:00
where
Renderer: self::Renderer + image::Renderer,
{
fn width(&self) -> Length {
self.width
}
fn height(&self) -> Length {
self.height
}
fn layout(
&self,
2020-12-18 10:44:24 +01:00
renderer: &Renderer,
2020-04-23 15:34:55 -07:00
limits: &layout::Limits,
) -> layout::Node {
2020-12-18 10:44:24 +01:00
let (width, height) = renderer.dimensions(&self.handle);
2020-04-23 15:34:55 -07:00
2020-12-18 10:44:24 +01:00
let aspect_ratio = width as f32 / height as f32;
let mut size = limits
2020-04-23 15:34:55 -07:00
.width(self.width)
.height(self.height)
2020-12-18 10:44:24 +01:00
.resolve(Size::new(width as f32, height as f32));
let viewport_aspect_ratio = size.width / size.height;
2020-04-23 15:34:55 -07:00
2020-12-18 10:44:24 +01:00
if viewport_aspect_ratio > aspect_ratio {
size.width = width as f32 * size.height / height as f32;
} else {
size.height = height as f32 * size.width / width as f32;
}
2020-04-23 15:34:55 -07:00
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>,
) -> event::Status {
2020-04-23 15:34:55 -07:00
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
match event {
Event::Mouse(mouse::Event::WheelScrolled { delta })
if is_mouse_over =>
{
match delta {
mouse::ScrollDelta::Lines { y, .. }
| mouse::ScrollDelta::Pixels { y, .. } => {
let previous_scale = self.state.scale;
if y < 0.0 && previous_scale > self.min_scale
|| y > 0.0 && previous_scale < self.max_scale
{
self.state.scale = (if y > 0.0 {
self.state.scale * (1.0 + self.scale_step)
} else {
self.state.scale / (1.0 + self.scale_step)
})
.max(self.min_scale)
.min(self.max_scale);
let image_size =
self.image_size(renderer, bounds.size());
let factor =
self.state.scale / previous_scale - 1.0;
let cursor_to_center = relative_cursor_position(
cursor_position,
bounds,
) - relative_center(bounds);
let adjustment = cursor_to_center * factor
+ self.state.current_offset * factor;
self.state.current_offset = Vector::new(
if image_size.width > bounds.width {
self.state.current_offset.x + adjustment.x
} else {
0.0
},
if image_size.height > bounds.height {
self.state.current_offset.y + adjustment.y
} else {
0.0
},
);
2020-04-23 15:34:55 -07:00
}
}
}
}
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
if is_mouse_over =>
{
self.state.starting_cursor_pos = Some(cursor_position);
self.state.starting_offset = self.state.current_offset;
}
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
self.state.starting_cursor_pos = None
}
Event::Mouse(mouse::Event::CursorMoved { position })
if self.state.is_cursor_clicked() =>
{
let image_size = self.image_size(renderer, bounds.size());
2020-12-18 10:44:24 +01:00
self.state.pan(position.x, position.y, bounds, image_size);
2020-04-23 15:34:55 -07:00
}
_ => {}
2020-04-23 15:34:55 -07:00
}
event::Status::Ignored
2020-04-23 15:34:55 -07:00
}
fn draw(
&self,
renderer: &mut Renderer,
_defaults: &Renderer::Defaults,
layout: Layout<'_>,
cursor_position: Point,
_viewport: &Rectangle,
2020-04-23 15:34:55 -07:00
) -> Renderer::Output {
let bounds = layout.bounds();
2020-12-18 10:44:24 +01:00
let image_size = self.image_size(renderer, bounds.size());
2020-04-23 15:34:55 -07:00
2020-05-27 13:39:26 -07:00
let translation = {
let image_top_left = Vector::new(
2020-12-18 10:44:24 +01:00
bounds.width / 2.0 - image_size.width / 2.0,
bounds.height / 2.0 - image_size.height / 2.0,
2020-05-27 13:39:26 -07:00
);
2020-04-23 15:34:55 -07:00
2020-12-18 10:44:24 +01:00
image_top_left - self.state.offset(bounds, image_size)
2020-04-23 15:34:55 -07:00
};
let is_mouse_over = bounds.contains(cursor_position);
self::Renderer::draw(
renderer,
&self.state,
bounds,
2020-12-18 10:44:24 +01:00
image_size,
2020-05-27 13:39:26 -07:00
translation,
2020-04-23 15:34:55 -07:00
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);
}
}
2020-05-26 17:15:55 -07:00
/// The local state of a [`Viewer`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
#[derive(Debug, Clone, Copy)]
2020-04-23 15:34:55 -07:00
pub struct State {
scale: f32,
2020-05-27 13:39:26 -07:00
starting_offset: Vector,
current_offset: Vector,
starting_cursor_pos: Option<Point>,
2020-04-23 15:34:55 -07:00
}
impl Default for State {
fn default() -> Self {
Self {
scale: 1.0,
starting_offset: Vector::default(),
current_offset: Vector::default(),
starting_cursor_pos: None,
}
}
}
2020-04-23 15:34:55 -07:00
impl State {
2020-05-15 09:46:22 -07:00
/// Creates a new [`State`].
2020-04-23 15:34:55 -07:00
///
/// [`State`]: struct.State.html
pub fn new() -> Self {
State::default()
}
/// Apply a panning offset to the current [`State`], given the bounds of
2020-05-26 17:15:55 -07:00
/// the [`Viewer`] and its image.
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
/// [`State`]: struct.State.html
2020-12-18 10:44:24 +01:00
fn pan(&mut self, x: f32, y: f32, bounds: Rectangle, image_size: Size) {
let hidden_width =
(image_size.width - bounds.width / 2.0).max(0.0).round();
let hidden_height =
(image_size.height - bounds.height / 2.0).max(0.0).round();
2020-05-27 13:39:26 -07:00
let delta_x = x - self.starting_cursor_pos.unwrap().x;
let delta_y = y - self.starting_cursor_pos.unwrap().y;
2020-04-23 15:34:55 -07:00
2020-12-18 10:44:24 +01:00
if bounds.width < image_size.width {
2020-05-27 13:39:26 -07:00
self.current_offset.x = (self.starting_offset.x - delta_x)
.min(hidden_width)
.max(-1.0 * hidden_width);
2020-04-23 15:34:55 -07:00
}
2020-12-18 10:44:24 +01:00
if bounds.height < image_size.height {
2020-05-27 13:39:26 -07:00
self.current_offset.y = (self.starting_offset.y - delta_y)
.min(hidden_height)
.max(-1.0 * hidden_height);
2020-04-23 15:34:55 -07:00
}
}
2020-05-27 13:39:26 -07:00
/// Returns the current offset of the [`State`], given the bounds
/// of the [`Viewer`] and its image.
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
/// [`State`]: struct.State.html
2020-12-18 10:44:24 +01:00
fn offset(&self, bounds: Rectangle, image_size: Size) -> Vector {
let hidden_width =
(image_size.width - bounds.width / 2.0).max(0.0).round();
let hidden_height =
(image_size.height - bounds.height / 2.0).max(0.0).round();
2020-05-27 13:39:26 -07:00
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),
2020-04-23 15:34:55 -07:00
)
}
/// Returns if the left mouse button is still held down since clicking inside
2020-05-26 17:15:55 -07:00
/// the [`Viewer`].
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
/// [`State`]: struct.State.html
pub fn is_cursor_clicked(&self) -> bool {
self.starting_cursor_pos.is_some()
}
}
2020-05-26 17:15:55 -07:00
/// The renderer of an [`Viewer`].
2020-04-23 15:34:55 -07:00
///
/// Your [renderer] will need to implement this trait before being
2020-05-26 17:15:55 -07:00
/// able to use a [`Viewer`] in your user interface.
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
/// [renderer]: ../../../renderer/index.html
2020-04-23 15:34:55 -07:00
pub trait Renderer: crate::Renderer + Sized {
2020-05-26 17:15:55 -07:00
/// Draws the [`Viewer`].
2020-04-23 15:34:55 -07:00
///
/// It receives:
2020-05-26 17:15:55 -07:00
/// - the [`State`] of the [`Viewer`]
/// - the bounds of the [`Viewer`] widget
/// - the bounds of the scaled [`Viewer`] image
2020-05-27 13:39:26 -07:00
/// - the translation of the clipped image
2020-04-23 15:34:55 -07:00
/// - the [`Handle`] to the underlying image
2020-05-26 17:15:55 -07:00
/// - whether the mouse is over the [`Viewer`] or not
2020-04-23 15:34:55 -07:00
///
2020-05-26 17:15:55 -07:00
/// [`Viewer`]: struct.Viewer.html
2020-04-23 15:34:55 -07:00
/// [`State`]: struct.State.html
2020-05-26 17:15:55 -07:00
/// [`Handle`]: ../../image/struct.Handle.html
2020-04-23 15:34:55 -07:00
fn draw(
&mut self,
state: &State,
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;
}
2020-05-26 17:15:55 -07:00
impl<'a, Message, Renderer> From<Viewer<'a>> for Element<'a, Message, Renderer>
2020-04-23 15:34:55 -07:00
where
Renderer: 'a + self::Renderer + image::Renderer,
Message: 'a,
{
2020-05-26 17:15:55 -07:00
fn from(viewer: Viewer<'a>) -> Element<'a, Message, Renderer> {
2020-05-15 09:46:22 -07:00
Element::new(viewer)
2020-04-23 15:34:55 -07:00
}
}