PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M7: Add / Update / Remove menu on Header and footer, Watermark, Background (#1192) (#1857)
Alexis0a054a425ce3
diff --git a/ROADMAP.md b/ROADMAP.mdindex 284e87419..ed84b6ab4 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -458,3 +458,4 @@ Newest first: the date, what moved, and the new overall estimate.| 2026-10-12 | minor | M8: image redaction removes alternate images and unsupported masks only on affected images. Estimates unchanged. |- **2026-10-12 (M8):** Redaction cleans indirect and form structure references, preserving surviving form tags.+- **2026-10-13 (M7):** Edit a PDF ▸ Add content: Header and footer, Watermark and Background open an Add / Update / Remove menu (Update and Remove enabled only when the document has that mark), as Acrobat's Watermark control does. The catalogue gains `ToolItem::actions`, so any tool can list more commands (#1192).diff --git a/crates/engine/src/catalog.rs b/crates/engine/src/catalog.rsindex 991c86a9c..afc0673be 100644--- a/crates/engine/src/catalog.rs+++ b/crates/engine/src/catalog.rs@@ -24,6 +24,11 @@ pub struct ToolItem {pub icon: &'static str,pub command: &'static str,pub availability: Availability,+ /// Further commands of the same tool, shown as a drop-down beside the entry: Acrobat's+ /// Watermark control offers Add, Update and Remove. The first is `command` itself. A UI+ /// enables each one by its own command's needs, so Update and Remove stay greyed out until+ /// the document has that kind of mark. Empty for a plain entry.+ pub actions: &'static [&'static str],}#[derive(Clone, Copy, Debug)]@@ -45,7 +50,16 @@ pub struct ToolGroup {}const fn item(label: &'static str, icon: &'static str, command: &'static str, availability: Availability) -> ToolItem {- ToolItem { label, icon, command, availability }+ ToolItem { label, icon, command, availability, actions: &[] }+}++/// An entry that opens a menu of `actions` (the first one is its `command`).+const fn menu_item(label: &'static str, icon: &'static str, actions: &'static [&'static str]) -> ToolItem {+ let command = match actions {+ [first, ..] => *first,+ [] => "",+ };+ ToolItem { label, icon, command, availability: Ready, actions }}use Availability::{Planned, Provider, Ready};@@ -128,11 +142,11 @@ pub static TOOL_GROUPS: &[ToolGroup] = &[item("Edit text & images", "text-cursor-input", "edit.edit_text", Ready),item("Text", "type", "edit.text", Ready),item("Image", "image-plus", "edit.image", Ready),- item("Header and footer", "heading", "edit.header_footer", Ready),- item("Watermark", "stamp", "edit.watermark", Ready),+ menu_item("Header and footer", "heading", &["edit.header_footer", "edit.header_footer.update", "edit.header_footer.remove"]),+ menu_item("Watermark", "stamp", &["edit.watermark", "edit.watermark.update", "edit.watermark.remove"]),item("Link", "link-2", "edit.link", Ready),item("Bates numbering", "hash", "edit.bates", Ready),- item("Background", "palette", "edit.background", Ready),+ menu_item("Background", "palette", &["edit.background", "edit.background.update", "edit.background.remove"]),item("Attach file", "paperclip", "edit.attach", Planned("M12")),],},@@ -555,4 +569,38 @@ mod tests {}}}++ #[test]+ fn menu_entries_list_registered_commands_starting_with_their_own() {+ let mut menus = 0;+ for g in TOOL_GROUPS {+ for s in g.sections {+ for i in s.items {+ if i.actions.is_empty() {+ continue;+ }+ menus += 1;+ assert_eq!(i.actions[0], i.command, "{}: the first action is the entry's command", i.label);+ for id in i.actions {+ assert!(crate::commands::command(id).is_some(), "{id} is not a registered command");+ }+ }+ }+ }+ // Watermark, Header and footer, Background: Add / Update / Remove each (Acrobat's menu).+ assert!(menus >= 3);+ }++ #[test]+ fn every_update_and_remove_command_is_reachable_from_a_tool_entry() {+ let reachable: HashSet<&str> =+ TOOL_GROUPS.iter().flat_map(|g| g.sections.iter().flat_map(|s| s.items.iter())).flat_map(|i| i.actions.iter().copied()).collect();+ for id in crate::commands::COMMANDS+ .iter()+ .map(|c| c.id)+ .filter(|id| id.starts_with("edit.") && (id.ends_with(".update") || id.ends_with(".remove")))+ {+ assert!(reachable.contains(id), "{id} is in no tool menu");+ }+ }}diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex dc989d660..536a47575 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -230,8 +230,32 @@ fn tool_detail(app: &mut PdfCraftApp, ui: &mut egui::Ui, t: &Tokens, g: &'staticrow_label(ui, tl!(item.label), theme::regular(13.0), fg, body.left_center() + vec2(34.0, 0.0), r.left() - body.left() - 42.0);ui.painter().rect_filled(r, CornerRadius::same(4), fill);ui.painter().text(r.center(), Align2::CENTER_CENTER, chip_label, font, cfg);- if theme::hand(resp).on_hover_text(format!("{} — {}", tl!(item.label), item.command)).clicked() {- run = Some(item.command);+ let resp = theme::hand(resp).on_hover_text(format!("{} — {}", tl!(item.label), item.command));+ if item.actions.is_empty() {+ if resp.clicked() {+ run = Some(item.command);+ }+ } else {+ // A caret before the chip says the entry opens a menu (Add / Update / Remove).+ let c = r.left_center() - vec2(10.0, 0.0);+ ui.painter().add(egui::Shape::convex_polygon(+ vec![c + vec2(-3.5, -2.0), c + vec2(3.5, -2.0), c + vec2(0.0, 2.5)],+ fg,+ egui::Stroke::NONE,+ ));+ egui::Popup::menu(&resp).show(|ui| {+ ui.set_min_width(180.0);+ for (n, id) in item.actions.iter().enumerate() {+ let Some(spec) = pdfcraft_engine::commands::command(id) else { continue };+ // Add stays usable with no document open: it asks for a file first.+ let enabled = (n == 0 && app.active.is_none()) || app.command_enabled(spec);+ let label = crate::i18n::menu_label(spec.id, spec.label);+ if ui.add_enabled(enabled, egui::Button::new(label)).clicked() {+ run = Some(spec.id);+ ui.close();+ }+ }+ });}}}