Skip to content

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

M5/M6: Make Current Properties Default for comments (colour, opacity, width become the tool's default); required fields get a red border when fields are highlighted

Brandon Thomas4991a59479b8
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 989595e59..ae6bdaa80 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -1215,18 +1215,18 @@ pub fn document_area(app: &mut PrintCraftApp, index: usize, ui: &mut egui::Ui) {
crate::forms_ui::paint_page(ui, painter, &xf, i, info, &form, view);
}
- // Form-field highlight (Acrobat's "Highlight existing fields").
+ // Form-field highlight (Acrobat's "Highlight existing fields"); required fields get a
+ // red border.
if view.highlight_fields {
- for f in info.fields.iter().filter(|f| f.page == Some(i)) {
- if let Some(fr) = f.rect {
- let sr = xf.user_rect(info, i, fr);
+ for f in form.iter() {
+ let required = f.has(printcraft_engine::field_flags::REQUIRED);
+ for w in f.widgets.iter().filter(|w| w.page == Some(i)) {
+ let r = w.rect;
+ let sr = xf.user_rect(info, i, [r[0] as f32, r[1] as f32, r[2] as f32, r[3] as f32]);
painter.rect_filled(sr, CornerRadius::same(1), Color32::from_rgba_unmultiplied(0x6E, 0x8E, 0xF5, 48));
- painter.rect_stroke(
- sr,
- CornerRadius::same(1),
- Stroke::new(1.0, Color32::from_rgb(0x6E, 0x8E, 0xF5)),
- egui::StrokeKind::Inside,
- );
+ let (width, color) =
+ if required { (2.0, Color32::from_rgb(0xE3, 0x22, 0x22)) } else { (1.0, Color32::from_rgb(0x6E, 0x8E, 0xF5)) };
+ painter.rect_stroke(sr, CornerRadius::same(1), Stroke::new(width, color), egui::StrokeKind::Inside);
}
}
}
@@ -1420,6 +1420,9 @@ pub fn document_area(app: &mut PrintCraftApp, index: usize, ui: &mut egui::Ui) {
if let Some((p, i)) = open_props {
app.open_comment_props(p, i);
}
+ if let Some((p, i)) = app.views[index].comments.default_request.take() {
+ app.make_comment_default(p, i);
+ }
if let Some((name, w)) = field_props {
app.open_field_props(&name, w);
}
diff --git a/crates/ui-egui/src/comment_props.rs b/crates/ui-egui/src/comment_props.rs
index d7f0b2599..57cafa0dc 100644
--- a/crates/ui-egui/src/comment_props.rs
+++ b/crates/ui-egui/src/comment_props.rs
@@ -29,6 +29,26 @@ const ICONS: [NoteIcon; 7] =
[NoteIcon::Comment, NoteIcon::Note, NoteIcon::Help, NoteIcon::Insert, NoteIcon::Key, NoteIcon::NewParagraph, NoteIcon::Paragraph];
impl PrintCraftApp {
+ /// Make Current Properties Default: new comments of this kind take this one's colour,
+ /// opacity and line width.
+ pub fn make_comment_default(&mut self, page: usize, index: usize) {
+ let Some((_, id)) = self.active_ids() else { return };
+ let Some(doc) = self.session.get(id) else { return };
+ let Some(a) = doc.info.annotations.iter().find(|a| a.page == page && a.index == index && a.in_reply_to.is_none()) else { return };
+ let Some(tool) = crate::comments::tool_for(a) else { return };
+ let Some(p) = doc.comment_props(page, index) else { return };
+ let mut style = self.comment_prefs.style(tool);
+ if let Some(c) = p.color {
+ style.color = c;
+ }
+ style.opacity = p.opacity;
+ if let Some(w) = p.width {
+ style.width = w;
+ }
+ self.comment_prefs.set_style(tool, style);
+ self.notify(format!("New {} comments will look like this one", tool.label().to_lowercase()));
+ }
+
/// Open Comment Properties for the comment at `(page, index)` of the active document.
pub fn open_comment_props(&mut self, page: usize, index: usize) {
let Some((_, id)) = self.active_ids() else { return };
diff --git a/crates/ui-egui/src/comments.rs b/crates/ui-egui/src/comments.rs
index 57f85d77d..a7008f6b3 100644
--- a/crates/ui-egui/src/comments.rs
+++ b/crates/ui-egui/src/comments.rs
@@ -249,6 +249,34 @@ impl CommentPrefs {
s.width = w.clamp(0.5, 12.0);
}
}
+
+ pub fn set_style(&mut self, tool: CommentTool, style: Style) {
+ if let Some((_, s)) = self.styles.iter_mut().find(|(t, _)| *t == tool) {
+ *s = style;
+ }
+ }
+}
+
+/// The tool that makes comments like `a` (for Make Current Properties Default).
+pub fn tool_for(a: &Annotation) -> Option<CommentTool> {
+ Some(match (a.subtype.as_str(), a.intent.as_deref()) {
+ ("Text", _) => CommentTool::Note,
+ ("FreeText", Some("FreeTextCallout")) => CommentTool::Callout,
+ ("FreeText", Some("FreeTextTypeWriter")) => return None,
+ ("FreeText", _) => CommentTool::TextBox,
+ ("Highlight", _) => CommentTool::Highlight,
+ ("Underline", _) => CommentTool::Underline,
+ ("StrikeOut", _) => CommentTool::StrikeOut,
+ ("Ink", _) => CommentTool::Ink,
+ ("Line", _) => CommentTool::Line,
+ ("Square", _) => CommentTool::Rectangle,
+ ("Circle", _) => CommentTool::Oval,
+ ("Polygon", Some("PolygonCloud")) => CommentTool::Cloud,
+ ("Polygon", _) => CommentTool::Polygon,
+ ("PolyLine", _) => CommentTool::PolyLine,
+ ("Caret", _) => CommentTool::Caret,
+ _ => return None,
+ })
}
/// The user's login name, which Acrobat uses as the default comment author.
@@ -329,6 +357,8 @@ pub struct CommentView {
pub sort: SortBy,
/// Comment Properties was asked for from the panel: (page, index).
pub props_request: Option<(usize, usize)>,
+ /// Make Current Properties Default was asked for: (page, index).
+ pub default_request: Option<(usize, usize)>,
pub search_focus: bool,
/// Where the canvas context menu was opened: (page, user-space point).
pub context_at: Option<(usize, [f64; 2])>,
@@ -1067,6 +1097,10 @@ pub(crate) fn context_menu(ui: &mut egui::Ui, view: &mut DocView, info: &DocInfo
action = Some(CanvasAction::Properties(page, index));
ui.close();
}
+ if ui.add_enabled(tool_for(a).is_some(), egui::Button::new("Make Current Properties Default")).clicked() {
+ view.comments.default_request = Some((page, index));
+ ui.close();
+ }
}
None => {
if let Some((page, at)) = view.comments.context_at
diff --git a/crates/ui-egui/src/comments_panel.rs b/crates/ui-egui/src/comments_panel.rs
index ba9ddfb27..fa3908c0b 100644
--- a/crates/ui-egui/src/comments_panel.rs
+++ b/crates/ui-egui/src/comments_panel.rs
@@ -423,6 +423,10 @@ fn card(
view.comments.props_request = Some((a.page, a.index));
ui.close();
}
+ if ui.add_enabled(crate::comments::tool_for(a).is_some(), egui::Button::new("Make Current Properties Default")).clicked() {
+ view.comments.default_request = Some((a.page, a.index));
+ ui.close();
+ }
ui.separator();
if ui.add_enabled(!a.locked, egui::Button::new("Delete")).clicked() {
view.comments.selected = None;
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 3ef5c45fc..aed61adff 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -515,6 +515,9 @@ pub fn right_panel(app: &mut PrintCraftApp, ui: &mut egui::Ui) {
}
None => {}
}
+ if let Some((p, i)) = app.views.get_mut(index).and_then(|v| v.comments.default_request.take()) {
+ app.make_comment_default(p, i);
+ }
if let Some((p, i)) = app.views.get_mut(index).and_then(|v| v.comments.props_request.take()) {
app.open_comment_props(p, i);
}
diff --git a/crates/ui-egui/tests/comments.rs b/crates/ui-egui/tests/comments.rs
index a046da03a..146ee4412 100644
--- a/crates/ui-egui/tests/comments.rs
+++ b/crates/ui-egui/tests/comments.rs
@@ -424,3 +424,33 @@ fn the_panel_filters_by_colour_and_checkmark() {
h.run_steps(2);
assert_eq!(cards(&h), before);
}
+
+#[test]
+fn make_current_properties_default() {
+ let mut h = harness(|app| {
+ app.set_option("panel", "comments").unwrap();
+ app.set_option("quick", "square").unwrap();
+ });
+ drag_pt(&mut h, (40.0, 100.0), (140.0, 40.0));
+ h.state_mut().set_option("quick", "select").unwrap();
+ h.state_mut().apply_edit(printcraft_engine::Edit::StyleAnnotation {
+ page: 0,
+ index: 0,
+ color: Some([0.0, 0.47, 0.84]),
+ opacity: Some(0.5),
+ width: Some(5.0),
+ });
+ h.run_steps(2);
+ h.get_by_label("More").click();
+ h.run_steps(2);
+ h.get_by_label("Make Current Properties Default").click();
+ h.run_steps(3);
+ let st = h.state().comment_prefs.style(printcraft_ui_egui::comments::CommentTool::Rectangle);
+ assert_eq!((st.color, st.opacity, st.width), ([0.0, 0.47, 0.84], 0.5, 5.0));
+ // The next rectangle takes it.
+ h.state_mut().set_option("quick", "square").unwrap();
+ drag_pt(&mut h, (160.0, 100.0), (260.0, 40.0));
+ let c = comments(&h);
+ let last = c.iter().rfind(|a| a.subtype == "Square").unwrap();
+ assert_eq!(last.color, Some([0.0, 0.47, 0.84]));
+}
diff --git a/crates/ui-egui/tests/view.rs b/crates/ui-egui/tests/view.rs
index 8e3fa1b94..ad22e0325 100644
--- a/crates/ui-egui/tests/view.rs
+++ b/crates/ui-egui/tests/view.rs
@@ -201,3 +201,26 @@ fn the_hand_tool_pans_by_dragging() {
assert!(before.top() - after.top() > 200.0, "dragging up scrolls down: {before:?} → {after:?}");
assert!(h.state().views[0].selected_text().is_none(), "no text selection with the hand");
}
+
+#[test]
+fn required_fields_get_a_red_border_when_highlighting() {
+ let mut h = form_harness();
+ h.state_mut().set_option("panel", "none").unwrap();
+ h.state_mut().set_option("fields", "on").unwrap();
+ let props = printcraft_engine::FieldProps { required: Some(true), ..Default::default() };
+ h.state_mut().apply_edit(printcraft_engine::Edit::SetFieldProps { name: "fullname".into(), props: Box::new(props) });
+ for _ in 0..100 {
+ h.run_steps(2);
+ if !h.state().render_pending() {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ let r = rect(&h, 0).expect("page 1");
+ // Just inside the left edge of the field (x 50, y 300..330 on a 300×400 page).
+ let ppp = h.ctx.pixels_per_point();
+ let at = egui::pos2(r.min.x + r.width() * (50.0 / 300.0) + 0.75, r.min.y + r.height() * (1.0 - 315.0 / 400.0));
+ let img = h.render().expect("renders");
+ let px = *img.get_pixel((at.x * ppp) as u32, (at.y * ppp) as u32);
+ assert!(px[0] > 180 && px[1] < 100 && px[2] < 100, "a red border: {px:?}");
+}
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index 98cb6ff4a..5aadc451f 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -3779,11 +3779,11 @@ title = "Make current properties the tool default"
area = "E"
tier = "P1"
milestone = "M5"
-status = "planned"
-commands = []
+status = "shipped"
+commands = ["comment.list"]
tools = []
-evidence = []
-notes = ""
+evidence = ["crates/ui-egui/tests/comments.rs::make_current_properties_default"]
+notes = "colour, opacity and line width become the tool's defaults (canvas and Comments panel menus)"
[[feature]]
id = "comment.author-identity"
@@ -4527,11 +4527,11 @@ title = "Required-field highlighting"
area = "F"
tier = "P1"
milestone = "M6"
-status = "planned"
+status = "shipped"
commands = []
-tools = []
-evidence = []
-notes = ""
+tools = ["form_set_props"]
+evidence = ["crates/ui-egui/tests/view.rs::required_fields_get_a_red_border_when_highlighting"]
+notes = "required fields get a red border when fields are highlighted"
[[feature]]
id = "form.reset-form"