Merge pull request #788 from Liamolucko/web-in-memory-image
feat(web): Support in-memory image data
This commit is contained in:
commit
dcd362813a
2 changed files with 21 additions and 5 deletions
|
|
@ -20,6 +20,7 @@ wasm-bindgen = "0.2"
|
||||||
wasm-bindgen-futures = "0.4"
|
wasm-bindgen-futures = "0.4"
|
||||||
url = "2.0"
|
url = "2.0"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
|
base64 = "0.13"
|
||||||
|
|
||||||
[dependencies.iced_core]
|
[dependencies.iced_core]
|
||||||
version = "0.4"
|
version = "0.4"
|
||||||
|
|
|
||||||
|
|
@ -72,12 +72,15 @@ impl<Message> Widget<Message> for Image {
|
||||||
use dodrio::builder::*;
|
use dodrio::builder::*;
|
||||||
use dodrio::bumpalo::collections::String;
|
use dodrio::bumpalo::collections::String;
|
||||||
|
|
||||||
let src = String::from_str_in(
|
let src = match self.handle.data.as_ref() {
|
||||||
match self.handle.data.as_ref() {
|
Data::Path(path) => {
|
||||||
Data::Path(path) => path.to_str().unwrap_or(""),
|
String::from_str_in(path.to_str().unwrap_or(""), bump)
|
||||||
|
}
|
||||||
|
Data::Bytes(bytes) => {
|
||||||
|
// The web is able to infer the kind of image, so we don't have to add a dependency on image-rs to guess the mime type.
|
||||||
|
bumpalo::format!(in bump, "data:;base64,{}", base64::encode(bytes))
|
||||||
},
|
},
|
||||||
bump,
|
}
|
||||||
)
|
|
||||||
.into_bump_str();
|
.into_bump_str();
|
||||||
|
|
||||||
let alt = String::from_str_in(&self.alt, bump).into_bump_str();
|
let alt = String::from_str_in(&self.alt, bump).into_bump_str();
|
||||||
|
|
@ -122,6 +125,14 @@ impl Handle {
|
||||||
Self::from_data(Data::Path(path.into()))
|
Self::from_data(Data::Path(path.into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates an image [`Handle`] containing the image data directly.
|
||||||
|
///
|
||||||
|
/// This is useful if you already have your image loaded in-memory, maybe
|
||||||
|
/// because you downloaded or generated it procedurally.
|
||||||
|
pub fn from_memory(bytes: Vec<u8>) -> Handle {
|
||||||
|
Self::from_data(Data::Bytes(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
fn from_data(data: Data) -> Handle {
|
fn from_data(data: Data) -> Handle {
|
||||||
let mut hasher = Hasher::default();
|
let mut hasher = Hasher::default();
|
||||||
data.hash(&mut hasher);
|
data.hash(&mut hasher);
|
||||||
|
|
@ -160,12 +171,16 @@ impl From<&str> for Handle {
|
||||||
pub enum Data {
|
pub enum Data {
|
||||||
/// A remote image
|
/// A remote image
|
||||||
Path(PathBuf),
|
Path(PathBuf),
|
||||||
|
|
||||||
|
/// In-memory data
|
||||||
|
Bytes(Vec<u8>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for Data {
|
impl std::fmt::Debug for Data {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Data::Path(path) => write!(f, "Path({:?})", path),
|
Data::Path(path) => write!(f, "Path({:?})", path),
|
||||||
|
Data::Bytes(_) => write!(f, "Bytes(...)"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue