diff --git a/src/main.rs b/src/main.rs index 870ed2c..a241feb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,8 +37,7 @@ struct SelfWriteRecord { enum UserEvent { NewFile(WindowId), OpenFile(WindowId), - NewWindow(WindowId), - OpenPaths(Vec, bool), + OpenPaths(Option, Vec, bool), ActivateTab(WindowId, u64), CloseTab(WindowId, u64), DetachTab(WindowId, u64), @@ -3732,26 +3731,32 @@ fn focus_open_tab( recent_files: &Arc>>, strings: &Strings, proxy: &EventLoopProxy, -) { - app.active_window = window_id; +) -> bool { let Some(ws) = app.windows.get_mut(&window_id) else { - return; + return false; }; - ws.window.set_minimized(false); - ws.window.set_focus(); let mut session = ws.session.lock().unwrap(); let target_active = session.active_id == Some(tab_id); let active_dirty = session.active().map(|tab| tab.dirty).unwrap_or(false); - if !target_active && active_dirty { - // Do not switch away from unsaved edits just to focus another tab. - return; + let target_dirty = session + .tabs + .iter() + .find(|tab| tab.id == tab_id) + .map(|tab| tab.dirty) + .unwrap_or(false); + if !target_active && (active_dirty || target_dirty) { + // Never switch away from or reload an unsaved tab just to focus it. + return false; } if !session.activate(tab_id) { - return; + return false; } + app.active_window = window_id; + ws.window.set_minimized(false); + ws.window.set_focus(); if target_active { update_tabs(&ws.webview, &session); - return; + return true; } drop(session); ws.render_active(recent_files, strings); @@ -3762,6 +3767,7 @@ fn focus_open_tab( .active() .map(|tab| tab.path.clone()); install_file_watcher(&ws.watcher, proxy, &ws.last_self_write, window_id, path); + true } fn update_tabs(webview: &WebView, session: &DocumentSession) { @@ -3920,7 +3926,11 @@ fn build_webview( let _ = open::that(&url); false } else if let Some(path) = local_document_path_from_url(&url) { - let _ = proxy_for_navigation.send_event(UserEvent::OpenPaths(vec![path], false)); + let _ = proxy_for_navigation.send_event(UserEvent::OpenPaths( + Some(window_id), + vec![path], + false, + )); false } else if url.starts_with("file:") { false @@ -3934,15 +3944,16 @@ fn build_webview( let _ = proxy_for_ipc.send_event(UserEvent::NewFile(window_id)); } else if body == "open" { let _ = proxy_for_ipc.send_event(UserEvent::OpenFile(window_id)); - } else if body == "new-window" { - let _ = proxy_for_ipc.send_event(UserEvent::NewWindow(window_id)); } else if let Some(index) = body.strip_prefix("open-recent:") { if let Ok(index) = index.parse::() { let path = recent_files.lock().unwrap().get(index).cloned(); if let Some(path) = path { if path.exists() { - let _ = - proxy_for_ipc.send_event(UserEvent::OpenPaths(vec![path], false)); + let _ = proxy_for_ipc.send_event(UserEvent::OpenPaths( + Some(window_id), + vec![path], + false, + )); } else if forget_recent_file(&recent_files, &path) { let _ = proxy_for_ipc.send_event(UserEvent::RecentChanged); } @@ -3951,7 +3962,11 @@ fn build_webview( } else if let Some(url) = body.strip_prefix("open-local-link:") { let active_path = session.lock().unwrap().active().map(|tab| tab.path.clone()); if let Some(path) = resolve_document_link(url, active_path.as_deref()) { - let _ = proxy_for_ipc.send_event(UserEvent::OpenPaths(vec![path], false)); + let _ = proxy_for_ipc.send_event(UserEvent::OpenPaths( + Some(window_id), + vec![path], + false, + )); } } else if let Some(rest) = body.strip_prefix("tab-action:") { let (header, pending_content) = rest @@ -4054,7 +4069,11 @@ fn build_webview( .filter(|path| is_supported_document(path)) .collect::>(); if !paths.is_empty() { - let _ = proxy_for_drag.send_event(UserEvent::OpenPaths(paths, false)); + let _ = proxy_for_drag.send_event(UserEvent::OpenPaths( + Some(window_id), + paths, + false, + )); } } true @@ -4113,21 +4132,6 @@ fn secondary_geom(source: &WindowState, target: &EventLoopWindowTarget, - app: &mut AppState, - proxy: &EventLoopProxy, -) -> Result { - let source_window_id = app.active_window; - create_window_with_session( - target, - app, - proxy, - DocumentSession::default(), - source_window_id, - ) -} - fn create_window_with_session( target: &EventLoopWindowTarget, app: &mut AppState, @@ -4309,7 +4313,7 @@ fn main() { let proxy = proxy.clone(); primary.listen(move |launch| { proxy - .send_event(UserEvent::OpenPaths(launch.paths, launch.edit)) + .send_event(UserEvent::OpenPaths(None, launch.paths, launch.edit)) .is_ok() }); } @@ -4466,7 +4470,11 @@ fn main() { let path = normalize_new_markdown_path(path); match fs::write(&path, "") { Ok(()) => { - let _ = proxy.send_event(UserEvent::OpenPaths(vec![path], true)); + let _ = proxy.send_event(UserEvent::OpenPaths( + Some(window_id), + vec![path], + true, + )); } Err(error) => { show_warning_dialog("Could Not Create File", &error.to_string()); @@ -4474,14 +4482,6 @@ fn main() { } } } - TaoEvent::UserEvent(UserEvent::NewWindow(window_id)) => { - if !app.windows.contains_key(&window_id) { - return; - } - if let Err(error) = create_empty_window(target, &mut app, &proxy) { - show_warning_dialog("Could Not Create Window", &error); - } - } TaoEvent::UserEvent(UserEvent::OpenFile(window_id)) => { let Some(ws) = app.windows.get_mut(&window_id) else { return; @@ -4503,11 +4503,15 @@ fn main() { .add_filter("Markdown", &["md", "markdown", "mdown", "mkd", "txt"]) .pick_files() { - let _ = proxy.send_event(UserEvent::OpenPaths(paths, false)); + let _ = proxy.send_event(UserEvent::OpenPaths( + Some(window_id), + paths, + false, + )); } } - TaoEvent::UserEvent(UserEvent::OpenPaths(paths, edit_on_open)) => { - let mut target_window = app.active_window; + TaoEvent::UserEvent(UserEvent::OpenPaths(target, paths, edit_on_open)) => { + let target_window = target.unwrap_or(app.active_window); let Some(ws) = app.windows.get_mut(&target_window) else { return; }; @@ -4527,15 +4531,19 @@ fn main() { } for path in paths { if let Some((window_id, tab_id)) = find_open_path(&app, &path) { - focus_open_tab( + if !focus_open_tab( &mut app, window_id, tab_id, &recent_files, &strings, &proxy, - ); - target_window = window_id; + ) { + show_warning_dialog( + "Unsaved Changes", + "That file is open in another window with unsaved changes.", + ); + } continue; } let Some(ws) = app.windows.get_mut(&target_window) else { @@ -4583,13 +4591,22 @@ fn main() { } } TaoEvent::UserEvent(UserEvent::CloseTab(window_id, id)) => { + let multiple_windows = app.windows.len() > 1; 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) { - if was_active { + if session.tabs.is_empty() { + if multiple_windows { + drop(session); + close_window(&mut app, window_id, control_flow); + } else { + drop(session); + ws.render_active(&recent_files, &strings); + } + } else if was_active { drop(session); ws.render_active(&recent_files, &strings); let path = ws.session.lock().unwrap().active().map(|tab| tab.path.clone()); @@ -4603,7 +4620,7 @@ fn main() { let Some(ws) = app.windows.get_mut(&window_id) else { return; }; - let mut session = ws.session.lock().unwrap(); + let session = ws.session.lock().unwrap(); let Some(tab) = session.tabs.iter().find(|tab| tab.id == tab_id).cloned() else { return; }; @@ -4616,9 +4633,6 @@ fn main() { ); return; } - if !target_active { - session.activate(tab_id); - } drop(session); let mut moved = tab; @@ -4666,6 +4680,7 @@ fn main() { } TaoEvent::UserEvent(UserEvent::CloseActiveTab) => { let window_id = app.active_window; + let multiple_windows = app.windows.len() > 1; let Some(ws) = app.windows.get_mut(&window_id) else { return; }; @@ -4673,9 +4688,8 @@ fn main() { let _ = ws.webview.evaluate_script( "if(window.__mdPreviewCloseActiveTab)window.__mdPreviewCloseActiveTab();", ); - } else { - save_window_geom(&ws.window); - *control_flow = ControlFlow::Exit; + } else if multiple_windows { + close_window(&mut app, window_id, control_flow); } } TaoEvent::UserEvent(UserEvent::LocateTab(window_id, id)) => { @@ -4684,14 +4698,19 @@ fn main() { .pick_file() { if let Some((other_id, other_tab)) = find_open_path(&app, &path) { - focus_open_tab( + if !focus_open_tab( &mut app, other_id, other_tab, &recent_files, &strings, &proxy, - ); + ) { + show_warning_dialog( + "Unsaved Changes", + "That file is open in another window with unsaved changes.", + ); + } return; } let Some(ws) = app.windows.get_mut(&window_id) else { @@ -4858,7 +4877,9 @@ fn main() { escape_js(&html) ); for ws in app.windows.values() { - let _ = ws.webview.evaluate_script(&js); + if ws.session.lock().unwrap().tabs.is_empty() { + let _ = ws.webview.evaluate_script(&js); + } } } TaoEvent::UserEvent(UserEvent::Print(window_id)) => { @@ -4912,7 +4933,7 @@ fn main() { install_file_watcher(&ws.watcher, &proxy, &ws.last_self_write, window_id, path); } for (paths, edit) in ws.pending_launches.drain(..) { - let _ = proxy.send_event(UserEvent::OpenPaths(paths, edit)); + let _ = proxy.send_event(UserEvent::OpenPaths(Some(window_id), paths, edit)); } if bench { eprintln!("[bench] +{}ms ready", t0.elapsed().as_millis()); @@ -4933,7 +4954,11 @@ fn main() { match action { FinderAction::Create { folder, kind } => match create_finder_file(&folder, &kind) { Ok(path) if kind == "md" => { - let _ = proxy.send_event(UserEvent::OpenPaths(vec![path], true)); + let _ = proxy.send_event(UserEvent::OpenPaths( + None, + vec![path], + true, + )); } Ok(_) => {} Err(error) => show_warning_dialog("Could Not Create File", &error.to_string()), @@ -4947,7 +4972,7 @@ fn main() { } } if !paths.is_empty() { - let _ = proxy.send_event(UserEvent::OpenPaths(paths, false)); + let _ = proxy.send_event(UserEvent::OpenPaths(None, paths, false)); } } TaoEvent::WindowEvent {