cotis_web/images.rs
1//! Image URL types for web rendering.
2//!
3//! Image drawables ([`cotis_defaults::render_commands::Image`]) require an associated type that
4//! implements [`URLImage`]. The renderer uses [`URLImage::url`] to set CSS
5//! `background-image: url("...")`.
6//!
7//! [`URLImageEnum`] is the usual choice for [`ElementConfig`](cotis_defaults::element_configs::ElementConfig)
8//! image type parameters in WASM apps.
9//!
10//! # Path conventions
11//!
12//! URLs are passed to the browser as-is. Use paths relative to the page origin (e.g.
13//! `"assets/logo.png"`) or absolute URLs. The renderer does not prepend
14//! [`HTMLRenderer::new`](crate::renderer::HTMLRenderer::new)'s stored origin automatically today.
15//!
16//! # API status
17//!
18//! The image URL API is **incomplete** (`// TODO Finish this` in source). Current behavior
19//! delegates to `get_path()` on [`PNGImage`],
20//! [`JPEGImage`], and
21//! [`SVGImage`]. Blob URLs, data URLs, and async
22//! loading are not yet handled.
23
24use cotis_defaults::generic_image::{JPEGImage, PNGImage, SVGImage};
25
26/// Types that can provide a URL string for CSS `background-image`.
27pub trait URLImage {
28 /// Returns the URL or path used in `background-image: url(...)`.
29 fn url(&self) -> &str;
30}
31
32impl URLImage for PNGImage {
33 fn url(&self) -> &str {
34 self.get_path()
35 }
36}
37
38impl URLImage for JPEGImage {
39 fn url(&self) -> &str {
40 self.get_path()
41 }
42}
43
44impl URLImage for SVGImage {
45 fn url(&self) -> &str {
46 self.get_path()
47 }
48}
49
50/// Sum type over the standard Cotis image formats for web apps.
51pub enum URLImageEnum {
52 /// PNG image path or URL.
53 PNG(PNGImage),
54 /// JPEG image path or URL.
55 JPEG(JPEGImage),
56 /// SVG image path or URL.
57 SVG(SVGImage),
58}
59
60impl URLImage for URLImageEnum {
61 fn url(&self) -> &str {
62 match self {
63 URLImageEnum::PNG(s) => s.url(),
64 URLImageEnum::JPEG(s) => s.url(),
65 URLImageEnum::SVG(s) => s.url(),
66 }
67 }
68}