PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
test(ui): page modules claim the cursor and never set it
alehel61f92a1d0c93
diff --git a/crates/ui-egui/src/page_pointer.rs b/crates/ui-egui/src/page_pointer.rsindex a6a318c1f..c6baf7090 100644--- a/crates/ui-egui/src/page_pointer.rs+++ b/crates/ui-egui/src/page_pointer.rs@@ -346,22 +346,33 @@ mod tests {rest.find(|c: char| !(c.is_alphanumeric() || c == '_')).map(|end| &rest[..end])}- /// Whether `line` calls `read`, and not a longer name that ends in it.- fn reads(line: &str, read: &str) -> bool {- line.match_indices(read).any(|(at, _)| !line[..at].ends_with(|c: char| c.is_alphanumeric() || c == '_'))+ /// Ways to set egui's cursor.+ const CURSOR_WRITES: [&str; 4] = ["set_cursor_icon(", ".cursor_icon = ", "on_hover_cursor(", "on_hover_and_drag_cursor("];++ /// Functions in the page modules that set a cursor other than the pages': (file, function,+ /// why).+ const NOT_PAGE_CURSOR: [(&str, &str, &str); 3] = [+ ("fill_sign.rs", "scan_tab", "the signature dialog's Scan tab"),+ ("edit_text_ui.rs", "overlay", "the paragraph editor is an Area of its own over the page, drawn after the pages' cursor is set"),+ ("calc_order_ui.rs", "panel", "the Calculation order panel, whose rows are dragged to reorder them"),+ ];++ /// Whether `line` uses `name`, and not a longer name that ends in it (a field, after its+ /// `.`, is never part of a longer name).+ fn uses(line: &str, name: &str) -> bool {+ line.match_indices(name).any(|(at, _)| name.starts_with('.') || !line[..at].ends_with(|c: char| c.is_alphanumeric() || c == '_'))}- /// Page modules read the pointer only through `PagePointer`. egui's raw state misses what is- /// drawn over the pages and where a touch lifted, so a raw read in a page module brings back- /// the bugs `PagePointer` fixed: a cursor or a press through a dialog, a tap that does nothing.- #[test]- fn page_modules_read_the_pointer_only_through_page_pointer() {+ /// Each use of one of `names` in the page modules' code (their tests left out) outside the+ /// functions `allowed` to, as "src/file:line: `name` why"; then each allowed function that+ /// uses none of them any more, as "src/file: fn function unused".+ fn misuses(names: &[&str], allowed: &[(&str, &str, &str)], why: &str, unused: &str) -> Vec<String> {let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");let mut problems = Vec::new();- let mut needed = vec![false; NOT_PAGE_INPUT.len()];+ let mut needed = vec![false; allowed.len()];for file in PAGE_MODULES {let source = std::fs::read_to_string(src.join(file)).expect("page module").replace("\r\n", "\n");- // Tests drive egui's input themselves.+ // Tests drive egui's input and read its output themselves.let code = source.split("#[cfg(test)]\nmod ").next().unwrap_or_default();// The top-level function, or method of an `impl`, that each line is in.let (mut function, mut in_impl) = (None, false);@@ -376,24 +387,36 @@ mod tests {if line.trim_start().starts_with("//") {continue;}- for read in RAW_READS.into_iter().filter(|read| reads(line, read)) {- match NOT_PAGE_INPUT.iter().position(|(f, name, _)| *f == file && function == Some(*name)) {+ for name in names.iter().filter(|name| uses(line, name)) {+ match allowed.iter().position(|(f, function_name, _)| *f == file && function == Some(*function_name)) {Some(k) => needed[k] = true,- None => problems.push(format!(- "src/{file}:{}: `{read}` reads egui's pointer state in a page module. Read `PagePointer` \- (src/page_pointer.rs) instead: it knows what is drawn over the pages and where a touch \- lifted. Not page input? Add the function to NOT_PAGE_INPUT there, with why.",- n + 1- )),+ None => problems.push(format!("src/{file}:{}: `{name}` {why}", n + 1)),}}}}- for ((file, name, _), needed) in NOT_PAGE_INPUT.iter().zip(needed) {+ for ((file, name, _), needed) in allowed.iter().zip(needed) {if !needed {- problems.push(format!("src/{file}: fn {name} reads no pointer state any more: remove it from NOT_PAGE_INPUT."));+ problems.push(format!("src/{file}: fn {name} {unused}"));}}+ problems+ }++ /// Page modules read the pointer only through `PagePointer`. egui's raw state misses what is+ /// drawn over the pages and where a touch lifted, so a raw read in a page module brings back+ /// the bugs `PagePointer` fixed: a cursor or a press through a dialog, a tap that does nothing.+ #[test]+ fn page_modules_read_the_pointer_only_through_page_pointer() {+ let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");+ let mut problems = misuses(+ &RAW_READS,+ &NOT_PAGE_INPUT,+ "reads egui's pointer state in a page module. Read `PagePointer` (src/page_pointer.rs) instead: it knows \+ what is drawn over the pages and where a touch lifted. Not page input? Add the function to NOT_PAGE_INPUT \+ there, with why.",+ "reads no pointer state any more: remove it from NOT_PAGE_INPUT.",+ );// A module that takes the page pointer is a page module, so it is checked too.let mut dirs = vec![src.clone()];while let Some(dir) = dirs.pop() {@@ -416,4 +439,21 @@ mod tests {}assert!(problems.is_empty(), "{}", problems.join("\n"));}++ /// Page modules claim the cursor and never set it. Where each set it, the last write of a+ /// frame won, so the order of the per-page chain decided the cursor and not what a press+ /// does: a link's hand over every tool, the I-beam over a comment that a drag moves.+ #[test]+ fn page_modules_claim_the_cursor_and_never_set_it() {+ let problems = misuses(+ &CURSOR_WRITES,+ &NOT_PAGE_CURSOR,+ "sets the cursor in a page module, where the last write of a frame would win. Claim it instead, on the \+ layer of what is under the pointer (`view.cursor.claim`, src/page_cursor.rs): `document_area` sets it \+ once, from the claim on the highest layer the active tool reaches. Not the pages' cursor? Add the \+ function to NOT_PAGE_CURSOR in src/page_pointer.rs, with why.",+ "sets no cursor any more: remove it from NOT_PAGE_CURSOR.",+ );+ assert!(problems.is_empty(), "{}", problems.join("\n"));+ }}