2019-09-20 19:15:31 +02:00
|
|
|
//! Display images in your user interface.
|
|
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget};
|
2019-09-20 19:15:31 +02:00
|
|
|
|
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
/// A frame that displays an image while keeping aspect ratio.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// # use iced_native::Image;
|
|
|
|
|
///
|
|
|
|
|
/// let image = Image::new("resources/ferris.png");
|
|
|
|
|
/// ```
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Image {
|
|
|
|
|
/// The image path
|
|
|
|
|
pub path: String,
|
|
|
|
|
|
|
|
|
|
/// The width of the image
|
|
|
|
|
pub width: Length,
|
|
|
|
|
|
|
|
|
|
/// The height of the image
|
|
|
|
|
pub height: Length,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Image {
|
|
|
|
|
/// Creates a new [`Image`] with the given path.
|
|
|
|
|
///
|
|
|
|
|
/// [`Image`]: struct.Image.html
|
|
|
|
|
pub fn new<T: Into<String>>(path: T) -> Self {
|
|
|
|
|
Image {
|
|
|
|
|
path: path.into(),
|
|
|
|
|
width: Length::Shrink,
|
|
|
|
|
height: Length::Shrink,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the width of the [`Image`] boundaries.
|
|
|
|
|
///
|
|
|
|
|
/// [`Image`]: struct.Image.html
|
|
|
|
|
pub fn width(mut self, width: Length) -> Self {
|
|
|
|
|
self.width = width;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the height of the [`Image`] boundaries.
|
|
|
|
|
///
|
|
|
|
|
/// [`Image`]: struct.Image.html
|
|
|
|
|
pub fn height(mut self, height: Length) -> Self {
|
|
|
|
|
self.height = height;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-20 19:15:31 +02:00
|
|
|
|
2019-10-22 23:20:24 +02:00
|
|
|
impl<Message, Renderer> Widget<Message, Renderer> for Image
|
2019-09-20 19:15:31 +02:00
|
|
|
where
|
2019-10-22 23:20:24 +02:00
|
|
|
Renderer: self::Renderer,
|
2019-09-20 19:15:31 +02:00
|
|
|
{
|
2019-11-16 22:08:49 +01:00
|
|
|
fn width(&self) -> Length {
|
|
|
|
|
self.width
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn height(&self) -> Length {
|
|
|
|
|
self.height
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 06:05:20 +01:00
|
|
|
fn layout(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node {
|
2019-11-21 13:47:20 +01:00
|
|
|
let (width, height) = renderer.dimensions(&self.path);
|
|
|
|
|
|
|
|
|
|
let aspect_ratio = width as f32 / height as f32;
|
|
|
|
|
|
|
|
|
|
// TODO: Deal with additional cases
|
|
|
|
|
let (width, height) = match (self.width, self.height) {
|
|
|
|
|
(Length::Units(width), _) => (
|
|
|
|
|
self.width,
|
|
|
|
|
Length::Units((width as f32 / aspect_ratio).round() as u16),
|
|
|
|
|
),
|
|
|
|
|
(_, _) => {
|
|
|
|
|
(Length::Units(width as u16), Length::Units(height as u16))
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut size = limits.width(width).height(height).resolve(Size::ZERO);
|
|
|
|
|
|
|
|
|
|
size.height = size.width / aspect_ratio;
|
|
|
|
|
|
|
|
|
|
layout::Node::new(size)
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &mut Renderer,
|
2019-11-10 06:05:20 +01:00
|
|
|
layout: Layout<'_>,
|
2019-09-20 19:15:31 +02:00
|
|
|
_cursor_position: Point,
|
2019-10-11 22:15:39 +02:00
|
|
|
) -> Renderer::Output {
|
2019-10-05 03:56:18 +02:00
|
|
|
renderer.draw(&self, layout)
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hash_layout(&self, state: &mut Hasher) {
|
|
|
|
|
self.width.hash(state);
|
|
|
|
|
self.height.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The renderer of an [`Image`].
|
|
|
|
|
///
|
|
|
|
|
/// Your [renderer] will need to implement this trait before being able to use
|
|
|
|
|
/// an [`Image`] in your user interface.
|
|
|
|
|
///
|
|
|
|
|
/// [`Image`]: struct.Image.html
|
|
|
|
|
/// [renderer]: ../../renderer/index.html
|
2019-10-22 23:20:24 +02:00
|
|
|
pub trait Renderer: crate::Renderer {
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Returns the dimensions of an [`Image`] located on the given path.
|
|
|
|
|
///
|
|
|
|
|
/// [`Image`]: struct.Image.html
|
2019-11-21 13:47:20 +01:00
|
|
|
fn dimensions(&self, path: &str) -> (u32, u32);
|
2019-09-20 19:15:31 +02:00
|
|
|
|
|
|
|
|
/// Draws an [`Image`].
|
|
|
|
|
///
|
|
|
|
|
/// [`Image`]: struct.Image.html
|
2019-11-10 06:05:20 +01:00
|
|
|
fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output;
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-22 23:20:24 +02:00
|
|
|
impl<'a, Message, Renderer> From<Image> for Element<'a, Message, Renderer>
|
2019-09-20 19:15:31 +02:00
|
|
|
where
|
2019-10-22 23:20:24 +02:00
|
|
|
Renderer: self::Renderer,
|
2019-09-20 19:15:31 +02:00
|
|
|
{
|
2019-10-22 23:20:24 +02:00
|
|
|
fn from(image: Image) -> Element<'a, Message, Renderer> {
|
2019-09-20 19:15:31 +02:00
|
|
|
Element::new(image)
|
|
|
|
|
}
|
|
|
|
|
}
|