Add Handle
and Data
to image
in iced_web
This commit is contained in:
parent
8f52604987
commit
9a06e481b7
@ -1,5 +1,4 @@
|
|||||||
//! Display images in your user interface.
|
//! Display images in your user interface.
|
||||||
|
|
||||||
use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget};
|
use crate::{layout, Element, Hasher, Layout, Length, Point, Size, Widget};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -19,6 +19,7 @@ iced_style = { version = "0.1.0-alpha", path = "../style" }
|
|||||||
dodrio = "0.1.0"
|
dodrio = "0.1.0"
|
||||||
wasm-bindgen = "0.2.51"
|
wasm-bindgen = "0.2.51"
|
||||||
wasm-bindgen-futures = "0.4"
|
wasm-bindgen-futures = "0.4"
|
||||||
|
url = "2.0"
|
||||||
|
|
||||||
[dependencies.iced_core]
|
[dependencies.iced_core]
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
@ -18,6 +18,7 @@ use crate::{Bus, Css};
|
|||||||
use dodrio::bumpalo;
|
use dodrio::bumpalo;
|
||||||
|
|
||||||
pub mod button;
|
pub mod button;
|
||||||
|
pub mod image;
|
||||||
pub mod scrollable;
|
pub mod scrollable;
|
||||||
pub mod slider;
|
pub mod slider;
|
||||||
pub mod text_input;
|
pub mod text_input;
|
||||||
@ -25,7 +26,6 @@ pub mod text_input;
|
|||||||
mod checkbox;
|
mod checkbox;
|
||||||
mod column;
|
mod column;
|
||||||
mod container;
|
mod container;
|
||||||
mod image;
|
|
||||||
mod radio;
|
mod radio;
|
||||||
mod row;
|
mod row;
|
||||||
mod space;
|
mod space;
|
||||||
|
@ -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 dodrio::bumpalo;
|
||||||
|
use std::{
|
||||||
|
hash::{Hash, Hasher as _},
|
||||||
|
path::PathBuf,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
/// A frame that displays an image while keeping aspect ratio.
|
/// A frame that displays an image while keeping aspect ratio.
|
||||||
///
|
///
|
||||||
@ -14,7 +20,7 @@ use dodrio::bumpalo;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Image {
|
pub struct Image {
|
||||||
/// The image path
|
/// The image path
|
||||||
pub path: String,
|
pub handle: Handle,
|
||||||
|
|
||||||
/// The width of the image
|
/// The width of the image
|
||||||
pub width: Length,
|
pub width: Length,
|
||||||
@ -27,9 +33,9 @@ impl Image {
|
|||||||
/// Creates a new [`Image`] with the given path.
|
/// Creates a new [`Image`] with the given path.
|
||||||
///
|
///
|
||||||
/// [`Image`]: struct.Image.html
|
/// [`Image`]: struct.Image.html
|
||||||
pub fn new<T: Into<String>>(path: T) -> Self {
|
pub fn new<T: Into<Handle>>(handle: T) -> Self {
|
||||||
Image {
|
Image {
|
||||||
path: path.into(),
|
handle: handle.into(),
|
||||||
width: Length::Shrink,
|
width: Length::Shrink,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
}
|
}
|
||||||
@ -61,7 +67,9 @@ impl<Message> Widget<Message> for Image {
|
|||||||
) -> dodrio::Node<'b> {
|
) -> dodrio::Node<'b> {
|
||||||
use dodrio::builder::*;
|
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());
|
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)
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user