mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-19 02:26:48 +00:00
fix(ui): Edit ▸ Copy and Paste are enabled for selected comments (#7123)
Fixes #7123 ## What this changes With a drawn annotation (or any comment) selected, Edit ▸ Copy is now enabled, and Edit ▸ Paste is enabled once comments have been copied, matching ⌘C / ⌘V. **Root cause.** `run_menu_command` already sends the clipboard items as their keys (`menus.rs:561`: Copy pushes `Event::Copy`, Paste requests a paste), and `clipboard_keys` handles those by copying the selected comments when no text is selected and by pasting copied XFDF onto the canvas. Only the enabled state was out of sync. `menu_state` enabled Copy for selected text, a picked image, Edit-a-PDF objects or Organize, and Paste only for objects or pages, so both stayed greyed out with a comment selected. **Fix.** There are two new arms in `menu_state` (`crates/ui-egui/src/menus.rs`), placed after the existing image, object and page arms so those still take precedence: - Copy follows `comment.copy` when a comment is selected and no text is. Selected text is what ⌘C copies first, so it is left to the existing text rule. - Paste follows `comment.paste` when the comment clipboard holds something, outside Organize and Prepare Form, where the canvas does not take pastes. ## How you verified it - [x] `cargo fmt --check` - [x] clippy with `-D warnings` on `pdfcraft-ui-egui` (the only warning is in vendored `hayro-interpret`, which was already there) - [x] tests pass on `pdfcraft-ui-egui`, except for failures that also happen on `main` on my machine (Windows, MSVC): `comments::the_selection_bar_stays_inside_the_pages_area` fails every time, and `print::printing_landscape_sheets_tells_cups_to_turn_them` and `print::each_printer_keeps_what_it_last_printed_with_across_a_restart` fail some runs and not others **Test.** `comments::edit_copy_and_paste_are_enabled_for_a_selected_comment` draws a square, selects it, and checks that Edit ▸ Copy is enabled. It then runs Edit ▸ Copy through `run_menu_command` and checks that the comment was copied and that Paste is now enabled. Finally it deselects the comment and checks that Copy is greyed out again. It fails on `main` (`Edit ▸ Copy is enabled with a comment selected`) and passes with the fix. ## Never-crash - [x] No new `unwrap`/`expect`/`panic!` outside tests, no indexing, no `unsafe` ## Anything a reviewer should look at first - **Paste is enabled from app state, without reading the clipboard.** After copying a comment, if another app later puts plain text on the system clipboard, Edit ▸ Paste looks enabled but pastes nothing, as ⌘V already does in that case. I did not read the clipboard just to draw the menu (see #6169 / #6379). - **Copy follows `comment.copy`, which needs only an open document.** So Edit ▸ Copy is now enabled for a selected comment on a document that forbids copying. The same was already true of ⌘C and Edit ▸ Copy comments, so this is not a new way to copy. Whether copy-restricted documents should block copying comments at all is a separate question, and this PR leaves it alone. - **The command palette is unchanged.** `edit.copy` run from the palette (`command_enabled` / `execute`) still covers only text; this PR fixes the menus the issue reports.
This commit is contained in:
1 parent
4448ee940b
commit
4f9d93c173
2 files changed
+67
No files matched your search
@@ -497,6 +497,17 @@ impl PdfCraftApp {
|
||||
doc.is_some_and(|d| d.allows_copying() && (command.id != CUT || d.allows_modification()))
|
||||
}
|
||||
PASTE if edit_objects && self.object_clipboard.is_some() => doc.is_some_and(pdfcraft_engine::Document::allows_modification),
|
||||
// Selected comments are what ⌘C copies when no text is selected, and ⌘V pastes
|
||||
// copied ones onto the pages (#7123); the menu items send those same events.
|
||||
// Selected text is ⌘C's first, and Prepare Form takes neither key (`clipboard_keys`).
|
||||
"edit.copy"
|
||||
if !organize
|
||||
&& !self.is_preparing()
|
||||
&& view.is_some_and(|v| v.comments.selected.is_some() && v.selected_text().is_none_or(|t| t.is_empty())) =>
|
||||
{
|
||||
enabled_id("comment.copy")
|
||||
}
|
||||
PASTE if !organize && !self.is_preparing() && self.comment_clipboard.is_some() => enabled_id("comment.paste"),
|
||||
CUT => organize && enabled_id("page.cut"),
|
||||
PASTE => organize && self.page_clipboard.is_some() && enabled_id("page.paste"),
|
||||
SELECT_ALL | DELETE => has_doc,
|
||||
|
||||
@@ -5587,6 +5587,62 @@ fn edit_delete_with_a_popup_open_removes_the_selected_comment() {
|
||||
assert!(comments(&h).is_empty(), "Edit ▸ Delete removed the comment");
|
||||
}
|
||||
|
||||
/// Edit ▸ Copy and Paste take a selected drawn comment as ⌘C and ⌘V do (#7123). Both were
|
||||
/// greyed out, as the menu only counted selected text, pictures and objects.
|
||||
#[test]
|
||||
fn edit_copy_and_paste_are_enabled_for_a_selected_comment() {
|
||||
let mut h = selected_square();
|
||||
let state = |h: &Harness<'static, PdfCraftApp>, id: &str| h.state().menu_state(&h.ctx, &craft_menu::CommandRef::new(id)).enabled;
|
||||
assert!(state(&h, "edit.copy"), "Edit ▸ Copy is enabled with a comment selected");
|
||||
assert!(!state(&h, "edit.paste"), "nothing is copied yet");
|
||||
|
||||
let ctx = h.ctx.clone();
|
||||
h.state_mut().run_menu_command(&ctx, &craft_menu::CommandRef::new("edit.copy"));
|
||||
h.run_steps(4);
|
||||
assert!(h.state().comment_clipboard.is_some(), "Edit ▸ Copy copied the comment");
|
||||
assert!(state(&h, "edit.paste"), "Edit ▸ Paste is enabled with a comment copied");
|
||||
|
||||
// Edit ▸ Paste asks the platform for the clipboard; its answer, the XFDF Copy put there,
|
||||
// arrives as a paste (the harness has no platform, so the test delivers it). The copy hangs
|
||||
// on the pointer, as with ⌘V, and a click places it there.
|
||||
let id = h.state().views[0].id;
|
||||
let annotations = |h: &Harness<'static, PdfCraftApp>| h.state().session.get(id).unwrap().info.annotations.clone();
|
||||
let before = annotations(&h).len();
|
||||
let xfdf = h.state().comment_clipboard.as_ref().map(|c| c.xfdf.clone()).expect("copied");
|
||||
h.state_mut().run_menu_command(&ctx, &craft_menu::CommandRef::new("edit.paste"));
|
||||
h.step();
|
||||
assert!(crate::support::asked_for_the_clipboard(&h), "Edit ▸ Paste asked the platform for the clipboard");
|
||||
h.step();
|
||||
h.event(egui::Event::Paste(xfdf));
|
||||
h.run_steps(2);
|
||||
assert!(h.state().views[0].comments.gesture.is_some(), "Edit ▸ Paste waits for a click to place the copy");
|
||||
let at = (200.0, 160.0);
|
||||
click_pt(&mut h, at);
|
||||
let after = annotations(&h);
|
||||
assert_eq!(after.len(), before + 1, "Edit ▸ Paste placed one copy: {after:?}");
|
||||
let placed = after.iter().max_by_key(|a| a.index).map(|a| a.rect).expect("placed");
|
||||
assert!(((placed[0] + placed[2]) / 2.0 - at.0).abs() < 1.0, "centred on the click: {placed:?}");
|
||||
assert!(h.state().views[0].comments.gesture.is_none(), "the copy is placed");
|
||||
assert!(h.state().views[0].comments.selected.is_some(), "the copy is selected");
|
||||
|
||||
h.state_mut().views[0].comments.selected = None;
|
||||
h.run_steps(2);
|
||||
assert!(!state(&h, "edit.copy"), "with nothing selected, Copy is greyed out again");
|
||||
}
|
||||
|
||||
/// Prepare Form keeps ⌘C off the selected comments, so Edit ▸ Copy, which sends the same Copy,
|
||||
/// is greyed out there too rather than enabled and doing nothing (#7123).
|
||||
#[test]
|
||||
fn edit_copy_is_off_for_a_selected_comment_while_preparing_a_form() {
|
||||
let mut h = selected_square();
|
||||
assert!(h.state_mut().execute("form.prepare"));
|
||||
h.run_steps(2);
|
||||
assert!(h.state().is_preparing());
|
||||
assert!(h.state().views[0].comments.selected.is_some(), "the comment stays selected");
|
||||
let copy = h.state().menu_state(&h.ctx, &craft_menu::CommandRef::new("edit.copy"));
|
||||
assert!(!copy.enabled, "Edit ▸ Copy is greyed out while preparing a form");
|
||||
}
|
||||
|
||||
/// A native menu's click never reaches the window, so Edit ▸ Delete closes the Hotbar tools window,
|
||||
/// as it closes egui's popups, and removes the selected comment. The window held the Delete.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user