Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Group UI interaction and rendering parameters

Dylan M. Taylor31833c992d07
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index ee556ba4a..ea37eb0df 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1071,6 +1071,18 @@ struct PageInput {
area_tool: bool,
}
+/// Common geometry and pointer state for a tool acting on one page.
+#[derive(Clone, Copy)]
+pub(crate) struct PageToolContext<'a> {
+ pub ui: &'a egui::Ui,
+ pub resp: &'a egui::Response,
+ pub xf: &'a PageXform,
+ pub ptr: &'a PagePointer,
+ pub page: usize,
+ pub doc: &'a pdfcraft_engine::Document,
+ pub info: &'a DocInfo,
+}
+
/// Comments: tools, selection, moving and resizing come before text selection. Form fields take
/// clicks first with the Select tool (as Acrobat fills fields in every viewing mode); then
/// comments; then text selection.
@@ -1105,6 +1117,7 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
} = frame.env;
let PageSlot { i, xf, .. } = *slot;
let PageFrame { resp, ptr, pointer, .. } = *frame;
+ let cx = PageToolContext { ui, resp, xf: &xf, ptr, page: i, doc, info };
let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
if let QuickTool::Measure(measure_tool) = tool {
crate::measure_ui::page_input(ui, resp, doc, view, &pcx, measure_tool, units);
@@ -1162,20 +1175,17 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
&& !comments::fill_grabs(&pcx, view)
{
match crate::fill_sign::page_input(
- ui,
- resp,
- &xf,
- ptr,
- i,
- info,
- ft,
- fill_text_size,
+ cx,
+ crate::fill_sign::PageInput {
+ tool: ft,
+ text_size: fill_text_size,
+ signature: signature.as_ref(),
+ initials: initials.as_ref(),
+ preview: frame.signature_preview,
+ author,
+ date_text,
+ },
view,
- signature.as_ref(),
- initials.as_ref(),
- frame.signature_preview,
- author,
- date_text,
) {
Some(crate::fill_sign::FillAction::Edit(e)) => view.pending_edit = Some(*e),
Some(crate::fill_sign::FillAction::Signature(e)) => {
@@ -1197,7 +1207,9 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
if tool == QuickTool::EnhanceCamera {
crate::camera_ui::page_input(ui, resp, &xf, ptr, i, view, can_modify);
}
- if tool == QuickTool::Crop && crate::crop::page_input(ui, resp, &xf, ptr, i, info, view, can_crop) {
+ if tool == QuickTool::Crop
+ && crate::crop::page_input(ui, crate::crop::CropPageInput { resp, xf: &xf, ptr, page: i, info, view, allowed: can_crop })
+ {
view.current = i;
frame.out.open_boxes = true;
}
@@ -1209,7 +1221,7 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
};
// While a table is being set up, nothing else is drawn or moved.
let allowed = can_modify && table_preview.is_none() && frame.out.open_table.is_none();
- let o = crate::prepare::page_input(ui, resp, &xf, ptr, i, info, form, field_tool, allowed, view);
+ let o = crate::prepare::page_input(cx, form, field_tool, allowed, view);
frame.out.field_props |= o.properties;
frame.out.field_placed |= o.placed;
frame.out.open_table = frame.out.open_table.or(o.table);
@@ -1220,7 +1232,7 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
&& !(editing_content && can_modify && crate::content_ui::select_grabs(&xf, ptr, i, info, added, view))
&& !comments::select_grabs(&pcx, view)
{
- crate::forms_ui::page_input(ui, resp, &xf, ptr, i, info, form, can_fill, view)
+ crate::forms_ui::page_input(cx, form, can_fill, view)
} else {
FieldPointer::Away
};
@@ -1234,11 +1246,10 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
let (vx, vy) = xf.screen_to_view(p);
text.as_ref().is_some_and(|t| t.glyphs.iter().any(|g| vx >= g.rect[0] && vx <= g.rect[2] && vy >= g.rect[1] && vy <= g.rect[3]))
};
- crate::redact_ui::page_input(ui, resp, &xf, ptr, i, info, over_text, view)
+ crate::redact_ui::page_input(cx, over_text, view)
};
let image_owns_pointer = tool == QuickTool::EditText && crate::edit_text_ui::image_owns_pointer(ui, &xf, ptr, i, info, view);
- let on_objects =
- !image_owns_pointer && tool == QuickTool::EditText && can_modify && crate::object_ui::page_input(ui, resp, &xf, ptr, i, doc, view);
+ let on_objects = !image_owns_pointer && tool == QuickTool::EditText && can_modify && crate::object_ui::page_input(cx, view);
let lines = if matches!(tool, QuickTool::AddText | QuickTool::EditText) && can_modify {
let generation = doc.edit_generation();
match view.edit_lines.get(&i) {
@@ -1267,12 +1278,7 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
let on_content = !on_objects && editing_content && can_modify && {
source_blocked
|| crate::content_ui::page_input(
- ui,
- resp,
- &xf,
- ptr,
- i,
- info,
+ cx,
added,
tool == QuickTool::AddText && !over_source,
matches!(tool, QuickTool::AddText | QuickTool::EditText),
@@ -1299,7 +1305,7 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
};
// Editable text takes precedence over background/overlapping artwork. Both
// inventories are painted every frame, even when text owns the pointer.
- let text_used = crate::edit_text_ui::page_input(ui, resp, &xf, ptr, i, doc, &lines, view, !others_own_pointer && !image_owns_pointer);
+ let text_used = crate::edit_text_ui::page_input(cx, &lines, view, !others_own_pointer && !image_owns_pointer);
let image_cx = crate::edit_text_ui::ImageCx {
ui,
resp,
@@ -1312,7 +1318,7 @@ fn page_tool_input(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<
let image_used = crate::edit_text_ui::image_input(&image_cx, &images, view, &mut frame.out.image_action);
!others_own_pointer && (text_used || image_used)
};
- let on_link = tool == QuickTool::Link && can_modify && crate::link_ui::page_input(ui, resp, &xf, ptr, i, info, doc_links, view);
+ let on_link = tool == QuickTool::Link && can_modify && crate::link_ui::page_input(cx, doc_links, view);
if tool == QuickTool::Select
&& resp.secondary_clicked()
&& can_modify
@@ -1558,6 +1564,7 @@ fn page_overlays(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_
let PageSlot { i, xf, .. } = *slot;
let PageFrame { resp, ptr, pointer, .. } = *frame;
let painter = ui.painter();
+ let tool_cx = PageToolContext { ui, resp, xf: &xf, ptr, page: i, doc, info };
let pcx = comments::PageCx { page: i, xf: &xf, ptr, info, tool, prefs, allowed, hidden: comments_hidden, redact_mark };
if tool == QuickTool::Select && !input.consumed {
crate::image_pick::page_input(resp, &xf, i, info, doc, pointer, view);
@@ -1585,13 +1592,13 @@ fn page_overlays(ui: &mut egui::Ui, view: &mut DocView, frame: &mut PageFrame<'_
crate::link_ui::paint(painter, &xf, i, info, doc_links, view, pointer);
}
if preparing {
- crate::prepare::paint_page(ui, painter, &xf, i, info, form, view, pointer);
+ crate::prepare::paint_page(tool_cx, form, view);
if let Some(preview) = &table_preview {
crate::field_table_ui::paint_preview(painter, &xf, i, preview);
}
if calc_order {
let selected = view.prepare.selected.as_ref().map(|(name, _)| name.as_str());
- if let Some(name) = crate::calc_order_ui::paint_page(painter, &xf, i, info, form, &view.calc_order, generation, selected, pointer) {
+ if let Some(name) = crate::calc_order_ui::paint_page(tool_cx, form, &view.calc_order, generation, selected) {
frame.calc_hover = Some(name);
}
}
diff --git a/crates/ui-egui/src/canvas/crop.rs b/crates/ui-egui/src/canvas/crop.rs
index 1f1e1aa13..403209291 100644
--- a/crates/ui-egui/src/canvas/crop.rs
+++ b/crates/ui-egui/src/canvas/crop.rs
@@ -18,17 +18,18 @@ fn to_user(xf: &PageXform, info: &DocInfo, page: usize, p: Pos2) -> [f64; 2] {
}
/// Crop gestures on one page. Returns `Some(true)` to open Set Page Boxes for this page.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- view: &mut DocView,
- allowed: bool,
-) -> bool {
+pub(crate) struct CropPageInput<'a> {
+ pub(crate) resp: &'a egui::Response,
+ pub(crate) xf: &'a PageXform,
+ pub(crate) ptr: &'a PagePointer,
+ pub(crate) page: usize,
+ pub(crate) info: &'a DocInfo,
+ pub(crate) view: &'a mut DocView,
+ pub(crate) allowed: bool,
+}
+
+pub(crate) fn page_input(ui: &egui::Ui, input: CropPageInput<'_>) -> bool {
+ let CropPageInput { resp, xf, ptr, page, info, view, allowed } = input;
let (pointer, origin) = (ptr.pos, ptr.origin);
if pointer.is_some_and(|p| xf.rect.contains(p)) {
ui.ctx().set_cursor_icon(if allowed { egui::CursorIcon::Crosshair } else { egui::CursorIcon::NotAllowed });
diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs
index b81ac2d58..a46f2173c 100644
--- a/crates/ui-egui/src/chrome.rs
+++ b/crates/ui-egui/src/chrome.rs
@@ -134,18 +134,20 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
let file = (name != doc.name).then_some(doc.name.as_str());
let response = tab(
ui,
- ui.scope_id().with(("document-tab", app.views[i].id)),
- &t,
- "file-text",
- &name,
- file,
- dirty,
- app.active == Some(i),
+ TabSpec {
+ id: ui.scope_id().with(("document-tab", app.views[i].id)),
+ theme: &t,
+ icon: "file-text",
+ name: &name,
+ file,
+ dirty,
+ active: app.active == Some(i),
+ index: i,
+ cap,
+ height: tab_h,
+ draggable: true,
+ },
&mut close,
- i,
- cap,
- tab_h,
- true,
);
slots.push((i, response.rect));
if response.dragged() || response.drag_stopped() {
@@ -216,18 +218,20 @@ pub fn tab_strip(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
let mut close = None;
let response = tab(
ui,
- ui.scope_id().with("combine-tab"),
- &t,
- "files",
- tl!("Combine files"),
- None,
- false,
- app.combine_showing(),
+ TabSpec {
+ id: ui.scope_id().with("combine-tab"),
+ theme: &t,
+ icon: "files",
+ name: tl!("Combine files"),
+ file: None,
+ dirty: false,
+ active: app.combine_showing(),
+ index: usize::MAX,
+ cap,
+ height: tab_h,
+ draggable: false,
+ },
&mut close,
- usize::MAX,
- cap,
- tab_h,
- false,
);
if changed && app.combine_showing() {
response.scroll_to_me(Some(Align::Center));
@@ -397,22 +401,22 @@ fn tab_cap(natural: &[f32], budget: f32, gap: f32, min: f32) -> Option<f32> {
/// Draw one tab with its icon, elided label, close button and unsaved dot; records a close request and
/// returns the tab response for drag handling.
-#[allow(clippy::too_many_arguments)]
-fn tab(
- ui: &mut egui::Ui,
+struct TabSpec<'a> {
id: egui::Id,
- t: &Tokens,
- icon: &str,
- name: &str,
- file: Option<&str>,
+ theme: &'a Tokens,
+ icon: &'a str,
+ name: &'a str,
+ file: Option<&'a str>,
dirty: bool,
active: bool,
- close: &mut Option<usize>,
index: usize,
cap: Option<f32>,
height: f32,
draggable: bool,
-) -> egui::Response {
+}
+
+fn tab(ui: &mut egui::Ui, spec: TabSpec<'_>, close: &mut Option<usize>) -> egui::Response {
+ let TabSpec { id, theme: t, icon, name, file, dirty, active, index, cap, height, draggable } = spec;
let font = theme::regular(13.0);
let mut label = tab_name(name);
// A shrunk tab shows as much of its name as fits (the tooltip has all of it).
diff --git a/crates/ui-egui/src/panels/comments_panel.rs b/crates/ui-egui/src/panels/comments_panel.rs
index 54ca9e515..294d90f2b 100644
--- a/crates/ui-egui/src/panels/comments_panel.rs
+++ b/crates/ui-egui/src/panels/comments_panel.rs
@@ -285,7 +285,7 @@ pub(crate) fn show(
}
let thread = replies_of(a);
let rotation = info.pages.get(a.page).map_or(0, |p| p.rotation);
- if let Some(e) = card(ui, t, a, rotation, thread, info, view, prefs, allowed, nav) {
+ if let Some(e) = card(ui, view, CardContext { theme: t, annotation: a, rotation, thread, info, prefs, allowed, nav }) {
edit = Some(e);
}
}
@@ -294,20 +294,20 @@ pub(crate) fn show(
/// Draw one comment thread card, including author, body or inline editor, replies, status and per-comment
/// actions; returns an edit to apply.
-#[allow(clippy::too_many_arguments)]
-fn card(
- ui: &mut egui::Ui,
- t: &Tokens,
- a: &Annotation,
+struct CardContext<'a> {
+ theme: &'a Tokens,
+ annotation: &'a Annotation,
// The clockwise `/Rotate` of the comment's page.
rotation: u16,
- thread: &[&Annotation],
- info: &DocInfo,
- view: &mut DocView,
- prefs: &CommentPrefs,
+ thread: &'a [&'a Annotation],
+ info: &'a DocInfo,
+ prefs: &'a CommentPrefs,
allowed: bool,
- nav: &mut Option<Nav>,
-) -> Option<Edit> {
+ nav: &'a mut Option<Nav>,
+}
+
+fn card(ui: &mut egui::Ui, view: &mut DocView, context: CardContext<'_>) -> Option<Edit> {
+ let CardContext { theme: t, annotation: a, rotation, thread, info, prefs, allowed, nav } = context;
let mut edit = None;
let key = (a.page, a.index);
let selected = view.comments.selected == Some(key);
diff --git a/crates/ui-egui/src/panels/right.rs b/crates/ui-egui/src/panels/right.rs
index 7b6502b9c..0470543ac 100644
--- a/crates/ui-egui/src/panels/right.rs
+++ b/crates/ui-egui/src/panels/right.rs
@@ -207,7 +207,20 @@ pub fn right_panel(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
}
}
RightPanel::Pages => pages(ui, &t, info, view, modal, bm_editable, &mut nav),
- RightPanel::Fields => field_properties = fields(ui, &t, info, &doc.form, preparing, &mut view.prepare, &mut nav, &mut panel_edit),
+ RightPanel::Fields => {
+ field_properties = fields(
+ ui,
+ FieldsPanel {
+ theme: &t,
+ info,
+ form: doc.form.as_slice(),
+ preparing,
+ selection: &mut view.prepare,
+ nav: &mut nav,
+ edit: &mut panel_edit,
+ },
+ );
+ }
RightPanel::Layers => {
if info.layers.is_empty() {
empty(ui, &t, "layers", "This document has no layers.");
@@ -447,17 +460,18 @@ fn empty(ui: &mut egui::Ui, t: &Tokens, icon: &str, text: &str) {
/// The Fields panel: fields by page, in tab order. While preparing a form, each field can move
/// earlier or later in its page's tab order (Acrobat: Order Tabs Manually).
-#[allow(clippy::too_many_arguments)]
-fn fields(
- ui: &mut egui::Ui,
- t: &Tokens,
- info: &DocInfo,
- form: &[pdfcraft_engine::FormField],
+struct FieldsPanel<'a> {
+ theme: &'a Tokens,
+ info: &'a DocInfo,
+ form: &'a [pdfcraft_engine::FormField],
preparing: bool,
- selection: &mut crate::prepare::PrepareView,
- nav: &mut Option<Nav>,
- edit: &mut Option<pdfcraft_engine::Edit>,
-) -> Option<(String, usize)> {
+ selection: &'a mut crate::prepare::PrepareView,
+ nav: &'a mut Option<Nav>,
+ edit: &'a mut Option<pdfcraft_engine::Edit>,
+}
+
+fn fields(ui: &mut egui::Ui, panel: FieldsPanel<'_>) -> Option<(String, usize)> {
+ let FieldsPanel { theme: t, info, form, preparing, selection, nav, edit } = panel;
let mut properties = None;
if info.fields.is_empty() {
empty(ui, t, "text-cursor-input", "This document has no form fields.");
diff --git a/crates/ui-egui/src/tools/calc_order_ui.rs b/crates/ui-egui/src/tools/calc_order_ui.rs
index 4cbfa2817..2b7a0c5f9 100644
--- a/crates/ui-egui/src/tools/calc_order_ui.rs
+++ b/crates/ui-egui/src/tools/calc_order_ui.rs
@@ -13,14 +13,12 @@
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
+use crate::canvas::PageToolContext;
+use crate::theme::{self, Tokens};
+use crate::{LeftPanel, PdfCraftApp, icons, widgets};
use egui::{Align2, Color32, CornerRadius, Pos2, Rect, Sense, Shape, Stroke, Vec2, pos2, vec2};
use pdfcraft_engine::form_scripts::{CalcOp, Calculate, format_value};
use pdfcraft_engine::{CalculatedField, CalculationIssue, CalculationOrder, Edit, FormField, FormFieldKind};
-use pdfcraft_render::DocInfo;
-
-use crate::canvas::PageXform;
-use crate::theme::{self, Tokens};
-use crate::{LeftPanel, PdfCraftApp, icons, widgets};
/// Links and steps on the page, which is paper white in either theme.
const LINK_OK: Color32 = Color32::from_rgb(0x1B, 0x63, 0xE0);
@@ -726,18 +724,16 @@ pub(crate) fn panel(app: &mut PdfCraftApp, ui: &mut egui::Ui, t: &Tokens) {
/// nothing to do with faded, and the steps of the focus and its linked fields; with nothing in
/// focus, the steps of the fields whose place is wrong. A form with many calculations stays
/// readable either way. Returns the field under the pages' `pointer`.
-#[allow(clippy::too_many_arguments)]
pub(crate) fn paint_page(
- painter: &egui::Painter,
- xf: &PageXform,
- page: usize,
- info: &DocInfo,
+ cx: PageToolContext<'_>,
form: &[FormField],
view: &CalcOrderView,
generation: u64,
selected: Option<&str>,
- pointer: Option<Pos2>,
) -> Option<String> {
+ let PageToolContext { ui, xf, page, info, ptr, .. } = cx;
+ let painter = ui.painter();
+ let pointer = ptr.pos;
let listed = view.current(generation)?;
// Each field's first widget on this page.
let mut on_page: Vec<(&str, Rect)> = Vec::new();
diff --git a/crates/ui-egui/src/tools/content_ui.rs b/crates/ui-egui/src/tools/content_ui.rs
index 0da3b235c..ee99ee0a2 100644
--- a/crates/ui-egui/src/tools/content_ui.rs
+++ b/crates/ui-egui/src/tools/content_ui.rs
@@ -7,7 +7,7 @@ use egui::{Color32, CornerRadius, Pos2, Rect, Stroke};
use pdfcraft_engine::{Added, AddedContent, AddedText, Edit};
use pdfcraft_render::DocInfo;
-use crate::canvas::{DocView, PageXform};
+use crate::canvas::{DocView, PageToolContext, PageXform};
use crate::page_pointer::PagePointer;
use crate::theme::Tokens;
use crate::widgets;
@@ -345,20 +345,15 @@ pub(crate) fn select_grabs(xf: &PageXform, ptr: &PagePointer, page: usize, info:
/// Pointer input on one page in Edit mode (`adding_text` with the Text tool). `true` when it
/// used the input.
-#[allow(clippy::too_many_arguments)]
pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
+ cx: PageToolContext<'_>,
added: &[Added],
adding_text: bool,
edit_on_click: bool,
style: &AddedText,
view: &mut DocView,
) -> bool {
+ let PageToolContext { ui, resp, xf, ptr, page, info, .. } = cx;
let mut consumed = false;
let pointer = ptr.pos;
let cv = &mut view.content;
diff --git a/crates/ui-egui/src/tools/edit_text_ui.rs b/crates/ui-egui/src/tools/edit_text_ui.rs
index 297c3d9a3..a14e4e661 100644
--- a/crates/ui-egui/src/tools/edit_text_ui.rs
+++ b/crates/ui-egui/src/tools/edit_text_ui.rs
@@ -11,7 +11,7 @@ use egui::{Color32, CornerRadius, FontFamily, FontId, Pos2, Rect, Stroke};
use pdfcraft_engine::{Edit, ObjectKind, ObjectTarget};
use pdfcraft_render::DocInfo;
-use crate::canvas::{DocView, PageXform};
+use crate::canvas::{DocView, PageToolContext, PageXform};
use crate::page_pointer::PagePointer;
mod background;
@@ -292,17 +292,8 @@ fn user_box(xf: &PageXform, info: &DocInfo, page: usize, r: Rect) -> [f64; 4] {
/// Open `block` on `page` in the inline editor. The box's geometry and source are read from the
/// `lines` handed in, so callers pass the document's current inventory.
-#[allow(clippy::too_many_arguments)]
-fn open_editor(
- ui: &egui::Ui,
- xf: &PageXform,
- doc: &pdfcraft_engine::Document,
- lines: &[pdfcraft_engine::TextBlock],
- boxes: &[Rect],
- page: usize,
- block: usize,
- view: &mut DocView,
-) {
+fn open_editor(cx: PageToolContext<'_>, lines: &[pdfcraft_engine::TextBlock], boxes: &[Rect], block: usize, view: &mut DocView) {
+ let PageToolContext { ui, xf, doc, page, .. } = cx;
view.pending_text_open = None;
let (Some(l), Some(b)) = (lines.get(block), boxes.get(block)) else { return };
view.image_selection = None;
@@ -341,18 +332,8 @@ fn open_editor(
/// Lines and their screen boxes for a page; hover outlines, click opens the editor. Returns
/// `true` when the pointer was used.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- doc: &pdfcraft_engine::Document,
- lines: &[pdfcraft_engine::TextBlock],
- view: &mut DocView,
- allow_interaction: bool,
-) -> bool {
+pub(crate) fn page_input(cx: PageToolContext<'_>, lines: &[pdfcraft_engine::TextBlock], view: &mut DocView, allow_interaction: bool) -> bool {
+ let PageToolContext { ui, resp, xf, ptr, page, doc, .. } = cx;
let info = &doc.info;
if view.block_drag.is_some_and(|d| d.generation != doc.edit_generation()) {
view.block_drag = None;
@@ -377,7 +358,7 @@ pub(crate) fn page_input(
// The edit may have regrouped the page: open the paragraph that was clicked, wherever it
// is now, and none if it was merged into another (#2516).
match key.find(lines, 0) {
- Some(block) => open_editor(ui, xf, doc, lines, &boxes, page, block, view),
+ Some(block) => open_editor(cx, lines, &boxes, block, view),
None => view.pending_text_open = None,
}
}
@@ -487,7 +468,7 @@ pub(crate) fn page_input(
view.pending_text_open = lines.get(hit).map(|l| (page, pdfcraft_engine::BlockKey::of(l)));
return true;
}
- open_editor(ui, xf, doc, lines, &boxes, page, hit, view);
+ open_editor(cx, lines, &boxes, hit, view);
}
true
}
diff --git a/crates/ui-egui/src/tools/fill_sign.rs b/crates/ui-egui/src/tools/fill_sign.rs
index 3bf9b3326..995b60fc4 100644
--- a/crates/ui-egui/src/tools/fill_sign.rs
+++ b/crates/ui-egui/src/tools/fill_sign.rs
@@ -8,8 +8,7 @@ use pdfcraft_engine::signature_scan::{DEFAULT_SENSITIVITY, PREVIEW_MAX_SIDE, SCA
use pdfcraft_engine::{Edit, FillMark, NewAnnotation, PhotoRect, Shape, SignatureImage, SignaturePhoto, Style, Turn};
use pdfcraft_render::{DocInfo, PageInfo};
-use crate::canvas::{DocView, PageXform};
-use crate::page_pointer::PagePointer;
+use crate::canvas::{DocView, PageToolContext, PageXform};
use crate::theme::Tokens;
mod scan;
@@ -429,24 +428,21 @@ pub(crate) fn paint_saved(ui: &egui::Ui, rect: egui::Rect, saved: &SavedSig, cac
}
}
+/// Values that shape how Fill & Sign handles this page's input.
+pub(crate) struct PageInput<'a> {
+ pub(crate) tool: FillTool,
+ pub(crate) text_size: f64,
+ pub(crate) signature: Option<&'a SavedSig>,
+ pub(crate) initials: Option<&'a SavedSig>,
+ pub(crate) preview: &'a mut Option<(SavedSig, egui::TextureHandle)>,
+ pub(crate) author: &'a str,
+ pub(crate) date_text: &'a Result<String, String>,
+}
+
/// Preview and clicks with a Fill & Sign tool on one page.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- tool: FillTool,
- text_size: f64,
- view: &mut DocView,
- signature: Option<&SavedSig>,
- initials: Option<&SavedSig>,
- preview: &mut Option<(SavedSig, egui::TextureHandle)>,
- author: &str,
- date_text: &Result<String, String>,
-) -> Option<FillAction> {
+pub(crate) fn page_input(cx: PageToolContext<'_>, input: PageInput<'_>, view: &mut DocView) -> Option<FillAction> {
+ let PageToolContext { ui, resp, xf, ptr, page, info, .. } = cx;
+ let PageInput { tool, text_size, signature, initials, preview, author, date_text } = input;
let pointer = ptr.within(xf.rect)?;
let p = info.pages.get(page)?;
let at = to_user(xf, info, page, pointer);
diff --git a/crates/ui-egui/src/tools/forms_ui.rs b/crates/ui-egui/src/tools/forms_ui.rs
index 6bcec6469..705ecf45b 100644
--- a/crates/ui-egui/src/tools/forms_ui.rs
+++ b/crates/ui-egui/src/tools/forms_ui.rs
@@ -13,8 +13,7 @@ use egui::{Color32, CornerRadius, Pos2, Rect, Stroke, vec2};
use pdfcraft_engine::{Edit, FieldValue, FormField, FormFieldKind, FormWidget, field_flags, form_appearance};
use pdfcraft_render::DocInfo;
-use crate::canvas::{DocView, PageXform};
-use crate::page_pointer::PagePointer;
+use crate::canvas::{DocView, PageToolContext, PageXform};
use crate::theme::Tokens;
const FOCUS_BLUE: Color32 = Color32::from_rgb(0x14, 0x73, 0xE6);
@@ -165,18 +164,8 @@ pub(crate) enum FieldPointer {
}
/// Clicks on form widgets of one page.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- form: &[FormField],
- allowed: bool,
- view: &mut DocView,
-) -> FieldPointer {
+pub(crate) fn page_input(cx: PageToolContext<'_>, form: &[FormField], allowed: bool, view: &mut DocView) -> FieldPointer {
+ let PageToolContext { ui, resp, xf, ptr, page, info, .. } = cx;
let Some(p) = ptr.within(xf.rect) else { return FieldPointer::Away };
let hit = form.iter().find_map(|f| {
f.widgets
@@ -319,8 +308,17 @@ pub(crate) fn commit(view: &mut DocView, form: &[FormField]) {
/// The text box of an editable combo box, over the field. What is typed becomes the value when
/// Enter, Tab or a click away commits it; a pick from the list still sets an option.
-#[allow(clippy::too_many_arguments)]
-fn combo_editor(ctx: &egui::Context, view: &mut DocView, form: &[FormField], f: &FormField, w: &FormWidget, rect: Rect, zoom: f32, widget: usize) {
+struct ComboEditor<'a> {
+ form: &'a [FormField],
+ field: &'a FormField,
+ widget: &'a FormWidget,
+ rect: Rect,
+ zoom: f32,
+ index: usize,
+}
+
+fn combo_editor(ctx: &egui::Context, view: &mut DocView, editor: ComboEditor<'_>) {
+ let ComboEditor { form, field: f, widget: w, rect, zoom, index: widget } = editor;
let mut enter = false;
egui::Area::new(egui::Id::unique(("form-editor", view.id.0)))
.fade_in(false)
@@ -847,7 +845,7 @@ pub(crate) fn overlay(
// An editable combo box has a text box over the field as well as its list (ISO
// 32000-2 Table 233, Edit).
if editable_combo(f) {
- combo_editor(ctx, view, form, f, w, rect, zoom, focus.widget);
+ combo_editor(ctx, view, ComboEditor { form, field: f, widget: w, rect, zoom, index: focus.widget });
}
let resp = egui::Area::new(egui::Id::unique(("form-choices", view.id.0)))
.order(egui::Order::Foreground)
diff --git a/crates/ui-egui/src/tools/link_ui.rs b/crates/ui-egui/src/tools/link_ui.rs
index f804f121e..c68ccca81 100644
--- a/crates/ui-egui/src/tools/link_ui.rs
+++ b/crates/ui-egui/src/tools/link_ui.rs
@@ -6,8 +6,7 @@ use egui::{Color32, CornerRadius, Pos2, Rect, Stroke};
use pdfcraft_engine::{Edit, LinkAction, LinkHighlight, LinkItem, LinkStyle};
use pdfcraft_render::DocInfo;
-use crate::canvas::{DocView, PageXform};
-use crate::page_pointer::PagePointer;
+use crate::canvas::{DocView, PageToolContext, PageXform};
use crate::theme::{self, Tokens};
use crate::{PdfCraftApp, widgets};
@@ -57,17 +56,8 @@ fn to_user(xf: &PageXform, info: &DocInfo, page: usize, p: Pos2) -> [f64; 2] {
}
/// Link tool input on one page. Returns `true` when the gesture belongs to the tool.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- links: &[LinkItem],
- view: &mut DocView,
-) -> bool {
+pub(crate) fn page_input(cx: PageToolContext<'_>, links: &[LinkItem], view: &mut DocView) -> bool {
+ let PageToolContext { ui, resp, xf, ptr, page, info, .. } = cx;
let pointer = ptr.pos;
let lv = &mut view.links;
if let Some((dp, start)) = lv.drag
diff --git a/crates/ui-egui/src/tools/object_ui.rs b/crates/ui-egui/src/tools/object_ui.rs
index d495116c6..be23b86d4 100644
--- a/crates/ui-egui/src/tools/object_ui.rs
+++ b/crates/ui-egui/src/tools/object_ui.rs
@@ -7,8 +7,7 @@ use egui::{Color32, Pos2, Rect, Stroke};
use pdfcraft_engine::{Document, Edit, EditableObject, MAX_MOVE_OBJECTS, ObjectKind, ObjectTarget};
use pdfcraft_render::DocInfo;
-use crate::canvas::{DocView, PageXform};
-use crate::page_pointer::PagePointer;
+use crate::canvas::{DocView, PageToolContext, PageXform};
const BLUE: Color32 = Color32::from_rgb(0x14, 0x73, 0xE6);
@@ -163,15 +162,8 @@ fn leave_single(view: &mut DocView) {
/// Returns true while group selection owns the interaction. Ordinary clicks still reach the
/// single-object text/resize tools. A text draft is finished or discarded through its editor.
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- doc: &Document,
- view: &mut DocView,
-) -> bool {
+pub(crate) fn page_input(cx: PageToolContext<'_>, view: &mut DocView) -> bool {
+ let PageToolContext { ui, xf, ptr, page, .. } = cx;
if view.objects.input_blocked {
return true;
}
@@ -189,22 +181,13 @@ pub(crate) fn page_input(
return false;
}
let mut selection = std::mem::take(&mut view.objects);
- let result = input(&mut selection, ui, resp, xf, ptr, page, doc, view);
+ let result = input(&mut selection, cx, view);
view.objects = selection;
result
}
-#[allow(clippy::too_many_arguments)]
-fn input(
- s: &mut ObjectSelection,
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- doc: &Document,
- view: &mut DocView,
-) -> bool {
+fn input(s: &mut ObjectSelection, cx: PageToolContext<'_>, view: &mut DocView) -> bool {
+ let PageToolContext { ui, resp, xf, ptr, page, doc, .. } = cx;
let pointer = ptr.pos;
let (pressed, down) = (ptr.pressed().is_some(), ptr.primary_down);
let info = &doc.info;
diff --git a/crates/ui-egui/src/tools/prepare.rs b/crates/ui-egui/src/tools/prepare.rs
index 9713791e3..88f0b1d2b 100644
--- a/crates/ui-egui/src/tools/prepare.rs
+++ b/crates/ui-egui/src/tools/prepare.rs
@@ -10,9 +10,8 @@ use pdfcraft_engine::form_scripts::{CalcOp, Calculate, DATE_PRESETS, Format, TIM
use pdfcraft_engine::{BorderStyle, Document, Edit, FieldFont, FieldLook, FieldProps, FormField, FormFieldKind, NewField};
use pdfcraft_render::DocInfo;
-use crate::canvas::{DocView, PageXform};
+use crate::canvas::{DocView, PageToolContext, PageXform};
use crate::comments;
-use crate::page_pointer::PagePointer;
use crate::theme;
use crate::widgets;
@@ -253,19 +252,8 @@ pub(crate) struct Outcome {
}
/// Pointer input on one page while preparing a form.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- form: &[FormField],
- tool: Option<FieldTool>,
- allowed: bool,
- view: &mut DocView,
-) -> Outcome {
+pub(crate) fn page_input(cx: PageToolContext<'_>, form: &[FormField], tool: Option<FieldTool>, allowed: bool, view: &mut DocView) -> Outcome {
+ let PageToolContext { ui, resp, xf, ptr, page, info, .. } = cx;
let mut out = Outcome { consumed: false, properties: false, placed: false, table: None };
let pointer = ptr.pos;
let prep = &mut view.prepare;
@@ -556,17 +544,10 @@ pub fn arrange(form: &[FormField], anchor: &(String, usize), others: &[(String,
/// Fields with their light-blue fill and name tags, the selection with its handles, and the
/// rectangle being drawn or dragged to the pages' `pointer`.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn paint_page(
- ui: &egui::Ui,
- painter: &egui::Painter,
- xf: &PageXform,
- page: usize,
- info: &DocInfo,
- form: &[FormField],
- view: &DocView,
- pointer: Option<Pos2>,
-) {
+pub(crate) fn paint_page(cx: PageToolContext<'_>, form: &[FormField], view: &DocView) {
+ let PageToolContext { ui, xf, page, info, ptr, .. } = cx;
+ let painter = ui.painter();
+ let pointer = ptr.pos;
let prep = &view.prepare;
let grab = prep.grab.filter(|(gp, ..)| *gp == page);
let zoom = xf.zoom().clamp(0.5, 3.0);
diff --git a/crates/ui-egui/src/tools/redact_ui.rs b/crates/ui-egui/src/tools/redact_ui.rs
index 8e7ac02ee..99974787f 100644
--- a/crates/ui-egui/src/tools/redact_ui.rs
+++ b/crates/ui-egui/src/tools/redact_ui.rs
@@ -7,8 +7,7 @@ use egui::{Color32, CornerRadius, Pos2, Rect, Stroke};
use pdfcraft_engine::{Edit, NewAnnotation, REDACT_PATTERNS, REDACTION_CODE_SETS, RedactPattern, RedactionCodeSet, Rgb, Shape, Style, rect_quad};
use pdfcraft_render::DocInfo;
-use crate::canvas::{DocView, PageXform};
-use crate::page_pointer::PagePointer;
+use crate::canvas::{DocView, PageToolContext, PageXform};
use crate::theme::Tokens;
use crate::{PdfCraftApp, widgets};
@@ -235,17 +234,8 @@ fn to_user(xf: &PageXform, info: &DocInfo, page: usize, p: Pos2) -> [f64; 2] {
/// Redact tool input on a page. Presses on text are left to text selection (the selection is
/// marked when it ends, see [`after_text`]); presses elsewhere draw a box. Returns `true` when
/// the gesture is a box.
-#[allow(clippy::too_many_arguments)]
-pub(crate) fn page_input(
- ui: &egui::Ui,
- resp: &egui::Response,
- xf: &PageXform,
- ptr: &PagePointer,
- page: usize,
- info: &DocInfo,
- over_text: impl Fn(Pos2) -> bool,
- view: &mut DocView,
-) -> bool {
+pub(crate) fn page_input(cx: PageToolContext<'_>, over_text: impl Fn(Pos2) -> bool, view: &mut DocView) -> bool {
+ let PageToolContext { ui, resp, xf, ptr, page, info, .. } = cx;
let pointer = ptr.pos;
if let Some((dp, start)) = view.redact_drag
&& dp == page
diff --git a/crates/ui-egui/src/workflows/combine_ui/table.rs b/crates/ui-egui/src/workflows/combine_ui/table.rs
index 86015db39..7d6ac3114 100644
--- a/crates/ui-egui/src/workflows/combine_ui/table.rs
+++ b/crates/ui-egui/src/workflows/combine_ui/table.rs
@@ -51,9 +51,9 @@ pub(super) fn table(
let rows = Rows { checks, selected, twice, conflicts, dragging, sort: app.combine_tab.sort };
let mut heading_event = None;
let mut shown = None;
- egui::ScrollArea::horizontal()
- .id_salt("combine-files-h")
- .show(ui, |ui| table_body(app, ui, t, &rows, widths, &mut events, &mut heading_event, &mut shown));
+ egui::ScrollArea::horizontal().id_salt("combine-files-h").show(ui, |ui| {
+ table_body(ui, TableBody { app, theme: t, rows: &rows, widths, events: &mut events, heading_event: &mut heading_event, shown: &mut shown })
+ });
// A column resized by hand keeps its width from now on.
if let Some(shown) = shown
&& shown.iter().zip(&widths).any(|(a, b)| (a - b).abs() > 0.5)
@@ -154,17 +154,18 @@ fn heading(ui: &mut egui::Ui, t: &Tokens, key: SortKey, sort: Option<(SortKey, b
/// Draw the Combine files table body: reorder handles, editable page ranges with validation, file metadata
/// and per-row warnings.
-#[allow(clippy::too_many_arguments)]
-fn table_body(
- app: &mut PdfCraftApp,
- ui: &mut egui::Ui,
- t: &Tokens,
- rows: &Rows,
+struct TableBody<'a> {
+ app: &'a mut PdfCraftApp,
+ theme: &'a Tokens,
+ rows: &'a Rows<'a>,
widths: [f32; 5],
- events: &mut TableEvents,
- heading_event: &mut Option<HeadingEvent>,
- shown: &mut Option<[f32; 5]>,
-) {
+ events: &'a mut TableEvents,
+ heading_event: &'a mut Option<HeadingEvent>,
+ shown: &'a mut Option<[f32; 5]>,
+}
+
+fn table_body(ui: &mut egui::Ui, context: TableBody<'_>) {
+ let TableBody { app, theme: t, rows, widths, events, heading_event, shown } = context;
let n = app.combine_draft.len();
let order = app.combine_columns.order;
// The heading being dragged follows the pointer.
diff --git a/crates/ui-egui/src/workflows/export_ui.rs b/crates/ui-egui/src/workflows/export_ui.rs
index 0af69f182..5435531e8 100644
--- a/crates/ui-egui/src/workflows/export_ui.rs
+++ b/crates/ui-egui/src/workflows/export_ui.rs
@@ -125,8 +125,7 @@ pub(crate) fn body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens, kind: E
}
/// Write the files (`name`, bytes) produced for `pages`, reporting progress.
-#[allow(clippy::too_many_arguments)]
-fn run(
+struct ExportTask<'a> {
src: ExportSource,
kind: ExportKind,
dpi: f64,
@@ -134,10 +133,12 @@ fn run(
min_side: u32,
pages: Vec<usize>,
stem: String,
- mut sink: impl FnMut(&str, Vec<u8>) -> Result<(), String>,
- status: &ExportStatus,
+ status: &'a ExportStatus,
lang: crate::i18n::Lang,
-) -> String {
+}
+
+fn run(task: ExportTask<'_>, mut sink: impl FnMut(&str, Vec<u8>) -> Result<(), String>) -> String {
+ let ExportTask { src, kind, dpi, format, min_side, pages, stem, status, lang } = task;
// May run on a worker thread: messages are drawn in the UI's language.
crate::i18n::set_current(lang);
let total = pages.len();
@@ -250,7 +251,7 @@ impl PdfCraftApp {
let sink = |name: &str, bytes: Vec<u8>| {
crate::editing::write_atomically(&dir.join(name).to_string_lossy(), &bytes).map_err(|e| format!("{name}: {e}"))
};
- let msg = run(src, kind, dpi, format, min_side, pages, stem, sink, &st, lang);
+ let msg = run(ExportTask { src, kind, dpi, format, min_side, pages, stem, status: &st, lang }, sink);
if let Ok(mut s) = st.lock() {
let (done, total) = s.as_ref().map_or((0, 0), |(d, t, _)| (*d, *t));
let done_msg = crate::i18n::fmt(tl!("{msg} to {dir}"), &[("msg", &msg), ("dir", &shown)]);
@@ -274,18 +275,10 @@ impl PdfCraftApp {
}
#[cfg(target_arch = "wasm32")]
{
- let msg = run(
- src,
- kind,
- dpi,
- format,
- min_side,
- pages,
- stem,
- |name, bytes| crate::editing::download(name, &bytes),
- &status,
- crate::i18n::current(),
- );
+ let msg =
+ run(ExportTask { src, kind, dpi, format, min_side, pages, stem, status: &status, lang: crate::i18n::current() }, |name, bytes| {
+ crate::editing::download(name, &bytes)
+ });
if let Ok(mut s) = status.lock() {
*s = Some((0, 0, Some(msg)));
}