From e39dfb94552f2d697333a538dc42508cd4bb3083 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 07:24:26 +0000 Subject: [PATCH] Appearance and Language menu picks change the theme and the language A theme or language row is a fired command (app.theme, app.language) with the choice in its parameters. The drain's dispatch refused both ids (no enable rule, so the engine's rule answered: not available) and passed invoke empty parameters. Both rows are enabled with or without a document, and dispatch passes the fired command's parameters to invoke. Trials run the whole app with and without a document: a theme fired as the macOS menu fires it, a theme clicked in Window > Appearance, and a language row. --- crates/ui-egui/src/input/shortcut_dispatch.rs | 16 ++- crates/ui-egui/src/menus.rs | 3 + .../src/menus/appearance_menu_trials.rs | 118 ++++++++++++++++++ crates/ui-egui/src/menus/state.rs | 4 + 4 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 crates/ui-egui/src/menus/appearance_menu_trials.rs diff --git a/crates/ui-egui/src/input/shortcut_dispatch.rs b/crates/ui-egui/src/input/shortcut_dispatch.rs index f0af34bbdf9..1d0c5189b71 100644 --- a/crates/ui-egui/src/input/shortcut_dispatch.rs +++ b/crates/ui-egui/src/input/shortcut_dispatch.rs @@ -709,17 +709,20 @@ fn run_pressed(app: &mut PhotocraftApp, ctx: &egui::Context, f: &Fired) { // The Delete key is Edit › Clear, or deletes the layers when nothing is selected (`delete_key_command`). let id = if id == EditCommands::Delete.id() { "edit.clear" } else { id }; let id = clip_key_command(app, delete_key_command(app, id.to_string())); - dispatch_as(app, ctx, &id, f.alt_variant); + // A key that runs another command (Delete, the clip keys) runs it without the fired command's parameters. + let command = if id == f.command.id { f.command.clone() } else { CommandRef::new(id) }; + dispatch_as(app, ctx, &command, f.alt_variant); } /// Run a shortcut's command like its menu item (dialogs included), or say why it can't run. pub fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str) { - dispatch_as(app, ctx, id, false); + dispatch_as(app, ctx, &CommandRef::new(id), false); } -/// [`dispatch`]; with `last`, an adjustment dialog opens at its last confirmed settings and New Layer makes the -/// layer without its dialog. -fn dispatch_as(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, last: bool) { +/// [`dispatch`] of a fired command, its parameters included (an Appearance or Language row's choice); with `last`, an +/// adjustment dialog opens at its last confirmed settings and New Layer makes the layer without its dialog. +fn dispatch_as(app: &mut PhotocraftApp, ctx: &egui::Context, command: &CommandRef, last: bool) { + let id = command.id.as_str(); let action = id.strip_prefix(crate::actions::SHORTCUT_PREFIX); let command_id = if action.is_some() { "actions.play" } else { id }; // A key saved for an action that was since deleted or renamed names no action. @@ -748,7 +751,8 @@ fn dispatch_as(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, last: boo let opened = if last { crate::adjust_dialog::open_last(app, id) } else { crate::adjust_dialog::open(app, id) }; opened.map(|_| serde_json::Value::Null) } else { - crate::menus::invoke(app, ctx, id, json!({})) + let params = if command.params.is_null() { json!({}) } else { command.params.clone() }; + crate::menus::invoke(app, ctx, id, params) }; match r { Ok(_) => Outcome::Ran, diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 4df91e6dc1e..2b3ccdd3f1a 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -104,6 +104,9 @@ pub(crate) const DOCUMENT_ITEM: &str = "window.document."; #[cfg(test)] mod tests; +#[cfg(test)] +mod appearance_menu_trials; + #[cfg(test)] mod open_recent_position_tests; diff --git a/crates/ui-egui/src/menus/appearance_menu_trials.rs b/crates/ui-egui/src/menus/appearance_menu_trials.rs new file mode 100644 index 00000000000..bdff4c39005 --- /dev/null +++ b/crates/ui-egui/src/menus/appearance_menu_trials.rs @@ -0,0 +1,118 @@ +//! The Appearance and Language menus in the whole app: a pick changes the theme the framework draws with and the +//! language, whether it comes from a click on the in-window bar or from the macOS menu, which fires the row's command. +//! Both menus work with or without a document open. + +use craft::keymap::CommandRef; +use craft::menu::{LANGUAGE_COMMAND, ThemeChoice}; +use craft::ui::runtime::commands::{FireSource, fire}; +use egui::accesskit::Role; +use egui_kittest::Harness; +use egui_kittest::kittest::{AccessKitNode, Queryable}; + +use crate::PhotocraftApp; +use crate::theme::AppTheme; + +/// The whole app, empty or with one document open. +fn harness(document: bool) -> Harness<'static, Box> { + let mut session = photocraft_engine::Session::new(); + if document { + session.execute("file.new", serde_json::json!({"width": 64, "height": 48})).unwrap(); + } + crate::shortcut_dispatch::tests::app_harness(PhotocraftApp::new(session, crate::Services::default())) +} + +/// The theme the framework's root takes each frame (`frame_input`) and the panel fill the theme installs. +fn shown(h: &Harness<'_, Box>) -> (String, bool, egui::Color32) { + let theme = &h.state().ui.theme; + (theme.id().as_str().to_string(), theme.is_dark(), h.ctx.global_style().visuals.panel_fill) +} + +/// Classic in light mode: a light theme of its own row, unlike the dark theme the app starts with. +fn classic() -> (AppTheme, ThemeChoice) { + let theme = AppTheme::classic(); + let choice = ThemeChoice::theme(theme.id().clone(), false); + (theme, choice) +} + +/// Runs the frames that take a fired command and show its result. +fn settle(h: &mut Harness<'_, Box>) { + h.run_steps(6); +} + +/// The centre of the top-most menu title or row labelled `label`. +fn top(h: &Harness<'_, Box>, label: &str) -> egui::Pos2 { + let named = |node: &AccessKitNode<'_>| { + node.label().as_deref() == Some(label) && matches!(node.role(), Role::MenuItem | Role::MenuItemRadio | Role::MenuItemCheckBox) + }; + let node = h.get_all_by(named).min_by(|a, b| a.rect().min.y.total_cmp(&b.rect().min.y)).unwrap_or_else(|| panic!("no menu row {label}")); + node.rect().center() +} + +/// Moves the pointer over the top-most node labelled `label`. +fn hover_top(h: &mut Harness<'_, Box>, label: &str) { + let at = top(h, label); + h.event(egui::Event::PointerMoved(at)); + settle(h); +} + +/// Clicks the top-most node labelled `label` with the pointer, a frame for the press and one for the release. +fn click_top(h: &mut Harness<'_, Box>, label: &str) { + let at = top(h, label); + h.event(egui::Event::PointerMoved(at)); + h.step(); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }); + h.step(); + } + settle(h); +} + +/// Asserts the app shows Classic light after a pick, with the panel fill it installs. +fn assert_classic(h: &Harness<'_, Box>, before: &(String, bool, egui::Color32), document: bool) { + let (theme, _) = classic(); + let after = shown(h); + assert_eq!((after.0.as_str(), after.1), (theme.id().as_str(), false), "document {document}; status: {}", h.state().ui.status); + assert_ne!(after.2, before.2, "document {document}: the panels take the theme's fill"); + assert_eq!(after.2, theme.tokens().chrome, "document {document}"); +} + +#[test] +fn a_theme_fired_from_the_menu_shows_it() { + for document in [true, false] { + let mut h = harness(document); + let (theme, choice) = classic(); + let before = shown(&h); + assert_ne!(before.0, theme.id().as_str(), "the app starts on another theme"); + assert!(fire(&h.ctx.clone(), choice.command(), FireSource::Menu), "the runtime takes the menu's theme row"); + settle(&mut h); + assert_classic(&h, &before, document); + } +} + +#[test] +fn a_theme_clicked_in_the_window_menu_shows_it() { + for document in [true, false] { + let mut h = harness(document); + let (theme, _) = classic(); + let before = shown(&h); + click_top(&mut h, "Window"); + hover_top(&mut h, "Appearance"); + let name = h.state().framework.theme_name(theme.id(), false); + click_top(&mut h, &name); + assert_classic(&h, &before, document); + } +} + +#[test] +fn a_language_fired_from_the_menu_is_saved() { + // A language other than English where the build has the catalogs; English (from Automatic) where it has only that. + let code = crate::i18n::Lang::all().map(crate::i18n::Lang::code).find(|code| *code != "en").unwrap_or("en"); + for document in [true, false] { + let mut h = harness(document); + assert_eq!(h.state().session.prefs().interface.language, "auto"); + let row = CommandRef::with_params(LANGUAGE_COMMAND, serde_json::json!({"lang": code})); + assert!(fire(&h.ctx.clone(), row, FireSource::Menu), "the runtime takes the menu's language row"); + settle(&mut h); + assert_eq!(h.state().session.prefs().interface.language, code, "document {document}; status: {}", h.state().ui.status); + } +} diff --git a/crates/ui-egui/src/menus/state.rs b/crates/ui-egui/src/menus/state.rs index b3910381658..7831ae93827 100644 --- a/crates/ui-egui/src/menus/state.rs +++ b/crates/ui-egui/src/menus/state.rs @@ -1,3 +1,5 @@ +use craft::menu::{LANGUAGE_COMMAND, THEME_COMMAND}; + use super::invoke::{panel_alias, proof_preset, workspace_id}; use super::{DOCUMENT_ITEM, PhotocraftApp, Value}; @@ -72,6 +74,8 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool { i if crate::links::url_for(i).is_some() => true, // Interface brightness works with or without a document, as in Photoshop. "view.darkenInterface" | "view.lightenInterface" => true, + // Appearance and Language rows: the theme and the language change with or without a document. + THEME_COMMAND | LANGUAGE_COMMAND => true, "file.save" | "file.saveAs" | "file.export.exportAs" | "file.export.quickExportAsPng" => { app.session.active().is_some() && app.services.export.is_some() }