Files
Marcos Paulo 16a0786a2d
ci / ci (macos-latest) (push) Has been cancelled
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
feat: recurring task toggle, Templater render, icons in Explorer rows and tabs (MiniMax M3.1 Flash)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 11:16:23 -03:00

235 lines
10 KiB
Rust

//! The decorations of the Explorer rows and the tab icons: a decorated note paints its own
//! icon, a folder paints the icon of its folder note, the tint of the row reaches the icon and
//! a scrolled tree of 5 000 rows paints only the rows on screen.
//!
//! The Explorer never asks the engine for an icon: the snapshot of the decorations is published
//! once and every row reads it from the egui memory, so a row of the tree costs one lookup.
use std::collections::BTreeSet;
use egui::{Color32, Context, Event, RawInput, Rect, pos2, vec2};
use notescraft_ui_egui::app::decor::DecorSnapshot;
use notescraft_ui_egui::theme::Tokens;
use notescraft_ui_egui::{fonts, motion, tabs, tree};
use serde_json::{Map, Value, json};
/// The rocket icon of a note, as the engine answers it.
const ROCKET: &str = "🚀";
/// The folder icon of a folder note, as the engine answers it.
const FOLDER: &str = "📁";
/// 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
}
/// 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, events: Vec<Event>, 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(400.0, 600.0))), events, ..Default::default() }, |ui| draw(ui));
apply_textures(ctx, &mut output);
output.shapes.into_iter().map(|shape| shape.shape).collect()
}
/// The shapes the tree paints in one frame, once the images of the icons are loaded.
fn tree_shapes(ctx: &Context, model: &tree::TreeModel, collapsed: &mut BTreeSet<String>, selected: Option<&str>) -> Vec<egui::Shape> {
let tokens = tokens();
for _ in 0..3 {
frame(ctx, Vec::new(), |ui| {
tree::show(ui, &tokens, model, collapsed, selected);
});
}
frame(ctx, Vec::new(), |ui| {
tree::show(ui, &tokens, model, collapsed, selected);
})
}
/// 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 number of icons painted with the character `icon`.
fn icon_count(shapes: &[egui::Shape], icon: &str) -> usize {
shapes.iter().filter(|shape| matches!(shape, egui::Shape::Text(text) if text.galley.text() == icon)).count()
}
/// The colour of the icon painted with the character `icon`.
fn icon_color(shapes: &[egui::Shape], icon: &str) -> Option<Color32> {
shapes.iter().find_map(|shape| match shape {
egui::Shape::Text(text) if text.galley.text() == icon => text.galley.job.sections.first().map(|section| section.format.color),
_ => None,
})
}
/// The textures of the icons one frame paints inside `slot`.
fn icon_textures(shapes: &[egui::Shape], slot: Rect) -> Vec<egui::TextureId> {
shapes
.iter()
.filter_map(|shape| match shape {
egui::Shape::Rect(rect) if slot.contains_rect(rect.rect) => {
rect.brush.as_deref().filter(|brush| brush.uv != Rect::ZERO).map(|brush| brush.fill_texture_id)
}
_ => None,
})
.collect()
}
/// The slot of the icon of the row at `depth` of the tree.
fn icon_slot(depth: usize) -> Rect {
let depth = f32::from(u16::try_from(depth).unwrap_or(u16::MAX));
let left = tree::ROW_PADDING + tree::INDENT * depth;
Rect::from_center_size(pos2(left + tree::ICON_SIZE + 4.0 + tree::ICON_SIZE / 2.0, tree::ROW_HEIGHT / 2.0), vec2(tree::ICON_SIZE, tree::ICON_SIZE))
}
#[test]
fn a_decorated_note_paints_its_own_icon() {
let ctx = context();
let model = tree::TreeModel::from_paths(&["rocket.md".into(), "plain.md".into()]);
let mut collapsed = BTreeSet::new();
let plain = tree_shapes(&ctx, &model, &mut collapsed, None);
assert_eq!(icon_count(&plain, ROCKET), 0, "a plain row paints the file glyph");
tree::publish_decor(&ctx, &snapshot(&json!({ "rocket.md": { "icon": { "kind": "emoji", "value": ROCKET } } })));
let decorated = tree_shapes(&ctx, &model, &mut collapsed, None);
assert_eq!(icon_count(&decorated, ROCKET), 1, "the decorated row paints its own icon");
}
#[test]
fn a_decorated_note_paints_another_image_than_the_file_glyph() {
let ctx = context();
let model = tree::TreeModel::from_paths(&["rocket.md".into()]);
let mut collapsed = BTreeSet::new();
let plain = tree_shapes(&ctx, &model, &mut collapsed, None);
let file = icon_textures(&plain, icon_slot(0));
assert_eq!(file.len(), 1, "the plain row paints the file glyph");
tree::publish_decor(&ctx, &snapshot(&json!({ "rocket.md": { "icon": { "kind": "lucide", "value": "rocket" } } })));
let decorated = tree_shapes(&ctx, &model, &mut collapsed, None);
let rocket = icon_textures(&decorated, icon_slot(0));
assert_eq!(rocket.len(), 1, "the decorated row paints the icon of the note");
assert_ne!(file, rocket, "the row paints another image");
}
#[test]
fn a_folder_paints_the_icon_of_its_folder_note() {
let ctx = context();
let model = tree::TreeModel::from_paths(&["notes/rocket.md".into()]);
let mut collapsed = BTreeSet::new();
let plain = tree_shapes(&ctx, &model, &mut collapsed, None);
assert_eq!(icon_count(&plain, FOLDER), 0, "an undecorated folder paints the folder glyph");
assert_eq!(icon_textures(&plain, icon_slot(0)).len(), 1, "an undecorated folder paints the folder image");
tree::publish_decor(&ctx, &snapshot(&json!({ "notes": { "icon": { "kind": "emoji", "value": FOLDER } } })));
let decorated = tree_shapes(&ctx, &model, &mut collapsed, None);
assert_eq!(icon_count(&decorated, FOLDER), 1, "the folder row paints the icon of its folder note");
assert_eq!(icon_textures(&decorated, icon_slot(0)).len(), 0, "a decorated folder loses the folder image");
}
#[test]
fn the_selection_tint_reaches_the_icon() {
let ctx = context();
let tokens = tokens();
let model = tree::TreeModel::from_paths(&["rocket.md".into()]);
let mut collapsed = BTreeSet::new();
tree::publish_decor(&ctx, &snapshot(&json!({ "rocket.md": { "icon": { "kind": "emoji", "value": ROCKET } } })));
let plain = tree_shapes(&ctx, &model, &mut collapsed, None);
let selected = tree_shapes(&ctx, &model, &mut collapsed, Some("rocket.md"));
assert_eq!(icon_color(&plain, ROCKET), Some(notescraft_ui_egui::theme::color(tokens.theme.colors.text_muted)), "an idle row paints a muted icon");
assert_eq!(icon_color(&selected, ROCKET), Some(notescraft_ui_egui::theme::color(tokens.theme.colors.text)), "the selected row paints the icon brighter");
}
#[test]
fn the_colour_of_the_note_paints_its_icon() {
let ctx = context();
let model = tree::TreeModel::from_paths(&["rocket.md".into()]);
let mut collapsed = BTreeSet::new();
tree::publish_decor(
&ctx,
&snapshot(&json!({ "rocket.md": { "icon": { "kind": "emoji", "value": ROCKET }, "color": { "kind": "rgb", "value": "#ff8800" } } })),
);
let decorated = tree_shapes(&ctx, &model, &mut collapsed, None);
assert_eq!(icon_color(&decorated, ROCKET), Some(Color32::from_rgb(0xff, 0x88, 0x00)), "the icon wears the colour of the note");
}
#[test]
fn a_decorated_tab_paints_its_own_icon() {
let ctx = context();
let tokens = tokens();
tree::publish_decor(&ctx, &snapshot(&json!({ "rocket.md": { "icon": { "kind": "emoji", "value": ROCKET } } })));
let mut open = tabs::Tabs::new();
open.open("rocket.md");
let shapes = frame(&ctx, Vec::new(), |ui| {
tabs::show(ui, &tokens, &open, &[]);
});
assert_eq!(icon_count(&shapes, ROCKET), 1, "the tab paints the icon of the note");
}
#[test]
fn a_scrolled_tree_of_five_thousand_rows_paints_only_the_rows_on_screen() {
let ctx = context();
let tokens = tokens();
let paths: Vec<String> = (0..5000).map(|number| format!("note-{number:04}.md")).collect();
let model = tree::TreeModel::from_paths(&paths);
let items: Map<String, Value> = paths.iter().map(|path| (path.clone(), json!({ "icon": { "kind": "emoji", "value": ROCKET } }))).collect();
tree::publish_decor(&ctx, &snapshot(&Value::Object(items)));
let mut collapsed = BTreeSet::new();
frame(&ctx, vec![Event::PointerMoved(pos2(100.0, 300.0))], |ui| {
tree::show(ui, &tokens, &model, &mut collapsed, None);
});
let wheel: Vec<Event> = (0..300)
.map(|_| Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: vec2(0.0, -5.0),
phase: egui::TouchPhase::Move,
modifiers: egui::Modifiers::NONE,
})
.collect();
frame(&ctx, wheel, |ui| {
tree::show(ui, &tokens, &model, &mut collapsed, None);
});
let scrolled = frame(&ctx, Vec::new(), |ui| {
tree::show(ui, &tokens, &model, &mut collapsed, None);
});
let painted = icon_count(&scrolled, ROCKET);
assert!(painted > 0, "the rows on screen paint their icons");
assert!(painted < 100, "only the rows on screen are painted: {painted}");
let names: Vec<String> = scrolled
.iter()
.filter_map(|shape| match shape {
egui::Shape::Text(text) => Some(text.galley.text().to_owned()),
_ => None,
})
.collect();
let rows = names.iter().filter(|name| name.starts_with("note-")).count();
assert_eq!(painted, rows, "every painted row paints its own icon and no other row is looked up");
assert!(!names.contains(&"note-0000".to_owned()), "the tree scrolled away from the first row: {names:?}");
}