From c583a2174d28878a6b1a31288e80b96fac62e799 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 4 Sep 2019 11:09:57 +0200 Subject: Improve tour example --- src/widget/image.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 10 deletions(-) (limited to 'src/widget/image.rs') diff --git a/src/widget/image.rs b/src/widget/image.rs index 8cbcd02c..161dc963 100644 --- a/src/widget/image.rs +++ b/src/widget/image.rs @@ -1,7 +1,8 @@ //! Display images in your user interface. use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, + Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, + Widget, }; use std::hash::Hash; @@ -25,6 +26,8 @@ use std::hash::Hash; pub struct Image { image: I, source: Option>, + width: Option, + height: Option, style: Style, } @@ -32,6 +35,8 @@ impl std::fmt::Debug for Image { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Image") .field("source", &self.source) + .field("width", &self.width) + .field("height", &self.height) .field("style", &self.style) .finish() } @@ -45,7 +50,9 @@ impl Image { Image { image, source: None, - style: Style::default().fill_width().fill_height(), + width: None, + height: None, + style: Style::default(), } } @@ -60,16 +67,27 @@ impl Image { /// Sets the width of the [`Image`] boundaries in pixels. /// /// [`Image`]: struct.Image.html - pub fn width(mut self, width: u32) -> Self { - self.style = self.style.width(width); + pub fn width(mut self, width: u16) -> Self { + self.width = Some(width); self } /// Sets the height of the [`Image`] boundaries in pixels. /// /// [`Image`]: struct.Image.html - pub fn height(mut self, height: u32) -> Self { - self.style = self.style.height(height); + 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. + /// + /// [`Image`]: struct.Button.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); self } } @@ -79,8 +97,14 @@ where Renderer: self::Renderer, I: Clone, { - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style) + fn node(&self, renderer: &Renderer) -> Node { + renderer.node( + self.style, + &self.image, + self.width, + self.height, + self.source, + ) } fn draw( @@ -89,13 +113,15 @@ where layout: Layout<'_>, _cursor_position: Point, ) -> MouseCursor { - renderer.draw(layout.bounds(), self.image.clone(), self.source); + renderer.draw(&self.image, layout.bounds(), self.source); MouseCursor::OutOfBounds } fn hash_layout(&self, state: &mut Hasher) { self.style.hash(state); + self.width.hash(state); + self.height.hash(state); } } @@ -107,6 +133,23 @@ where /// [`Image`]: struct.Image.html /// [renderer]: ../../renderer/index.html pub trait Renderer { + /// 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, + height: Option, + source: Option>, + ) -> Node; + /// Draws an [`Image`]. /// /// It receives: @@ -118,8 +161,8 @@ pub trait Renderer { /// [`Image`]: struct.Image.html fn draw( &mut self, + image: &I, bounds: Rectangle, - image: I, source: Option>, ); } -- cgit