diff options
| author | 2020-02-05 04:13:13 +0100 | |
|---|---|---|
| committer | 2020-02-05 04:13:55 +0100 | |
| commit | 9a06e481b7f52a9d8e123c909d5614332f607c53 (patch) | |
| tree | 811e7e83a1960194d7285f74764482253874f882 /web | |
| parent | 8f52604987038225ce90261f17fd8408f1a7ebe3 (diff) | |
| download | iced-9a06e481b7f52a9d8e123c909d5614332f607c53.tar.gz iced-9a06e481b7f52a9d8e123c909d5614332f607c53.tar.bz2 iced-9a06e481b7f52a9d8e123c909d5614332f607c53.zip  | |
Add `Handle` and `Data` to `image` in `iced_web`
Diffstat (limited to '')
| -rw-r--r-- | web/Cargo.toml | 1 | ||||
| -rw-r--r-- | web/src/widget.rs | 2 | ||||
| -rw-r--r-- | web/src/widget/image.rs | 83 | 
3 files changed, 80 insertions, 6 deletions
diff --git a/web/Cargo.toml b/web/Cargo.toml index 3b2d24d5..7ddf3400 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -19,6 +19,7 @@ iced_style = { version = "0.1.0-alpha", path = "../style" }  dodrio = "0.1.0"  wasm-bindgen = "0.2.51"  wasm-bindgen-futures = "0.4" +url = "2.0"  [dependencies.iced_core]  version = "0.1.0" diff --git a/web/src/widget.rs b/web/src/widget.rs index 2e80346e..3192a767 100644 --- a/web/src/widget.rs +++ b/web/src/widget.rs @@ -18,6 +18,7 @@ use crate::{Bus, Css};  use dodrio::bumpalo;  pub mod button; +pub mod image;  pub mod scrollable;  pub mod slider;  pub mod text_input; @@ -25,7 +26,6 @@ pub mod text_input;  mod checkbox;  mod column;  mod container; -mod image;  mod radio;  mod row;  mod space; diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index 17e9b0f7..4296d0e2 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -1,6 +1,12 @@ -use crate::{Bus, Css, Element, Length, Widget}; +//! Display images in your user interface. +use crate::{Bus, Css, Element, Hasher, Length, Widget};  use dodrio::bumpalo; +use std::{ +    hash::{Hash, Hasher as _}, +    path::PathBuf, +    sync::Arc, +};  /// A frame that displays an image while keeping aspect ratio.  /// @@ -14,7 +20,7 @@ use dodrio::bumpalo;  #[derive(Debug)]  pub struct Image {      /// The image path -    pub path: String, +    pub handle: Handle,      /// The width of the image      pub width: Length, @@ -27,9 +33,9 @@ impl Image {      /// Creates a new [`Image`] with the given path.      ///      /// [`Image`]: struct.Image.html -    pub fn new<T: Into<String>>(path: T) -> Self { +    pub fn new<T: Into<Handle>>(handle: T) -> Self {          Image { -            path: path.into(), +            handle: handle.into(),              width: Length::Shrink,              height: Length::Shrink,          } @@ -61,7 +67,9 @@ impl<Message> Widget<Message> for Image {      ) -> dodrio::Node<'b> {          use dodrio::builder::*; -        let src = bumpalo::format!(in bump, "{}", self.path); +        let src = bumpalo::format!(in bump, "{}", match self.handle.data.as_ref() { +            Data::Path(path) => path.to_str().unwrap_or("") +        });          let mut image = img(bump).attr("src", src.into_bump_str()); @@ -89,3 +97,68 @@ impl<'a, Message> From<Image> for Element<'a, Message> {          Element::new(image)      }  } + +/// An [`Image`] handle. +/// +/// [`Image`]: struct.Image.html +#[derive(Debug, Clone)] +pub struct Handle { +    id: u64, +    data: Arc<Data>, +} + +impl Handle { +    /// Creates an image [`Handle`] pointing to the image of the given path. +    /// +    /// [`Handle`]: struct.Handle.html +    pub fn from_path<T: Into<PathBuf>>(path: T) -> Handle { +        Self::from_data(Data::Path(path.into())) +    } + +    fn from_data(data: Data) -> Handle { +        let mut hasher = Hasher::default(); +        data.hash(&mut hasher); + +        Handle { +            id: hasher.finish(), +            data: Arc::new(data), +        } +    } + +    /// Returns the unique 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<&str> for Handle { +    fn from(path: &str) -> Handle { +        Handle::from_path(path) +    } +} + +/// The data of an [`Image`]. +/// +/// [`Image`]: struct.Image.html +#[derive(Clone, Hash)] +pub enum Data { +    /// A remote image +    Path(PathBuf), +} + +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), +        } +    } +}  | 
