diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 86d4d70666..aaaf7d8497 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -924,10 +924,12 @@ fn page_parts_of( for placed in body.pages { parts.extend(crate::edit_text_ui::picture_parts(session, cx.doc, placed.page, placed, geo)); } - for placed in body.pages { + // A picture being cropped holds every press on its page: its paragraphs and added text take none there. + let cropping = session.image_selection.as_ref().filter(|s| s.crop.is_some()).map(|s| s.page); + for placed in body.pages.iter().filter(|placed| Some(placed.page) != cropping) { parts.extend(crate::edit_text_ui::page_parts(session, cx.doc, placed.page, placed, geo)); } - for placed in body.pages { + for placed in body.pages.iter().filter(|placed| Some(placed.page) != cropping) { parts.extend(crate::content_ui::page_parts(session, cx.doc, placed.page, placed)); } } @@ -978,30 +980,57 @@ fn links_of<'a>(session: &'a PageSession, cx: &'a PageBodyCx<'_>, body: &'a Page PageLinks::new(mode, session, cx.info, cx.doc.links(), body.pages, cx.rotation, cx.sizes).text_links(cx.text_links).tips(cx.unobstructed) } -/// Whether `phase` is the first pass of a press of `button`. +/// Whether `phase` is a phase of a press of `button`: a press and release in one pass report only the release. fn pressed_by(phase: &PressPhase, button: PointerButton) -> bool { - matches!(phase, PressPhase::Down { start, .. } if start.button == button) + phase.start().is_some_and(|start| start.button == button) } -/// What a press on a part does, routed by its owner. -fn route_press(ui: &egui::Ui, session: &mut PageSession, press: &PagePress, cx: &PageBodyCx<'_>, body: &PageBody<'_>, out: &mut BodyOut) { +/// The topmost picture part of `page` under `at` among `parts`, given in paint order. +fn picture_under(parts: &[PagePart], page: usize, at: egui::Pos2) -> Option { + parts.iter().rev().find_map(|part| match part.owner { + Owner::Picture { index } if part.page == page && part.shape.hit(at) => Some(index), + _ => None, + }) +} + +/// What a press on a part does, routed by its owner. `parts` are the pass's parts, in paint order. +fn route_press( + ui: &egui::Ui, + session: &mut PageSession, + press: &PagePress, + parts: &[PagePart], + cx: &PageBodyCx<'_>, + body: &PageBody<'_>, + out: &mut BodyOut, +) { let geo = cx.geometry(body); if pressed_by(&press.phase, PointerButton::Secondary) { // With the primary button held (a comment being moved, a box drawn) the selections stay as they are. let held = ui.input(|i| i.pointer.primary_down()); - // Edit text: a right-click on a picture selects it for its menu, one on anything else unselects it. - if !held && cx.tool == QuickTool::EditText && cx.can_modify { - let picture = match press.owner { - Owner::Picture { index } => Some(index), - _ => None, - }; - crate::edit_text_ui::picture_context(press.page, picture, session); + let mut owner = press.owner.clone(); + if cx.tool == QuickTool::EditText && cx.can_modify { + // Paragraphs, added text and an open paragraph's frame have no menu: the picture under them takes the + // right-click (the user's ruling). + if matches!(owner, Owner::Paragraph { .. } | Owner::Added { .. } | Owner::Frame { .. }) + && let Some(start) = press.phase.start() + && let Some(index) = picture_under(parts, press.page, start.origin) + { + owner = Owner::Picture { index }; + } + // A picture is selected for its menu; a right-click on anything else lets the selected one go. + if !held { + let picture = match owner { + Owner::Picture { index } => Some(index), + _ => None, + }; + crate::edit_text_ui::picture_context(press.page, picture, session); + } } - let mut menu_for = Some((press.page, press.owner.clone())); + let mut menu_for = Some((press.page, owner.clone())); // The menu is anchored on the page the right-click lies on, the first where two pages touch, whichever part // took it. A right-click on another part is the comments' too, unless the tool takes it (Add text, the Link // tool on a link, a field tool): the comment over the part is selected for its menu, or the selection cleared. - if let PressPhase::Down { start, .. } = &press.phase + if let Some(start) = press.phase.start() && let Some(page) = body.pages.iter().find(|placed| placed.rect.contains(start.origin)).map(|placed| placed.page) && let Some(xf) = session.page_xform(page, geo) { @@ -1014,7 +1043,7 @@ fn route_press(ui: &egui::Ui, session: &mut PageSession, press: &PagePress, cx: }; // Added text, pictures and the objects selected together lie over the comments: a right-click on them // reaches no comment under them and leaves the selection cleared. - let over_comments = matches!(press.owner, Owner::Added { .. } | Owner::Picture { .. } | Owner::Objects | Owner::Frame { .. }); + let over_comments = matches!(owner, Owner::Added { .. } | Owner::Picture { .. } | Owner::Objects | Owner::Frame { .. }); if held || tool_takes_it || matches!(press.owner, Owner::Comment { .. }) { comments::context_anchor(&page_cx, &mut session.comments, start.origin); } else if over_comments { @@ -1146,7 +1175,7 @@ fn page_body(ui: &mut egui::Ui, body: &PageBody<'_>, session: &mut PageSession, if matches!(press.phase, PressPhase::Down { .. }) { session.all_text = false; } - route_press(ui, session, press, cx, body, out); + route_press(ui, session, press, &parts, cx, body, out); } // Focus arrived on a field (Tab, a request or a click): the field opens, a text field with its text selected. let field_arrived = match &parts_out.focused { @@ -1168,7 +1197,8 @@ fn page_body(ui: &mut egui::Ui, body: &PageBody<'_>, session: &mut PageSession, // A press nobody's part took, on the page or the gap between pages, ends Select All of the document. session.all_text = false; } - let tool_cx = PageToolCx { pages: body.pages, viewport: body.rect, ..cx.tools }; + let button_held = ui.input(|i| i.pointer.any_down()); + let tool_cx = PageToolCx { pages: body.pages, viewport: body.rect, button_held, ..cx.tools }; let mut bench = page_tools::ToolBench::default(); let mut fill = None; let mut tool = page_tool(cx.chosen, tool_cx, &mut *session, &mut bench, &mut fill, &pass.press); @@ -1488,6 +1518,7 @@ pub fn document_area_at(app: &mut PdfCraftApp, index: usize, rect: Rect, ui: &mu sizes: &page_sizes[..], rotation: state.rotation, viewport: Rect::NOTHING, + button_held: false, text_style: &app.text_style, allowed, can_modify, diff --git a/crates/ui-egui/src/canvas/page_tools.rs b/crates/ui-egui/src/canvas/page_tools.rs index 6ac78940e7..472196c008 100644 --- a/crates/ui-egui/src/canvas/page_tools.rs +++ b/crates/ui-egui/src/canvas/page_tools.rs @@ -75,6 +75,9 @@ pub(crate) struct PageToolCx<'a> { pub(crate) rotation: u16, /// The page body's rect this pass, filled from the body each run as `pages` is. pub(crate) viewport: egui::Rect, + /// A pointer button is down this pass, filled from the body each run as `pages` is: a key waits for it to come + /// up where the press held would then name another item. + pub(crate) button_held: bool, /// The style new text gets (Edit a PDF ▸ Format text). pub(crate) text_style: &'a pdfcraft_engine::AddedText, /// The document allows comments. @@ -353,7 +356,9 @@ pub(crate) fn page_tool<'a>( } QuickTool::Field(field_tool) => Arm::Area(Box::new(PrepareTool::new(Some(field_tool), cx.can_modify, cx.form, session, cx.info, xforms))), QuickTool::AddText => Arm::Area(Box::new(crate::tools::content_ui::AddTextTool::new(session, cx.doc, cx.pages, cx.text_style))), - QuickTool::EditText => Arm::Area(Box::new(EditTextTool::new(session, cx.doc, cx.info, cx.pages, press, cx.geometry()))), + QuickTool::EditText => { + Arm::Area(Box::new(EditTextTool::new(session, cx.doc, cx.info, cx.pages, press, cx.geometry()).button_held(cx.button_held))) + } QuickTool::Stamp(kind) => { Arm::Stamp(StampTool::new(StampChoice::Builtin(kind), cx.by_line.to_owned(), None, cx.author.to_owned(), cx.pages, cx.info, map)) } diff --git a/crates/ui-egui/src/tools/edit_text_tool.rs b/crates/ui-egui/src/tools/edit_text_tool.rs index 7220f7067f..effcfd2aa9 100644 --- a/crates/ui-egui/src/tools/edit_text_tool.rs +++ b/crates/ui-egui/src/tools/edit_text_tool.rs @@ -23,6 +23,8 @@ pub(crate) struct EditTextTool<'a> { press: &'a PressState, /// The page sizes and rotation the pages are placed with. geo: PageGeometry<'a>, + /// A pointer button is down this pass. + button_held: bool, } impl<'a> EditTextTool<'a> { @@ -36,7 +38,14 @@ impl<'a> EditTextTool<'a> { press: &'a PressState, geo: PageGeometry<'a>, ) -> Self { - Self { session, doc, info, placed, press, geo } + Self { session, doc, info, placed, press, geo, button_held: false } + } + + /// The tool with a pointer button down this pass (`held`): Delete waits for it to come up. + #[must_use] + pub(crate) fn button_held(mut self, held: bool) -> Self { + self.button_held = held; + self } /// Something is selected that an arrow moves or Delete removes. @@ -100,6 +109,11 @@ impl CanvasTool for EditTextTool<'_> { if !self.has_selection() { return false; } + // Delete waits for the button to come up: the press held would then name another item, whose + // drag or click would follow. The key is still the tool's. + if self.button_held { + return true; + } let edit = object_ui::delete(self.session) .or_else(|| crate::edit_text_ui::delete_picture(self.session, self.placed)) .or_else(|| crate::content_ui::delete_selected(self.session)); diff --git a/crates/ui-egui/src/tools/edit_text_ui/image.rs b/crates/ui-egui/src/tools/edit_text_ui/image.rs index 89279e1edd..040d396841 100644 --- a/crates/ui-egui/src/tools/edit_text_ui/image.rs +++ b/crates/ui-egui/src/tools/edit_text_ui/image.rs @@ -2,7 +2,7 @@ use super::{DocInfo, Edit, MAX_OUTLINES, PageGeometry, PageSession, PageXform, P use crate::canvas::page_parts::{Owner, PageLayer, PagePart, PagePress}; use craft::behavior::geometry::{BoxAllow, BoxRules, BoxState, CropBox, CropGuide, ScaleRule, TransformMode}; use craft::behavior::kurbo::{BezPath, Point, Rect as DocRect, Vec2}; -use craft::behavior::{CursorIcon, EditEvent, HitShape, IconId, PointerButton}; +use craft::behavior::{CanvasView, CursorIcon, EditEvent, HitShape, IconId, PointerButton}; use craft::draw::ui::transform::{RotateZone, TransformBox}; use craft::imaging::ui::transform_preview::{PreviewMap, PreviewPixels, PreviewTextures, TransformPreview}; use craft::pages::ui::{PageBody, PlacedPage}; @@ -192,16 +192,18 @@ fn cut_preview(doc: &pdfcraft_engine::Document, placed: &PlacedPage, index: usiz Some(PreviewTextures::new(format!("picture-{}-{index}", placed.page), PreviewPixels::new(image, [1.0, 1.0]))) } -fn norm_rect(placed: &PlacedPage, xf: &PageXform, r: DocRect) -> Rect { +/// `r`, in the canvas's document space, as the page's 0..1 coordinates. +fn norm_rect(view: &CanvasView, xf: &PageXform, r: DocRect) -> Rect { let n = |x: f64, y: f64| { - let (u, v) = xf.screen_to_norm(placed.to_screen(Point::new(x, y))); + let (u, v) = xf.screen_to_norm(view.to_screen(Point::new(x, y))); Pos2::new(u, v) }; Rect::from_two_pos(n(r.x0, r.y0), n(r.x1, r.y1)) } -fn doc_rect(placed: &PlacedPage, xf: &PageXform, r: Rect) -> DocRect { - let d = |p: Pos2| placed.to_page(xf.norm_to_screen(p.x, p.y)); +/// `r`, in the page's 0..1 coordinates, in the canvas's document space. +fn doc_rect(view: &CanvasView, xf: &PageXform, r: Rect) -> DocRect { + let d = |p: Pos2| view.to_doc(xf.norm_to_screen(p.x, p.y)); DocRect::from_points(d(r.min), d(r.max)) } @@ -238,7 +240,7 @@ pub(crate) fn picture_controls( .iter() .enumerate() .filter(|(i, _)| !(moving && Some(*i) == selected) && (all || Some(*i) == selected)) - .map(|(_, image)| path_of(&picture_quad(&xf, info, placed.page, image).map(|p| placed.to_page(p)))) + .map(|(_, image)| path_of(&picture_quad(&xf, info, placed.page, image).map(|p| body.view.to_doc(p)))) .collect(); if !paths.is_empty() { let _ = SelectionOutline::faint(tl!("Pictures"), &paths).show(ui); @@ -269,7 +271,8 @@ fn box_controls( { s.preview = None; } - let quad = picture_quad(xf, info, page, image).map(|p| placed.to_page(p)); + let view = &body.view; + let quad = picture_quad(xf, info, page, image).map(|p| view.to_doc(p)); let bounds = bounds_of(&quad); let pivot = bounds.center(); let rules = BoxRules::in_mode(TransformMode::Free) @@ -283,7 +286,7 @@ fn box_controls( // The arrow keys nudge through the tool (`EditTextTool::key`). .session_arrows(false) .show_at(ui, body.rect); - let to_user = |p: Point| screen_to_user(xf, info, page, placed.to_screen(p)); + let to_user = |p: Point| screen_to_user(xf, info, page, view.to_screen(p)); let mut edit = None; for event in shown.edit.events.iter() { match event { @@ -297,7 +300,7 @@ fn box_controls( EditEvent::Commit { after, .. } => { edit = picture_edit(page, index, image.matrix, image.rect, quad, after.quad, &to_user); if let Some(s) = session.image_selection.as_mut() { - s.settled = edit.as_ref().map(|_| (norm_rect(placed, xf, bounds), norm_rect(placed, xf, bounds_of(&after.quad)))); + s.settled = edit.as_ref().map(|_| (norm_rect(view, xf, bounds), norm_rect(view, xf, bounds_of(&after.quad)))); if s.settled.is_none() { s.preview = None; } @@ -316,11 +319,11 @@ fn box_controls( s.paper = None; let Some(textures) = s.preview.as_ref() else { return edit }; let (from, to) = match s.settled { - Some((from, to)) => (corners_of(doc_rect(placed, xf, from)), corners_of(doc_rect(placed, xf, to))), + Some((from, to)) => (corners_of(doc_rect(view, xf, from)), corners_of(doc_rect(view, xf, to))), None => (quad, shown.edit.shown.as_ref().and_then(|m| m.same()).map_or(quad, |state| state.quad)), }; - s.paper = Some(from); - let full = matrix_corners(xf, info, page, image.matrix).map(|p| placed.to_page(p)); + s.paper = Some(from.map(|p| placed.to_page(view.to_screen(p)))); + let full = matrix_corners(xf, info, page, image.matrix).map(|p| view.to_doc(p)); let map = affine_between(from, to); let _ = TransformPreview::new(tl!("Picture"), textures).map(PreviewMap::Quad(full.map(map))).picture().show(ui); edit @@ -330,9 +333,10 @@ fn crop_controls(ui: &mut egui::Ui, body: &PageBody<'_>, session: &mut PageSessi let PagePicture { placed, xf, info, image, .. } = *pic; let page = placed.page; let keep = session.image_selection.as_ref().and_then(|s| s.crop.as_ref()).map(|c| c.keep)?; - let frame = picture_quad(xf, info, page, image).map(|p| placed.to_page(p)); + let view = &body.view; + let frame = picture_quad(xf, info, page, image).map(|p| view.to_doc(p)); let kept_screen = xf.user_rect(info, page, keep.map(|v| v as f32)); - let kept = DocRect::from_points(placed.to_page(kept_screen.min), placed.to_page(kept_screen.max)); + let kept = DocRect::from_points(view.to_doc(kept_screen.min), view.to_doc(kept_screen.max)); let before = CropBox::upright(kept); let mut crop = before; let shown = CropOverlay::new(tl!("Crop"), Bind::bound(&mut crop)) @@ -343,7 +347,7 @@ fn crop_controls(ui: &mut egui::Ui, body: &PageBody<'_>, session: &mut PageSessi .guide((CropGuide::Thirds, 0)) .show_at(ui, body.rect); if crop.is_finite() && crop.rect != before.rect { - let to_screen = |p: Point| placed.to_screen(p); + let to_screen = |p: Point| view.to_screen(p); let moved = Rect::from_two_pos(to_screen(Point::new(crop.rect.x0, crop.rect.y0)), to_screen(Point::new(crop.rect.x1, crop.rect.y1))); let keep = user_box(xf, info, page, moved); if let Some(mode) = session.image_selection.as_mut().and_then(|s| s.crop.as_mut()) { @@ -426,11 +430,14 @@ fn picture_change( if s.iter().chain(&e).any(|p| !p.0.is_finite() || !p.1.is_finite()) { return None; } - let (dx, dy) = (e[0].0 - s[0].0, e[0].1 - s[0].1); - if (0..4).all(|i| (e[i].0 - s[i].0 - dx).abs() < EPS && (e[i].1 - s[i].1 - dy).abs() < EPS) { - if dx.abs() < EPS && dy.abs() < EPS { + // A translation is told where the box made it, in document units: user space is reached through screen + // points, whose rounding would make an exact move look like a slight resize. + let moved = end[0] - start[0]; + if (0..4).all(|i| ((end[i] - start[i]) - moved).hypot() < EPS) { + if moved.hypot() < EPS { return None; } + let (dx, dy) = (e[0].0 - s[0].0, e[0].1 - s[0].1); return Some(E::Move([rect[0] + dx, rect[1] + dy, rect[2] + dx, rect[3] + dy])); } let (sw, sh, ew, eh) = (start[1] - start[0], start[3] - start[0], end[1] - end[0], end[3] - end[0]); diff --git a/crates/ui-egui/src/workflows/combine_ui/view.rs b/crates/ui-egui/src/workflows/combine_ui/view.rs index 17f60a53bc..56c1344f54 100644 --- a/crates/ui-egui/src/workflows/combine_ui/view.rs +++ b/crates/ui-egui/src/workflows/combine_ui/view.rs @@ -9,7 +9,7 @@ use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _ use craft::ui::dialog::Prompt; use craft::ui::display::Text; use craft::ui::menu::Menu; -use craft::ui::notices::Notice; +use craft::ui::notices::{Notice, NoticeAction}; use craft::ui::patterns::form_dialogs::{PasswordDialog, PasswordPurpose}; use craft::ui::popover::DropVeil; use craft::ui::sections::{DialogRole, FooterButton, Panel}; @@ -128,9 +128,18 @@ pub(crate) fn page(app: &mut PdfCraftApp, ui: &mut egui::Ui) { dropped = Some(files.files.clone()); } if n == 0 { - Notice::new(Tone::Info, tl!("Add the files to combine")) + // Choose the files, or on the desktop a folder of them. + let mut empty = Notice::new(Tone::Info, tl!("Add the files to combine")) .lines([tl!("Drop PDFs, images, text files or folders here, or choose them.")]) - .show(ui); + .action(NoticeAction::new(tl!("Add files…")).default_action()); + if cfg!(not(target_arch = "wasm32")) { + empty = empty.action(NoticeAction::new(tl!("Add folder…"))); + } + match empty.show(ui).action { + Some(0) => app.combine_dialog(), + Some(1) => app.combine_add_folder(false), + _ => {} + } } else if grid { grid_actions = crate::combine_grid::grid(app, ui, &checks, &twice); // PLAN: assumed `combine_grid::preview(app, ui)`, the sheet names it `card::preview` (pdf-workflows-w7)