use super::SharedString;
use crate::geometry::{Points, Size};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ImageAsset {
pub url: &'static str,
pub size: Size<Points>,
pub scales: &'static [f32],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FontAsset {
pub uri: &'static str,
pub family: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FileAsset {
pub uri: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AssetKind {
Image,
Font,
File,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AssetFile {
pub uri: &'static str,
pub source: Option<&'static str>,
pub kind: AssetKind,
}
#[doc(hidden)]
#[macro_export]
#[cfg(any(test, debug_assertions, feature = "declarations"))]
macro_rules! __asset_source {
($path:literal) => {
::core::option::Option::Some($path)
};
}
#[doc(hidden)]
#[macro_export]
#[cfg(not(any(test, debug_assertions, feature = "declarations")))]
macro_rules! __asset_source {
($path:literal) => {
::core::option::Option::None
};
}
impl AssetFile {
pub fn path(&self) -> &'static str {
self.uri.strip_prefix("asset:/").unwrap_or(self.uri)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ImageSrc {
pub(crate) uri: SharedString,
pub(crate) size: Option<(f32, f32)>,
pub(crate) scales: crate::ImageScales,
pub(crate) data: Option<crate::ImageData>,
}
impl<T: Into<SharedString>> From<T> for ImageSrc {
fn from(uri: T) -> Self {
Self {
uri: uri.into(),
size: None,
scales: crate::ImageScales::NONE,
data: None,
}
}
}
impl From<ImageAsset> for ImageSrc {
fn from(asset: ImageAsset) -> Self {
Self {
uri: asset.url.into(),
size: Some((asset.size.width.0, asset.size.height.0)),
scales: asset.scales.iter().copied().collect(),
data: None,
}
}
}
impl From<&ImageAsset> for ImageSrc {
fn from(asset: &ImageAsset) -> Self {
(*asset).into()
}
}
impl ImageSrc {
pub fn size(mut self, size: Size<Points>) -> Self {
self.size = Some((size.width.0, size.height.0));
self
}
pub fn is_empty(&self) -> bool {
self.uri.is_empty()
}
pub(crate) fn menu_icon(&self) -> Option<crate::ImageSource> {
(!self.is_empty()).then(|| {
crate::ImageSource {
uri: self.uri.to_arc(),
size: self.size.map(|(w, h)| crate::geometry::size_pt(w, h)),
scales: self.scales,
data: self.data.clone(),
}
.inline()
})
}
pub fn rgba(width: u32, height: u32, pixels: impl AsRef<[u8]> + Send + Sync + 'static) -> Self {
Self {
uri: crate::image::rgba_key().into(),
size: Some((width as f32, height as f32)),
scales: crate::ImageScales::NONE,
data: Some(crate::ImageData::from_rgba(width, height, pixels)),
}
}
pub fn svg_markup(markup: &str) -> Self {
use std::fmt::Write;
let mut uri = String::with_capacity(markup.len() + 32);
uri.push_str("data:image/svg+xml,");
for b in markup.bytes() {
if b.is_ascii_alphanumeric() || b" -._~!$'()*+,;=:@/?".contains(&b) {
uri.push(b as char);
} else {
let _ = write!(uri, "%{b:02X}");
}
}
uri.into()
}
}
crate::static_reactive!(
ImageSrc: ImageSrc,
ImageAsset,
&ImageAsset,
super::SharedString,
String,
&'static str
);
#[cfg(test)]
mod tests {
#[test]
fn a_development_build_keeps_the_path() {
assert_eq!(
crate::__asset_source!("/app/assets/a.png"),
Some("/app/assets/a.png")
);
}
}