rename to image::Viewer

This commit is contained in:
Cory Forsstrom 2020-05-26 17:15:55 -07:00
parent 431171f975
commit 5d045c2e9a
No known key found for this signature in database
GPG key ID: 64D6B5851FFCAC9E
9 changed files with 50 additions and 73 deletions

View file

@ -1,4 +1,7 @@
//! Display images in your user interface.
pub mod viewer;
pub use viewer::{State, Viewer};
use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget};
use std::{

View file

@ -8,7 +8,7 @@ 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 ImageViewer<'a> {
pub struct Viewer<'a> {
state: &'a mut State,
padding: u16,
width: Length,
@ -18,14 +18,14 @@ pub struct ImageViewer<'a> {
handle: image::Handle,
}
impl<'a> ImageViewer<'a> {
/// Creates a new [`ImageViewer`] with the given [`State`] and [`Handle`].
impl<'a> Viewer<'a> {
/// Creates a new [`Viewer`] with the given [`State`] and [`Handle`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
/// [`State`]: struct.State.html
/// [`Handle`]: ../image/struct.Handle.html
/// [`Handle`]: ../../image/struct.Handle.html
pub fn new(state: &'a mut State, handle: image::Handle) -> Self {
ImageViewer {
Viewer {
state,
padding: 0,
width: Length::Shrink,
@ -36,48 +36,48 @@ impl<'a> ImageViewer<'a> {
}
}
/// Sets the padding of the [`ImageViewer`].
/// Sets the padding of the [`Viewer`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
pub fn padding(mut self, units: u16) -> Self {
self.padding = units;
self
}
/// Sets the width of the [`ImageViewer`].
/// Sets the width of the [`Viewer`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
pub fn width(mut self, width: Length) -> Self {
self.width = width;
self
}
/// Sets the height of the [`ImageViewer`].
/// Sets the height of the [`Viewer`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
pub fn height(mut self, height: Length) -> Self {
self.height = height;
self
}
/// Sets the max width of the [`ImageViewer`].
/// Sets the max width of the [`Viewer`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`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 [`ImageViewer`].
/// Sets the max height of the [`Viewer`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
pub fn max_height(mut self, max_height: u32) -> Self {
self.max_height = max_height;
self
}
}
impl<'a, Message, Renderer> Widget<Message, Renderer> for ImageViewer<'a>
impl<'a, Message, Renderer> Widget<Message, Renderer> for Viewer<'a>
where
Renderer: self::Renderer + image::Renderer,
{
@ -263,9 +263,9 @@ where
}
}
/// The local state of an [`ImageViewer`].
/// The local state of a [`Viewer`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
#[derive(Debug, Clone, Copy, Default)]
pub struct State {
scale: Option<f32>,
@ -283,9 +283,9 @@ impl State {
}
/// Apply a panning offset to the current [`State`], given the bounds of
/// the [`ImageViewer`] and its image.
/// the [`Viewer`] and its image.
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
/// [`State`]: struct.State.html
fn pan(
&mut self,
@ -311,9 +311,9 @@ impl State {
}
/// Returns the current clipping offset of the [`State`], given the bounds
/// of the [`ImageViewer`] and its contents.
/// of the [`Viewer`] and its contents.
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
/// [`State`]: struct.State.html
fn offset(&self, bounds: Rectangle, image_bounds: Rectangle) -> (u32, u32) {
let hidden_width = ((image_bounds.width - bounds.width) as f32)
@ -331,36 +331,36 @@ impl State {
}
/// Returns if the left mouse button is still held down since clicking inside
/// the [`ImageViewer`].
/// the [`Viewer`].
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`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 [`ImageViewer`].
/// The renderer of an [`Viewer`].
///
/// Your [renderer] will need to implement this trait before being
/// able to use a [`ImageViewer`] in your user interface.
/// able to use a [`Viewer`] in your user interface.
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [renderer]: ../../renderer/index.html
/// [`Viewer`]: struct.Viewer.html
/// [renderer]: ../../../renderer/index.html
pub trait Renderer: crate::Renderer + Sized {
/// Draws the [`ImageViewer`].
/// Draws the [`Viewer`].
///
/// It receives:
/// - the [`State`] of the [`ImageViewer`]
/// - the bounds of the [`ImageViewer`] widget
/// - the bounds of the scaled [`ImageViewer`] image
/// - the [`State`] of the [`Viewer`]
/// - the bounds of the [`Viewer`] widget
/// - the bounds of the scaled [`Viewer`] image
/// - the clipping x,y offset
/// - the [`Handle`] to the underlying image
/// - whether the mouse is over the [`ImageViewer`] or not
/// - whether the mouse is over the [`Viewer`] or not
///
/// [`ImageViewer`]: struct.ImageViewer.html
/// [`Viewer`]: struct.Viewer.html
/// [`State`]: struct.State.html
/// [`Handle`]: ../image/struct.Handle.html
/// [`Handle`]: ../../image/struct.Handle.html
fn draw(
&mut self,
state: &State,
@ -372,13 +372,12 @@ pub trait Renderer: crate::Renderer + Sized {
) -> Self::Output;
}
impl<'a, Message, Renderer> From<ImageViewer<'a>>
for Element<'a, Message, Renderer>
impl<'a, Message, Renderer> From<Viewer<'a>> for Element<'a, Message, Renderer>
where
Renderer: 'a + self::Renderer + image::Renderer,
Message: 'a,
{
fn from(viewer: ImageViewer<'a>) -> Element<'a, Message, Renderer> {
fn from(viewer: Viewer<'a>) -> Element<'a, Message, Renderer> {
Element::new(viewer)
}
}