Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Control: ui.menu.invoke respects the modal menu policy (#1671)

Nigel Ihza Firdausy8bc986122407
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index d8dca9dbe..c422d4b3e 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -239,6 +239,11 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
}
"engine.execute" | "ui.menu.invoke" => {
let Some(id) = s("command").or(s("id")) else { return err("missing `command`") };
+ // A control-channel menu click must respect the same modal gate as the native menu.
+ // engine.execute is deliberately not subject to the UI's menu-click semantics.
+ if req.method == "ui.menu.invoke" && !crate::menus::modal_allows(app, id) {
+ return err("menu command is unavailable while a modal dialog is open");
+ }
let params = p.get("params").cloned().unwrap_or(json!({}));
if let Some(authorize) = app.services.automation_command.as_ref()
&& let Err(error) = authorize(id, &params)
@@ -955,6 +960,38 @@ mod tests {
assert_eq!(app.session.active().unwrap().revision, rev, "opening a dialog must not edit the document");
}
+ #[test]
+ fn menu_invoke_cannot_edit_document_behind_an_open_dialog() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
+
+ let opened = call(&mut app, &ctx, "ui.menu.invoke", json!({"id": "image.imageSize"}));
+ assert_eq!(opened["ok"], true, "{opened}");
+ let dialog = opened["result"]["dialog"].as_u64().unwrap();
+ let revision = app.session.active().unwrap().revision;
+ let before = call(&mut app, &ctx, "ui.inspect", json!({}));
+ let fields = before["result"]["dialogs"][0]["fields"].clone();
+
+ for params in [json!({"id": "image.imageRotation.90cw"}), json!({"id": "image.imageRotation.90cw", "params": {}})] {
+ let blocked = call(&mut app, &ctx, "ui.menu.invoke", params);
+ assert_eq!(blocked["ok"], false, "{blocked}");
+ assert_eq!(app.session.active().unwrap().revision, revision);
+ let snapshot = call(&mut app, &ctx, "ui.inspect", json!({}));
+ assert_eq!(snapshot["result"]["dialogs"][0]["id"], dialog);
+ assert_eq!(snapshot["result"]["dialogs"][0]["fields"], fields);
+ }
+
+ // View navigation stays permitted by the same policy as native menus and shortcuts.
+ assert!(crate::menus::modal_allows(&app, "view.zoomIn"));
+ assert!(!crate::menus::modal_allows(&app, "image.imageRotation.90cw"));
+
+ assert_eq!(call(&mut app, &ctx, "ui.dialog.cancel", json!({"dialog": dialog}))["ok"], true);
+ let rotated = call(&mut app, &ctx, "ui.menu.invoke", json!({"id": "image.imageRotation.90cw"}));
+ assert_eq!(rotated["ok"], true, "{rotated}");
+ assert!(app.session.active().unwrap().revision > revision);
+ }
+
#[test]
fn ui_set_rejects_unknown_fields_before_changing_anything() {
use crate::theme::ThemeKind;
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 7bce021e2..a45aa7a74 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -5,6 +5,16 @@ use serde_json::{Value, json};
use crate::PhotocraftApp;
use crate::state::DialogKind;
+/// Shared gate for native menu clicks and control-channel `ui.menu.invoke`.
+/// Dialogs and unsaved-changes prompts block edits, but allow the four view-navigation
+/// commands also usable through shortcuts. Camera Raw blocks menu commands entirely.
+pub(crate) fn modal_allows(app: &PhotocraftApp, id: &str) -> bool {
+ if app.camera_raw.is_some() {
+ return false;
+ }
+ (app.ui.dialogs.is_empty() && app.discard.is_none()) || crate::shortcuts::NAV_COMMANDS.contains(&id)
+}
+
/// Top-level menus in Photoshop order.
pub const TOP_MENUS: [&str; 10] = ["File", "Edit", "Image", "Layer", "Type", "Select", "Filter", "View", "Window", "Help"];
diff --git a/crates/ui-egui/src/native_menu.rs b/crates/ui-egui/src/native_menu.rs
index e828ea30f..bdda57383 100644
--- a/crates/ui-egui/src/native_menu.rs
+++ b/crates/ui-egui/src/native_menu.rs
@@ -647,17 +647,6 @@ impl NativeMenu {
}
}
-/// Dialogs, the unsaved-changes prompt and Camera Raw are modal, as they are for shortcuts
-/// ([`crate::shortcuts::handle`]): while one is open, only the view navigation Photoshop keeps
-/// live under a dialog may run from the menu bar. A rotation under Image Size used to leave the
-/// dialog's stale size to be applied on OK (#1354).
-fn modal_allows(app: &PhotocraftApp, id: &str) -> bool {
- if app.camera_raw.is_some() {
- return false;
- }
- (app.ui.dialogs.is_empty() && app.discard.is_none()) || crate::shortcuts::NAV_COMMANDS.contains(&id)
-}
-
/// Run the native menu clicks that arrived since the last frame, like in-window menu clicks.
pub fn run(app: &mut PhotocraftApp, ctx: &egui::Context) {
let Some(menu) = app.services.native_menu.as_mut() else { return };
@@ -668,7 +657,7 @@ pub fn run(app: &mut PhotocraftApp, ctx: &egui::Context) {
continue;
}
// The rows are disabled while a dialog is open; a click that raced the update is dropped.
- if !modal_allows(app, &id) {
+ if !crate::menus::modal_allows(app, &id) {
continue;
}
if let Err(e) = crate::menus::invoke(app, ctx, &id, serde_json::json!({})) {
@@ -691,7 +680,7 @@ pub fn sync(app: &mut PhotocraftApp, ctx: &egui::Context) {
let lang = crate::i18n::current();
let mut items = crate::menus::menu_items(app);
for it in &mut items {
- it.enabled &= modal_allows(app, &it.id);
+ it.enabled &= crate::menus::modal_allows(app, &it.id);
}
let layout = photocraft_layout(&items, lang, &app.session.prefs().interface.language);
let Some(menu) = app.services.native_menu.as_mut() else { return };