mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
Keep large PDF previews and confirmed page imports off the desktop UI thread
This commit is contained in:
1 parent
8df3fd17ac
commit
7c8d4ea5a7
2 files changed
+74
-9
No files matched your search
@@ -114,11 +114,19 @@ pub(super) fn render_preview(
|
||||
app: &mut VectorcraftApp,
|
||||
ctx: &egui::Context,
|
||||
doc: &vectorcraft_engine::doc::Document,
|
||||
) -> Option<Arc<egui::ColorImage>> {
|
||||
render_page_preview(&mut app.canvas.renderer, ctx.pixels_per_point(), doc)
|
||||
}
|
||||
|
||||
fn render_page_preview(
|
||||
renderer: &mut vectorcraft_render::Renderer,
|
||||
pixels_per_point: f32,
|
||||
doc: &vectorcraft_engine::doc::Document,
|
||||
) -> Option<Arc<egui::ColorImage>> {
|
||||
let region = doc.artboards.first()?.rect;
|
||||
let px = PREVIEW_PX * ctx.pixels_per_point() as f64;
|
||||
let px = PREVIEW_PX * pixels_per_point as f64;
|
||||
let scale = px / region.width().max(region.height()).max(1.0);
|
||||
let img = app.canvas.renderer.render_region(doc, region, scale, true);
|
||||
let img = renderer.render_region(doc, region, scale, true);
|
||||
Some(Arc::new(crate::canvas::color_image(&img)))
|
||||
}
|
||||
|
||||
@@ -286,6 +294,8 @@ pub(super) struct ImportPdfDialog {
|
||||
/// The preview shown for a (page, crop, text) revision; `None` for a page that could not be read, which is
|
||||
/// not asked for again.
|
||||
preview: Option<(Revision, Option<Arc<egui::ColorImage>>)>,
|
||||
/// One preview at a time. Page changes coalesce while the worker finishes.
|
||||
pending_preview: Option<(Revision, std::sync::mpsc::Receiver<Option<Arc<egui::ColorImage>>>)>,
|
||||
/// Why the last password did not open the file.
|
||||
password_error: Option<String>,
|
||||
/// The OK the pump has not answered.
|
||||
@@ -294,7 +304,7 @@ pub(super) struct ImportPdfDialog {
|
||||
|
||||
impl ImportPdfDialog {
|
||||
fn new(s: State) -> Self {
|
||||
ImportPdfDialog { id: dialog_id(KIND), s, preview: None, password_error: None, held: HeldAnswer::new() }
|
||||
ImportPdfDialog { id: dialog_id(KIND), s, preview: None, pending_preview: None, password_error: None, held: HeldAnswer::new() }
|
||||
}
|
||||
|
||||
/// The preview to render next: the revision of the shown page, crop and text, when the stored one differs.
|
||||
@@ -700,7 +710,14 @@ fn confirm(app: &mut VectorcraftApp, s: &State) -> Result<(), String> {
|
||||
if s.links_on() {
|
||||
p["linkPages"] = json!(true);
|
||||
}
|
||||
io::open_document(app, &s.name, &s.file.bytes, s.path.clone(), &p).map(|_| ())
|
||||
if crate::opening::available()
|
||||
&& let Some(path) = s.path.as_deref()
|
||||
{
|
||||
crate::opening::start_with(app, path, s.file.bytes.clone(), p);
|
||||
Ok(())
|
||||
} else {
|
||||
io::open_document(app, &s.name, &s.file.bytes, s.path.clone(), &p).map(|_| ())
|
||||
}
|
||||
};
|
||||
// Asked only for the password, the dialog showed no Text choice to keep; the choice for a `.ai`
|
||||
// or `.ait` file is not kept for PDFs.
|
||||
@@ -711,10 +728,10 @@ fn confirm(app: &mut VectorcraftApp, s: &State) -> Result<(), String> {
|
||||
}
|
||||
|
||||
/// The preview of the page and boxes `s` shows; `None` for a page that can't be read.
|
||||
fn preview_image(app: &mut VectorcraftApp, ctx: &egui::Context, s: &State) -> Option<Arc<egui::ColorImage>> {
|
||||
fn preview_image(renderer: &mut vectorcraft_render::Renderer, pixels_per_point: f32, s: &State) -> Option<Arc<egui::ColorImage>> {
|
||||
let opts = fileio::LoadOptions { crop: s.crop, password: s.password(), text_as: s.preview_text_as(), ..Default::default() };
|
||||
let (doc, _) = fileio::page_document(&s.file.bytes, s.page().saturating_sub(1), &opts).ok()?;
|
||||
render_preview(app, ctx, &doc)
|
||||
render_page_preview(renderer, pixels_per_point, &doc)
|
||||
}
|
||||
|
||||
/// The host pump: judges the OK the dialog holds (opening or placing the file; a refusal keeps the dialog open with
|
||||
@@ -730,10 +747,44 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) {
|
||||
dialog.answer_ok(answer);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
let wanted = dialogs.with(|host| host.get::<ImportPdfDialog>().and_then(|d| Some((d.wanted_preview()?, d.s.clone()))));
|
||||
if let Some((revision, s)) = wanted {
|
||||
let image = preview_image(app, ctx, &s);
|
||||
let wanted = dialogs.with(|host| {
|
||||
let dialog = host.get_mut::<ImportPdfDialog>()?;
|
||||
if let Some((revision, pending)) = &dialog.pending_preview {
|
||||
let image = match pending.try_recv() {
|
||||
Ok(image) => image,
|
||||
Err(std::sync::mpsc::TryRecvError::Disconnected) => None,
|
||||
Err(std::sync::mpsc::TryRecvError::Empty) => return None,
|
||||
};
|
||||
if dialog.wanted_preview() == Some(*revision) {
|
||||
dialog.set_preview(*revision, image);
|
||||
}
|
||||
dialog.pending_preview = None;
|
||||
}
|
||||
Some((dialog.wanted_preview()?, dialog.s.clone()))
|
||||
});
|
||||
let Some((revision, state)) = wanted else { return };
|
||||
let pixels_per_point = ctx.pixels_per_point();
|
||||
if crate::opening::available() {
|
||||
let (sender, pending) = std::sync::mpsc::channel();
|
||||
dialogs.with(|host| {
|
||||
if let Some(dialog) = host.get_mut::<ImportPdfDialog>() {
|
||||
dialog.pending_preview = Some((revision, pending));
|
||||
}
|
||||
});
|
||||
let wake = ctx.clone();
|
||||
let scope = vectorcraft_engine::file_access::ScopeHandle::capture();
|
||||
app.jobs().spawn(
|
||||
"vectorcraft-pdf-preview",
|
||||
Box::new(move || {
|
||||
let image = scope.run(|| preview_image(&mut vectorcraft_render::Renderer::new(), pixels_per_point, &state));
|
||||
let _ = sender.send(image);
|
||||
wake.request_repaint();
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
let image = preview_image(&mut app.canvas.renderer, pixels_per_point, &state);
|
||||
dialogs.with(|host| {
|
||||
if let Some(dialog) = host.get_mut::<ImportPdfDialog>() {
|
||||
dialog.set_preview(revision, image);
|
||||
|
||||
@@ -117,6 +117,20 @@ fn pdf_page_footer_ok_opens_the_chosen_range() {
|
||||
assert_eq!(artboard_widths(h.state()), [200.0, 300.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirming_a_desktop_pdf_import_queues_the_read_and_closes_the_dialog() {
|
||||
crate::opening::tests::with_threads(|| {
|
||||
let mut app = app();
|
||||
io::open_bytes(&mut app, "three.pdf", &three_pages(), Some("three.pdf".into())).unwrap();
|
||||
set(&mut app, "allPages", json!(false));
|
||||
set(&mut app, "range", json!("2-3"));
|
||||
confirm(&mut app).unwrap();
|
||||
assert!(!is_open(&app));
|
||||
assert_eq!(app.opening.pending.len(), 1);
|
||||
assert_eq!(app.opening.pending[0].path, "three.pdf");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opening_a_multi_page_pdf_asks_for_the_pages() {
|
||||
let mut app = app();
|
||||
|
||||
Reference in new issue
Block a user