68dd899844
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>
265 lines
12 KiB
Rust
265 lines
12 KiB
Rust
//! The media tabs, drawn headless: the PDF tab shows the first page and asks the renderer for the
|
|
//! next one, and the video tab says that the file cannot play when `ffmpeg` is missing.
|
|
|
|
use egui_kittest::kittest::Queryable;
|
|
use notescraft_engine::Session;
|
|
use notescraft_themes::builtin;
|
|
use notescraft_ui_egui::app::video::{VideoState, state_id};
|
|
use notescraft_ui_egui::media_view::pdf;
|
|
use notescraft_ui_egui::media_view::video;
|
|
use notescraft_ui_egui::theme::Tokens;
|
|
use serde_json::json;
|
|
|
|
/// The theme the views are drawn with.
|
|
fn tokens() -> Tokens {
|
|
Tokens::new(builtin("catppuccin-mocha").expect("the built-in theme"))
|
|
}
|
|
|
|
/// A temp vault whose `doc.pdf` is the PDF the note `note.md` exports to.
|
|
fn session_with_pdf(name: &str) -> Session {
|
|
let root = std::env::temp_dir().join(format!("notescraft-media-pdf-{name}-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
let _ = std::fs::create_dir_all(&root);
|
|
let note = (0..90).map(|line| format!("Line {line} of the note that fills the exported PDF.")).collect::<Vec<String>>().join("\n\n");
|
|
let _ = std::fs::write(root.join("note.md"), note);
|
|
let session = Session::open(&root).expect("a temp vault opens");
|
|
let (bytes, _pages) = notescraft_engine::render_pdf(&session, &json!({ "path": "note.md" })).expect("the note exports to a PDF");
|
|
let _ = std::fs::write(root.join("doc.pdf"), bytes);
|
|
session
|
|
}
|
|
|
|
/// The state the app publishes when nothing plays.
|
|
fn idle_state(available: bool) -> VideoState {
|
|
VideoState {
|
|
available,
|
|
playing: None,
|
|
starting: None,
|
|
position: 0.0,
|
|
duration: Some(60.0),
|
|
speed: 1.0,
|
|
volume: 100,
|
|
muted: false,
|
|
looping: false,
|
|
paused: false,
|
|
}
|
|
}
|
|
|
|
/// A temp vault with a small `clip.mp4` in it.
|
|
fn session_with_video(name: &str) -> Session {
|
|
let root = std::env::temp_dir().join(format!("notescraft-media-video-{name}-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
let _ = std::fs::create_dir_all(&root);
|
|
let _ = std::fs::write(root.join("clip.mp4"), 0_u8.to_le_bytes().repeat(2048));
|
|
Session::open(&root).expect("a temp vault opens")
|
|
}
|
|
|
|
#[test]
|
|
fn the_pdf_tab_shows_the_first_page_and_asks_the_renderer_for_the_next_one() {
|
|
let session = session_with_pdf("pages");
|
|
let tokens = tokens();
|
|
let mut harness = egui_kittest::Harness::builder().with_size(egui::Vec2::new(900.0, 700.0)).build_ui_state(
|
|
|ui, warmed: &mut bool| {
|
|
if !*warmed {
|
|
// Fonts bind one pass after installing; the first pass warms only.
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
*warmed = true;
|
|
return;
|
|
}
|
|
pdf::show(ui, &tokens, &session, "doc.pdf");
|
|
},
|
|
false,
|
|
);
|
|
harness.run_steps(2);
|
|
assert!(harness.get_all_by_label_contains("Page 1").count() >= 1, "the first page is on screen");
|
|
assert_eq!(pdf::state(&harness.ctx, "doc.pdf").page, 1, "the tab is on the first page");
|
|
assert_eq!(pdf::state(&harness.ctx, "doc.pdf").asked, 1, "the renderer was asked for the first page");
|
|
|
|
harness.get_by_label("Next page").click();
|
|
harness.step();
|
|
assert!(harness.get_all_by_label_contains("Page 2").count() >= 1, "the second page is on screen");
|
|
assert_eq!(pdf::state(&harness.ctx, "doc.pdf").page, 2, "the tab is on the second page");
|
|
assert_eq!(pdf::state(&harness.ctx, "doc.pdf").asked, 2, "the renderer was asked for the second page");
|
|
}
|
|
|
|
#[test]
|
|
fn the_video_tab_offers_the_system_player_when_ffmpeg_is_missing() {
|
|
let session = session_with_video("missing");
|
|
let tokens = tokens();
|
|
let mut harness = egui_kittest::Harness::builder().with_size(egui::Vec2::new(900.0, 700.0)).build_ui_state(
|
|
|ui, state: &mut (bool, Option<String>)| {
|
|
let (warmed, opened) = state;
|
|
if !*warmed {
|
|
// Fonts bind one pass after installing; the first pass warms only.
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
*warmed = true;
|
|
return;
|
|
}
|
|
// The app publishes the state of the player; here it says that `ffmpeg` is missing.
|
|
ui.ctx().data_mut(|data| data.insert_temp(state_id(), idle_state(false)));
|
|
if let Some(path) = video::show(ui, &tokens, &session, "clip.mp4") {
|
|
*opened = Some(path);
|
|
}
|
|
},
|
|
(false, None),
|
|
);
|
|
harness.run_steps(2);
|
|
assert!(harness.get_all_by_label_contains("ffmpeg is not installed").count() >= 1, "the tab says that the file cannot play");
|
|
assert_eq!(harness.get_all_by_label("Open with the system").count(), 1, "one action button");
|
|
|
|
harness.get_by_label("Open with the system").click();
|
|
harness.step();
|
|
let (_warmed, opened) = harness.into_state();
|
|
assert_eq!(opened, Some(String::from("clip.mp4")), "the tab hands the file to the shell");
|
|
}
|
|
|
|
#[test]
|
|
fn the_video_tab_has_its_actions_and_space_asks_the_player_to_toggle() {
|
|
use notescraft_ui_egui::app::video::{VideoCommand, commands_id};
|
|
let session = session_with_video("actions");
|
|
let tokens = tokens();
|
|
let mut harness = egui_kittest::Harness::builder().with_size(egui::Vec2::new(1100.0, 700.0)).build_ui_state(
|
|
|ui, warmed: &mut bool| {
|
|
if !*warmed {
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
*warmed = true;
|
|
return;
|
|
}
|
|
ui.ctx().data_mut(|data| data.insert_temp(state_id(), idle_state(true)));
|
|
video::show(ui, &tokens, &session, "clip.mp4");
|
|
},
|
|
false,
|
|
);
|
|
harness.run_steps(4);
|
|
for label in [
|
|
"Play",
|
|
"Back 10 seconds",
|
|
"Forward 10 seconds",
|
|
"Play from the start",
|
|
"Loop",
|
|
"Mute",
|
|
"Open with the system",
|
|
"Show in the file manager",
|
|
"Copy the path",
|
|
] {
|
|
assert!(harness.get_all_by_label_contains(label).count() >= 1, "the {label} button is there");
|
|
}
|
|
harness.key_press(egui::Key::Space);
|
|
harness.step();
|
|
let queued = harness.ctx.data(|data| data.get_temp::<Vec<VideoCommand>>(commands_id())).unwrap_or_default();
|
|
assert!(matches!(queued.first(), Some(VideoCommand::Play { path, from, .. }) if path == "clip.mp4" && *from == 0.0), "{queued:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn the_pdf_tab_has_its_actions_and_a_go_to_page_field() {
|
|
let session = session_with_pdf("actions");
|
|
let tokens = tokens();
|
|
let mut harness = egui_kittest::Harness::builder().with_size(egui::Vec2::new(1100.0, 700.0)).build_ui_state(
|
|
|ui, warmed: &mut bool| {
|
|
if !*warmed {
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
*warmed = true;
|
|
return;
|
|
}
|
|
pdf::show(ui, &tokens, &session, "doc.pdf");
|
|
},
|
|
false,
|
|
);
|
|
harness.run_steps(3);
|
|
for label in ["Open with the system", "Show in the file manager", "Copy the path", "Fit page", "Fit width", "Previous page"] {
|
|
assert!(harness.get_all_by_label_contains(label).count() >= 1, "the {label} button is there");
|
|
}
|
|
}
|
|
|
|
/// A harness over the video tab of `clip.mp4`, with the player state `state` published each frame.
|
|
fn video_harness(name: &str, state: impl Fn(&egui::Context) -> VideoState + 'static) -> egui_kittest::Harness<'static, bool> {
|
|
let session = session_with_video(name);
|
|
let tokens = tokens();
|
|
egui_kittest::Harness::builder().with_size(egui::Vec2::new(1100.0, 700.0)).build_ui_state(
|
|
move |ui, warmed: &mut bool| {
|
|
if !*warmed {
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
*warmed = true;
|
|
return;
|
|
}
|
|
let published = state(ui.ctx());
|
|
ui.ctx().data_mut(|data| data.insert_temp(state_id(), published));
|
|
video::show(ui, &tokens, &session, "clip.mp4");
|
|
},
|
|
false,
|
|
)
|
|
}
|
|
|
|
/// The state of a video that plays (or is held when `paused`) at `position`.
|
|
fn playing_state(ctx: &egui::Context, paused: bool) -> VideoState {
|
|
let texture = ctx.load_texture("test-frame", egui::ColorImage::example(), egui::TextureOptions::LINEAR);
|
|
VideoState { playing: Some((String::from("clip.mp4"), texture)), position: 12.0, paused, ..idle_state(true) }
|
|
}
|
|
|
|
#[test]
|
|
fn the_main_button_pauses_a_playing_video_and_goes_on_with_a_paused_one() {
|
|
use notescraft_ui_egui::app::video::{VideoCommand, commands_id};
|
|
for (paused, label, expected) in [(false, "Pause", VideoCommand::Pause(true)), (true, "Play", VideoCommand::Pause(false))] {
|
|
let mut harness = video_harness(&format!("pause-{paused}"), move |ctx| playing_state(ctx, paused));
|
|
harness.run_steps(4);
|
|
harness.get_by_label(label).click();
|
|
harness.step();
|
|
let queued = harness.ctx.data(|data| data.get_temp::<Vec<VideoCommand>>(commands_id())).unwrap_or_default();
|
|
assert_eq!(queued.first(), Some(&expected), "paused={paused}: {queued:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_the_seek_track_shows_the_time_under_the_pointer() {
|
|
let mut harness = video_harness("hover", |ctx| playing_state(ctx, false));
|
|
harness.run_steps(4);
|
|
let track = harness.get_by_role(egui::accesskit::Role::Slider).rect();
|
|
assert!(track.height() >= 20.0, "the track is a target of at least 20 points: {track:?}");
|
|
harness.hover_at(track.center());
|
|
harness.run_steps(3);
|
|
// The track is 60 s long and the pointer is in the middle of it.
|
|
assert!(harness.get_all_by_label_contains("0:30").count() >= 1, "the preview names the second under the pointer");
|
|
}
|
|
|
|
/// Renders the card and the hover preview to PNGs under `$NOTESCRAFT_RENDER_DIR`; needs `ffmpeg`
|
|
/// and a GPU, so it only runs when asked for.
|
|
#[test]
|
|
#[ignore = "renders with wgpu and needs ffmpeg"]
|
|
fn render_the_video_card_and_the_hover_preview() {
|
|
let Some(dir) = std::env::var_os("NOTESCRAFT_RENDER_DIR").map(std::path::PathBuf::from) else { return };
|
|
let root = std::env::temp_dir().join(format!("notescraft-media-video-render-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
let _ = std::fs::create_dir_all(&root);
|
|
let made = std::process::Command::new("ffmpeg")
|
|
.args(["-v", "error", "-f", "lavfi", "-i", "testsrc2=size=640x360:rate=24", "-t", "60", "-pix_fmt", "yuv420p"])
|
|
.arg(root.join("clip.mp4"))
|
|
.status();
|
|
assert!(made.is_ok_and(|status| status.success()), "ffmpeg makes the test clip");
|
|
let session = Session::open(&root).expect("a temp vault opens");
|
|
let tokens = tokens();
|
|
let mut harness = egui_kittest::Harness::builder().with_size(egui::Vec2::new(1100.0, 700.0)).wgpu().build_ui_state(
|
|
|ui, warmed: &mut bool| {
|
|
if !*warmed {
|
|
notescraft_ui_egui::fonts::install(ui.ctx());
|
|
egui_extras::install_image_loaders(ui.ctx());
|
|
*warmed = true;
|
|
return;
|
|
}
|
|
let published = playing_state(ui.ctx(), false);
|
|
ui.ctx().data_mut(|data| data.insert_temp(state_id(), published));
|
|
video::show(ui, &tokens, &session, "clip.mp4");
|
|
},
|
|
false,
|
|
);
|
|
harness.run_steps(4);
|
|
let image = harness.render().expect("the card renders");
|
|
image.save(dir.join("video-card.png")).expect("the PNG is saved");
|
|
let track = harness.get_by_role(egui::accesskit::Role::Slider).rect();
|
|
harness.hover_at(track.lerp_inside(egui::vec2(0.4, 0.5)));
|
|
for _ in 0..30 {
|
|
harness.step();
|
|
std::thread::sleep(std::time::Duration::from_millis(60));
|
|
}
|
|
let image = harness.render().expect("the preview renders");
|
|
image.save(dir.join("video-preview.png")).expect("the PNG is saved");
|
|
}
|