290 lines
11 KiB
Rust
290 lines
11 KiB
Rust
//! The header of a note: the banner at the top of the note view and the icon above the
|
|
//! title.
|
|
//!
|
|
//! A note with a banner paints the picture across the full width, cropped around the focus
|
|
//! of the crop; a banner that names an address paints a placeholder and fetches nothing; a
|
|
//! banner that names a file the vault does not hold paints nothing. The icon of a note is
|
|
//! painted above the title, in the tint of the note.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use egui::{Color32, Context, RawInput, Rect, Vec2, pos2, vec2};
|
|
use notescraft_ui_egui::app::decor::DecorSnapshot;
|
|
use notescraft_ui_egui::note_view::{banner, title_icon};
|
|
use notescraft_ui_egui::theme::Tokens;
|
|
use notescraft_ui_egui::{fonts, motion, tree};
|
|
use serde_json::{Value, json};
|
|
|
|
/// A picture of the vault, used as the banner of a note.
|
|
const PICTURE: &[u8] = include_bytes!("fixtures/cover-a.png");
|
|
/// The name of the picture inside the vault.
|
|
const COVER: &str = "cover.png";
|
|
/// The note the banner belongs to.
|
|
const NOTE: &str = "note.md";
|
|
/// The emoji icon of a note.
|
|
const ROCKET: &str = "🚀";
|
|
|
|
/// The lock the tests take: the cache of the banners is shared by the whole process.
|
|
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
|
|
|
/// The lock of the tests, taken while a test runs.
|
|
fn lock() -> std::sync::MutexGuard<'static, ()> {
|
|
LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
|
}
|
|
|
|
/// The tokens of the default theme.
|
|
fn tokens() -> Tokens {
|
|
let theme = notescraft_themes::builtin("catppuccin-mocha").expect("the theme is bundled");
|
|
Tokens::new(theme)
|
|
}
|
|
|
|
/// A context with the fonts, the image loaders and no motion.
|
|
fn context() -> Context {
|
|
let ctx = Context::default();
|
|
fonts::install(&ctx);
|
|
motion::set_enabled(&ctx, false);
|
|
ctx.all_styles_mut(|style| style.animation_time = 0.0);
|
|
egui_extras::install_image_loaders(&ctx);
|
|
ctx
|
|
}
|
|
|
|
/// A vault with the picture of a banner in it.
|
|
fn vault(name: &str) -> PathBuf {
|
|
let root = std::env::temp_dir().join(format!("notescraft-banner-{name}-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
std::fs::create_dir_all(&root).expect("the temp vault is made");
|
|
std::fs::write(root.join(COVER), PICTURE).expect("the picture is written");
|
|
root
|
|
}
|
|
|
|
/// The snapshot of a vault with the decorations of `items`.
|
|
fn snapshot(items: &Value) -> DecorSnapshot {
|
|
let mut snapshot = DecorSnapshot::default();
|
|
assert!(snapshot.apply(&json!({ "items": items, "generation": 1 })), "the answer fills the snapshot");
|
|
snapshot
|
|
}
|
|
|
|
/// The textures of a frame, applied to the context so the next frame can paint them.
|
|
fn apply_textures(ctx: &egui::Context, output: &mut egui::FullOutput) {
|
|
let manager = ctx.tex_manager();
|
|
let mut manager = manager.write();
|
|
for (id, deltas) in output.textures_delta.set.drain() {
|
|
for delta in deltas {
|
|
manager.set(id, delta);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The shapes one frame of the UI paints.
|
|
fn frame(ctx: &Context, mut draw: impl FnMut(&mut egui::Ui)) -> Vec<egui::Shape> {
|
|
let mut output = ctx.run_ui(RawInput { screen_rect: Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(600.0, 400.0))), ..Default::default() }, |ui| draw(ui));
|
|
apply_textures(ctx, &mut output);
|
|
output.shapes.into_iter().map(|shape| shape.shape).collect()
|
|
}
|
|
|
|
/// The shapes of the banner of a note, painted in a frame of its own.
|
|
fn banner_shapes(ctx: &Context, root: &Path, height: f32) -> Vec<egui::Shape> {
|
|
let tokens = tokens();
|
|
frame(ctx, |ui| {
|
|
banner::show(ui, &tokens, root, NOTE, height);
|
|
})
|
|
}
|
|
|
|
/// The shapes of the icon above the title of a note.
|
|
fn icon_shapes(ctx: &Context) -> Vec<egui::Shape> {
|
|
let tokens = tokens();
|
|
frame(ctx, |ui| {
|
|
title_icon::show(ui, &tokens, NOTE);
|
|
})
|
|
}
|
|
|
|
/// The texts one frame paints.
|
|
fn texts(shapes: &[egui::Shape]) -> Vec<String> {
|
|
shapes
|
|
.iter()
|
|
.filter_map(|shape| match shape {
|
|
egui::Shape::Text(text) => Some(text.galley.text().to_owned()),
|
|
_ => None,
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The pictures one frame paints: a textured rectangle or a mesh.
|
|
fn pictures(shapes: &[egui::Shape]) -> Vec<egui::Shape> {
|
|
shapes.iter().filter(|shape| is_textured(shape)).cloned().collect()
|
|
}
|
|
|
|
/// Whether the shape paints a texture of its own.
|
|
fn is_textured(shape: &egui::Shape) -> bool {
|
|
match shape {
|
|
egui::Shape::Rect(rect) => rect.brush.as_deref().is_some_and(|brush| brush.uv != Rect::ZERO),
|
|
egui::Shape::Mesh(mesh) => mesh.texture_id != egui::TextureId::default(),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
/// The box the picture of a frame covers.
|
|
fn picture_box(shapes: &[egui::Shape]) -> Option<Rect> {
|
|
let mut bounds: Option<Rect> = None;
|
|
for shape in shapes {
|
|
match shape {
|
|
egui::Shape::Rect(rect) if rect.brush.as_deref().is_some_and(|brush| brush.uv != Rect::ZERO) => {
|
|
bounds = Some(bounds.map_or(rect.rect, |box_| box_.union(rect.rect)));
|
|
}
|
|
egui::Shape::Mesh(mesh) => {
|
|
for vertex in &mesh.vertices {
|
|
let point = Rect::from_min_size(vertex.pos, Vec2::ZERO);
|
|
bounds = Some(bounds.map_or(point, |box_| box_.union(point)));
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
bounds
|
|
}
|
|
|
|
/// The colour of the text one frame paints.
|
|
fn text_color(shapes: &[egui::Shape], text: &str) -> Option<Color32> {
|
|
shapes.iter().find_map(|shape| match shape {
|
|
egui::Shape::Text(painted) if painted.galley.text() == text => painted.galley.job.sections.first().map(|section| section.format.color),
|
|
_ => None,
|
|
})
|
|
}
|
|
|
|
/// The banner of a note that points at the picture of the vault.
|
|
fn banner_of_the_picture() -> Value {
|
|
json!({ "banner": { "path": COVER, "x": 0.5, "y": 0.2 } })
|
|
}
|
|
|
|
#[test]
|
|
fn a_note_with_a_banner_paints_the_picture() {
|
|
let _guard = lock();
|
|
let ctx = context();
|
|
let root = vault("picture");
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: banner_of_the_picture() })));
|
|
|
|
let before = banner::started_loads();
|
|
let mut shapes = Vec::new();
|
|
for _ in 0..200 {
|
|
shapes = banner_shapes(&ctx, &root, 180.0);
|
|
if !pictures(&shapes).is_empty() {
|
|
break;
|
|
}
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
}
|
|
assert!(!pictures(&shapes).is_empty(), "the picture of the banner is painted");
|
|
assert_eq!(banner::started_loads() - before, 1, "the picture is asked for once");
|
|
assert_eq!(banner::cached_banners(), 1, "the picture is kept in the cache");
|
|
}
|
|
|
|
#[test]
|
|
fn the_banner_fills_the_width_of_the_note() {
|
|
let _guard = lock();
|
|
let ctx = context();
|
|
let root = vault("width");
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: banner_of_the_picture() })));
|
|
let mut shapes = Vec::new();
|
|
for _ in 0..200 {
|
|
shapes = banner_shapes(&ctx, &root, 180.0);
|
|
if !pictures(&shapes).is_empty() {
|
|
break;
|
|
}
|
|
std::thread::sleep(std::time::Duration::from_millis(10));
|
|
}
|
|
let Some(box_) = picture_box(&shapes) else {
|
|
panic!("the banner paints a picture: {shapes:?}");
|
|
};
|
|
assert!((box_.width() - 600.0).abs() < 1.0, "the banner is as wide as the note: {box_:?}");
|
|
assert!((box_.height() - 180.0).abs() < 1.0, "the banner is as tall as the settings say: {box_:?}");
|
|
let painted = shapes
|
|
.iter()
|
|
.find_map(|shape| match shape {
|
|
egui::Shape::Rect(rect) if rect.brush.as_deref().is_some_and(|brush| brush.uv != Rect::ZERO) => Some(rect),
|
|
_ => None,
|
|
})
|
|
.expect("the banner paints a rectangle");
|
|
assert_eq!(painted.corner_radius.nw, 0, "the top corners of the banner are square");
|
|
assert!(painted.corner_radius.sw > 0, "the bottom corners of the banner are rounded");
|
|
}
|
|
|
|
#[test]
|
|
fn the_crop_of_the_banner_follows_the_focus() {
|
|
// A picture of 200x400 in a box of 200x180: the focus at 0.2 shows the top of it.
|
|
let rect = banner::crop_uv(vec2(200.0, 400.0), vec2(200.0, 180.0), vec2(0.5, 0.2));
|
|
assert_eq!((rect.min.x, rect.min.y, rect.max.x, rect.max.y), (0.0, 0.0, 1.0, 0.45));
|
|
// The focus at the bottom shows the bottom of it.
|
|
let rect = banner::crop_uv(vec2(200.0, 400.0), vec2(200.0, 180.0), vec2(0.5, 0.9));
|
|
assert_eq!((rect.min.x, rect.min.y, rect.max.x, rect.max.y), (0.0, 0.55, 1.0, 1.0));
|
|
}
|
|
|
|
#[test]
|
|
fn a_remote_banner_paints_a_placeholder_and_fetches_nothing() {
|
|
let _guard = lock();
|
|
let ctx = context();
|
|
let root = vault("remote");
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: { "banner": { "url": "https://example.com/a.png", "x": 0.5, "y": 0.5 } } })));
|
|
|
|
let before = banner::started_loads();
|
|
let shapes = banner_shapes(&ctx, &root, 180.0);
|
|
let shown = texts(&shapes);
|
|
assert!(shown.iter().any(|text| text.contains("Remote banners are off")), "the placeholder is shown: {shown:?}");
|
|
assert_eq!(banner::started_loads() - before, 0, "no file of the vault is read");
|
|
assert_eq!(banner::cached_banners(), 0, "no picture is kept");
|
|
}
|
|
|
|
#[test]
|
|
fn a_note_without_a_banner_paints_nothing() {
|
|
let _guard = lock();
|
|
let ctx = context();
|
|
let root = vault("none");
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: { "icon": { "kind": "emoji", "value": ROCKET } } })));
|
|
|
|
let before = banner::started_loads();
|
|
let shapes = banner_shapes(&ctx, &root, 180.0);
|
|
assert!(texts(&shapes).is_empty(), "no text is painted: {:?}", texts(&shapes));
|
|
assert!(pictures(&shapes).is_empty(), "no picture is painted");
|
|
assert_eq!(banner::started_loads() - before, 0, "no file of the vault is read");
|
|
}
|
|
|
|
#[test]
|
|
fn a_banner_of_a_missing_file_paints_nothing() {
|
|
let _guard = lock();
|
|
let ctx = context();
|
|
let root = vault("missing");
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: { "banner": { "path": "not-there.png", "x": 0.5, "y": 0.5 } } })));
|
|
|
|
let before = banner::started_loads();
|
|
let shapes = banner_shapes(&ctx, &root, 180.0);
|
|
assert!(texts(&shapes).is_empty(), "no text is painted: {:?}", texts(&shapes));
|
|
assert!(pictures(&shapes).is_empty(), "no picture is painted");
|
|
assert_eq!(banner::started_loads() - before, 0, "no file of the vault is read");
|
|
}
|
|
|
|
#[test]
|
|
fn the_icon_of_a_note_is_painted_above_its_title() {
|
|
let ctx = context();
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: { "icon": { "kind": "emoji", "value": ROCKET } } })));
|
|
|
|
let shapes = icon_shapes(&ctx);
|
|
let shown = texts(&shapes);
|
|
assert!(shown.iter().any(|text| text.contains(ROCKET)), "the icon is painted: {shown:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn the_icon_wears_the_colour_of_the_note() {
|
|
let ctx = context();
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: { "icon": { "kind": "emoji", "value": ROCKET }, "color": { "kind": "rgb", "value": "#ff8800" } } })));
|
|
|
|
let shapes = icon_shapes(&ctx);
|
|
assert_eq!(text_color(&shapes, ROCKET), Some(Color32::from_rgb(0xff, 0x88, 0x00)), "the icon wears the colour of the note");
|
|
}
|
|
|
|
#[test]
|
|
fn a_note_without_an_icon_paints_nothing_above_its_title() {
|
|
let ctx = context();
|
|
tree::publish_decor(&ctx, &snapshot(&json!({ NOTE: { "banner": { "path": COVER, "x": 0.5, "y": 0.5 } } })));
|
|
|
|
let shapes = icon_shapes(&ctx);
|
|
assert!(texts(&shapes).is_empty(), "no text is painted: {:?}", texts(&shapes));
|
|
}
|