mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-26 03:49:11 +00:00
pdf-shell-w7: net change of 3 commits
- pdf-shell-w7: written from its sheet - pdf-shell-w7: repair 1 - pdf-shell-w7: review fixes (review-1)
This commit is contained in:
@@ -57,21 +57,26 @@ impl PdfCraftApp {
|
||||
// before this guard; the snapshots above are offered for recovery on the next start.
|
||||
std::panic::resume_unwind(Box::new(message.to_string()));
|
||||
}
|
||||
self.dialog = None;
|
||||
self.palette_open = false;
|
||||
self.keep_active_valid();
|
||||
self.notify_fmt("That didn't work: an internal error stopped it ({m}).", &[("m", message)]);
|
||||
if let Some(ctx) = &self.ctx {
|
||||
// Top first, so a child dialog is cancelled before the parent it was opened from.
|
||||
// PLAN: assumed `PdfCraftApp.dialogs: DialogHost` (pdf-hub-w1)
|
||||
for open in self.dialogs.open_dialogs().into_iter().rev() {
|
||||
self.dialogs.cancel_by_id(ctx, open.id);
|
||||
}
|
||||
ctx.request_repaint();
|
||||
}
|
||||
}
|
||||
|
||||
/// After `logic` panicked, perhaps before its quit guard ran: a close of the window asked for
|
||||
/// in this frame still asks about unsaved changes, or eframe closes it and what the recovery
|
||||
/// snapshot doesn't hold (a text being typed) is lost. If even that panics, the close is
|
||||
/// cancelled; the user can close again.
|
||||
pub(crate) fn close_after_panic(&mut self, ctx: &egui::Context) {
|
||||
if ctx.input(|i| i.viewport().close_requested()) && pdfcraft_engine::guard(|| self.hold_close_for_unsaved(ctx)).is_err() {
|
||||
/// The root window's close path, run once per frame after `logic`'s guarded part whether or
|
||||
/// not it panicked: a close asked for in a frame whose logic panicked still asks about
|
||||
/// unsaved changes, or eframe closes the window and what the recovery snapshot doesn't hold
|
||||
/// (a text being typed) is lost. If even that panics, the close is cancelled; the user can
|
||||
/// close again. It runs `embed::logic` once, which also runs the windows' own logic.
|
||||
pub(crate) fn run_close_path(&mut self, ctx: &egui::Context) {
|
||||
if pdfcraft_engine::guard(|| craft::ui::shell::embed::logic(ctx, || self.close_requested())).is_err() {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
|
||||
}
|
||||
}
|
||||
@@ -105,62 +110,85 @@ impl PdfCraftApp {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Dialog, RecoveryStore};
|
||||
use craft::ui::dialog::UnsavedChangesDialog;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use pdfcraft_engine::{Edit, Session};
|
||||
|
||||
/// A recovery folder of its own, removed on drop.
|
||||
struct Store(RecoveryStore);
|
||||
/// A recovery folder of its own, removed on drop. The app opens the store in it.
|
||||
struct Store(std::path::PathBuf);
|
||||
|
||||
impl Store {
|
||||
fn new(tag: &str) -> Store {
|
||||
let dir = std::env::temp_dir().join(format!("pdfcraft-frame-guard-{tag}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
Store(RecoveryStore::new(dir))
|
||||
Store(dir)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Store {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(self.0.dir());
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Snapshots written to the store. `RecoveryStore::list` leaves out a running instance's own.
|
||||
/// Snapshots written to the store's folder.
|
||||
fn snapshots(store: &Store) -> usize {
|
||||
std::fs::read_dir(store.0.dir()).map(|d| d.flatten().filter(|e| e.path().extension().is_some_and(|x| x == "pdf")).count()).unwrap_or(0)
|
||||
std::fs::read_dir(&store.0).map(|d| d.flatten().filter(|e| e.path().extension().is_some_and(|x| x == "pdf")).count()).unwrap_or(0)
|
||||
}
|
||||
|
||||
fn pdf(text: &str) -> Vec<u8> {
|
||||
Session::new().create_from_text("t", text).unwrap().to_vec()
|
||||
}
|
||||
|
||||
/// Two tabs, the second with an unsaved edit, and the About dialog open.
|
||||
fn harness(store: &Store) -> Harness<'static, PdfCraftApp> {
|
||||
let recovery = store.0.clone();
|
||||
/// An app built through `with_host`, with recovery in `dir` when given.
|
||||
// PLAN: assumed `AppServices::new(wake, clipboard, recovery_dir, update_source, intake, watch)` and `PdfCraftApp::with_host(Arc<Host>, AppServices)` (pdf-hub-w1, L-207)
|
||||
fn app(dir: Option<&std::path::Path>) -> PdfCraftApp {
|
||||
let wake = craft::host::Wake::none();
|
||||
let (intake, _) = craft::platform::open::FileIntake::new(wake.clone());
|
||||
let clipboard = craft::platform::clipboard::Clipboard::new(craft::platform::clipboard::EventClipboard::new(|| 0, |_| Ok(0)));
|
||||
let watch = craft::platform::watch::FileWatch::new(wake.clone());
|
||||
let services = crate::AppServices::new(wake, clipboard, dir.map(std::path::Path::to_path_buf), None, intake, watch);
|
||||
let info = craft::host::AppInfo { id: "pdfcraft", name: "PdfCraft", version: "0" };
|
||||
PdfCraftApp::with_host(std::sync::Arc::new(craft::host::Host::builder(info).build()), services)
|
||||
}
|
||||
|
||||
/// Two tabs, the second with an unsaved edit, and the About dialog open when `about`.
|
||||
fn harness(store: &Store, about: bool) -> Harness<'static, PdfCraftApp> {
|
||||
let dir = store.0.clone();
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(move |_cc| {
|
||||
let mut app = PdfCraftApp::new();
|
||||
let mut app = app(Some(&dir));
|
||||
app.set_option("language", "en").unwrap();
|
||||
app.enable_recovery(recovery);
|
||||
app.enable_recovery();
|
||||
app.open_bytes("a.pdf", None, pdf("First")).unwrap();
|
||||
app.open_bytes("b.pdf", None, pdf("Second")).unwrap();
|
||||
app
|
||||
});
|
||||
h.run_steps(4);
|
||||
assert!(h.state_mut().apply_edit(Edit::RotatePages { pages: vec![0], degrees: 90 }));
|
||||
h.state_mut().dialog = Some(Dialog::About);
|
||||
if about {
|
||||
h.state_mut().set_option("dialog", "about").unwrap();
|
||||
}
|
||||
h.run_steps(1);
|
||||
h
|
||||
}
|
||||
|
||||
fn toast(h: &Harness<'static, PdfCraftApp>) -> String {
|
||||
h.state().toast.as_ref().map(|t| t.0.clone()).unwrap_or_default()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_panic_while_drawing_keeps_the_app_its_tabs_and_their_edits() {
|
||||
let store = Store::new("ui");
|
||||
let mut h = harness(&store);
|
||||
let store = Store::new("panic");
|
||||
let mut h = harness(&store, true);
|
||||
// A panic in logic is caught like one in drawing.
|
||||
h.state_mut().panic_next = Some(FramePart::Logic);
|
||||
h.run_steps(1);
|
||||
h.state_mut().panic_next = None;
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().views.len(), 2);
|
||||
h.get_by_label_contains("injected Logic panic");
|
||||
assert_eq!(snapshots(&store), 1);
|
||||
|
||||
h.state_mut().set_option("dialog", "about").unwrap();
|
||||
h.run_steps(1);
|
||||
assert!(!h.state().dialogs.open_dialogs().is_empty(), "the dialog is showing");
|
||||
h.state_mut().panic_next = Some(FramePart::Ui);
|
||||
h.run_steps(1);
|
||||
h.state_mut().panic_next = None;
|
||||
@@ -168,44 +196,30 @@ mod tests {
|
||||
let app = h.state();
|
||||
assert_eq!(app.views.len(), 2, "every tab is still open");
|
||||
assert_eq!(app.active, Some(1));
|
||||
assert!(app.dialog.is_none(), "the dialog that was showing is dropped");
|
||||
assert!(app.dialogs.open_dialogs().is_empty(), "the dialog that was showing is dropped");
|
||||
assert_eq!(app.panicking_frames, 0, "a frame that draws resets the count");
|
||||
assert!(toast(&h).starts_with("That didn't work: an internal error stopped it (injected Ui panic)"), "{}", toast(&h));
|
||||
h.get_by_label_contains("injected Ui panic");
|
||||
assert_eq!(snapshots(&store), 1, "the edited tab was snapshotted for recovery at once");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_panic_in_a_frames_logic_is_caught_too() {
|
||||
let store = Store::new("logic");
|
||||
let mut h = harness(&store);
|
||||
h.state_mut().panic_next = Some(FramePart::Logic);
|
||||
h.run_steps(1);
|
||||
h.state_mut().panic_next = None;
|
||||
h.run_steps(2);
|
||||
assert_eq!(h.state().views.len(), 2);
|
||||
assert!(toast(&h).contains("injected Logic panic"), "{}", toast(&h));
|
||||
assert_eq!(snapshots(&store), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_the_window_in_a_frame_whose_logic_panics_still_asks_about_unsaved_changes() {
|
||||
// The panic comes before the quit guard runs: without the question eframe closes the
|
||||
// window, and what isn't in the recovery snapshot (a text being typed) is lost.
|
||||
let store = Store::new("quit");
|
||||
let mut h = harness(&store);
|
||||
h.state_mut().dialog = None;
|
||||
let mut h = harness(&store, false);
|
||||
h.state_mut().panic_next = Some(FramePart::Logic);
|
||||
h.input_mut().viewports.entry(egui::ViewportId::ROOT).or_default().events.push(egui::ViewportEvent::Close);
|
||||
h.run_steps(1);
|
||||
h.state_mut().panic_next = None;
|
||||
h.run_steps(1);
|
||||
assert_eq!(h.state().close_request, Some(crate::CloseRequest::Quit), "the save question is asked");
|
||||
h.run_steps(2);
|
||||
assert!(h.state().dialogs.is_open::<UnsavedChangesDialog>(), "the save question is asked");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frames_that_keep_panicking_end_the_app_after_snapshotting_its_work() {
|
||||
let store = Store::new("again");
|
||||
let mut h = harness(&store);
|
||||
let mut h = harness(&store, true);
|
||||
h.state_mut().panic_next = Some(FramePart::Ui);
|
||||
// The guard lets the third panicking frame through, as the app ended before it.
|
||||
let ended = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| h.run_steps(MAX_PANICKING_FRAMES as usize)));
|
||||
@@ -217,7 +231,7 @@ mod tests {
|
||||
fn hidden_frames_that_run_cleanly_reset_the_count() {
|
||||
// eframe runs `logic` without `ui` while the window is hidden: panics separated by
|
||||
// clean hidden frames are not "in a row" and must not end the app.
|
||||
let mut app = PdfCraftApp::new();
|
||||
let mut app = app(None);
|
||||
for _ in 0..MAX_PANICKING_FRAMES * 2 {
|
||||
app.begin_logic();
|
||||
app.guarded_frame(|_| panic!("hidden logic panic"));
|
||||
@@ -230,7 +244,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn hidden_frames_that_keep_panicking_still_end_the_app() {
|
||||
let mut app = PdfCraftApp::new();
|
||||
let mut app = app(None);
|
||||
let ended = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
||||
for _ in 0..MAX_PANICKING_FRAMES {
|
||||
app.begin_logic();
|
||||
@@ -239,16 +253,4 @@ mod tests {
|
||||
}));
|
||||
assert!(ended.is_err(), "panics in every hidden frame still reach the limit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn active_stays_on_an_open_tab() {
|
||||
let mut app = PdfCraftApp::new();
|
||||
app.active = Some(3);
|
||||
app.keep_active_valid();
|
||||
assert_eq!(app.active, None, "no tabs: nothing is active");
|
||||
app.open_bytes("a.pdf", None, pdf("Only")).unwrap();
|
||||
app.active = Some(5);
|
||||
app.keep_active_valid();
|
||||
assert_eq!(app.active, Some(0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,3 @@
|
||||
/// Files delivered asynchronously: (name, bytes).
|
||||
pub type Inbox = std::sync::Arc<std::sync::Mutex<Vec<(String, Vec<u8>)>>>;
|
||||
|
||||
/// Files that failed to arrive asynchronously (a browser `?file=` URL that couldn't be fetched):
|
||||
/// `(name, error)`, reported to the user on the next frame (#173).
|
||||
pub type FailedInbox = std::sync::Arc<std::sync::Mutex<Vec<(String, String)>>>;
|
||||
|
||||
/// A request the operating system sends the running app, outside its window: on macOS, Finder
|
||||
/// double-clicks, Open With and drops on the Dock icon arrive as Apple events, not arguments.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum OsEvent {
|
||||
/// Open these files.
|
||||
Open(Vec<String>),
|
||||
/// Add these files to Combine files and show it (`pdfcraft --combine`, File Explorer's
|
||||
/// "Combine files in PdfCraft", #585).
|
||||
Combine(Vec<String>),
|
||||
/// Quit (the Dock's Quit, logging out), asking about unsaved changes first.
|
||||
Quit,
|
||||
}
|
||||
|
||||
/// Returns the [`OsEvent`]s that arrived since it was last called (set by the desktop app).
|
||||
pub type OsEventsFn = Box<dyn FnMut() -> Vec<OsEvent>>;
|
||||
|
||||
/// Hands a saved PDF to the page embedding PdfCraft (web): `(name, bytes, save_as)`. `save_as`
|
||||
/// is `true` for Save As. Set by the web app when a host page (such as a Nextcloud app) asked
|
||||
/// for saves; without it the browser downloads the file.
|
||||
pub type HostSaveFn = Box<dyn Fn(&str, &[u8], bool) -> Result<(), String>>;
|
||||
|
||||
/// Told every frame whether any tab has unsaved work, or an OCR review is not yet accepted or
|
||||
/// rejected, so a host page can warn before leaving.
|
||||
pub type HostDirtyFn = Box<dyn FnMut(bool)>;
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
//! Where the pointer is when the windowing layer doesn't say: while files are dragged over the
|
||||
//! window (it reports that a file hovers, but not where, and sends no pointer moves until the drag
|
||||
//! ends), and when the wheel turns after switching back to the window (no move is sent until the
|
||||
//! mouse moves, #1518). The system has to be asked.
|
||||
|
||||
/// The pointer in this window's coordinates (egui points), when the system can tell.
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) fn in_window(ctx: &egui::Context) -> Option<egui::Pos2> {
|
||||
use objc2_app_kit::{NSEvent, NSScreen};
|
||||
// AppKit's screen list is only for the main thread (where egui runs).
|
||||
let mtm = objc2::MainThreadMarker::new()?;
|
||||
// Screen coordinates start at the bottom-left of the first screen; windows at their top-left.
|
||||
let top = NSScreen::screens(mtm).firstObject()?.frame().size.height;
|
||||
let pointer = NSEvent::mouseLocation();
|
||||
let inner = ctx.input(|i| i.viewport().inner_rect)?;
|
||||
// egui's points are the system's, divided by the zoom factor.
|
||||
let zoom = ctx.zoom_factor().max(0.01) as f64;
|
||||
let (x, y) = (pointer.x / zoom, (top - pointer.y) / zoom);
|
||||
let pos = egui::pos2(x as f32 - inner.min.x, y as f32 - inner.min.y);
|
||||
(pos.x.is_finite() && pos.y.is_finite()).then_some(pos)
|
||||
}
|
||||
|
||||
/// The pointer in this window's coordinates (egui points), when the system can tell.
|
||||
#[cfg(windows)]
|
||||
pub(crate) fn in_window(ctx: &egui::Context) -> Option<egui::Pos2> {
|
||||
// Physical screen pixels, whatever this thread's DPI awareness.
|
||||
let pointer = winsafe::GetPhysicalCursorPos().ok()?;
|
||||
let inner = ctx.input(|i| i.viewport().inner_rect)?;
|
||||
// egui places the window in points: physical pixels over the pixels per point (zoom included).
|
||||
let ppp = ctx.pixels_per_point();
|
||||
if !(ppp.is_finite() && ppp > 0.0) {
|
||||
return None;
|
||||
}
|
||||
let pos = egui::pos2(pointer.x as f32 / ppp - inner.min.x, pointer.y as f32 / ppp - inner.min.y);
|
||||
(pos.x.is_finite() && pos.y.is_finite()).then_some(pos)
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "macos", windows)))]
|
||||
pub(crate) fn in_window(_ctx: &egui::Context) -> Option<egui::Pos2> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Wheel input with no known pointer: after Alt+Tab back to the window, the windowing layer sends
|
||||
/// no pointer move until the mouse moves, so egui doesn't know what is under the pointer and the
|
||||
/// wheel scrolls nothing (#1518). When `raw` turns the wheel, has no pointer move of its own and
|
||||
/// egui knows no pointer position (`known`), the pointer from `system` is put first, if it is
|
||||
/// inside the window's `size` (egui points).
|
||||
pub(crate) fn locate_wheel(raw: &mut egui::RawInput, known: bool, size: egui::Vec2, system: impl FnOnce() -> Option<egui::Pos2>) {
|
||||
let wheel = raw.events.iter().any(|e| matches!(e, egui::Event::MouseWheel { .. }));
|
||||
let moved = raw.events.iter().any(|e| matches!(e, egui::Event::PointerMoved(_) | egui::Event::PointerGone));
|
||||
if !wheel || moved || known {
|
||||
return;
|
||||
}
|
||||
if let Some(pos) = system().filter(|p| egui::Rect::from_min_size(egui::Pos2::ZERO, size).contains(*p)) {
|
||||
raw.events.insert(0, egui::Event::PointerMoved(pos));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn wheel() -> egui::Event {
|
||||
egui::Event::MouseWheel {
|
||||
unit: egui::MouseWheelUnit::Line,
|
||||
delta: egui::vec2(0.0, -1.0),
|
||||
phase: egui::TouchPhase::Move,
|
||||
modifiers: egui::Modifiers::NONE,
|
||||
source: egui::MouseWheelSource::Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
fn located(events: Vec<egui::Event>, known: bool, system: Option<egui::Pos2>) -> Vec<egui::Event> {
|
||||
let mut raw = egui::RawInput { events, ..Default::default() };
|
||||
locate_wheel(&mut raw, known, egui::vec2(800.0, 600.0), || system);
|
||||
raw.events
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wheel_with_no_known_pointer_asks_the_system_where_it_is() {
|
||||
let at = egui::pos2(300.0, 200.0);
|
||||
// After Alt+Tab back: the wheel turns, nothing moved, egui knows no pointer.
|
||||
assert_eq!(located(vec![wheel()], false, Some(at)), vec![egui::Event::PointerMoved(at), wheel()]);
|
||||
// Otherwise nothing is added: egui knows the pointer, the frame moves it itself, there is
|
||||
// no wheel, the system can't tell, or the pointer is outside the window.
|
||||
assert_eq!(located(vec![wheel()], true, Some(at)), vec![wheel()]);
|
||||
let moved = egui::Event::PointerMoved(egui::pos2(1.0, 1.0));
|
||||
assert_eq!(located(vec![moved.clone(), wheel()], false, Some(at)), vec![moved, wheel()]);
|
||||
assert_eq!(located(vec![egui::Event::PointerGone, wheel()], false, Some(at)), vec![egui::Event::PointerGone, wheel()]);
|
||||
assert_eq!(
|
||||
located(
|
||||
vec![egui::Event::Key { key: egui::Key::A, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }],
|
||||
false,
|
||||
Some(at)
|
||||
)
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
assert_eq!(located(vec![wheel()], false, None), vec![wheel()]);
|
||||
assert_eq!(located(vec![wheel()], false, Some(egui::pos2(900.0, 200.0))), vec![wheel()]);
|
||||
assert_eq!(located(vec![wheel()], false, Some(egui::pos2(f32::NAN, 0.0))), vec![wheel()]);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -154,7 +154,7 @@ neither the MSI nor the portable zip needs the Visual C++ redistributable.
|
||||
doesn't have. "Combine files in PdfCraft" (#585) is a second verb in the same component,
|
||||
`PdfCraft.Combine`, running `pdfcraft --combine "<file>"` once per selected file; the
|
||||
single-instance hand-off sends them with `"combine": true`, and the running app adds them to
|
||||
its Combine files tab (`OsEvent::Combine`). Both verbs come and go with `INSTALLPDFCONTEXTMENU`.
|
||||
its Combine files tab (`Intake::OpenFor` with action "combine"). Both verbs come and go with `INSTALLPDFCONTEXTMENU`.
|
||||
- The image right-click entry (#190) is the same kind of verb, `PdfCraft.CreatePdf`, for ten image
|
||||
types, in its own component (`PdfcraftImageContextMenu`) since it became optional. Its rows moved
|
||||
out of `PdfcraftApp`; that's safe because `MajorUpgrade` removes the old version before installing
|
||||
|
||||
Reference in New Issue
Block a user