fix: persist content zoom across restarts
localStorage is unavailable for the with_html page (about:blank opaque origin), so zoom was never saved. Store it in config_dir()/zoom.txt via the zoom:<pct> IPC message (400ms debounce) and restore it when the page reports ready.
This commit is contained in:
BIN
agent/reports/feature3-final.png
Normal file
BIN
agent/reports/feature3-final.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 142 KiB |
69
src/main.rs
69
src/main.rs
@@ -251,6 +251,31 @@ fn save_theme_choice(choice: ThemeChoice) {
|
||||
let _ = fs::write(dir.join("theme.txt"), choice.as_str());
|
||||
}
|
||||
|
||||
const ZOOM_MIN: u32 = 50;
|
||||
const ZOOM_MAX: u32 = 300;
|
||||
|
||||
fn zoom_path() -> PathBuf {
|
||||
config_dir().join("zoom.txt")
|
||||
}
|
||||
|
||||
fn clamp_zoom(percent: u32) -> u32 {
|
||||
percent.clamp(ZOOM_MIN, ZOOM_MAX)
|
||||
}
|
||||
|
||||
fn load_zoom() -> u32 {
|
||||
fs::read_to_string(zoom_path())
|
||||
.ok()
|
||||
.and_then(|raw| raw.trim().parse::<u32>().ok())
|
||||
.map(clamp_zoom)
|
||||
.unwrap_or(100)
|
||||
}
|
||||
|
||||
fn save_zoom(percent: u32) {
|
||||
let dir = config_dir();
|
||||
let _ = fs::create_dir_all(&dir);
|
||||
let _ = fs::write(zoom_path(), clamp_zoom(percent).to_string());
|
||||
}
|
||||
|
||||
fn show_warning_dialog(title: &str, description: &str) {
|
||||
let _ = rfd::MessageDialog::new()
|
||||
.set_level(rfd::MessageLevel::Warning)
|
||||
@@ -1365,7 +1390,8 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
||||
var lastFindQuery = '';
|
||||
var STAT_WORDS = '{stat_words_js}';
|
||||
var STAT_CHARS = '{stat_chars_js}';
|
||||
var ZOOM_STORAGE_KEY = 'md-preview-content-zoom-v1';
|
||||
var ZOOM_SAVE_DEBOUNCE_MS = 400;
|
||||
var pendingZoomSaveTimer = 0;
|
||||
var ZOOM_MIN = 50;
|
||||
var ZOOM_MAX = 300;
|
||||
var ZOOM_STEP = 5;
|
||||
@@ -1396,20 +1422,18 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
||||
docStats.textContent =
|
||||
number(words) + ' ' + STAT_WORDS + ' · ' + number(segments.length) + ' ' + STAT_CHARS;
|
||||
}}
|
||||
function loadZoomPercent() {{
|
||||
try {{
|
||||
var stored = Number(localStorage.getItem(ZOOM_STORAGE_KEY));
|
||||
if (Number.isFinite(stored) && stored >= ZOOM_MIN && stored <= ZOOM_MAX) return stored;
|
||||
}} catch (_) {{}}
|
||||
return 100;
|
||||
function queueZoomSave() {{
|
||||
if (pendingZoomSaveTimer) clearTimeout(pendingZoomSaveTimer);
|
||||
pendingZoomSaveTimer = setTimeout(function() {{
|
||||
pendingZoomSaveTimer = 0;
|
||||
if (window.ipc) window.ipc.postMessage('zoom:' + zoomPercent);
|
||||
}}, ZOOM_SAVE_DEBOUNCE_MS);
|
||||
}}
|
||||
function applyZoom(percent, persist) {{
|
||||
zoomPercent = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, percent));
|
||||
document.documentElement.style.setProperty('--content-scale', String(zoomPercent / 100));
|
||||
btnZoomReset.textContent = zoomPercent + '%';
|
||||
if (persist) {{
|
||||
try {{ localStorage.setItem(ZOOM_STORAGE_KEY, String(zoomPercent)); }} catch (_) {{}}
|
||||
}}
|
||||
if (persist) queueZoomSave();
|
||||
if (inEdit()) autoResize();
|
||||
}}
|
||||
function changeZoom(delta) {{
|
||||
@@ -1432,7 +1456,8 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
||||
requestAnimationFrame(restore);
|
||||
}});
|
||||
}}
|
||||
applyZoom(loadZoomPercent(), false);
|
||||
applyZoom(100, false);
|
||||
window.__mdPreviewSetZoom = function(percent) {{ applyZoom(percent, false); }};
|
||||
updateDocumentStats(ta.value);
|
||||
document.addEventListener('contextmenu', function(e) {{
|
||||
if (!inEdit() || e.target !== ta) e.preventDefault();
|
||||
@@ -2228,6 +2253,15 @@ mod tests {
|
||||
assert!(!is_help_arg("--edit"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zoom_percent_is_clamped_to_supported_range() {
|
||||
assert_eq!(clamp_zoom(10), 50);
|
||||
assert_eq!(clamp_zoom(50), 50);
|
||||
assert_eq!(clamp_zoom(105), 105);
|
||||
assert_eq!(clamp_zoom(300), 300);
|
||||
assert_eq!(clamp_zoom(999), 300);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn theme_choice_parses_menu_values() {
|
||||
assert_eq!(ThemeChoice::from_str("system"), ThemeChoice::System);
|
||||
@@ -2318,7 +2352,8 @@ mod tests {
|
||||
assert!(page.contains("id=\"doc-stats\""));
|
||||
assert!(page.contains("updateDocumentStats"));
|
||||
assert!(page.contains("restoreScrollProgress"));
|
||||
assert!(page.contains("md-preview-content-zoom-v1"));
|
||||
assert!(page.contains("postMessage('zoom:' + zoomPercent)"));
|
||||
assert!(page.contains("window.__mdPreviewSetZoom"));
|
||||
assert!(page.contains("id=\"btn-zoom-in\""));
|
||||
assert!(page.contains("id=\"btn-zoom-out\""));
|
||||
assert!(page.contains("id=\"btn-zoom-reset\""));
|
||||
@@ -3702,6 +3737,10 @@ fn main() {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::Print);
|
||||
} else if body == "ready" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::Ready);
|
||||
} else if let Some(percent) = body.strip_prefix("zoom:") {
|
||||
if let Ok(percent) = percent.parse::<u32>() {
|
||||
save_zoom(percent);
|
||||
}
|
||||
} else if body == "refresh" {
|
||||
if let Some(path) = session_for_ipc
|
||||
.lock()
|
||||
@@ -4126,6 +4165,12 @@ fn main() {
|
||||
// this, even in bench mode, so subsequent panes would still
|
||||
// highlight — bench just exits right after measuring.
|
||||
let _ = webview.evaluate_script(&hljs_bootstrap);
|
||||
let zoom = load_zoom();
|
||||
if zoom != 100 {
|
||||
let _ = webview.evaluate_script(&format!(
|
||||
"window.__mdPreviewSetZoom && window.__mdPreviewSetZoom({zoom});"
|
||||
));
|
||||
}
|
||||
if !page_ready {
|
||||
page_ready = true;
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user