diff --git a/Cargo.lock b/Cargo.lock index dcd84eb01..46fac52ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,6 +254,7 @@ dependencies = [ "engine", "flexi_logger", "i18n", + "image", "log", "platform", "profiling", diff --git a/README.md b/README.md index 1e9ede460..5ff9f991c 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ HTTP, GraphQL and WebSocket. Collections, environments and variables, a history ## Notes and snippets -![The notes: by date, as a board of folders, a search quoting the line that matched, the facets, a selection, a snippet with its fields and their form, its history, a formatted Note and its presentation, a todo list, a Calcul sheet, the palette, the trash, and the light theme](docs/tour-notes.gif) +![The notes: by date, as a board of folders, a search quoting the line that matched, the facets, a selection, a snippet with its fields and their form, its history, a formatted Note and its presentation, a todo list, a Calcul sheet, the palette and the trash](docs/tour-notes.gif) - **Three kinds of note.** Snippets highlighted in twenty-six languages and formatted with Prettier; Notes written formatted — headings, lists, checkboxes, tables, code blocks — and stored as Markdown; todo lists you tick off. - **`{{fields}}`.** `ssh -p {{port=22}} {{user}}@{{host}}` asks for its values before it reaches the clipboard, with defaults you set once. @@ -91,7 +91,7 @@ Every tool runs on your machine and calls nothing. Search them, star the ones yo ## API client -![The API client: its collections, an HTTP request, a GraphQL request, a WebSocket, the environments, the code generated from a request, and the light theme](docs/tour-http.gif) +![The API client: its collections, an HTTP request, a GraphQL request, a WebSocket, the environments and the code generated from a request](docs/tour-http.gif) - **HTTP and GraphQL** requests in tabs, with bodies, authentication and cookies; a GraphQL answer's errors read apart from its data. - **WebSocket** connections with a live log, and messages kept to send again. diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml index 207ed1129..6dafc1b12 100644 --- a/crates/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -77,6 +77,8 @@ puffin_http = { workspace = true, optional = true } winit.workspace = true [dev-dependencies] +# `examples/tours.rs`, which assembles the README's tours, and their test. +image.workspace = true # `examples/icons.rs`, which draws the icons. resvg.workspace = true tempfile.workspace = true diff --git a/crates/app/examples/tours.rs b/crates/app/examples/tours.rs new file mode 100644 index 000000000..7489bf21e --- /dev/null +++ b/crates/app/examples/tours.rs @@ -0,0 +1,148 @@ +//! Assembles the README's tours from the screens `app::screens` draws: +//! `DEVNOTES_SCREENS= cargo test -p ui --lib app::screens`, then +//! `cargo run -p app --example tours -- `, which writes `docs/tour-*.gif`. +//! +//! One theme throughout, the dark one: a loop that ends on a light frame flashes. + +use std::collections::HashMap; +use std::fs::File; +use std::path::{Path, PathBuf}; +use std::time::Duration; + +use image::codecs::gif::{GifEncoder, Repeat}; +use image::{Delay, Frame, Rgba, RgbaImage}; + +/// A tour and its screens, in the order of the README's caption. +const TOURS: [(&str, &[&str]); 3] = [ + ( + "notes", + &[ + "notes-list", + "notes-board", + "notes-search", + "notes-facets", + "notes-selection", + "editor-snippet-fields", + "notes-fields-form", + "editor-snippet-revisions", + "editor-note", + "editor-note-presentation", + "editor-checklist", + "editor-calc", + "notes-palette", + "notes-trash", + ], + ), + ( + "tools", + &[ + "tools-home", + "tool-jwt", + "tool-json-generator", + "tool-diff", + "tool-hash", + "tool-password", + "tool-dates", + "tool-zones", + "tool-cron", + "tool-cidr", + "tool-colour", + "tool-qr", + "tool-identity", + "tool-http-status", + ], + ), + ( + "http", + &[ + "http-home", + "http-request", + "http-graphql", + "http-websocket", + "http-environments", + "http-code", + ], + ), +]; + +const SHOWN: Duration = Duration::from_secs(2); +/// What a GIF holds. +const COLOURS: usize = 256; + +fn main() { + let screens = std::env::args_os().nth(1).map(PathBuf::from); + let screens = screens.expect("the folder the screens were drawn in"); + let docs = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../docs"); + for (name, frames) in TOURS { + let path = docs.join(format!("tour-{name}.gif")); + let frames: Vec = frames.iter().map(|one| screen(&screens, one)).collect(); + write(&path, &frames); + let bytes = std::fs::metadata(&path).expect("written").len(); + println!("{} {} frames, {bytes} bytes", path.display(), frames.len()); + } +} + +fn screen(screens: &Path, name: &str) -> RgbaImage { + let path = screens.join(format!("{name}--dark.png")); + let image = image::open(&path).unwrap_or_else(|error| panic!("{}: {error}", path.display())); + image.into_rgba8() +} + +/// One palette for the whole loop, or a ground would change its shade from a frame to the next. +/// ⚠️ Each frame whole: `image` writes frames that clear what is under them. +fn write(path: &Path, frames: &[RgbaImage]) { + let palette = palette(frames); + let mut nearest = HashMap::new(); + let file = File::create(path).unwrap_or_else(|error| panic!("{}: {error}", path.display())); + let mut encoder = GifEncoder::new(file); + encoder.set_repeat(Repeat::Infinite).expect("a loop"); + for frame in frames { + let mut reduced = frame.clone(); + for pixel in reduced.pixels_mut() { + *pixel = *nearest + .entry(*pixel) + .or_insert_with(|| closest(&palette, *pixel)); + } + let delay = Delay::from_saturating_duration(SHOWN); + encoder + .encode_frame(Frame::from_parts(reduced, 0, 0, delay)) + .expect("a frame"); + } +} + +/// The colours most seen, each far enough from the ones before it: grounds and inks stay exact, +/// and what is left goes to the edges of the letters. +fn palette(frames: &[RgbaImage]) -> Vec> { + let mut seen: HashMap, u32> = HashMap::new(); + for pixel in frames.iter().flat_map(RgbaImage::pixels) { + *seen.entry(*pixel).or_default() += 1; + } + let mut seen: Vec<(Rgba, u32)> = seen.into_iter().collect(); + seen.sort_unstable_by_key(|(colour, count)| (std::cmp::Reverse(*count), colour.0)); + let mut apart = 0; + loop { + let mut kept: Vec> = Vec::new(); + for (colour, _) in &seen { + if kept.iter().all(|one| distance(*one, *colour) >= apart) { + kept.push(*colour); + } + if kept.len() > COLOURS { + break; + } + } + if kept.len() <= COLOURS { + return kept; + } + apart += 1; + } +} + +fn closest(palette: &[Rgba], colour: Rgba) -> Rgba { + let by_distance = palette.iter().min_by_key(|one| distance(**one, colour)); + *by_distance.expect("a frame has colours") +} + +fn distance(a: Rgba, b: Rgba) -> u32 { + let channels = a.0.iter().zip(b.0); + channels.map(|(a, b)| u32::from(a.abs_diff(b)).pow(2)).sum() +} diff --git a/crates/app/tests/tours.rs b/crates/app/tests/tours.rs new file mode 100644 index 000000000..f415fb313 --- /dev/null +++ b/crates/app/tests/tours.rs @@ -0,0 +1,35 @@ +//! The README's tours: one theme throughout, and light enough for a page. + +use std::path::Path; + +use image::AnimationDecoder; +use image::codecs::gif::GifDecoder; + +const HEAVIEST: usize = 1_000_000; +/// In the title bar's corner, clear of everything on it: its ground, in every frame but a +/// presentation's, whose ground is as dark. +const GROUND: (u32, u32) = (3, 3); +const DARK: u8 = 40; + +#[test] +fn every_frame_of_every_tour_is_dark_and_each_file_under_a_megabyte() { + for name in ["notes", "tools", "http"] { + let path = Path::new(env!("CARGO_MANIFEST_DIR")); + let path = path.join(format!("../../docs/tour-{name}.gif")); + let bytes = std::fs::read(&path).unwrap(); + assert!(bytes.len() < HEAVIEST, "{name}: {} bytes", bytes.len()); + + let decoder = GifDecoder::new(std::io::Cursor::new(bytes)).unwrap(); + let mut drawn = 0; + for frame in decoder.into_frames() { + let frame = frame.unwrap(); + let whole = frame.buffer().pixels().all(|pixel| pixel.0[3] == 255); + assert!(whole, "{name}, frame {drawn}: seen through"); + let ground = frame.buffer().get_pixel(GROUND.0, GROUND.1); + let brightest = ground.0[..3].iter().max().unwrap(); + assert!(*brightest < DARK, "{name}, frame {drawn}: {ground:?}"); + drawn += 1; + } + assert!(drawn > 5, "{name}: {drawn} frames"); + } +} diff --git a/docs/architecture.md b/docs/architecture.md index b8e689fc2..1db7b36b8 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -6597,8 +6597,11 @@ pixels: the geometry and SSIM scoring the plan describes are postponed (D24). `app::screens` replays the reference's own screens, its data, gestures, file names and window sizes: `DEVNOTES_SCREENS= cargo test -p ui --lib app::screens` writes the PNGs to set beside the `angular-reference` zip, one profile copy a screen since a board shown or a rail put away is -remembered. Without the variable it does nothing, and CI does not set it. The three tours of the README (`docs/tour-notes.gif`, -`docs/tour-tools.gif`, `docs/tour-http.gif`) are frames of those screens, two seconds each. +remembered. Without the variable it does nothing, and CI does not set it. The three tours of the +README (`docs/tour-notes.gif`, `docs/tour-tools.gif`, `docs/tour-http.gif`) are frames of those +screens, two seconds each, assembled by `cargo run -p app --example tours -- `: in the dark +theme throughout, with one palette a loop. A frame of another theme, or a file of a megabyte, +fails `crates/app/tests/tours.rs`. ### The scenarios, on the assembled window diff --git a/docs/tour-http.gif b/docs/tour-http.gif index e4aac7aaf..c703157a9 100644 Binary files a/docs/tour-http.gif and b/docs/tour-http.gif differ diff --git a/docs/tour-notes.gif b/docs/tour-notes.gif index ad980714b..077feb180 100644 Binary files a/docs/tour-notes.gif and b/docs/tour-notes.gif differ diff --git a/docs/tour-tools.gif b/docs/tour-tools.gif index cc0ee39fb..4853e3ba3 100644 Binary files a/docs/tour-tools.gif and b/docs/tour-tools.gif differ