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