mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Merge PR #15835: Document tab menu: Close, Close Others and Reveal in Finder act on the clicked tab
This commit is contained in:
commit
33ce9d6184
2 files changed
+40
-11
No files matched your search
@@ -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),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
Reference in new issue
Block a user