iced-yoda/src/widget/image.rs

179 lines
4.4 KiB
Rust
Raw Normal View History

2019-09-03 14:30:12 +02:00
//! Display images in your user interface.
2019-08-22 15:57:18 +02:00
use crate::{
2019-09-04 11:09:57 +02:00
Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style,
Widget,
2019-08-22 15:57:18 +02:00
};
use std::hash::Hash;
2019-09-03 14:30:12 +02:00
/// A frame that displays an image while keeping aspect ratio.
///
2019-09-03 14:30:12 +02:00
/// It implements [`Widget`] when the associated `Renderer` implements the
/// [`image::Renderer`] trait.
2019-08-22 15:57:18 +02:00
///
/// [`Widget`]: ../../core/trait.Widget.html
/// [`image::Renderer`]: trait.Renderer.html
2019-09-03 14:30:12 +02:00
///
2019-08-22 15:57:18 +02:00
/// # Example
///
/// ```
/// use iced::Image;
///
/// # let my_handle = String::from("some_handle");
/// let image = Image::new(my_handle);
2019-08-22 15:57:18 +02:00
/// ```
pub struct Image<I> {
image: I,
source: Option<Rectangle<u16>>,
2019-09-04 11:09:57 +02:00
width: Option<u16>,
height: Option<u16>,
2019-08-22 15:57:18 +02:00
style: Style,
}
impl<I> std::fmt::Debug for Image<I> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Image")
.field("source", &self.source)
2019-09-04 11:09:57 +02:00
.field("width", &self.width)
.field("height", &self.height)
2019-08-22 15:57:18 +02:00
.field("style", &self.style)
.finish()
}
}
impl<I> Image<I> {
/// Creates a new [`Image`] with given image handle.
///
/// [`Image`]: struct.Image.html
pub fn new(image: I) -> Self {
2019-08-22 15:57:18 +02:00
Image {
image,
2019-08-22 15:57:18 +02:00
source: None,
2019-09-04 11:09:57 +02:00
width: None,
height: None,
style: Style::default(),
2019-08-22 15:57:18 +02:00
}
}
2019-09-03 14:30:12 +02:00
/// Sets the portion of the [`Image`] to draw.
///
2019-08-22 15:57:18 +02:00
/// [`Image`]: struct.Image.html
pub fn clip(mut self, source: Rectangle<u16>) -> Self {
self.source = Some(source);
self
}
/// Sets the width of the [`Image`] boundaries in pixels.
///
/// [`Image`]: struct.Image.html
2019-09-04 11:09:57 +02:00
pub fn width(mut self, width: u16) -> Self {
self.width = Some(width);
2019-08-22 15:57:18 +02:00
self
}
/// Sets the height of the [`Image`] boundaries in pixels.
///
/// [`Image`]: struct.Image.html
2019-09-04 11:09:57 +02:00
pub fn height(mut self, height: u16) -> Self {
self.height = Some(height);
self
}
/// Sets the alignment of the [`Image`] itself.
///
/// This is useful if you want to override the default alignment given by
/// the parent container.
///
2019-09-05 10:02:55 +02:00
/// [`Image`]: struct.Image.html
2019-09-04 11:09:57 +02:00
pub fn align_self(mut self, align: Align) -> Self {
self.style = self.style.align_self(align);
2019-08-22 15:57:18 +02:00
self
}
}
impl<I, Message, Renderer> Widget<Message, Renderer> for Image<I>
where
Renderer: self::Renderer<I>,
I: Clone,
{
2019-09-04 11:09:57 +02:00
fn node(&self, renderer: &Renderer) -> Node {
renderer.node(
self.style,
&self.image,
self.width,
self.height,
self.source,
)
2019-08-22 15:57:18 +02:00
}
fn draw(
&self,
renderer: &mut Renderer,
layout: Layout<'_>,
_cursor_position: Point,
) -> MouseCursor {
2019-09-04 11:09:57 +02:00
renderer.draw(&self.image, layout.bounds(), self.source);
2019-08-22 15:57:18 +02:00
MouseCursor::OutOfBounds
}
fn hash_layout(&self, state: &mut Hasher) {
2019-08-22 15:57:18 +02:00
self.style.hash(state);
2019-09-04 11:09:57 +02:00
self.width.hash(state);
self.height.hash(state);
2019-08-22 15:57:18 +02:00
}
}
2019-09-03 14:30:12 +02:00
/// The renderer of an [`Image`].
2019-08-22 15:57:18 +02:00
///
2019-09-03 14:30:12 +02:00
/// Your [renderer] will need to implement this trait before being able to use
/// an [`Image`] in your user interface.
2019-08-22 15:57:18 +02:00
///
/// [`Image`]: struct.Image.html
2019-09-03 14:30:12 +02:00
/// [renderer]: ../../renderer/index.html
2019-08-22 15:57:18 +02:00
pub trait Renderer<I> {
2019-09-04 11:09:57 +02:00
/// Creates a [`Node`] with the given [`Style`] for the provided [`Image`]
/// and its size.
///
/// You should probably keep the original aspect ratio, if possible.
///
/// [`Node`]: ../../struct.Node.html
/// [`Style`]: ../../struct.Style.html
/// [`Image`]: struct.Image.html
fn node(
&self,
style: Style,
image: &I,
width: Option<u16>,
height: Option<u16>,
source: Option<Rectangle<u16>>,
) -> Node;
2019-09-03 14:30:12 +02:00
/// Draws an [`Image`].
2019-08-22 15:57:18 +02:00
///
/// It receives:
/// * the bounds of the [`Image`]
/// * the handle of the loaded [`Image`]
2019-09-03 14:30:12 +02:00
/// * the portion of the image to draw. If not specified, the entire image
/// should be drawn.
///
2019-08-22 15:57:18 +02:00
/// [`Image`]: struct.Image.html
fn draw(
&mut self,
2019-09-04 11:09:57 +02:00
image: &I,
2019-08-22 15:57:18 +02:00
bounds: Rectangle<f32>,
source: Option<Rectangle<u16>>,
);
}
impl<'a, I, Message, Renderer> From<Image<I>> for Element<'a, Message, Renderer>
where
Renderer: self::Renderer<I>,
I: Clone + 'a,
{
fn from(image: Image<I>) -> Element<'a, Message, Renderer> {
Element::new(image)
}
}