diff --git a/crates/ui-egui/src/menus/model.rs b/crates/ui-egui/src/menus/model.rs index f812f1c784..d37cde732b 100644 --- a/crates/ui-egui/src/menus/model.rs +++ b/crates/ui-egui/src/menus/model.rs @@ -365,13 +365,13 @@ fn row_state(app: &VectorcraftApp, command: &CommandRef, label: &str) -> Command state } -/// What the dimmed row `command`, named `label`, says: the engine's reason, the panels' in Presentation Mode, else that -/// the command is not available right now. +/// What the dimmed row `command`, named `label`, says in the interface language: the engine's reason, the panels' in +/// Presentation Mode, else that the command is not available right now. pub(crate) fn off_reason(app: &VectorcraftApp, command: &CommandRef, label: &str) -> String { why_off(app, crate::keymap::state_id(command)) .flatten() .or_else(|| crate::palette::refusal(app, command).filter(|_| command.id == "window.panel")) - .unwrap_or_else(|| crate::i18n::msg(&format!("command `{label}` is not available right now"))) + .map_or_else(|| crate::i18n::msg(&format!("command `{label}` is not available right now")), |why| crate::i18n::msg(&why)) } /// The title a row shows in `lang`: the Settings pages read `Page…`, the other rows their label as the menus draw it. diff --git a/crates/ui-egui/src/menus/state.rs b/crates/ui-egui/src/menus/state.rs index d13767f9f1..382630ccbb 100644 --- a/crates/ui-egui/src/menus/state.rs +++ b/crates/ui-egui/src/menus/state.rs @@ -235,8 +235,9 @@ pub fn enabled(app: &VectorcraftApp, id: &str) -> bool { } /// Why command `id` can't run now ([`enabled`]): `None` while it can, else `Some` with the -/// engine's reason when the engine's check of an engine command fails. The engine's check runs at -/// most once. +/// engine's reason when the engine's check of an engine command fails, and with the reason of +/// [`crate::panels::align::why_off`] for an Align panel button. The engine's check runs at most +/// once. pub(crate) fn why_off(app: &VectorcraftApp, id: &str) -> Option> { if waits_for_ime(app, id) { return Some(None); @@ -250,6 +251,9 @@ pub(crate) fn why_off(app: &VectorcraftApp, id: &str) -> Option> { return (!facts.any_empty).then_some(None); } + if let Some(b) = crate::panels::align::button(id) { + return crate::panels::align::why_off(app, &b); + } if let Some(c) = vectorcraft_engine::find_command(id) { return match (c.enabled)(&app.session) { Ok(()) => None, @@ -279,7 +283,6 @@ fn ui_enabled(app: &VectorcraftApp, id: &str) -> bool { | "type.highlightFontSize" => crate::panels::character::text_style(app).is_some(), id if crate::panels::paragraph::ALIGN_COMMANDS.iter().any(|c| c.0 == id) => crate::panels::character::text_style(app).is_some(), "type.applyBullets" | "type.applyNumbers" | "type.convertListToText" => crate::panels::character::text_style(app).is_some(), - id if let Some(b) = crate::panels::align::button(id) => crate::panels::align::button_enabled(app, &b) && enabled(app, b.command_now(app)), id if let Some(v) = crate::panels::align::align_to_command(id) => crate::panels::align::can_choose(app, v), "file.reveal" => app.services.reveal.is_some() && app.session.active().is_some_and(|d| d.path.is_some()), "file.cancelExport" | "file.exportInBackground" => app.background.running(crate::background::Kind::Export), diff --git a/crates/ui-egui/src/panels/align.rs b/crates/ui-egui/src/panels/align.rs index 8cc25394cb..5a630eaa13 100644 --- a/crates/ui-egui/src/panels/align.rs +++ b/crates/ui-egui/src/panels/align.rs @@ -327,6 +327,28 @@ pub(crate) fn button_enabled(app: &VectorcraftApp, b: &Button) -> bool { } } +/// Why the Align or Distribute button `b` can't run now, as [`crate::menus::why_off`] answers for +/// its command: `None` while it can, else `Some` with the reason when one is known. While +/// [`button_enabled`] dims it, the reason is how many artboards it needs with the Artboard tool +/// chosen, else the engine's for the object command it runs, whose check `button_enabled` repeats; +/// otherwise the engine's for the command a click runs. +pub(crate) fn why_off(app: &VectorcraftApp, b: &Button) -> Option> { + if button_enabled(app, b) { + return crate::menus::why_off(app, b.command_now(app)); + } + if !on_artboards(app) || app.session.active().is_none() { + return Some(vectorcraft_engine::find_command(b.command()).and_then(|c| (c.enabled)(&app.session).err())); + } + Some(Some( + match b { + Button::Align(..) | Button::AlignCenters => "select an artboard", + Button::Distribute(..) | Button::Spacing(_) if app.ui.align_to == AlignTo::Artboard => "select at least two artboards", + Button::Distribute(..) | Button::Spacing(_) => "select at least three artboards", + } + .to_string(), + )) +} + /// The Control bar's Align content: the align buttons, the Align To menu button showing the choice /// of [`shown_align_to`], and the Distribute buttons while they can act (the artboards the Artboard /// tool has selected, else two or more objects). The bar's row decides what leaves when it is diff --git a/crates/ui-egui/src/panels/align/tests.rs b/crates/ui-egui/src/panels/align/tests.rs index db93c710c2..01320f7cf8 100644 --- a/crates/ui-egui/src/panels/align/tests.rs +++ b/crates/ui-egui/src/panels/align/tests.rs @@ -1,5 +1,7 @@ use super::*; +use crate::i18n::LangExt as _; use crate::tests::panel_harness::Panel; +use craft::keymap::CommandRef; #[test] fn single_object_aligns_to_artboard() { @@ -788,3 +790,56 @@ fn a_button_runs_the_artboard_command_only_with_the_artboard_tool() { run_button(&mut app, Button::Align("horizontal", "left")).ok(); assert_eq!(artboard_corners(&app), before, "with the Selection tool the buttons leave the artboards alone"); } + +/// The description of the dimmed command of each button in [`COMMANDS`], which its tooltip and its +/// menu item show. +fn off_reasons(app: &VectorcraftApp, distribute: bool) -> Vec<(&'static str, Option)> { + COMMANDS + .iter() + .filter(|(_, b)| matches!(b, Button::Distribute(..) | Button::Spacing(_)) == distribute) + .map(|(id, _)| { + let command = CommandRef::new(*id); + (*id, (!crate::menus::enabled(app, id)).then(|| crate::menus::model::off_reason(app, &command, "Label"))) + }) + .collect() +} + +/// Every reason in `reasons` is `why`. +fn all_say(reasons: &[(&'static str, Option)], why: &str) { + for (id, reason) in reasons { + assert_eq!(reason.as_deref(), Some(why), "{id}"); + } +} + +/// A dimmed Align or Distribute button says why it can't run, as an engine command's menu item +/// does, in the interface language: with no document, that none is open; with nothing selected, +/// that Align needs a selection; with fewer than two objects, that Distribute needs two or more; +/// with the Artboard tool chosen and too few artboards selected, how many it needs. +#[test] +fn a_dimmed_align_or_distribute_button_says_why() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + all_say(&off_reasons(&app, true), "no document open"); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + all_say(&off_reasons(&app, false), "nothing selected"); + all_say(&off_reasons(&app, true), "select at least two objects"); + let a = app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap()["id"].clone(); + app.run("select.set", json!({"ids": [a]})).unwrap(); + assert!(off_reasons(&app, false).iter().all(|(_, r)| r.is_none()), "one object aligns"); + all_say(&off_reasons(&app, true), "select at least two objects"); + // The reason is shown in the interface language. + crate::i18n::set_current(crate::i18n::Lang::from_code("de").unwrap()); + let german = crate::i18n::msg("select at least two objects"); + let reasons = off_reasons(&app, true); + crate::i18n::set_current(crate::i18n::Lang::EN); + assert_ne!(german, "select at least two objects"); + all_say(&reasons, &german); + + let mut app = selected_artboards(); + app.session.set_tool_option("selected", &json!([1, 0])); + assert!(off_reasons(&app, false).iter().all(|(_, r)| r.is_none()), "two artboards align"); + all_say(&off_reasons(&app, true), "select at least three artboards"); + app.ui.align_to = AlignTo::Artboard; + assert!(off_reasons(&app, true).iter().all(|(_, r)| r.is_none()), "two artboards distribute to the artboard"); + app.session.set_tool_option("selected", &json!([1])); + all_say(&off_reasons(&app, true), "select at least two artboards"); +}