Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge remote-tracking branch 'upstream/main' into main

Combiler013dfcff3884c7
diff --cc crates/ui-egui/src/canvas.rs
index 59f524e8f,4f00aa6be..9f15ca0f3
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@@ -4788,9 -4258,11 +4934,9 @@@ pub fn document_area(app: &mut PdfCraft
}
// Over a picture (and no comment): Copy Image, as in Acrobat's page menu.
let mut copy_image = None;
- if view.comments.selected.is_none()
- && let Some((page, at)) = view.comments.context_at
- {
+ if let Some((page, at)) = view.comments.context_at {
// The page's content is parsed once per right-click, not on every frame the menu is open.
- let key = egui::Id::new("copy-image-under-menu");
+ let key = egui::Id::unique("copy-image-under-menu");
let cached = ui.data(|d| d.get_temp::<(usize, [f64; 2], Option<usize>)>(key)).filter(|c| c.0 == page && c.1 == at);
let hit = match cached {
Some(c) => c.2,
diff --cc crates/ui-egui/src/edit_text_ui.rs
index 1a743241a,4f5630c0b..8ff5e3267
--- a/crates/ui-egui/src/edit_text_ui.rs
+++ b/crates/ui-egui/src/edit_text_ui.rs
@@@ -969,19 -460,30 +969,32 @@@ pub struct ImageDrag
pub struct ImageSelection {
pub page: usize,
pub index: usize,
- /// Dragging: the start point and, for a corner, the opposite corner (screen).
- drag: Option<(Pos2, Option<Pos2>)>,
+ /// Dragging in progress.
+ pub drag: Option<ImageDrag>,
/// Crop mode (Acrobat's crop handles): the box kept so far.
pub crop: Option<CropMode>,
+ pub crop_mode: bool,
+ pub crop_rect: Option<Rect>,
/// The drag is turning the picture by its rotate handle (the `drag` start is then unused).
- turning: bool,
+ pub turning: bool,
/// A move or resize applied on release: the frame it had and the one it got, in the page's
/// 0..1 coordinates, while the page raster still has the picture where it was.
- settled: Option<(Rect, Rect)>,
+ pub settled: Option<(Rect, Rect)>,
}
+ impl ImageSelection {
+ /// Cancel an in-progress text, rotation or crop drag and restore the crop it was previewing.
+ pub(crate) fn cancel_pointer_gesture(&mut self) {
+ self.drag = None;
+ self.turning = false;
+ if let Some(crop) = &mut self.crop
+ && let Some((_, _, before)) = crop.drag.take()
+ {
+ crop.keep = before;
+ }
+ }
+ }
+
/// Cropping a page image: the handles trim instead of resizing, the part that will go is
/// dimmed, Enter or Done applies and Esc or Cancel leaves it.
#[derive(Clone, Debug, PartialEq)]
diff --cc crates/ui-egui/src/home.rs
index 4912b4ac3,bfabf701e..781da2a35
--- a/crates/ui-egui/src/home.rs
+++ b/crates/ui-egui/src/home.rs
@@@ -18,57 -18,7 +18,58 @@@ pub(crate) fn card_lift(ui: &egui::Ui,
}
}
+/// Render a single tool card with the standard lift, styling, icon, title, description, and action.
+/// If `allow_unpin` is true, an unpin button appears on hover in the top right corner.
+fn tool_card(app: &mut PdfCraftApp, ui: &mut egui::Ui, t: &Tokens, g: &'static catalog::ToolGroup, allow_unpin: bool) -> Option<&'static str> {
+ let (rect, resp) = ui.allocate_exact_size(vec2(190.0, 104.0), Sense::click());
- resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!(g.label)));
++ resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!(g.label)));
+ let press = theme::Press::track(ui, &resp);
+ card_lift(ui, t, rect, press);
+ let fill = press.fill(t.card, t.hover, t.pressed);
+ ui.painter().rect(rect, CornerRadius::same(10), fill, Stroke::new(1.0, t.divider), egui::StrokeKind::Inside);
+ let body = rect.translate(press.offset());
+ let color = egui::Color32::from_rgb(g.hue[0], g.hue[1], g.hue[2]);
+ icons::paint(ui, Rect::from_min_size(body.min + vec2(14.0, 14.0), vec2(22.0, 22.0)), g.icon, 21.0, color);
+
+ let mut unpin_clicked = false;
+ if allow_unpin && (ui.rect_contains_pointer(rect) || resp.hovered()) {
+ let unpin_rect = Rect::from_center_size(body.right_top() + vec2(-18.0, 18.0), vec2(22.0, 22.0));
+ let unpin_resp = ui.interact(unpin_rect, ui.id().with(("unpin_card_btn", g.id)), Sense::click());
- unpin_resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!("Unpin tool")));
++ unpin_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("Unpin tool")));
+ if unpin_resp.hovered() {
+ ui.painter().rect_filled(unpin_rect, CornerRadius::same(4), t.pressed);
+ }
+ icons::paint(ui, unpin_rect.shrink(4.0), "x", 14.0, t.icon);
+ if unpin_resp.on_hover_text(tl!("Unpin from Home")).clicked() {
+ unpin_clicked = true;
+ }
+ }
+
+ let mut title_job = egui::text::LayoutJob::simple_singleline(tl!(g.label).to_owned(), theme::semibold(13.5), t.text);
+ title_job.wrap = egui::text::TextWrapping { max_width: body.width() - 58.0, max_rows: 1, break_anywhere: true, ..Default::default() };
+ let title = ui.fonts_mut(|f| f.layout_job(title_job));
+ let title_elided = title.elided;
+ ui.painter().galley(body.min + vec2(44.0, 25.0 - title.size().y * 0.5), title, t.text);
+ let blurb = g.sections.first().map(|s| s.items.iter().take(3).map(|i| tl!(i.label)).collect::<Vec<_>>().join(" · ")).unwrap_or_default();
+ // Fixed-height cards must leave room for their action in every language.
+ let mut job = egui::text::LayoutJob::simple(blurb.clone(), theme::regular(11.5), t.text_muted, body.width() - 28.0);
+ job.wrap.max_rows = 2;
+ let galley = ui.fonts_mut(|f| f.layout_job(job));
+ let blurb_elided = galley.elided;
+ ui.painter().galley(body.min + vec2(14.0, 46.0), galley, t.text_muted);
+ ui.painter().text(body.left_bottom() + vec2(14.0, -14.0), Align2::LEFT_CENTER, tl!("Use now"), theme::medium(12.0), t.accent_text);
+ let resp = if title_elided || blurb_elided { resp.on_hover_text(format!("{}\n{blurb}", tl!(g.label))) } else { resp };
+ if unpin_clicked {
+ Some(g.id)
+ } else {
+ if theme::hand(resp).clicked() {
+ app.select_tool(g.id);
+ }
+ None
+ }
+}
+
+ /// Draw the Home page with the welcome text, community card and recent documents.
pub fn show(app: &mut PdfCraftApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
@@@ -122,10 -72,46 +123,10 @@@
ui.spacing_mut().item_spacing = vec2(14.0, 14.0);
for id in RECOMMENDED {
let Some(g) = catalog::group(id) else { continue };
- let (rect, resp) = ui.allocate_exact_size(vec2(190.0, 104.0), Sense::click());
- resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!(g.label)));
- let press = theme::Press::track(ui, &resp);
- card_lift(ui, &t, rect, press);
- let fill = press.fill(t.card, t.hover, t.pressed);
- ui.painter().rect(rect, CornerRadius::same(10), fill, Stroke::new(1.0, t.divider), egui::StrokeKind::Inside);
- let body = rect.translate(press.offset());
- let color = egui::Color32::from_rgb(g.hue[0], g.hue[1], g.hue[2]);
- icons::paint(ui, Rect::from_min_size(body.min + vec2(14.0, 14.0), vec2(22.0, 22.0)), g.icon, 21.0, color);
- let mut title_job = egui::text::LayoutJob::simple_singleline(tl!(g.label).to_owned(), theme::semibold(13.5), t.text);
- title_job.wrap =
- egui::text::TextWrapping { max_width: body.width() - 58.0, max_rows: 1, break_anywhere: true, ..Default::default() };
- let title = ui.fonts_mut(|f| f.layout_job(title_job));
- let title_elided = title.elided;
- ui.painter().galley(body.min + vec2(44.0, 25.0 - title.size().y * 0.5), title, t.text);
- let blurb = g
- .sections
- .first()
- .map(|s| s.items.iter().take(3).map(|i| tl!(i.label)).collect::<Vec<_>>().join(" · "))
- .unwrap_or_default();
- // Fixed-height cards must leave room for their action in every language.
- let mut job = egui::text::LayoutJob::simple(blurb.clone(), theme::regular(11.5), t.text_muted, body.width() - 28.0);
- job.wrap.max_rows = 2;
- let galley = ui.fonts_mut(|f| f.layout_job(job));
- let blurb_elided = galley.elided;
- ui.painter().galley(body.min + vec2(14.0, 46.0), galley, t.text_muted);
- ui.painter().text(
- body.left_bottom() + vec2(14.0, -14.0),
- Align2::LEFT_CENTER,
- tl!("Use now"),
- theme::medium(12.0),
- t.accent_text,
- );
- let resp = if title_elided || blurb_elided { resp.on_hover_text(format!("{}\n{blurb}", tl!(g.label))) } else { resp };
- if theme::hand(resp).clicked() {
- app.select_tool(g.id);
- }
+ tool_card(app, ui, &t, g, false);
}
let (rect, resp) = ui.allocate_exact_size(vec2(170.0, 104.0), Sense::click());
- resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!("Open file")));
+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("Open file")));
let press = theme::Press::track(ui, &resp);
card_lift(ui, &t, rect, press);
ui.painter().rect(
diff --cc crates/ui-egui/src/lib.rs
index 07133a7d2,79f46bee7..bd632a943
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -2487,51 -2474,8 +2490,50 @@@ impl PdfCraftApp
pub(crate) fn reveal_tools(&mut self) {
self.left_open = true;
- self.pending_panel_reveals.push(docking::Panel::Tools);
}
+ /// Check whether a tool group is pinned to Home.
+ pub fn is_tool_pinned(&self, id: &str) -> bool {
+ self.pinned_tools.iter().any(|t| t == id)
+ }
+
+ /// Pin a tool group to Home. Returns true if newly pinned.
+ pub fn pin_tool(&mut self, id: &str) -> bool {
+ if pdfcraft_engine::catalog::group(id).is_none() {
+ return false;
+ }
+ if !self.is_tool_pinned(id) {
+ self.pinned_tools.push(id.to_string());
+ if let Some(g) = pdfcraft_engine::catalog::group(id) {
+ self.notify_fmt("Pinned {tool} to Home", &[("tool", tl!(g.label))]);
+ }
+ true
+ } else {
+ false
+ }
+ }
+
+ /// Remove a tool group from Home.
+ pub fn unpin_tool(&mut self, id: &str) {
+ if let Some(pos) = self.pinned_tools.iter().position(|t| t == id) {
+ self.pinned_tools.remove(pos);
+ if let Some(g) = pdfcraft_engine::catalog::group(id) {
+ self.notify_fmt("Unpinned {tool} from Home", &[("tool", tl!(g.label))]);
+ }
+ }
+ }
+
+ /// Toggle a tool group's pinned state on Home.
+ pub fn toggle_pin_tool(&mut self, id: &str) -> bool {
+ if self.is_tool_pinned(id) {
+ self.unpin_tool(id);
+ false
+ } else {
+ self.pin_tool(id);
+ true
+ }
+ }
+
/// Enter a tool from the catalogue, Home, palette or control channel. Organize is a
/// workspace, so opening its panel also opens the page grid (without toggling it off).
pub(crate) fn select_tool(&mut self, id: &'static str) {
diff --cc crates/ui-egui/src/panels.rs
index 949f39cd7,bcc4123f2..73bac867e
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@@ -112,31 -108,22 +112,32 @@@ fn row_label(ui: &mut egui::Ui, text: &
elided
}
-/// Draw one tool-group row with its icon, label, badge and availability hint.
-fn tool_row(ui: &mut egui::Ui, t: &Tokens, g: &ToolGroup) -> egui::Response {
++/// Draw one tool-group row with its icon, label, badge, pin toggle and availability hint.
+fn tool_row(ui: &mut egui::Ui, t: &Tokens, g: &ToolGroup, is_pinned: bool) -> (egui::Response, bool) {
let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), 34.0), Sense::click());
- resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!(g.label)));
+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!(g.label)));
let press = theme::Press::track(ui, &resp);
press.wash(ui, rect, 6, false);
let body = rect.translate(press.offset());
icons::paint(ui, Rect::from_min_size(body.min + vec2(6.0, 7.0), vec2(20.0, 20.0)), g.icon, 19.0, hue(g));
- let right_reserved = match (g.badge, g.availability) {
- (Some(b), _) => ui.fonts_mut(|f| f.layout_no_wrap(tl!(b).to_owned(), theme::semibold(9.5), Color32::WHITE).size().x) + 26.0,
+
+ let pin_rect = Rect::from_center_size(body.right_center() - vec2(14.0, 0.0), vec2(22.0, 22.0));
+ let pin_resp = ui.interact(pin_rect, ui.id().with(("pin_toggle_row", g.id)), Sense::click());
+ let tip = if is_pinned { tl!("Unpin from Home") } else { tl!("Pin to Home") };
- pin_resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tip));
++ pin_resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tip));
+ let pin_hovered = pin_resp.hovered();
+ let mut pin_clicked = false;
+
+ let pin_space = 26.0;
+ let badge_or_hint_reserved = match (g.badge, g.availability) {
+ (Some(b), _) => ui.fonts_mut(|f| f.layout_no_wrap(tl!(b).to_owned(), theme::semibold(9.5), Color32::WHITE).size().x) + 18.0,
(None, Availability::Planned(m)) if resp.hovered() => {
let hint = format!("{} · {m}", tl!("Planned"));
- ui.fonts_mut(|f| f.layout_no_wrap(hint, theme::medium(10.5), t.text_faint).size().x) + 18.0
+ ui.fonts_mut(|f| f.layout_no_wrap(hint, theme::medium(10.5), t.text_faint).size().x) + 10.0
}
- _ => 8.0,
+ _ => 0.0,
};
+ let right_reserved = pin_space + badge_or_hint_reserved + 8.0;
let clipped =
row_label(ui, tl!(g.label), theme::regular(13.5), t.text, body.left_center() + vec2(36.0, 0.0), rect.width() - 36.0 - right_reserved);
match (g.badge, g.availability) {
@@@ -187,9 -153,10 +188,10 @@@
Availability::Provider => tl!("Optional: needs an AI provider you configure").into(),
};
let tip = if clipped { format!("{} — {tip}", tl!(g.label)) } else { tip };
- theme::hand(resp).on_hover_text(tip)
+ (theme::hand(resp).on_hover_text(tip), pin_clicked)
}
+ /// Draw the detail view for one tool group, dispatching its commands and tool-specific panels.
fn tool_detail(app: &mut PdfCraftApp, ui: &mut egui::Ui, t: &Tokens, g: &'static ToolGroup) {
if g.id == "edit" {
edit_panel(app, ui, t, g);
diff --cc crates/ui-egui/src/updates.rs
index 73a616838,fa59f9a89..85f433740
--- a/crates/ui-egui/src/updates.rs
+++ b/crates/ui-egui/src/updates.rs
@@@ -14,53 -10,34 +10,35 @@@ use crate::{PdfCraftApp, theme, widgets
/// Where every PdfCraft release is listed.
pub const RELEASES_PAGE: &str = "https://github.com/storytold/pdfcraft/releases";
- /// The latest published release.
- #[derive(Clone, Debug, PartialEq, Eq)]
- pub struct Release {
- /// Its version tag, such as `v0.2.0`.
- pub version: String,
- /// Its page on [`RELEASES_PAGE`], where the downloads are.
- pub url: String,
- }
-
- /// Asks for the latest release (blocking; it runs on its own thread).
- pub type UpdateSource = Arc<dyn Fn() -> Result<Release, String> + Send + Sync>;
-
- /// Whether release `latest` (a tag such as `v0.2.0`) is newer than version `current` (`0.1.1`).
- /// Pre-release and build suffixes are ignored; a version that doesn't parse is never newer.
- pub fn is_newer(latest: &str, current: &str) -> bool {
- matches!((parse(latest), parse(current)), (Some(l), Some(c)) if l > c)
- }
-
- fn parse(v: &str) -> Option<(u64, u64, u64)> {
- let v = v.trim().trim_start_matches(['v', 'V']);
- let core = v.split(['-', '+']).next()?;
- let mut parts = core.split('.');
- let mut next = |required: bool| match parts.next() {
- Some(p) => p.parse::<u64>().ok(),
- None if required => None,
- None => Some(0),
- };
- let version = (next(true)?, next(false)?, next(false)?);
- parts.next().is_none().then_some(version)
- }
-
- /// Where a check is.
- #[derive(Default)]
- pub(crate) enum Check {
- #[default]
- Idle,
- #[cfg(not(target_arch = "wasm32"))]
- Running(std::sync::mpsc::Receiver<Result<Release, String>>),
- #[allow(dead_code)]
- Done(Result<Release, String>),
- }
+ /// Stands in for the network in tests.
+ #[cfg(not(target_arch = "wasm32"))]
+ pub type UpdateSource = CheckSource;
+#[derive(Default)]
pub(crate) struct Updates {
- pub(crate) check: Check,
+ #[cfg(not(target_arch = "wasm32"))]
+ updater: Updater,
/// The Updates dialog is showing.
pub(crate) open: bool,
+ /// Ask for a newer release at start; the desktop app turns it on, so tests never reach the network.
+ #[cfg(not(target_arch = "wasm32"))]
+ startup_check: bool,
+ #[cfg(not(target_arch = "wasm32"))]
+ check_requested: bool,
+ }
+
+ impl Default for Updates {
+ fn default() -> Self {
+ Self {
+ #[cfg(not(target_arch = "wasm32"))]
+ updater: Updater::new(&craft_update::suite::PDFCRAFT, env!("CARGO_PKG_VERSION")),
+ open: false,
+ #[cfg(not(target_arch = "wasm32"))]
+ startup_check: false,
+ #[cfg(not(target_arch = "wasm32"))]
+ check_requested: false,
+ }
+ }
}
impl PdfCraftApp {