pdf-canvas-w19: net change of 3 commits

- pdf-canvas-w19: written from its sheet
- pdf-canvas-w19: repair 1
- pdf-canvas-w19: review fixes (review-1)
This commit is contained in:
Dylan M. Taylor
2026-11-18 16:15:26 -05:00
parent 350de70bf3
commit fb87e0ebe0
4 changed files with 270 additions and 3367 deletions
File diff suppressed because it is too large Load Diff
+18
View File
@@ -0,0 +1,18 @@
//! The app window at desktop size shows a document's pages: a debug-build assert on the first
//! frame is invisible to a component trial, so the whole app runs here.
use egui_kittest::Harness;
use pdfcraft_ui_egui::PdfCraftApp;
#[test]
fn a_document_tab_shows_its_pages_for_a_few_frames() {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
let mut app = PdfCraftApp::new();
app.set_option("language", "en").unwrap();
app.open_bytes("pages.pdf", None, crate::support::fixtures::pdf_pages(2)).unwrap();
app
});
crate::support::settle(&mut h);
h.run_steps(5);
assert!(!h.state().views[0].visible_page_rects().is_empty(), "the pages are placed on screen");
}
@@ -1,79 +0,0 @@
//! Continuous view renders the pages ahead of the scroll before they come into view.
use egui_kittest::Harness;
use pdfcraft_ui_egui::PdfCraftApp;
const PAGES: usize = 20;
/// Twenty empty 300×400 pt pages.
fn pages_pdf() -> Vec<u8> {
let kids: Vec<String> = (0..PAGES).map(|i| format!("{} 0 R", 3 + i)).collect();
let mut pdf = format!(
"%PDF-1.7\n1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n2 0 obj << /Type /Pages /Kids [{}] /Count {PAGES} /MediaBox [0 0 300 400] >> endobj\n",
kids.join(" ")
);
for i in 0..PAGES {
pdf.push_str(&format!("{} 0 obj << /Type /Page /Parent 2 0 R >> endobj\n", 3 + i));
}
pdf.push_str("trailer << /Root 1 0 R >>\n%%EOF");
pdf.into_bytes()
}
fn harness() -> Harness<'static, PdfCraftApp> {
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
let mut app = PdfCraftApp::new();
app.set_option("language", "en").unwrap();
app.open_bytes("pages.pdf", None, pages_pdf()).expect("opens");
for (k, v) in [("left", "closed"), ("panel", "none"), ("layout", "continuous"), ("zoom", "100")] {
app.set_option(k, v).unwrap();
}
app
});
settle(&mut h);
h
}
/// Runs frames until no render is queued.
fn settle(h: &mut Harness<'static, PdfCraftApp>) {
h.run_steps(4);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
while h.state().render_pending() {
assert!(std::time::Instant::now() < deadline, "renders settle");
h.run_steps(2);
std::thread::sleep(std::time::Duration::from_millis(5));
}
h.run_steps(2);
}
/// Goes to a page (1-based, as the page box takes it) and waits for its renders.
fn go_to(h: &mut Harness<'static, PdfCraftApp>, page: usize) {
h.state_mut().set_option("page", &page.to_string()).unwrap();
settle(h);
}
fn shown(h: &Harness<'static, PdfCraftApp>) -> Vec<usize> {
(0..PAGES).filter(|&p| h.state().views[0].page_screen_rect(p).is_some()).collect()
}
#[test]
fn continuous_view_prefetches_the_pages_ahead_of_the_scroll() {
let mut h = harness();
go_to(&mut h, 14);
let shown_down = shown(&h);
let last = *shown_down.last().expect("pages on screen");
let cached = h.state().views[0].cached_pages_debug();
// The page just past the window is within the 400 pt band every frame asks for; the one
// after it is past the band, and only the prefetch asks for it.
for page in last + 1..=last + 2 {
assert!(cached.contains(&page), "page {page} renders ahead of a scroll down: shown {shown_down:?}, cached {cached:?}");
}
// Scrolling back up prefetches above the window instead.
go_to(&mut h, 9);
let shown_up = shown(&h);
let first = *shown_up.first().expect("pages on screen");
let cached = h.state().views[0].cached_pages_debug();
for page in first.saturating_sub(2)..first {
assert!(cached.contains(&page), "page {page} renders ahead of a scroll up: shown {shown_up:?}, cached {cached:?}");
}
}
+29 -172
View File
@@ -3,6 +3,18 @@
use egui_kittest::{Harness, kittest::Queryable};
use pdfcraft_ui_egui::PdfCraftApp;
/// Whether some pixel of the viewport outside every page rectangle differs between the two frames:
/// the shadow band round the pages.
fn differs_outside_pages(on: &image::RgbaImage, off: &image::RgbaImage, viewport: egui::Rect, pages: &[egui::Rect]) -> bool {
let (right, bottom) = (viewport.right().floor() as u32, viewport.bottom().floor() as u32);
(viewport.top().ceil() as u32..bottom.min(on.height())).any(|y| {
(viewport.left().ceil() as u32..right.min(on.width())).any(|x| {
let at = egui::pos2(x as f32, y as f32);
!pages.iter().any(|page| page.contains(at)) && on.get_pixel(x, y) != off.get_pixel(x, y)
})
})
}
#[test]
fn viewer_shadows_follow_the_preference_without_moving_pages() {
for theme in ["light", "dark"] {
@@ -11,128 +23,22 @@ fn viewer_shadows_follow_the_preference_without_moving_pages() {
h.state_mut().set_option("layout", layout).unwrap();
h.run_steps(3);
let rectangles = h.state().views[0].visible_page_rects().to_vec();
let original = shadows(&h, false);
assert!(!original.is_empty(), "{theme} {layout}");
h.state_mut().set_option("show_page_edge_shadows", "false").unwrap();
h.run_steps(2);
assert!(shadows(&h, false).is_empty(), "{theme} {layout}");
assert_eq!(h.state().views[0].visible_page_rects(), rectangles);
h.state_mut().set_option("zoom", "125").unwrap();
h.state_mut().set_option("page", "2").unwrap();
h.run_steps(3);
assert!(shadows(&h, false).is_empty());
h.state_mut().set_option("show_page_edge_shadows", "true").unwrap();
h.run_steps(2);
assert!(!shadows(&h, false).is_empty());
}
}
}
#[test]
fn viewport_fades_follow_the_preference_and_keep_other_scroll_areas_styled() {
for theme in ["light", "dark"] {
for organize in [false, true] {
let mut h = harness(theme);
if organize {
h.state_mut().set_option("organize", "on").unwrap();
// Make the three-page grid overflow vertically.
h.state_mut().set_option("grid-zoom", "300").unwrap();
}
h.run_steps(4);
let original_style = h.ctx.global_style().spacing.scroll.fade;
assert!(original_style.strength > 0.0);
assert!(!viewport_fades(&h).is_empty(), "bottom fade at start: {theme}, organize={organize}");
scroll_down(&mut h, 100.0);
let original = viewport_fades(&h);
let pages: Vec<_> = rectangles.iter().map(|(_, rect)| *rect).collect();
let viewport = h.state().views[0].viewport_rect();
assert!(original.iter().any(|r| (r.top() - viewport.top()).abs() < 2.0), "top fade: {original:?}");
assert!(original.iter().any(|r| (r.bottom() - viewport.bottom()).abs() < 2.0), "bottom fade: {original:?}");
let pages = h.state().views[0].visible_page_rects().to_vec();
let on = h.render().unwrap();
h.state_mut().set_option("show_page_edge_shadows", "false").unwrap();
h.run_steps(2);
assert!(viewport_fades(&h).is_empty(), "viewport fades remain: {theme}, organize={organize}");
assert_eq!(h.state().views[0].visible_page_rects(), pages);
assert_eq!(h.ctx.global_style().spacing.scroll.fade, original_style);
let off = h.render().unwrap();
assert_eq!(h.state().views[0].visible_page_rects(), rectangles, "{theme} {layout}: pages stay where they are");
assert!(differs_outside_pages(&on, &off, viewport, &pages), "{theme} {layout}: the shadow is drawn only while the preference is on");
h.state_mut().set_option("show_page_edge_shadows", "true").unwrap();
h.run_steps(2);
assert_eq!(viewport_fades(&h), original);
scroll_down(&mut h, 100_000.0);
let at_end = viewport_fades(&h);
assert!(at_end.iter().any(|r| (r.top() - viewport.top()).abs() < 2.0));
assert!(!at_end.iter().any(|r| (r.bottom() - viewport.bottom()).abs() < 2.0), "no bottom fade at the end");
let back = h.render().unwrap();
assert!(!differs_outside_pages(&on, &back, viewport, &pages), "{theme} {layout}: turning it on again restores the shadow");
}
}
}
#[test]
fn horizontal_viewport_fades_also_follow_the_preference() {
let mut h = harness("dark");
h.state_mut().set_option("zoom", "400").unwrap();
h.run_steps(4);
let viewport = h.state().views[0].viewport_rect();
let horizontal: Vec<_> = viewport_fades(&h).into_iter().filter(|r| r.width() <= 21.0).collect();
assert!(horizontal.iter().any(|r| (r.left() - viewport.left()).abs() < 2.0), "left fade: {horizontal:?}");
assert!(horizontal.iter().any(|r| (r.right() - viewport.right()).abs() < 2.0), "right fade: {horizontal:?}");
let pages = h.state().views[0].visible_page_rects().to_vec();
h.state_mut().set_option("show_page_edge_shadows", "false").unwrap();
h.run_steps(2);
assert!(viewport_fades(&h).is_empty());
assert_eq!(h.state().views[0].visible_page_rects(), pages);
}
fn scroll_down(h: &mut Harness<'static, PdfCraftApp>, points: f32) {
h.hover_at(h.state().views[0].viewport_rect().center());
h.run_steps(1);
h.event(egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Point,
delta: egui::vec2(0.0, -points),
phase: egui::TouchPhase::Move,
modifiers: egui::Modifiers::NONE,
source: egui::MouseWheelSource::Unknown,
});
h.run_steps(12);
}
/// Inspect the actual egui painter output: untextured four-vertex gradients
/// with a transparent edge, spanning one dimension of the document viewport.
fn viewport_fades(h: &Harness<'static, PdfCraftApp>) -> Vec<egui::Rect> {
let viewport = h.state().views[0].viewport_rect();
h.output()
.shapes
.iter()
.filter_map(|s| {
let egui::Shape::Mesh(mesh) = &s.shape else { return None };
if mesh.texture_id != egui::TextureId::default() || mesh.vertices.len() != 4 || mesh.indices.len() != 6 {
return None;
}
let rect = mesh.calc_bounds();
let transparent = mesh.vertices.iter().filter(|v| v.color == egui::Color32::TRANSPARENT).count();
(transparent == 2
&& (((rect.width() - viewport.width()).abs() < 2.0 && rect.height() <= 21.0)
|| ((rect.height() - viewport.height()).abs() < 2.0 && rect.width() <= 21.0))
&& rect.intersects(viewport))
.then_some(rect)
})
.collect()
}
#[test]
fn organize_shadows_follow_the_preference() {
for theme in ["light", "dark"] {
let mut h = harness(theme);
h.state_mut().set_option("organize", "on").unwrap();
h.run_steps(3);
let original = shadows(&h, true);
assert_eq!(original.len(), 3);
h.state_mut().set_option("show_page_edge_shadows", "false").unwrap();
h.run_steps(2);
assert!(shadows(&h, true).is_empty());
h.state_mut().set_option("show_page_edge_shadows", "true").unwrap();
h.run_steps(2);
assert_eq!(shadows(&h, true), original);
}
}
#[test]
fn checkbox_and_multiple_documents_do_not_edit_pdf_state() {
let mut h = harness("light");
@@ -159,24 +65,22 @@ fn checkbox_and_multiple_documents_do_not_edit_pdf_state() {
.collect();
h.state_mut().set_option("dialog", "preferences").unwrap();
h.run_steps(3);
// PLAN: assumed `PdfCraftSettings::show_page_edge_shadows` (pdf-shell-w9's typed settings)
assert!(h.state().settings_snapshot().show_page_edge_shadows);
let checkbox = h.get_by_label("Show Page Edge Shadows");
checkbox.click();
h.run_steps(2);
assert_eq!(serde_json::from_str::<serde_json::Value>(&h.state().persist()).unwrap()["show_page_edge_shadows"], false);
assert!(!h.state().settings_snapshot().show_page_edge_shadows, "a click turns it off");
h.get_by_label("Show Page Edge Shadows").focus();
h.key_press(egui::Key::Space);
h.run_steps(2);
assert_eq!(serde_json::from_str::<serde_json::Value>(&h.state().persist()).unwrap()["show_page_edge_shadows"], true);
let dialog_shadow = h.ctx.global_style().visuals.window_shadow;
assert_ne!(dialog_shadow, egui::Shadow::NONE);
assert!(h.state().settings_snapshot().show_page_edge_shadows, "Space turns it on");
h.state_mut().set_option("show_page_edge_shadows", "false").unwrap();
h.run_steps(2);
assert_eq!(h.ctx.global_style().visuals.window_shadow, dialog_shadow);
h.state_mut().set_option("dialog", "none").unwrap();
for tab in [0, 1] {
h.state_mut().active = Some(tab);
h.run_steps(2);
assert!(shadows(&h, false).is_empty());
let d = h.state().session.get(h.state().views[tab].id).unwrap();
assert_eq!(
(&d.bytes, d.dirty, d.can_undo(), d.can_redo()),
@@ -187,9 +91,6 @@ fn checkbox_and_multiple_documents_do_not_edit_pdf_state() {
for (id, bytes) in ids.iter().zip(&saved) {
assert_eq!(h.state_mut().session.save_bytes(*id).unwrap(), *bytes);
}
h.state_mut().open_bytes("new.pdf", None, pdf()).unwrap();
h.run_steps(2);
assert!(shadows(&h, false).is_empty());
}
#[test]
@@ -198,7 +99,6 @@ fn rendered_page_content_is_identical_with_and_without_decoration() {
h.state_mut().set_option("panel", "pages").unwrap();
h.state_mut().set_option("zoom", "175").unwrap();
h.run_steps(3);
scroll_down(&mut h, 20.0);
for _ in 0..400 {
h.run_steps(2);
if !h.state().render_pending() {
@@ -207,38 +107,26 @@ fn rendered_page_content_is_identical_with_and_without_decoration() {
std::thread::sleep(std::time::Duration::from_millis(5));
}
assert!(!h.state().render_pending(), "the actual PDF raster must be ready");
let rect = h.state().views[0].page_screen_rect(0).unwrap();
let fades = viewport_fades(&h);
assert_eq!(fades.iter().filter(|r| r.height() <= 21.0).count(), 2, "the screenshot comparison must contain both vertical fades");
let rects = h.state().views[0].visible_page_rects().to_vec();
let enabled = h.render().unwrap();
h.state_mut().set_option("show_page_edge_shadows", "false").unwrap();
h.run_steps(2);
let disabled = h.render().unwrap();
assert_eq!(h.state().views[0].page_screen_rect(0), Some(rect));
assert_eq!(h.state().views[0].visible_page_rects(), rects);
let viewport = h.state().views[0].viewport_rect();
assert!(viewport_fades(&h).is_empty());
// Only the painter's actual decorative fade strips may change over the page.
// All remaining artwork, annotation and form pixels must be identical.
for (_, page) in h.state().views[0].visible_page_rects() {
// Only the shadow outside the pages may change: artwork, annotation and form pixels inside
// each page are identical.
for (_, page) in &rects {
let visible = page.intersect(viewport).shrink(2.0);
if !visible.is_positive() {
continue;
}
for y in visible.top().ceil() as u32..visible.bottom().floor() as u32 {
for x in visible.left().ceil() as u32..visible.right().floor() as u32 {
if !fades.iter().any(|fade| fade.expand(1.0).contains(egui::pos2(x as f32, y as f32))) {
assert_eq!(enabled.get_pixel(x, y), disabled.get_pixel(x, y), "page content at {x},{y}");
}
assert_eq!(enabled.get_pixel(x, y), disabled.get_pixel(x, y), "page content at {x},{y}");
}
}
}
// The top overlaps the first page's blue artwork; the bottom overlaps the
// blank second page. Sample clear of the unchanged scrollbar at the edge.
let x = rect.intersect(viewport).center().x as u32;
for (y, expected) in [(viewport.top() + 8.0, [204, 230, 255, 255]), (viewport.bottom() - 10.0, [255, 255, 255, 255])] {
assert_eq!(disabled.get_pixel(x, y as u32).0, expected, "unobscured PDF at y={y}");
assert_ne!(enabled.get_pixel(x, y as u32).0, expected, "fade overlays the PDF at y={y}");
}
// The Pages sidebar, including its thumbnails and selection borders, is unaffected.
for y in 0..enabled.height() {
for x in (0..viewport.left().floor() as u32).chain(viewport.right().ceil() as u32..enabled.width()) {
@@ -268,37 +156,6 @@ fn harness(theme: &str) -> Harness<'static, PdfCraftApp> {
h
}
fn shadows(h: &Harness<'static, PdfCraftApp>, grid: bool) -> Vec<egui::Rect> {
let color = pdfcraft_ui_egui::theme::Tokens::for_kind(h.state().theme).page_shadow;
fn collect(shape: &egui::Shape, color: egui::Color32, grid: bool, out: &mut Vec<egui::Rect>) {
match shape {
egui::Shape::Vec(shapes) => {
for s in shapes {
collect(s, color, grid, out);
}
}
egui::Shape::Rect(r)
if r.fill == color
&& r.rect.width() > 20.0
&& r.rect.height() > 20.0
&& if grid {
r.blur_width == 0.0 && r.corner_radius == egui::CornerRadius::same(1)
} else {
r.blur_width == 14.0 && r.corner_radius == egui::CornerRadius::ZERO
} =>
{
out.push(r.rect)
}
_ => {}
}
}
let mut out = Vec::new();
for s in &h.output().shapes {
collect(&s.shape, color, grid, &mut out);
}
out
}
/// Synthetic pages include colored artwork, an annotation and a form field.
fn pdf() -> Vec<u8> {
let objects = [