refactor: route events through per-window state
This commit is contained in:
502
src/main.rs
502
src/main.rs
@@ -13,19 +13,17 @@ use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::Component;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Instant;
|
||||
use tao::dpi::{LogicalPosition, LogicalSize};
|
||||
use tao::event::{Event as TaoEvent, WindowEvent};
|
||||
use tao::event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy};
|
||||
use tao::window::{Theme, Window, WindowBuilder};
|
||||
use wry::{WebView, WebViewBuilder};
|
||||
use tao::window::{Theme, Window, WindowBuilder, WindowId};
|
||||
use wry::{WebContext, WebView, WebViewBuilder};
|
||||
|
||||
const ICON_BYTES: &[u8] = include_bytes!("../assets/icon.ico");
|
||||
const DEFAULT_W: f64 = 900.0;
|
||||
const DEFAULT_H: f64 = 700.0;
|
||||
static APP_DIRTY: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SelfWriteRecord {
|
||||
@@ -35,25 +33,78 @@ struct SelfWriteRecord {
|
||||
|
||||
#[derive(Debug)]
|
||||
enum UserEvent {
|
||||
NewFile,
|
||||
OpenFile,
|
||||
NewFile(WindowId),
|
||||
OpenFile(WindowId),
|
||||
OpenPaths(Vec<PathBuf>, bool),
|
||||
ActivateTab(u64),
|
||||
CloseTab(u64),
|
||||
ActivateTab(WindowId, u64),
|
||||
CloseTab(WindowId, u64),
|
||||
CloseActiveTab,
|
||||
LocateTab(u64),
|
||||
FileChanged(PathBuf), // external change: refresh preview AND textarea
|
||||
ExternalChangeResolved(bool),
|
||||
FileSaved(PathBuf), // our own save: refresh preview only, leave textarea cursor alone
|
||||
SaveFailed(PathBuf, String),
|
||||
DirtyChanged(bool),
|
||||
LocateTab(WindowId, u64),
|
||||
FileChanged(WindowId, PathBuf), // external change: refresh preview AND textarea
|
||||
ExternalChangeResolved(WindowId, bool),
|
||||
FileSaved(WindowId, PathBuf), // our own save: refresh preview only, leave textarea cursor alone
|
||||
SaveFailed(WindowId, PathBuf, String),
|
||||
DirtyChanged(WindowId, bool),
|
||||
ToggleEdit,
|
||||
ShowFind,
|
||||
Print, // route print through wry's native API (WKWebView ignores window.print())
|
||||
Print(WindowId), // route print through wry's native API (WKWebView ignores window.print())
|
||||
SetTheme(ThemeChoice),
|
||||
Quit,
|
||||
RecentChanged,
|
||||
Ready, // first paint landed: inject hljs now; if bench mode, also exit
|
||||
Ready(WindowId), // first paint landed: inject hljs now; if bench mode, also exit
|
||||
}
|
||||
|
||||
struct WindowState {
|
||||
webview: WebView,
|
||||
window: Window,
|
||||
session: Arc<Mutex<DocumentSession>>,
|
||||
last_self_write: Arc<Mutex<Option<SelfWriteRecord>>>,
|
||||
watcher: Arc<Mutex<Option<notify::RecommendedWatcher>>>,
|
||||
enhance_flags: Arc<Mutex<EnhanceFlags>>,
|
||||
loaded_enhancers: EnhanceFlags,
|
||||
page_ready: bool,
|
||||
pending_launches: Vec<(Vec<PathBuf>, bool)>,
|
||||
pending_close: bool,
|
||||
warned_external_change: Option<PathBuf>,
|
||||
pending_external_change: Option<PathBuf>,
|
||||
dirty: bool,
|
||||
}
|
||||
|
||||
impl WindowState {
|
||||
fn render_active(
|
||||
&mut self,
|
||||
recent_files: &Arc<Mutex<Vec<PathBuf>>>,
|
||||
strings: &Strings,
|
||||
) -> bool {
|
||||
let session = Arc::clone(&self.session);
|
||||
let mut session = session.lock().unwrap();
|
||||
let dirty = render_active_document(
|
||||
&self.webview,
|
||||
&self.window,
|
||||
&mut session,
|
||||
recent_files,
|
||||
&self.enhance_flags,
|
||||
&mut self.loaded_enhancers,
|
||||
strings,
|
||||
);
|
||||
self.dirty = dirty;
|
||||
dirty
|
||||
}
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
windows: HashMap<WindowId, WindowState>,
|
||||
active_window: WindowId,
|
||||
recent_files: Arc<Mutex<Vec<PathBuf>>>,
|
||||
strings: Strings,
|
||||
_web_context: WebContext,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
fn active_mut(&mut self) -> Option<&mut WindowState> {
|
||||
let id = self.active_window;
|
||||
self.windows.get_mut(&id)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
|
||||
@@ -131,6 +182,7 @@ fn detect_lang() -> Lang {
|
||||
.unwrap_or(Lang::En)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Strings {
|
||||
drop_hint: &'static str,
|
||||
cannot_read: &'static str,
|
||||
@@ -3366,6 +3418,7 @@ fn install_file_watcher(
|
||||
holder: &Arc<Mutex<Option<notify::RecommendedWatcher>>>,
|
||||
proxy: &EventLoopProxy<UserEvent>,
|
||||
last_self_write: &Arc<Mutex<Option<SelfWriteRecord>>>,
|
||||
window_id: WindowId,
|
||||
path: Option<PathBuf>,
|
||||
) {
|
||||
let mut current = holder.lock().unwrap();
|
||||
@@ -3390,7 +3443,8 @@ fn install_file_watcher(
|
||||
&mut observed_content,
|
||||
&mut last_self_write,
|
||||
) {
|
||||
let _ = proxy.send_event(UserEvent::FileChanged(callback_path.clone()));
|
||||
let _ =
|
||||
proxy.send_event(UserEvent::FileChanged(window_id, callback_path.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3609,9 +3663,8 @@ fn render_active_document(
|
||||
enhance_flags: &Arc<Mutex<EnhanceFlags>>,
|
||||
loaded_enhancers: &mut EnhanceFlags,
|
||||
strings: &Strings,
|
||||
) {
|
||||
) -> bool {
|
||||
let Some(active) = session.active().cloned() else {
|
||||
APP_DIRTY.store(false, Ordering::SeqCst);
|
||||
let html = empty_preview_html(strings, &recent_files.lock().unwrap());
|
||||
let _ = webview.evaluate_script(&format!(
|
||||
"if(window.__setEmptyPreview)window.__setEmptyPreview('{}');",
|
||||
@@ -3619,7 +3672,7 @@ fn render_active_document(
|
||||
));
|
||||
update_tabs(webview, session);
|
||||
update_window_title(window, session);
|
||||
return;
|
||||
return false;
|
||||
};
|
||||
|
||||
match fs::read_to_string(&active.path) {
|
||||
@@ -3694,12 +3747,10 @@ fn render_active_document(
|
||||
}
|
||||
}
|
||||
|
||||
APP_DIRTY.store(
|
||||
session.active().map(|tab| tab.dirty).unwrap_or(false),
|
||||
Ordering::SeqCst,
|
||||
);
|
||||
let dirty = session.active().map(|tab| tab.dirty).unwrap_or(false);
|
||||
update_tabs(webview, session);
|
||||
update_window_title(window, session);
|
||||
dirty
|
||||
}
|
||||
|
||||
// WebView2 NavigateToString has a 2 MiB limit. Never include document or
|
||||
@@ -3822,6 +3873,7 @@ fn main() {
|
||||
let window = window_builder
|
||||
.build(&event_loop)
|
||||
.expect("failed to build window");
|
||||
let window_id = window.id();
|
||||
bench_log("window_built");
|
||||
let recent_files: Arc<Mutex<Vec<PathBuf>>> = Arc::new(Mutex::new(load_recent_files()));
|
||||
|
||||
@@ -3877,9 +3929,9 @@ fn main() {
|
||||
.with_ipc_handler(move |msg| {
|
||||
let body = msg.body();
|
||||
if body == "new-file" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::NewFile);
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::NewFile(window_id));
|
||||
} else if body == "open" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::OpenFile);
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::OpenFile(window_id));
|
||||
} else if let Some(index) = body.strip_prefix("open-recent:") {
|
||||
if let Ok(index) = index.parse::<usize>() {
|
||||
let path = recent_files_for_ipc.lock().unwrap().get(index).cloned();
|
||||
@@ -3927,10 +3979,11 @@ fn main() {
|
||||
});
|
||||
match fs::write(&path, content) {
|
||||
Ok(()) => {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(path));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(window_id, path));
|
||||
}
|
||||
Err(error) => {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
|
||||
window_id,
|
||||
path.clone(),
|
||||
error.to_string(),
|
||||
));
|
||||
@@ -3940,29 +3993,31 @@ fn main() {
|
||||
}
|
||||
match action {
|
||||
"activate" => {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::ActivateTab(id));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::ActivateTab(window_id, id));
|
||||
}
|
||||
"close" => {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::CloseTab(id));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::CloseTab(window_id, id));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if let Some(id) = body.strip_prefix("locate-tab:") {
|
||||
if let Ok(id) = id.parse::<u64>() {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::LocateTab(id));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::LocateTab(window_id, id));
|
||||
}
|
||||
} else if body == "dirty:1" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(true));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(window_id, true));
|
||||
} else if body == "dirty:0" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(false));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::DirtyChanged(window_id, false));
|
||||
} else if body == "external-change:dirty" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(true));
|
||||
let _ =
|
||||
proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(window_id, true));
|
||||
} else if body == "external-change:clean" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(false));
|
||||
let _ =
|
||||
proxy_for_ipc.send_event(UserEvent::ExternalChangeResolved(window_id, false));
|
||||
} else if body == "print" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::Print);
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::Print(window_id));
|
||||
} else if body == "ready" {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::Ready);
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::Ready(window_id));
|
||||
} else if let Some(percent) = body.strip_prefix("zoom:") {
|
||||
if let Ok(percent) = percent.parse::<u32>() {
|
||||
save_zoom(percent);
|
||||
@@ -3974,7 +4029,7 @@ fn main() {
|
||||
.active()
|
||||
.map(|tab| tab.path.clone())
|
||||
{
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::FileChanged(path));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::FileChanged(window_id, path));
|
||||
}
|
||||
} else if let Some(content) = body.strip_prefix("save:") {
|
||||
let path = session_for_ipc
|
||||
@@ -3989,10 +4044,11 @@ fn main() {
|
||||
});
|
||||
match fs::write(&path, content) {
|
||||
Ok(()) => {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(path));
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::FileSaved(window_id, path));
|
||||
}
|
||||
Err(error) => {
|
||||
let _ = proxy_for_ipc.send_event(UserEvent::SaveFailed(
|
||||
window_id,
|
||||
path.clone(),
|
||||
error.to_string(),
|
||||
));
|
||||
@@ -4048,7 +4104,6 @@ fn main() {
|
||||
}
|
||||
};
|
||||
bench_log("webview_built");
|
||||
let session_for_event = Arc::clone(&document_session);
|
||||
// hljs + extra language packs aren't part of first-paint HTML anymore.
|
||||
// We push them in via evaluate_script the moment the webview tells us
|
||||
// it's painted (IPC 'ready'). Keeps ~125KB out of the HTML-parse critical
|
||||
@@ -4061,33 +4116,63 @@ fn main() {
|
||||
|
||||
// File watcher state
|
||||
let watcher_holder: Arc<Mutex<Option<notify::RecommendedWatcher>>> = Arc::new(Mutex::new(None));
|
||||
let watcher_for_event = Arc::clone(&watcher_holder);
|
||||
|
||||
let mut loaded_enhancers = EnhanceFlags::default();
|
||||
let mut page_ready = false;
|
||||
let mut pending_launches = Vec::new();
|
||||
let mut pending_window_close = false;
|
||||
let mut warned_external_change: Option<PathBuf> = None;
|
||||
let mut pending_external_change: Option<PathBuf> = None;
|
||||
let initial_dirty = document_session
|
||||
.lock()
|
||||
.unwrap()
|
||||
.active()
|
||||
.map(|tab| tab.dirty)
|
||||
.unwrap_or(false);
|
||||
let mut app = AppState {
|
||||
windows: HashMap::from([(
|
||||
window_id,
|
||||
WindowState {
|
||||
window,
|
||||
webview,
|
||||
session: Arc::clone(&document_session),
|
||||
last_self_write: Arc::clone(&last_self_write),
|
||||
watcher: Arc::clone(&watcher_holder),
|
||||
enhance_flags: Arc::clone(&enhance_flags),
|
||||
loaded_enhancers: EnhanceFlags::default(),
|
||||
page_ready: false,
|
||||
pending_launches: Vec::new(),
|
||||
pending_close: false,
|
||||
warned_external_change: None,
|
||||
pending_external_change: None,
|
||||
dirty: initial_dirty,
|
||||
},
|
||||
)]),
|
||||
active_window: window_id,
|
||||
recent_files: Arc::clone(&recent_files),
|
||||
strings,
|
||||
_web_context: web_context,
|
||||
};
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
event_loop.run(move |event, _target, control_flow| {
|
||||
*control_flow = ControlFlow::Wait;
|
||||
let strings = app.strings.clone();
|
||||
let recent_files = Arc::clone(&app.recent_files);
|
||||
|
||||
match event {
|
||||
TaoEvent::UserEvent(UserEvent::NewFile) => {
|
||||
if session_for_event
|
||||
TaoEvent::UserEvent(UserEvent::NewFile(window_id)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
let active_dirty = ws
|
||||
.session
|
||||
.lock()
|
||||
.unwrap()
|
||||
.active()
|
||||
.map(|tab| tab.dirty)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
let _ = webview.evaluate_script(
|
||||
.unwrap_or(false);
|
||||
if active_dirty {
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewNewFile)window.__mdPreviewNewFile();",
|
||||
);
|
||||
return;
|
||||
}
|
||||
let current_dir = session_for_event
|
||||
let current_dir = ws
|
||||
.session
|
||||
.lock()
|
||||
.unwrap()
|
||||
.active()
|
||||
@@ -4110,15 +4195,19 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::OpenFile) => {
|
||||
if session_for_event
|
||||
TaoEvent::UserEvent(UserEvent::OpenFile(window_id)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
let active_dirty = ws
|
||||
.session
|
||||
.lock()
|
||||
.unwrap()
|
||||
.active()
|
||||
.map(|tab| tab.dirty)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
let _ = webview.evaluate_script(
|
||||
.unwrap_or(false);
|
||||
if active_dirty {
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewOpenFile)window.__mdPreviewOpenFile();",
|
||||
);
|
||||
return;
|
||||
@@ -4131,15 +4220,19 @@ fn main() {
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::OpenPaths(paths, edit_on_open)) => {
|
||||
if !page_ready {
|
||||
pending_launches.push((paths, edit_on_open));
|
||||
let window_id = app.active_window;
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
if !ws.page_ready {
|
||||
ws.pending_launches.push((paths, edit_on_open));
|
||||
return;
|
||||
}
|
||||
|
||||
window.set_minimized(false);
|
||||
window.set_focus();
|
||||
ws.window.set_minimized(false);
|
||||
ws.window.set_focus();
|
||||
if paths.is_empty() { return; }
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
let mut session = ws.session.lock().unwrap();
|
||||
let previous_active = session.active_id;
|
||||
let preserve_active = session.active().map(|tab| tab.dirty).unwrap_or(false);
|
||||
for path in paths.into_iter().filter(|path| is_supported_document(path)) {
|
||||
@@ -4152,199 +4245,186 @@ fn main() {
|
||||
}
|
||||
persist_session(&session);
|
||||
if preserve_active {
|
||||
update_tabs(&webview, &session);
|
||||
update_tabs(&ws.webview, &session);
|
||||
} else {
|
||||
render_active_document(
|
||||
&webview,
|
||||
&window,
|
||||
&mut session,
|
||||
&recent_files,
|
||||
&enhance_flags,
|
||||
&mut loaded_enhancers,
|
||||
&strings,
|
||||
);
|
||||
drop(session);
|
||||
ws.render_active(&recent_files, &strings);
|
||||
}
|
||||
let path = session.active().map(|tab| tab.path.clone());
|
||||
drop(session);
|
||||
install_file_watcher(&watcher_for_event, &proxy, &last_self_write, path);
|
||||
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||
install_file_watcher(&ws.watcher, &proxy, &ws.last_self_write, window_id, path);
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::ActivateTab(id)) => {
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
TaoEvent::UserEvent(UserEvent::ActivateTab(window_id, id)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
let mut session = ws.session.lock().unwrap();
|
||||
if session.activate(id) {
|
||||
persist_session(&session);
|
||||
render_active_document(
|
||||
&webview,
|
||||
&window,
|
||||
&mut session,
|
||||
&recent_files,
|
||||
&enhance_flags,
|
||||
&mut loaded_enhancers,
|
||||
&strings,
|
||||
);
|
||||
let path = session.active().map(|tab| tab.path.clone());
|
||||
drop(session);
|
||||
install_file_watcher(&watcher_for_event, &proxy, &last_self_write, path);
|
||||
ws.render_active(&recent_files, &strings);
|
||||
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||
install_file_watcher(&ws.watcher, &proxy, &ws.last_self_write, window_id, path);
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::CloseTab(id)) => {
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
TaoEvent::UserEvent(UserEvent::CloseTab(window_id, id)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
let mut session = ws.session.lock().unwrap();
|
||||
let was_active = session.active_id == Some(id);
|
||||
if session.close(id) {
|
||||
persist_session(&session);
|
||||
if was_active {
|
||||
render_active_document(
|
||||
&webview,
|
||||
&window,
|
||||
&mut session,
|
||||
&recent_files,
|
||||
&enhance_flags,
|
||||
&mut loaded_enhancers,
|
||||
&strings,
|
||||
);
|
||||
let path = session.active().map(|tab| tab.path.clone());
|
||||
drop(session);
|
||||
install_file_watcher(&watcher_for_event, &proxy, &last_self_write, path);
|
||||
ws.render_active(&recent_files, &strings);
|
||||
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||
install_file_watcher(&ws.watcher, &proxy, &ws.last_self_write, window_id, path);
|
||||
} else {
|
||||
update_tabs(&webview, &session);
|
||||
update_tabs(&ws.webview, &session);
|
||||
}
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::CloseActiveTab) => {
|
||||
if session_for_event.lock().unwrap().active_id.is_some() {
|
||||
let _ = webview.evaluate_script(
|
||||
let window_id = app.active_window;
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
if ws.session.lock().unwrap().active_id.is_some() {
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewCloseActiveTab)window.__mdPreviewCloseActiveTab();",
|
||||
);
|
||||
} else {
|
||||
save_window_geom(&window);
|
||||
save_window_geom(&ws.window);
|
||||
*control_flow = ControlFlow::Exit;
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::LocateTab(id)) => {
|
||||
TaoEvent::UserEvent(UserEvent::LocateTab(window_id, id)) => {
|
||||
if let Some(path) = rfd::FileDialog::new()
|
||||
.add_filter("Markdown", &["md", "markdown", "mdown", "mkd", "txt"])
|
||||
.pick_file()
|
||||
{
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
let mut session = ws.session.lock().unwrap();
|
||||
if session.relocate(id, path) && session.activate(id) {
|
||||
persist_session(&session);
|
||||
render_active_document(
|
||||
&webview,
|
||||
&window,
|
||||
&mut session,
|
||||
&recent_files,
|
||||
&enhance_flags,
|
||||
&mut loaded_enhancers,
|
||||
&strings,
|
||||
);
|
||||
let path = session.active().map(|tab| tab.path.clone());
|
||||
drop(session);
|
||||
install_file_watcher(&watcher_for_event, &proxy, &last_self_write, path);
|
||||
ws.render_active(&recent_files, &strings);
|
||||
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||
install_file_watcher(&ws.watcher, &proxy, &ws.last_self_write, window_id, path);
|
||||
} else {
|
||||
show_warning_dialog("Already Open", "That file is already open in another tab.");
|
||||
}
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::FileChanged(path)) => {
|
||||
if session_for_event
|
||||
TaoEvent::UserEvent(UserEvent::FileChanged(window_id, path)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
if ws
|
||||
.session
|
||||
.lock()
|
||||
.unwrap()
|
||||
.active()
|
||||
.map(|tab| tab.path.as_path())
|
||||
== Some(path.as_path())
|
||||
{
|
||||
pending_external_change = Some(path);
|
||||
let _ = webview.evaluate_script(
|
||||
ws.pending_external_change = Some(path);
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewResolveExternalChange)window.__mdPreviewResolveExternalChange();",
|
||||
);
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::ExternalChangeResolved(webview_dirty)) => {
|
||||
let Some(path) = pending_external_change.take() else {
|
||||
TaoEvent::UserEvent(UserEvent::ExternalChangeResolved(window_id, webview_dirty)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
if self_write_still_matches_disk(last_self_write.lock().unwrap().as_ref(), &path) {
|
||||
let Some(path) = ws.pending_external_change.take() else {
|
||||
return;
|
||||
};
|
||||
if self_write_still_matches_disk(ws.last_self_write.lock().unwrap().as_ref(), &path) {
|
||||
return;
|
||||
}
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
let mut session = ws.session.lock().unwrap();
|
||||
if session.active().map(|tab| tab.path.as_path()) == Some(path.as_path()) {
|
||||
let session_dirty = session.active().map(|tab| tab.dirty).unwrap_or(false);
|
||||
if should_protect_external_change(webview_dirty, session_dirty) {
|
||||
if let Some(tab) = session.active_mut() {
|
||||
tab.dirty = true;
|
||||
}
|
||||
APP_DIRTY.store(true, Ordering::SeqCst);
|
||||
let _ = webview.evaluate_script(
|
||||
ws.dirty = true;
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewPauseAutosave)window.__mdPreviewPauseAutosave();",
|
||||
);
|
||||
drop(session);
|
||||
if warned_external_change.as_ref() != Some(&path) {
|
||||
if ws.warned_external_change.as_ref() != Some(&path) {
|
||||
show_warning_dialog(
|
||||
"File Changed on Disk",
|
||||
"Automatic saving is paused and your edits are still in the editor. Press Cmd/Ctrl+S to replace the disk version, or reopen the file to keep the external version.",
|
||||
);
|
||||
warned_external_change = Some(path);
|
||||
ws.warned_external_change = Some(path);
|
||||
}
|
||||
return;
|
||||
}
|
||||
render_active_document(
|
||||
&webview,
|
||||
&window,
|
||||
&mut session,
|
||||
&recent_files,
|
||||
&enhance_flags,
|
||||
&mut loaded_enhancers,
|
||||
&strings,
|
||||
);
|
||||
persist_session(&session);
|
||||
drop(session);
|
||||
ws.render_active(&recent_files, &strings);
|
||||
persist_session(&ws.session.lock().unwrap());
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::FileSaved(path)) => {
|
||||
if warned_external_change.as_ref() == Some(&path) {
|
||||
warned_external_change = None;
|
||||
TaoEvent::UserEvent(UserEvent::FileSaved(window_id, path)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
if ws.warned_external_change.as_ref() == Some(&path) {
|
||||
ws.warned_external_change = None;
|
||||
}
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
let mut session = ws.session.lock().unwrap();
|
||||
let active_matches = session.active().map(|tab| tab.path.as_path()) == Some(path.as_path());
|
||||
if let Some(tab) = session.tabs.iter_mut().find(|tab| tab.path == path) {
|
||||
tab.dirty = false;
|
||||
tab.missing = false;
|
||||
}
|
||||
APP_DIRTY.store(false, Ordering::SeqCst);
|
||||
if active_matches {
|
||||
if let Ok(raw) = fs::read_to_string(&path) {
|
||||
let html = md_to_html_with_base(&raw, path.parent());
|
||||
let flags = enhance_flags_for(&raw);
|
||||
*enhance_flags.lock().unwrap() = flags;
|
||||
let _ = webview.evaluate_script(&format!(
|
||||
*ws.enhance_flags.lock().unwrap() = flags;
|
||||
let _ = ws.webview.evaluate_script(&format!(
|
||||
"if(window.__setPreview)window.__setPreview('{}', {}, {});if(window.__markSaved)window.__markSaved('{}');",
|
||||
escape_js(&html),
|
||||
flags.math,
|
||||
flags.mermaid,
|
||||
escape_js(&raw)
|
||||
));
|
||||
for script in build_enhancer_bootstrap(flags, loaded_enhancers) {
|
||||
let _ = webview.evaluate_script(&script);
|
||||
for script in build_enhancer_bootstrap(flags, ws.loaded_enhancers) {
|
||||
let _ = ws.webview.evaluate_script(&script);
|
||||
}
|
||||
loaded_enhancers.math |= flags.math;
|
||||
loaded_enhancers.mermaid |= flags.mermaid;
|
||||
ws.loaded_enhancers.math |= flags.math;
|
||||
ws.loaded_enhancers.mermaid |= flags.mermaid;
|
||||
}
|
||||
}
|
||||
ws.dirty = session.active().map(|tab| tab.dirty).unwrap_or(false);
|
||||
persist_session(&session);
|
||||
update_tabs(&webview, &session);
|
||||
update_window_title(&window, &session);
|
||||
if pending_window_close {
|
||||
save_window_geom(&window);
|
||||
update_tabs(&ws.webview, &session);
|
||||
update_window_title(&ws.window, &session);
|
||||
if ws.pending_close {
|
||||
save_window_geom(&ws.window);
|
||||
*control_flow = ControlFlow::Exit;
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::SaveFailed(_path, error)) => {
|
||||
let _ = webview.evaluate_script(
|
||||
TaoEvent::UserEvent(UserEvent::SaveFailed(window_id, _path, error)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__markSaveBlocked)window.__markSaveBlocked();",
|
||||
);
|
||||
if pending_window_close {
|
||||
pending_window_close = false;
|
||||
if ws.pending_close {
|
||||
ws.pending_close = false;
|
||||
if confirm_discard_unsaved(&error) {
|
||||
save_window_geom(&window);
|
||||
persist_session(&session_for_event.lock().unwrap());
|
||||
save_window_geom(&ws.window);
|
||||
persist_session(&ws.session.lock().unwrap());
|
||||
*control_flow = ControlFlow::Exit;
|
||||
}
|
||||
} else {
|
||||
@@ -4356,22 +4436,32 @@ fn main() {
|
||||
);
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::DirtyChanged(dirty)) => {
|
||||
APP_DIRTY.store(dirty, Ordering::SeqCst);
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
TaoEvent::UserEvent(UserEvent::DirtyChanged(window_id, dirty)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
ws.dirty = dirty;
|
||||
let mut session = ws.session.lock().unwrap();
|
||||
if let Some(tab) = session.active_mut() {
|
||||
tab.dirty = dirty;
|
||||
}
|
||||
update_tabs(&webview, &session);
|
||||
update_window_title(&window, &session);
|
||||
update_tabs(&ws.webview, &session);
|
||||
update_window_title(&ws.window, &session);
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::ToggleEdit) => {
|
||||
let _ = webview.evaluate_script(
|
||||
let Some(ws) = app.active_mut() else {
|
||||
return;
|
||||
};
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewToggleEdit)window.__mdPreviewToggleEdit();",
|
||||
);
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::ShowFind) => {
|
||||
let _ = webview
|
||||
let Some(ws) = app.active_mut() else {
|
||||
return;
|
||||
};
|
||||
let _ = ws
|
||||
.webview
|
||||
.evaluate_script("if(window.__mdPreviewShowFind)window.__mdPreviewShowFind();");
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::RecentChanged) => {
|
||||
@@ -4380,52 +4470,60 @@ fn main() {
|
||||
"if(window.__setEmptyPreview)window.__setEmptyPreview('{}');",
|
||||
escape_js(&html)
|
||||
);
|
||||
let _ = webview.evaluate_script(&js);
|
||||
for ws in app.windows.values() {
|
||||
let _ = ws.webview.evaluate_script(&js);
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::Print) => {
|
||||
let _ = webview.print();
|
||||
TaoEvent::UserEvent(UserEvent::Print(window_id)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
let _ = ws.webview.print();
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::Quit) => {
|
||||
if APP_DIRTY.load(Ordering::SeqCst) && !pending_window_close {
|
||||
pending_window_close = true;
|
||||
let _ = webview.evaluate_script(
|
||||
let window_id = app.active_window;
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
if ws.dirty && !ws.pending_close {
|
||||
ws.pending_close = true;
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewSave)window.__mdPreviewSave();",
|
||||
);
|
||||
} else if !pending_window_close {
|
||||
save_window_geom(&window);
|
||||
persist_session(&session_for_event.lock().unwrap());
|
||||
} else if !ws.pending_close {
|
||||
save_window_geom(&ws.window);
|
||||
persist_session(&ws.session.lock().unwrap());
|
||||
*control_flow = ControlFlow::Exit;
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::SetTheme(choice)) => {
|
||||
save_theme_choice(choice);
|
||||
window.set_theme(choice.tao_theme());
|
||||
for ws in app.windows.values() {
|
||||
ws.window.set_theme(choice.tao_theme());
|
||||
}
|
||||
}
|
||||
TaoEvent::UserEvent(UserEvent::Ready) => {
|
||||
TaoEvent::UserEvent(UserEvent::Ready(window_id)) => {
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
// First paint is on the screen; now push hljs into the page
|
||||
// (kept out of first-paint HTML to keep it slim). Always do
|
||||
// this, even in bench mode, so subsequent panes would still
|
||||
// highlight — bench just exits right after measuring.
|
||||
let _ = webview.evaluate_script(&hljs_bootstrap);
|
||||
let _ = ws.webview.evaluate_script(&hljs_bootstrap);
|
||||
let zoom = load_zoom();
|
||||
if zoom != 100 {
|
||||
let _ = webview.evaluate_script(&format!(
|
||||
let _ = ws.webview.evaluate_script(&format!(
|
||||
"window.__mdPreviewSetZoom && window.__mdPreviewSetZoom({zoom});"
|
||||
));
|
||||
}
|
||||
if !page_ready {
|
||||
page_ready = true;
|
||||
let mut session = session_for_event.lock().unwrap();
|
||||
render_active_document(
|
||||
&webview, &window, &mut session, &recent_files,
|
||||
&enhance_flags, &mut loaded_enhancers, &strings,
|
||||
);
|
||||
install_file_watcher(
|
||||
&watcher_holder, &proxy, &last_self_write,
|
||||
session.active().map(|tab| tab.path.clone()),
|
||||
);
|
||||
if !ws.page_ready {
|
||||
ws.page_ready = true;
|
||||
ws.render_active(&recent_files, &strings);
|
||||
let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone());
|
||||
install_file_watcher(&ws.watcher, &proxy, &ws.last_self_write, window_id, path);
|
||||
}
|
||||
for (paths, edit) in pending_launches.drain(..) {
|
||||
for (paths, edit) in ws.pending_launches.drain(..) {
|
||||
let _ = proxy.send_event(UserEvent::OpenPaths(paths, edit));
|
||||
}
|
||||
if bench {
|
||||
@@ -4466,16 +4564,20 @@ fn main() {
|
||||
}
|
||||
TaoEvent::WindowEvent {
|
||||
event: WindowEvent::CloseRequested,
|
||||
window_id,
|
||||
..
|
||||
} => {
|
||||
if APP_DIRTY.load(Ordering::SeqCst) && !pending_window_close {
|
||||
pending_window_close = true;
|
||||
let _ = webview.evaluate_script(
|
||||
let Some(ws) = app.windows.get_mut(&window_id) else {
|
||||
return;
|
||||
};
|
||||
if ws.dirty && !ws.pending_close {
|
||||
ws.pending_close = true;
|
||||
let _ = ws.webview.evaluate_script(
|
||||
"if(window.__mdPreviewSave)window.__mdPreviewSave();",
|
||||
);
|
||||
} else if !pending_window_close {
|
||||
save_window_geom(&window);
|
||||
persist_session(&session_for_event.lock().unwrap());
|
||||
} else if !ws.pending_close {
|
||||
save_window_geom(&ws.window);
|
||||
persist_session(&ws.session.lock().unwrap());
|
||||
*control_flow = ControlFlow::Exit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user