diff options
author | 2019-12-06 19:37:56 +0100 | |
---|---|---|
committer | 2019-12-11 21:34:40 +0100 | |
commit | a88aae5e04e0a92457e5dd617a86af823e90af6c (patch) | |
tree | 2b3601cc15d041709a883c3b80685cf8e0baaa20 /native/src | |
parent | 80324284282f173e4d26e1f297daaf71a93f51a6 (diff) | |
download | iced-a88aae5e04e0a92457e5dd617a86af823e90af6c.tar.gz iced-a88aae5e04e0a92457e5dd617a86af823e90af6c.tar.bz2 iced-a88aae5e04e0a92457e5dd617a86af823e90af6c.zip |
Added an `Icon` widget to native.
Diffstat (limited to 'native/src')
-rw-r--r-- | native/src/widget.rs | 3 | ||||
-rw-r--r-- | native/src/widget/icon.rs | 103 |
2 files changed, 106 insertions, 0 deletions
diff --git a/native/src/widget.rs b/native/src/widget.rs index 71dcdc0d..0cf6a639 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -24,6 +24,7 @@ pub mod button; pub mod checkbox; pub mod column; pub mod container; +pub mod icon; pub mod image; pub mod radio; pub mod row; @@ -41,6 +42,8 @@ pub use column::Column; #[doc(no_inline)] pub use container::Container; #[doc(no_inline)] +pub use icon::Icon; +#[doc(no_inline)] pub use image::Image; #[doc(no_inline)] pub use radio::Radio; diff --git a/native/src/widget/icon.rs b/native/src/widget/icon.rs new file mode 100644 index 00000000..4687d7e6 --- /dev/null +++ b/native/src/widget/icon.rs @@ -0,0 +1,103 @@ +//! Display an icon. +use crate::{layout, Element, Hasher, Layout, Length, Point, Rectangle, Widget}; + +use std::hash::Hash; +use std::path::{Path, PathBuf}; + +/// A simple icon_loader widget. +#[derive(Debug, Clone)] +pub struct Icon { + path: PathBuf, + size: Length, +} + +impl Icon { + /// Create a new [`Icon`] from the file at `path`. + /// + /// [`Icon`]: struct.Icon.html + pub fn new(path: impl Into<PathBuf>) -> Self { + Icon { + path: path.into(), + size: Length::Fill, + } + } + + /// Sets the size of the [`Icon`]. + /// + /// [`Icon`]: struct.Icon.html + pub fn size(mut self, size: Length) -> Self { + self.size = size; + self + } +} + +impl<Message, Renderer> Widget<Message, Renderer> for Icon +where + Renderer: self::Renderer, +{ + fn width(&self) -> Length { + self.size + } + + fn height(&self) -> Length { + self.size + } + + fn layout(&self, _: &Renderer, limits: &layout::Limits) -> layout::Node { + let mut size = limits.width(self.size).height(self.size).max(); + + if size.width > size.height { + size.width = size.height; + } else if size.width < size.height { + size.height = size.width; + } + + layout::Node::new(size) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> Renderer::Output { + let bounds = layout.bounds(); + + renderer.draw( + bounds, + self.path.as_path(), + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.size.hash(state); + } +} + +/// The renderer of an [`Icon`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use [`Icon`] in your [`UserInterface`]. +/// +/// [`Icon`]: struct.Icon.html +/// [renderer]: ../../renderer/index.html +/// [`UserInterface`]: ../../struct.UserInterface.html +pub trait Renderer: crate::Renderer { + /// Draws an [`Icon`]. + /// + /// [`Icon`]: struct.Icon.html + fn draw( + &mut self, + bounds: Rectangle, + path: &Path, + ) -> Self::Output; +} + +impl<'a, Message, Renderer> From<Icon> for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ + fn from(icon: Icon) -> Element<'a, Message, Renderer> { + Element::new(icon) + } +} |