diff --git a/crates/engine/src/cmd/graph.rs b/crates/engine/src/cmd/graph.rs index 9de04ef106..3f8c0a41bc 100644 --- a/crates/engine/src/cmd/graph.rs +++ b/crates/engine/src/cmd/graph.rs @@ -42,7 +42,7 @@ pub(crate) use data::{apply_csv, graph_csv_from_file, parse_csv}; #[cfg(test)] use data::{fmt_value, to_csv}; pub(crate) use designs::{holds_designs, redraw_detached, redraw_symbols_with_designs, redraw_with_designs, refresh_graphs, series_drawn_designs}; -pub use edit::{GraphTypeSubject, graph_type_subject}; +pub use edit::{GraphTypeSubject, graph_type_subject, has_graph_selection}; pub(crate) use edit::{edit_graph_art, graphs_holding, keep_placed}; pub use generate::calculated_axes; #[cfg(test)] diff --git a/crates/engine/src/cmd/graph/edit.rs b/crates/engine/src/cmd/graph/edit.rs index e5ce1e7ed0..c0278748f3 100644 --- a/crates/engine/src/cmd/graph/edit.rs +++ b/crates/engine/src/cmd/graph/edit.rs @@ -200,6 +200,11 @@ pub(super) fn target(s: &Session, p: &Value, cmd: &str) -> Result { Err(bad(cmd, "select a graph")) } +/// Whether a graph command can find a graph from the current selection. +pub fn has_graph_selection(s: &Session) -> bool { + target(s, &Value::Null, "graph.setData").is_ok() +} + pub(super) fn spec_of(s: &Session, id: NodeId) -> Result { s.doc()?.doc.node(id).and_then(|n| n.extras().graph.as_deref().cloned()).ok_or(EngineError::NoNode(id)) } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 80c2ea3d1a..e48cc79425 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -36,7 +36,7 @@ pub use cmd::create::constrain_click; pub use cmd::distortcmds::{perspective_click, width_point_pair}; pub use cmd::flare::flare_settings; pub use cmd::gradient::{GradientEditPaints, StopOwner}; -pub use cmd::graph::{GraphTypeSubject, calculated_axes as graph_calculated_axes, graph_type_subject}; +pub use cmd::graph::{GraphTypeSubject, calculated_axes as graph_calculated_axes, graph_type_subject, has_graph_selection}; pub use cmd::params::{check_keys, check_params}; pub use cmd::pathops::{HAIRLINE_SLIVER_WIDTH, HAIRLINE_SLIVER_WIDTH_RANGE, auto_simplify_level, can_outline_stroke}; pub use cmd::rasterfx::{export_pdf, flatten_raster_effects}; diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv index 7631ba753a..37254dc84d 100644 --- a/crates/ui-egui/src/i18n/zh-hans.tsv +++ b/crates/ui-egui/src/i18n/zh-hans.tsv @@ -325,6 +325,7 @@ antiAlias Sharp 锐利 Brush Options… 画笔选项… Options of Selected Object… 所选对象的选项… Options of Selected Object 所选对象的选项 + Check for Updates… 检查更新… @msg no path painted with a brush is selected 未选择用画笔绘制的路径 @msg the selected paths are painted with brushes of different types 所选路径使用了不同类型的画笔 @msg give `params` or `reset`, not both 请提供 `params` 或 `reset`,不能同时提供 diff --git a/crates/ui-egui/src/menus/state.rs b/crates/ui-egui/src/menus/state.rs index da9084e89c..b3c6139c43 100644 --- a/crates/ui-egui/src/menus/state.rs +++ b/crates/ui-egui/src/menus/state.rs @@ -300,6 +300,9 @@ pub(crate) fn why_off(app: &VectorcraftApp, id: &str) -> Option> } if let Some(c) = vectorcraft_engine::find_command(id) { return match (c.enabled)(&app.session) { + Ok(()) if matches!(id, "graph.setData" | "graph.setType" | "graph.column" | "graph.marker") => { + (!vectorcraft_engine::has_graph_selection(&app.session)).then(|| Some("select a graph".into())) + } Ok(()) => None, // The system clipboard's contents can be pasted into an open document with an empty // internal clipboard. diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index 39909b57eb..05e556b9a9 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -1231,6 +1231,30 @@ fn app_with_a_rectangle() -> VectorcraftApp { app } +#[test] +fn graph_menu_requires_a_live_graph_and_explicit_ids_still_work() { + let mut app = app_with_a_rectangle(); + let rectangle = app.session.active().unwrap().selection.objects[0]; + let commands = ["graph.setData", "graph.setType", "graph.column", "graph.marker"]; + assert!(commands.iter().all(|id| !enabled(&app, id))); + assert!(enabled(&app, "graph.design")); + + let graph = app.run("graph.create", json!({"type": "column", "x": 100, "y": 100, "width": 200, "height": 120})).unwrap(); + let graph = vectorcraft_doc::NodeId(graph["id"].as_u64().unwrap()); + let state = app.session.active().unwrap(); + let member = state.doc.node(graph).unwrap().children().unwrap()[0].id; + assert!(commands.iter().all(|id| enabled(&app, id))); + app.run("select.set", json!({"ids": [member.0]})).unwrap(); + assert!(commands.iter().all(|id| enabled(&app, id))); + app.run("select.none", json!({})).unwrap(); + assert!(commands.iter().all(|id| !enabled(&app, id))); + + app.run("select.set", json!({"ids": [rectangle.0]})).unwrap(); + let data = app.session.execute("graph.setData", &json!({"id": graph.0})).unwrap(); + assert!(data["csv"].is_string()); + assert_eq!(app.session.active().unwrap().selection.objects, [rectangle]); +} + /// The in-window Edit menu draws Undo and Redo with the step's name, as the History panel lists it. #[test] fn the_edit_menu_draws_undo_and_redo_with_the_step_name() {