From 505588d5851fed8a3bb334edacfa6d96c545d562 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 29 Nov 2019 21:44:39 +0100 Subject: Allow to load an image from memory New `image::Handle` opaque type uniquely identifying some `image::Data`, allowing reliable caching. --- native/src/widget/image.rs | 93 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 8 deletions(-) (limited to 'native/src') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 4c588c9d..c82525e4 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -2,7 +2,11 @@ use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget}; -use std::hash::Hash; +use std::{ + hash::{Hash, Hasher as _}, + path::PathBuf, + rc::Rc, +}; /// A frame that displays an image while keeping aspect ratio. /// @@ -17,7 +21,7 @@ use std::hash::Hash; /// #[derive(Debug)] pub struct Image { - path: String, + handle: Handle, width: Length, height: Length, } @@ -26,9 +30,9 @@ impl Image { /// Creates a new [`Image`] with the given path. /// /// [`Image`]: struct.Image.html - pub fn new>(path: T) -> Self { + pub fn new>(handle: T) -> Self { Image { - path: path.into(), + handle: handle.into(), width: Length::Shrink, height: Length::Shrink, } @@ -68,7 +72,7 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let (width, height) = renderer.dimensions(&self.path); + let (width, height) = renderer.dimensions(&self.handle); let aspect_ratio = width as f32 / height as f32; @@ -96,7 +100,7 @@ where layout: Layout<'_>, _cursor_position: Point, ) -> Renderer::Output { - renderer.draw(&self.path, layout) + renderer.draw(self.handle.clone(), layout) } fn hash_layout(&self, state: &mut Hasher) { @@ -105,6 +109,79 @@ where } } +/// An [`Image`] handle. +/// +/// [`Image`]: struct.Image.html +#[derive(Debug, Clone)] +pub struct Handle { + id: u64, + data: Rc, +} + +impl Handle { + /// Creates an image [`Handle`] pointing to the image of the given path. + /// + /// [`Handle`]: struct.Handle.html + pub fn from_path>(path: T) -> Handle { + Self::from_data(Data::Path(path.into())) + } + + /// Creates an image [`Handle`] containing the image data directly. + /// + /// [`Handle`]: struct.Handle.html + pub fn from_bytes(bytes: Vec) -> Handle { + Self::from_data(Data::Bytes(bytes)) + } + + fn from_data(data: Data) -> Handle { + let mut hasher = Hasher::default(); + data.hash(&mut hasher); + + Handle { + id: hasher.finish(), + data: Rc::new(data), + } + } + + /// Returns the uniquie identifier of the [`Handle`]. + /// + /// [`Handle`]: struct.Handle.html + pub fn id(&self) -> u64 { + self.id + } + + /// Returns a reference to the image [`Data`]. + /// + /// [`Data`]: enum.Data.html + pub fn data(&self) -> &Data { + &self.data + } +} + +impl From for Handle { + fn from(path: String) -> Handle { + Handle::from_path(path) + } +} + +impl From<&str> for Handle { + fn from(path: &str) -> Handle { + Handle::from_path(path) + } +} + +/// The data of an [`Image`]. +/// +/// [`Image`]: struct.Image.html +#[derive(Debug, Clone, Hash)] +pub enum Data { + /// File data + Path(PathBuf), + + /// In-memory data + Bytes(Vec), +} + /// The renderer of an [`Image`]. /// /// Your [renderer] will need to implement this trait before being able to use @@ -116,12 +193,12 @@ pub trait Renderer: crate::Renderer { /// Returns the dimensions of an [`Image`] located on the given path. /// /// [`Image`]: struct.Image.html - fn dimensions(&self, path: &str) -> (u32, u32); + fn dimensions(&self, handle: &Handle) -> (u32, u32); /// Draws an [`Image`]. /// /// [`Image`]: struct.Image.html - fn draw(&mut self, path: &str, layout: Layout<'_>) -> Self::Output; + fn draw(&mut self, handle: Handle, layout: Layout<'_>) -> Self::Output; } impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -- cgit From cdd34e1e4b741a97dc6da8813a00d792eda8172a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 30 Nov 2019 02:14:56 +0100 Subject: Implement `image` viewer example --- native/src/widget/image.rs | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'native/src') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index c82525e4..aaa3eca5 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -5,7 +5,7 @@ use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget}; use std::{ hash::{Hash, Hasher as _}, path::PathBuf, - rc::Rc, + sync::Arc, }; /// A frame that displays an image while keeping aspect ratio. @@ -76,20 +76,18 @@ where 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(self.width) + .height(self.height) + .resolve(Size::new(width as f32, height as f32)); - let mut size = limits.width(width).height(height).resolve(Size::ZERO); + let viewport_aspect_ratio = size.width / size.height; - size.height = size.width / aspect_ratio; + 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; + } layout::Node::new(size) } @@ -115,7 +113,7 @@ where #[derive(Debug, Clone)] pub struct Handle { id: u64, - data: Rc, + data: Arc, } impl Handle { @@ -139,7 +137,7 @@ impl Handle { Handle { id: hasher.finish(), - data: Rc::new(data), + data: Arc::new(data), } } @@ -173,7 +171,7 @@ impl From<&str> for Handle { /// The data of an [`Image`]. /// /// [`Image`]: struct.Image.html -#[derive(Debug, Clone, Hash)] +#[derive(Clone, Hash)] pub enum Data { /// File data Path(PathBuf), @@ -182,6 +180,15 @@ pub enum Data { Bytes(Vec), } +impl std::fmt::Debug for Data { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Data::Path(path) => write!(f, "Path({:?})", path), + Data::Bytes(_) => write!(f, "Bytes(...)"), + } + } +} + /// The renderer of an [`Image`]. /// /// Your [renderer] will need to implement this trait before being able to use -- cgit From 1747eb2745d032275dff652aed2c4d8730d377e6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 30 Nov 2019 04:10:16 +0100 Subject: Fix typo in `image::Handle` docs --- native/src/widget/image.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'native/src') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index aaa3eca5..1b3964d7 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -141,7 +141,7 @@ impl Handle { } } - /// Returns the uniquie identifier of the [`Handle`]. + /// Returns the unique identifier of the [`Handle`]. /// /// [`Handle`]: struct.Handle.html pub fn id(&self) -> u64 { -- cgit From 4293dcb2540144cc69a9f1370103bb780eca69f3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 4 Dec 2019 03:55:33 +0100 Subject: Rename `image::Handle::from_bytes` to `from_memory` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also, replace `image` example with a new `pokedex` example using the PokéAPI. --- native/src/widget/image.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'native/src') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 1b3964d7..0d73fdb0 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -126,8 +126,11 @@ impl Handle { /// Creates an image [`Handle`] containing the image data directly. /// + /// This is useful if you already have your image loaded in-memory, maybe + /// because you downloaded or generated it procedurally. + /// /// [`Handle`]: struct.Handle.html - pub fn from_bytes(bytes: Vec) -> Handle { + pub fn from_memory(bytes: Vec) -> Handle { Self::from_data(Data::Bytes(bytes)) } -- cgit From 1f60e2820463ef9f7fe9dda5d8b8fa040fdd8666 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 4 Dec 2019 04:04:18 +0100 Subject: Update `Image::hash_layout` to hash new `Handle` --- native/src/widget/image.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'native/src') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 7a7138ff..20375822 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -102,7 +102,7 @@ where } fn hash_layout(&self, state: &mut Hasher) { - self.path.hash(state); + self.handle.hash(state); self.width.hash(state); self.height.hash(state); } @@ -172,6 +172,12 @@ impl From<&str> for Handle { } } +impl Hash for Handle { + fn hash(&self, state: &mut H) { + self.id.hash(state); + } +} + /// The data of an [`Image`]. /// /// [`Image`]: struct.Image.html -- cgit