fix: keep app closable when autosave fails

- Save failures now pause autosave for the file and show one warning instead of retrying every 700ms
- Closing the window after a failed save asks once to discard changes instead of looping on the error dialog
- Closing/switching a blocked tab asks before discarding its unsaved edits
- Widen preview/editor side padding to 48px and scale fold toggles with the heading font
This commit is contained in:
2026-10-21 17:07:11 +08:00
parent ef34b5a29b
commit 097d2eeb0e
2 changed files with 68 additions and 18 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB

After

Width:  |  Height:  |  Size: 114 KiB

View File

@@ -45,7 +45,7 @@ enum UserEvent {
FileChanged(PathBuf), // external change: refresh preview AND textarea
ExternalChangeResolved(bool),
FileSaved(PathBuf), // our own save: refresh preview only, leave textarea cursor alone
SaveFailed(String),
SaveFailed(PathBuf, String),
DirtyChanged(bool),
ToggleEdit,
ShowFind,
@@ -140,6 +140,7 @@ struct Strings {
missing_body: &'static str,
locate_file: &'static str,
close_tab: &'static str,
cannot_save: &'static str,
btn_edit: &'static str,
btn_preview: &'static str,
btn_new: &'static str,
@@ -166,6 +167,7 @@ impl Strings {
missing_body: "这个标签会继续保留。你可以重新定位文件,或关闭标签。",
locate_file: "重新定位",
close_tab: "关闭标签",
cannot_save: "修改无法保存(文件可能只读或被占用)。继续将丢弃这些修改,是否继续?",
btn_edit: "编辑 (Cmd/Ctrl+E)",
btn_preview: "预览 (Cmd/Ctrl+E)",
btn_new: "新建 Markdown (Cmd/Ctrl+N)",
@@ -188,6 +190,7 @@ impl Strings {
missing_body: "This tab is kept. Locate the file again or close the tab.",
locate_file: "Locate File",
close_tab: "Close Tab",
cannot_save: "This file could not be saved (read-only or locked). Continue and discard the unsaved changes?",
btn_edit: "Edit (Cmd/Ctrl+E)",
btn_preview: "Preview (Cmd/Ctrl+E)",
btn_new: "New Markdown (Cmd/Ctrl+N)",
@@ -287,6 +290,20 @@ fn show_warning_dialog(title: &str, description: &str) {
.show();
}
fn confirm_discard_unsaved(error: &str) -> bool {
matches!(
rfd::MessageDialog::new()
.set_level(rfd::MessageLevel::Warning)
.set_title("Could Not Save")
.set_description(&format!(
"{error}\n\nThe file may be read-only or locked by another program. Close the window and discard the unsaved changes?"
))
.set_buttons(rfd::MessageButtons::OkCancel)
.show(),
rfd::MessageDialogResult::Ok
)
}
fn load_window_geom() -> Option<WindowGeom> {
let txt = fs::read_to_string(geom_path()).ok()?;
let parts: Vec<&str> = txt.trim().split(',').collect();
@@ -1041,7 +1058,7 @@ body {{
color: #1a1a1a; background: #fff;
}}
body.has-tabs {{ --chrome-top: 50px; }}
#app {{ width: 100%; box-sizing: border-box; margin: 0 auto; padding: 24px; }}
#app {{ width: 100%; box-sizing: border-box; margin: 0 auto; padding: 24px 48px; }}
#preview {{ font-size: calc(15px * var(--content-scale)); }}
#preview .front-matter {{
margin: 0 0 1.5em;
@@ -1073,8 +1090,8 @@ body.has-tabs {{ --chrome-top: 50px; }}
#preview .mdp-fold > h5,
#preview .mdp-fold > h6 {{ position: relative; }}
#preview .mdp-fold-toggle {{
position: absolute; left: -20px; top: .3em;
width: 16px; height: 16px; padding: 0; border: 0; border-radius: 4px;
position: absolute; left: -1.3em; top: .05em;
width: 1.15em; height: 1.15em; padding: 0; border: 0; border-radius: 5px;
display: grid; place-items: center;
background: transparent; color: #999; cursor: pointer;
opacity: 0; transition: opacity .12s ease, background .12s ease;
@@ -1087,7 +1104,7 @@ body.has-tabs {{ --chrome-top: 50px; }}
#preview h6:hover > .mdp-fold-toggle,
#preview .mdp-fold-toggle:focus-visible {{ opacity: .6; }}
#preview .mdp-fold-toggle:hover {{ opacity: 1; background: rgba(0,0,0,.07); }}
#preview .mdp-fold-toggle svg {{ width: 12px; height: 12px; transition: transform .15s ease; }}
#preview .mdp-fold-toggle svg {{ width: 100%; height: 100%; transition: transform .15s ease; }}
#preview .mdp-fold.collapsed > h1 > .mdp-fold-toggle svg,
#preview .mdp-fold.collapsed > h2 > .mdp-fold-toggle svg,
#preview .mdp-fold.collapsed > h3 > .mdp-fold-toggle svg,
@@ -1350,7 +1367,7 @@ body.empty .toolbar {{ display: none !important; }}
padding: 0;
}}
body.editing #preview {{ display: none; }}
body.editing #editor {{ display: block; padding: 16px 24px; }}
body.editing #editor {{ display: block; padding: 16px 48px; }}
body.editing #app {{ max-width: none; padding: 0; }}
@page {{
@@ -1400,6 +1417,7 @@ body.editing #app {{ max-width: none; padding: 0; }}
var ICON_CLOSE = '<svg viewBox="0 0 24 24" width="17" height="17" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>';
var L_EDIT = '{btn_edit}', L_VIEW = '{btn_preview}';
window.__mdPreviewFoldLabel = '{fold_toggle}';
window.__mdPreviewCannotSave = '{cannot_save_js}';
var btnToggle = document.getElementById('btn-toggle');
var btnZoom = document.getElementById('btn-zoom');
@@ -1420,6 +1438,7 @@ body.editing #app {{ max-width: none; padding: 0; }}
var activeTabId = 0;
var pendingAutosaveTimer = 0;
var autosavePaused = false;
var saveBlocked = false;
var AUTOSAVE_DEBOUNCE_MS = 700;
var composingFind = false;
var pendingFindTimer = 0;
@@ -1528,7 +1547,13 @@ body.editing #app {{ max-width: none; padding: 0; }}
function requestTabAction(action, id) {{
cancelPendingAutosave();
var message = 'tab-action:' + action + ':' + id;
if (dirty) message += '\n' + ta.value;
if (dirty) {{
if (saveBlocked) {{
if (!window.confirm(window.__mdPreviewCannotSave || 'Discard unsaved changes?')) return;
}} else {{
message += '\n' + ta.value;
}}
}}
window.ipc.postMessage(message);
}}
function openFile() {{
@@ -1878,8 +1903,13 @@ body.editing #app {{ max-width: none; padding: 0; }}
return;
}}
autosavePaused = false;
saveBlocked = false;
setDirty(false);
}};
window.__markSaveBlocked = function() {{
saveBlocked = true;
autosavePaused = true;
}};
window.__mdPreviewPauseAutosave = function() {{
cancelPendingAutosave();
autosavePaused = true;
@@ -2003,6 +2033,7 @@ if(window.__enhancePreview)window.__enhancePreview();
btn_edit = s.btn_edit,
btn_preview = s.btn_preview,
fold_toggle = s.fold_toggle,
cannot_save_js = escape_js(s.cannot_save),
btn_zoom = s.btn_zoom,
btn_zoom_out = s.btn_zoom_out,
btn_zoom_reset = s.btn_zoom_reset,
@@ -2387,6 +2418,9 @@ mod tests {
assert!(page.contains("AUTOSAVE_DEBOUNCE_MS = 700"));
assert!(page.contains("pendingAutosaveTimer = setTimeout"));
assert!(page.contains("window.__markSaved = function(savedRaw)"));
assert!(page.contains("window.__markSaveBlocked"));
assert!(page.contains("var saveBlocked = false"));
assert!(page.contains("window.__mdPreviewCannotSave"));
assert!(page.contains("window.__mdPreviewResolveExternalChange"));
assert!(page.contains("'external-change:' + (dirty ? 'dirty' : 'clean')"));
assert!(page.contains("(e.key === 'n' || e.key === 'N')"));
@@ -3765,10 +3799,10 @@ fn main() {
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(path));
}
Err(error) => {
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(format!(
"{}: {error}",
path.display()
)));
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
path.clone(),
error.to_string(),
));
return;
}
}
@@ -3827,10 +3861,10 @@ fn main() {
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(path));
}
Err(error) => {
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(format!(
"{}: {error}",
path.display()
)));
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
path.clone(),
error.to_string(),
));
}
}
}
@@ -4171,9 +4205,25 @@ fn main() {
*control_flow = ControlFlow::Exit;
}
}
TaoEvent::UserEvent(UserEvent::SaveFailed(error)) => {
TaoEvent::UserEvent(UserEvent::SaveFailed(_path, error)) => {
let _ = webview.evaluate_script(
"if(window.__markSaveBlocked)window.__markSaveBlocked();",
);
if pending_window_close {
pending_window_close = false;
show_warning_dialog("Could Not Save", &error);
if confirm_discard_unsaved(&error) {
save_window_geom(&window);
persist_session(&session_for_event.lock().unwrap());
*control_flow = ControlFlow::Exit;
}
} else {
show_warning_dialog(
"Could Not Save",
&format!(
"{error}\n\nAutosave is paused for this file. Close the window to discard the unsaved changes."
),
);
}
}
TaoEvent::UserEvent(UserEvent::DirtyChanged(dirty)) => {
APP_DIRTY.store(dirty, Ordering::SeqCst);