Keep Analysis tool choices available without an open document

This commit is contained in:
baumar73 committed 2026-11-28 19:03:18 +01:00
1 parent f7d76f5be4
commit d79dace82b
3 files changed
+53 -6

No files matched your search

+2 -2
View File
@@ -42,8 +42,8 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool {
if let Some(e) = crate::workspace_ui::is_enabled(app, id) {
return e;
}
if crate::analysis::handles(id) {
return true;
if let Some(enabled) = crate::analysis::enabled(app, id) {
return enabled;
}
if let Some(e) = crate::view_cmds::is_enabled(app, id) {
return e;
@@ -0,0 +1,33 @@
use crate::state::Tool;
use crate::{PhotocraftApp, Services};
use serde_json::json;
const DATA_POINTS: &str = "image.analysis.selectDataPoints";
const RULER: &str = "image.analysis.rulerTool";
const COUNT: &str = "image.analysis.countTool";
const SCALE: &str = "image.analysis.setMeasurementScale";
const RECORD: &str = "image.analysis.recordMeasurements";
const MARKER: &str = "image.analysis.placeScaleMarker";
#[test]
fn analysis_menu_can_select_tools_without_a_document() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default());
let ctx = app.ctx().clone();
for (id, tool) in [(RULER, Tool::Ruler), (COUNT, Tool::Count)] {
assert!(crate::menus::is_enabled(&app, id), "{id} can select a tool before opening a document");
crate::menus::invoke(&mut app, &ctx, id, json!({})).unwrap();
assert_eq!(app.ui.tool, tool);
assert!(!app.session.is_enabled(id), "the headless command still requires a document");
}
for id in [DATA_POINTS, SCALE, RECORD, MARKER] {
assert!(!crate::menus::is_enabled(&app, id), "{id} is disabled without a document");
}
assert!(crate::menus::invoke(&mut app, &ctx, DATA_POINTS, json!({})).is_err());
assert!(app.session.is_enabled(DATA_POINTS), "headless session measurement settings remain available");
app.run("file.new", json!({"width": 16, "height": 16})).unwrap();
for id in [RULER, COUNT, DATA_POINTS, SCALE, RECORD, MARKER] {
assert!(crate::menus::is_enabled(&app, id), "{id} is enabled with a document");
}
}
+18 -4
View File
@@ -22,6 +22,8 @@ pub(crate) mod log_panel;
pub(crate) mod note_alerts;
pub(crate) mod notes_panel;
#[cfg(test)]
mod menu_enabled_no_doc_tests;
#[cfg(test)]
mod tests;
@@ -106,9 +108,15 @@ impl Default for AnalysisUi {
const PANELS: [&str; 2] = ["window.panel.measurementLog", "window.panel.notes"];
/// Menu ids handled here (live menu items).
pub fn handles(id: &str) -> bool {
PANELS.contains(&id)
/// Menu state for the Analysis front ends that act differently from their headless commands.
pub(crate) fn enabled(app: &PhotocraftApp, id: &str) -> Option<bool> {
match id {
// These menu items choose a tool; their engine counterparts read or edit a document.
"image.analysis.rulerTool" | "image.analysis.countTool" => Some(true),
"image.analysis.selectDataPoints" => Some(app.session.active().is_some()),
_ if PANELS.contains(&id) => Some(true),
_ => None,
}
}
/// The check mark of a menu item of this module.
@@ -159,7 +167,13 @@ pub fn menu(app: &mut PhotocraftApp, id: &str, params: &Value) -> Option<Result<
app.session.active()?;
dialogs::open(app, dialogs::Kind::Scale).map(|_| json!({"dialog": "measurementScale"}))
}
"image.analysis.selectDataPoints" => dialogs::open(app, dialogs::Kind::DataPoints).map(|_| json!({"dialog": "dataPoints"})),
"image.analysis.selectDataPoints" => {
if app.session.active().is_none() {
Err(tl!("no document open").to_string())
} else {
dialogs::open(app, dialogs::Kind::DataPoints).map(|_| json!({"dialog": "dataPoints"}))
}
}
"image.analysis.placeScaleMarker" => {
app.session.active()?;
dialogs::open(app, dialogs::Kind::ScaleMarker).map(|_| json!({"dialog": "scaleMarker"}))