Skip to content

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

M10: select pages in the Pages panel and print the selection (#296)

Sameh Elhawarya9209c323574
diff --git a/ATTRIBUTION.toml b/ATTRIBUTION.toml
index 969a3093a..323d05e0b 100644
--- a/ATTRIBUTION.toml
+++ b/ATTRIBUTION.toml
@@ -19,7 +19,7 @@ licence = "MIT OR Apache-2.0"
licence_file = "LICENSE-MIT"
kind = "translation"
usage = "Simplified Chinese UI strings, compiled in by crates/ui-egui/src/i18n/mod.rs"
-sha256 = "e7b02a3dd6b53496a45ec3c20a03c0572fc2586825a15452ae2395038285ec55"
+sha256 = "4f002361b308776512acb1e0fad553ba51af7287f60e675846861bd44cc36e46"
[[asset]]
path = "crates/ui-egui/src/i18n/zh-hant.tsv"
@@ -30,7 +30,7 @@ licence = "MIT OR Apache-2.0"
licence_file = "LICENSE-MIT"
kind = "translation"
usage = "Traditional Chinese UI strings, compiled in by crates/ui-egui/src/i18n/mod.rs"
-sha256 = "c5ef451d44d941f455c7202839cc1ed60f81ede9a6fdcc4a1a2c1ebc4a0923ba"
+sha256 = "e31ae39637ae02e9aec4c887ca1c52924ba7161f78219a82b1c49e3502e04222"
[[asset]]
path = "crates/ui-egui/src/i18n/cs.tsv"
@@ -63,7 +63,7 @@ licence = "MIT OR Apache-2.0"
licence_file = "LICENSE-MIT"
kind = "translation"
usage = "Spanish UI strings, compiled in by crates/ui-egui/src/i18n/mod.rs"
-sha256 = "399a6a51a6da28a9323f7afaa5b7cb8c6f143f8a9082859fa4f77235784ebcdc"
+sha256 = "2ec17cb75e1c5936921e55a8a210128383cfd4fb03211d1acd750c1bec1a5d19"
[[asset]]
path = "crates/ui-egui/src/i18n/fr.tsv"
@@ -74,7 +74,7 @@ licence = "MIT OR Apache-2.0"
licence_file = "LICENSE-MIT"
kind = "translation"
usage = "French UI strings, compiled in by crates/ui-egui/src/i18n/mod.rs"
-sha256 = "2f2b4b75f1914dc873584c4a2e6f94bd8be95c4146cf1d42eb57e67d646a672d"
+sha256 = "2587fb0e75d5a8c1e2bb9c918cbb2d8adb34835c8ddec77500ba6517555c0c55"
[[asset]]
path = "crates/ui-egui/src/i18n/ja.tsv"
@@ -85,7 +85,7 @@ licence = "MIT OR Apache-2.0"
licence_file = "LICENSE-MIT"
kind = "translation"
usage = "Japanese UI strings, compiled in by crates/ui-egui/src/i18n/mod.rs"
-sha256 = "1151e22f87e8ad69e5ab4db0d818b23eb4a6655addc4f3ebb5cc04ec01c03b2c"
+sha256 = "9ce3369dac8963a90633b4191680587dd43003c1cf5edf24fe641d8d0e8c6b3c"
[[asset]]
path = "assets/icons/accessibility.svg"
diff --git a/crates/print/src/lib.rs b/crates/print/src/lib.rs
index 25b6873e4..35e4661c3 100644
--- a/crates/print/src/lib.rs
+++ b/crates/print/src/lib.rs
@@ -25,7 +25,7 @@ pub mod spool;
#[cfg(test)]
mod tests;
-pub use range::{Subset, select_pages};
+pub use range::{Subset, select_listed, select_pages};
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum PrintError {
diff --git a/crates/print/src/range.rs b/crates/print/src/range.rs
index 063874623..2fea7541e 100644
--- a/crates/print/src/range.rs
+++ b/crates/print/src/range.rs
@@ -50,6 +50,21 @@ pub fn select_pages(count: usize, range: Option<&str>, labels: &[String], subset
}
}
}
+ narrow(pages, subset, reverse)
+}
+
+/// The pages to print from an explicit list (0-based, e.g. the thumbnails selected in the
+/// interface), in the order given. Unlike a typed range the entries are page positions, never
+/// labels. The subset and `reverse` apply as in [`select_pages`].
+pub fn select_listed(count: usize, pages: &[usize], subset: Subset, reverse: bool) -> Result<Vec<usize>, PrintError> {
+ if let Some(p) = pages.iter().find(|p| **p >= count) {
+ return Err(PrintError::Invalid(format!("page {} is out of range (1–{count})", p.saturating_add(1))));
+ }
+ narrow(pages.to_vec(), subset, reverse)
+}
+
+/// Odd or even positions of the chosen pages, then the print order.
+fn narrow(pages: Vec<usize>, subset: Subset, reverse: bool) -> Result<Vec<usize>, PrintError> {
let mut out: Vec<usize> = match subset {
Subset::All => pages,
Subset::Odd => pages.into_iter().step_by(2).collect(),
diff --git a/crates/print/src/tests.rs b/crates/print/src/tests.rs
index 9e14fe1c2..30ca598bd 100644
--- a/crates/print/src/tests.rs
+++ b/crates/print/src/tests.rs
@@ -68,6 +68,13 @@ fn page_selection() {
assert!(matches!(select_pages(5, Some("7"), &[], Subset::All, false), Err(PrintError::Invalid(_))));
assert!(matches!(select_pages(5, Some("x"), &[], Subset::All, false), Err(PrintError::Invalid(_))));
assert_eq!(select_pages(1, None, &[], Subset::Even, false), Err(PrintError::NoPages));
+ // An explicit list (selected thumbnails): positions, never labels, in the order given.
+ assert_eq!(select_listed(5, &[1, 3], Subset::All, false).unwrap(), [1, 3]);
+ assert_eq!(select_listed(5, &[0, 2, 4], Subset::Even, false).unwrap(), [2]);
+ assert_eq!(select_listed(5, &[0, 2, 4], Subset::Odd, true).unwrap(), [4, 0]);
+ assert!(matches!(select_listed(5, &[1, 5], Subset::All, false), Err(PrintError::Invalid(_))));
+ assert!(matches!(select_listed(0, &[usize::MAX], Subset::All, false), Err(PrintError::Invalid(_))));
+ assert_eq!(select_listed(5, &[], Subset::All, false), Err(PrintError::NoPages));
}
#[test]
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 4c01ac70f..8a1e51181 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -235,7 +235,7 @@ pub struct DocView {
/// Turns wheel input into page turns in single-page view.
wheel: crate::wheel_pager::WheelPager,
pub(crate) auto_scroll: crate::autoscroll::AutoScroll,
- /// Pages selected in the organize grid (0-based). Empty means "the current page".
+ /// Pages selected in the organize grid or the Pages panel (0-based). Empty means "the current page".
pub selected: BTreeSet<usize>,
/// Anchor for ⇧-click range selection in the organize grid.
select_anchor: Option<usize>,
@@ -415,6 +415,37 @@ impl DocView {
}
}
+ /// A click on page `i`'s thumbnail, in the organize grid or the Pages panel. ⇧ selects the
+ /// range from the anchor (or the current page); ⌘/Ctrl toggles the page; a plain click
+ /// selects only it. `seed_current` is for the Pages panel, where an empty selection means
+ /// the current page: the first ⌘-click on another page keeps the current one selected too.
+ pub fn click_page(&mut self, i: usize, modifiers: egui::Modifiers, seed_current: bool) {
+ if i >= self.page_count {
+ return;
+ }
+ if modifiers.shift {
+ let a = self.select_anchor.unwrap_or(self.current);
+ self.selected = (a.min(i)..=a.max(i)).collect();
+ } else if modifiers.command {
+ if seed_current && self.selected.is_empty() && i != self.current {
+ self.selected.insert(self.current);
+ }
+ if !self.selected.remove(&i) {
+ self.selected.insert(i);
+ }
+ self.select_anchor = Some(i);
+ } else {
+ self.selected = [i].into();
+ self.select_anchor = Some(i);
+ }
+ }
+
+ /// Drop the page selection; a later ⇧-click ranges from `anchor`.
+ pub fn clear_page_selection(&mut self, anchor: Option<usize>) {
+ self.selected.clear();
+ self.select_anchor = anchor.filter(|a| *a < self.page_count);
+ }
+
pub fn render_pending(&self) -> bool {
!self.last_queue.is_empty()
}
@@ -2589,18 +2620,7 @@ fn organize_grid(view: &mut DocView, info: &DocInfo, pool: &RenderPool, editable
ui.painter().text(pos2(c.center().x, pr.bottom() + 16.0), Align2::CENTER_CENTER, &p.label, theme::medium(12.0), t.text_muted);
if resp.clicked() {
let m = ui.input(|i| i.modifiers);
- if m.shift {
- let a = view.select_anchor.unwrap_or(view.current);
- view.selected = (a.min(i)..=a.max(i)).collect();
- } else if m.command {
- if !view.selected.remove(&i) {
- view.selected.insert(i);
- }
- view.select_anchor = Some(i);
- } else {
- view.selected = [i].into();
- view.select_anchor = Some(i);
- }
+ view.click_page(i, m, false);
view.current = i;
}
if resp.double_clicked() {
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index be3855a24..f3295c6e1 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -553,6 +553,8 @@ impl Host for crate::PdfCraftApp {
"fit": format!("{:?}", v.fit),
"layout": format!("{:?}", v.layout),
"organize": v.organize,
+ // Pages picked in the organize grid or the Pages panel (empty: the current page).
+ "selected_pages": v.selected.iter().map(|p| p + 1).collect::<Vec<_>>(),
"auto_scrolling": v.auto_scrolling(),
"viewport": [v.viewport_rect().min.x, v.viewport_rect().min.y, v.viewport_rect().max.x, v.viewport_rect().max.y],
"find_open": v.find.is_some(),
diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv
index 1a63ad1ce..972d8158c 100644
--- a/crates/ui-egui/src/i18n/es.tsv
+++ b/crates/ui-egui/src/i18n/es.tsv
@@ -1160,6 +1160,7 @@
Detected {n} form fields Se han detectado {n} campos de formulario
{field}: {e} {field}: {e}
Current page Página actual
+ Selected pages Páginas seleccionadas
Document language Idioma del documento
Output Salida
Searchable Image (Exact) Imagen con búsqueda (exacta)
diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv
index bc3d8a458..62d687128 100644
--- a/crates/ui-egui/src/i18n/fr.tsv
+++ b/crates/ui-egui/src/i18n/fr.tsv
@@ -1160,6 +1160,7 @@
Detected {n} form fields {n} champs de formulaire détectés
{field}: {e} {field} : {e}
Current page Page actuelle
+ Selected pages Pages sélectionnées
Document language Langue du document
Output Sortie
Searchable Image (Exact) Image recherchable (exacte)
diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv
index a6213664c..0c1b41c3a 100644
--- a/crates/ui-egui/src/i18n/ja.tsv
+++ b/crates/ui-egui/src/i18n/ja.tsv
@@ -1166,6 +1166,7 @@
Detected {n} form fields フォームフィールドを {n} 件検出しました
{field}: {e} {field}: {e}
Current page 現在のページ
+ Selected pages 選択したページ
Document language 文書の言語
Output 出力
Searchable Image (Exact) 検索可能な画像(元の画像を保持)
diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv
index b4ad3b29d..60b41a340 100644
--- a/crates/ui-egui/src/i18n/zh-hans.tsv
+++ b/crates/ui-egui/src/i18n/zh-hans.tsv
@@ -829,6 +829,7 @@
Light gray 浅灰色
Dark gray 深灰色
Current page 当前页
+ Selected pages 选定的页面
Document language 文档语言
Output 输出
Searchable Image (Exact) 可搜索图像(精确)
diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsv
index 46d88a681..8ad3a99a0 100644
--- a/crates/ui-egui/src/i18n/zh-hant.tsv
+++ b/crates/ui-egui/src/i18n/zh-hant.tsv
@@ -828,6 +828,7 @@
Light gray 淺灰色
Dark gray 深灰色
Current page 當前頁
+ Selected pages 選取的頁面
Document language 文件語言
Output 輸出
Searchable Image (Exact) 可搜尋圖片(精確)
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index c3f1116ff..adc4f0c57 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -409,6 +409,8 @@ pub fn right_panel(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
let a11y = &mut app.a11y;
let compare = &app.compare;
let comment_allowed = doc.allows_annotation();
+ // A dialog or the palette owns the keyboard: the panel leaves Escape to it.
+ let modal = app.dialog.is_some() || app.palette_open;
egui::Panel::right("right_panel")
.resizable(true)
.default_size(330.0)
@@ -437,6 +439,11 @@ pub fn right_panel(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
if let Some(c) = count {
ui.label(egui::RichText::new(c.to_string()).font(theme::medium(13.0)).color(t.text_faint));
}
+ if panel == RightPanel::Pages && view.selected.len() > 1 {
+ let n = view.selected.len().to_string();
+ let picked = crate::i18n::fmt(tl!("{n} pages selected"), &[("n", &n)]);
+ ui.label(egui::RichText::new(picked).font(theme::medium(12.0)).color(t.accent_text));
+ }
ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
if icons::button(ui, "x", 26.0, false, tl!("Close")).clicked() {
close = true;
@@ -501,7 +508,7 @@ pub fn right_panel(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
outline_item(ui, &t, info, item, &[i], info.outline.len(), &mut ctx);
}
}
- RightPanel::Pages => pages(ui, &t, info, view, &mut nav),
+ RightPanel::Pages => pages(ui, &t, info, view, modal, &mut nav),
RightPanel::Fields => fields(ui, &t, info, &doc.form, preparing, &mut nav, &mut panel_edit),
RightPanel::Layers => {
if info.layers.is_empty() {
@@ -778,20 +785,36 @@ fn outline_item(ui: &mut egui::Ui, t: &Tokens, info: &DocInfo, item: &OutlineIte
}
}
-fn pages(ui: &mut egui::Ui, t: &Tokens, info: &DocInfo, view: &crate::DocView, nav: &mut Option<Nav>) {
+/// The page thumbnails. A click goes to the page; ⌘/Ctrl-click and ⇧-click pick several pages
+/// (the selection page commands and Print act on) without moving the document.
+fn pages(ui: &mut egui::Ui, t: &Tokens, info: &DocInfo, view: &mut crate::DocView, modal: bool, nav: &mut Option<Nav>) {
+ // Escape drops the selection while the pointer is over the panel and nothing else wants the key.
+ if !modal
+ && !view.selected.is_empty()
+ && !ui.ctx().egui_wants_keyboard_input()
+ && ui.rect_contains_pointer(ui.clip_rect())
+ && ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape))
+ {
+ view.clear_page_selection(Some(view.current));
+ }
let w = (ui.available_width() - 40.0).min(150.0);
for (i, p) in info.pages.iter().enumerate() {
ui.vertical_centered(|ui| {
let h = w * p.height / p.width.max(1.0);
let (rect, resp) = ui.allocate_exact_size(vec2(w + 16.0, h + 16.0), Sense::click());
let info = crate::i18n::fmt(tl!("Page {label}"), &[("label", &p.label)]);
- resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, info.clone()));
+ let picked = view.selected.contains(&i);
+ resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, true, picked, info.clone()));
+ // The current page keeps its heavier border; picked pages share its fill.
let selected = i == view.current;
- if selected {
+ if selected || picked {
ui.painter().rect_filled(rect, CornerRadius::same(8), t.accent_soft);
} else if resp.hovered() {
ui.painter().rect_filled(rect, CornerRadius::same(8), t.hover);
}
+ if picked {
+ ui.painter().rect_stroke(rect, CornerRadius::same(8), Stroke::new(1.5, t.accent), egui::StrokeKind::Inside);
+ }
let pr = rect.shrink(8.0);
ui.painter().rect_filled(pr, CornerRadius::ZERO, Color32::WHITE);
if let Some(tex) = view.thumb(i) {
@@ -803,9 +826,15 @@ fn pages(ui: &mut egui::Ui, t: &Tokens, info: &DocInfo, view: &crate::DocView, n
Stroke::new(if selected { 2.0 } else { 1.0 }, if selected { t.accent } else { t.border }),
egui::StrokeKind::Outside,
);
- ui.label(egui::RichText::new(&p.label).font(theme::medium(12.0)).color(if selected { t.accent_text } else { t.text_muted }));
+ ui.label(egui::RichText::new(&p.label).font(theme::medium(12.0)).color(if selected || picked { t.accent_text } else { t.text_muted }));
if resp.clicked() {
- *nav = Some(Nav::Page(i));
+ let m = ui.input(|i| i.modifiers);
+ if m.shift || m.command {
+ view.click_page(i, m, true);
+ } else {
+ view.clear_page_selection(Some(i));
+ *nav = Some(Nav::Page(i));
+ }
}
});
ui.add_space(4.0);
diff --git a/crates/ui-egui/src/print_ui.rs b/crates/ui-egui/src/print_ui.rs
index bcc2d9e8c..951b1d314 100644
--- a/crates/ui-egui/src/print_ui.rs
+++ b/crates/ui-egui/src/print_ui.rs
@@ -1,5 +1,5 @@
//! The Print dialog (Acrobat's File ▸ Print, execution plan M10.5): printer, copies, grayscale;
-//! pages to print (all, current, range with labels; odd/even, reverse); page sizing & handling
+//! pages to print (all, current, range with labels, the selected pages; odd/even, reverse); page sizing & handling
//! (Size, Poster, Multiple, Booklet); orientation; comments & forms; and a live preview of the
//! sheets. Printing sends the print-ready PDF to the system spooler; "Save as PDF" writes it.
@@ -14,6 +14,8 @@ pub enum Which {
All,
Current,
Range,
+ /// The pages picked in the Pages panel or the organize grid when the dialog opened.
+ Selected,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -35,6 +37,8 @@ pub struct PrintDraft {
pub duplex: spool::Duplex,
pub which: Which,
pub range: String,
+ /// The pages `Which::Selected` prints (0-based, in page order); empty when none were picked.
+ pub selected: Vec<usize>,
pub subset: Subset,
pub reverse: bool,
pub handling: Handling,
@@ -68,6 +72,7 @@ impl Default for PrintDraft {
duplex: spool::Duplex::Off,
which: Which::All,
range: String::new(),
+ selected: Vec::new(),
subset: Subset::All,
reverse: false,
handling: Handling::Size,
@@ -98,11 +103,17 @@ impl PrintDraft {
return Err("Cut and stack needs Two-sided: Off. Print single-sided sheets.".into());
}
let range = match self.which {
- Which::All => None,
- Which::Current => Some((self.current_page + 1).to_string()),
+ Which::All | Which::Selected => None,
+ Which::Current => Some(self.current_page.saturating_add(1).to_string()),
Which::Range => Some(self.range.clone()),
};
- let pages = print::select_pages(count, range.as_deref(), labels, self.subset, self.reverse).map_err(|e| e.to_string())?;
+ let pages = if self.which == Which::Selected {
+ // Positions, not a typed range: a range would read numbers as page labels first.
+ print::select_listed(count, &self.selected, self.subset, self.reverse)
+ } else {
+ print::select_pages(count, range.as_deref(), labels, self.subset, self.reverse)
+ }
+ .map_err(|e| e.to_string())?;
let layout = match self.handling {
Handling::Size => Layout::Size(match self.size {
SizeMode::Custom(_) => SizeMode::Custom(self.custom_scale),
@@ -138,8 +149,16 @@ impl PdfCraftApp {
let printers = spool::printers();
let default = printers.iter().find(|p| p.default).or(printers.first()).map(|p| p.name.clone());
let current = self.views[i].current;
+ let selected: Vec<usize> = self.views[i].selected.iter().copied().collect();
let keep = std::mem::take(&mut self.print_draft);
- self.print_draft = PrintDraft { printers, printer: default, current_page: current, sheet: 0, ..keep };
+ // Picked pages are what Print is for (Acrobat's "Selected pages"); without any, a choice
+ // left over from another document falls back to the whole document.
+ let which = match (selected.is_empty(), keep.which) {
+ (false, _) => Which::Selected,
+ (true, Which::Selected) => Which::All,
+ (true, other) => other,
+ };
+ self.print_draft = PrintDraft { printers, printer: default, current_page: current, sheet: 0, selected, which, ..keep };
self.dialog = Some(crate::Dialog::Print);
}
@@ -316,6 +335,11 @@ pub(crate) fn body(
d.which = Which::Range;
}
});
+ // Offered only when pages were picked before the dialog opened.
+ if !d.selected.is_empty() {
+ let label = format!("{} ({})", tl!("Selected pages"), d.selected.len());
+ ui.radio_value(&mut d.which, Which::Selected, label);
+ }
ui.horizontal(|ui| {
ui.label(tl!("More options:"));
combo(
diff --git a/crates/ui-egui/tests/control.rs b/crates/ui-egui/tests/control.rs
index 383a9acc8..2afebce7a 100644
--- a/crates/ui-egui/tests/control.rs
+++ b/crates/ui-egui/tests/control.rs
@@ -652,6 +652,14 @@ fn select_all_key_selects_every_page_in_organize() {
assert_eq!(ok(&mut h, &c, "ui.state", json!({}))["documents"][0]["dirty"], false);
}
+#[test]
+fn state_reports_the_selected_pages() {
+ let (mut h, c) = harness();
+ assert_eq!(ok(&mut h, &c, "ui.state", json!({}))["active"]["selected_pages"], json!([]));
+ ok(&mut h, &c, "ui.set", json!({ "key": "select", "value": "2,4" }));
+ assert_eq!(ok(&mut h, &c, "ui.state", json!({}))["active"]["selected_pages"], json!([2, 4]));
+}
+
#[test]
fn saved_signature_can_be_changed_through_the_control_channel() {
let (mut h, c) = harness();
diff --git a/crates/ui-egui/tests/editing.rs b/crates/ui-egui/tests/editing.rs
index eaa3020a9..510fda00d 100644
--- a/crates/ui-egui/tests/editing.rs
+++ b/crates/ui-egui/tests/editing.rs
@@ -1040,3 +1040,103 @@ fn dragging_a_paragraph_moves_it_and_its_edge_rewraps_it() {
assert!(near(doc.text_lines(0)[0].rect[0], moved.rect[0]), "it keeps its place");
assert!(h.state().views[0].line_editor.is_none());
}
+
+/// The Pages panel, in a window tall enough to show every thumbnail of a short fixture.
+fn pages_panel(pages: usize) -> Harness<'static, PdfCraftApp> {
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 1900.0)).build_eframe(move |_cc| {
+ let mut app = PdfCraftApp::new();
+ app.open_bytes("doc.pdf", None, fixture(pages)).expect("fixture opens");
+ app.set_option("panel", "pages").unwrap();
+ app
+ });
+ h.run_steps(4);
+ h
+}
+
+fn picked(h: &Harness<'static, PdfCraftApp>) -> Vec<usize> {
+ h.state().views[0].selected.iter().copied().collect()
+}
+
+#[test]
+fn pages_panel_command_click_picks_pages_without_moving_the_document() {
+ let mut h = pages_panel(5);
+ // The first ⌘-click on another page keeps the current page (1) selected too.
+ h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [0, 2]);
+ assert_eq!(h.state().views[0].current, 0, "picking pages does not turn the page");
+ h.get_by_label_contains("2 pages selected");
+ h.get_by_label("Page 5").click_modifiers(Modifiers::COMMAND);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [0, 2, 4]);
+ // ⌘-click again takes a page back out.
+ h.get_by_label("Page 3").click_modifiers(Modifiers::COMMAND);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [0, 4]);
+ // Page commands act on what is picked, as in the organize grid.
+ assert_eq!(h.state().views[0].target_pages(), [0, 4]);
+}
+
+#[test]
+fn pages_panel_shift_click_picks_a_range_and_a_plain_click_starts_over() {
+ let mut h = pages_panel(6);
+ // A plain click goes to the page and is the anchor of the next range.
+ h.get_by_label("Page 2").click_modifiers(Modifiers::NONE);
+ h.run_steps(2);
+ assert_eq!(h.state().views[0].current, 1);
+ assert!(picked(&h).is_empty());
+ h.get_by_label("Page 4").click_modifiers(Modifiers::SHIFT);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [1, 2, 3]);
+ assert_eq!(h.state().views[0].current, 1);
+ // A second ⇧-click ranges from the same anchor, in either direction.
+ h.get_by_label("Page 1").click_modifiers(Modifiers::SHIFT);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [0, 1]);
+ // ⇧ with no earlier click ranges from the current page.
+ h.get_by_label("Page 6").click_modifiers(Modifiers::NONE);
+ h.run_steps(2);
+ assert!(picked(&h).is_empty(), "a plain click drops the selection");
+ assert_eq!(h.state().views[0].current, 5);
+ h.get_by_label("Page 5").click_modifiers(Modifiers::SHIFT);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [4, 5]);
+}
+
+#[test]
+fn pages_panel_escape_clears_and_deleted_pages_leave_the_selection() {
+ let mut h = pages_panel(5);
+ h.get_by_label("Page 2").click_modifiers(Modifiers::COMMAND);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [0, 1]);
+ // The pointer is still over the panel.
+ h.key_press(Key::Escape);
+ h.run_steps(2);
+ assert!(picked(&h).is_empty());
+ // Pages that no longer exist drop out of the selection.
+ h.get_by_label("Page 5").click_modifiers(Modifiers::COMMAND);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [0, 4]);
+ h.state_mut().views[0].select_pages(&[3, 4]);
+ assert!(h.state_mut().execute("page.delete"));
+ h.run_steps(3);
+ assert_eq!(page_texts(h.state()).len(), 3);
+ assert!(picked(&h).iter().all(|p| *p < 3), "{:?}", picked(&h));
+}
+
+#[test]
+fn print_shortcut_offers_the_pages_picked_in_the_pages_panel() {
+ let mut h = pages_panel(5);
+ h.get_by_label("Page 2").click_modifiers(Modifiers::NONE);
+ h.run_steps(2);
+ h.get_by_label("Page 4").click_modifiers(Modifiers::COMMAND);
+ h.run_steps(2);
+ assert_eq!(picked(&h), [1, 3]);
+ h.key_press_modifiers(Modifiers::COMMAND, Key::P);
+ h.run_steps(3);
+ assert_eq!(h.state().dialog, Some(pdfcraft_ui_egui::Dialog::Print));
+ assert_eq!(h.state().print_draft.which, pdfcraft_ui_egui::PrintWhich::Selected);
+ assert_eq!(h.state().print_draft.selected, [1, 3]);
+ h.get_by_label("Selected pages (2)");
+ h.get_by_label("Sheet 1 of 2");
+}
diff --git a/crates/ui-egui/tests/print.rs b/crates/ui-egui/tests/print.rs
index d471f6c23..94ab37ff4 100644
--- a/crates/ui-egui/tests/print.rs
+++ b/crates/ui-egui/tests/print.rs
@@ -40,6 +40,49 @@ fn print_dialog_lays_out_sheets_and_saves_a_pdf() {
let _ = std::fs::remove_file(out);
}
+/// Pages picked beforehand are what the dialog offers to print (Acrobat's "Selected pages").
+#[test]
+fn print_dialog_prints_the_selected_pages() {
+ use pdfcraft_ui_egui::PrintWhich;
+ let mut h = harness();
+ let bytes = h.state().session.create_blank(200.0, 300.0, 5).unwrap();
+ h.state_mut().open_bytes("five.pdf", None, bytes.as_ref().clone()).unwrap();
+ h.run_steps(3);
+ let tab = h.state().views.len() - 1;
+ // Nothing picked: the whole document, and no "Selected pages" choice.
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(3);
+ assert_eq!(h.state().print_draft.which, PrintWhich::All);
+ assert!(h.query_by_label_contains("Selected pages").is_none());
+ h.get_by_label("Sheet 1 of 5");
+ h.state_mut().dialog = None;
+ h.run_steps(2);
+ // Pages 2 and 4 picked.
+ h.state_mut().views[tab].select_pages(&[1, 3]);
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(3);
+ assert_eq!(h.state().print_draft.which, PrintWhich::Selected);
+ h.get_by_label("Selected pages (2)");
+ h.get_by_label("Sheet 1 of 2");
+ let out = std::env::temp_dir().join(format!("pdfcraft-print-selected-{}.pdf", std::process::id()));
+ h.state_mut().save_override = Some(out.to_string_lossy().into_owned());
+ h.state_mut().print_draft.printer = None;
+ h.run_steps(1);
+ h.get_by_label("Save as PDF").click();
+ h.run_steps(3);
+ assert_eq!(h.state().dialog, None);
+ let saved = std::fs::read(&out).expect("saved");
+ let doc = pdfcraft_cos::Document::open(std::sync::Arc::new(saved)).unwrap();
+ assert_eq!(pdfcraft_model::pages(&doc).len(), 2);
+ let _ = std::fs::remove_file(out);
+ // The selection gone, the remembered choice falls back to the whole document.
+ h.state_mut().views[tab].select_pages(&[]);
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(3);
+ assert_eq!(h.state().print_draft.which, PrintWhich::All);
+ assert!(h.state().print_draft.selected.is_empty());
+}
+
#[test]
fn invalid_ranges_are_explained_in_the_preview() {
let mut h = harness();
diff --git a/parity/acrobat-features.toml b/parity/acrobat-features.toml
index bbe839b67..660478014 100644
--- a/parity/acrobat-features.toml
+++ b/parity/acrobat-features.toml
@@ -8031,11 +8031,11 @@ title = "Print selected pages from thumbnails"
area = "L"
tier = "P2"
milestone = "M10"
-status = "planned"
-commands = []
+status = "shipped"
+commands = ["print.dialog"]
tools = []
-evidence = []
-notes = ""
+evidence = ["crates/ui-egui/tests/editing.rs::print_shortcut_offers_the_pages_picked_in_the_pages_panel", "crates/ui-egui/tests/editing.rs::pages_panel_command_click_picks_pages_without_moving_the_document", "crates/ui-egui/tests/editing.rs::pages_panel_shift_click_picks_a_range_and_a_plain_click_starts_over", "crates/ui-egui/tests/print.rs::print_dialog_prints_the_selected_pages", "crates/print/src/tests.rs::page_selection"]
+notes = "Ctrl/⌘-click and ⇧-click pick thumbnails in the Pages panel (and the Organize grid); Print opens on \"Selected pages\" and prints them in page order. Missing: the thumbnails' own context-menu Print command, picking by dragging a marquee, arrow-key selection, and a printer on Windows (Save as PDF only there, see print.printer-selection)."
[[feature]]
id = "print.postscript-output"