diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index 0f11820..cedd30f 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -53,6 +53,8 @@ pub enum ActionId { Paste, Cut, Regroup, + Detach, + Group, Select, SelectNext, SelectPrev, @@ -74,7 +76,7 @@ pub enum ActionId { impl ActionId { /// Display order everywhere -- the editor lists them in it and /// [`Keymap::defaults`] binds them in it. - pub const ALL: [ActionId; 29] = [ + pub const ALL: [ActionId; 31] = [ ActionId::Play, ActionId::StepBack, ActionId::StepForward, @@ -88,6 +90,8 @@ impl ActionId { ActionId::Paste, ActionId::Cut, ActionId::Regroup, + ActionId::Detach, + ActionId::Group, ActionId::Select, ActionId::SelectNext, ActionId::SelectPrev, @@ -122,6 +126,8 @@ impl ActionId { ActionId::Paste => "Paste", ActionId::Cut => "Cut", ActionId::Regroup => "Regroup", + ActionId::Detach => "Detach the sound from the picture", + ActionId::Group => "Group with the clip on another track", ActionId::Select => "Select the clip under the playhead (again for the next lane)", ActionId::SelectNext => "Select the next clip in the lane", ActionId::SelectPrev => "Select the previous clip in the lane", @@ -158,6 +164,8 @@ impl ActionId { ActionId::Paste => "paste", ActionId::Cut => "cut", ActionId::Regroup => "regroup", + ActionId::Detach => "detach", + ActionId::Group => "group", ActionId::Select => "select", ActionId::SelectNext => "select-next", ActionId::SelectPrev => "select-prev", @@ -207,6 +215,8 @@ impl ActionId { ActionId::Resolution => Category::View, ActionId::Cut | ActionId::Regroup + | ActionId::Detach + | ActionId::Group | ActionId::Undo | ActionId::AddVideoLane | ActionId::AddAudioLane => Category::Editing, @@ -453,6 +463,12 @@ impl Keymap { // The two lane keys, on the letters they are named after: both // were free, and neither is next to a key that edits. b(ActionId::Regroup, "g", false), + // The pair that takes a take apart and puts it back: "d" for + // detach was free and is one press like the rest of the clip + // keys, and grouping two clips is the "g" that rejoins a cut one + // lane over -- the chord beside it, so the two read as one idea. + b(ActionId::Detach, "d", false), + b(ActionId::Group, "g", true), // Selection without a pointer. Tab is what a keyboard already // means by "move on to the next thing", and the two brackets // are the pair either side of it in the lane -- shift+tab is @@ -753,7 +769,7 @@ mod tests { #[test] fn every_default_stroke_reaches_its_action() { let k = Keymap::defaults(); - assert_eq!(k.entries().len(), 31); + assert_eq!(k.entries().len(), 33); assert_eq!(k.lookup("space", false), Some(ActionId::Play)); // The seek keys: bare arrows a frame, ctrl arrows a second, and the two // ends of the timeline. @@ -769,6 +785,9 @@ mod tests { assert_eq!(k.lookup("v", true), Some(ActionId::Paste)); assert_eq!(k.lookup("c", false), Some(ActionId::Cut)); assert_eq!(k.lookup("g", false), Some(ActionId::Regroup)); + // The take apart / put back pair beside it. + assert_eq!(k.lookup("d", false), Some(ActionId::Detach)); + assert_eq!(k.lookup("g", true), Some(ActionId::Group)); // The keyboard's way onto a clip, and the pair that walks the lane. assert_eq!(k.lookup("tab", false), Some(ActionId::Select)); assert_eq!(k.lookup("]", false), Some(ActionId::SelectNext)); @@ -977,7 +996,7 @@ mod tests { assert_eq!(whole("edith-keys 1\n").display(ActionId::Cut), "unbound"); // The label is the editor's column, and never the file's word for it. assert_eq!(ActionId::CancelExport.label(), "Cancel export"); - assert_eq!(ActionId::ALL.len(), 29); + assert_eq!(ActionId::ALL.len(), 31); } #[test] diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 0222090..3809067 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -232,11 +232,13 @@ struct ContextMenu { /// action a stroke already reaches -- the menu is a second way *to* the actions /// and never a second version of them -- so both the label and the hint come /// out of the keymap registry and the two can never disagree. -const MENU_ITEMS: [ActionId; 8] = [ +const MENU_ITEMS: [ActionId; 10] = [ ActionId::Cut, ActionId::Delete, ActionId::Lift, ActionId::Regroup, + ActionId::Detach, + ActionId::Group, ActionId::Equalizer, ActionId::Color, ActionId::Fit, @@ -549,6 +551,8 @@ impl Player { ActionId::Paste => self.paste(cx), ActionId::Cut => self.cut(cx), ActionId::Regroup => self.regroup(cx), + ActionId::Detach => self.detach(cx), + ActionId::Group => self.group(cx), ActionId::Select => self.select_under_playhead(cx), ActionId::SelectNext => self.select_step(true, cx), ActionId::SelectPrev => self.select_step(false, cx), @@ -965,6 +969,63 @@ impl Player { cx.notify(); } + /// Takes the selected clip out of its group, so the picture and the sound + /// under it are edited apart from here on: each half selects, moves, trims + /// and is removed alone, and both draw outlined instead of tinted. The + /// selection stays -- the half that was clicked is still the half in hand. + fn detach(&mut self, cx: &mut Context) { + if self.exporting().is_some() { + return; + } + match (&mut self.session, self.selected) { + (Some(session), Some((lane, idx))) => { + if !session.ungroup(lane, idx) { + self.notice = + Some("NOTHING DETACHED — that clip is not grouped with another".into()); + } + } + (Some(_), None) => { + self.notice = Some("NOTHING DETACHED — click the take to take apart first".into()) + } + (None, _) => {} + } + cx.notify(); + } + + /// Puts the selected clip back in a group with the clip covering exactly the + /// same frames on another track -- the way back from [`Player::detach`], and + /// the way to group a picture with sound it was never opened with. The + /// partner is not clicked because there is nothing to choose: a group id + /// names one span, so only a clip covering these very frames could join it, + /// and the engine words what to do when none does. + fn group(&mut self, cx: &mut Context) { + if self.exporting().is_some() { + return; + } + let partner = match (&self.session, self.selected) { + (Some(session), Some((lane, idx))) => span_partner(session, lane, idx), + _ => None, + }; + match (&mut self.session, self.selected, partner) { + (Some(session), Some((lane, idx)), Some((other, o_idx))) => { + if let Err(e) = session.group(lane, idx, other, o_idx) { + self.notice = Some(format!("NOT GROUPED — {e}").into()); + } + } + (Some(_), Some(_), None) => { + self.notice = Some( + "NOTHING TO GROUP WITH — no clip on another track covers exactly these frames" + .into(), + ) + } + (Some(_), None, _) => { + self.notice = Some("NOTHING GROUPED — click one of the halves first".into()) + } + (None, ..) => {} + } + cx.notify(); + } + /// Drops the selected clip and closes the hole: a whole take goes, both /// lanes of it, and everything after it moves up. A half with no take under /// it in the video lane -- what a lift leaves behind -- has nothing to @@ -3946,6 +4007,10 @@ fn applicable(clip: &Clip, lane: Lane, action: ActionId, playhead: u32) -> bool // at an edge of this clip. Whether those two halves were ever one take // is the engine's question, and it words that refusal itself. ActionId::Regroup => playhead == clip.start || playhead == clip.end(), + // Nothing to take apart in a clip that names no group at all. Whether + // the group it names still has another half is the engine's question, + // like the regroup above, and it words that refusal itself. + ActionId::Detach => clip.link.is_some(), _ => true, } } @@ -4457,20 +4522,50 @@ fn whole_take(session: &PlaybackSession, lane: Lane, idx: usize) -> bool { let Some(clip) = session.lane_clips(lane).get(idx) else { return false; }; - match (lane.kind, lane.ord) { - (_, 1..) => false, - (LaneKind::Video, _) => true, - // The sound of a take, only while the take is still there: its group is - // carried by a clip on some other lane. - (LaneKind::Audio, _) => session + let paired = || { + session .lanes() .into_iter() .filter(|&other| other != lane) .flat_map(|other| session.lane_clips(other)) - .any(|o| o.link.is_some() && o.link == clip.link), + .any(|o| o.link.is_some() && o.link == clip.link) + }; + match (lane.kind, lane.ord) { + (_, 1..) => false, + // The picture of a take -- unless the take has been taken apart: a + // detached picture (a group id no other lane carries, which is also what + // a lift of the sound leaves) is a half like the sound is, and a ripple + // under it would drag away the very half it was detached from. A clip in + // no group at all is not a half but a placement, and on `V1` a placement + // is the take there is. + (LaneKind::Video, _) => clip.link.is_none() || paired(), + // The sound of a take, only while the take is still there: its group is + // carried by a clip on some other lane. + (LaneKind::Audio, _) => paired(), } } +/// The clip a Group would pair this one with: the first clip on another track, +/// in the order the lanes are drawn, covering exactly the same frames and not in +/// this clip's group already. Exactly the same frames because that is all a +/// group id can mean (engine `links_are_consistent`), which is what leaves +/// nothing for a second click to choose. `None` when no track has one, and the +/// notice says so. +fn span_partner(session: &PlaybackSession, lane: Lane, idx: usize) -> Option<(Lane, usize)> { + let clip = *session.lane_clips(lane).get(idx)?; + session + .lanes() + .into_iter() + .filter(|&other| other != lane) + .find_map(|other| { + let i = session.lane_clips(other).iter().position(|c| { + (c.start, c.end()) == (clip.start, clip.end()) + && !(c.link.is_some() && c.link == clip.link) + })?; + Some((other, i)) + }) +} + /// Whether a click marks this clip: the clip that was clicked always, and the /// other lane's clip of the same group with it. A clip whose group has no other /// half -- what a lift leaves behind -- marks only itself, which is what makes a @@ -4807,8 +4902,8 @@ mod tests { eq_y, export_path, export_settings, format_line, frac_along, frac_down, frame_at, is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked, menu_at, mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label, - source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources, whole_take, - width_frac, window_title, + source_frames, source_tint, span_partner, start_frac, timecode, unseen_paths, + unseen_sources, whole_take, width_frac, window_title, }; use super::{file_name, file_uri, library_rows}; @@ -5057,6 +5152,16 @@ mod tests { assert!(applicable(&clip, v1, ActionId::Regroup, 30)); assert!(applicable(&clip, v1, ActionId::Regroup, 90)); assert!(!applicable(&clip, v1, ActionId::Regroup, 60)); + // Detach is the clip's own business: nothing to take apart in one that + // names no group, and whether the group still has another half is the + // engine's question. Group is offered on every clip, for that reason. + assert!(!applicable(&clip, v1, ActionId::Detach, 0)); + let grouped = Clip { + link: Some(3), + ..clip + }; + assert!(applicable(&grouped, v1, ActionId::Detach, 60)); + assert!(applicable(&clip, a1, ActionId::Group, 0)); // The equalizer is the one item the *lane* decides: it filters samples, // and a video clip has none of its own. Never the playhead's business. assert!(applicable(&clip, a1, ActionId::Equalizer, 0)); @@ -5098,6 +5203,55 @@ mod tests { } } + /// What the Detach and Group items do to a real timeline: a music video's + /// sound comes off its picture, Delete on the loose half takes that half + /// only, undo puts both back, and Group makes the two one take again -- + /// whole-take delete and all. + #[test] + fn a_detached_half_is_removed_alone_and_groups_again() { + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + assert!(whole_take(&session, Lane::V1, 0), "one take to start with"); + assert!(whole_take(&session, Lane::A1, 0)); + let frames = session.lane_clips(Lane::V1)[0].len(); + + // Detach audio: neither half is a whole take any more, so Delete on + // either leaves the other exactly where it is. + assert!(session.ungroup(Lane::V1, 0)); + assert!( + !whole_take(&session, Lane::A1, 0), + "the sound is a half now" + ); + assert!(!whole_take(&session, Lane::V1, 0), "and so is the picture"); + assert!(session.lift_clip(Lane::A1, 0)); + assert!(session.lane_clips(Lane::A1).is_empty(), "the sound went"); + assert_eq!(session.lane_clips(Lane::V1).len(), 1, "the picture stayed"); + assert_eq!(session.lane_clips(Lane::V1)[0].len(), frames, "untrimmed"); + + // One undo per edit, the removal then the detach. + assert!(session.undo()); + assert_eq!(session.lane_clips(Lane::A1).len(), 1, "the sound is back"); + assert!(session.undo()); + assert!(whole_take(&session, Lane::A1, 0), "one take again"); + + // Group: the partner is the clip covering these very frames on the + // other track, which is what the item hands the engine. + assert!(session.ungroup(Lane::V1, 0)); + assert_eq!(span_partner(&session, Lane::V1, 0), Some((Lane::A1, 0))); + session + .group(Lane::V1, 0, Lane::A1, 0) + .expect("both halves still cover the same frames"); + assert!( + whole_take(&session, Lane::A1, 0), + "a take that ripples again" + ); + assert_eq!( + span_partner(&session, Lane::V1, 0), + None, + "and nothing left on another track to group with" + ); + } + /// The refusal path, end to end against the real files: an incompatible /// import changes nothing, and the library mirrors `sources()` 1:1, so a /// refused file cannot leave a row behind. diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 881b8d1..ab3d8df 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -584,6 +584,21 @@ impl PlaybackSession { .regroup(secs_to_frame(timeline_secs, self.meta.frame_rate)) } + /// Takes that clip out of its group, so its picture and its sound are edited + /// apart from here on. Metadata only like a cut -- no reseek -- and one undo + /// step. `false` for a bad index and for a clip that is not grouped with + /// anything, which is already detached. + pub fn ungroup(&mut self, lane: Lane, idx: usize) -> bool { + self.project.ungroup(lane, idx) + } + + /// Puts two clips covering the same frames back into one group: the regroup + /// of what [`ungroup`](Self::ungroup) took apart. Metadata only, one undo + /// step, and the error says why when it refuses. + pub fn group(&mut self, a: Lane, a_idx: usize, b: Lane, b_idx: usize) -> crate::Result<()> { + self.project.group(a, a_idx, b, b_idx) + } + /// What the clip at `idx` of `lane` is equalized with, or `None` for one /// that plays flat -- what a card shows before it lets anyone drag a band. pub fn eq_of(&self, lane: Lane, idx: usize) -> Option<&EqParams> { diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 190dc15..e77d6bd 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -971,6 +971,115 @@ impl Project { true } + /// Take the clip at `idx` of `lane` out of its group: every clip carrying + /// its id -- on however many lanes -- is handed an id of its own, so from + /// here on each half moves, trims and is deleted alone. The music video + /// whose sound is to be cut against its picture starts here. + /// + /// An id of its own rather than none at all: a half no other lane is grouped + /// with is exactly what a [`lift`](Project::lift) leaves behind and is legal + /// (see [`links_are_consistent`]), and it is what a front-end already draws + /// as detached. `None` would instead say "was never part of a take", which + /// is what a one-lane [`place`](Project::place) means. + /// + /// Metadata only, like a [`split`](Project::split): no mapping changes, so + /// nothing has to be reseeked. One snapshot, so one [`Project::undo`] puts + /// the group back. Refused (`false`, nothing changed) for an index that is + /// not there, a clip in no group at all, and one whose group has no other + /// half -- that one is already detached, and a refusal must not cost an undo + /// step. + pub fn ungroup(&mut self, lane: Lane, idx: usize) -> bool { + let Some(id) = self.lane(lane).get(idx).and_then(|c| c.link) else { + return false; + }; + let members = self + .lanes + .iter() + .flat_map(|l| &l.clips) + .filter(|c| c.link == Some(id)) + .count(); + if members < 2 { + return false; + } + self.snapshot(); + // Drawn before the walk: `new_link` takes the whole project, and the + // walk holds the lanes. + let mut fresh = (0..members).map(|_| self.new_link()).collect::>(); + for data in &mut self.lanes { + for c in data.clips.iter_mut().filter(|c| c.link == Some(id)) { + c.link = fresh.pop(); + } + } + debug_assert!(links_are_consistent(&self.lanes).is_ok()); + true + } + + /// Put two clips on two lanes into one group, so what moves one moves the + /// other again -- the undo of a [`ungroup`](Project::ungroup) by hand, and + /// how a picture is regrouped with sound it was never opened with. Whatever + /// either of them was grouped with comes along: those clips cover this same + /// span already, so the result is one group and not two overlapping ones. + /// + /// Same frames or nothing: a group id names **one span** on however many + /// lanes ([`links_are_consistent`] refuses to load anything else), so two + /// clips that do not cover the same frames cannot be one take, and the + /// refusal says which bounds to trim to. Kinds are *not* checked: a take may + /// run on `V1` and `V2` at once, and picture-with-sound is the case people + /// mean, not the rule. + /// + /// Metadata only, one snapshot. The error says what is wrong, because + /// `false` would not: a bad index, one lane twice (a group is at most one + /// clip per lane), spans that disagree, and a pair that is already one take + /// -- nothing changes, and none of them costs an undo step. + pub fn group(&mut self, a: Lane, a_idx: usize, b: Lane, b_idx: usize) -> crate::Result<()> { + if a == b { + return Err(format!( + "a group is one clip per lane: pick the clip to group with on another track, not a second one on {}", + a.label() + ) + .into()); + } + let clip = |p: &Self, lane: Lane, idx: usize| -> crate::Result { + p.lane(lane) + .get(idx) + .copied() + .ok_or_else(|| format!("there is no clip {idx} on {}", lane.label()).into()) + }; + let (x, y) = (clip(self, a, a_idx)?, clip(self, b, b_idx)?); + if (x.start, x.end()) != (y.start, y.end()) { + return Err(format!( + "{} covers [{}, {}) and {} covers [{}, {}): trim them to matching bounds first", + a.label(), + x.start, + x.end(), + b.label(), + y.start, + y.end() + ) + .into()); + } + if x.link.is_some() && x.link == y.link { + return Err("those two are one take already".into()); + } + self.snapshot(); + let id = self.new_link(); + // Every clip either of them was grouped with, by the same rule: all of + // them cover this span, so one id over the lot stays consistent. + let old = [x.link, y.link]; + for data in &mut self.lanes { + for c in data.clips.iter_mut().filter(|c| c.link.is_some()) { + if old.contains(&c.link) { + c.link = Some(id); + } + } + } + // And the two themselves, which a `place` may have left in no group. + self.lane_mut(a).expect("read above")[a_idx].link = Some(id); + self.lane_mut(b).expect("read above")[b_idx].link = Some(id); + debug_assert!(links_are_consistent(&self.lanes).is_ok()); + Ok(()) + } + /// Place `clip` in one lane at `timeline_frame`, overwriting whatever it /// lands on and leaving every other clip exactly where it is -- the /// per-lane paste. Anything already there is trimmed away (and split in two @@ -1671,6 +1780,103 @@ mod tests { ); } + /// The music video's own path: the take comes apart into halves that carry + /// ids of their own, one undo puts it back, and two clips over the same + /// frames become one take again by hand. + #[test] + fn a_take_comes_apart_and_goes_back_together() { + let mut p = Project::single(FILE, 9); + let one = p.clips()[0].link.expect("a fresh project is one take"); + assert_eq!(p.lane(Lane::A1)[0].link, Some(one)); + + // A third lane in the same group -- and the merge case with it: a + // placement is in no group, and grouping it in leaves *one* group. + let v2 = p.add_lane(LaneKind::Video); + assert!(p.place(v2, 0, clip(0, 0, 9, 0))); + assert!(p.lane(v2)[0].link.is_none(), "a placement joins no group"); + p.group(Lane::V1, 0, v2, 0) + .expect("the same frames, one lane over"); + let id = p.clips()[0].link.expect("still a group"); + assert!( + p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)), + "one id over the three, not two groups sharing a span" + ); + links_are_consistent(&p.lanes).expect("one id, one span"); + + // Detached by the sound: every half of the group, not only the two. + assert!(p.ungroup(Lane::A1, 0)); + let ids: Vec> = p.lanes().into_iter().map(|l| p.lane(l)[0].link).collect(); + assert!( + ids.iter().all(Option::is_some), + "a half keeps an id of its own" + ); + assert!( + ids.iter() + .enumerate() + .all(|(i, a)| ids[..i].iter().all(|b| a != b)), + "and no two halves are the same group any more: {ids:?}" + ); + links_are_consistent(&p.lanes).expect("a lone id is legal"); + + // One snapshot for the whole detach. + assert!(p.undo()); + assert!( + p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)), + "one undo puts the whole group back" + ); + + // And back together by hand: the two the pointer named, and only them. + assert!(p.ungroup(Lane::V1, 0)); + p.group(Lane::V1, 0, Lane::A1, 0) + .expect("both cover [0, 9)"); + assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link); + assert_ne!( + p.lane(v2)[0].link, + p.lane(Lane::V1)[0].link, + "the third half stayed detached" + ); + links_are_consistent(&p.lanes).expect("one id, one span"); + } + + /// What a group may not be, and what a detach has nothing to take apart -- + /// none of which may cost an undo step. + #[test] + fn a_group_is_one_span_and_one_clip_per_lane() { + let mut p = three(); + let before = shape(&p); + let why = |r: crate::Result<()>| r.expect_err("refused").to_string(); + + assert!( + why(p.group(Lane::V1, 0, Lane::V1, 1)).contains("one clip per lane"), + "two clips of one lane are never one take" + ); + assert!(why(p.group(Lane::V1, 0, Lane::A1, 9)).contains("there is no clip 9 on A1")); + let spans = why(p.group(Lane::V1, 0, Lane::A1, 1)); + assert!( + spans.contains("V1 covers [0, 3) and A1 covers [3, 5)"), + "{spans}" + ); + assert!( + spans.contains("trim them to matching bounds first"), + "{spans}" + ); + assert!(why(p.group(Lane::V1, 0, Lane::A1, 0)).contains("one take already")); + + // A half nothing else is grouped with is already detached, a placement + // is in no group at all, and an index that is not there has nothing to + // detach either. + assert!(p.place(Lane::V1, 20, clip(20, 0, 3, 0))); + assert!(!p.ungroup(Lane::V1, 3), "a placement is in no group"); + assert!(p.lift(Lane::A1, 0)); + assert!(!p.ungroup(Lane::V1, 0), "its group has no other half"); + assert!(!p.ungroup(Lane::V1, 9), "no clip there"); + + // Two undos, one per edit: not one of the refusals pushed a snapshot. + assert!(p.undo()); + assert!(p.undo()); + assert_eq!(shape(&p), before); + } + #[test] fn split_refused_at_zero_boundary_end_and_in_a_gap() { let mut p = three();