diff --git a/crates/ui-egui/src/tools/tags_ui.rs b/crates/ui-egui/src/tools/tags_ui.rs index ed019cacb3..37593b5cbe 100644 --- a/crates/ui-egui/src/tools/tags_ui.rs +++ b/crates/ui-egui/src/tools/tags_ui.rs @@ -3,13 +3,20 @@ use std::collections::{BTreeSet, HashMap, HashSet}; use std::sync::Arc; +use craft::behavior::{DisplayOrder, ItemSource, Selection, SelectionChange}; +use craft::theme::variant::Tone; +use craft::ui::buttons::Button; +use craft::ui::contract::{Bind, CollectionEvent, Outcome as _, Props as _, SelectMode, Show as _, ShowCollection as _, ShowContainer as _}; +use craft::ui::display::{Shade, Text}; +use craft::ui::form::{Field, FieldGroup}; +use craft::ui::list::{List, ListRow}; +use craft::ui::notices::Notice; +use craft::ui::text::TextField; use egui::Ui; use pdfcraft_cos::ObjRef; use pdfcraft_engine::a11y::{TagContentItem, TagInfo, TagPlacement, TagProperty}; use pdfcraft_engine::{DocId, Edit}; -use crate::{icons, theme::Tokens, widgets}; - #[derive(Clone, Debug, Default)] pub(crate) struct State { doc: Option, @@ -119,10 +126,76 @@ impl State { } } +/// The tags in display order, as the list shows them. +struct TagRows<'a> { + rows: &'a [&'a TagInfo], + parents: &'a HashSet, + collapsed: &'a BTreeSet, + /// A search lists matches with their ancestors and ignores collapsed groups, so no row is a group then. + searching: bool, + selected: Option, + /// An unsaved draft keeps every other row from being selected. + dirty: bool, +} + +impl TagRows<'_> { + fn is_group(&self, tag: &TagInfo) -> bool { + self.parents.contains(&tag.object) && !self.searching + } +} + +impl DisplayOrder for TagRows<'_> { + fn len(&self) -> usize { + self.rows.len() + } + fn key(&self, at: usize) -> ObjRef { + // The list never asks past `len()`; object 0 is a key no tag has. + self.rows.get(at).map_or_else(|| ObjRef::new(0, 0), |tag| tag.object) + } + fn depth(&self, at: usize) -> usize { + self.rows.get(at).map_or(0, |tag| tag.depth) + } + fn has_children(&self, at: usize) -> bool { + self.rows.get(at).is_some_and(|tag| self.is_group(tag)) + } + fn open(&self, at: usize) -> bool { + self.rows.get(at).is_some_and(|tag| self.is_group(tag) && !self.collapsed.contains(&tag.object)) + } + fn enabled(&self, at: usize) -> bool { + self.rows.get(at).is_some_and(|tag| !self.dirty || self.selected == Some(tag.object)) + } +} + +impl ItemSource for TagRows<'_> {} + +/// A list of `count` rows keyed by position. +struct Positions(usize); + +impl DisplayOrder for Positions { + fn len(&self) -> usize { + self.0 + } + fn key(&self, at: usize) -> usize { + at + } +} + +impl ItemSource for Positions {} + +/// The label of a marked-content row. +fn content_label(item: &TagContentItem) -> String { + let page_label = item.page.map(|p| crate::i18n::fmt(tl!("Go to page {p}"), &[("p", &(p + 1).to_string())])); + match (&page_label, item.text.trim()) { + (Some(page), text) if !text.is_empty() => format!("MCID {} · {page} · {text}", item.mcid), + (Some(page), _) => format!("MCID {} · {page}", item.mcid), + (_, text) if !text.is_empty() => format!("MCID {} · {text}", item.mcid), + _ => format!("MCID {}", item.mcid), + } +} + /// The chosen page and an edit to apply after the panel releases its document borrow. pub(crate) fn panel( ui: &mut Ui, - t: &Tokens, doc: DocId, tags: &[TagInfo], content: &[TagContentItem], @@ -137,106 +210,103 @@ pub(crate) fn panel( state.collapsed.clear(); } if tags.is_empty() { - ui.label(tl!("This document has no editable structure tags.")); + Notice::new(Tone::Neutral, tl!("This document has no editable structure tags.")).show(ui); return (None, None); } if state.selected.is_none_or(|r| !tags.iter().any(|t| t.object == r)) { state.selected = Some(tags[0].object); state.draft = Draft::from_tag(&tags[0]); } - let search_label = ui.label(tl!("Find a tag")); - ui.add(egui::TextEdit::singleline(&mut state.query).hint_text(tl!("Find a tag"))).labelled_by(search_label.id); - ui.add_space(4.0); + TextField::new(tl!("Find a tag"), Bind::bound(&mut state.query)).show(ui); let query = state.query.to_lowercase(); let matching = visible_tags(tags, &query, &state.collapsed); let parents: HashSet = tags.iter().filter_map(|tag| tag.parent).collect(); let dirty = state.selected.and_then(|r| tags.iter().find(|t| t.object == r)).is_some_and(|tag| state.draft.edit(tag).is_some()); - ui.label(egui::RichText::new(crate::i18n::fmt(tl!("{n} tags"), &[("n", &matching.len().to_string())])).small().color(t.text_muted)); + Text::new(crate::i18n::fmt(tl!("{n} tags"), &[("n", &matching.len().to_string())])).shade(Shade::Muted).show(ui); let mut page = None; - egui::ScrollArea::vertical().max_height(100.0).show_rows(ui, 26.0, matching.len(), |ui, range| { - for tag in &matching[range] { + let source = TagRows { rows: &matching, parents: &parents, collapsed: &state.collapsed, searching: !query.is_empty(), selected: state.selected, dirty }; + let mut picked = match state.selected { + Some(object) => Selection::single(object), + None => Selection::new(Vec::new(), None, None), + }; + let out = List::::new(tl!("Tags")) + .tree(true) + .natural_height(true) + .select(SelectMode::Single) + .selection(Bind::bound(&mut picked)) + .show_items(ui, &source, &mut |key, _| { + let Some(tag) = matching.iter().find(|tag| tag.object == *key) else { return ListRow::new("") }; let title = tag.title.as_deref().filter(|s| !s.is_empty()).unwrap_or(&tag.kind); - let label = format!("{title} · {}", tag.object.num); - ui.horizontal(|ui| { - ui.add_space((tag.depth.min(8) as f32) * 12.0); - if parents.contains(&tag.object) && query.is_empty() { - let closed = state.collapsed.contains(&tag.object); - if icons::button( - ui, - if closed { "chevron-right" } else { "chevron-down" }, - 18.0, - false, - if closed { tl!("Expand tag") } else { tl!("Collapse tag") }, - ) - .clicked() - { - if closed { - state.collapsed.remove(&tag.object); - } else { - state.collapsed.insert(tag.object); - } - } + let row = ListRow::new(format!("{title} · {}", tag.object.num)).depth(tag.depth); + if source.is_group(tag) { row.group(!source.collapsed.contains(&tag.object)) } else { row } + }); + for event in &out.events { + match event { + CollectionEvent::Open(change) => { + if change.open { + state.collapsed.remove(&change.key); } else { - ui.add_space(18.0); + state.collapsed.insert(change.key); } - if ui - .add_enabled(!dirty || state.selected == Some(tag.object), egui::Button::selectable(state.selected == Some(tag.object), label)) - .clicked() - { + } + CollectionEvent::Selection(SelectionChange::Set(keys)) => { + if let Some(tag) = keys.last().and_then(|key| tags.iter().find(|tag| tag.object == *key)) { state.selected = Some(tag.object); state.draft = Draft::from_tag(tag); page = tag.page; } - }); + } + _ => {} } - }); - ui.separator(); + } let Some(tag) = state.selected.and_then(|r| tags.iter().find(|t| t.object == r)) else { return (page, None) }; - ui.label(egui::RichText::new(format!("{} {} R", tag.object.num, tag.object.generation)).small().color(t.text_faint)); + Text::new(format!("{} {} R", tag.object.num, tag.object.generation)).shade(Shade::Muted).show(ui); if let Some(p) = tag.page - && ui.button(crate::i18n::fmt(tl!("Go to page {p}"), &[("p", &(p + 1).to_string())])).clicked() + && Button::new(crate::i18n::fmt(tl!("Go to page {p}"), &[("p", &(p + 1).to_string())])).show(ui).response().clicked() { page = Some(p); } - ui.add_enabled_ui(can_edit, |ui| { - field(ui, tl!("Tag type"), &mut state.draft.kind, false); - field(ui, tl!("Title"), &mut state.draft.title, false); - field(ui, tl!("Actual text"), &mut state.draft.actual_text, true); - field(ui, tl!("Alternate text"), &mut state.draft.alt, true); - field(ui, tl!("Language"), &mut state.draft.language, false); + let draft = &mut state.draft; + FieldGroup::new(tl!("Tag properties")).show(ui, |ui, _| { + Field::new(tl!("Tag type")).show(ui, |ui, _| { + TextField::new(tl!("Tag type"), Bind::bound(&mut draft.kind)).enabled(can_edit).show(ui); + }); + Field::new(tl!("Title")).show(ui, |ui, _| { + TextField::new(tl!("Title"), Bind::bound(&mut draft.title)).enabled(can_edit).show(ui); + }); + Field::new(tl!("Actual text")).show(ui, |ui, _| { + TextField::new(tl!("Actual text"), Bind::bound(&mut draft.actual_text)).lines(2).enabled(can_edit).show(ui); + }); + Field::new(tl!("Alternate text")).show(ui, |ui, _| { + TextField::new(tl!("Alternate text"), Bind::bound(&mut draft.alt)).lines(2).enabled(can_edit).show(ui); + }); + Field::new(tl!("Language")).show(ui, |ui, _| { + TextField::new(tl!("Language"), Bind::bound(&mut draft.language)).enabled(can_edit).show(ui); + }); }); let edit = state.draft.edit(tag); - let mut save = false; let siblings: Vec<&TagInfo> = tag.parent.map(|parent| tags.iter().filter(|other| other.parent == Some(parent)).collect()).unwrap_or_default(); let position = siblings.iter().position(|sibling| sibling.object == tag.object); - let before = position.and_then(|position| position.checked_sub(1)).map(|position| siblings[position]); + let before = position.and_then(|position| position.checked_sub(1)).and_then(|position| siblings.get(position).copied()); let after = position.and_then(|position| siblings.get(position + 1)).copied(); - ui.horizontal(|ui| { - save = ui.add_enabled(can_edit && !state.draft.kind.is_empty() && edit.is_some(), egui::Button::new(tl!("Save tag properties"))).clicked(); - if ui.add_enabled(dirty, egui::Button::new(tl!("Revert changes"))).clicked() { - state.draft = Draft::from_tag(tag); - } - }); - let (mut earlier, mut later, mut artifact) = (false, false, false); - ui.horizontal(|ui| { - earlier = ui.add_enabled(can_edit && !dirty && before.is_some(), egui::Button::new(tl!("Move tag earlier"))).clicked(); - later = ui.add_enabled(can_edit && !dirty && after.is_some(), egui::Button::new(tl!("Move tag later"))).clicked(); - artifact = ui - .add_enabled(can_edit && !dirty, egui::Button::new(tl!("Mark as artifact"))) - .on_hover_text(tl!("Remove this leaf tag from reading order and mark its direct content as an artifact")) - .clicked(); - }); - ui.separator(); - for item in content { - let page_label = item.page.map(|p| crate::i18n::fmt(tl!("Go to page {p}"), &[("p", &(p + 1).to_string())])); - let label = match (&page_label, item.text.trim()) { - (Some(page), text) if !text.is_empty() => format!("MCID {} · {page} · {text}", item.mcid), - (Some(page), _) => format!("MCID {} · {page}", item.mcid), - (_, text) if !text.is_empty() => format!("MCID {} · {text}", item.mcid), - _ => format!("MCID {}", item.mcid), - }; - if ui.button(label).clicked() { - page = item.page; + let save = Button::new(tl!("Save tag properties")).enabled(can_edit && !state.draft.kind.is_empty() && edit.is_some()).show(ui).response().clicked(); + if Button::new(tl!("Revert changes")).enabled(dirty).show(ui).response().clicked() { + state.draft = Draft::from_tag(tag); + } + let earlier = Button::new(tl!("Move tag earlier")).enabled(can_edit && !dirty && before.is_some()).show(ui).response().clicked(); + let later = Button::new(tl!("Move tag later")).enabled(can_edit && !dirty && after.is_some()).show(ui).response().clicked(); + let artifact = Button::new(tl!("Mark as artifact")).enabled(can_edit && !dirty).show(ui).response().clicked(); + let mut chosen = Selection::new(Vec::new(), None, None); + let marked = List::::new(tl!("Marked content")) + .natural_height(true) + .select(SelectMode::Single) + .selection(Bind::bound(&mut chosen)) + .show_items(ui, &Positions(content.len()), &mut |key, _| ListRow::new(content.get(*key).map(content_label).unwrap_or_default())); + for event in &marked.events { + if let CollectionEvent::Selection(SelectionChange::Set(keys)) = event + && let Some(item) = keys.last().and_then(|key| content.get(*key)) + { + page = item.page.or(page); } } ( @@ -282,12 +352,3 @@ fn visible_tags<'a>(tags: &'a [TagInfo], query: &str, collapsed: &BTreeSet, /// The rule whose explanation is showing. pub explain: Option, + /// The rule whose context menu is open. + pub menu: Option, } #[derive(Clone, Debug, PartialEq)] @@ -72,271 +91,401 @@ pub(crate) fn fixable(rule: Rule) -> bool { matches!(rule, Rule::PrimaryLanguage | Rule::Title | Rule::TabOrder) } -pub(crate) fn options_body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens) -> (bool, bool) { - let pages = app.active_ids().and_then(|(_, id)| app.session.get(id)).map_or(1, |d| d.info.pages.len().max(1)); - let o = &mut app.a11y_options; - o.to = o.to.clamp(1, pages); - o.from = o.from.clamp(1, o.to); - ui.label(egui::RichText::new(tl!("Accessibility Checker Options")).font(theme::semibold(18.0))); - ui.add_space(8.0); - let group = |ui: &mut egui::Ui, title: &str, body: &mut dyn FnMut(&mut egui::Ui)| { - ui.label(egui::RichText::new(tl!(title)).font(theme::semibold(13.0))); - egui::Frame::new().fill(t.hover).corner_radius(egui::CornerRadius::same(6)).inner_margin(egui::Margin::same(10)).show(ui, |ui| { - ui.set_width(ui.available_width()); - body(ui); - }); - ui.add_space(8.0); - }; - group(ui, "Report Options", &mut |ui| { - ui.add(widgets::checkbox(&mut o.create_report, tl!("Create accessibility report"))); - }); - group(ui, "Page Range", &mut |ui| { - ui.horizontal(|ui| { - widgets::radio_value(ui, &mut o.all_pages, true, tl!("All pages in document")); - widgets::radio_value(ui, &mut o.all_pages, false, tl!("Pages from")); - ui.add_enabled(!o.all_pages, egui::DragValue::new(&mut o.from).range(1..=pages)); - ui.label(tl!("to")); - ui.add_enabled(!o.all_pages, egui::DragValue::new(&mut o.to).range(1..=pages)); - }); - }); - let on = o.rules.len(); - group(ui, &crate::i18n::fmt(tl!("Checking Options ({on} of 32 in all categories)"), &[("on", &on.to_string())]), &mut |ui| { - ui.horizontal(|ui| { - ui.label(tl!("Category:")); - craft_ui::ComboBox::from_id_salt("a11y-category").selected_text(tl!(o.category.label())).width(260.0).show_ui(ui, |ui| { - for c in Category::ALL { - ui.selectable_value(&mut o.category, c, tl!(c.label())); - } - }); - }); - ui.add_space(4.0); - for r in Rule::ALL.into_iter().filter(|r| r.category() == o.category) { - let mut checked = o.rules.contains(&r); - if ui.add(widgets::checkbox(&mut checked, tl!(r.description()))).changed() { - if checked { - o.rules.insert(r); - } else { - o.rules.remove(&r); +/// The key of a rule's check box in the options dialog. +fn rule_key(rule: Rule) -> String { + format!("rule.{rule:?}") +} + +/// The id of a category among the dialog's choices. +fn category_id(category: Category) -> String { + format!("{category:?}") +} + +/// The category the options dialog lists rules of. +fn shown_category(fields: &DialogFields) -> Category { + fields.text("category").and_then(|id| Category::ALL.into_iter().find(|c| category_id(*c) == id)).unwrap_or(Category::Document) +} + +/// The keys of the rules the options dialog lists now. +fn shown_rule_keys(fields: &DialogFields) -> Vec { + let category = shown_category(fields); + Rule::ALL.into_iter().filter(|r| r.category() == category).map(rule_key).collect() +} + +/// The options dialog's values at opening: the saved options, the range held to the document. +fn options_fields(o: &A11yOptions, pages: usize) -> DialogFields { + let to = o.to.clamp(1, pages); + let from = o.from.clamp(1, to); + let range = PageRange { scope: if o.all_pages { PageScope::All } else { PageScope::Range }, from, to: Some(to), ..PageRange::default() }; + let mut fields = DialogFields::new(); + fields.set("create_report", o.create_report); + fields.set("range", PageRangeOptions::field_value(&range)); + fields.set("category", category_id(o.category)); + for rule in Rule::ALL { + fields.set(rule_key(rule), o.rules.contains(&rule)); + } + fields.set("show_dialog", o.show_dialog); + fields +} + +/// The options dialog over its values. +fn options_dialog(fields: &mut DialogFields, pages: usize, current: usize) -> ParamDialog<'_> { + let category = shown_category(fields); + let on = Rule::ALL.into_iter().filter(|r| fields.bool(&rule_key(*r)) == Some(true)).count(); + let heading = crate::i18n::fmt( + tl!("Checking Options ({on} of {all} in all categories)"), + &[("on", &on.to_string()), ("all", &Rule::ALL.len().to_string())], + ); + let categories = Category::ALL.into_iter().map(|c| (category_id(c), tl!(c.label()).into())).collect(); + let mut params = vec![ + Param::section(tl!("Report Options")), + Param::new("create_report", tl!("Create accessibility report"), ParamKind::bool(false)), + PageRangeOptions::param("range", tl!("Page Range"), pages, current, PageRangeChoices::FromTo), + // Keyed apart from its changing label, so the section keeps its identity as the count moves. + Param::new("section:checking", heading, ParamKind::Section), + Param::new("category", tl!("Category:"), ParamKind::choice(categories, Some(category_id(Category::Document)))), + ]; + for rule in Rule::ALL { + params.push(Param::new(rule_key(rule), tl!(rule.description()), ParamKind::bool(true)).visible_when("category", category_id(rule.category()))); + } + params.push( + Param::custom("rules.all", tl!("Rules"), |ui, fields, _| { + let keys = shown_rule_keys(fields); + let all = Button::new(tl!("Select All")).show(ui); + let none = Button::new(tl!("Clear All")).show(ui); + if all.response().clicked() { + for key in &keys { + fields.set(key.clone(), true); } } - } - ui.add_space(4.0); - ui.horizontal(|ui| { - if ui.button(tl!("Select All")).clicked() { - o.rules.extend(Rule::ALL.into_iter().filter(|r| r.category() == o.category)); - } - if ui.button(tl!("Clear All")).clicked() { - let c = o.category; - o.rules.retain(|r| r.category() != c); - } - }); - }); - ui.add(widgets::checkbox(&mut o.show_dialog, tl!("Show this dialog when the Checker starts"))); - ui.add_space(10.0); - let (mut start, mut cancel) = (false, false); - ui.horizontal(|ui| { - ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - if widgets::pill_button(ui, tl!("Start Checking"), true).clicked() { - start = true; - } - if widgets::pill_button(ui, tl!("Cancel"), false).clicked() { - cancel = true; + if none.response().clicked() { + for key in &keys { + fields.set(key.clone(), false); + } } + all.response().union(none.response().clone()) }) - }); - (start, cancel) + .companions(shown_rule_keys(fields)), + ); + params.push(Param::new("show_dialog", tl!("Show this dialog when the Checker starts"), ParamKind::bool(true))); + ParamDialog::new("a11y.options", tl!("Accessibility Checker Options"), params).ok_label(tl!("Start Checking")) } -/// The red of failed checks (as for invalid signatures). -const FAILED: egui::Color32 = egui::Color32::from_rgb(0xD7, 0x37, 0x3F); +/// Opens the Accessibility Checker Options dialog over the saved options. +pub(crate) fn open_a11y_options(app: &mut PdfCraftApp, ctx: &egui::Context) { + let Some((index, id)) = app.active_ids() else { return }; + let Some(doc) = app.session.get(id) else { return }; + let pages = doc.info.pages.len().max(1); + let current = app.views.get(index).map_or(1, |v| v.current() + 1); + let fields = options_fields(&app.a11y_options, pages); + app.dialogs.open(HostedParamDialog::new(ctx, "a11y.options", fields, move |fields| options_dialog(fields, pages, current))); +} -fn status_icon(t: &Tokens, s: Status) -> (&'static str, egui::Color32) { +/// What the dialogs of this module answered: called once a pass, after the host ran. +pub(crate) fn pass(app: &mut PdfCraftApp) { + if let Some(DialogResult::Ok(result)) = app.dialogs.take_by_id::(dialog_id("a11y.options")) { + let Some((_, id)) = app.active_ids() else { return }; + let pages = app.session.get(id).map_or(1, |d| d.info.pages.len().max(1)); + let fields = &result.fields; + let o = &mut app.a11y_options; + o.create_report = fields.bool("create_report").unwrap_or(o.create_report); + o.show_dialog = fields.bool("show_dialog").unwrap_or(o.show_dialog); + o.category = shown_category(fields); + o.rules = Rule::ALL.into_iter().filter(|r| fields.bool(&rule_key(*r)).unwrap_or_else(|| o.rules.contains(r))).collect(); + // `FromTo` offers neither Current page nor a subset, so only All and Range arrive. + if let Some(range) = PageRangeOptions::value(fields, "range") { + o.all_pages = range.scope == PageScope::All; + o.from = range.from; + o.to = range.to.unwrap_or(pages); + } + // Starting again forgets the skips made in this document. + app.a11y_skipped.retain(|(doc, _)| *doc != id); + app.run_accessibility_check(); + } + if let Some(DialogResult::Ok(answer)) = app.dialogs.take::() { + app.save_alt_text(answer); + } +} + +/// The icon and tone of a rule's status. +fn status_icon(s: Status) -> (&'static str, Tone) { match s { - Status::Passed => ("circle-check", egui::Color32::from_rgb(0x2D, 0x9D, 0x4F)), - Status::Failed => ("circle-x", FAILED), - Status::Manual => ("circle-help", egui::Color32::from_rgb(0xD9, 0x8E, 0x04)), - Status::Skipped => ("circle-dashed", t.text_faint), + Status::Passed => ("circle-check", Tone::Good), + Status::Failed => ("circle-x", Tone::Bad), + Status::Manual => ("circle-help", Tone::Warn), + Status::Skipped => ("circle-dashed", Tone::Neutral), } } -/// The Accessibility Checker panel. -pub(crate) fn panel(ui: &mut egui::Ui, t: &Tokens, state: &mut A11yState, doc: DocId) -> Option { - let mut action = None; - let Some((_, report)) = state.report.as_ref().filter(|(d, _)| *d == doc) else { - ui.add_space(24.0); - ui.vertical_centered(|ui| { - ui.add(icons::image("accessibility", 32.0, t.text_faint)); - ui.add_space(6.0); - ui.label(egui::RichText::new(tl!("This document hasn't been checked yet.")).color(t.text_muted)); - ui.add_space(6.0); - if widgets::pill_button(ui, tl!("Check for accessibility"), true).clicked() { - action = Some(PanelAction::Options); - } - }); - return action; - }; - let failed = report.count(Status::Failed); - let manual = report.count(Status::Manual); - let skipped = report.count(Status::Skipped); - ui.horizontal(|ui| { - ui.label( - egui::RichText::new(if failed == 0 && manual == 0 && skipped == 0 { - tl!("No issues found").to_string() - } else if failed == 1 { - tl!("1 issue").to_string() - } else if failed > 1 { - crate::i18n::fmt(tl!("{n} issues"), &[("n", &failed.to_string())]) - } else if manual > 0 { - format!("{}: {manual}", tl!("Needs manual check")) - } else { - format!("{}: {skipped}", tl!("Skipped")) - }) - .color(t.text_muted), - ); - ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - if widgets::pill_button(ui, tl!("Check again"), false).clicked() { - action = Some(PanelAction::CheckAgain); - } - if widgets::pill_button(ui, tl!("Report"), false).on_hover_text(tl!("Show the accessibility report")).clicked() { - action = Some(PanelAction::ShowReport); - } - }); - }); - if failed > 0 && manual > 0 { - ui.label(egui::RichText::new(format!("{}: {manual}", tl!("Needs manual check"))).small().color(t.text_muted)); +/// What a row of the results list stands for. +enum LineKind { + Category { label: String, issues: usize }, + Rule { rule: Rule, status: Status }, + Finding { rule: Rule, index: usize }, +} + +/// One row of the results tree, in display order. +struct Line { + key: u64, + depth: usize, + /// `Some(open)` for a row with children. + group: Option, + kind: LineKind, +} + +/// The results rows as the list's display order. +struct Lines(Vec); + +/// Rule rows come after the categories' keys; finding rows after every rule's. +const RULE_KEY_BASE: u64 = 100; +const FINDING_KEY_BASE: u64 = 1_000_000; +const FINDING_KEY_STRIDE: u64 = 1_000_000; + +impl DisplayOrder for Lines { + fn len(&self) -> usize { + self.0.len() } - if let Some((_, previous)) = state.previous.as_ref().filter(|(d, _)| *d == doc) { - egui::CollapsingHeader::new(tl!("Compare")).default_open(true).show(ui, |ui| { - for status in [Status::Passed, Status::Failed, Status::Manual, Status::Skipped] { - ui.label(format!("{}: {} → {}", tl!(status.label()), previous.count(status), report.count(status))); - } - for current in &report.results { - let Some(prior) = previous.result(current.rule) else { continue }; - if prior.status != current.status || prior.findings.len() != current.findings.len() { - ui.label(format!( - "{}: {} ({}) → {} ({})", - tl!(current.rule.name()), - tl!(prior.status.label()), - prior.findings.len(), - tl!(current.status.label()), - current.findings.len() - )); - } - } - }); + fn key(&self, at: usize) -> u64 { + self.0.get(at).map_or(0, |line| line.key) } - ui.add_space(6.0); - for c in Category::ALL { + fn depth(&self, at: usize) -> usize { + self.0.get(at).map_or(0, |line| line.depth) + } + fn has_children(&self, at: usize) -> bool { + self.0.get(at).is_some_and(|line| line.group.is_some()) + } + fn open(&self, at: usize) -> bool { + self.0.get(at).is_some_and(|line| line.group == Some(true)) + } +} + +impl ItemSource for Lines {} + +fn rule_position(rule: Rule) -> u64 { + Rule::ALL.iter().position(|r| *r == rule).unwrap_or(0) as u64 +} + +/// The results tree: categories with their rules, and the findings of the expanded rules. +fn result_lines(report: &Report, collapsed: &BTreeSet, expanded: &BTreeSet) -> Lines { + let mut lines = Vec::new(); + for (position, c) in Category::ALL.into_iter().enumerate() { let rules: Vec<_> = report.in_category(c).collect(); let issues = rules.iter().filter(|r| r.status == Status::Failed).count(); - let open = !state.collapsed.contains(&c); - let head = if issues == 0 { + let open = !collapsed.contains(&c); + let label = if issues == 0 { tl!(c.label()).to_string() } else if issues == 1 { crate::i18n::fmt(tl!("{label} (1 issue)"), &[("label", tl!(c.label()))]) } else { crate::i18n::fmt(tl!("{label} ({n} issues)"), &[("label", tl!(c.label())), ("n", &issues.to_string())]) }; - let clicked = ui - .horizontal(|ui| { - let toggle = icons::button( - ui, - if open { "chevron-down" } else { "chevron-right" }, - 20.0, - false, - if open { tl!("Collapse") } else { tl!("Expand") }, - ) - .clicked(); - let color = if issues > 0 { FAILED } else { t.text }; - let l = ui.add(egui::Label::new(egui::RichText::new(&head).font(theme::semibold(13.0)).color(color)).sense(egui::Sense::click())); - toggle || l.clicked() - }) - .inner; - if clicked { - if open { - state.collapsed.insert(c); - } else { - state.collapsed.remove(&c); - } - } + lines.push(Line { key: position as u64, depth: 0, group: Some(open), kind: LineKind::Category { label, issues } }); if !open { continue; } for r in rules { - let (icon, color) = status_icon(t, r.status); - let row = ui - .horizontal(|ui| { - ui.add_space(24.0); - ui.add(icons::image(icon, 15.0, color)); - ui.add( - egui::Label::new( - egui::RichText::new(crate::i18n::fmt( - tl!("{rule} - {status}"), - &[("rule", tl!(r.rule.name())), ("status", tl!(r.status.label()))], - )) - .font(theme::regular(12.5)) - .color(t.text), - ) - .sense(egui::Sense::click()), - ) - }) - .inner; - if row.clicked() && !r.findings.is_empty() && !state.expanded.remove(&r.rule) { - state.expanded.insert(r.rule); - } - row.context_menu(|ui| { - ui.set_min_width(170.0); - if ui.add_enabled(fixable(r.rule) && r.status == Status::Failed, egui::Button::new(tl!("Fix"))).clicked() { - action = Some(PanelAction::Fix(r.rule)); - ui.close(); - } - if ui.add_enabled(r.status != Status::Skipped, egui::Button::new(tl!("Skip Rule"))).clicked() { - action = Some(PanelAction::Skip(r.rule)); - ui.close(); - } - if ui.button(tl!("Explain")).clicked() { - state.explain = if state.explain == Some(r.rule) { None } else { Some(r.rule) }; - ui.close(); - } - ui.separator(); - if ui.button(tl!("Check Again")).clicked() { - action = Some(PanelAction::CheckAgain); - ui.close(); - } - if ui.button(tl!("Show Report")).clicked() { - action = Some(PanelAction::ShowReport); - ui.close(); - } - if ui.button(tl!("Options…")).clicked() { - action = Some(PanelAction::Options); - ui.close(); - } + let at = rule_position(r.rule); + let shown = !r.findings.is_empty() && expanded.contains(&r.rule); + lines.push(Line { + key: RULE_KEY_BASE + at, + depth: 1, + group: (!r.findings.is_empty()).then_some(shown), + kind: LineKind::Rule { rule: r.rule, status: r.status }, }); - if state.explain == Some(r.rule) { - egui::Frame::NONE.inner_margin(egui::Margin { left: 46, right: 4, top: 0, bottom: 6 }).show(ui, |ui| { - ui.label(egui::RichText::new(tl!(r.rule.explanation())).small().color(t.text_muted)); - }); + if shown { + for index in 0..r.findings.len() { + lines.push(Line { + key: FINDING_KEY_BASE + at * FINDING_KEY_STRIDE + index as u64, + depth: 2, + group: None, + kind: LineKind::Finding { rule: r.rule, index }, + }); + } } - if state.expanded.contains(&r.rule) { - egui::Frame::NONE.inner_margin(egui::Margin { left: 46, right: 4, top: 0, bottom: 6 }).show(ui, |ui| { - ui.spacing_mut().item_spacing.y = 2.0; - for f in &r.findings { - // The checker hands over the sentence and the document's own words apart, so - // the sentence is said in the panel's language with those words still in it. - let said = f.said(&crate::i18n::words); - let l = ui.add(egui::Label::new(egui::RichText::new(said).small().color(t.text_muted)).sense(egui::Sense::click())); - if let Some(object) = f.object { - if l.on_hover_text(tl!("Tags")).clicked() { - action = Some(PanelAction::GoToTag(object, f.page, f.bbox)); - } - } else if let Some(p) = f.page - && l.on_hover_text(tl!("Go to the page")).clicked() - { - action = Some(PanelAction::GoTo(p, f.bbox)); + } + } + Lines(lines) +} + +/// What opening a finding does: its tag when it has one, else its page. +fn finding_action(report: &Report, rule: Rule, index: usize) -> Option { + let f = report.result(rule)?.findings.get(index)?; + if let Some(object) = f.object { + Some(PanelAction::GoToTag(object, f.page, f.bbox)) + } else { + f.page.map(|p| PanelAction::GoTo(p, f.bbox)) + } +} + +/// The summary line over the results. +fn summary(report: &Report) -> String { + let failed = report.count(Status::Failed); + let manual = report.count(Status::Manual); + let skipped = report.count(Status::Skipped); + if failed == 0 && manual == 0 && skipped == 0 { + tl!("No issues found").to_string() + } else if failed == 1 { + tl!("1 issue").to_string() + } else if failed > 1 { + crate::i18n::fmt(tl!("{n} issues"), &[("n", &failed.to_string())]) + } else if manual > 0 { + format!("{}: {manual}", tl!("Needs manual check")) + } else { + format!("{}: {skipped}", tl!("Skipped")) + } +} + +/// The Accessibility Checker panel. +pub(crate) fn panel(ui: &mut Ui, state: &mut A11yState, doc: DocId) -> Option { + let A11yState { report, previous, collapsed, expanded, explain, menu, .. } = state; + let mut action = None; + let Some((_, report)) = report.as_ref().filter(|(d, _)| *d == doc) else { + let mut notice = Notice::new(Tone::Neutral, tl!("This document hasn't been checked yet.")); + let icon = IconId::new("accessibility"); + if has_icon(&icon) { + notice = notice.icon(NoticeIcon::Custom(IconSpec::svg(icon))); + } + let shown = notice.action(NoticeAction::new(tl!("Check for accessibility"))).show(ui); + if shown.action == Some(0) { + action = Some(PanelAction::Options); + } + return action; + }; + let failed = report.count(Status::Failed); + let manual = report.count(Status::Manual); + Text::new(summary(report)).shade(Shade::Muted).show(ui); + if Button::new(tl!("Check again")).show(ui).response().clicked() { + action = Some(PanelAction::CheckAgain); + } + if Button::new(tl!("Report")).show(ui).response().clicked() { + action = Some(PanelAction::ShowReport); + } + if failed > 0 && manual > 0 { + Text::new(format!("{}: {manual}", tl!("Needs manual check"))).shade(Shade::Muted).show(ui); + } + if let Some((_, previous)) = previous.as_ref().filter(|(d, _)| *d == doc) { + Section::new(tl!("Compare")).show(ui, |ui, _| { + for status in [Status::Passed, Status::Failed, Status::Manual, Status::Skipped] { + Text::new(format!("{}: {} → {}", tl!(status.label()), previous.count(status), report.count(status))).show(ui); + } + for current in &report.results { + let Some(prior) = previous.result(current.rule) else { continue }; + if prior.status != current.status || prior.findings.len() != current.findings.len() { + Text::new(format!( + "{}: {} ({}) → {} ({})", + tl!(current.rule.name()), + tl!(prior.status.label()), + prior.findings.len(), + tl!(current.status.label()), + current.findings.len() + )) + .show(ui); + } + } + }); + } + + let lines = result_lines(report, collapsed, expanded); + let explained = *explain; + // A click on a finding is reported as a selection of it; the selection is not kept, so the same finding + // can be opened again. + let mut picked = Selection::new(Vec::new(), None, None); + let out = List::::new(tl!("Accessibility results")) + .tree(true) + .natural_height(true) + .select(SelectMode::Single) + .selection(Bind::bound(&mut picked)) + .show_items(ui, &lines, &mut |key, _| { + let Some(line) = lines.0.iter().find(|l| l.key == *key) else { return ListRow::new("") }; + let row = ListRow::new(match &line.kind { + LineKind::Category { label, .. } => label.clone(), + LineKind::Rule { rule, status } => { + crate::i18n::fmt(tl!("{rule} - {status}"), &[("rule", tl!(rule.name())), ("status", tl!(status.label()))]) + } + // The checker hands over the sentence and the document's own words apart, so the sentence is said in + // the panel's language with those words still in it. + LineKind::Finding { rule, index } => { + report.result(*rule).and_then(|r| r.findings.get(*index)).map(|f| f.said(&crate::i18n::words).to_string()).unwrap_or_default() + } + }) + .depth(line.depth); + let row = match line.group { + Some(open) => row.group(open), + None => row, + }; + match &line.kind { + LineKind::Category { issues, .. } if *issues > 0 => row.icon(IconSpec::svg(IconId::new("circle-x"))).tone(Tone::Bad), + LineKind::Category { .. } | LineKind::Finding { .. } => row, + LineKind::Rule { rule, status } => { + let (icon, tone) = status_icon(*status); + let row = row.icon(IconSpec::svg(IconId::new(icon))).tone(tone); + let row = if *status == Status::Skipped { row.ink(RowInk::Muted) } else { row }; + if explained == Some(*rule) { row.below(Text::new(tl!(rule.explanation())).shade(Shade::Muted), |_| {}) } else { row } + } + } + }); + for event in &out.events { + match event { + CollectionEvent::Open(change) => match lines.0.iter().find(|l| l.key == change.key).map(|l| &l.kind) { + Some(LineKind::Category { .. }) => { + if let Some(c) = Category::ALL.into_iter().nth(change.key as usize) { + if change.open { + collapsed.remove(&c); + } else { + collapsed.insert(c); } } - }); + } + Some(LineKind::Rule { rule, .. }) => { + if change.open { + expanded.insert(*rule); + } else { + expanded.remove(rule); + } + } + _ => {} + }, + CollectionEvent::Selection(SelectionChange::Set(keys)) => { + if let Some(LineKind::Finding { rule, index }) = keys.last().and_then(|k| lines.0.iter().find(|l| l.key == *k)).map(|l| &l.kind) { + action = finding_action(report, *rule, *index).or(action); + } } + CollectionEvent::Activate(key) => { + if let Some(LineKind::Finding { rule, index }) = lines.0.iter().find(|l| l.key == *key).map(|l| &l.kind) { + action = finding_action(report, *rule, *index).or(action); + } + } + CollectionEvent::ContextMenu(request) => { + if let Some(LineKind::Rule { rule, .. }) = lines.0.iter().find(|l| l.key == request.key).map(|l| &l.kind) { + *menu = Some(*rule); + } + } + _ => {} + } + } + if let Some(rule) = *menu { + let status = report.result(rule).map(|r| r.status); + let entries = [ + Entry::new(String::from("fix"), tl!("Fix")).enabled(fixable(rule) && status == Some(Status::Failed)), + Entry::new(String::from("skip"), tl!("Skip Rule")).enabled(status != Some(Status::Skipped)), + Entry::new(String::from("explain"), tl!("Explain")), + Entry::separator(), + Entry::new(String::from("again"), tl!("Check Again")), + Entry::new(String::from("report"), tl!("Show Report")), + Entry::new(String::from("options"), tl!("Options…")), + ]; + let shown = Menu::context(tl!("Accessibility results"), &out.response).pick(ui, &entries); + match shown.picked.as_deref() { + Some("fix") => action = Some(PanelAction::Fix(rule)), + Some("skip") => action = Some(PanelAction::Skip(rule)), + Some("explain") => *explain = if *explain == Some(rule) { None } else { Some(rule) }, + Some("again") => action = Some(PanelAction::CheckAgain), + Some("report") => action = Some(PanelAction::ShowReport), + Some("options") => action = Some(PanelAction::Options), + _ => {} + } + if shown.closed.is_some() { + *menu = None; } } action @@ -365,9 +514,10 @@ impl PdfCraftApp { } /// Check for accessibility: the options first, unless they are turned off. - pub(crate) fn start_accessibility_check(&mut self) { + // PLAN: takes the context the options dialog needs; pdf-hub-w3 passes it from `commands.rs`. + pub(crate) fn start_accessibility_check(&mut self, ctx: &egui::Context) { if self.a11y_options.show_dialog { - self.dialog = Some(Dialog::AccessibilityOptions); + open_a11y_options(self, ctx); } else { self.run_accessibility_check(); } @@ -448,10 +598,11 @@ impl PdfCraftApp { } } - pub(crate) fn a11y_action(&mut self, index: usize, action: PanelAction) { + // PLAN: takes `ctx` for the dialogs it opens; pdf-chrome-w13 passes `ui.ctx()` from `panels/right.rs`. + pub(crate) fn a11y_action(&mut self, ctx: &egui::Context, index: usize, action: PanelAction) { match action { PanelAction::CheckAgain => self.run_accessibility_check(), - PanelAction::Options => self.dialog = Some(Dialog::AccessibilityOptions), + PanelAction::Options => open_a11y_options(self, ctx), PanelAction::ShowReport => self.show_accessibility_report(), PanelAction::GoTo(p, bbox) => self.a11y_show_on_page(index, p, bbox), PanelAction::GoToTag(object, page, bbox) => { @@ -485,7 +636,7 @@ impl PdfCraftApp { let Some((_, id)) = self.active_ids() else { return }; match rule { // The language is chosen in Document Properties ▸ Advanced. - Rule::PrimaryLanguage => self.dialog = Some(Dialog::Properties(PropsTab::Advanced)), + Rule::PrimaryLanguage => self.open_properties(ctx, PropsTab::Advanced), _ => { let edit = self.session.get(id).map(|d| d.accessibility_fix(rule, None)); match edit { @@ -496,7 +647,7 @@ impl PdfCraftApp { self.run_accessibility_check(); // The title can then be edited. if rule == Rule::Title { - self.dialog = Some(Dialog::Properties(PropsTab::Description)); + self.open_properties(ctx, PropsTab::Description); } } Some(Err(e)) => self.notify_error(e), @@ -509,32 +660,28 @@ impl PdfCraftApp { } } -/// Add alternate text: the figures one by one. -#[derive(Clone, Default)] -pub struct AltDraft { - pub doc: Option, - pub figures: Vec, - pub index: usize, - pub texts: Vec, - pub decorative: Vec, - /// The figure shown (index, picture). - preview: Option<(usize, egui::TextureHandle)>, +/// Where a figure sits on its page, in the page's view space, for its preview. +struct FigureShot { + page: usize, + x0: f32, + y0: f32, + w: f32, + h: f32, } -impl std::fmt::Debug for AltDraft { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("AltDraft").field("figures", &self.figures.len()).field("index", &self.index).finish() - } -} - -/// Render a figure's area for the dialog, at the preview pane's device resolution. -fn figure_picture(ctx: &egui::Context, doc: &pdfcraft_engine::Document, f: &pdfcraft_engine::a11y::Figure) -> Option { +fn figure_shot(doc: &pdfcraft_engine::Document, f: &Figure) -> Option { let (page, b) = (f.page?, f.bbox?); let info = doc.info.pages.get(page)?; let (u, v) = (info.user_to_view(b[0] as f32, b[1] as f32), info.user_to_view(b[2] as f32, b[3] as f32)); let (x0, y0, x1, y1) = (u[0].min(v[0]), u[1].min(v[1]), u[0].max(v[0]), u[1].max(v[1])); - let (w, h) = ((x1 - x0).max(1.0), (y1 - y0).max(1.0)); - let ppp = ctx.pixels_per_point().max(1.0); + Some(FigureShot { page, x0, y0, w: (x1 - x0).max(1.0), h: (y1 - y0).max(1.0) }) +} + +/// Render a figure's area for the dialog, at the preview's device resolution; a blank picture when it can't be drawn. +fn figure_image(bytes: &Arc>, password: Option<&str>, shot: Option<&FigureShot>, ppp: f32) -> egui::ColorImage { + let blank = || egui::ColorImage::filled([1, 1], egui::Color32::TRANSPARENT); + let Some(FigureShot { page, x0, y0, w, h }) = shot else { return blank() }; + let ppp = ppp.max(1.0); let scale = ((380.0 * ppp) / w).min((220.0 * ppp) / h).clamp(0.05, 32.0); let tile = pdfcraft_render::Tile { x: (x0 * scale).floor().max(0.0) as u32, @@ -542,12 +689,12 @@ fn figure_picture(ctx: &egui::Context, doc: &pdfcraft_engine::Document, f: &pdfc w: (w * scale).ceil().max(1.0) as u32, h: (h * scale).ceil().max(1.0) as u32, }; - // The picture is only shown in the panel, so it draws like the viewer (installed CJK faces + // The picture is only shown in the dialog, so it draws like the viewer (installed CJK faces // for fonts the PDF doesn't embed); nothing is saved from it (AGENTS.md §1.4). - let config = pdfcraft_render::RenderConfig { password: doc.password.as_deref().map(std::sync::Arc::from), ..Default::default() }.on_screen(); - let mut r = pdfcraft_render::PageRenderer::new(doc.bytes.clone(), config); + let config = pdfcraft_render::RenderConfig { password: password.map(Arc::from), ..Default::default() }.on_screen(); + let mut r = pdfcraft_render::PageRenderer::new(bytes.clone(), config); let out = r.render(pdfcraft_render::RenderRequest { - page, + page: *page, kind: pdfcraft_render::RequestKind::Pixels, tile: Some(tile), scale, @@ -555,79 +702,134 @@ fn figure_picture(ctx: &egui::Context, doc: &pdfcraft_engine::Document, f: &pdfc preview: false, }); if out.error.is_some() || out.width == 0 { - return None; + return blank(); } - let img = egui::ColorImage::from_rgba_premultiplied([out.width as usize, out.height as usize], &out.rgba); - Some(ctx.load_texture("alt-figure", img, egui::TextureOptions::LINEAR)) + egui::ColorImage::from_rgba_premultiplied([out.width as usize, out.height as usize], &out.rgba) } -pub(crate) fn alt_body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens) -> (bool, bool) { - let ctx = ui.ctx().clone(); - let doc = app.alt_draft.doc.and_then(|id| app.session.get(id)); - let d = &mut app.alt_draft; - ui.label(egui::RichText::new(tl!("Set Alternate Text")).font(theme::semibold(18.0))); - ui.add_space(8.0); - let n = d.figures.len(); - if n == 0 { - ui.label(egui::RichText::new(tl!("This document has no tagged figures.")).color(t.text_muted)); - let mut cancel = false; - ui.horizontal(|ui| { - ui.with_layout(Layout::right_to_left(Align::Center), |ui| cancel = widgets::pill_button(ui, tl!("Close"), true).clicked()) - }); - return (false, cancel); +/// The figure the Set Alternate Text dialog is on and what has been typed for each figure. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct AltTextValue { + index: usize, + texts: Vec, + decorative: Vec, +} + +/// What Save & Close answers: the figures as they were and what was set for them. +pub struct AltTextAnswer { + figures: Vec
, + value: AltTextValue, +} + +/// Add alternate text: the figures one by one. +pub struct AltTextDialog { + id: DialogId, + figures: Vec
, + shots: Vec>, + /// The document's bytes and password, which draw the previews. + bytes: Arc>, + password: Option, + /// The document and its edit generation, which keep a figure's preview apart from another document's. + doc: DocId, + generation: u64, + value: AltTextValue, + open: bool, +} + +impl StackDialog for AltTextDialog { + type Result = AltTextAnswer; + + fn id(&self) -> DialogId { + self.id } - d.index = d.index.min(n - 1); - let i = d.index; - if d.preview.as_ref().is_none_or(|(k, _)| *k != i) - && let Some(doc) = doc - { - d.preview = figure_picture(&ctx, doc, &d.figures[i]).map(|tex| (i, tex)); + + fn key(&self) -> Option<&str> { + Some("a11y.alt_text") } - let page = d.figures[i].page.map(|p| crate::i18n::fmt(tl!(" on page {p}"), &[("p", &(p + 1).to_string())])).unwrap_or_default(); - ui.label( - egui::RichText::new(crate::i18n::fmt(tl!("Figure {i} of {n}{page}"), &[("i", &(i + 1).to_string()), ("n", &n.to_string()), ("page", &page)])) - .color(t.text_muted), - ); - ui.add_space(6.0); - egui::Frame::new().fill(t.hover).corner_radius(egui::CornerRadius::same(6)).inner_margin(egui::Margin::same(8)).show(ui, |ui| { - let (area, _) = ui.allocate_exact_size(egui::vec2(380.0, 220.0), egui::Sense::hover()); - match &d.preview { - Some((_, tex)) => { - let s = tex.size_vec2(); - let k = (area.width() / s.x).min(area.height() / s.y).min(2.0); - let fit = egui::Rect::from_center_size(area.center(), s * k); - ui.painter().image(tex.id(), fit, egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1.0, 1.0)), egui::Color32::WHITE); - } - None => { - ui.painter().text(area.center(), egui::Align2::CENTER_CENTER, tl!("No preview"), theme::regular(13.0), t.text_faint); - } + + fn show(&mut self, ui: &mut Ui, _: &DialogStack) -> Option> { + let AltTextDialog { figures, shots, bytes, password, doc, generation, value, open, .. } = self; + let ppp = ui.ctx().pixels_per_point(); + let n = figures.len(); + let shown = FormDialog::new(tl!("Set Alternate Text"), Bind::bound(&mut *value)) + .opened(Bind::bound(&mut *open)) + .ok_label(tl!("Save & Close")) + .show(ui, |ui, _, value| { + let i = value.index.min(n.saturating_sub(1)); + value.index = i; + let (Some(figure), Some(text), Some(decorative)) = (figures.get(i), value.texts.get_mut(i), value.decorative.get_mut(i)) else { + return true; + }; + Stack::new().show(ui, |ui, _| { + let page = figure.page.map(|p| crate::i18n::fmt(tl!(" on page {p}"), &[("p", &(p + 1).to_string())])).unwrap_or_default(); + Text::new(crate::i18n::fmt( + tl!("Figure {i} of {n}{page}"), + &[("i", &(i + 1).to_string()), ("n", &n.to_string()), ("page", &page)], + )) + .shade(Shade::Muted) + .show(ui); + // The picture renders once per key: the dialog keeps no image of its own. + let key = { + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + (*doc, *generation, figure.obj.num).hash(&mut hasher); + hasher.finish() + }; + let shot = shots.get(i).and_then(Option::as_ref); + let render = || figure_image(bytes, password.as_deref(), shot, ppp); + Picture::new(tl!("Figure"), key, &render).aspect(ThumbAspect::new(19, 11)).extent(PictureSize::Sample(ThumbSize::Large)).show(ui); + let mut alt = AltText::new(text.clone()).decorative(*decorative); + AltTextField::new(tl!("Alternate text"), Bind::bound(&mut alt)).show(ui); + if let Some(typed) = alt.text.same() { + *text = typed.clone(); + } + if let Some(on) = alt.decorative.same() { + *decorative = *on; + } + let back = IconButton::new(IconSpec::svg(IconId::new("chevron-left"))).label(tl!("Previous figure")).enabled(i > 0).show(ui); + let next = IconButton::new(IconSpec::svg(IconId::new("chevron-right"))).label(tl!("Next figure")).enabled(i + 1 < n).show(ui); + if back.response().clicked() { + value.index = i.saturating_sub(1); + } + if next.response().clicked() { + value.index = (i + 1).min(n.saturating_sub(1)); + } + }); + true + }); + match (shown.accepted, shown.closed) { + (Some(value), _) => Some(DialogResult::Ok(AltTextAnswer { figures: figures.clone(), value })), + (None, Some(_)) => Some(DialogResult::Cancel), + (None, None) => None, } - }); - ui.add_space(8.0); - ui.add_enabled_ui(!d.decorative[i], |ui| { - let l = ui.label(tl!("Alternate text")); - ui.add(crate::widgets::block(&mut d.texts[i]).desired_rows(3).desired_width(380.0).hint_text(tl!("Describe the figure"))).labelled_by(l.id); - }); - ui.add(widgets::checkbox(&mut d.decorative[i], tl!("Decorative figure"))); - ui.add_space(10.0); - let (mut save, mut cancel) = (false, false); - ui.horizontal(|ui| { - if ui.add_enabled_ui(i > 0, |ui| icons::button(ui, "chevron-left", 28.0, false, tl!("Previous figure"))).inner.clicked() { - d.index -= 1; - } - if ui.add_enabled_ui(i + 1 < n, |ui| icons::button(ui, "chevron-right", 28.0, false, tl!("Next figure"))).inner.clicked() { - d.index += 1; - } - ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - if widgets::pill_button(ui, tl!("Save & Close"), true).clicked() { - save = true; - } - if widgets::pill_button(ui, tl!("Cancel"), false).clicked() { - cancel = true; - } - }); - }); - (save, cancel) + } +} + +/// Opens Set Alternate Text over the figures of the active document, or says it has none. +pub(crate) fn open_alt_text(app: &mut PdfCraftApp, ctx: &egui::Context) { + let Some((_, id)) = app.active_ids() else { return }; + let Some(doc) = app.session.get(id) else { return }; + let figures = doc.figures(); + if figures.is_empty() { + app.dialogs.open(HostedAlert::new(Alert::new(tl!("Set Alternate Text"), tl!("This document has no tagged figures.")))); + return; + } + let dialog = AltTextDialog { + id: dialog_id("a11y.alt_text"), + shots: figures.iter().map(|f| figure_shot(doc, f)).collect(), + bytes: doc.bytes.clone(), + password: doc.password.clone(), + doc: id, + generation: doc.edit_generation(), + value: AltTextValue { + index: 0, + texts: figures.iter().map(|f| f.alt.clone().unwrap_or_default()).collect(), + decorative: vec![false; figures.len()], + }, + figures, + open: true, + }; + // A second request while the dialog is open leaves it as it is. + let _ = app.dialogs.open_unless_open_in(ctx, dialog); } impl PdfCraftApp { @@ -637,31 +839,16 @@ impl PdfCraftApp { self.choose_right_panel(Some(RightPanel::Tags)); } - /// Prepare for accessibility ▸ Add alternate text. - pub(crate) fn start_alt_text(&mut self) { - let Some((_, id)) = self.active_ids() else { return }; - let Some(doc) = self.session.get(id) else { return }; - let figures = doc.figures(); - self.alt_draft = AltDraft { - doc: Some(id), - texts: figures.iter().map(|f| f.alt.clone().unwrap_or_default()).collect(), - decorative: vec![false; figures.len()], - figures, - index: 0, - preview: None, - }; - self.dialog = Some(Dialog::AltText); - } - /// Save & Close: one undo step for every change. - pub(crate) fn save_alt_text(&mut self) { - let d = std::mem::take(&mut self.alt_draft); + pub(crate) fn save_alt_text(&mut self, answer: AltTextAnswer) { + let AltTextAnswer { figures, value: d } = answer; let mut edits = Vec::new(); - for (k, f) in d.figures.iter().enumerate() { - if d.decorative[k] { + for (k, f) in figures.iter().enumerate() { + let (Some(text), Some(decorative)) = (d.texts.get(k), d.decorative.get(k)) else { continue }; + if *decorative { edits.push(pdfcraft_engine::Edit::MarkDecorative { figure: f.obj.num }); - } else if d.texts[k].trim() != f.alt.as_deref().unwrap_or("").trim() { - edits.push(pdfcraft_engine::Edit::SetAltText { figure: f.obj.num, alt: Some(d.texts[k].clone()) }); + } else if text.trim() != f.alt.as_deref().unwrap_or("").trim() { + edits.push(pdfcraft_engine::Edit::SetAltText { figure: f.obj.num, alt: Some(text.clone()) }); } } if !edits.is_empty() { diff --git a/crates/ui-egui/tests/it/accessibility.rs b/crates/ui-egui/tests/it/accessibility.rs index eebfc40020..cd2b67b69a 100644 --- a/crates/ui-egui/tests/it/accessibility.rs +++ b/crates/ui-egui/tests/it/accessibility.rs @@ -1,11 +1,14 @@ //! Prepare for accessibility ▸ Check for accessibility in the real shell (egui_kittest): the //! options dialog, the results panel, Fix, Skip Rule and the report. +use std::sync::Arc; + +use craft::host::ScriptedFiles; use egui::accesskit::Role; use egui_kittest::Harness; use egui_kittest::kittest::Queryable; use pdfcraft_engine::a11y::{Rule, Status}; -use pdfcraft_ui_egui::{Dialog, PdfCraftApp, RightPanel}; +use pdfcraft_ui_egui::{PdfCraftApp, RightPanel}; const FIXTURE: &[u8] = b"%PDF-1.7 1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj @@ -60,15 +63,14 @@ fn automatic_text_tagging_opens_reviewable_tags() { #[test] fn checking_fixing_skipping_and_reporting() { - let dir = std::env::temp_dir().join(format!("pdfcraft-a11y-ui-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).unwrap(); - let d = dir.clone(); + let files = Arc::new(ScriptedFiles::new().saving_suggested()); + let scripted = files.clone(); 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("notes.pdf", None, FIXTURE.to_vec()).unwrap(); - app.export_dir_override = Some(d.to_string_lossy().into_owned()); + // PLAN: assumed `PdfCraftApp::set_files(Arc)` (pdf-workflows-w13, A-6) + app.set_files(scripted); app }); h.run_steps(4); @@ -79,7 +81,7 @@ fn checking_fixing_skipping_and_reporting() { h.get_by_label("Document title is showing in title bar"); h.get_by_label("Start Checking").click(); h.run_steps(3); - assert_eq!(h.state().dialog, None); + assert!(h.state().dialogs.is_empty()); assert_eq!(h.state().right, Some(RightPanel::Accessibility)); h.get_by_label("Document (3 issues)"); h.get_by_label("Title - Failed"); @@ -90,10 +92,11 @@ fn checking_fixing_skipping_and_reporting() { h.run_steps(3); assert_eq!(status(&h, Rule::Title), Status::Passed); assert_eq!(h.state().a11y.previous.as_ref().unwrap().1.result(Rule::Title).unwrap().status, Status::Failed); - assert!(matches!(h.state().dialog, Some(Dialog::Properties(_)))); + assert!(!h.state().dialogs.is_empty(), "Document Properties opens to edit the title"); assert_eq!(h.state().window_title, "notes • — PdfCraft"); - h.state_mut().dialog = None; + h.get_by_label("Cancel").click(); h.run_steps(2); + assert!(h.state().dialogs.is_empty()); h.get_by_label("Compare"); h.get_by_label_contains("Title: Failed (2) → Passed (0)"); // Skip a rule; it stays skipped when checking again. @@ -110,12 +113,18 @@ fn checking_fixing_skipping_and_reporting() { h.run_steps(2); h.get_by_label_contains("Screen readers choose their voice"); h.get_by_label("Primary language - Failed").click(); + h.key_press(egui::Key::ArrowRight); h.run_steps(2); h.get_by_label("No document language is set"); // The report. h.get_by_label("Report").click(); h.run_steps(2); - let html = std::fs::read_to_string(dir.join("notes Accessibility Report.html")).unwrap(); + let requests = files.requests(); + assert_eq!(requests.len(), 1, "the report asks where to be saved once"); + assert_eq!(requests[0].file_name.as_deref(), Some("notes Accessibility Report.html")); + // PLAN: assumed the scripted save lands at the suggested directory and name (pdf-workflows-w13, A-6) + let saved = requests[0].directory.clone().unwrap_or_default().join("notes Accessibility Report.html"); + let html = String::from_utf8(files.file(&saved).expect("the report was written")).unwrap(); assert!(html.contains("Accessibility Report") && html.contains("Skipped")); } @@ -142,7 +151,7 @@ fn a_rule_skipped_in_one_document_is_not_skipped_in_another() { }; assert!(h.state_mut().execute("a11y.check")); h.run_steps(3); - assert_eq!(h.state().dialog, None); + assert!(h.state().dialogs.is_empty(), "no options dialog while it is turned off"); right_click(&mut h, "Tagged PDF - Failed"); h.get_by_label("Skip Rule").click(); h.run_steps(2); @@ -152,7 +161,7 @@ fn a_rule_skipped_in_one_document_is_not_skipped_in_another() { h.run_steps(2); assert!(h.state_mut().execute("a11y.check")); h.run_steps(3); - assert_eq!(h.state().dialog, None); + assert!(h.state().dialogs.is_empty()); assert_eq!(tagged(&h), (Status::Failed, 2), "b.pdf's own result"); // Starting again from the options dialog forgets b.pdf's skips, not a.pdf's. right_click(&mut h, "Tagged PDF - Failed"); @@ -162,6 +171,7 @@ fn a_rule_skipped_in_one_document_is_not_skipped_in_another() { h.state_mut().a11y_options.show_dialog = true; assert!(h.state_mut().execute("a11y.check")); h.run_steps(2); + assert!(!h.state().dialogs.is_empty(), "the options dialog opens"); h.get_by_label("Start Checking").click(); h.run_steps(3); assert_eq!(tagged(&h), (Status::Failed, 2)); @@ -270,7 +280,9 @@ fn checker_shows_a_figure_finding_on_its_page() { h.run_steps(4); h.state_mut().run_accessibility_check(); h.run_steps(3); + // The rule opens as a user opens a tree row: select it, then Right. h.get_by_label("Figures alternate text - Failed").click(); + h.key_press(egui::Key::ArrowRight); h.run_steps(2); h.get_by_label_contains("Figure element on page 1: no alternate text").click(); h.run_steps(1); @@ -291,12 +303,14 @@ fn setting_alternate_text_figure_by_figure() { h.run_steps(2); h.get_by_label("Set Alternate Text"); h.get_by_label("Figure 1 of 2 on page 1"); - h.state_mut().alt_draft.texts[0] = "A grey square".into(); + let field = h.get_by_role_and_label(Role::TextInput, "Alternate text"); + field.focus(); + field.type_text("A grey square"); h.run_steps(2); h.get_by_label("Next figure").click(); h.run_steps(2); h.get_by_label("Figure 2 of 2 on page 1"); - h.get_by_label("Decorative figure").click(); + h.get_by_label_contains("decorative").click(); h.run_steps(1); h.get_by_label("Save & Close").click(); h.run_steps(3); @@ -320,10 +334,12 @@ fn tags_panel_edits_a_tag_property_and_undoes_it() { assert!(h.state_mut().execute("a11y.tags")); h.run_steps(2); assert_eq!(h.state().right, Some(RightPanel::Tags)); - h.get_by_label("Collapse tag").click(); + // The list's own disclosure: Left closes the selected group, Right opens it. + h.get_by_label("Document · 6").click(); + h.key_press(egui::Key::ArrowLeft); h.run_steps(2); assert!(h.query_by_label("Figure · 7").is_none()); - h.get_by_label("Expand tag").click(); + h.key_press(egui::Key::ArrowRight); h.run_steps(2); h.get_by_label("Figure · 7").click(); h.run_steps(2);