Document tab menu: Close, Close Others and Reveal in Finder act on the clicked tab

Since the move to the craft UI framework, the document tab's right-click
menu sent the document's id as `document` to file.close,
file.closeOthers and file.revealInFinder, but those commands take the
document's index in the session's list. Ids come from a counter shared
with layers and other objects, so the command found no such document and
nothing happened. The menu now sends the tab's current index; Move to
New Window keeps the id it takes.

Test: three documents; the menu's Close on a tab that is not the active
one closes that document, then Close Others and Close run on the clicked
tab (fails before the change: "no active document").
This commit is contained in:
Ivan Sanchez committed 2026-11-19 13:13:14 -08:00
1 parent 86c2056d01
commit f137c271b7
2 files changed
+40 -11

No files matched your search

+7 -6
View File
@@ -172,7 +172,7 @@ fn tab_menu(app: &mut PhotocraftApp, ui: &mut egui::Ui, cx: &mut BodyCx<'_>, str
};
let count = app.session.documents().len();
let has_path = app.session.documents()[index].path.is_some();
let items = tab_context_items(id, count, has_path);
let items = tab_context_items(id, index, count, has_path);
let mut entries = Vec::with_capacity(items.len() + TAB_MENU_SEPARATORS_AFTER.len());
for (row, (label, _, _, enabled)) in items.iter().enumerate() {
entries.push(Entry::new(row, tl!(*label)).enabled(*enabled));
@@ -198,18 +198,19 @@ pub(crate) const TAB_MENU_SEPARATORS_AFTER: [usize; 2] = [4, 6];
pub(crate) type TabMenuItem = (&'static str, &'static str, serde_json::Value, bool);
/// All tab actions go through the same guarded commands as the menu bar, including the
/// unsaved-changes prompt. The clicked tab's document `doc` is explicit even when another document is active.
/// unsaved-changes prompt. The clicked tab's document `doc` (at `index` in the session's list now) is explicit even when
/// another document is active: the file commands name it by its index, Move to New Window by its id.
/// Order and separators follow the reference application (#11403); see [`TAB_MENU_SEPARATORS_AFTER`].
pub(crate) fn tab_context_items(doc: DocId, count: usize, has_path: bool) -> [TabMenuItem; 8] {
pub(crate) fn tab_context_items(doc: DocId, index: usize, count: usize, has_path: bool) -> [TabMenuItem; 8] {
[
("Consolidate All to Here", "window.arrange.consolidateAllToTabs", json!({}), count > 1),
("Close", "file.close", json!({"document": doc.0}), true),
("Close", "file.close", json!({"document": index}), true),
("Close All", "file.closeAll", json!({}), true),
("Close Others", "file.closeOthers", json!({"document": doc.0}), count > 1),
("Close Others", "file.closeOthers", json!({"document": index}), count > 1),
("Move to New Window", "window.arrange.floatInWindow", json!({"document": doc.0}), true),
("New Document…", "file.new", json!({}), true),
("Open Document…", "file.open", json!({}), true),
(crate::menus::reveal_label(), "file.revealInFinder", json!({"document": doc.0}), has_path),
(crate::menus::reveal_label(), "file.revealInFinder", json!({"document": index}), has_path),
]
}
+33 -5
View File
@@ -6,23 +6,51 @@ use serde_json::json;
use crate::canvas::tabs::tab_context_items;
use crate::{PhotocraftApp, Services};
/// The rows name the clicked tab's document by its id, which stays the same when another document closes.
/// The rows name the clicked tab's document: the file commands by its index in the session's list (they take an index),
/// Move to New Window by its id.
#[test]
fn each_row_names_its_command_and_the_clicked_tabs_document() {
let items = tab_context_items(DocId(7), 3, true);
let items = tab_context_items(DocId(7), 1, 3, true);
let menu = crate::menus::menu_rows(&PhotocraftApp::new(photocraft_engine::Session::new(), Services::default()));
let want = [
("Consolidate All to Here", "window.arrange.consolidateAllToTabs", json!({})),
("Close", "file.close", json!({"document": 7})),
("Close", "file.close", json!({"document": 1})),
("Close All", "file.closeAll", json!({})),
("Close Others", "file.closeOthers", json!({"document": 7})),
("Close Others", "file.closeOthers", json!({"document": 1})),
("Move to New Window", "window.arrange.floatInWindow", json!({"document": 7})),
("New Document…", "file.new", json!({})),
("Open Document…", "file.open", json!({})),
(crate::menus::reveal_label(), "file.revealInFinder", json!({"document": 7})),
(crate::menus::reveal_label(), "file.revealInFinder", json!({"document": 1})),
];
for ((label, id, params, _), (wl, wi, wp)) in items.iter().zip(want) {
assert_eq!((*label, *id, params), (wl, wi, &wp));
assert!(menu.iter().any(|row| row.command.id == *id) || photocraft_engine::commands::find(id).is_some(), "{id} is a command");
}
}
/// A document's id is not its place in the list: ids come from a counter shared with layers and other objects, so the
/// menu's Close, Close Others and Reveal in Finder named a document that did not exist and nothing happened.
#[test]
fn the_tab_menu_closes_the_clicked_document() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default());
for name in ["A", "B", "C"] {
app.run("file.new", json!({"width": 8, "height": 8, "name": name})).unwrap();
}
let ctx = app.ctx().clone();
let names = |app: &PhotocraftApp| app.session.documents().iter().map(|st| st.doc.name.clone()).collect::<Vec<_>>();
assert!(app.session.documents().iter().enumerate().any(|(i, st)| st.doc.id.0 != i as u64), "ids are not indices");
let run = |app: &mut PhotocraftApp, name: &str, label: &str| {
let index = app.session.documents().iter().position(|st| st.doc.name == name).unwrap();
let id = app.session.documents()[index].doc.id;
let count = app.session.documents().len();
let (_, command, params, _) = tab_context_items(id, index, count, false).into_iter().find(|(l, ..)| *l == label).unwrap();
crate::menus::invoke(app, &ctx, command, params).unwrap();
};
// Close on a tab that is not the active one (C is).
run(&mut app, "B", "Close");
assert_eq!(names(&app), ["A", "C"]);
run(&mut app, "A", "Close Others");
assert_eq!(names(&app), ["A"]);
run(&mut app, "A", "Close");
assert!(names(&app).is_empty());
}