Merge pull request #9250 from dotCooCoo/fix/open-with-empty-entry

Menus: Open With, Object › Plug-ins and File › Scripts show their notes as disabled rows
This commit is contained in:
BFlat
2026-11-25 11:33:12 +07:00
committed by GitHub
10 changed files with 100 additions and 20 deletions
+5 -5
View File
@@ -46,14 +46,14 @@ use vectorcraft_plugins::{Budget, Kind, Manifest, Plugin, permissions, registry}
use super::form;
use super::preview::LivePreview;
use crate::VectorcraftApp;
use crate::menus::{Sep, c, cp, heading, sub};
use crate::menus::{Sep, c, cp, placeholder, sub};
/// The key of the hosted dialog of plug-in filters.
pub const KIND: &str = "plugin";
const CMD: &str = "plugin.run";
/// Object › Plug-ins' header while no filter plug-in is installed.
/// Object › Plug-ins' disabled row while no filter plug-in is installed.
pub const NO_FILTERS: &str = "No filter plug-ins installed";
/// A menu label for plug-in `name`: with an ellipsis when it opens a dialog.
@@ -199,13 +199,13 @@ pub fn object_menu() -> Vec<Node> {
.map(|p| with_commands(p, cp(label(&p.manifest().name, !p.manifest().params.is_empty()), "plugin.dialog", json!({ "id": p.id() }))))
.collect();
if items.is_empty() {
items.push(heading(NO_FILTERS));
items.push(placeholder(NO_FILTERS));
}
items.extend([Sep, c("Install Plug-in…", "ui.installPlugin"), c("Reload Plug-ins", "plugin.reload")]);
items
}
/// File › Open With's header while no importer plug-in is installed.
/// File › Open With's disabled row while no importer plug-in is installed.
pub const NO_IMPORTERS: &str = "No importer plug-ins installed";
/// File › Open With: the installed importers (`file.openWith {importer}`), each by its name.
@@ -214,7 +214,7 @@ pub fn open_with_menu() -> Vec<Node> {
.iter()
.map(|p| cp(label(&p.manifest().name, true), "file.openWith", json!({ "importer": vectorcraft_plugins::export::format_id(p.id()) })))
.collect();
if items.is_empty() { vec![heading(NO_IMPORTERS)] } else { items }
if items.is_empty() { vec![placeholder(NO_IMPORTERS)] } else { items }
}
/// Effect › Plug-ins: the live-effect plug-ins, each with its commands if it has any (`None`
+5 -5
View File
@@ -805,7 +805,7 @@ fn menu_labels() -> Vec<(String, String, serde_json::Value)> {
fn walk(items: &[Node], out: &mut Vec<(String, String, serde_json::Value)>) {
for i in items {
match i {
Node::Item(it) if it.command.id == crate::menus::TODO => out.push((it.label.clone(), String::new(), serde_json::Value::Null)),
Node::Item(it) if crate::menus::unbound(&it.command.id) => out.push((it.label.clone(), String::new(), serde_json::Value::Null)),
Node::Item(it) => out.push((it.label.clone(), it.command.id.clone(), it.command.params.clone())),
Node::Heading(l) => out.push((l.clone(), String::new(), serde_json::Value::Null)),
Node::Submenu { label, children, .. } => {
@@ -831,11 +831,11 @@ fn menu_item_effect<'a>(id: &str, p: &'a serde_json::Value) -> Option<&'a str> {
/// The label a menu item shows in `lang`, as the menus resolve it: an Effect menu item by its
/// effect's `@id` row when the catalog has one, any other command by its id's row or its English
/// label, and a submenu or an unbuilt item by its English label.
/// label, and a submenu, an unbuilt item or a placeholder by its English label.
fn shown_menu_label(lang: Lang, item: &crate::menus::Node) -> Option<&str> {
use crate::menus::Node;
match item {
Node::Item(it) if it.command.id == crate::menus::TODO => Some(tr(lang, &it.label)),
Node::Item(it) if crate::menus::unbound(&it.command.id) => Some(tr(lang, &it.label)),
Node::Item(it) => {
let (id, p) = (it.command.id.as_str(), &it.command.params);
let by_effect = menu_item_effect(id, p).map(|e| tr_id(lang, e, "")).filter(|s| !s.is_empty());
@@ -857,7 +857,7 @@ fn menu_items_side_by_side_have_different_labels() {
for item in items {
let Some(shown) = shown_menu_label(lang, item) else { continue };
let (english, what) = match item {
Node::Item(it) if it.command.id == crate::menus::TODO => (it.label.as_str(), "submenu or unbuilt item"),
Node::Item(it) if crate::menus::unbound(&it.command.id) => (it.label.as_str(), "submenu, unbuilt item or placeholder"),
Node::Item(it) => (it.label.as_str(), menu_item_effect(&it.command.id, &it.command.params).unwrap_or(it.command.id.as_str())),
Node::Submenu { label, .. } => (label.as_str(), "submenu or unbuilt item"),
_ => continue,
@@ -912,7 +912,7 @@ fn menu_items_never_show_an_id() {
fn walk(lang: Lang, items: &[crate::menus::Node], out: &mut Vec<String>) {
for item in items {
match item {
crate::menus::Node::Item(it) if it.command.id != crate::menus::TODO => {
crate::menus::Node::Item(it) if !crate::menus::unbound(&it.command.id) => {
let (l, id) = (&it.label, it.command.id.as_str());
let key = menu_item_effect(id, &it.command.params).unwrap_or(id);
let shown = shown_menu_label(lang, item).unwrap_or_default();
+15
View File
@@ -23,6 +23,16 @@ pub(crate) use craft::menu::Node::Separator as Sep;
/// The command id of an item that is not built yet; its state is always disabled.
pub(crate) const TODO: &str = "todo";
/// The command id of a placeholder row that stands for an empty list (File › Open With's "No
/// importer plug-ins installed"); it runs nothing and its state is always disabled.
pub(crate) const PLACEHOLDER: &str = "placeholder";
/// Whether a row bound to command `id` runs nothing: a Coming Soon row ([`TODO`]) or a placeholder
/// ([`PLACEHOLDER`]).
pub(crate) fn unbound(id: &str) -> bool {
id == TODO || id == PLACEHOLDER
}
/// An item that runs command `id` with `params`, labelled `label` (the English source label for
/// tree rows, the shown name for rows that list names).
pub(crate) fn named(label: impl Into<String>, id: &str, params: Value) -> Node {
@@ -38,6 +48,11 @@ pub(crate) fn cp(label: &'static str, id: &'static str, params: Value) -> Node {
pub(crate) fn todo(label: &'static str) -> Node {
named(label, TODO, json!({ "label": label }))
}
/// A disabled row that stands for an empty list, so its submenu stays in the menu: the layout
/// drops a heading with no item after it, and with it a submenu left empty.
pub(crate) fn placeholder(label: &'static str) -> Node {
named(label, PLACEHOLDER, json!({ "label": label }))
}
pub(crate) fn sub(label: &'static str, children: Vec<Node>) -> Node {
Node::submenu(label, children)
}
+2 -2
View File
@@ -3,7 +3,7 @@ use super::labels::{display_label, dynamic_label, effect_item_label, item_shortc
use super::slots::{recent_readable, recent_slot};
use super::state::{shortcut_of, shown_state};
use super::tree::{Listed, menu_tree_named};
use super::{Node, Serialize, TODO, Value, VectorcraftApp};
use super::{Node, Serialize, Value, VectorcraftApp, unbound};
/// The label of the menu item that runs `id` with `p` and reads `label` in English, as the menus
/// draw it in the UI language: an effect's own name (Pathfinder › Add), an object's name, a
@@ -169,7 +169,7 @@ pub fn insertable_items(english_names: bool, listed: &Listed) -> Vec<InsertableI
pub(super) fn flatten(app: &VectorcraftApp, path: Vec<String>, shown: Vec<String>, nodes: &[Node], out: &mut Vec<MenuEntry>) {
for node in nodes {
match node {
Node::Item(it) if it.command.id == TODO => out.push(MenuEntry {
Node::Item(it) if unbound(&it.command.id) => out.push(MenuEntry {
path: path.clone(),
label: it.label.clone(),
command: None,
+2 -2
View File
@@ -2,7 +2,7 @@ use super::slots::{perspective_grid, recent_slot, saved_selection_name, saved_se
use super::state::{hidden_when_disabled, shortcut_of};
use super::tree::menu_tree;
use super::ui_commands::UI_COMMANDS;
use super::{Node, TODO, Value, VectorcraftApp};
use super::{Node, Value, VectorcraftApp, unbound};
use craft::keymap::Chord;
/// Label for toggles whose text flips (Outline/Preview, Hide/Show …).
@@ -371,7 +371,7 @@ pub fn menu_strings() -> std::collections::BTreeSet<String> {
fn walk(nodes: &[Node], under: &str, out: &mut BTreeSet<String>) {
for node in nodes {
match node {
Node::Item(it) if it.command.id == TODO => {
Node::Item(it) if unbound(&it.command.id) => {
out.insert(it.label.clone());
}
Node::Item(it) => {
+11 -1
View File
@@ -18,7 +18,7 @@ use craft::ui::runtime::windows::WindowHost;
use serde_json::{Value, json};
use super::labels::holds_plugin_commands;
use super::{Listed, TODO, display_label, menu_tree_named, plugin_revision, shown_label, shown_state, why_off};
use super::{Listed, PLACEHOLDER, TODO, display_label, menu_tree_named, plugin_revision, shown_label, shown_state, why_off};
use crate::i18n::LangExt as _;
use crate::{UiState, VectorcraftApp};
@@ -432,6 +432,16 @@ impl Build<'_> {
let tip = crate::i18n::tr(self.lang, TODO_TOOLTIP).to_string();
return Some(item.tooltip(tip));
}
// A placeholder shows its text in the UI language, as a heading does, and runs nothing.
if command.id == PLACEHOLDER {
self.states.set(command, CommandState::new().enabled(false));
let mut item = item.enabled(false);
let shown = self.title(&item.label, true);
if shown != item.label {
item.english = Some(std::mem::replace(&mut item.label, shown));
}
return Some(item);
}
let (id, params) = (command.id.as_str(), &command.params);
let (enabled, checked) = shown_state(self.app, id, params)?;
let mut state = CommandState::new().enabled(enabled).title(self.item_title(id, params, &item.label));
+2 -2
View File
@@ -5,8 +5,8 @@ use craft::menu::{Gate, ItemKind, LayoutOptions, MenuBar, NativeMenu, Node, mac,
use craft::ui::runtime::commands::{FirePhase, FireSource, Fired, holder};
use crate::VectorcraftApp;
use crate::menus::TODO;
use crate::menus::model::{APP_NAME, item_title, pressed_pass, recording_shortcut, refresh, state_key, translate_label};
use crate::menus::unbound;
/// The layout options every Mac layout of VectorCraft's menus uses: the app's name and its
/// translation of the labels the layout writes itself.
@@ -63,7 +63,7 @@ fn stated(app: &VectorcraftApp) -> MenuBar {
fn walk(app: &VectorcraftApp, lang: crate::i18n::Lang, nodes: &mut [Node]) {
for node in nodes {
match node {
Node::Item(it) if it.command.id == TODO => it.enabled = false,
Node::Item(it) if unbound(&it.command.id) => it.enabled = false,
Node::Item(it) => {
let (id, params) = (it.command.id.as_str(), &it.command.params);
if let Some((enabled, checked)) = crate::menus::shown_state(app, id, params) {
+52 -2
View File
@@ -1237,8 +1237,8 @@ fn every_bound_menu_command_exists() {
match n {
Node::Item(it) => {
let id = it.command.id.as_str();
// A Coming Soon row (`TODO`) is bound to no command.
if id != TODO
// A Coming Soon row (`TODO`) or a placeholder is bound to no command.
if !unbound(id)
&& vectorcraft_engine::find_command(id).is_none()
&& !UI_COMMANDS.iter().any(|c| c.0 == id)
&& !id.starts_with("object.path.")
@@ -1258,6 +1258,56 @@ fn every_bound_menu_command_exists() {
assert!(bad.is_empty(), "menu items bound to unknown commands: {bad:?}");
}
/// File › Open With and Object › Plug-ins stay in the laid-out bar with nothing of their kind installed, showing a
/// disabled row that says so. Plug-ins are process-wide and other tests install some, so each list is checked only
/// while it holds none of its own rows.
#[test]
fn plugin_lists_show_a_disabled_row_when_nothing_is_installed() {
fn submenu<'a>(nodes: &'a [Node], english: &str) -> Option<&'a [Node]> {
nodes.iter().find_map(|n| match n {
Node::Submenu { label, english: e, children, .. } if e.as_deref().unwrap_or(label) == english => Some(children.as_slice()),
_ => None,
})
}
fn placeholder_off(nodes: &[Node], text: &str) -> bool {
nodes.iter().any(|n| matches!(n, Node::Item(it) if it.command.id == PLACEHOLDER && it.command.params["label"] == text && !it.enabled))
}
fn lists(nodes: &[Node], id: &str) -> bool {
nodes.iter().any(|n| match n {
Node::Item(it) => it.command.id == id,
Node::Submenu { children, .. } => lists(children, id),
_ => false,
})
}
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default());
let ctx = crate::testing::ui_context();
crate::menus::model::refresh(&mut app, &ctx);
let menu = |english: &str| {
app.menus.laid.menus.iter().find(|m| m.english.as_deref().unwrap_or(&m.title) == english).map(|m| m.children.as_slice()).unwrap_or_default()
};
let open_with = submenu(menu("File"), "Open With").expect("File › Open With is in the bar");
if !lists(open_with, "file.openWith") {
assert!(placeholder_off(open_with, crate::dialogs::plugin::NO_IMPORTERS), "{open_with:?}");
}
let plugins = submenu(menu("Object"), "Plug-ins").expect("Object › Plug-ins is in the bar");
if !lists(plugins, "plugin.dialog") {
assert!(placeholder_off(plugins, crate::dialogs::plugin::NO_FILTERS), "{plugins:?}");
}
// Whatever is installed: the layout keeps an empty list's row, and the submenu that holds only that row.
let row = |nodes: &[Node], text: &str| nodes.iter().any(|n| matches!(n, Node::Item(it) if it.command.id == PLACEHOLDER && it.label == text));
let mut empty = vec![
sub("Open With", vec![placeholder(crate::dialogs::plugin::NO_IMPORTERS)]),
placeholder(crate::dialogs::plugin::NO_FILTERS),
Sep,
c("Reload Plug-ins", "plugin.reload"),
];
craft::menu::tidy(&mut empty);
assert!(submenu(&empty, "Open With").is_some_and(|o| row(o, crate::dialogs::plugin::NO_IMPORTERS)), "{empty:?}");
assert!(row(&empty, crate::dialogs::plugin::NO_FILTERS), "{empty:?}");
}
/// #543: Object › Image Trace › Make and Make and Expand trace with the Default preset at once,
/// with no form asking for a preset's name.
#[test]
+1 -1
View File
@@ -241,7 +241,7 @@ pub(crate) fn items_at(now: f64) -> Vec<Node> {
};
let mut nodes = level(&listing.entries);
let notes = [listing.truncated.then_some(TRUNCATED), listing.unreadable.then_some(UNREADABLE), note];
nodes.extend(notes.into_iter().flatten().map(Node::heading));
nodes.extend(notes.into_iter().flatten().map(crate::menus::placeholder));
if !nodes.is_empty() {
nodes.push(Node::Separator);
}
+5
View File
@@ -88,6 +88,7 @@ fn label(node: &Node) -> Option<String> {
/// for a separator.
fn shape(nodes: &[Node]) -> String {
let one = |node: &Node| match node {
Node::Item(item) if item.command.id == menus::PLACEHOLDER => format!("({})", item.label),
Node::Item(item) => item.label.clone(),
Node::Submenu { label, children, .. } => format!("{label}/[{}]", shape(children)),
Node::Heading(text) => format!("({text})"),
@@ -297,6 +298,10 @@ fn the_menu_notes_a_listing_that_stopped_and_a_folder_it_cannot_read() {
std::fs::write(dir.join("Scripts"), "").unwrap();
read(&app);
assert_eq!(shape(&scripts()), format!("({}), -, {FIXED}", scripts_menu::UNREADABLE));
// The menu layout keeps the note.
let mut laid = scripts();
craft::menu::tidy(&mut laid);
assert_eq!(shape(&laid), format!("({}), -, {FIXED}", scripts_menu::UNREADABLE));
let _ = std::fs::remove_dir_all(dir);
}