From 65eb218d3d7ba52b2869a586a1480eeb3c8f84e4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Nov 2019 13:47:20 +0100 Subject: Move widgets from `core` to `native` and `web` Also made fields private and improved `Renderer` traits. --- native/src/widget/image.rs | 81 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 10 deletions(-) (limited to 'native/src/widget/image.rs') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index b2541b87..8420ca76 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -1,10 +1,58 @@ //! Display images in your user interface. -use crate::{layout, Element, Hasher, Layout, Point, Widget}; +use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget}; use std::hash::Hash; -pub use iced_core::Image; +/// 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>(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 + } +} impl Widget for Image where @@ -15,7 +63,26 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - renderer.layout(&self, limits) + 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) } fn draw( @@ -41,13 +108,7 @@ where /// [`Image`]: struct.Image.html /// [renderer]: ../../renderer/index.html pub trait Renderer: crate::Renderer { - /// Creates a [`Node`] for the provided [`Image`]. - /// - /// You should probably keep the original aspect ratio, if possible. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Image`]: struct.Image.html - fn layout(&self, image: &Image, limits: &layout::Limits) -> layout::Node; + fn dimensions(&self, path: &str) -> (u32, u32); /// Draws an [`Image`]. /// -- cgit From a7dba612f03e58d7bd9527499d893987986b347c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Nov 2019 19:36:57 +0100 Subject: Write docs for `iced` and `iced_native` --- native/src/widget/image.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'native/src/widget/image.rs') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 696de683..c64f07b1 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -116,6 +116,9 @@ where /// [`Image`]: struct.Image.html /// [renderer]: ../../renderer/index.html 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); /// Draws an [`Image`]. -- cgit From f943764a292837cbf262a4f29dc0021d808852d6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 24 Nov 2019 10:44:55 +0100 Subject: Fix `iced_native` widget examples --- native/src/widget/image.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'native/src/widget/image.rs') diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index c64f07b1..4c588c9d 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -10,19 +10,16 @@ use std::hash::Hash; /// /// ``` /// # 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, + path: String, + width: Length, + height: Length, } impl Image { @@ -99,7 +96,7 @@ where layout: Layout<'_>, _cursor_position: Point, ) -> Renderer::Output { - renderer.draw(&self, layout) + renderer.draw(&self.path, layout) } fn hash_layout(&self, state: &mut Hasher) { @@ -124,7 +121,7 @@ pub trait Renderer: crate::Renderer { /// Draws an [`Image`]. /// /// [`Image`]: struct.Image.html - fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output; + fn draw(&mut self, path: &str, layout: Layout<'_>) -> Self::Output; } impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -- cgit