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());
|
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) {
|
fn show_warning_dialog(title: &str, description: &str) {
|
||||||
let _ = rfd::MessageDialog::new()
|
let _ = rfd::MessageDialog::new()
|
||||||
.set_level(rfd::MessageLevel::Warning)
|
.set_level(rfd::MessageLevel::Warning)
|
||||||
@@ -1365,7 +1390,8 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
|||||||
var lastFindQuery = '';
|
var lastFindQuery = '';
|
||||||
var STAT_WORDS = '{stat_words_js}';
|
var STAT_WORDS = '{stat_words_js}';
|
||||||
var STAT_CHARS = '{stat_chars_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_MIN = 50;
|
||||||
var ZOOM_MAX = 300;
|
var ZOOM_MAX = 300;
|
||||||
var ZOOM_STEP = 5;
|
var ZOOM_STEP = 5;
|
||||||
@@ -1396,20 +1422,18 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
|||||||
docStats.textContent =
|
docStats.textContent =
|
||||||
number(words) + ' ' + STAT_WORDS + ' · ' + number(segments.length) + ' ' + STAT_CHARS;
|
number(words) + ' ' + STAT_WORDS + ' · ' + number(segments.length) + ' ' + STAT_CHARS;
|
||||||
}}
|
}}
|
||||||
function loadZoomPercent() {{
|
function queueZoomSave() {{
|
||||||
try {{
|
if (pendingZoomSaveTimer) clearTimeout(pendingZoomSaveTimer);
|
||||||
var stored = Number(localStorage.getItem(ZOOM_STORAGE_KEY));
|
pendingZoomSaveTimer = setTimeout(function() {{
|
||||||
if (Number.isFinite(stored) && stored >= ZOOM_MIN && stored <= ZOOM_MAX) return stored;
|
pendingZoomSaveTimer = 0;
|
||||||
}} catch (_) {{}}
|
if (window.ipc) window.ipc.postMessage('zoom:' + zoomPercent);
|
||||||
return 100;
|
}}, ZOOM_SAVE_DEBOUNCE_MS);
|
||||||
}}
|
}}
|
||||||
function applyZoom(percent, persist) {{
|
function applyZoom(percent, persist) {{
|
||||||
zoomPercent = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, percent));
|
zoomPercent = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, percent));
|
||||||
document.documentElement.style.setProperty('--content-scale', String(zoomPercent / 100));
|
document.documentElement.style.setProperty('--content-scale', String(zoomPercent / 100));
|
||||||
btnZoomReset.textContent = zoomPercent + '%';
|
btnZoomReset.textContent = zoomPercent + '%';
|
||||||
if (persist) {{
|
if (persist) queueZoomSave();
|
||||||
try {{ localStorage.setItem(ZOOM_STORAGE_KEY, String(zoomPercent)); }} catch (_) {{}}
|
|
||||||
}}
|
|
||||||
if (inEdit()) autoResize();
|
if (inEdit()) autoResize();
|
||||||
}}
|
}}
|
||||||
function changeZoom(delta) {{
|
function changeZoom(delta) {{
|
||||||
@@ -1432,7 +1456,8 @@ body.editing #app {{ max-width: none; padding: 0; }}
|
|||||||
requestAnimationFrame(restore);
|
requestAnimationFrame(restore);
|
||||||
}});
|
}});
|
||||||
}}
|
}}
|
||||||
applyZoom(loadZoomPercent(), false);
|
applyZoom(100, false);
|
||||||
|
window.__mdPreviewSetZoom = function(percent) {{ applyZoom(percent, false); }};
|
||||||
updateDocumentStats(ta.value);
|
updateDocumentStats(ta.value);
|
||||||
document.addEventListener('contextmenu', function(e) {{
|
document.addEventListener('contextmenu', function(e) {{
|
||||||
if (!inEdit() || e.target !== ta) e.preventDefault();
|
if (!inEdit() || e.target !== ta) e.preventDefault();
|
||||||
@@ -2228,6 +2253,15 @@ mod tests {
|
|||||||
assert!(!is_help_arg("--edit"));
|
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]
|
#[test]
|
||||||
fn theme_choice_parses_menu_values() {
|
fn theme_choice_parses_menu_values() {
|
||||||
assert_eq!(ThemeChoice::from_str("system"), ThemeChoice::System);
|
assert_eq!(ThemeChoice::from_str("system"), ThemeChoice::System);
|
||||||
@@ -2318,7 +2352,8 @@ mod tests {
|
|||||||
assert!(page.contains("id=\"doc-stats\""));
|
assert!(page.contains("id=\"doc-stats\""));
|
||||||
assert!(page.contains("updateDocumentStats"));
|
assert!(page.contains("updateDocumentStats"));
|
||||||
assert!(page.contains("restoreScrollProgress"));
|
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-in\""));
|
||||||
assert!(page.contains("id=\"btn-zoom-out\""));
|
assert!(page.contains("id=\"btn-zoom-out\""));
|
||||||
assert!(page.contains("id=\"btn-zoom-reset\""));
|
assert!(page.contains("id=\"btn-zoom-reset\""));
|
||||||
@@ -3702,6 +3737,10 @@ fn main() {
|
|||||||
let _ = proxy_for_ipc.send_event(UserEvent::Print);
|
let _ = proxy_for_ipc.send_event(UserEvent::Print);
|
||||||
} else if body == "ready" {
|
} else if body == "ready" {
|
||||||
let _ = proxy_for_ipc.send_event(UserEvent::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" {
|
} else if body == "refresh" {
|
||||||
if let Some(path) = session_for_ipc
|
if let Some(path) = session_for_ipc
|
||||||
.lock()
|
.lock()
|
||||||
@@ -4126,6 +4165,12 @@ fn main() {
|
|||||||
// this, even in bench mode, so subsequent panes would still
|
// this, even in bench mode, so subsequent panes would still
|
||||||
// highlight — bench just exits right after measuring.
|
// highlight — bench just exits right after measuring.
|
||||||
let _ = webview.evaluate_script(&hljs_bootstrap);
|
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 {
|
if !page_ready {
|
||||||
page_ready = true;
|
page_ready = true;
|
||||||
let mut session = session_for_event.lock().unwrap();
|
let mut session = session_for_event.lock().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user