Files
Marcos Paulo 68dd899844 feat: viewers round 2: pause, seek with hover preview, draw bar, PDF actions, note lightbox with zoom animation
Video: real pause, interactive seek track with ffmpeg hover thumbnails, skip, speed, loop, mute, open/reveal/copy, decode width follows the tab, probe and spawn off the UI thread. PDF: page jump, zoom, fit width/page, open/reveal/copy. Draw bar redesigned as one card. WebP opens in the tab lightbox. Note embeds open a lightbox with zoom-in/out animation from the thumbnail, a tools bar, a source link, and the same for videos, PDFs and other files.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-20 23:59:14 -03:00

96 lines
4.3 KiB
Rust

//! The lightbox over a note, driven the way a user does: click an embedded picture, zoom it with
//! Ctrl and the wheel, close it with Escape and watch it fly back and go.
use egui::accesskit::Role;
use egui::{Event, Modifiers, MouseWheelUnit, TouchPhase, vec2};
use egui_kittest::kittest::{NodeT as _, Queryable};
use notescraft_engine::Session;
use notescraft_themes::builtin;
use notescraft_ui_egui::attachment_drop::Images;
use notescraft_ui_egui::note_view::{self, BodyHooks, CalloutFolds};
use notescraft_ui_egui::theme::Tokens;
/// The state the harness keeps between frames.
struct Note {
images: Images,
doc: notescraft_markdown::Document,
tokens: Tokens,
folds: CalloutFolds,
warmed: bool,
}
/// A note that embeds `pic.png` (64 by 48 pixels) in a vault of its own.
fn note(name: &str) -> egui_kittest::Harness<'static, Note> {
let root = std::env::temp_dir().join(format!("notescraft-lightbox-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("the temp vault is made");
image::RgbImage::from_pixel(64, 48, image::Rgb([40, 90, 160])).save_with_format(root.join("pic.png"), image::ImageFormat::Png).expect("the png is written");
let session = Session::open(&root).expect("the temp vault opens");
let doc = notescraft_markdown::parse("Before\n\n![[pic.png]]\n\nAfter\n");
let images = Images::load(&session, "note.md", &doc).expect("the pictures load");
let tokens = Tokens::new(builtin("catppuccin-mocha").expect("the theme is there"));
egui_kittest::Harness::builder().with_size(egui::Vec2::new(900.0, 700.0)).with_max_steps(400).build_ui_state(
|ui, state: &mut Note| {
if !state.warmed {
notescraft_ui_egui::fonts::install(ui.ctx());
egui_extras::install_image_loaders(ui.ctx());
state.warmed = true;
return;
}
let (tokens, images) = (state.tokens.clone(), state.images.clone());
let mut embed = |ui: &mut egui::Ui, target: &str, size: Option<&str>| images.show(ui, target, size);
let mut fence = |_: &mut egui::Ui, _: std::ops::Range<usize>, _: &str| {};
note_view::show_body_with_fences(
ui,
&tokens,
&state.doc,
&|_: &str| true,
&mut state.folds,
BodyHooks { fence: &mut fence, embed: &mut embed, editable: false },
);
images.show_lightbox(&ui.ctx().clone(), &tokens);
},
Note { images, doc, tokens, folds: CalloutFolds::default(), warmed: false },
)
}
/// The text of the zoom chip, when the lightbox is up.
fn zoom_chip(harness: &egui_kittest::Harness<'_, Note>) -> Option<String> {
harness.query_all_by_label_contains("%").filter_map(|node| node.accesskit_node().label()).find(|text| text.ends_with('%') && text != "100%")
}
#[test]
fn click_zoom_escape_and_the_lightbox_goes() {
let mut harness = note("flow");
harness.run_steps(3);
assert!(zoom_chip(&harness).is_none(), "closed at first");
harness.get_by_role(Role::Image).click();
harness.run_steps(30);
let before = zoom_chip(&harness);
assert!(before.is_some(), "the lightbox is up after the click");
assert!(harness.query_by_label("Zoom in").is_some(), "the bar is there");
harness.event_modifiers(
Event::MouseWheel { unit: MouseWheelUnit::Line, delta: vec2(0.0, 3.0), phase: TouchPhase::Move, modifiers: Modifiers::COMMAND },
Modifiers::COMMAND,
);
harness.run_steps(10);
let after = zoom_chip(&harness);
assert_ne!(before, after, "Ctrl+wheel changes the zoom");
harness.key_press(egui::Key::Escape);
harness.run_steps(60);
assert!(zoom_chip(&harness).is_none(), "the lightbox is gone after the flight back");
}
/// Renders the open lightbox to the PNG named by `NOTESCRAFT_LIGHTBOX_PNG` (a look for the eye).
#[test]
#[ignore = "needs a GPU adapter; run by hand with NOTESCRAFT_LIGHTBOX_PNG=/path/out.png"]
fn the_open_lightbox_renders() {
let Ok(out) = std::env::var("NOTESCRAFT_LIGHTBOX_PNG") else { return };
let mut harness = note("render");
harness.run_steps(3);
harness.get_by_role(Role::Image).click();
harness.run_steps(40);
let picture = harness.render().expect("the frame renders");
picture.save(out).expect("the png is written");
}