cotis_web/lib.rs
1//! Browser/WASM render backend for the [Cotis](https://docs.rs/cotis) UI framework.
2//!
3//! `cotis-web` implements [`CotisRendererAsync`](cotis::renders::CotisRendererAsync) for
4//! HTML/CSS output. Layout output from [`cotis-layout`](https://docs.rs/cotis-layout) is
5//! converted to [`cotis_defaults::render_commands`](https://docs.rs/cotis-defaults) draw
6//! commands by [`cotis-pipes`](https://docs.rs/cotis-pipes), then mapped to DOM elements
7//! via CSS absolute positioning and a companion JavaScript module (`renderer.js`).
8//!
9//! # Stack
10//!
11//! - [`wasm-bindgen`](https://docs.rs/wasm-bindgen) / [`web-sys`](https://docs.rs/web-sys) —
12//! Rust ↔ browser FFI
13//! - [`serde_json`](https://docs.rs/serde_json) — custom-element property serialization
14//! - [`renderer.js`](https://github.com/igna-778/cotis-web) — frame timing, input, DOM host
15//! management (loaded via `wasm-bindgen` `module =` attribute)
16//!
17//! This crate is **async-only**: use [`HTMLRenderer`](renderer::HTMLRenderer) with
18//! [`AsyncRenderApp`](cotis::cotis_app::AsyncRenderApp), not the sync
19//! [`CotisRenderer`](cotis::renders::CotisRenderer) trait.
20//!
21//! # DOM model
22//!
23//! Each render command id maps to one host element with id `cotis-{id}`. Drawables accumulate
24//! CSS on that node via [`HTMLCanvas::ensure_command_element`](rendering::HTMLCanvas::ensure_command_element)
25//! and [`HTMLCanvas::current_element_css_push`](rendering::HTMLCanvas::current_element_css_push).
26//! At the end of a frame, [`HTMLCanvas::finish`](rendering::HTMLCanvas::finish) removes DOM
27//! nodes that were not used.
28//!
29//! # Quick start
30//!
31//! ```rust,ignore
32//! use cotis::cotis_app::{AsyncRenderApp, CotisApp};
33//! use cotis_layout::preamble::CotisLayoutManager;
34//! use cotis_pipes::cotis_layout_pipes::CotisLayoutToRenderListPipeForGenerics;
35//! use cotis_utils::math::Dimensions;
36//! use cotis_web::renderer::HTMLRenderer;
37//!
38//! let renderer = HTMLRenderer::new();
39//! let manager = CotisLayoutManager::new(Dimensions::new(800.0, 800.0));
40//! let mut app = CotisApp::new(renderer, manager, CotisLayoutToRenderListPipeForGenerics);
41//! // AsyncRenderApp::compute_frame_async(&mut app, |layout| { ... }).await;
42//! ```
43//!
44//! See [`web-example`](../web-example/src/lib.rs) for a complete UI tree with custom HTML,
45//! images, and form state reading.
46//!
47//! # WASM bootstrap
48//!
49//! Typical page load sequence (see bundled `resources/on-root/index.html`):
50//!
51//! 1. `await init()` — load the `.wasm` binary (wasm-bindgen glue).
52//! 2. `init_wasm()` — initialize logging and a panic hook (wasm export from this crate).
53//! 3. Call an app entry export:
54//! - **`main_run`** — WASM export in the app crate (e.g. [`web-example`](../web-example/src/lib.rs))
55//! that calls [`cotis_launch_async`](cotis::launch::cotis_launch_async) after `init_wasm()`.
56//! Requires the `app_launch` feature on `cotis` / `cotis-web`.
57//!
58//! # Build workflow
59//!
60//! Use the [`cotis-web-builder`](../cotis-web-builder/README.md) crate / CLI plugin to run
61//! `wasm-pack`, copy `renderer.js` and `index.html`, and patch the WASM import path.
62//!
63//! # Modules
64//!
65//! | Module | Visibility | Role |
66//! |--------|------------|------|
67//! | [`renderer`] | public | [`HTMLRenderer`](renderer::HTMLRenderer) — main app-facing type |
68//! | [`rendering`] | public | [`HTMLCanvas`](rendering::HTMLCanvas), [`HTMLDrawable`](rendering::HTMLDrawable), draw pipeline |
69//! | [`images`] | public | [`URLImage`](images::URLImage) trait and [`URLImageEnum`](images::URLImageEnum) for CSS `background-image` |
70//! | [`color`] | public | Cotis [`Color`](cotis_defaults::colors::Color) → CSS string helpers |
71//! | [`custom_data`] | public | [`ExtraHTMLData`](custom_data::ExtraHTMLData) for custom DOM tags and styles |
72//! | [`cotis_traits`] | public | Cotis context trait impls on [`HTMLRenderer`](renderer::HTMLRenderer) |
73//! | `start_up` | public | `init`, `main_run` helpers for WASM app crates |
74//! | `interactivity` | crate-private | Mouse/keyboard providers for the browser |
75//! | `web_functions` | crate-private | Low-level JS FFI bindings |
76//!
77//! # Custom HTML and images
78//!
79//! Attach [`ExtraHTMLData`](custom_data::ExtraHTMLData) as the `extra_data` generic on
80//! [`ElementConfig`](cotis_defaults::element_configs::ElementConfig) / render commands to
81//! control the host element tag (`div`, `button`, …) and optional inline CSS.
82//!
83//! Use [`URLImageEnum`](images::URLImageEnum) as the image type parameter and implement
84//! [`URLImage`](images::URLImage) (or use the provided enum) so image drawables emit
85//! `background-image: url(...)`.
86//!
87//! Read back form state with [`HTMLRenderer::get_custom_element_html`](renderer::HTMLRenderer::get_custom_element_html) and
88//! [`HTMLRenderer::get_custom_element_properties`](renderer::HTMLRenderer::get_custom_element_properties).
89//!
90//! Load fonts with [`HTMLRenderer::load_font`](renderer::HTMLRenderer::load_font) and a [`FontManager`](cotis_utils::font_manager::FontManager).
91//!
92//! # Features
93//!
94//! By default, only solid [`Color`](cotis_defaults::colors::Color) values are supported.
95//!
96//! | Feature | Effect |
97//! |---------|--------|
98//! | `complex_color` | Background/fill colors become [`ColorLayer`](cotis_defaults::colors::ColorLayer) |
99//! | `complex_colored_text` | Text colors become `ColorLayer` (implies `complex_color` in `cotis-defaults`) |
100//!
101//! Enable via Cargo, e.g. `cotis-web = { path = "...", features = ["complex_color"] }`.
102//!
103//! **Gradient roadmap:** when features are enabled, non-`Solid` [`ColorLayer`](cotis_defaults::colors::ColorLayer)
104//! variants currently map to `"transparent"` in the Rust draw path. Full CSS gradient
105//! support is planned; the legacy JSON `draw_frame` path in `renderer.js` already handles
106//! gradients but is not used by [`HTMLRenderer`](renderer::HTMLRenderer).
107//!
108//! # Related crates (not part of this crate)
109//!
110//! - [`cotis-web-builder`](../cotis-web-builder) — WASM build routine
111//! - [`web-example`](../web-example) — full example application
112
113pub(crate) mod web_functions;
114
115pub mod renderer;
116
117pub mod rendering;
118
119pub mod start_up;
120
121pub mod images;
122
123pub mod color;
124
125pub mod custom_data;
126
127pub mod cotis_traits;
128
129#[allow(dead_code)]
130pub(crate) mod interactivity;