mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Closing or reloading the browser tab dropped unsaved documents without a word, while the desktop app asks before closing its window. The web shell now registers a beforeunload listener that has the browser show its own "Leave site?" prompt while a document has unsaved changes (the new PhotocraftApp::has_unsaved_work, refreshed every frame). Claude-Session: https://claude.ai/code/session_01UHgFEHEsefoQoBParH47JA Co-authored-by: Claude <[email protected]>
This commit is contained in:
1 parent
15e4a00991
commit
4477cf6bdf
3 files changed
+44
-2
No files matched your search
@@ -34,4 +34,4 @@ rfd = { workspace = true }
|
||||
wasm-bindgen = "0.2"
|
||||
wasm-bindgen-futures = "0.4"
|
||||
js-sys = "0.3"
|
||||
web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "Location", "CssStyleDeclaration", "Storage", "Event", "EventTarget", "MouseEvent", "PointerEvent"] }
|
||||
web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "Location", "CssStyleDeclaration", "Storage", "Event", "EventTarget", "MouseEvent", "PointerEvent", "BeforeUnloadEvent"] }
|
||||
@@ -1,5 +1,6 @@
|
||||
//! The browser shell: web `Services`, drag-and-drop, and the eframe web runner.
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use photocraft_codecs::{ChannelLayout, EncodeOptions, Image};
|
||||
@@ -56,7 +57,9 @@ pub fn start() {
|
||||
log::info!("photocraft-web: wgpu backend {:?}", rs.adapter.get_info().backend);
|
||||
app.set_wgpu(rs);
|
||||
}
|
||||
Ok(Box::new(WebShell { app, inbox }))
|
||||
let unsaved = Arc::new(AtomicBool::new(false));
|
||||
guard_unload(unsaved.clone());
|
||||
Ok(Box::new(WebShell { app, inbox, unsaved }))
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
@@ -99,6 +102,24 @@ fn listen_pen(target: &web_sys::HtmlCanvasElement, feed: photocraft_ui_egui::sty
|
||||
}
|
||||
}
|
||||
|
||||
/// Closing or reloading the tab while a document has unsaved changes asks first, as closing the
|
||||
/// desktop window does: the browser shows its own "Leave site?" prompt (#1380). `unsaved` is
|
||||
/// refreshed every frame by [`WebShell`].
|
||||
fn guard_unload(unsaved: Arc<AtomicBool>) {
|
||||
use wasm_bindgen::closure::Closure;
|
||||
let Some(window) = web_sys::window() else { return };
|
||||
let cb = Closure::<dyn FnMut(web_sys::BeforeUnloadEvent)>::new(move |e: web_sys::BeforeUnloadEvent| {
|
||||
if unsaved.load(Ordering::Relaxed) {
|
||||
e.prevent_default();
|
||||
// Older browsers show the prompt only when a return value is set.
|
||||
e.set_return_value("");
|
||||
}
|
||||
});
|
||||
if window.add_event_listener_with_callback("beforeunload", cb.as_ref().unchecked_ref()).is_ok() {
|
||||
cb.forget();
|
||||
}
|
||||
}
|
||||
|
||||
fn query() -> String {
|
||||
web_sys::window().and_then(|w| w.location().search().ok()).unwrap_or_default()
|
||||
}
|
||||
@@ -108,6 +129,8 @@ fn query() -> String {
|
||||
struct WebShell {
|
||||
app: PhotocraftApp,
|
||||
inbox: Inbox,
|
||||
/// Read by the `beforeunload` listener ([`guard_unload`]).
|
||||
unsaved: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl eframe::App for WebShell {
|
||||
@@ -128,6 +151,7 @@ impl eframe::App for WebShell {
|
||||
});
|
||||
}
|
||||
self.app.logic(ctx, frame);
|
||||
self.unsaved.store(self.app.has_unsaved_work(), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
|
||||
|
||||
@@ -80,6 +80,14 @@ pub fn intercept(app: &mut PhotocraftApp, id: &str, params: &Value) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl PhotocraftApp {
|
||||
/// Whether closing now would lose work: a document has unsaved changes and closing wasn't
|
||||
/// confirmed. The web build has the browser ask before leaving the page while this holds (#1380).
|
||||
pub fn has_unsaved_work(&self) -> bool {
|
||||
!self.allow_close && self.session.documents().iter().any(|d| d.is_dirty())
|
||||
}
|
||||
}
|
||||
|
||||
/// Called once per frame: holds back a window close request while there is unsaved work.
|
||||
pub fn guard_window_close(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
if app.allow_close || !ctx.input(|i| i.viewport().close_requested()) {
|
||||
@@ -461,6 +469,16 @@ mod tests {
|
||||
assert!(app.discard.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsaved_work_is_any_dirty_document_until_closing_is_confirmed() {
|
||||
let mut app = app_with_docs(2);
|
||||
assert!(!app.has_unsaved_work());
|
||||
make_dirty(&mut app, 1);
|
||||
assert!(app.has_unsaved_work(), "a dirty document that isn't the active one counts");
|
||||
app.allow_close = true;
|
||||
assert!(!app.has_unsaved_work());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn window_close_goes_through_when_nothing_is_unsaved_or_it_was_confirmed() {
|
||||
let mut app = app_with_docs(1);
|
||||
|
||||
Reference in new issue
Block a user