From 1bc3bb8ebc22f94a439ebc6641d9ff9f06b3cd09 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 05:31:01 +0700 Subject: [PATCH] =?UTF-8?q?Keys=20reach=20tools=20and=20panels=20as=20prod?= =?UTF-8?q?uction=20routed=20them:=20Enter=20opens=20a=20selection=20tool'?= =?UTF-8?q?s=20Move=20dialog=20or=20what=20the=20tool=20asks=20for;=20whil?= =?UTF-8?q?e=20a=20tool's=20gesture=20runs=20its=20keys=20go=20to=20it=20a?= =?UTF-8?q?nd=20its=20own=20shortcut=20ends=20it;=20Escape=20reaches=20wha?= =?UTF-8?q?t=20it=20ends;=20framework=20sets=20made=20for=20other=20apps?= =?UTF-8?q?=20(blend=20mode,=20layer=20order,=20find=20bar,=20warp=20pins,?= =?UTF-8?q?=20rich=20text=20editor)=20give=20up=20their=20keys;=20the=20To?= =?UTF-8?q?ols=20panel=20and=20toolbars=20come=20back=20when=20shown=20aga?= =?UTF-8?q?in=20and=20Window=20=E2=80=BA=20Toolbars=20checks=20the=20shown?= =?UTF-8?q?=20ones;=20Unlock=20All=20and=20Show=20All=20keep=20their=20Shi?= =?UTF-8?q?ft+Alt=20group=20alternate;=20Escape=20in=20Help=20keeps=20its?= =?UTF-8?q?=20query;=20dimmed=20rows=20say=20they=20are=20not=20available;?= =?UTF-8?q?=20the=20Control=20bar's=20Select=20Similar=20menu=20shows=20it?= =?UTF-8?q?s=20ampersand=20and=20Global=20Edit=20Options=20closes=20while?= =?UTF-8?q?=20Global=20Edit=20runs;=20the=20app=20bar's=20search=20keeps?= =?UTF-8?q?=20production's=20name;=20an=20sRGB=20monitor=20profile=20conve?= =?UTF-8?q?rts=20nothing.=20Tests:=20dialogs=20keyed=20by=20their=20comman?= =?UTF-8?q?d,=20the=20window=20frame=20that=20builds=20the=20keymap=20or?= =?UTF-8?q?=20applies=20claims,=20production's=20Pen=20clicks,=20held=20OK?= =?UTF-8?q?s=20answered=20through=20the=20pump?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ui-egui/src/canvas.rs | 2 +- crates/ui-egui/src/canvas/tool_adapter.rs | 91 ++++++++++++++++++++++- crates/ui-egui/src/chrome.rs | 2 + crates/ui-egui/src/chrome/control_bar.rs | 11 ++- crates/ui-egui/src/chrome/tests.rs | 4 +- crates/ui-egui/src/custom_toolbars.rs | 5 ++ crates/ui-egui/src/display_profile.rs | 7 +- crates/ui-egui/src/menus/model.rs | 30 ++++++-- crates/ui-egui/src/menus/state.rs | 8 +- crates/ui-egui/src/menus/tests.rs | 5 ++ crates/ui-egui/src/menus/tree.rs | 11 ++- crates/ui-egui/src/palette.rs | 2 +- crates/ui-egui/src/panel_docking.rs | 5 +- crates/ui-egui/src/panels/discover.rs | 11 +-- crates/ui-egui/src/panels/properties.rs | 2 +- crates/ui-egui/src/shortcuts.rs | 9 +++ crates/ui-egui/src/shortcuts/tests.rs | 43 +++++++---- crates/ui-egui/src/toolbar/tests.rs | 13 +++- 18 files changed, 214 insertions(+), 47 deletions(-) diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index d077d5e940..6bd21ada0c 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -52,7 +52,7 @@ pub(crate) use raster::{KeptFrame, color_image, color_image_owned, render_job}; pub(crate) use rulers::{general_lengths, stroke_lengths}; pub(crate) use task_bar::{TASK_BAR_ACTIONS, TASK_BAR_CHIPS, has_colors, is_task_bar_action, read_hidden_task_bar_actions}; pub use tool_adapter::{apply_requests, dispatch, mods}; -pub(crate) use tool_adapter::{in_gesture, pose_on_page}; +pub(crate) use tool_adapter::{busy_tool, in_gesture, pose_on_page}; /// Which body a document's canvas runs: the current window's interactive one, or a display-only /// pane of a tiled or floating window. diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index e778f256af..47e143f737 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -144,6 +144,68 @@ impl<'a> SessionTool<'a> { apply_requests(&mut app, r); true } + + /// Enter that the tool does not claim, while no gesture runs: it takes what the tool asks of the UI (Rotate, + /// Scale, Reflect and Shear open their dialog), and with a selection tool it opens the Move dialog, as a + /// double-click on the tool's button does. Taken only when a dialog opened, so that Enter is not also its OK. + fn enter(&self, mods: Mods) -> bool { + let mut app = self.app(); + if app.session.tool_busy() { + return false; + } + let open = |app: &VectorcraftApp| app.dialogs().map_or(0, |d| d.with(|h| h.open_dialogs().len())); + let before = open(&app); + let r = app.session.tool_key(EngineKey::Enter, mods, self.view); + apply_requests(&mut app, r); + let tool = app.session.tool_id().to_owned(); + if open(&app) == before && vectorcraft_tools::catalog::is_selection_tool(&tool) { + // Nothing selected: it fails and nothing opens (the menu item is disabled then too). + let _ = crate::toolbar::open_options(&mut app, &tool); + } + open(&app) > before + } + + /// A key the tool does not claim while its gesture runs: arrows and digits still reach it, with no modifier (a + /// grid's rows and columns, the Perspective Selection tool's planes); other keys do nothing then. + fn busy_key(&self, key: EngineKey) -> bool { + let mut app = self.app(); + if !busy_tool(&app) { + return false; + } + if matches!(key, EngineKey::Left | EngineKey::Right | EngineKey::Up | EngineKey::Down | EngineKey::Digit(_)) { + let r = app.session.tool_key(key, Mods::default(), self.view); + apply_requests(&mut app, r); + } + true + } + + /// While the gesture runs, the active tool's own shortcut chooses it again between clicks, which ends the gesture + /// (W with the Blend tool's first pick held finishes the blend); any other plain key does nothing. + fn again(&self, chord: Chord) -> bool { + let mut app = self.app(); + if !busy_tool(&app) { + return false; + } + let active = format!("tool:{}", app.session.tool_id()); + let own = app.ui_ctx.as_ref().and_then(craft::ui::runtime::commands::keymap).is_some_and(|keymap| { + match keymap.lookup(&chord, false, &craft::keymap::Active::NONE) { + craft::keymap::Lookup::Run(command) => command.id == active, + craft::keymap::Lookup::Cycle(commands) => commands.iter().any(|command| command.id == active), + _ => false, + } + }); + if own && !app.session.pointer_held() { + let tool = app.session.tool_id().to_owned(); + app.select_tool(&tool); + } + true + } +} + +/// Whether the active tool's gesture runs (a press, an open pick or loop), outside text editing: production gave the +/// keys to the tool then and ran no shortcut. +pub(crate) fn busy_tool(app: &VectorcraftApp) -> bool { + app.session.tool_busy() && !app.session.tool_wants_text() } /// The engine's key for the letter or digit key `key`, from its physical name (`KeyA`, `Digit5`). @@ -157,6 +219,10 @@ fn engine_key(chord: Chord) -> Option<(EngineKey, bool)> { Key::Tab => (EngineKey::Tab, shift), Key::Home => (EngineKey::Home, shift), Key::End => (EngineKey::End, shift), + Key::Left => (EngineKey::Left, shift), + Key::Right => (EngineKey::Right, shift), + Key::Up => (EngineKey::Up, shift), + Key::Down => (EngineKey::Down, shift), key => { let name = key.code_name(); if let Some(letter) = name.strip_prefix("Key").and_then(|l| l.chars().next()) { @@ -306,6 +372,10 @@ impl CanvasTool for SessionTool<'_> { _ => &[], }; let mut claims = ToolClaims::NONE.keys(keys); + // While a gesture runs the plain keys are the tool's: no single-key shortcut runs then. + if busy_tool(&app) { + claims = claims.keyboard(); + } if claim(&mut app, EngineKey::Delete) { claims = claims.deletes(); } @@ -326,7 +396,7 @@ impl CanvasTool for SessionTool<'_> { (false, _, true) => (EngineKey::Up, Key::Up), (false, _, false) => (EngineKey::Down, Key::Down), }; - if self.send_key(k, tool_mods(*mods, space)) { + if self.send_key(k, tool_mods(*mods, space)) || self.busy_key(k) { return true; } // An arrow an application command is bound to runs it, as production's keymap ran it first; one it @@ -345,16 +415,29 @@ impl CanvasTool for SessionTool<'_> { !bound } ToolKey::Delete { forward, .. } => self.send_key(if *forward { EngineKey::Delete } else { EngineKey::Backspace }, held), - ToolKey::Submit => self.send_key(EngineKey::Enter, held), + ToolKey::Submit => self.send_key(EngineKey::Enter, held) || self.enter(held), ToolKey::Cancel => { // In Presentation Mode Escape leaves the mode: the tool takes none. if self.app().ui.screen_mode == 3 { return false; } let ends = self.app().session.tool_ends_on_escape(self.view); - self.send_key(EngineKey::Escape, held) || ends + if self.send_key(EngineKey::Escape, held) { + return true; + } + // What Escape ends (a loop, a path being drawn, a running gesture) gets it whether or not the tool + // claims it. + let mut app = self.app(); + if !ends && !app.session.tool_busy() { + return false; + } + let r = app.session.tool_key(EngineKey::Escape, held, self.view); + apply_requests(&mut app, r); + true + } + ToolKey::Kept(chord) => { + engine_key(*chord).is_some_and(|(k, shift)| self.send_key(k, Mods { shift, ..held }) || self.busy_key(k)) || self.again(*chord) } - ToolKey::Kept(chord) => engine_key(*chord).is_some_and(|(k, shift)| self.send_key(k, Mods { shift, ..held })), ToolKey::AltTap => self.send_key(EngineKey::AltTap, held), _ => false, } diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index da631b764d..dfe809712b 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -99,7 +99,9 @@ impl Trailing { Trailing::Arrange => arrange_documents(app, ui), Trailing::Search => { // PLAN: assumed `Button::command` is bound by a `&str` command id; the command opens the palette. + // Named as production's field, not after the command's menu row. Button::command(PALETTE) + .label(tl!("Search commands")) .icon(IconSpec::svg(IconId::new("search"))) .variant(Variant::SECONDARY) .align(HAlign::Start) diff --git a/crates/ui-egui/src/chrome/control_bar.rs b/crates/ui-egui/src/chrome/control_bar.rs index 9452dee1a0..b0bf95f59f 100644 --- a/crates/ui-egui/src/chrome/control_bar.rs +++ b/crates/ui-egui/src/chrome/control_bar.rs @@ -271,8 +271,12 @@ fn global_edit_options(app: &mut VectorcraftApp, ui: &mut Ui) { let on = app.session.active().is_some_and(|d| d.global_edit.is_some()); let label = tl!("Global Edit Options"); ranked(ui, rank::DETAIL, |ui| { - let options = IconButton::new(IconId::new("chevron-down")).label(label).enabled(!on).popup(PopupKind::Menu).show(ui); - Menu::new(label).opened_by(&options, Trigger::Click).show(ui, |ui, _| crate::panels::properties::global_edit_options(app, ui)); + let options = IconButton::new(IconId::new("chevron-down")).label(label).enabled(!on).show(ui); + // Properties' popover with the bar's own flag: closed while Global Edit is on, and it stays closed after. + let id = options.response.id.with("open"); + let mut open = ui.data(|d| d.get_temp::(id)).unwrap_or(false); + crate::panels::properties::global_edit_popover(app, ui, &options.response, &mut open); + ui.data_mut(|d| d.insert_temp(id, open)); }); } @@ -377,7 +381,8 @@ fn trailing_select_similar(app: &mut VectorcraftApp, ui: &mut Ui) { let options = IconButton::new(IconId::new("chevron-down")).label(label).enabled(enabled).popup(PopupKind::Menu).show(ui); Menu::new(label).opened_by(&options, Trigger::Click).show(ui, |ui, _| { for &(by, on) in &kept { - if MenuItem::new(similar_label(by)).check(on).show(ui).ran { + // A menu row reads `&` as its mnemonic marker: `&&` is the character. + if MenuItem::new(similar_label(by).replace('&', "&&")).check(on).show(ui).ran { params = Some(json!({ "by": by })); } } diff --git a/crates/ui-egui/src/chrome/tests.rs b/crates/ui-egui/src/chrome/tests.rs index d9f9e32a66..1514732bc2 100644 --- a/crates/ui-egui/src/chrome/tests.rs +++ b/crates/ui-egui/src/chrome/tests.rs @@ -932,9 +932,11 @@ fn properties_delete(bar: &mut Shown, shown: &str) -> Target { delete } -/// A press and release of `key` through the window's whole frame, as the keyboard sends it. +/// A press and release of `key` through the window's whole frame, as the keyboard sends it. The window's first frame +/// builds its keymap and the keys the tool claims, as a running window has them before any key. fn key_through_the_app(app: &mut crate::VectorcraftApp, key: egui::Key, modifiers: egui::Modifiers) { let ctx = crate::testing::ui_context(); + crate::tests_synthetic::key_frame(app, &ctx, vec![]); let event = |pressed| egui::Event::Key { key, physical_key: None, pressed, repeat: false, modifiers }; crate::tests_synthetic::key_frame(app, &ctx, vec![event(true)]); crate::tests_synthetic::key_frame(app, &ctx, vec![event(false)]); diff --git a/crates/ui-egui/src/custom_toolbars.rs b/crates/ui-egui/src/custom_toolbars.rs index 8fc345cd2a..0947fc37fe 100644 --- a/crates/ui-egui/src/custom_toolbars.rs +++ b/crates/ui-egui/src/custom_toolbars.rs @@ -71,6 +71,11 @@ pub(crate) fn panel_id(id: u32) -> String { format!("toolbar:{id}") } +/// Whether `bar` is on, as Window › Toolbars checks it: its panel is in the layout, though Tab may hide it for now. +pub(crate) fn is_on(ui: &UiState, bar: &CustomToolbar) -> bool { + panel_docking::layout_of(ui).contains(&panel_id(bar.id)) +} + /// The toolbar named `name`, whatever its case. pub(crate) fn find<'a>(ui: &'a UiState, name: &str) -> Option<&'a CustomToolbar> { let k = key(name); diff --git a/crates/ui-egui/src/display_profile.rs b/crates/ui-egui/src/display_profile.rs index f7d02bd6d5..a4e58500c2 100644 --- a/crates/ui-egui/src/display_profile.rs +++ b/crates/ui-egui/src/display_profile.rs @@ -13,7 +13,7 @@ use std::sync::{Arc, RwLock}; -use craft::cms::{Builtin, Profile}; +use craft::cms::Profile; use craft::color_ui::display::{DisplayTransform, check_monitor}; use craft::color_ui::ui::ProfileWords; use serde_json::{Value, json}; @@ -260,7 +260,10 @@ pub fn check_profile(icc: &[u8]) -> Result<(), String> { fn build(icc: &[u8]) -> Result { check_profile(icc)?; let monitor = Profile::parse(icc).map_err(|e| e.to_string())?; - DisplayTransform::new(Arc::new(Builtin::Srgb.profile().clone()), Arc::new(monitor)).map_err(|e| e.to_string()) + // The canvas holds the document's sRGB: a monitor profile of that sRGB converts nothing. + let srgb = vectorcraft_color::cms::icc_bytes(vectorcraft_color::cms::SRGB).map_err(|e| e.to_string())?; + let source = Profile::parse(&srgb).map_err(|e| e.to_string())?; + DisplayTransform::new(Arc::new(source), Arc::new(monitor)).map_err(|e| e.to_string()) } /// One opaque sRGB color through `t`. diff --git a/crates/ui-egui/src/menus/model.rs b/crates/ui-egui/src/menus/model.rs index 5946d03d29..521b7fba2a 100644 --- a/crates/ui-egui/src/menus/model.rs +++ b/crates/ui-egui/src/menus/model.rs @@ -184,6 +184,22 @@ fn hash_listed_names(ui: &UiState, h: &mut DefaultHasher) { } } +/// The prefixes of linked command sets made for other apps, which VectorCraft does not route: their keys (Shift+= +/// and Alt+Shift+letters for blend modes, Cmd+[ and Cmd+] for layer order, Cmd+G and F3 for a find bar, Insert for +/// warp pins, a rich text editor's Cmd+Enter and Cmd+Shift+B, which the Type tool's editor has) are VectorCraft's own +/// commands' keys, so the catalog drops them. +const UNROUTED_SETS: [&str; 6] = ["blendMode.", "layersPanel.", "find.", "pinWarp.", "perspectiveWarp.", "textEditor."]; + +/// The commands of [`UNROUTED_SETS`] that come with default keys. +static UNROUTED: std::sync::LazyLock> = std::sync::LazyLock::new(|| { + craft::ui::runtime::commands::sets() + .into_iter() + .flat_map(|set| (set.defs)().unwrap_or_default()) + .filter(|def| !def.defaults.is_empty() && UNROUTED_SETS.iter().any(|prefix| def.command.id.starts_with(prefix))) + .map(|def| def.command.id) + .collect() +}); + fn rebuild(app: &mut VectorcraftApp, target: Target) { // The Symbols panel menu's states read the selection's instances from the walk this keeps. crate::panels::symbols::cache_instances(app); @@ -207,7 +223,7 @@ fn rebuild(app: &mut VectorcraftApp, target: Target) { let options = LayoutOptions::new(APP_NAME).label(&translate_label); let laid = layout(&bar, target, &options).bar; match AppCatalog::new(defs, generation(&listed, &crate::panels::actions::function_keys(&app.ui.actions))) { - Ok(catalog) => app.menus.catalog = Some(catalog), + Ok(catalog) => app.menus.catalog = Some(UNROUTED.iter().fold(catalog, |catalog, id| catalog.replace_defaults(id.as_str(), Vec::new()))), // A refused catalog is a bug in the rows; the last good one keeps the commands working. Err(e) => eprintln!("menu catalog refused: {e:?}"), } @@ -259,9 +275,9 @@ fn catalog_defs(app: &VectorcraftApp, refs: &[(CommandRef, String, bool)], state } let shown = display_label(app, id, &d.label); if shown != d.label { - state = state.title(shown); + state = state.title(shown.clone()); } - if let Some(reason) = Some(enabled).filter(|e| !e).and_then(|_| why_off(app, id).flatten()) { + if let Some(reason) = Some(enabled).filter(|e| !e).and_then(|_| off_reason(app, id, &shown)) { state = state.hint(reason); } states.set(d.command.clone(), state); @@ -269,6 +285,11 @@ fn catalog_defs(app: &VectorcraftApp, refs: &[(CommandRef, String, bool)], state defs } +/// What a dimmed row named `label` says: the engine's reason, else that the command is not available right now. +fn off_reason(app: &VectorcraftApp, id: &str, label: &str) -> Option { + why_off(app, id).map(|why| why.unwrap_or_else(|| crate::i18n::msg(&format!("command `{label}` is not available right now")))) +} + /// The tree turned into the bar's nodes, with the state of every item it keeps. struct Build<'a> { app: &'a VectorcraftApp, @@ -332,7 +353,7 @@ impl Build<'_> { } else if enabled { None } else { - why_off(self.app, id).flatten() + off_reason(self.app, id, &self.item_title(id, params, &item.label)) }; if let Some(tip) = tip { state = state.hint(tip.clone()); @@ -361,7 +382,6 @@ impl Build<'_> { let run_with = |mods: Mods, p: Value| Alternate::new(mods, CommandRef::with_params(id, p)); let held = |shift: bool, alt: bool| Mods { cmd: false, ctrl: false, alt, shift }; match id { - "object.unlockAll" | "object.showAll" => item.alternate(run_with(held(true, true), json!({ "scope": "group" }))), "ui.expandDialog" => item.alternate(run_with(held(false, true), json!({ "immediate": true }))), crate::addons::select::link_status::COMMAND => { item.alternate(run_with(held(true, false), crate::addons::select::link_status::with_shift(params.clone()))) diff --git a/crates/ui-egui/src/menus/state.rs b/crates/ui-egui/src/menus/state.rs index 4dc66647bf..c2a1d27a16 100644 --- a/crates/ui-egui/src/menus/state.rs +++ b/crates/ui-egui/src/menus/state.rs @@ -83,9 +83,11 @@ pub fn checked(app: &VectorcraftApp, id: &str, p: &Value) -> Option { && crate::custom_toolbars::drawer_target(&app.ui).map(crate::custom_toolbars::key) == p.get("toolbar").and_then(Value::as_str).map(crate::custom_toolbars::key) } - "window.toolbar.custom" => { - p.get("name").and_then(Value::as_str).and_then(|name| crate::custom_toolbars::find(&app.ui, name)).is_some_and(|b| b.shown) - } + "window.toolbar.custom" => p + .get("name") + .and_then(Value::as_str) + .and_then(|name| crate::custom_toolbars::find(&app.ui, name)) + .is_some_and(|b| crate::custom_toolbars::is_on(&app.ui, b)), "window.floatTools" => { let tool = p.get("tool").and_then(Value::as_str).unwrap_or(""); let toolbar = p.get("toolbar").and_then(Value::as_str); diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index 25f7cfbd5c..3e06c81731 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -314,12 +314,16 @@ fn discover_finds_commands_by_their_shown_menu_label() { fn discover_results_show_the_current_shortcut_and_language_after_a_change() { // Help searches in the window's interface language. let mut app = crate::testing::windowed(VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default())); + // The keymap is the window's: its frame builds it, and builds it again with the shortcuts as they changed. + let ctx = app.ui_ctx.clone().unwrap(); + app_frame(&mut app, &ctx, vec![]); let found = app.run("help.discover.search", json!({"query": "file.newDialog"})).unwrap(); let index = found["results"].as_array().unwrap().iter().position(|hit| hit["id"] == "file.newDialog").unwrap(); assert_eq!(found["results"][index]["shortcut"], "Cmd+N"); app.run("help.discover.choose", json!({"index": index})).unwrap(); app.run("window.panel.close", json!({"panel": "discover"})).unwrap(); app.run("shortcuts.set", json!({"id": "file.newDialog", "shortcut": "Cmd+Alt+Shift+F9"})).unwrap(); + app_frame(&mut app, &ctx, vec![]); let reopened = app.run("help.discover", json!({})).unwrap(); assert_eq!(reopened["query"], "file.newDialog"); @@ -331,6 +335,7 @@ fn discover_results_show_the_current_shortcut_and_language_after_a_change() { assert_eq!(picked["location"]["shortcut"], "Cmd+Alt+Shift+F9"); app.run("shortcuts.set", json!({"id": "file.newDialog", "shortcut": "Cmd+Alt+Shift+F8"})).unwrap(); + app_frame(&mut app, &ctx, vec![]); let text = crate::tests_labels::painted_text(&mut app, crate::panels::discover::show); // PLAN: assumed `keymap::chord_text(&str) -> String`, the text the chord shows as assert!(text.contains(&crate::keymap::chord_text("Cmd+Alt+Shift+F8")), "{text}"); diff --git a/crates/ui-egui/src/menus/tree.rs b/crates/ui-egui/src/menus/tree.rs index 2e6bcbdb6f..13b33dde44 100644 --- a/crates/ui-egui/src/menus/tree.rs +++ b/crates/ui-egui/src/menus/tree.rs @@ -6,6 +6,13 @@ use craft::menu::{Item, ItemRole, Platforms}; use crate::i18n::LangExt as _; +/// `label` running `id`, and with Shift+Alt held as it is chosen, `id` within the selected object's group. +fn within_group(label: &'static str, id: &'static str) -> Node { + let held = craft::keymap::Mods { cmd: false, ctrl: false, alt: true, shift: true }; + let group = craft::ui::menu::Alternate::new(held, CommandRef::with_params(id, json!({ "scope": "group" }))); + Node::Item(Item::new(CommandRef::with_params(id, serde_json::Value::Null), label).alternate(group)) +} + /// The in-window and native menu tree. The in-window bar skips the VectorCraft menu (see /// `menu_bar`); the native Mac bar is composed from the whole tree (see `native_menu`). /// `english_names` is Preferences › Type › Show Font Names in English (the Type → Font labels @@ -220,7 +227,7 @@ pub fn menu_tree_named(english_names: bool, listed: &Listed) -> Vec<(&'static st "Lock", vec![c("Selection", "object.lock"), c("All Artwork Above", "object.lock.above"), c("Other Layers", "object.lock.otherLayers")], ), - c("Unlock All", "object.unlockAll"), + within_group("Unlock All", "object.unlockAll"), sub( "Hide", vec![ @@ -230,7 +237,7 @@ pub fn menu_tree_named(english_names: bool, listed: &Listed) -> Vec<(&'static st c("Unselected Artwork", "object.hide.unselected"), ], ), - c("Show All", "object.showAll"), + within_group("Show All", "object.showAll"), Sep, c("Expand…", "ui.expandDialog"), c("Expand Appearance", "effect.expandAppearance"), diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs index 1cc9820fd8..f363f8eefc 100644 --- a/crates/ui-egui/src/palette.rs +++ b/crates/ui-egui/src/palette.rs @@ -472,7 +472,7 @@ fn tool_home(app: &VectorcraftApp, tool: &str) -> String { }); let active = app.session.tool_id(); let custom = |shown: bool| { - app.ui.custom_toolbars.iter().filter(|b| b.shown == shown).find_map(|bar| { + app.ui.custom_toolbars.iter().filter(|b| crate::custom_toolbars::is_on(&app.ui, b) == shown).find_map(|bar| { let slot = bar.slots.iter().find(|s| s.iter().any(|t| t == tool))?; let tools: Vec<&'static str> = slot.iter().filter_map(|id| vectorcraft_tools::tool_info(id).map(|t| t.id)).collect(); if tools.len() > 1 && tools.first().is_some_and(|first| crate::toolbar::is_floating(app, Some(&bar.name), first)) { diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs index f6829b04c4..a87f1f4f55 100644 --- a/crates/ui-egui/src/panel_docking.rs +++ b/crates/ui-egui/src/panel_docking.rs @@ -464,8 +464,9 @@ pub(crate) fn reveal_panel(app: &mut VectorcraftApp, panel: &str) -> Result held_tool::release(app, ctx, fired), diff --git a/crates/ui-egui/src/shortcuts/tests.rs b/crates/ui-egui/src/shortcuts/tests.rs index 99cf1d8165..294aabbf3c 100644 --- a/crates/ui-egui/src/shortcuts/tests.rs +++ b/crates/ui-egui/src/shortcuts/tests.rs @@ -56,6 +56,14 @@ fn frame(app: &mut VectorcraftApp, events: Vec) { key_frame(app, &ctx, events); } +/// Gives the current document's canvas the keyboard, as a press on it does, and runs the frame that applies it: the +/// keys a tool claims reach the canvas that has the keyboard, and these tests drive the gesture past the canvas. +fn focus_canvas(app: &mut VectorcraftApp, ctx: &egui::Context) { + let win = crate::doc_windows::current(app).expect("a document window"); + craft::ui::runtime::focus::request(ctx, craft::ui::runtime::focus::FocusTarget::Id(crate::canvas::canvas_id(win))); + key_frame(app, ctx, vec![]); +} + /// `n` empty frames, `0.05` s each. fn idle(app: &mut VectorcraftApp, ctx: &egui::Context, n: usize) { for _ in 0..n { @@ -132,6 +140,8 @@ fn dialog_field(app: &VectorcraftApp, key: &str) -> Option { /// OK (`ok`) or Cancel in the window's top dialog, and the app's pump over the answer. pub(super) fn close_top_dialog(app: &mut VectorcraftApp, ok: bool) { let ctx = app.ui_ctx.clone().expect("a window context"); + // A dialog opened after the window's pass shows in its next one, and only a shown dialog takes an answer. + key_frame(app, &ctx, vec![]); let id = crate::tests::picks::open_dialogs(app).pop().expect("a dialog is open").id; let host = app.dialogs().expect("the window's dialog host"); assert!(host.with(|h| if ok { h.confirm_by_id(&ctx, id) } else { h.cancel_by_id(&ctx, id) }), "the dialog stayed open"); @@ -313,7 +323,9 @@ fn the_blend_tool_shortcut_finishes_the_blend() { #[test] fn the_pen_shortcut_ends_the_path_being_drawn() { use vectorcraft_tools::{PointerEvent, PointerKind}; - let mut app = new_app(); + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 600, "height": 300})).unwrap(); + ready(&mut app); app.select_tool("pen"); let click = |app: &mut VectorcraftApp, x: f64| { let v = app.view_info(); @@ -325,11 +337,11 @@ fn the_pen_shortcut_ends_the_path_being_drawn() { path.map(|pd| pd.subpaths.iter().map(|sp| sp.anchors.len()).collect::>()) }; click(&mut app, 100.0); - click(&mut app, 190.0); + click(&mut app, 200.0); let first = app.session.active().unwrap().selection.objects[0]; frame(&mut app, typed(Key::P, Modifiers::NONE, "p")); assert_eq!(app.session.tool_id(), "pen"); - click(&mut app, 150.0); + click(&mut app, 300.0); assert_eq!(anchors(&app, first), Some(vec![2]), "the path keeps its two anchors"); let second = app.session.active().unwrap().selection.objects[0]; assert_ne!(second, first, "the click starts a new path"); @@ -716,7 +728,8 @@ fn cmd_alt_shift_j_opens_average_and_join() { let before = app.session.doc().unwrap().doc.clone(); let j = || tap(Key::J, cmd() | Modifiers::ALT | Modifiers::SHIFT); frame(&mut app, j()); - assert_eq!(dialog_kind(&app).as_deref(), Some("averageJoin")); + // A dialog is keyed by the command its OK runs. + assert_eq!(dialog_kind(&app).as_deref(), Some("path.averageJoin")); close_top_dialog(&mut app, false); assert!(dialog_kind(&app).is_none()); assert_eq!(app.session.doc().unwrap().doc, before, "Cancel changes nothing"); @@ -1051,6 +1064,7 @@ fn arrows_while_dragging_a_grid_change_its_rows_and_columns() { let grid = |app: &mut VectorcraftApp, keys: &[Key]| { app.session.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 80.0), view).unwrap(); app.session.pointer(&PointerEvent::new(PointerKind::Drag, 300.0, 220.0), view).unwrap(); + focus_canvas(app, &ctx); for &key in keys { key_frame(app, &ctx, press(key, Modifiers::NONE)); } @@ -1076,6 +1090,7 @@ fn grid_drag_letters_take_plain_and_shift_presses() { let ctx = ready(&mut app); app.session.pointer(&PointerEvent::new(PointerKind::Down, 100.0, 80.0), view).unwrap(); app.session.pointer(&PointerEvent::new(PointerKind::Drag, 300.0, 220.0), view).unwrap(); + focus_canvas(&mut app, &ctx); let skews = |app: &VectorcraftApp| (app.session.tool_options()["radialSkew"].clone(), app.session.tool_options()["concentricSkew"].clone()); key_frame(&mut app, &ctx, press(Key::V, Modifiers::NONE)); assert_eq!(skews(&app), (json!(10.0), json!(0.0))); @@ -1135,6 +1150,7 @@ fn polygonal_lasso_takes_backspace_delete_enter_and_escape_while_a_loop_is_open( for (x, y) in [(80.0, 80.0), (140.0, 80.0), (300.0, 300.0)] { click(&mut app, x, y); } + focus_canvas(&mut app, &ctx); key_frame(&mut app, &ctx, tap(Key::Backspace, Modifiers::NONE)); assert!(app.session.active().unwrap().doc.node(id).is_some(), "Backspace leaves the selected art"); click(&mut app, 300.0, 300.0); @@ -1147,6 +1163,7 @@ fn polygonal_lasso_takes_backspace_delete_enter_and_escape_while_a_loop_is_open( for (x, y) in [(80.0, 80.0), (140.0, 80.0), (300.0, 300.0)] { click(&mut app, x, y); } + focus_canvas(&mut app, &ctx); key_frame(&mut app, &ctx, tap(Key::Backspace, Modifiers::NONE)); click(&mut app, 80.0, 140.0); key_frame(&mut app, &ctx, tap(Key::Enter, Modifiers::NONE)); @@ -1394,13 +1411,13 @@ fn enter_opens_the_tools_dialog_and_leaves_it_open() { assert_eq!(dialog_kind(&app), None, "nothing is selected"); app.session.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 50, "height": 30})).unwrap(); for (tool, dialog) in [ - ("selection", "move"), - ("directSelection", "move"), - ("groupSelection", "move"), - ("rotate", "rotate"), - ("scale", "scale"), - ("reflect", "reflect"), - ("shear", "shear"), + ("selection", "object.move"), + ("directSelection", "object.move"), + ("groupSelection", "object.move"), + ("rotate", "object.rotate"), + ("scale", "object.scale"), + ("reflect", "object.reflect"), + ("shear", "object.shear"), ] { app.select_tool(tool); let before = steps(&app); @@ -1411,7 +1428,7 @@ fn enter_opens_the_tools_dialog_and_leaves_it_open() { assert_eq!(steps(&app), before, "{tool}: the Enter that opened it didn't confirm it"); // With its default values only Reflect changes the art. key_frame(&mut app, &ctx, enter()); - let after = (before.0 + usize::from(dialog == "reflect"), before.1 + 1); + let after = (before.0 + usize::from(dialog == "object.reflect"), before.1 + 1); assert_eq!((dialog_kind(&app), steps(&app)), (None, after), "{tool}: Enter confirms"); } app.select_tool("rectangle"); @@ -1444,7 +1461,7 @@ fn enter_after_a_drag_repeats_its_offset() { assert!(step > 0.0); key_frame(&mut app, &ctx, vec![]); key_frame(&mut app, &ctx, enter()); - assert_eq!(dialog_kind(&app).as_deref(), Some("move"), "Enter opens Move"); + assert_eq!(dialog_kind(&app).as_deref(), Some("object.move"), "Enter opens Move"); assert_eq!(dialog_field(&app, "dx").and_then(|v| v.as_f64()).map(f64::round), Some(step)); set_top_field(&mut app, "copy", json!(true)); key_frame(&mut app, &ctx, enter()); diff --git a/crates/ui-egui/src/toolbar/tests.rs b/crates/ui-egui/src/toolbar/tests.rs index 8e8f2bd58d..0aa82f8e31 100644 --- a/crates/ui-egui/src/toolbar/tests.rs +++ b/crates/ui-egui/src/toolbar/tests.rs @@ -99,13 +99,18 @@ fn set_field(app: &VectorcraftApp, key: &str, value: serde_json::Value) { app.dialogs().expect("a window").with(|host| host.set_json_field(&ctx, id, key, value)).unwrap_or_else(|e| panic!("{key}: {e}")); } -/// Answers the open tool-options dialog with OK or Cancel; the frame's pump takes the answer. +/// Answers the open tool-options dialog with OK or Cancel; the frame's pump takes the answer. A held OK files the +/// answer for the pump and the dialog closes once the pump accepted it. fn answer(app: &mut VectorcraftApp, ok: bool) { let (id, _) = tool_dialog(app).expect("the dialog is open"); let ctx = app.ui_ctx.clone().expect("a window"); - let taken = app.dialogs().expect("a window").with(|host| if ok { host.confirm_by_id(&ctx, id) } else { host.cancel_by_id(&ctx, id) }); - assert!(taken, "the dialog took the answer"); - crate::dialogs::host::pump(app, &ctx); + if ok { + crate::tests::picks::answer_dialog(app, id, &[], true); + } else { + app.dialogs().expect("a window").with(|host| host.cancel_by_id(&ctx, id)); + crate::dialogs::host::pump(app, &ctx); + } + assert!(tool_dialog(app).is_none_or(|(open, _)| open != id), "the dialog took the answer"); } /// A double-click on a tool's button opens what `open_options` opens: the artboard's, a graph's and the tools' dialogs