FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#591: named clips that cannot stand in for a selection say why
vangel761e3d4a8a8cc4
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 0b3a9f8a6..bc07eef07 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -140,6 +140,29 @@ pub(crate) fn named_clips(s: &Session, spec: &CommandSpec, p: &Value) -> Option<let clips: Vec<ClipId> = named_ids(spec, p, "clips", "clip")?.into_iter().map(ClipId).filter(|c| seq.find_item(*c).is_some()).collect();(!clips.is_empty()).then_some(clips)}+/// Why the clips a caller named in `clips` / `clip` stand in for no selection (#591): a clip of+/// another sequence, an id of no clip (a track, a project item…) or a value that is not an id.+/// `None` when the caller named nothing or no sequence is open, so the selection's reason stands.+pub(crate) fn named_clips_missing(s: &Session, spec: &CommandSpec, p: &Value) -> Option<String> {+ s.active_sequence()?;+ let first = if spec.params.contains("\"clips\":[id]")+ && let Some(a) = p.get("clips").and_then(Value::as_array)+ {+ a.first()?+ } else if spec.params.contains("\"clip\":id") {+ p.get("clip").filter(|v| !v.is_null())?+ } else {+ return None;+ };+ let Some(id) = first.as_u64() else {+ return Some(format!("{first} is not a clip id (clip ids are the `clip` numbers of the items in sequence.inspect)"));+ };+ let elsewhere = s.project.sequences().find(|i| i.as_sequence().is_some_and(|q| q.find_item(ClipId(id)).is_some()));+ Some(match elsewhere {+ Some(i) => format!("clip {id} is in sequence \"{}\", not the active one: open it first with sequence.open {{\"item\":{}}}", i.name, i.id.0),+ None => format!("no clip {id} in the active sequence (clip ids are the `clip` numbers of the items in sequence.inspect)"),+ })+}/// Project items named explicitly in `items` / `item`, likewise. The Project panel's selection/// holds bins beside items (one id space, see `project.select`), so a bin named here counts too;/// the project's own top bin does not, since no command acts on it (#244).diff --git a/crates/engine/src/explicit_targets_tests.rs b/crates/engine/src/explicit_targets_tests.rsindex ce377f64d..c3df07e18 100644--- a/crates/engine/src/explicit_targets_tests.rs+++ b/crates/engine/src/explicit_targets_tests.rs@@ -119,10 +119,15 @@ fn clip_commands_take_explicit_clips_without_a_selection() {fn naming_nothing_usable_does_not_enable_a_command() {let mut s = demo();let before = s.project.clone();- // no ids, ids of no clip, the wrong type, and a key the command does not take- for p in [json!({"clips": []}), json!({"clips": [987_654_321u64]}), json!({"clips": "all"}), json!({"clips": [-1, 1.5, null]}), json!({"items": [1]})] {+ // no ids, the wrong type, and a key the command does not take+ for p in [json!({"clips": []}), json!({"clips": "all"}), json!({"items": [1]})] {assert_eq!(disabled(s.execute("edit.rippleDelete", p.clone())), "nothing selected", "{p}");}+ // ids of no clip and values that are not ids: still disabled, and the reason names them (#591)+ let why = disabled(s.execute("edit.rippleDelete", json!({"clips": [987_654_321u64]})));+ assert!(why.starts_with("no clip 987654321 in the active sequence"), "{why}");+ let why = disabled(s.execute("edit.rippleDelete", json!({"clips": [-1, 1.5, null]})));+ assert!(why.starts_with("-1 is not a clip id"), "{why}");assert_eq!(disabled(s.execute("clip.replaceFromBin", json!({"clips": [v1(&s)[0].id.0], "item": 987_654_321u64}))), "select a clip in the Project panel");// `edit.cut` takes no `clips`: it works on the selection onlyassert_eq!(disabled(s.execute("edit.cut", json!({"clips": [v1(&s)[0].id.0]}))), "no clips selected");@@ -159,6 +164,44 @@ fn link_and_speed_take_explicit_clips_without_a_selection() {assert!(s.state.selection.is_empty() && !s.is_enabled("clip.speedDuration"), "menu enablement still follows the selection");}+/// #591: a caller that names clips which cannot stand in for the selection hears why, instead of+/// "no clips selected": a clip of another sequence, an id of something else, a string id.+#[test]+fn named_clips_that_are_not_usable_say_why() {+ let mut s = demo();+ let demo_seq = s.state.active_sequence.unwrap();+ let demo_name = s.project.item(demo_seq).unwrap().name.clone();+ let last = v1(&s).last().unwrap().clone();+ // a track id and a project item id are not clip ids+ let track = s.active_sequence().unwrap().video_tracks[0].id.0;+ let why = disabled(s.execute("clip.speedDuration", json!({"clips": [track], "speed": 50})));+ assert_eq!(why, format!("no clip {track} in the active sequence (clip ids are the `clip` numbers of the items in sequence.inspect)"));+ let why = disabled(s.execute("clip.speedDuration", json!({"clips": [last.item.0], "speed": 50})));+ assert!(why.starts_with(&format!("no clip {} in the active sequence", last.item.0)), "{why}");+ // a string is not an id+ let why = disabled(s.execute("clip.speedDuration", json!({"clips": [last.id.0.to_string()], "speed": 50})));+ assert!(why.starts_with(&format!("\"{}\" is not a clip id", last.id.0)), "{why}");+ // `clip`, where the command documents it+ let why = disabled(s.execute("graphics.setText", json!({"clip": "x", "text": "x"})));+ assert!(why.starts_with("\"x\" is not a clip id"), "{why}");+ // a clip of a sequence that is not the active one+ s.execute("file.newSequence", json!({"name": "Other"})).unwrap();+ assert_ne!(s.state.active_sequence, Some(demo_seq));+ let why = disabled(s.execute("clip.speedDuration", json!({"clips": [last.id.0], "speed": 200})));+ assert_eq!(+ why,+ format!("clip {} is in sequence \"{demo_name}\", not the active one: open it first with sequence.open {{\"item\":{}}}", last.id.0, demo_seq.0)+ );+ // and doing what it says works+ s.execute("sequence.open", json!({"item": demo_seq.0})).unwrap();+ s.execute("clip.speedDuration", json!({"clips": [last.id.0], "speed": 200})).unwrap();+ assert!(clip(&s, last.id).unwrap().duration < last.duration);+ // naming nothing keeps the selection's reason, for menus and callers alike+ s.state.selection.clear();+ assert_eq!(disabled(s.execute("clip.speedDuration", json!({"speed": 50}))), "no clips selected");+ assert_eq!(disabled(s.execute("clip.speedDuration", json!({"clips": [], "speed": 50}))), "no clips selected");+}+#[test]fn naming_targets_lifts_only_the_selection_condition() {// no sequence: named clips cannot stand in for onediff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex eea55b32a..b2779f641 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -694,8 +694,11 @@ impl Session {}let clips = commands::named_clips(self, spec, params);let items = commands::named_items(self, spec, params);+ // clips were named but none is one of the active sequence: say which and why, rather+ // than "no clips selected" to a caller that did not mean the selection (#591)+ let missing = if clips.is_none() { commands::named_clips_missing(self, spec, params) } else { None };if clips.is_none() && items.is_none() {- return by_selection;+ return by_selection.map_err(|why| missing.unwrap_or(why));}let mut clips = clips.unwrap_or_else(|| self.state.selection.clone());let mut items = items.unwrap_or_else(|| self.state.project_selection.clone());@@ -704,7 +707,7 @@ impl Session {let by_params = (spec.enabled)(self);self.state.selection = clips;self.state.project_selection = items;- by_params+ by_params.map_err(|why| missing.unwrap_or(why))}/// Whether the command can run on the current selection (what the menus show).