From 64fc0ffded3d4243bbe288a760319ff100d060e1 Mon Sep 17 00:00:00 2001 From: debris Date: Thu, 22 Aug 2019 15:57:18 +0200 Subject: migrated Image and ProgressBar widgets --- src/widget/image.rs | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/widget/image.rs (limited to 'src/widget/image.rs') diff --git a/src/widget/image.rs b/src/widget/image.rs new file mode 100644 index 00000000..f9b3a037 --- /dev/null +++ b/src/widget/image.rs @@ -0,0 +1,139 @@ +//! Displays image to your users. + +use crate::{ + Style, Node, Element, MouseCursor, Layout, Hasher, Widget, + Rectangle, Point, +}; + +use std::hash::Hash; + +/// A widget that displays an image. +/// +/// It implements [`Widget`] when the associated [`core::Renderer`] implements +/// the [`image::Renderer`] trait. +/// +/// [`Widget`]: ../../core/trait.Widget.html +/// [`core::Renderer`]: ../../core/trait.Renderer.html +/// [`image::Renderer`]: trait.Renderer.html +/// # Example +/// +/// ``` +/// use iced::Image; +/// +/// let image = Image::new("image"); +/// ``` +pub struct Image { + image: I, + source: Option>, + style: Style, +} + +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("style", &self.style) + .finish() + } +} + +impl Image { + /// Creates a new [`Image`] with given image handle. + /// + /// [`Image`]: struct.Image.html + pub fn new(image: &I) -> Self where I: Clone { + Image { + image: image.clone(), + source: None, + style: Style::default().fill_width().fill_height(), + } + } + + /// Sets the portion of the [`Image`] that we want to draw. + /// + /// [`Image`]: struct.Image.html + pub fn clip(mut self, source: Rectangle) -> Self { + self.source = Some(source); + self + } + + /// 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); + 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); + self + } +} + +impl Widget for Image +where + Renderer: self::Renderer, + I: Clone, +{ + fn node(&self, _renderer: &Renderer) -> Node { + Node::new(self.style) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + layout.bounds(), + self.image.clone(), + self.source, + ); + + MouseCursor::OutOfBounds + } + + fn hash(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +/// The renderer of a [`Image`]. +/// +/// Your [`core::Renderer`] will need to implement this trait before being +/// able to use a [`Image`] in your user interface. +/// +/// [`Image`]: struct.Image.html +/// [`core::Renderer`]: ../../core/trait.Renderer.html +pub trait Renderer { + /// Draws a [`Image`]. + /// + /// It receives: + /// * the bounds of the [`Image`] + /// * the handle of the loaded [`Image`] + /// * the portion of the image that we wants to draw, + /// if not specified, draw entire image + /// + /// [`Image`]: struct.Image.html + fn draw( + &mut self, + bounds: Rectangle, + image: I, + source: Option>, + ); +} + +impl<'a, I, Message, Renderer> From> for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + I: Clone + 'a, +{ + fn from(image: Image) -> Element<'a, Message, Renderer> { + Element::new(image) + } +} -- cgit From 52394732fc2a5c6152a938f1b2967c921bfef7be Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 3 Sep 2019 13:47:32 +0200 Subject: Fix `Image` and `ProgressBar` doc examples --- src/widget/image.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'src/widget/image.rs') diff --git a/src/widget/image.rs b/src/widget/image.rs index f9b3a037..500abe0d 100644 --- a/src/widget/image.rs +++ b/src/widget/image.rs @@ -1,14 +1,13 @@ //! Displays image to your users. use crate::{ - Style, Node, Element, MouseCursor, Layout, Hasher, Widget, - Rectangle, Point, + Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, }; use std::hash::Hash; /// A widget that displays an image. -/// +/// /// It implements [`Widget`] when the associated [`core::Renderer`] implements /// the [`image::Renderer`] trait. /// @@ -20,7 +19,8 @@ use std::hash::Hash; /// ``` /// use iced::Image; /// -/// let image = Image::new("image"); +/// # let my_handle = String::from("some_handle"); +/// let image = Image::new(my_handle); /// ``` pub struct Image { image: I, @@ -41,16 +41,16 @@ impl Image { /// Creates a new [`Image`] with given image handle. /// /// [`Image`]: struct.Image.html - pub fn new(image: &I) -> Self where I: Clone { + pub fn new(image: I) -> Self { Image { - image: image.clone(), + image, source: None, style: Style::default().fill_width().fill_height(), } } /// Sets the portion of the [`Image`] that we want to draw. - /// + /// /// [`Image`]: struct.Image.html pub fn clip(mut self, source: Rectangle) -> Self { self.source = Some(source); @@ -89,11 +89,7 @@ where layout: Layout<'_>, _cursor_position: Point, ) -> MouseCursor { - renderer.draw( - layout.bounds(), - self.image.clone(), - self.source, - ); + renderer.draw(layout.bounds(), self.image.clone(), self.source); MouseCursor::OutOfBounds } @@ -117,8 +113,8 @@ pub trait Renderer { /// * the bounds of the [`Image`] /// * the handle of the loaded [`Image`] /// * the portion of the image that we wants to draw, - /// if not specified, draw entire image - /// + /// if not specified, draw entire image + /// /// [`Image`]: struct.Image.html fn draw( &mut self, -- cgit