fix: restore the page content call and stop writing the registry
- render_active_document passed the tab id as the first __setContent argument,
but the page signature starts with previewHtml, so #preview rendered the tab
id ("1", "2", "3" as tabs were opened) and the window looked blank. The call
now goes through set_content_script(), and a test pins the exact script plus
the page signature so the two cannot drift silently.
- remove register_as_default() and the winreg dependency: the app no longer
writes HKCU on launch. publish.ps1 still refreshes the .md association.
This commit is contained in:
139
src/main.rs
139
src/main.rs
@@ -534,70 +534,6 @@ fn load_window_icon() -> Option<tao::window::Icon> {
|
||||
tao::window::Icon::from_rgba(rgba.into_raw(), w, h).ok()
|
||||
}
|
||||
|
||||
/// Register the ProgID in HKCU and advertise it once for Markdown extensions
|
||||
/// so MD Preview shows up in the "Open with" list. Win8+ blocks silent
|
||||
/// default-handler changes — only the Settings app can confirm those.
|
||||
fn register_as_default() {
|
||||
use winreg::enums::HKEY_CURRENT_USER;
|
||||
use winreg::RegKey;
|
||||
|
||||
let Ok(exe) = std::env::current_exe() else {
|
||||
return;
|
||||
};
|
||||
let exe_str = exe.to_string_lossy().to_string();
|
||||
let progid = "MDPreview.md";
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
|
||||
// Refresh the ProgID command on every launch: the exe is portable, so a
|
||||
// moved or renamed binary must not leave "Open with" pointing at a stale path.
|
||||
let progid_root = format!(r"Software\Classes\{progid}");
|
||||
if let Ok((k, _)) = hkcu.create_subkey(&progid_root) {
|
||||
let _ = k.set_value("", &"Markdown Document".to_string());
|
||||
let _ = k.set_value("FriendlyTypeName", &"Markdown Document".to_string());
|
||||
}
|
||||
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{progid_root}\DefaultIcon")) {
|
||||
let _ = k.set_value("", &format!("\"{exe_str}\",0"));
|
||||
}
|
||||
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{progid_root}\shell\open\command")) {
|
||||
let _ = k.set_value("", &format!("\"{exe_str}\" \"%1\""));
|
||||
}
|
||||
|
||||
// Applications\<exe-name> entry gives us a friendly label in the "Open with" menu.
|
||||
if let Some(exe_name) = exe.file_name().map(|n| n.to_string_lossy().to_string()) {
|
||||
let app_root = format!(r"Software\Classes\Applications\{exe_name}");
|
||||
if let Ok((k, _)) = hkcu.create_subkey(&app_root) {
|
||||
let _ = k.set_value("FriendlyAppName", &"MD Preview".to_string());
|
||||
}
|
||||
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{app_root}\shell\open\command")) {
|
||||
let _ = k.set_value("", &format!("\"{exe_str}\" \"%1\""));
|
||||
}
|
||||
if let Ok((k, _)) = hkcu.create_subkey(format!(r"{app_root}\SupportedTypes")) {
|
||||
for ext in [".md", ".markdown", ".mdown", ".mkd"] {
|
||||
let _ = k.set_value::<String, _>(ext, &String::new());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Advertise the ProgID once; re-running this is unnecessary.
|
||||
let marker_dir = config_dir();
|
||||
let marker = marker_dir.join(".md-preview-registered");
|
||||
if marker.exists() {
|
||||
return;
|
||||
}
|
||||
for ext in [".md", ".markdown", ".mdown", ".mkd"] {
|
||||
let path = format!(r"Software\Classes\{ext}\OpenWithProgids");
|
||||
if let Ok((key, _)) = hkcu.create_subkey(&path) {
|
||||
let _ = key.set_value::<String, _>(progid, &String::new());
|
||||
}
|
||||
}
|
||||
|
||||
let _ = fs::create_dir_all(&marker_dir);
|
||||
let _ = fs::write(&marker, "");
|
||||
// Intentionally no dialog: users can pick MD Preview via "Open with"
|
||||
// whenever they want, and Win10+ blocks silent default-handler changes
|
||||
// anyway — asking them to click through Settings on first launch is noise.
|
||||
}
|
||||
|
||||
fn is_supported_document(path: &Path) -> bool {
|
||||
path.extension()
|
||||
.map(|extension| {
|
||||
@@ -868,6 +804,32 @@ fn render_home(
|
||||
update_window_title(window, session);
|
||||
}
|
||||
|
||||
/// Build the `__setContent` call.
|
||||
///
|
||||
/// The argument list must match the `window.__setContent` signature in
|
||||
/// `assets/page/page.js`. Nothing type-checks this boundary: when the two
|
||||
/// drift apart the page reads a wrong value as HTML (it once rendered the tab
|
||||
/// id, so a white window showed "1", "2", "3" as tabs were opened).
|
||||
fn set_content_script(
|
||||
html: &str,
|
||||
raw: &str,
|
||||
base_href: &str,
|
||||
flags: EnhanceFlags,
|
||||
editing: bool,
|
||||
scroll_ratio: f32,
|
||||
) -> String {
|
||||
format!(
|
||||
"if(window.__setContent)window.__setContent('{}', '{}', '{}', {}, {}, {}, {});",
|
||||
escape_js(html),
|
||||
escape_js(raw),
|
||||
escape_js(base_href),
|
||||
flags.math,
|
||||
flags.mermaid,
|
||||
editing,
|
||||
scroll_ratio
|
||||
)
|
||||
}
|
||||
|
||||
fn render_active_document(
|
||||
webview: &WebView,
|
||||
window: &Window,
|
||||
@@ -897,16 +859,13 @@ fn render_active_document(
|
||||
let base_href = base_href_for_file(&path).unwrap_or_default();
|
||||
let flags = enhance_flags_for(&raw);
|
||||
*enhance_flags.lock().unwrap() = flags;
|
||||
let _ = webview.evaluate_script(&format!(
|
||||
"if(window.__setContent)window.__setContent({}, '{}', '{}', '{}', {}, {}, {}, {});",
|
||||
active.id,
|
||||
escape_js(&html),
|
||||
escape_js(&raw),
|
||||
escape_js(&base_href),
|
||||
flags.math,
|
||||
flags.mermaid,
|
||||
let _ = webview.evaluate_script(&set_content_script(
|
||||
&html,
|
||||
&raw,
|
||||
&base_href,
|
||||
flags,
|
||||
active.editing,
|
||||
active.scroll_ratio
|
||||
active.scroll_ratio,
|
||||
));
|
||||
for script in build_enhancer_bootstrap(flags, *loaded_enhancers) {
|
||||
let _ = webview.evaluate_script(&script);
|
||||
@@ -1374,7 +1333,6 @@ fn main() {
|
||||
};
|
||||
|
||||
let strings = Strings::english();
|
||||
register_as_default();
|
||||
bench_log("after_register");
|
||||
|
||||
let mut initial_session = DocumentSession::default();
|
||||
@@ -2627,6 +2585,39 @@ mod tests {
|
||||
assert!(page.contains("sectionizeFolds"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_content_script_matches_the_page_signature() {
|
||||
let script = set_content_script(
|
||||
"<p>x</p>",
|
||||
"# x",
|
||||
"file:///D:/tmp/",
|
||||
EnhanceFlags {
|
||||
math: true,
|
||||
mermaid: false,
|
||||
},
|
||||
true,
|
||||
0.25,
|
||||
);
|
||||
assert_eq!(
|
||||
script,
|
||||
"if(window.__setContent)window.__setContent('<p>x</p>', '# x', 'file:///D:/tmp/', true, false, true, 0.25);"
|
||||
);
|
||||
|
||||
// The page must declare the same argument order; a mismatch renders a
|
||||
// wrong value as HTML and shows up as a blank window.
|
||||
let page = build_page(
|
||||
"",
|
||||
"",
|
||||
None,
|
||||
EnhanceFlags::default(),
|
||||
&Strings::english(),
|
||||
true,
|
||||
);
|
||||
assert!(page.contains(
|
||||
"window.__setContent = function(previewHtml, rawMd, baseHref, needsMath, needsMermaid, editing, scrollRatio)"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_the_last_editing_tab_restores_the_empty_preview() {
|
||||
let strings = Strings::english();
|
||||
|
||||
Reference in New Issue
Block a user