mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-26 04:58:07 +00:00
Merge branch 'red8/g6' into area/app-1
# Conflicts: # crates/ui-egui/src/dialogs/shortcuts.rs
This commit is contained in:
@@ -135,9 +135,18 @@ impl DocView {
|
||||
/// top-left at its zoom (null or 0 keeps the zoom); `/Fit`, `/FitH` and `/FitV` switch to fit
|
||||
/// page, width and height with `top` or `left` at the window's edge; `/FitR` zooms so the
|
||||
/// rectangle fits, centred. The `B` (content box) forms are read as the page. A null `/XYZ`
|
||||
/// coordinate keeps the reader's place: that axis of the view is not moved, as Acrobat does.
|
||||
/// A point past the end of the document scrolls as far as it can.
|
||||
/// coordinate keeps the reader's place in user space, as Acrobat does: the coordinate at the
|
||||
/// window's top-left now is the one put there on the new page. A point past the end of the
|
||||
/// document scrolls as far as it can.
|
||||
pub fn go_to_dest(&mut self, page: usize, dest: DestView, info: &DocInfo) {
|
||||
let kept = match dest {
|
||||
DestView::Xyz { left, top, .. } if left.is_none() || top.is_none() => self.place_at_corner(info),
|
||||
_ => None,
|
||||
};
|
||||
let dest = match (dest, kept) {
|
||||
(DestView::Xyz { left, top, zoom }, Some([x, y])) => DestView::Xyz { left: left.or(Some(x)), top: top.or(Some(y)), zoom },
|
||||
(dest, _) => dest,
|
||||
};
|
||||
self.go_to_page(page);
|
||||
let page = self.view.current;
|
||||
let Some(p) = info.pages.get(page) else { return };
|
||||
@@ -188,6 +197,17 @@ impl DocView {
|
||||
}
|
||||
}
|
||||
|
||||
/// The user-space point of the current page at the window's top-left as far as pages go (past
|
||||
/// the side gutter) in the last pass, if that page was drawn.
|
||||
fn place_at_corner(&self, info: &DocInfo) -> Option<[f32; 2]> {
|
||||
let page = self.view.current;
|
||||
let xf = self.page_xform(page)?;
|
||||
let side = self.spacing.as_ref().map_or(0.0, |spacing| spacing.side as f32);
|
||||
let (vx, vy) = xf.screen_to_view(self.viewport_rect().min + egui::vec2(side, 0.0));
|
||||
let u = info.pages.get(page)?.view_to_user(vx, vy);
|
||||
Some([u[0], u[1]])
|
||||
}
|
||||
|
||||
/// Next page (`true`) or previous page. In two-page view this moves a whole spread: the other
|
||||
/// page of the spread is already on screen, so stepping to it wouldn't move the view (#70).
|
||||
pub fn step_page(&mut self, forward: bool) {
|
||||
|
||||
@@ -432,8 +432,10 @@ pub struct PageOverlaysStyle {
|
||||
tab_ink: StyleColor,
|
||||
/// The colour of every verdict tone.
|
||||
tones: Vec<(Tone, StyleColor)>,
|
||||
/// The badge outline's width, and how far it stands off the widget.
|
||||
/// The badge outline's width, and how far it stands off the widget; also a required field's outline width.
|
||||
outline: f32,
|
||||
/// The outline width of a field that isn't required.
|
||||
field_line: f32,
|
||||
/// The corners of the badge outline and the flash.
|
||||
corners: CornerRadius,
|
||||
/// How far the flash and the compare marks stand off what they mark.
|
||||
@@ -509,6 +511,7 @@ impl Component for PageOverlays<'_> {
|
||||
tab_ink: cx.color(colors.text.on_accent),
|
||||
tones,
|
||||
outline: tokens.stroke.thick,
|
||||
field_line: tokens.stroke.thin,
|
||||
corners: CornerRadius::same(tokens.shape.radius.sm.round().clamp(0.0, 255.0) as u8),
|
||||
grow: tokens.space.gap.inline,
|
||||
}
|
||||
@@ -553,20 +556,26 @@ impl PageOverlays<'_> {
|
||||
fn paint_field_highlights(&self, draw: &Draw<'_>, style: &PageOverlaysStyle) {
|
||||
for field in self.doc.form.iter().filter(|f| f.kind != FormFieldKind::PushButton) {
|
||||
let required = field.has(pdfcraft_engine::field_flags::REQUIRED);
|
||||
let (width, edge) = if required { (StrokeWidth::Medium, style.required_edge) } else { (StrokeWidth::Thin, style.field_edge) };
|
||||
let (width, line, edge) = if required {
|
||||
(StrokeWidth::Thick, style.outline, style.required_edge)
|
||||
} else {
|
||||
(StrokeWidth::Thin, style.field_line, style.field_edge)
|
||||
};
|
||||
for widget in field.widgets.iter().filter(|w| w.page == Some(self.page) && !w.hidden && !w.read_only) {
|
||||
let rect = self.xf.user_rect_f64(&self.doc.info, self.page, widget.rect);
|
||||
// The page transform can turn a finite widget into one the screen cannot place.
|
||||
if !rect.is_finite() {
|
||||
continue;
|
||||
}
|
||||
let shape = if widget.round {
|
||||
PaintShape::Circle(rect.center(), rect.width().min(rect.height()) / 2.0)
|
||||
// The outline lies inside the widget: a rectangle strokes inside its edge, a circle on its radius.
|
||||
let radius = rect.width().min(rect.height()) / 2.0;
|
||||
let (shape, outline) = if widget.round {
|
||||
(PaintShape::Circle(rect.center(), radius), PaintShape::Circle(rect.center(), radius - line / 2.0))
|
||||
} else {
|
||||
PaintShape::Rect(rect, CornerRadius::ZERO)
|
||||
(PaintShape::Rect(rect, CornerRadius::ZERO), PaintShape::Rect(rect, CornerRadius::ZERO))
|
||||
};
|
||||
draw.fill(&shape, style.field_fill);
|
||||
draw.stroke(&shape, width, edge);
|
||||
draw.stroke(&outline, width, edge);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,8 +516,8 @@ pub(crate) fn edit_text_press(
|
||||
geo: PageGeometry<'_>,
|
||||
tool: QuickTool,
|
||||
) -> Option<Edit> {
|
||||
let Mods { cmd, shift, .. } = press.mods;
|
||||
if (shift || cmd) && crate::tools::object_ui::modified_press(session, doc, info, press, geo) {
|
||||
let Mods { cmd, ctrl, shift, .. } = press.mods;
|
||||
if (shift || cmd || ctrl) && crate::tools::object_ui::modified_press(session, doc, info, press, geo) {
|
||||
return None;
|
||||
}
|
||||
match press.owner {
|
||||
|
||||
@@ -65,7 +65,16 @@ impl PdfCraftApp {
|
||||
if self.run_catalog_command(ctx, fired) {
|
||||
return;
|
||||
}
|
||||
self.execute(ctx, id);
|
||||
// In Organize the clipboard rows and keys are the picked pages' (the catalog enables them as
|
||||
// `page.copy`, `page.cut` and `page.paste`); the registry's text Copy itself stays off there.
|
||||
let organize = self.active.and_then(|i| self.views.get(i)).is_some_and(|v| v.organize);
|
||||
let page_command = match id {
|
||||
"edit.copy" => Some("page.copy"),
|
||||
crate::menus::CUT => Some("page.cut"),
|
||||
crate::menus::PASTE => Some("page.paste"),
|
||||
_ => None,
|
||||
};
|
||||
self.execute(ctx, page_command.filter(|_| organize).unwrap_or(id));
|
||||
}
|
||||
|
||||
/// Answers a key press whose command only the app's own state disabled: the user is told
|
||||
@@ -205,7 +214,12 @@ impl PdfCraftApp {
|
||||
|| spec.needs == commands::Needs::Redo && self.open_box_can_redo())
|
||||
&& (spec.id != "edit.copy"
|
||||
|| self.active.and_then(|i| self.views.get(i)).is_some_and(|v| {
|
||||
!v.organize && (v.session.all_text || v.selection_waits_for_text().is_some() || v.selected_text().is_some_and(|t| !t.is_empty()))
|
||||
// Selected text, or a picked picture, which Copy takes in its original format.
|
||||
!v.organize
|
||||
&& (v.session.all_text
|
||||
|| v.selection_waits_for_text().is_some()
|
||||
|| v.selected_text().is_some_and(|t| !t.is_empty())
|
||||
|| v.session.picked_image.is_some())
|
||||
}))
|
||||
&& (spec.needs != commands::Needs::TwoPageView || self.active.and_then(|i| self.views.get(i)).is_some_and(crate::DocView::cover_applies))
|
||||
&& match spec.id {
|
||||
@@ -279,18 +293,6 @@ impl PdfCraftApp {
|
||||
}
|
||||
|
||||
fn execute_unguarded(&mut self, ctx: &egui::Context, id: &str) -> bool {
|
||||
// In Organize the clipboard commands are the picked pages' (the catalog enables them as
|
||||
// `page.copy`, `page.cut` and `page.paste`).
|
||||
let organize = self.active.and_then(|i| self.views.get(i)).is_some_and(|v| v.organize);
|
||||
let page_command = match id {
|
||||
"edit.copy" => Some("page.copy"),
|
||||
crate::menus::CUT => Some("page.cut"),
|
||||
crate::menus::PASTE => Some("page.paste"),
|
||||
_ => None,
|
||||
};
|
||||
if organize && let Some(page_command) = page_command {
|
||||
return self.execute_unguarded(ctx, page_command);
|
||||
}
|
||||
// Paste is the framework's clipboard command, not a registered one: the pages take what
|
||||
// the clipboard holds (objects, a picture or comment text).
|
||||
if id == crate::menus::PASTE {
|
||||
|
||||
@@ -40,6 +40,10 @@ const CATALOG_ONLY: &[(&str, &str, &str)] = &[
|
||||
/// Commands whose chords and rows are the framework's clipboard commands.
|
||||
const FRAMEWORK_EDIT: &[&str] = &["edit.copy", "edit.paste"];
|
||||
|
||||
/// Commands whose keys are the page view's own: it turns a page or spread on ⌘← / ⌘→ and ⌘Page Up / ⌘Page Down
|
||||
/// itself, so a chord here would turn it twice. The menus still show the registry's shortcut.
|
||||
const PAGE_VIEW_KEYS: &[&str] = &["view.next_page", "view.previous_page"];
|
||||
|
||||
static CATALOG_WIN: OnceLock<Option<AppCatalog>> = OnceLock::new();
|
||||
static CATALOG_MAC: OnceLock<Option<AppCatalog>> = OnceLock::new();
|
||||
|
||||
@@ -79,6 +83,9 @@ pub(crate) fn catalog(mac: bool) -> Result<AppCatalog, CommandDefError> {
|
||||
}
|
||||
let mut chords: Vec<Chord> = spec.shortcut_for(mac).and_then(menus::chord).into_iter().collect();
|
||||
chords.extend(second_chords(spec.id));
|
||||
if PAGE_VIEW_KEYS.contains(&spec.id) {
|
||||
chords.clear();
|
||||
}
|
||||
push(def, chords);
|
||||
}
|
||||
for (id, label, shortcut) in EXTRA.iter().filter(|(id, ..)| !FRAMEWORK_EDIT.contains(id)) {
|
||||
@@ -92,15 +99,12 @@ pub(crate) fn catalog(mac: bool) -> Result<AppCatalog, CommandDefError> {
|
||||
}
|
||||
|
||||
/// The chords a registry command takes beside its own shortcut: Alt+← / Alt+→ (⌥ on macOS) go back and forward
|
||||
/// through the view history, as in Acrobat and browsers (#3632), and ⌘Page Up / ⌘Page Down turn a page as ⌘← / ⌘→ do.
|
||||
/// through the view history, as in Acrobat and browsers (#3632).
|
||||
fn second_chords(id: &str) -> Vec<Chord> {
|
||||
let alt = Mods { alt: true, ..Mods::default() };
|
||||
let cmd = Mods { cmd: true, ..Mods::default() };
|
||||
match id {
|
||||
"view.previous_view" => vec![Chord::new(alt, Key::Left)],
|
||||
"view.next_view" => vec![Chord::new(alt, Key::Right)],
|
||||
"view.previous_page" => vec![Chord::new(cmd, Key::PageUp)],
|
||||
"view.next_page" => vec![Chord::new(cmd, Key::PageDown)],
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,13 +24,6 @@ fn fixed_rows(ctx: &egui::Context) -> Vec<FixedRow> {
|
||||
let pair = |a, b| format!("{} / {}", key(a), key(b));
|
||||
// Key names, translated where a catalog has them (not scanned as UI literals).
|
||||
let named = |k: &str| tl!(k).to_string();
|
||||
// Alt+← / Alt+→ (`canvas::shortcuts`): not a registered command, which has no Alt.
|
||||
let alt_arrows = if mac {
|
||||
"⌥← / ⌥→".to_owned()
|
||||
} else {
|
||||
let alt = crate::i18n::key_name("Alt");
|
||||
format!("{alt}+← / {alt}+→")
|
||||
};
|
||||
let scroll =
|
||||
crate::i18n::fmt(tl!("Zoom in / out (also pinch or {key}-scroll)"), &[("key", pdfcraft_engine::commands::Shortcut::command_name(mac))]);
|
||||
let first_last = format!("{} / {}, {}", crate::i18n::key_name("Home"), crate::i18n::key_name("End"), pair(FIRST_PAGE, LAST_PAGE));
|
||||
@@ -48,7 +41,6 @@ fn fixed_rows(ctx: &egui::Context) -> Vec<FixedRow> {
|
||||
(pair(ROTATE_CW, ROTATE_CCW), tl!("Rotate view").to_string()),
|
||||
(first_last, tl!("First / last page").to_string()),
|
||||
(format!("← / →, {}", pair(PAGE_PREV, PAGE_NEXT)), tl!("Previous / next page").to_string()),
|
||||
(alt_arrows, format!("{} / {}", tl!("Previous view"), tl!("Next view"))),
|
||||
(named("V"), tl!("Select (V)").to_string()),
|
||||
(named("H / Space (hold)"), tl!("Hand (H)").to_string()),
|
||||
(crate::i18n::key_name("Delete").to_string(), tl!("Delete selected pages (Organize)").to_string()),
|
||||
@@ -66,10 +58,9 @@ impl PdfCraftApp {
|
||||
let mut options = Options::default();
|
||||
options.editable = false;
|
||||
options.fixed = fixed_rows(ctx);
|
||||
// The catalog the keys run on; its rows are all app-scoped, so no panel or context scope is named.
|
||||
let rows =
|
||||
crate::commands::catalog::shared_catalog(crate::commands::mac_shortcuts(ctx)).map(|catalog| catalog.defs().to_vec()).unwrap_or_default();
|
||||
let keymap = Keymap::new(rows, Scopes::new(), Platform::current());
|
||||
// The catalog the keys run on, in the app scope; the page view's own keys are the fixed rows.
|
||||
let defs = crate::commands::catalog::shared_catalog(crate::commands::mac_shortcuts(ctx)).map(|c| c.defs().to_vec()).unwrap_or_default();
|
||||
let keymap = Keymap::new(defs, Scopes::new(), Platform::current());
|
||||
self.shortcuts = Some(ShortcutsSession { state: DialogState::new(), keymap, options });
|
||||
}
|
||||
|
||||
|
||||
@@ -140,14 +140,15 @@ fn document_scripts(app: &mut PdfCraftApp, ui: &mut egui::Ui, open: &mut bool, d
|
||||
let before = scripts.iter().position(|(n, _)| *n == draft.name);
|
||||
let mut selection = before.map_or_else(Selection::default, Selection::single);
|
||||
let source = ScriptRows(scripts.len());
|
||||
let out = List::new(tl!("Document JavaScripts")).select(SelectMode::Single).selection(Bind::bound(&mut selection)).show_items(
|
||||
ui,
|
||||
&source,
|
||||
&mut |&at, icx| {
|
||||
// A short list that scrolls its own rows, above the script.
|
||||
let out = List::new(tl!("Document JavaScripts"))
|
||||
.select(SelectMode::Single)
|
||||
.selection(Bind::bound(&mut selection))
|
||||
.max_rows(5)
|
||||
.show_items(ui, &source, &mut |&at, icx| {
|
||||
let name = scripts.get(at).map(|(n, _)| n.clone()).unwrap_or_default();
|
||||
ListRow::new(name).striped(icx.at % 2 == 1)
|
||||
},
|
||||
);
|
||||
});
|
||||
// Only the pass's own pick copies; a drained event list is empty on a second pass.
|
||||
if out.events.iter().any(|e| !matches!(e, CollectionEvent::Column(_))) && selection.lead != before {
|
||||
if let Some((name, script)) = selection.lead.and_then(|at| scripts.get(at)) {
|
||||
|
||||
@@ -438,17 +438,21 @@ const EASED: [&str; 2] = ["system", "on"];
|
||||
|
||||
#[test]
|
||||
fn a_wheel_notch_scrolls_at_once_with_animation_effects_off() {
|
||||
// Eased, egui spreads a notch over about a quarter of a second. At once, the page moves the
|
||||
// notch's whole distance in one frame.
|
||||
// Eased, the page view spreads a notch over several frames. At once, the page moves the notch's
|
||||
// whole distance in one frame.
|
||||
let notch = pdfcraft_ui_egui::scroll_speed::ScrollSpeed::default().wheel_line();
|
||||
// The first frame the page stands the whole notch further on.
|
||||
let arrives = |moved: &[f32]| moved.iter().position(|y| (y - notch).abs() < 0.5).unwrap_or(moved.len());
|
||||
let mut at_once = 0;
|
||||
for effects in AT_ONCE {
|
||||
let moved = frames_after_a_notch(effects, false);
|
||||
// The frame the notch arrives in scrolls; the next one draws the page where it stays.
|
||||
assert!(moved[2..].iter().all(|y| (y - notch).abs() < 0.5), "{effects}: one notch is {notch} points, all at once: {moved:?}");
|
||||
at_once = at_once.max(arrives(&moved));
|
||||
}
|
||||
for effects in EASED {
|
||||
let moved = frames_after_a_notch(effects, false);
|
||||
assert!(moved[2] < notch * 0.75, "{effects}: egui eases the notch: {moved:?}");
|
||||
assert!(arrives(&moved) > at_once + 1, "{effects}: the notch eases over more frames than at once ({at_once}): {moved:?}");
|
||||
assert!((moved[39] - notch).abs() < 1.0, "{effects}: easing changes how long a notch takes, not how far it goes: {moved:?}");
|
||||
}
|
||||
}
|
||||
@@ -1002,6 +1006,11 @@ fn from_viewport_corner(h: &Harness<'static, PdfCraftApp>, page: usize, fx: f32,
|
||||
egui::pos2(r.min.x + fx * r.width(), r.min.y + fy * r.height()) - vp.min
|
||||
}
|
||||
|
||||
/// The side gutter the page view keeps clear (the spacing token); a destination's left lands past it (L-245).
|
||||
fn side_gutter(h: &Harness<'static, PdfCraftApp>) -> f32 {
|
||||
h.state().views[0].spacing.as_ref().expect("laid out").side as f32
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bookmark_destinations_scroll_to_their_position() {
|
||||
use egui_kittest::kittest::Queryable;
|
||||
@@ -1024,7 +1033,8 @@ fn bookmark_destinations_scroll_to_their_position() {
|
||||
let v = &h.state().views[0];
|
||||
assert_eq!((v.current(), v.fit(), v.zoom()), (1, Fit::None, 3.0));
|
||||
let d = from_viewport_corner(&h, 1, 0.25, 0.5);
|
||||
assert!((d.x - 70.0).abs() < 2.0 && d.y.abs() < 2.0, "the destination point is at the window's top-left: {d:?}");
|
||||
let side = side_gutter(&h);
|
||||
assert!((d.x - side).abs() < 2.0 && d.y.abs() < 2.0, "the destination point is at the window's top-left, past the gutter {side}: {d:?}");
|
||||
|
||||
// Unusable operands are read as null, which keeps the reader's place in user space: y 300
|
||||
// was at the window's top on page 2 (its crop box starts at y 100), so on page 3 (crop from
|
||||
@@ -1078,7 +1088,8 @@ fn a_link_without_a_position_keeps_the_place_on_the_next_page() {
|
||||
assert_eq!((h.state().views[0].current(), h.state().views[0].zoom()), (0, 4.0));
|
||||
let (fx, fy) = (40.0 / 300.0, 40.0 / 400.0);
|
||||
let d = from_viewport_corner(&h, 0, fx, fy);
|
||||
assert!((d.x - 70.0).abs() < 2.0 && d.y.abs() < 2.0, "(40, 360) is at the window's top-left: {d:?}");
|
||||
let side = side_gutter(&h);
|
||||
assert!((d.x - side).abs() < 2.0 && d.y.abs() < 2.0, "(40, 360) is at the window's top-left, past the gutter {side}: {d:?}");
|
||||
|
||||
let r = rect(&h, 0).expect("page 1");
|
||||
let at = egui::pos2(r.min.x + r.width() * (100.0 / 300.0), r.min.y + r.height() * (1.0 - 335.0 / 400.0));
|
||||
@@ -1090,7 +1101,7 @@ fn a_link_without_a_position_keeps_the_place_on_the_next_page() {
|
||||
let v = &h.state().views[0];
|
||||
assert_eq!((v.current(), v.zoom()), (1, 4.0), "the link goes to page 2 at the same zoom");
|
||||
let d = from_viewport_corner(&h, 1, fx, fy);
|
||||
assert!((d.x - 70.0).abs() < 2.0 && d.y.abs() < 2.0, "page 2 shows the same place, not its top: {d:?}");
|
||||
assert!((d.x - side).abs() < 2.0 && d.y.abs() < 2.0, "page 2 shows the same place, not its top: {d:?}");
|
||||
}
|
||||
|
||||
/// Two pages with a text field on each and a non-embedded Helvetica.
|
||||
|
||||
@@ -175,8 +175,11 @@ fn alt_arrows_go_back_and_forward_through_the_view_history() {
|
||||
h.ctx.set_os(egui::os::OperatingSystem::Nix);
|
||||
assert!(h.execute("help.shortcuts"));
|
||||
h.run_steps(3);
|
||||
h.get_by_label("Alt+← / Alt+→");
|
||||
h.get_by_label("Previous view / Next view");
|
||||
// The keys are the history commands' own bindings, listed with the catalog's rows; the search
|
||||
// brings the row into the list.
|
||||
h.get_by_label("Search:").type_text("Previous view");
|
||||
h.run_steps(3);
|
||||
assert!(h.query_all_by_label_contains("Previous view: Alt+").next().is_some(), "Previous view is listed with its Alt chord");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user