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