2019-09-20 19:15:31 +02:00
|
|
|
//! Display images in your user interface.
|
2020-05-26 17:15:55 -07:00
|
|
|
pub mod viewer;
|
2020-05-27 14:20:07 -07:00
|
|
|
pub use viewer::Viewer;
|
2020-05-26 17:15:55 -07:00
|
|
|
|
2021-12-10 20:15:27 +01:00
|
|
|
use crate::image;
|
2020-08-18 03:37:32 +02:00
|
|
|
use crate::layout;
|
2021-10-18 15:19:04 +07:00
|
|
|
use crate::renderer;
|
2020-08-18 03:37:32 +02:00
|
|
|
use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget};
|
2019-09-20 19:15:31 +02:00
|
|
|
|
2021-10-31 16:20:50 +07:00
|
|
|
use std::hash::Hash;
|
2019-09-20 19:15:31 +02:00
|
|
|
|
2019-11-21 13:47:20 +01:00
|
|
|
/// A frame that displays an image while keeping aspect ratio.
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
2021-10-31 15:35:12 +07:00
|
|
|
/// # use iced_native::widget::Image;
|
2021-12-10 20:15:27 +01:00
|
|
|
/// # use iced_native::image;
|
2019-11-24 10:44:55 +01:00
|
|
|
/// #
|
2021-12-10 20:15:27 +01:00
|
|
|
/// let image = Image::<image::Handle>::new("resources/ferris.png");
|
2019-11-21 13:47:20 +01:00
|
|
|
/// ```
|
2019-11-24 10:44:55 +01:00
|
|
|
///
|
|
|
|
|
/// <img src="https://github.com/hecrj/iced/blob/9712b319bb7a32848001b96bd84977430f14b623/examples/resources/ferris.png?raw=true" width="300">
|
2020-01-10 14:39:29 +01:00
|
|
|
#[derive(Debug, Hash)]
|
2021-12-10 20:15:27 +01:00
|
|
|
pub struct Image<Handle> {
|
2019-11-29 21:44:39 +01:00
|
|
|
handle: Handle,
|
2019-11-24 10:44:55 +01:00
|
|
|
width: Length,
|
|
|
|
|
height: Length,
|
2019-11-21 13:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-10 20:15:27 +01:00
|
|
|
impl<Handle> Image<Handle> {
|
2019-11-21 13:47:20 +01:00
|
|
|
/// Creates a new [`Image`] with the given path.
|
2019-11-29 21:44:39 +01:00
|
|
|
pub fn new<T: Into<Handle>>(handle: T) -> Self {
|
2019-11-21 13:47:20 +01:00
|
|
|
Image {
|
2019-11-29 21:44:39 +01:00
|
|
|
handle: handle.into(),
|
2019-11-21 13:47:20 +01:00
|
|
|
width: Length::Shrink,
|
|
|
|
|
height: Length::Shrink,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the width of the [`Image`] boundaries.
|
|
|
|
|
pub fn width(mut self, width: Length) -> Self {
|
|
|
|
|
self.width = width;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets the height of the [`Image`] boundaries.
|
|
|
|
|
pub fn height(mut self, height: Length) -> Self {
|
|
|
|
|
self.height = height;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-20 19:15:31 +02:00
|
|
|
|
2021-12-10 20:15:27 +01:00
|
|
|
impl<Message, Renderer, Handle> Widget<Message, Renderer> for Image<Handle>
|
2019-09-20 19:15:31 +02:00
|
|
|
where
|
2021-12-10 20:15:27 +01:00
|
|
|
Renderer: image::Renderer<Handle = Handle>,
|
|
|
|
|
Handle: Clone + Hash,
|
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-29 21:44:39 +01:00
|
|
|
let (width, height) = renderer.dimensions(&self.handle);
|
2019-11-21 13:47:20 +01:00
|
|
|
|
|
|
|
|
let aspect_ratio = width as f32 / height as f32;
|
|
|
|
|
|
2019-11-30 02:14:56 +01:00
|
|
|
let mut size = limits
|
|
|
|
|
.width(self.width)
|
|
|
|
|
.height(self.height)
|
|
|
|
|
.resolve(Size::new(width as f32, height as f32));
|
2019-11-21 13:47:20 +01:00
|
|
|
|
2019-11-30 02:14:56 +01:00
|
|
|
let viewport_aspect_ratio = size.width / size.height;
|
2019-11-21 13:47:20 +01:00
|
|
|
|
2019-11-30 02:14:56 +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;
|
|
|
|
|
}
|
2019-11-21 13:47:20 +01:00
|
|
|
|
|
|
|
|
layout::Node::new(size)
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
renderer: &mut Renderer,
|
2021-10-18 15:19:04 +07:00
|
|
|
_style: &renderer::Style,
|
2019-11-10 06:05:20 +01:00
|
|
|
layout: Layout<'_>,
|
2019-09-20 19:15:31 +02:00
|
|
|
_cursor_position: Point,
|
2020-08-18 03:37:32 +02:00
|
|
|
_viewport: &Rectangle,
|
2021-10-14 16:07:22 +07:00
|
|
|
) {
|
2021-10-28 17:01:23 +07:00
|
|
|
renderer.draw(self.handle.clone(), layout.bounds());
|
2019-09-20 19:15:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn hash_layout(&self, state: &mut Hasher) {
|
2020-04-12 01:20:40 +02:00
|
|
|
struct Marker;
|
|
|
|
|
std::any::TypeId::of::<Marker>().hash(state);
|
2020-03-30 17:28:55 +02:00
|
|
|
|
2019-12-04 04:04:18 +01:00
|
|
|
self.handle.hash(state);
|
2019-09-20 19:15:31 +02:00
|
|
|
self.width.hash(state);
|
|
|
|
|
self.height.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 20:15:27 +01:00
|
|
|
impl<'a, Message, Renderer, Handle> From<Image<Handle>>
|
|
|
|
|
for Element<'a, Message, Renderer>
|
2019-09-20 19:15:31 +02:00
|
|
|
where
|
2021-12-10 20:15:27 +01:00
|
|
|
Renderer: image::Renderer<Handle = Handle>,
|
|
|
|
|
Handle: Clone + Hash + 'a,
|
2019-09-20 19:15:31 +02:00
|
|
|
{
|
2021-12-10 20:15:27 +01:00
|
|
|
fn from(image: Image<Handle>) -> Element<'a, Message, Renderer> {
|
2019-09-20 19:15:31 +02:00
|
|
|
Element::new(image)
|
|
|
|
|
}
|
|
|
|
|
}
|