mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-26 04:58:07 +00:00
pdf-dialogs-w8: net change of 3 commits
- pdf-dialogs-w8: written from its sheet - pdf-dialogs-w8: repair 1 - pdf-dialogs-w8: review fixes (review-1)
This commit is contained in:
@@ -1,265 +0,0 @@
|
||||
//! Preferences › Appearance: the appearance mode (follow the system, light or dark) above a light
|
||||
//! and a dark theme card, each with a small picture of PdfCraft in that theme.
|
||||
|
||||
use egui::{CornerRadius, Rect, RichText, Stroke, pos2, vec2};
|
||||
|
||||
use crate::PdfCraftApp;
|
||||
use crate::theme::{self, ThemeKind, ThemePreference, Tokens};
|
||||
|
||||
/// The themes each card offers. One of each today; a new theme is one more row here.
|
||||
pub const LIGHT_THEMES: &[(ThemeKind, &str)] = &[(ThemeKind::Light, "Light gray")];
|
||||
pub const DARK_THEMES: &[(ThemeKind, &str)] = &[(ThemeKind::Dark, "Dark gray")];
|
||||
|
||||
fn mode_label(preference: ThemePreference) -> &'static str {
|
||||
match preference {
|
||||
ThemePreference::System => "Use system setting",
|
||||
ThemePreference::Light => "Light",
|
||||
ThemePreference::Dark => "Dark",
|
||||
}
|
||||
}
|
||||
|
||||
fn mode_command(preference: ThemePreference) -> &'static str {
|
||||
match preference {
|
||||
ThemePreference::System => "view.theme.system",
|
||||
ThemePreference::Light => "view.theme.light",
|
||||
ThemePreference::Dark => "view.theme.dark",
|
||||
}
|
||||
}
|
||||
|
||||
/// A miniature PdfCraft window in `kind`'s colours: title bar and tabs, the tool bar, the page
|
||||
/// thumbnails, a page on the pasteboard and the right-hand panel.
|
||||
fn preview(ui: &mut egui::Ui, kind: ThemeKind, width: f32) {
|
||||
let p = Tokens::for_kind(kind);
|
||||
let (rect, _) = ui.allocate_exact_size(vec2(width, 118.0), egui::Sense::hover());
|
||||
let painter = ui.painter_at(rect);
|
||||
painter.rect_filled(rect, 3.0, p.chrome);
|
||||
let title = Rect::from_min_size(rect.min, vec2(rect.width(), 12.0));
|
||||
painter.rect_filled(title, 0.0, p.titlebar);
|
||||
// The active tab and the theme button.
|
||||
painter.rect_filled(Rect::from_min_size(title.min + vec2(6.0, 3.0), vec2(36.0, 9.0)), 2.0, p.panel);
|
||||
painter.rect_filled(Rect::from_min_size(title.min + vec2(9.0, 6.0), vec2(20.0, 2.0)), 1.0, p.text);
|
||||
painter.circle_filled(pos2(title.right() - 7.0, title.center().y), 2.5, p.icon);
|
||||
// The tool bar, with its first tool active.
|
||||
let tools = Rect::from_min_size(pos2(rect.left(), title.bottom()), vec2(rect.width(), 12.0));
|
||||
painter.rect_filled(tools, 0.0, p.panel);
|
||||
for i in 0..8 {
|
||||
let c = if i == 0 { p.accent } else { p.icon };
|
||||
painter.rect_filled(Rect::from_min_size(tools.min + vec2(8.0 + i as f32 * 9.0, 3.5), vec2(5.0, 5.0)), 1.0, c);
|
||||
}
|
||||
let body = Rect::from_min_max(pos2(rect.left(), tools.bottom() + 1.0), rect.max);
|
||||
// Page thumbnails on the left, the second one selected.
|
||||
let rail = Rect::from_min_size(body.min, vec2((body.width() * 0.16).round(), body.height()));
|
||||
painter.rect_filled(rail, 0.0, p.panel);
|
||||
for i in 0..3 {
|
||||
let thumb = Rect::from_min_size(rail.min + vec2(rail.width() * 0.25, 6.0 + i as f32 * 26.0), vec2(rail.width() * 0.5, 20.0));
|
||||
if i == 1 {
|
||||
painter.rect_filled(thumb.expand(2.0), 2.0, p.accent_soft);
|
||||
}
|
||||
painter.rect_filled(thumb, 1.0, egui::Color32::WHITE);
|
||||
painter.rect_stroke(thumb, 1.0, egui::Stroke::new(0.5, p.border), egui::StrokeKind::Outside);
|
||||
}
|
||||
// The right-hand panel: a heading and a few rows.
|
||||
let side = Rect::from_min_max(pos2(body.right() - (body.width() * 0.22).round(), body.top()), body.max);
|
||||
painter.rect_filled(side, 0.0, p.panel);
|
||||
painter.rect_filled(Rect::from_min_size(side.min + vec2(5.0, 5.0), vec2(side.width() * 0.5, 2.5)), 1.0, p.text);
|
||||
for i in 0..4 {
|
||||
let y = side.top() + 14.0 + i as f32 * 8.0;
|
||||
painter.rect_filled(Rect::from_min_size(pos2(side.left() + 5.0, y), vec2(side.width() * 0.7, 2.0)), 1.0, p.text_muted);
|
||||
}
|
||||
// The page, white in both themes, with a heading and lines of text.
|
||||
let paste = Rect::from_min_max(pos2(rail.right(), body.top()), pos2(side.left(), body.bottom()));
|
||||
painter.rect_filled(paste, 0.0, p.pasteboard);
|
||||
let page_h = paste.height() - 10.0;
|
||||
let page = Rect::from_center_size(paste.center(), vec2(page_h * 0.77, page_h));
|
||||
painter.rect_filled(page.translate(vec2(1.0, 1.0)), 1.0, p.page_shadow);
|
||||
painter.rect_filled(page, 1.0, egui::Color32::WHITE);
|
||||
let ink = egui::Color32::from_gray(0x60);
|
||||
painter.rect_filled(Rect::from_min_size(page.min + vec2(6.0, 7.0), vec2(page.width() * 0.5, 3.0)), 1.0, egui::Color32::from_gray(0x30));
|
||||
for i in 0..8 {
|
||||
let w = if i % 4 == 3 { 0.45 } else { 0.8 };
|
||||
painter.rect_filled(Rect::from_min_size(page.min + vec2(6.0, 15.0 + i as f32 * 6.0), vec2(page.width() * w - 6.0, 1.5)), 0.5, ink);
|
||||
}
|
||||
}
|
||||
|
||||
/// One theme card: its title, Active when it is what shows now, and a preview of the chosen theme.
|
||||
/// Returns whether the card was clicked and the control's Response.
|
||||
fn card(ui: &mut egui::Ui, t: &Tokens, title: &str, active: bool, themes: &[(ThemeKind, &str)], query: &str) -> (bool, egui::Response) {
|
||||
let mut card_clicked = false;
|
||||
let frame_stroke = if active { Stroke::new(1.5, t.accent) } else { Stroke::new(1.0, t.border) };
|
||||
let scope_res = ui.scope_builder(egui::UiBuilder::new().id_salt(title).sense(egui::Sense::click()), |ui| {
|
||||
egui::Frame::new().fill(t.card).stroke(frame_stroke).corner_radius(CornerRadius::same(6)).inner_margin(egui::Margin::same(8)).show(
|
||||
ui,
|
||||
|ui| {
|
||||
ui.set_width(ui.available_width());
|
||||
ui.horizontal(|ui| {
|
||||
let font = theme::semibold(12.5);
|
||||
let ranges = crate::dialogs::preferences::find_match_ranges(title, query);
|
||||
let galley = if ranges.is_empty() {
|
||||
crate::bidi::layout_ui(ui, title.to_owned(), font, t.text)
|
||||
} else {
|
||||
let (job, _) = crate::dialogs::preferences::create_highlight_job(title, &ranges, font, t.text);
|
||||
ui.painter().layout_job(job)
|
||||
};
|
||||
let (title_rect, _) = ui.allocate_exact_size(galley.size(), egui::Sense::hover());
|
||||
ui.painter().galley(title_rect.min, galley, t.text);
|
||||
if active {
|
||||
ui.label(RichText::new(tl!("Active")).color(t.accent_text).small());
|
||||
}
|
||||
});
|
||||
ui.add_space(4.0);
|
||||
let shown = themes.first().map_or(ThemeKind::Light, |(kind, _)| *kind);
|
||||
preview(ui, shown, ui.available_width());
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
let resp = scope_res.response;
|
||||
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::RadioButton, true, active, title));
|
||||
|
||||
if resp.has_focus() {
|
||||
ui.painter().rect_stroke(resp.rect, CornerRadius::same(6), Stroke::new(2.0, t.accent_text), egui::StrokeKind::Outside);
|
||||
}
|
||||
|
||||
let resp = theme::hand(resp);
|
||||
let key_select = resp.has_focus() && ui.input(|i| i.key_pressed(egui::Key::Space) || i.key_pressed(egui::Key::Enter));
|
||||
if resp.clicked() || key_select {
|
||||
card_clicked = true;
|
||||
}
|
||||
(card_clicked, resp)
|
||||
}
|
||||
|
||||
/// The Appearance section of Preferences.
|
||||
#[cfg_attr(not(test), expect(dead_code, reason = "called by unit tests"))]
|
||||
pub fn preferences_section(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens) {
|
||||
let mut ctx = crate::dialogs::preferences::PrefSearchContext::new("", false);
|
||||
preferences_section_with_context(ui, app, t, &mut ctx);
|
||||
}
|
||||
|
||||
/// The Appearance section of Preferences with live search highlighting and scrolling context.
|
||||
pub(crate) fn preferences_section_with_context(
|
||||
ui: &mut egui::Ui,
|
||||
app: &mut PdfCraftApp,
|
||||
t: &Tokens,
|
||||
ctx: &mut crate::dialogs::preferences::PrefSearchContext,
|
||||
) {
|
||||
ctx.label(ui, tl_ctx!("theme", "Appearance"), theme::semibold(13.0), t.text);
|
||||
ui.horizontal(|ui| {
|
||||
let label = ctx.label(ui, tl!("Appearance mode"), theme::regular(13.0), t.text);
|
||||
let combo = craft_ui::ComboBox::from_id_salt("appearance-mode").selected_text(tl!(mode_label(app.theme_preference))).show_ui(ui, |ui| {
|
||||
for preference in [ThemePreference::System, ThemePreference::Light, ThemePreference::Dark] {
|
||||
if ui.selectable_label(app.theme_preference == preference, tl!(mode_label(preference))).clicked() {
|
||||
app.execute(mode_command(preference));
|
||||
ui.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
// The visible label names the combo box for assistive technology and tests.
|
||||
combo.response.labelled_by(label.id);
|
||||
});
|
||||
ui.add_space(4.0);
|
||||
let light_active = app.theme == ThemeKind::Light;
|
||||
let light_title = tl!("Light theme");
|
||||
let dark_title = tl!("Dark theme");
|
||||
let light_matches = crate::dialogs::preferences::find_match_ranges(light_title, ctx.query).len();
|
||||
let dark_matches = crate::dialogs::preferences::find_match_ranges(dark_title, ctx.query).len();
|
||||
ctx.total_matches += light_matches + dark_matches;
|
||||
|
||||
ui.columns(2, |cols| {
|
||||
let [light, dark] = cols else { return };
|
||||
let (light_clicked, light_resp) = card(light, t, light_title, light_active, LIGHT_THEMES, ctx.query);
|
||||
if light_clicked {
|
||||
app.execute(mode_command(ThemePreference::Light));
|
||||
}
|
||||
ctx.scroll_if_first_match(&light_resp, light_matches);
|
||||
|
||||
let (dark_clicked, dark_resp) = card(dark, t, dark_title, !light_active, DARK_THEMES, ctx.query);
|
||||
if dark_clicked {
|
||||
app.execute(mode_command(ThemePreference::Dark));
|
||||
}
|
||||
ctx.scroll_if_first_match(&dark_resp, dark_matches);
|
||||
});
|
||||
ui.add_space(4.0);
|
||||
// Acrobat's "Highlight existing fields": shared with the toggle in the form bar.
|
||||
ctx.checkbox(ui, &mut app.view_defaults.highlight_fields, tl!("Highlight fields"), t);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
|
||||
use super::*;
|
||||
|
||||
/// The mode is chosen in Preferences and both theme cards show, the one on screen marked
|
||||
/// Active.
|
||||
#[test]
|
||||
fn preferences_choose_the_appearance_mode_and_show_both_themes() {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.set_option("language", "en").unwrap();
|
||||
let mut fonts = false;
|
||||
let mut h = Harness::builder().with_size(egui::vec2(640.0, 400.0)).build_ui_state(
|
||||
move |ui, app: &mut PdfCraftApp| {
|
||||
// The app's fonts (the semibold headings) take effect from the next frame.
|
||||
if !fonts {
|
||||
ui.ctx().set_fonts(theme::font_definitions());
|
||||
fonts = true;
|
||||
return;
|
||||
}
|
||||
let t = Tokens::get(ui.ctx());
|
||||
preferences_section(ui, app, &t);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run();
|
||||
assert!(h.query_by_label("Light theme").is_some() && h.query_by_label("Dark theme").is_some());
|
||||
assert_eq!(h.query_all_by_label("Active").count(), 1);
|
||||
if let Ok(path) = std::env::var("PDFCRAFT_APPEARANCE_SHOT") {
|
||||
h.render().unwrap().save(path).unwrap();
|
||||
}
|
||||
|
||||
// The field-highlight preference is on for new settings, and the box turns it off.
|
||||
assert!(h.state().view_defaults.highlight_fields);
|
||||
h.get_by_label("Highlight fields").click();
|
||||
h.run();
|
||||
assert!(!h.state().view_defaults.highlight_fields);
|
||||
|
||||
let combo = egui::accesskit::Role::ComboBox;
|
||||
h.get_by_role(combo).click();
|
||||
h.run();
|
||||
h.get_by_label("Dark").click();
|
||||
h.run();
|
||||
assert_eq!(h.state().theme_preference, ThemePreference::Dark);
|
||||
h.get_by_role(combo).click();
|
||||
h.run();
|
||||
h.get_by_label("Use system setting").click();
|
||||
h.run();
|
||||
assert_eq!(h.state().theme_preference, ThemePreference::System);
|
||||
}
|
||||
|
||||
/// Clicking a theme card picks that theme, not only the mode combo box.
|
||||
#[test]
|
||||
fn clicking_a_theme_card_picks_that_theme() {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.set_option("language", "en").unwrap();
|
||||
let mut fonts = false;
|
||||
let mut h = Harness::builder().with_size(egui::vec2(640.0, 400.0)).build_ui_state(
|
||||
move |ui, app: &mut PdfCraftApp| {
|
||||
if !fonts {
|
||||
ui.ctx().set_fonts(theme::font_definitions());
|
||||
fonts = true;
|
||||
return;
|
||||
}
|
||||
let t = Tokens::get(ui.ctx());
|
||||
preferences_section(ui, app, &t);
|
||||
},
|
||||
app,
|
||||
);
|
||||
h.run();
|
||||
h.get_by_label("Dark theme").click();
|
||||
h.run();
|
||||
assert_eq!((h.state().theme_preference, h.state().theme), (ThemePreference::Dark, ThemeKind::Dark));
|
||||
h.get_by_label("Light theme").click();
|
||||
h.run();
|
||||
assert_eq!((h.state().theme_preference, h.state().theme), (ThemePreference::Light, ThemeKind::Light));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,35 +0,0 @@
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
|
||||
#[test]
|
||||
fn preferences_scroll_preserves_disabled_animation() {
|
||||
for animated in [false, true] {
|
||||
let scroll = Rc::new(Cell::new(false));
|
||||
let requested = scroll.clone();
|
||||
let mut h = Harness::builder().with_size(egui::vec2(600.0, 400.0)).with_step_dt(1.0 / 60.0).build_ui(move |ui| {
|
||||
super::preferences_scroll(ui, 200.0, |ui| {
|
||||
ui.add_space(1000.0);
|
||||
let target = ui.label("Scroll target");
|
||||
if requested.replace(false) {
|
||||
ui.scroll_to_rect(target.rect, Some(egui::Align::BOTTOM));
|
||||
}
|
||||
ui.add_space(300.0);
|
||||
});
|
||||
});
|
||||
h.ctx.all_styles_mut(|style| {
|
||||
style.scroll_animation = if animated { egui::style::ScrollAnimation::default() } else { egui::style::ScrollAnimation::none() };
|
||||
});
|
||||
h.run_steps(4);
|
||||
let before = h.get_by_label("Scroll target").rect().top();
|
||||
scroll.set(true);
|
||||
h.run_steps(4);
|
||||
let early = h.get_by_label("Scroll target").rect().top();
|
||||
h.run_steps(20);
|
||||
let settled = h.get_by_label("Scroll target").rect().top();
|
||||
assert!((before - settled).abs() > 50.0, "the target must scroll: {before} -> {settled}");
|
||||
assert_eq!((early - settled).abs() > 1.0, animated, "animated: {animated}, early: {early}, settled: {settled}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
//! Preferences rows apply on OK and change nothing on Cancel.
|
||||
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
// PLAN: assumed public `pdfcraft_ui_egui::{Mode, units::Unit}` and `craft::theme::selection::MotionChoice` for tests
|
||||
use craft::theme::selection::MotionChoice;
|
||||
use pdfcraft_ui_egui::units::Unit;
|
||||
use pdfcraft_ui_egui::{Mode, PdfCraftApp};
|
||||
|
||||
const PDF: &[u8] = include_bytes!("../data/form.pdf");
|
||||
|
||||
type H = Harness<'static, PdfCraftApp>;
|
||||
|
||||
/// One row edit and the app state it must produce after OK; the same state must be absent after Cancel.
|
||||
struct Case {
|
||||
name: &'static str,
|
||||
page: &'static str,
|
||||
edit: fn(&mut H),
|
||||
applied: fn(&PdfCraftApp) -> bool,
|
||||
}
|
||||
|
||||
fn harness() -> H {
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.set_option("language", "en").unwrap();
|
||||
app.open_bytes("form.pdf", None, PDF.to_vec()).unwrap();
|
||||
app.recent.extend((0..5).map(|n| pdfcraft_ui_egui::RecentFile { name: format!("{n}.pdf"), path: format!("/nowhere/{n}.pdf"), pages: 1, size: 0 }));
|
||||
app
|
||||
});
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
fn click(h: &mut H, label: &str) {
|
||||
h.get_by_label(label).click();
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
/// Replaces the text of the field named `label` through the keyboard, as a user would.
|
||||
fn type_into(h: &mut H, label: &str, text: &str) {
|
||||
h.get_by_label(label).click();
|
||||
h.run_steps(2);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
h.run_steps(1);
|
||||
h.get_by_label(label).type_text(text);
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
fn cases() -> Vec<Case> {
|
||||
vec![
|
||||
Case { name: "default mode", page: "Documents and view", edit: |h| click(h, "E-Sign"), applied: |a| a.default_mode == Mode::Sign },
|
||||
Case {
|
||||
name: "layout and continuous on the active view",
|
||||
page: "Documents and view",
|
||||
edit: |h| {
|
||||
click(h, "Two-page view");
|
||||
click(h, "Continuous scrolling");
|
||||
},
|
||||
applied: |a| {
|
||||
let v = &a.views[0].view;
|
||||
v.display == craft::pages::PageDisplay::TwoUp && v.continuous != PdfCraftApp::new().views.first().map_or(true, |_| true)
|
||||
},
|
||||
},
|
||||
Case { name: "units", page: "Documents and view", edit: |h| click(h, Unit::Millimetres.label()), applied: |a| a.units == Unit::Millimetres },
|
||||
Case {
|
||||
name: "tools side and rail side",
|
||||
page: "General & Interface",
|
||||
edit: |h| {
|
||||
// The tools row comes first, then the rail row; each has a Left and a Right option.
|
||||
h.get_all_by_label("Right").next().unwrap().click();
|
||||
h.run_steps(3);
|
||||
h.get_all_by_label("Left").nth(1).unwrap().click();
|
||||
h.run_steps(3);
|
||||
},
|
||||
applied: |a| a.side_panels.tools_on_right() && !a.side_panels.rail_on_right(),
|
||||
},
|
||||
Case { name: "animation effects", page: "General & Interface", edit: |h| click(h, "Off"), applied: |a| a.animation_effects == MotionChoice::Off },
|
||||
Case {
|
||||
name: "recent limit truncates",
|
||||
page: "General & Interface",
|
||||
edit: |h| type_into(h, "Recent files to keep", "2"),
|
||||
applied: |a| a.recent_limit == 2 && a.recent.len() == 2,
|
||||
},
|
||||
Case { name: "comment author", page: "Comment", edit: |h| type_into(h, "Name on new comments", "Ada"), applied: |a| a.comment_prefs.author == "Ada" },
|
||||
Case { name: "date format", page: "Fill & Sign", edit: |h| type_into(h, "Pattern", "dd.mm.yyyy"), applied: |a| a.session.date_format() == "dd.mm.yyyy" },
|
||||
Case {
|
||||
name: "javascript",
|
||||
page: "JavaScript",
|
||||
edit: |h| click(h, "Enable Acrobat JavaScript"),
|
||||
applied: |a| !a.session.javascript(),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn edited(case: &Case) -> H {
|
||||
let mut h = harness();
|
||||
h.state_mut().open_preferences();
|
||||
h.run_steps(4);
|
||||
click(&mut h, case.page);
|
||||
(case.edit)(&mut h);
|
||||
h
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preference_rows_apply_on_ok_and_nothing_changes_on_cancel() {
|
||||
for case in cases() {
|
||||
let mut h = edited(&case);
|
||||
assert!(!(case.applied)(h.state()), "{}: a row edits the draft, not the app", case.name);
|
||||
click(&mut h, "OK");
|
||||
h.run_steps(3);
|
||||
assert!(h.state().preferences.is_none(), "{}: OK closes the window", case.name);
|
||||
assert!((case.applied)(h.state()), "{}: OK applies the draft", case.name);
|
||||
|
||||
let mut h = edited(&case);
|
||||
click(&mut h, "Cancel");
|
||||
h.run_steps(3);
|
||||
assert!(h.state().preferences.is_none(), "{}: Cancel closes the window", case.name);
|
||||
assert!(!(case.applied)(h.state()), "{}: Cancel leaves the app as it was", case.name);
|
||||
}
|
||||
}
|
||||
@@ -1,253 +0,0 @@
|
||||
//! The Preferences dialog keeps OK in reach in a short window (#3557): the settings scroll, the
|
||||
//! OK row does not.
|
||||
|
||||
use egui::Rect;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use pdfcraft_ui_egui::PdfCraftApp;
|
||||
|
||||
fn harness(height: f32) -> Harness<'static, PdfCraftApp> {
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1000.0, height)).build_eframe(|_cc| {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.set_option("language", "en").unwrap();
|
||||
app.set_option("dialog", "preferences").unwrap();
|
||||
app
|
||||
});
|
||||
h.run_steps(6);
|
||||
h
|
||||
}
|
||||
|
||||
fn on_screen(rect: Rect, height: f32) -> bool {
|
||||
rect.min.x >= 0.0 && rect.min.y >= 0.0 && rect.max.x <= 1000.0 && rect.max.y <= height
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ok_is_on_screen_without_scrolling_at_any_window_height() {
|
||||
// From a short laptop window to a tall monitor, the settings never fit the short ones. 173 is
|
||||
// the smallest window (520 pt) at the largest interface size (300%), 208 is 250%.
|
||||
for height in [173.0, 208.0, 260.0, 420.0, 480.0, 560.0, 700.0, 900.0, 1100.0] {
|
||||
let h = harness(height);
|
||||
let ok = h.get_by_label("OK").rect();
|
||||
assert!(on_screen(ok, height), "{height}: OK at {ok:?} is off the {height} pt window");
|
||||
let title = h.get_by(|n| n.role() == egui::accesskit::Role::Label && n.value().as_deref() == Some("Preferences")).rect();
|
||||
assert!(on_screen(title, height), "{height}: the title at {title:?} is off the window");
|
||||
// The OK row sits below the scrolled settings, never over them: the settings' last visible
|
||||
// row ends above it. The title is above everything.
|
||||
assert!(title.max.y < ok.min.y, "{height}: title {title:?} above OK {ok:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ok_closes_the_dialog_in_a_short_window_and_the_settings_still_scroll() {
|
||||
let mut h = harness(480.0);
|
||||
// A setting far down the list starts out below the visible settings; scrolling brings it in
|
||||
// between the title and the OK row, and OK did not move.
|
||||
let ok_before = h.get_by_label("OK").rect();
|
||||
let title = h.get_by(|n| n.role() == egui::accesskit::Role::Label && n.value().as_deref() == Some("Preferences")).rect();
|
||||
let far = h.get_by_label_contains("Enable Acrobat JavaScript").rect();
|
||||
assert!(far.min.y >= ok_before.min.y, "JavaScript setting starts out of sight: {far:?} vs OK {ok_before:?}");
|
||||
h.get_by_label_contains("Enable Acrobat JavaScript").scroll_to_me();
|
||||
h.run_steps(4);
|
||||
let far = h.get_by_label_contains("Enable Acrobat JavaScript").rect();
|
||||
assert!(far.min.y >= title.max.y && far.max.y <= ok_before.min.y, "JavaScript setting scrolled in between the title and OK: {far:?}");
|
||||
assert_eq!(h.get_by_label("OK").rect(), ok_before, "OK stays put while the settings scroll");
|
||||
assert!(h.state().dialog.is_some());
|
||||
h.get_by_label("OK").click();
|
||||
h.run_steps(3);
|
||||
assert!(h.state().dialog.is_none(), "OK closes Preferences");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_dialog_opens_at_its_full_size_and_does_not_grow_afterwards() {
|
||||
// With OK outside the scroll, the scroll area started from what the modal offered, which
|
||||
// grew some 45 pt a frame for ten frames: the dialog opened small and stretched, and clicks
|
||||
// in its first frames landed on controls that were still moving.
|
||||
for height in [260.0, 560.0, 900.0] {
|
||||
let mut h = harness(height);
|
||||
let settled = (
|
||||
h.get_by(|n| n.role() == egui::accesskit::Role::Label && n.value().as_deref() == Some("Preferences")).rect(),
|
||||
h.get_by_label("OK").rect(),
|
||||
);
|
||||
h.run_steps(8);
|
||||
let later = (
|
||||
h.get_by(|n| n.role() == egui::accesskit::Role::Label && n.value().as_deref() == Some("Preferences")).rect(),
|
||||
h.get_by_label("OK").rect(),
|
||||
);
|
||||
assert_eq!(settled, later, "{height}: the dialog moved after it had been shown for six frames");
|
||||
}
|
||||
// And it is in place within a few frames of opening, for the first click: a frame or two of
|
||||
// settling is fine, ten frames of stretching is not.
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_cc| {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.set_option("language", "en").unwrap();
|
||||
app
|
||||
});
|
||||
h.run_steps(4);
|
||||
h.state_mut().dialog = Some(pdfcraft_ui_egui::Dialog::Preferences);
|
||||
h.run_steps(3);
|
||||
let early = h.get_by_label("Right").rect();
|
||||
h.run_steps(8);
|
||||
let late = h.get_by_label("Right").rect();
|
||||
assert!((early.min.y - late.min.y).abs() <= 2.0 && (early.min.x - late.min.x).abs() <= 2.0, "{early:?} then {late:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_very_tall_window_does_not_leave_a_blank_gap_above_ok() {
|
||||
// The settings are some 1 400 pt tall; in a taller window the scroll area is not stretched
|
||||
// past them.
|
||||
let mut h = harness(2600.0);
|
||||
h.run_steps(6);
|
||||
let ok = h.get_by_label("OK").rect();
|
||||
let last = h.get_by_label_contains("Enable Acrobat JavaScript").rect();
|
||||
assert!(on_screen(ok, 2600.0) && on_screen(last, 2600.0), "{last:?} {ok:?}");
|
||||
assert!(ok.min.y - last.max.y < 120.0, "a gap of {} pt between the last setting and OK", ok.min.y - last.max.y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn segmented_pill_controls_switch_modes_and_layouts() {
|
||||
let mut h = harness(900.0);
|
||||
// Switch workspace mode via segmented pill
|
||||
h.get_by_role_and_label(egui::accesskit::Role::RadioButton, "Convert").scroll_to_me();
|
||||
h.run_steps(3);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::RadioButton, "Convert").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().default_mode, pdfcraft_ui_egui::Mode::Convert);
|
||||
|
||||
let continuous_before = h.state().view_defaults.continuous;
|
||||
|
||||
// Switch page layout via segmented pill
|
||||
h.get_by_label("Two-page view").scroll_to_me();
|
||||
h.run_steps(3);
|
||||
h.get_by_label("Two-page view").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().view_defaults.layout, pdfcraft_ui_egui::canvas::PageLayout::TwoUp);
|
||||
assert_eq!(h.state().view_defaults.continuous, continuous_before, "Two-page view retains continuous scrolling");
|
||||
|
||||
// Switch continuous scrolling pill (can be combined with Two-page view)
|
||||
h.get_by_label("Continuous scrolling").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().view_defaults.continuous, !continuous_before);
|
||||
assert_eq!(h.state().view_defaults.layout, pdfcraft_ui_egui::canvas::PageLayout::TwoUp, "two-page continuous scrolling is supported");
|
||||
|
||||
// Switch to single page mode (continuous setting remains independent)
|
||||
h.get_by_label("Single page").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().view_defaults.layout, pdfcraft_ui_egui::canvas::PageLayout::Single);
|
||||
assert_eq!(h.state().view_defaults.continuous, !continuous_before);
|
||||
|
||||
// Switch tools panel side via segmented pill
|
||||
h.get_by_label("Left").scroll_to_me();
|
||||
h.run_steps(3);
|
||||
h.get_by_label("Left").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().side_panels.tools_side, pdfcraft_ui_egui::side_panels::ToolsSide::Left);
|
||||
|
||||
// Switch measurement units via segmented pill
|
||||
h.get_by_label("pt").scroll_to_me();
|
||||
h.run_steps(3);
|
||||
h.get_by_label("pt").click();
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().units, pdfcraft_ui_egui::units::Unit::Points);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_settings_scans_and_highlights_in_situ_without_hiding_cards() {
|
||||
let mut h = harness(900.0);
|
||||
// Initially all card titles exist
|
||||
assert!(h.query_by_label("General & Interface").is_some());
|
||||
assert!(h.query_by_label("Comment").is_some());
|
||||
assert!(h.query_by_label("JavaScript").is_some());
|
||||
|
||||
// Type "Hide hover" in search box
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").click();
|
||||
h.run_steps(1);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").type_text("Hide hover");
|
||||
h.run_steps(4);
|
||||
|
||||
// In browser-style search, cards are NOT hidden
|
||||
assert!(h.query_by_label("General & Interface").is_some());
|
||||
assert!(h.query_by_label("Comment").is_some());
|
||||
assert!(h.query_by_label("JavaScript").is_some());
|
||||
|
||||
// The setting "Hide hover previews for highlights without comment text" is present and matched
|
||||
assert!(h.query_by_label("Hide hover previews for highlights without comment text").is_some());
|
||||
// Match badge displays 1 match
|
||||
assert!(h.query_by_label("1 match").is_some());
|
||||
|
||||
// Clearing search clears match badge
|
||||
h.get_by_label("Clear search").click();
|
||||
h.run_steps(4);
|
||||
assert!(h.query_by_label("1 match").is_none());
|
||||
|
||||
// Search for "javascript"
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").click();
|
||||
h.run_steps(1);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").type_text("javascript");
|
||||
h.run_steps(4);
|
||||
|
||||
// Cards remain visible and multiple matches are reported
|
||||
assert!(h.query_by_label("JavaScript").is_some());
|
||||
assert!(h.query_by_label("General & Interface").is_some());
|
||||
assert!(h.query_by_label_contains("matches").is_some());
|
||||
|
||||
// Clearing search shows everything again
|
||||
h.get_by_label("Clear search").click();
|
||||
h.run_steps(4);
|
||||
assert!(h.query_by_label("General & Interface").is_some());
|
||||
assert!(h.query_by_label("Comment").is_some());
|
||||
assert!(h.query_by_label("JavaScript").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preferences_dialog_scrollbar_spacing_and_controls() {
|
||||
let h = harness(600.0);
|
||||
let title = h.get_by(|n| n.role() == egui::accesskit::Role::Label && n.value().as_deref() == Some("Preferences")).rect();
|
||||
let search = h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").rect();
|
||||
let ok = h.get_by_label("OK").rect();
|
||||
assert!(on_screen(ok, 600.0));
|
||||
// Header title and search box are vertically aligned
|
||||
assert!((title.center().y - search.center().y).abs() <= 2.0);
|
||||
// Search box right edge is aligned within the dialog
|
||||
assert!(search.max.x <= ok.max.x);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn search_scrolls_option_only_and_theme_card_matches_into_visible_viewport() {
|
||||
let mut h = harness(480.0);
|
||||
let title = h.get_by(|n| n.role() == egui::accesskit::Role::Label && n.value().as_deref() == Some("Preferences")).rect();
|
||||
let ok = h.get_by_label("OK").rect();
|
||||
|
||||
// "Two-page view" is an option in Card 3 (Documents and view).
|
||||
// In a 480 pt window, it starts out below the visible scroll viewport.
|
||||
let opt_before = h.get_by_label("Two-page view").rect();
|
||||
assert!(opt_before.min.y >= ok.min.y, "Two-page option starts out below the visible viewport: {opt_before:?} vs OK {ok:?}");
|
||||
|
||||
// Searching "Two-page" (which only occurs inside that segmented pill option)
|
||||
// auto-scrolls the matching option into the visible viewport between title and OK.
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").click();
|
||||
h.run_steps(1);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").type_text("Two-page");
|
||||
h.run_steps(4);
|
||||
|
||||
let opt_after = h.get_by_label("Two-page view").rect();
|
||||
assert!(
|
||||
opt_after.min.y >= title.max.y && opt_after.max.y <= ok.min.y,
|
||||
"Two-page option scrolled into visible scroll viewport: {opt_after:?} between title {title:?} and OK {ok:?}"
|
||||
);
|
||||
|
||||
// Clear search and test a theme-card-only match
|
||||
h.get_by_label("Clear search").click();
|
||||
h.run_steps(4);
|
||||
|
||||
// Search for "Dark theme" (which occurs only in the dark theme card in Card 2).
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").click();
|
||||
h.run_steps(1);
|
||||
h.get_by_role_and_label(egui::accesskit::Role::TextInput, "Search settings…").type_text("Dark theme");
|
||||
h.run_steps(4);
|
||||
|
||||
let dark_card = h.get_by_label("Dark theme").rect();
|
||||
assert!(
|
||||
dark_card.min.y >= title.max.y && dark_card.max.y <= ok.min.y,
|
||||
"Dark theme card scrolled into visible viewport: {dark_card:?} between title {title:?} and OK {ok:?}"
|
||||
);
|
||||
}
|
||||
@@ -1,14 +1,19 @@
|
||||
//! Preferences ▸ Recent files to keep (#5572): how many files the recent list holds, from none
|
||||
//! up to 50, on every path that adds to it, shows it or saves it.
|
||||
|
||||
use egui::Key;
|
||||
use egui::accesskit::Role;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use pdfcraft_ui_egui::{Dialog, PdfCraftApp, RECENT_FILES_DEFAULT, RecentFile};
|
||||
// PLAN: assumed `pdfcraft_ui_egui::settings::import_json(&str) -> (PdfCraftSettings, Vec<SectionError>)` (pdf-shell-w9)
|
||||
use pdfcraft_ui_egui::settings::import_json;
|
||||
use pdfcraft_ui_egui::{PdfCraftApp, RecentFile};
|
||||
|
||||
use crate::support;
|
||||
|
||||
/// Settings as a settings file gives them, read the way the app reads them.
|
||||
fn restore(app: &mut PdfCraftApp, json: &str) {
|
||||
app.apply_settings(&import_json(json).0);
|
||||
}
|
||||
|
||||
const PDF: &[u8] = include_bytes!("../data/form.pdf");
|
||||
|
||||
fn recent_file(name: &str) -> RecentFile {
|
||||
@@ -43,30 +48,6 @@ fn open(app: &mut PdfCraftApp, name: &str) {
|
||||
app.open_bytes(name, Some(format!("/nowhere/{name}")), PDF.to_vec()).unwrap();
|
||||
}
|
||||
|
||||
/// Typed the way a person does: click the field, type, Enter.
|
||||
fn type_limit(h: &mut Harness<'static, PdfCraftApp>, value: &str) {
|
||||
h.get_by_role_and_label(Role::SpinButton, "Recent files to keep").click();
|
||||
h.run_steps(2);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, Key::A);
|
||||
h.run_steps(1);
|
||||
h.event(egui::Event::Text(value.into()));
|
||||
h.run_steps(1);
|
||||
h.key_press(Key::Enter);
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_list_keeps_twelve_files_until_preferences_says_otherwise() {
|
||||
let mut app = app_with(&[]);
|
||||
assert_eq!(app.recent_limit, 12);
|
||||
for n in 0..=RECENT_FILES_DEFAULT {
|
||||
open(&mut app, &format!("{n}.pdf"));
|
||||
}
|
||||
assert_eq!(app.recent.len(), 12);
|
||||
assert_eq!(app.recent[0].name, "12.pdf", "newest first");
|
||||
assert_eq!(app.recent[11].name, "1.pdf", "the oldest of the thirteen went");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opening_a_file_keeps_the_newest_up_to_the_limit() {
|
||||
let mut app = app_with(&[]);
|
||||
@@ -85,8 +66,7 @@ fn at_zero_opening_a_file_records_nothing() {
|
||||
app.recent_limit = 0;
|
||||
open(&mut app, "first.pdf");
|
||||
assert!(app.recent.is_empty());
|
||||
let saved: serde_json::Value = serde_json::from_str(&app.persist()).unwrap();
|
||||
assert_eq!(saved["recent"], serde_json::json!([]), "and the saved list is empty");
|
||||
assert!(app.settings_snapshot().recent.is_empty(), "and the saved list is empty"); // PLAN: assumed `recent` field of the snapshot (pdf-shell-w9)
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -148,51 +128,12 @@ fn the_longest_list_scrolls_in_the_smallest_window_down_to_clear() {
|
||||
assert!(h.state().recent.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preferences_cuts_the_list_when_it_closes_and_not_before() {
|
||||
let mut h = harness(12, &["first.pdf", "second.pdf", "third.pdf"]);
|
||||
h.state_mut().dialog = Some(Dialog::Preferences);
|
||||
h.run_steps(4);
|
||||
type_limit(&mut h, "0");
|
||||
assert_eq!(h.state().recent_limit, 0);
|
||||
assert_eq!(h.state().recent.len(), 3, "0 on the way to another number forgets nothing");
|
||||
assert!(h.query_by_label("first.pdf").is_none(), "though Home already leaves them out");
|
||||
type_limit(&mut h, "2");
|
||||
assert_eq!(h.state().recent.len(), 3);
|
||||
h.get_by_label("OK").click();
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().dialog, None);
|
||||
assert_eq!(names(h.state()), ["first.pdf", "second.pdf"], "the newest two stay");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_closes_preferences_with_the_same_result_as_ok() {
|
||||
let mut h = harness(12, &["first.pdf", "second.pdf"]);
|
||||
h.state_mut().dialog = Some(Dialog::Preferences);
|
||||
h.run_steps(4);
|
||||
type_limit(&mut h, "1");
|
||||
h.key_press(Key::Escape);
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.state().dialog, None);
|
||||
assert_eq!(names(h.state()), ["first.pdf"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_field_does_not_go_past_fifty() {
|
||||
let mut h = harness(12, &[]);
|
||||
h.state_mut().dialog = Some(Dialog::Preferences);
|
||||
h.run_steps(4);
|
||||
type_limit(&mut h, "999");
|
||||
assert_eq!(h.state().recent_limit, 50);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn settings_saved_while_preferences_is_open_already_leave_the_rest_out() {
|
||||
let mut app = app_with(&["first.pdf", "second.pdf"]);
|
||||
app.recent_limit = 1;
|
||||
let saved = app.persist();
|
||||
assert!(saved.contains("first.pdf"));
|
||||
assert!(!saved.contains("second.pdf"));
|
||||
let saved: Vec<String> = app.settings_snapshot().recent.iter().map(|r| r.name.clone()).collect();
|
||||
assert_eq!(saved, ["first.pdf"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -210,12 +151,12 @@ fn the_limit_is_saved_and_restored_and_cuts_a_longer_list() {
|
||||
let entries: Vec<RecentFile> = paths.iter().map(|p| RecentFile { name: "x.pdf".into(), path: p.clone(), pages: 1, size: 0 }).collect();
|
||||
let json = serde_json::json!({ "recent": entries, "recent_limit": 2 }).to_string();
|
||||
let mut restored = PdfCraftApp::new();
|
||||
restored.restore(&json);
|
||||
restore(&mut restored, &json);
|
||||
assert_eq!(restored.recent_limit, 2);
|
||||
let kept: Vec<&str> = restored.recent.iter().map(|r| r.path.as_str()).collect();
|
||||
assert_eq!(kept, [paths[0].as_str(), paths[1].as_str()]);
|
||||
let mut again = PdfCraftApp::new();
|
||||
again.restore(&restored.persist());
|
||||
again.apply_settings(&restored.settings_snapshot());
|
||||
assert_eq!(again.recent_limit, 2);
|
||||
assert_eq!(again.recent.len(), 2);
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
@@ -237,7 +178,7 @@ fn restoring_settings_does_not_check_recent_files_and_a_frame_drops_the_gone_one
|
||||
let json = serde_json::json!({ "recent": entries }).to_string();
|
||||
|
||||
let mut restored = PdfCraftApp::new();
|
||||
restored.restore(&json);
|
||||
restore(&mut restored, &json);
|
||||
let paths: Vec<&str> = restored.recent.iter().map(|r| r.path.as_str()).collect();
|
||||
assert_eq!(paths, [here.as_str(), gone.as_str()], "restoring keeps every entry without asking whether its file exists");
|
||||
|
||||
@@ -245,7 +186,7 @@ fn restoring_settings_does_not_check_recent_files_and_a_frame_drops_the_gone_one
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.set_option("language", "en").unwrap();
|
||||
app.run_inline = true;
|
||||
app.restore(&json);
|
||||
restore(&mut app, &json);
|
||||
app
|
||||
});
|
||||
h.run_steps(2);
|
||||
@@ -268,7 +209,7 @@ fn a_settings_file_can_not_ask_for_a_limit_that_is_not_one() {
|
||||
("{\"recent_limit\": 18446744073709551615}", 50),
|
||||
] {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.restore(saved);
|
||||
restore(&mut app, saved);
|
||||
assert_eq!(app.recent_limit, want, "{saved}");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user