M12: a portfolio's initial document is kept by its name-tree key, and Read mode gives back the panel the file list replaced (#1652)

This commit is contained in:
Eric Kaden committed 2026-10-29 05:25:10 -04:00
1 parent 12d53d707e
commit 014828ee1d
3 files changed
+75 -17

No files matched your search

+34 -13
View File
@@ -6,6 +6,11 @@ pub(crate) fn same_file(a: &str, b: &str) -> bool {
a == b || matches!((std::path::absolute(a), std::path::absolute(b)), (Ok(x), Ok(y)) if x == y)
}
/// An attachment opens as a PDF in a tab of its own, as the Attachments panel's Open does.
fn opens_as_pdf(a: &pdfcraft_render::Attachment) -> bool {
a.name.to_lowercase().ends_with(".pdf")
}
impl PdfCraftApp {
/// Open a document the way it asks to be opened: navigation panel, layout, magnification,
/// page and the place on it (Document Properties ▸ Initial View, `/OpenAction`). With Always
@@ -275,13 +280,15 @@ impl PdfCraftApp {
// the user closed stays closed (#1590).
let opening_mode = self.mode_override.unwrap_or(self.default_mode);
let initial = doc.initial_view();
// A portfolio presents the document its `/D` names (ISO 32000-2 Table 153), when that is a PDF.
let portfolio_initial = doc
.info
.portfolio
.as_ref()
.and_then(|p| p.initial)
.filter(|&i| doc.info.attachments.get(i).is_some_and(|a| a.name.to_lowercase().ends_with(".pdf")));
// A portfolio presents the document its `/D` names (ISO 32000-2 Table 153), when that is a
// PDF: kept by its name-tree key, which an edit meanwhile can't shift as it can an index.
let portfolio_initial =
doc.info.portfolio.as_ref().and_then(|p| p.initial).and_then(|i| doc.info.attachments.get(i)).filter(|a| opens_as_pdf(a)).and_then(|a| {
match &a.source {
pdfcraft_render::AttachmentSource::Document { key } => Some(key.clone()),
pdfcraft_render::AttachmentSource::Annotation { .. } => None,
}
});
if activate && self.right.is_none() && !self.right_panel_closed && opening_mode != Mode::Read {
self.right = if !doc.info.annotations.is_empty() {
Some(RightPanel::Comments)
@@ -320,7 +327,7 @@ impl PdfCraftApp {
}
// A document opening in front of a portfolio that hasn't presented its initial
// document yet (files opened together) keeps the front.
self.portfolio_initial = portfolio_initial.map(|index| (id, index));
self.portfolio_initial = portfolio_initial.map(|key| (id, key));
}
if activate
&& self.left_open
@@ -440,8 +447,9 @@ impl PdfCraftApp {
#[cfg(target_arch = "wasm32")]
let busy = self.modal_open() || self.web_reads.load(std::sync::atomic::Ordering::Relaxed) > 0;
if !busy
&& let Some((doc, index)) = self.portfolio_initial.take()
&& let Some((doc, key)) = self.portfolio_initial.take()
&& self.active_ids().is_some_and(|(_, id)| id == doc)
&& let Some(index) = self.portfolio_attachment(doc, &key)
{
self.attachment_action(doc, index, true);
// A portfolio inside the portfolio waits until the reader opens it, so a nested
@@ -477,14 +485,27 @@ impl PdfCraftApp {
self.right = Some(RightPanel::Attachments);
}
/// Where in `doc`'s attachments now the PDF its name tree keys `key` is: an attachment
/// deleted since the portfolio opened moves it, or it is gone.
fn portfolio_attachment(&self, doc: DocId, key: &str) -> Option<usize> {
self.session
.get(doc)?
.info
.attachments
.iter()
.position(|a| opens_as_pdf(a) && matches!(&a.source, pdfcraft_render::AttachmentSource::Document { key: k } if k == key))
}
/// Leave a portfolio's tab: the panel its file list replaced comes back, unless the reader
/// changed panels meanwhile.
/// changed panels meanwhile. In Read mode, which put the list away and gives it back on
/// leaving, it is that panel Read mode gives back instead.
fn leave_portfolio(&mut self) {
self.portfolio_seen = None;
if let Some(before) = self.portfolio_panel.take()
&& self.right == Some(RightPanel::Attachments)
{
let Some(before) = self.portfolio_panel.take() else { return };
if self.right == Some(RightPanel::Attachments) {
self.right = before;
} else if self.mode == Mode::Read && self.right_before_read == Some(RightPanel::Attachments) {
self.right_before_read = before;
}
}
+4 -4
View File
@@ -268,10 +268,10 @@ pub struct PdfCraftApp {
/// The user closed the right panel, so opening a document leaves it closed instead of
/// showing its comments or bookmarks (#1590). Remembered across restarts.
pub right_panel_closed: bool,
/// A portfolio that just opened and the attachment it presents first (`/Collection /D`),
/// opened by [`Self::present_portfolios`] on the next frame while the portfolio is still the
/// active tab.
portfolio_initial: Option<(DocId, usize)>,
/// A portfolio that just opened and the name-tree key of the attachment it presents first
/// (`/Collection /D`), opened by [`Self::present_portfolios`] on the next frame while the
/// portfolio is still the active tab.
portfolio_initial: Option<(DocId, String)>,
/// The document whose tab [`Self::present_portfolios`] last saw active.
portfolio_seen: Option<DocId>,
/// Browser file reads still under way (drops, the web file picker): each file opens in front
+37
View File
@@ -183,6 +183,43 @@ fn a_password_asked_for_a_file_opened_with_the_portfolio_stays_asked() {
}
}
/// An attachment deleted while the initial document waits (here for a password asked for a file
/// opened with the portfolio) doesn't change which document opens.
#[test]
fn a_deleted_attachment_doesnt_change_which_document_opens() {
let pdf = portfolio("/Collection << /D (form.pdf) >>", &[("intro.pdf", PAGE), ("form.pdf", PAGE)]);
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {
let mut app = PdfCraftApp::new();
app.set_option("language", "en").unwrap();
app.open_bytes("portfolio.pdf", None, pdf.clone()).unwrap();
app.open_bytes("locked.pdf", None, locked()).unwrap();
app
});
h.run_steps(4);
assert!(h.state_mut().apply_edit(pdfcraft_engine::Edit::DeleteAttachment { key: "intro.pdf".into() }));
h.state_mut().submit_password(None);
h.run_steps(4);
assert_eq!(tabs(&h), (vec!["portfolio.pdf".into(), "form.pdf".into()], Some("form.pdf".into())));
}
/// Leaving the portfolio's tab in Read mode: Read mode gives back the panel the file list
/// replaced, not the list, on the tab that is active when it ends.
#[test]
fn read_mode_gives_back_the_panel_the_file_list_replaced() {
let mut h = opened(portfolio("/Collection << /View /D >>", &[("form.pdf", PAGE)]), true);
h.state_mut().open_bytes("other.pdf", None, PAGE.to_vec()).unwrap();
h.run_steps(2);
select(&mut h, 0);
assert_eq!(h.state().right, Some(RightPanel::Attachments));
assert!(h.state_mut().execute("view.read_mode"));
h.run_steps(2);
assert_eq!(h.state().right, None);
select(&mut h, 1);
assert!(h.state_mut().execute("view.read_mode"));
h.run_steps(2);
assert_eq!(h.state().right, None, "the panel was closed before the list showed");
}
/// `/View /H` presents the initial document but leaves the file list to the reader.
#[test]
fn a_portfolio_that_keeps_its_list_hidden_still_presents_its_initial_document() {