diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rs index 510131be0e..40ebc46ba5 100644 --- a/crates/ui-egui/src/native_menu.rs +++ b/crates/ui-egui/src/native_menu.rs @@ -610,7 +610,7 @@ pub fn sync(app: &mut VectorcraftApp, ctx: &egui::Context) { /// interaction is open (for example a move, scale or shape drag, a dialog's or the font menu's /// preview, or a typing session), the revision, which each of its previews changes, is left out: /// the hash changes as the interaction begins and as it ends. -fn state_hash(app: &VectorcraftApp, input: Option) -> u64 { +pub(crate) fn state_hash(app: &VectorcraftApp, input: Option) -> u64 { let mut h = DefaultHasher::new(); input.hash(&mut h); app.run_count.hash(&mut h); diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index dbd19f3f57..6f6acb5643 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -437,6 +437,73 @@ pub(crate) fn run_pick(app: &mut VectorcraftApp, id: &str) { } } +/// Why the result `id` can't run now, as an English status message, or `None` while it can: the +/// engine's reason when it gives one, else the status line a dimmed menu item's pick gives +/// ([`crate::shortcut_editor::dimmed`]). A panel can't be shown in Presentation Mode, a tool can +/// always be chosen, and every other result asks [`menus::enabled`] about the command its pick +/// runs. +fn unavailable(app: &VectorcraftApp, id: &str) -> Option { + if id.starts_with("tool:") { + return None; + } + if id.starts_with("panel:") && app.ui.screen_mode >= 3 { + return Some("panels can't be shown in Presentation Mode".to_string()); + } + // The command the pick runs. + let id = if id.starts_with("panel:") || id.starts_with("plugin:") || id.starts_with("effect:") { + crate::keymap::command_of(id).id + } else { + id.to_string() + }; + if menus::enabled(app, &id) { + return None; + } + let why = vectorcraft_engine::find_command(&id).and_then(|c| (c.enabled)(&app.session).err()); + Some(why.unwrap_or_else(|| format!("command `{id}` is not available right now"))) +} + +/// The drawn results' [`unavailable`] answers, in the interface language, for the state `key` +/// ([`availability_key`]) they were read in. An engine check can take tens of milliseconds on a +/// large selection, so each result is asked once per state instead of on every frame. +#[derive(Clone, Default)] +struct Availability { + key: u64, + known: std::collections::HashMap>, +} + +impl Availability { + /// Why the result `id` can't run now, translated, or `None` while it can. + fn why(&mut self, app: &VectorcraftApp, id: &str) -> Option { + if let Some(why) = self.known.get(id) { + return why.clone(); + } + let why = unavailable(app, id).map(|why| crate::i18n::msg(&why)); + self.known.insert(id.to_string(), why.clone()); + why + } +} + +/// What the results' availability depends on: what the native menu bar's rows depend on +/// ([`crate::native_menu::state_hash`]: commands run, the document's revision, selection, history +/// and clipboard, the language and more), whether the document has unsaved changes, the screen +/// mode, an open dialog, and the background saves and exports, file reads, plug-in runs and traces +/// that run or show their progress. A frame with a pointer button release also changes it, for +/// what it leaves out (a panel shown, a tool chosen); typing in the search field does not. +fn availability_key(app: &VectorcraftApp, ctx: &egui::Context) -> u64 { + use std::hash::{Hash, Hasher}; + let click = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::PointerButton { pressed: false, .. }))); + let mut h = std::collections::hash_map::DefaultHasher::new(); + crate::native_menu::state_hash(app, click.then_some(app.host_iteration)).hash(&mut h); + let running = app.background.jobs.iter().filter(|j| j.progress.is_some() && !j.cancelled()).count(); + (app.ui.screen_mode, app.ui.dialog.is_some(), app.background.jobs.len(), running).hash(&mut h); + (app.opening.pending.len(), crate::opening::any_shown(app)).hash(&mut h); + // A background save marks the document saved without a new revision. + app.session.active().map(|d| (d.path.is_some(), d.converted, d.is_dirty())).hash(&mut h); + let plugins = (crate::plugin_runs::any_running(app), crate::plugin_runs::any_shown(app)); + (plugins, crate::trace_runs::any_running(app), crate::trace_runs::any_shown(app)).hash(&mut h); + h.finish() +} + /// The entries the query `q` matches, best first ([`search::rank`]), with the commands picked most /// often (`uses`) first among the entries that match as well, and then the picks that Recent /// Commands lists for the ids `recent_ids` ([`recent`]). The ranking of the last query is kept, so @@ -788,6 +855,13 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { }); } let screen = ctx.content_rect(); + // The drawn results' availability, kept while the state it was read in lasts. + let availability_id = egui::Id::unique("palette-availability"); + let key = availability_key(app, ctx); + let mut availability = ctx + .data_mut(|d| d.remove_temp::(availability_id)) + .filter(|a| a.key == key) + .unwrap_or(Availability { key, known: Default::default() }); // The highlighted result the rows and the field's description are drawn with. let mut drawn = sel; // The backdrop covers the canvas, the toolbars, the docked panels and the app bar under the @@ -884,19 +958,32 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { if on { ui.painter().rect_filled(rect, 4.0, ui.visuals().selection.bg_fill); } + // A result that can't run now stays in its place, drawn dimmed, and says + // why on hover and to screen readers. + let why = availability.why(app, &e.id); + let (name_color, shortcut_color) = if why.is_some() { (t.text_disabled, t.text_disabled) } else { (t.text, t.text_dim) }; let font = egui::TextStyle::Body.resolve(ui.style()); let shortcut = menus::shown_shortcut(&e.shortcut); let p = ui.painter(); let shortcut_at = rect.right_center() - egui::vec2(8.0, 0.0); - let used = p.text(shortcut_at, egui::Align2::RIGHT_CENTER, &shortcut, font.clone(), t.text_dim); + let used = p.text(shortcut_at, egui::Align2::RIGHT_CENTER, &shortcut, font.clone(), shortcut_color); let name_end = if shortcut.is_empty() { rect.right() - 8.0 } else { used.left() - 12.0 }; let name_at = rect.left() + 8.0; - let name = name_galley(p, &e.shown, font, t.text, name_end - name_at); - p.galley(egui::pos2(name_at, rect.center().y - name.size().y / 2.0), name, t.text); + let name = name_galley(p, &e.shown, font, name_color, name_end - name_at); + p.galley(egui::pos2(name_at, rect.center().y - name.size().y / 2.0), name, name_color); // `WidgetInfo::selected` reaches screen readers as a toggled state, so the // row sets the selected state on its node itself. - resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ListItem, true, row_label(e))); - ui.ctx().accesskit_node_builder(resp.id, |node| node.set_selected(on)); + resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ListItem, why.is_none(), row_label(e))); + ui.ctx().accesskit_node_builder(resp.id, |node| { + node.set_selected(on); + if let Some(why) = &why { + node.set_description(why.clone()); + } + }); + let resp = match why { + Some(why) => resp.on_hover_text(why), + None => resp, + }; if resp.clicked() { run = Some(e.id.clone()); } @@ -911,10 +998,23 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { } }); } + // While the highlighted result can't run now, the footer also says why, so a keyboard + // user reads it too: on the count's line, or alone under the recent picks, so the + // footer stays the height counted above. + let why = matches.get(sel).and_then(|e| availability.why(app, &e.id)); // Under the recent picks no count is shown; with none, one dimmed line says so. if !empty { ui.add_space(6.0); - ui.label(egui::RichText::new(footer(n, total)).color(t.text_dim)); + let count = ui.label(egui::RichText::new(footer(n, total)).color(t.text_dim)); + if let Some(why) = why { + let right = ui.max_rect().right(); + let at = egui::Rect::from_x_y_ranges((count.rect.right() + 12.0).min(right)..=right, count.rect.y_range()); + let why = egui::RichText::new(why).color(t.text_dim); + ui.put(at, egui::Label::new(why).truncate().halign(egui::Align::Max)); + } + } else if let Some(why) = why { + ui.add_space(6.0); + ui.add(egui::Label::new(egui::RichText::new(why).color(t.text_dim)).truncate()); } else if n == 0 { ui.label(egui::RichText::new(tl!("No recent commands")).color(t.text_dim)); } @@ -951,6 +1051,7 @@ pub fn show(app: &mut VectorcraftApp, ctx: &egui::Context) { ctx.data_mut(|d| { d.insert_temp::(sel_id, (sel, q, offset, view, sel_key)); d.insert_temp(rsel_id, rsel); + d.insert_temp(availability_id, availability); }); if let Some(id) = run { app.ui.palette_open = false; @@ -2621,6 +2722,165 @@ mod tests { assert!(crate::panel_docking::showing(&open.app.ui, &panel), "{panel}: {}", open.app.ui.status); } + /// The open palette on `query` over the session `session`. + fn open_on(query: &str, session: vectorcraft_engine::Session) -> Open { + let mut open = Open::new(query, WINDOW); + open.app = VectorcraftApp::new(session, crate::Services::default()); + open.app.ui.palette_open = true; + open.app.ui.palette_query = query.into(); + open.frame(vec![]); + open + } + + /// The screen-reader node of the result row named `name`. + fn row_named<'a>(nodes: &'a [egui::accesskit::Node], name: &str) -> &'a egui::accesskit::Node { + nodes + .iter() + .filter(|n| n.role() == egui::accesskit::Role::ListItem) + .find(|n| n.label().is_some_and(|l| l == name || l.starts_with(&format!("{name}, ")))) + .unwrap_or_else(|| panic!("a row named {name}: {:?}", rows(nodes))) + } + + /// Highlights the result `id` with the arrow keys: the frame's screen-reader tree. + fn highlight(open: &mut Open, id: &str) -> Vec { + let k = open.listed().iter().position(|e| e == id).unwrap_or_else(|| panic!("{id} is listed")); + assert!(k < VISIBLE_ROWS, "{:?}", open.listed()); + let nodes = open.press(egui::Key::ArrowDown, k); + assert_eq!(open.highlighted(), k); + nodes + } + + /// With nothing selected, Ungroup stays listed in its place, dimmed, with the engine's reason in + /// its screen-reader description and in the footer while it is highlighted. Selecting art while + /// the palette stays open shows it as available on the next frame, in the same order. (A query + /// other tests' plug-in names do not match, so the list stays the same between frames.) + #[test] + fn a_result_that_cannot_run_now_is_dimmed_and_says_why() { + let mut session = vectorcraft_testkit::fixtures::Fixture::Multi.session(); + session.execute("select.none", &json!({})).unwrap(); + let why = crate::i18n::msg(&(vectorcraft_engine::find_command("object.ungroup").unwrap().enabled)(&session).unwrap_err()); + let mut open = open_on("ungroup", session); + let name = entries(&open.ctx, Lang::EN).iter().find(|e| e.id == "object.ungroup").map(row_label).unwrap(); + let order = open.listed(); + let nodes = highlight(&mut open, "object.ungroup"); + let ungroup = row_named(&nodes, &name); + assert!(ungroup.is_disabled(), "{ungroup:?}"); + assert_eq!(ungroup.description(), Some(why.as_str())); + let footer_says = |nodes: &[egui::accesskit::Node]| labels(nodes).contains(&why); + assert!(footer_says(&nodes), "{:?}", labels(&nodes)); + let dimmed_rows: Vec = rows(&nodes).into_iter().map(|r| r.0).collect(); + + open.app.run("select.all", json!({})).unwrap(); + let nodes = open.frame(vec![]); + let ungroup = row_named(&nodes, &name); + assert!(!ungroup.is_disabled() && ungroup.description().is_none(), "{ungroup:?}"); + assert!(!footer_says(&nodes), "{:?}", labels(&nodes)); + // Whether a result can run never moves it. + assert_eq!(open.listed(), order); + assert_eq!(rows(&nodes).into_iter().map(|r| r.0).collect::>(), dimmed_rows); + } + + /// Under an empty search, a highlighted recent pick that can't run now is dimmed and the line + /// under the picks says why. + #[test] + fn a_recent_pick_that_cannot_run_now_says_why() { + let mut session = vectorcraft_testkit::fixtures::Fixture::Multi.session(); + session.execute("select.none", &json!({})).unwrap(); + let why = crate::i18n::msg(&(vectorcraft_engine::find_command("object.ungroup").unwrap().enabled)(&session).unwrap_err()); + let mut open = open_on("", session); + open.app.ui.recent_commands = vec!["object.ungroup".to_string(), "object.group".to_string()]; + let nodes = open.frame(vec![]); + assert_eq!(open.highlighted(), 0); + let rows: Vec<_> = nodes.iter().filter(|n| n.role() == egui::accesskit::Role::ListItem).collect(); + assert_eq!(rows.len(), 2, "{rows:?}"); + assert!(rows.iter().all(|n| n.is_disabled()), "{rows:?}"); + assert!(labels(&nodes).contains(&why), "{:?}", labels(&nodes)); + let popup = open.popup(); + assert!(popup.bottom() <= WINDOW.y, "{popup:?}"); + } + + /// The drawn results are asked whether they can run once per state: keys pressed in the palette + /// keep the answers, and a command run or a click asks again. + #[test] + fn availability_is_read_once_per_state() { + let mut session = vectorcraft_testkit::fixtures::Fixture::Multi.session(); + session.execute("select.none", &json!({})).unwrap(); + let mut open = open_on("ungroup", session); + let id = egui::Id::unique("palette-availability"); + let read = |open: &Open| open.ctx.data(|d| d.get_temp::(id)).expect("the availability is kept"); + let first = read(&open); + assert!(first.known.get("object.ungroup").is_some_and(Option::is_some), "{:?}", first.known); + // The key also takes in state shared by every test (the language, the loaded fonts, the + // installed plug-ins), which other tests change at the same time, so the presses are tried + // again until one try keeps the key. + let kept = (0..20).any(|_| { + open.frame(vec![]); + let before = read(&open).key; + open.press(egui::Key::ArrowDown, 1); + open.press(egui::Key::X, 1); + read(&open).key == before + }); + assert!(kept, "key presses keep the answers"); + let first = read(&open); + open.app.run("select.all", json!({})).unwrap(); + open.frame(vec![]); + let after = read(&open); + assert_ne!(after.key, first.key); + assert_eq!(after.known.get("object.ungroup"), Some(&None), "asked again: {:?}", after.known); + let release = egui::Event::PointerButton { + pos: egui::pos2(1.0, 1.0), + button: egui::PointerButton::Primary, + pressed: false, + modifiers: egui::Modifiers::NONE, + }; + open.frame(vec![release]); + assert_ne!(read(&open).key, after.key, "a click asks again"); + } + + /// Paste is dimmed while the clipboard is empty and nothing can be pasted from the system + /// clipboard, and not while the system clipboard can be pasted. + #[test] + fn paste_is_dimmed_only_while_nothing_can_be_pasted() { + let mut open = open_on("paste", vectorcraft_testkit::fixtures::Fixture::Multi.session()); + open.app.system_paste = false; + let nodes = open.frame(vec![]); + let paste = row_named(&nodes, "Edit › Paste"); + assert!(paste.is_disabled(), "{paste:?}"); + assert_eq!(paste.description(), Some(crate::i18n::msg("clipboard is empty").as_str())); + open.app.system_paste = true; + let nodes = open.frame(vec![]); + assert!(!row_named(&nodes, "Edit › Paste").is_disabled()); + } + + /// A tool can always be chosen; a panel can't be shown in Presentation Mode, and says so. + #[test] + fn tools_are_never_dimmed_and_panels_are_in_presentation_mode() { + let app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); + let tools: Vec = build(Lang::EN).into_iter().map(|e| e.id).filter(|id| id.starts_with("tool:")).collect(); + assert!(!tools.is_empty()); + assert!(tools.iter().all(|id| unavailable(&app, id).is_none()), "{tools:?}"); + let mut app = VectorcraftApp::new(vectorcraft_testkit::fixtures::Fixture::Multi.session(), crate::Services::default()); + assert_eq!(unavailable(&app, "panel:layers"), None); + app.ui.screen_mode = 3; + assert_eq!(unavailable(&app, "panel:layers").as_deref(), Some("panels can't be shown in Presentation Mode")); + } + + /// A UI command without an engine reason says it is not available, and a filter plug-in or an + /// effect asks about the command its pick runs. + #[test] + fn a_result_without_an_engine_reason_says_it_is_not_available() { + let app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); + assert!(vectorcraft_engine::find_command("ui.newBrush").is_none()); + assert_eq!(unavailable(&app, "ui.newBrush").as_deref(), Some("command `ui.newBrush` is not available right now")); + assert_eq!(unavailable(&app, "plugin:any.filter").as_deref(), Some("command `plugin.dialog` is not available right now")); + let effect = crate::shortcut_editor::effect_items().first().map(|e| format!("effect:{}", e.effect)).expect("an Effect menu item"); + assert!(unavailable(&app, &effect).is_some(), "{effect}"); + let app = VectorcraftApp::new(vectorcraft_testkit::fixtures::Fixture::Multi.session(), crate::Services::default()); + assert_eq!(unavailable(&app, "ui.newBrush"), None); + assert_eq!(unavailable(&app, "plugin:any.filter"), None); + assert_eq!(unavailable(&app, &effect), None, "{effect}"); + } + /// Screen readers stay on the field: its description names the highlighted result and its /// place, and follows the highlight; the rows are list items, the highlighted one selected, and /// the footer's count is a label. diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 5fc911bc7a..4a1d5c828e 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1312,6 +1312,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur The Command Palette ranks the commands listed in Recent Commands first among the results that match a query as well as they do, the most recent first, and below every result that matches it better: after a pick of Effect › Convert to Shape › Rectangle…, "rect" lists it above the Rectangle Tool, so Enter runs it again. A recent pick ranks after the whole-query match, the word grades and the matches through a typo, a description or a word with the same meaning, and before the kind of result and the name's length. The ranking uses the list Recent Commands shows, which also holds a pick whose command could not run. This is VectorCraft's own behavior (`palette::search` test `a_recent_pick_ranks_first_within_its_grades`; `palette` tests `a_recent_pick_ranks_first_among_results_that_match_as_well`, `the_kept_ranking_follows_the_recent_picks`). The Command Palette also counts how many times each command is picked and ranks the commands picked most often first among the results that match a query as well as they do, also after they leave Recent Commands, and below every result that matches it better. Counts are compared in grades that double: grade 1 starts at 1 pick, grade 2 at 3, grade 3 at 7, and so on up to grade 9, and a command picked once ranks above the commands never picked. The grade ranks after the word grades and the typo, description and same-meaning matches, and before the place among the recent picks. The count is kept for at most 200 commands: a new command then removes the one with the fewest picks, of those the one picked longest ago. When a count reaches 1,000, every count is halved and commands left at 0 are dropped. The counts are saved with the other interface settings (`command_uses` in `ui.json`); a count above 999 loads as 999 and an unreadable entry is dropped. The web build keeps them for the session. This is VectorCraft's own behavior (`palette::search` test `a_command_picked_often_ranks_first_within_its_grades`; `palette` tests `a_command_picked_often_ranks_first_among_results_that_match_as_well`, `the_kept_ranking_follows_the_pick_counts`, `a_count_does_not_pass_a_better_match`, `pick_counts_rank_in_grades_that_double`, `the_201st_command_removes_the_least_used_and_keeps_itself`, `counts_are_halved_at_one_thousand`, `saved_pick_counts_load_what_is_readable`). A Command Palette query word also finds a command by what the command's description says: "transparent background" lists Remove Background… among the first 3 results, and "png" lists it after every result with "png" in its name or menu path. The description is the command's params text (an engine or plug-in command's, a UI command's, or an effect's) without each `{…}` group that is not inside a `{`, `[` or `(` group, without each such `[…]` or `(…)` group just after a `→`, and without the `→` before a removed group, cut to its first 300 characters at the end of a word. The words of a schema ("corners" for Remove Background…) and of a result written as a group do not find the command; a result written as words after a `→` stays in the description. Words of 2 characters or fewer, words with no letter, and "the", "and", "ids", "true", "false" and "null" are left out. A query word that is a word of a description, shares an English base word with one, starts one or has the same meaning as one finds the result, and such a result ranks below every result that the query matches in its name, menu path or id, as text inside a word or through a typo. Descriptions are English only, and a typo does not match them. Tools, panels and filter plug-ins have no description (`palette::search` tests `a_description_leaves_out_the_schema_and_the_result`, `a_word_of_the_description_finds_the_result`, `a_description_match_ranks_below_a_typo`, `words_of_a_description_find_the_command`). + A Command Palette result that cannot run now stays in its place in the list and is drawn dimmed: an engine command whose engine check fails (Ungroup with nothing selected, Paste with an empty clipboard and nothing to paste from the system clipboard), a UI command the menus dim, a filter plug-in or an effect whose menu item is dimmed, and a panel in Presentation Mode. Tools are never dimmed. The engine's reason, or that the command is not available right now, shows on hover, in the row's screen-reader description and in the footer while the row is highlighted, beside the count or, under the recent picks of an empty search, on a line of its own. Only the drawn rows are checked, each once for as long as the state it was checked in lasts: a command run, an edit, a change of selection, document, history, clipboard, language, screen mode or open dialog, a save that marks the document saved, a background save, export, file read, plug-in run or trace that starts, ends or shows its progress window, or a click checks them again on the next frame, while typing in the search field does not. The check never changes the ranking. A pick of a dimmed result does what it did before (`palette` tests `a_result_that_cannot_run_now_is_dimmed_and_says_why`, `paste_is_dimmed_only_while_nothing_can_be_pasted`, `tools_are_never_dimmed_and_panels_are_in_presentation_mode`, `a_result_without_an_engine_reason_says_it_is_not_available`, `a_recent_pick_that_cannot_run_now_says_why`, `availability_is_read_once_per_state`). `object.setProps` with `visible: false` or `locked: true` deselects every selected object that is then hidden or locked, the key object included, as the eye and lock columns do. Ctrl/Cmd+Alt-clicking an eye switches the other layers to Outline and the row's layer to Preview, or switches all layers back to Preview when the others are in Outline (`layer.outlineOthers`, `layer.previewAll`). Ctrl/Cmd+Alt-clicking New Layer adds a layer directly below the current layer, at its level (`layer.new {below: true}`), and Ctrl/Cmd+Alt-clicking New Sublayer adds a sublayer under the contents of the layer it goes into (`layer.newSublayer {bottom: true}`), each in one undo step. The reference app's [Default keyboard shortcuts](https://web.archive.org/web/20260726060015/https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html) page lists these clicks for placing a layer below the selected layer and a new sublayer at the bottom of the layer list.