app red 5 (g2) (swarm/red5); committed by the lead

This commit is contained in:
Dylan M. Taylor
2026-11-18 23:06:26 -05:00
parent a28496de88
commit 42eaf1fce4
10 changed files with 113 additions and 14 deletions
+13 -1
View File
@@ -273,8 +273,20 @@ mod repeated_file_tests {
let mut app = test_app();
app.set_option(&egui::Context::default(), "default-layout", "continuous").unwrap();
app.open_path(&name);
// An open lands on a frame, as the files named at launch do in the window.
let mut h = egui_kittest::Harness::builder().build_eframe(move |cc| {
crate::tests::install_runtime(&cc.egui_ctx);
app
});
for _ in 0..20 {
if !h.state().views.is_empty() {
break;
}
h.run_steps(1);
}
let app = h.state_mut();
let original = app.views[0].id;
open_files(&mut app, &[name.clone(), name], true);
open_files(app, &[name.clone(), name], true);
assert_eq!(app.views.len(), 1);
assert_eq!(app.views[0].id, original);
assert_eq!(app.views[0].view.display, PageDisplay::TwoUp);
+1 -1
View File
@@ -13,7 +13,7 @@ fn gpu() -> std::sync::MutexGuard<'static, ()> {
/// Installs the framework runtime on a harness's context, as the app's window does, with a system
/// that opens no OS windows and the interface fonts of English.
fn install_runtime(ctx: &egui::Context) {
pub(crate) fn install_runtime(ctx: &egui::Context) {
use pdfcraft_ui_egui::i18n::Lang;
let spec = craft::ui::InstallSpec::new(craft::host::target::ShellInfo::default(), std::sync::Arc::new(craft::window::system::NoWindows));
craft::ui::shell::embed::install(ctx, spec, &pdfcraft_ui_egui::fonts::ui_fonts(Lang::EN)).expect("the runtime installs on a fresh context");
+5
View File
@@ -1027,6 +1027,11 @@ fn page_body(ui: &mut egui::Ui, body: &PageBody<'_>, session: &mut PageSession,
Some((Owner::Comment { index }, page)) => Some((page, index)),
_ => None,
};
// The pointer entering or leaving a form widget runs its Mouse Enter and Exit scripts, once per real run.
if body.pass.is_some() {
let widget = crate::forms_ui::hovered(cx.form, parts_out.hovered.as_ref().map(|(owner, _)| owner));
crate::forms_ui::hover_moved(session, widget);
}
if let Some(press) = &parts_out.press {
// Any press on a page ends Select All of the document, on a link too.
if matches!(press.phase, PressPhase::Down { .. }) {
+30 -1
View File
@@ -243,6 +243,28 @@ fn select_tool<'a>(
PageSelectTool::new(cx.pages, texts, text, cx.info, PickSlots::new(pending_pick, picked_image), xforms, cx.doc.edit_generation())
}
/// The Insert text tool's snap on `page`, the page the comments tool works on: the caret's tip and side in the page's
/// displayed units, from the page's text ([`comments::caret_snap`]). The caret is drawn upright, so it snaps only
/// where up on the page is up on screen; other tools have no snap.
fn caret_snap(
tool: QuickTool,
cx: &PageToolCx<'_>,
texts: &std::collections::HashMap<usize, std::sync::Arc<pdfcraft_render::PageText>>,
page: usize,
) -> Option<impl Fn(craft::behavior::kurbo::Point) -> Option<(craft::behavior::kurbo::Point, f64)> + Send + 'static> {
if tool != QuickTool::Comment(comments::CommentTool::Caret) || cx.rotation % 360 != 0 {
return None;
}
if !cx.info.pages.get(page)?.rotation.is_multiple_of(360) {
return None;
}
let text = texts.get(&page)?.clone();
Some(move |at: craft::behavior::kurbo::Point| {
let ([x, y], side) = comments::caret_snap(&text, at.x as f32, at.y as f32)?;
Some((craft::behavior::kurbo::Point::new(f64::from(x), f64::from(y)), f64::from(side)))
})
}
/// The page tool for `chosen` this pass. The placing rule applies first ([`seen_tool`]); Fill & Sign leaves its
/// click's outcome in `fill`, which the caller applies after the run with [`ToolFinish::apply`]. `press` is the canvas
/// background's press of the pass, whose span the area tools read.
@@ -286,9 +308,16 @@ pub(crate) fn page_tool<'a>(
// Select runs the comments tool too: a hanging paste is placed by its press, and a press no comment
// part took goes on to the text.
_ => {
let snap = caret_snap(tool, &cx, &session.texts, session.comments.tool.page);
let view = &mut session.comments;
let map = comments::PageMap::new(cx.info, cx.pages, cx.rotation);
let annotate = CommentsTool::new(&mut view.tool, &mut view.paste, map, &mut view.tool_done);
let annotate = CommentsTool::new(&mut view.tool, &mut view.paste, map, &mut view.tool_done)
.settings(tool, cx.comment_prefs, cx.allowed, cx.doc.comments_hidden())
.outcome(&mut session.pending_edit, &mut view.notice);
let annotate = match snap {
Some(snap) => annotate.caret_snap(snap),
None => annotate,
};
let annotate = if selects_text { annotate.text(select) } else { annotate };
Arm::Area(Box::new(annotate))
}
+36 -2
View File
@@ -204,6 +204,38 @@ fn edge_of(workspace: &Workspace<PanelId>, panel: PanelId) -> Option<DockEdge> {
None
}
/// The columns of the dock at `edge`, from the window edge inward; empty when there is no such dock.
fn dock_columns(workspace: &Workspace<PanelId>, edge: DockEdge) -> &[Column<PanelId>] {
let mut pending: Vec<(&Node<PanelId>, usize)> = workspace.layout.root.iter().map(|node| (node, 0)).collect();
while let Some((node, depth)) = pending.pop() {
if depth > MAX_DEPTH {
continue;
}
match node {
Node::Split { first, second, .. } => {
pending.push((&**first, depth + 1));
pending.push((&**second, depth + 1));
}
Node::Dock { side, columns, .. } if *side == edge => return columns,
_ => {}
}
}
&[]
}
/// Where a column moved to the `edge` dock goes: a dock holding columns takes it as a new column of its own (a new
/// dock is refused there), inward of the rail as in the default arrangement, else at the window edge.
fn edge_target(workspace: &Workspace<PanelId>, edge: DockEdge) -> Target<PanelId> {
let columns = dock_columns(workspace, edge);
if columns.iter().any(|column| column.panels().any(|p| *p == PanelId::Toolbar)) {
return Target::at(PanelId::Toolbar, DockPlacement::column(true));
}
match columns.first().and_then(|column| column.panels().next()) {
Some(outer) => Target::at(*outer, DockPlacement::column(false)),
None => Target::new_dock(edge),
}
}
fn edge_name(edge: Option<DockEdge>) -> &'static str {
if edge == Some(DockEdge::End) { "right" } else { "left" }
}
@@ -464,7 +496,8 @@ fn move_to_edge(app: &mut PdfCraftApp, panel: PanelId, edge: DockEdge) -> Result
if edge_of(&app.dock, panel) == Some(edge) {
return Ok(());
}
app.dock.apply(Action::move_column(panel, Target::new_dock(edge)), false, permissions).map(drop).map_err(|_| refused())
let to = edge_target(&app.dock, edge);
app.dock.apply(Action::move_column(panel, to), false, permissions).map(drop).map_err(|_| refused())
}
/// Puts the rail, and the side panels that share its edge, on `side`.
@@ -721,7 +754,8 @@ fn build(old: &Old, right: Option<RightPanel>, widths: bool) -> Option<Workspace
let mut workspace = default_workspace(old.rail);
let apply = |workspace: &mut Workspace<PanelId>, action| workspace.apply(action, false, permissions).ok();
if old.tools_right {
let moved = workspace.apply(Action::move_column(PanelId::Tools, Target::new_dock(DockEdge::End)), false, permissions);
let to = edge_target(&workspace, DockEdge::End);
let moved = workspace.apply(Action::move_column(PanelId::Tools, to), false, permissions);
moved.ok()?;
}
let side = right.filter(|_| old.side_open).map(PanelId::from);
+2 -1
View File
@@ -44,9 +44,10 @@ pub(crate) use colours::{RecentColours, border_choice, fill_choice, palette, to_
pub(crate) use composer::composer;
#[cfg(test)]
use composer::edited_in_view;
pub(crate) use geometry::caret_snap;
pub(crate) use geometry::page_rotation;
#[cfg(test)]
pub(crate) use geometry::{caret_snap, snapped_caret};
pub(crate) use geometry::snapped_caret;
pub use geometry::{drawn_text_box_rect, snug_text_box_rect, text_box_rect};
pub use menu::CanvasAction;
pub(crate) use menu::{CommentMenuCx, MenuRights, QuickBarPick, attachment_items, context_rows, quick_bar_controls};
+12 -4
View File
@@ -1971,7 +1971,12 @@ mod tests {
let host = craft::host::Host::builder(crate::APP_INFO).files(files.clone()).build();
let mut app = PdfCraftApp::for_unit_tests_on(Arc::new(host));
let text = app.session.create_from_text("t", "Recognize these scanned words").expect("fixture");
let id = app.session.open("t.pdf", None, text, None).expect("fixture");
// The source is a file on disk, which the destination is compared with.
let dir = std::env::temp_dir().join(format!("pdfcraft-ocr-source-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("temp folder");
let source = dir.join("t.pdf");
std::fs::write(&source, text.as_slice()).expect("source on disk");
let id = app.session.open("t.pdf", Some(source.to_string_lossy().into_owned()), text, None).expect("fixture");
let job = app.session.ocr_job(id, &[0], OcrSettings::default()).expect("fixture");
app.ocr_verify.load(
id,
@@ -1982,13 +1987,15 @@ mod tests {
app.ocr_draft.output_mode = OutputMode::EditableText;
// The source file as the destination is refused: nothing is written, the review stays
// in ReviewReady with the edit, Accept is enabled again.
files.answer(Picked::Paths(vec![PathBuf::from("t.pdf")]));
use craft::host::Files as _;
// Each answer is queued for the save dialog the next Accept opens.
files.answer_next(Picked::Paths(vec![source.clone()])).expect("queued");
with_state(&mut app, |v, app| do_accept(v, app, &egui::Context::default()));
app.process_picks(&egui::Context::default());
assert_eq!(app.ocr_verify.phase, Phase::ReviewReady, "a refused destination keeps the review");
assert_eq!(app.ocr_verify.text_of((0, 0)), Some("Edited"), "the edits are kept");
// A cancelled save dialog: nothing is written, nothing changes.
files.answer(Picked::Cancelled);
files.answer_next(Picked::Cancelled).expect("queued");
with_state(&mut app, |v, app| do_accept(v, app, &egui::Context::default()));
app.process_picks(&egui::Context::default());
assert_eq!(app.ocr_verify.phase, Phase::ReviewReady, "a cancelled save keeps the review");
@@ -1996,11 +2003,12 @@ mod tests {
assert_eq!(app.ocr_verify.doc, Some(id), "the review still belongs to its document");
// A real destination: the file lands under `<name>_ocr.pdf` and the review ends.
let target = PathBuf::from("out/t_ocr.pdf");
files.answer(Picked::Paths(vec![target.clone()]));
files.answer_next(Picked::Paths(vec![target.clone()])).expect("queued");
with_state(&mut app, |v, app| do_accept(v, app, &egui::Context::default()));
app.process_picks(&egui::Context::default());
assert!(files.file(&target).is_some(), "the editable copy is written");
assert_eq!(app.ocr_verify.phase, Phase::Idle, "a successful save ends the review");
let _ = std::fs::remove_dir_all(dir);
}
/// Region re-OCR refuses an empty, inverted or out-of-page rectangle BEFORE running: no
+7 -1
View File
@@ -47,6 +47,12 @@ fn has_field(h: &Harness<'static, PdfCraftApp>, placeholder: &str) -> bool {
h.query_all_by(|n| n.placeholder() == Some(placeholder)).next().is_some()
}
/// The find bar's Replace All button label, as the framework words it.
fn replace_all() -> String {
use craft::i18n::WordSet as _;
craft::text::ui::find::FindLabels::english().replace_all.as_str().to_owned()
}
/// The find bar's Replace with field: a text field named by its label, with no placeholder.
const REPLACE_WITH: &str = "Replace with:";
@@ -95,7 +101,7 @@ fn replace_all_from_the_find_bar_is_one_undo_step() {
assert_eq!(h.state().views[0].find.as_ref().map(|f| (f.query.as_str(), f.replacement.as_str())), Some(("cat", "dog")));
// The row may settle over a few frames once the status text changes its width.
h.run_steps(8);
h.get_by_label("Replace all").click();
h.get_by_label(&replace_all()).click();
h.run_steps(4);
assert_eq!(texts(&h), ["dog and dog", "a dog"]);
assert!(support::shown_notice(&mut h).is_some(), "Replace all says what it did");
+3 -1
View File
@@ -1379,7 +1379,9 @@ mod revert_tests {
pdfcraft_engine::Edit::SetFieldValue { name: "name".into(), value: pdfcraft_engine::FieldValue::Text("Saved original".into()) }
));
let ctx_h = h.ctx.clone();
assert!(h.state_mut().save_active(&ctx_h, pdfcraft_ui_egui::SaveTarget::InPlace));
// A document with no file asks where to save: the scripted answer arrives on a later pass.
h.state_mut().save_active(&ctx_h, pdfcraft_ui_egui::SaveTarget::InPlace);
crate::support::wait_until(&mut h, "the save", |s| s.views.first().and_then(|v| s.session.get(v.id)).is_some_and(|d| !d.dirty));
let saved = std::fs::read(&path).unwrap();
let ctx_h = h.ctx.clone();
assert!(h.state_mut().apply_edit(
+4 -2
View File
@@ -26,10 +26,12 @@ fn harness() -> Harness<'static, PdfCraftApp> {
h
}
/// Types into the masked field named `label` (its field row carries the same name).
fn type_into(h: &mut Harness<'static, PdfCraftApp>, label: &str, text: &str) {
h.get_by_label(label).click();
use egui::accesskit::Role;
h.get_by(|n| n.label().as_deref() == Some(label) && matches!(n.role(), Role::PasswordInput | Role::TextInput)).click();
h.run_steps(2);
h.get_by_label(label).type_text(text);
h.get_by(|n| n.label().as_deref() == Some(label) && matches!(n.role(), Role::PasswordInput | Role::TextInput)).type_text(text);
h.run_steps(2);
}