forge
Explore
Log in

Don't you dare go Hollow

A take detaches into halves and groups back together

Browse files at this commit

0c00d5c66e061702b166ddedad9770afd84c5796Ahmet Kaan GümüşParent: 2dd4b74
  • feat
    • a take detaches into halves and groups back together
Show full message
✨ feat(timeline): a take detaches into halves and groups back together

A music video's picture and its sound come apart on demand: Detach hands every
half of the group an id of its own, so each one selects, moves, trims and is
removed alone, and Group puts two clips covering the same frames back into one
take. Both are menu items and both are keys ("d" and ctrl+g), like every other
gesture here.

Engine: Project::ungroup(lane, idx) and Project::group(a, a_idx, b, b_idx), one
snapshot each, metadata only. A group id is one span on however many lanes
(links_are_consistent), so group refuses clips whose bounds disagree and names
the bounds to trim to; two clips of one lane, an index that is not there and a
pair that is already one take are refused in the engine's own words. Whatever
either side was grouped with is merged in, since all of it covers that span.

whole_take now reads the same rule on the picture as on the sound: a group id no
other lane carries is half a take on either lane, so Delete on a detached half
leaves the other half exactly where it is instead of rippling it away.
crates/app/src/keymap.rsmodified+22 -3
@@ -53,6 +53,8 @@ pub enum ActionId {
53 Paste,53 Paste,
54 Cut,54 Cut,
55 Regroup,55 Regroup,
56+ Detach,
57+ Group,
56 Select,58 Select,
57 SelectNext,59 SelectNext,
58 SelectPrev,60 SelectPrev,
@@ -74,7 +76,7 @@ pub enum ActionId {
74 impl ActionId {76 impl ActionId {
75 /// Display order everywhere -- the editor lists them in it and77 /// Display order everywhere -- the editor lists them in it and
76 /// [`Keymap::defaults`] binds them in it.78 /// [`Keymap::defaults`] binds them in it.
77- pub const ALL: [ActionId; 29] = [79+ pub const ALL: [ActionId; 31] = [
78 ActionId::Play,80 ActionId::Play,
79 ActionId::StepBack,81 ActionId::StepBack,
80 ActionId::StepForward,82 ActionId::StepForward,
@@ -88,6 +90,8 @@ impl ActionId {
88 ActionId::Paste,90 ActionId::Paste,
89 ActionId::Cut,91 ActionId::Cut,
90 ActionId::Regroup,92 ActionId::Regroup,
93+ ActionId::Detach,
94+ ActionId::Group,
91 ActionId::Select,95 ActionId::Select,
92 ActionId::SelectNext,96 ActionId::SelectNext,
93 ActionId::SelectPrev,97 ActionId::SelectPrev,
@@ -122,6 +126,8 @@ impl ActionId {
122 ActionId::Paste => "Paste",126 ActionId::Paste => "Paste",
123 ActionId::Cut => "Cut",127 ActionId::Cut => "Cut",
124 ActionId::Regroup => "Regroup",128 ActionId::Regroup => "Regroup",
129+ ActionId::Detach => "Detach the sound from the picture",
130+ ActionId::Group => "Group with the clip on another track",
125 ActionId::Select => "Select the clip under the playhead (again for the next lane)",131 ActionId::Select => "Select the clip under the playhead (again for the next lane)",
126 ActionId::SelectNext => "Select the next clip in the lane",132 ActionId::SelectNext => "Select the next clip in the lane",
127 ActionId::SelectPrev => "Select the previous clip in the lane",133 ActionId::SelectPrev => "Select the previous clip in the lane",
@@ -158,6 +164,8 @@ impl ActionId {
158 ActionId::Paste => "paste",164 ActionId::Paste => "paste",
159 ActionId::Cut => "cut",165 ActionId::Cut => "cut",
160 ActionId::Regroup => "regroup",166 ActionId::Regroup => "regroup",
167+ ActionId::Detach => "detach",
168+ ActionId::Group => "group",
161 ActionId::Select => "select",169 ActionId::Select => "select",
162 ActionId::SelectNext => "select-next",170 ActionId::SelectNext => "select-next",
163 ActionId::SelectPrev => "select-prev",171 ActionId::SelectPrev => "select-prev",
@@ -207,6 +215,8 @@ impl ActionId {
207 ActionId::Resolution => Category::View,215 ActionId::Resolution => Category::View,
208 ActionId::Cut216 ActionId::Cut
209 | ActionId::Regroup217 | ActionId::Regroup
218+ | ActionId::Detach
219+ | ActionId::Group
210 | ActionId::Undo220 | ActionId::Undo
211 | ActionId::AddVideoLane221 | ActionId::AddVideoLane
212 | ActionId::AddAudioLane => Category::Editing,222 | ActionId::AddAudioLane => Category::Editing,
@@ -453,6 +463,12 @@ impl Keymap {
453 // The two lane keys, on the letters they are named after: both463 // The two lane keys, on the letters they are named after: both
454 // were free, and neither is next to a key that edits.464 // were free, and neither is next to a key that edits.
455 b(ActionId::Regroup, "g", false),465 b(ActionId::Regroup, "g", false),
466+ // The pair that takes a take apart and puts it back: "d" for
467+ // detach was free and is one press like the rest of the clip
468+ // keys, and grouping two clips is the "g" that rejoins a cut one
469+ // lane over -- the chord beside it, so the two read as one idea.
470+ b(ActionId::Detach, "d", false),
471+ b(ActionId::Group, "g", true),
456 // Selection without a pointer. Tab is what a keyboard already472 // Selection without a pointer. Tab is what a keyboard already
457 // means by "move on to the next thing", and the two brackets473 // means by "move on to the next thing", and the two brackets
458 // are the pair either side of it in the lane -- shift+tab is474 // are the pair either side of it in the lane -- shift+tab is
@@ -753,7 +769,7 @@ mod tests {
753 #[test]769 #[test]
754 fn every_default_stroke_reaches_its_action() {770 fn every_default_stroke_reaches_its_action() {
755 let k = Keymap::defaults();771 let k = Keymap::defaults();
756- assert_eq!(k.entries().len(), 31);772+ assert_eq!(k.entries().len(), 33);
757 assert_eq!(k.lookup("space", false), Some(ActionId::Play));773 assert_eq!(k.lookup("space", false), Some(ActionId::Play));
758 // The seek keys: bare arrows a frame, ctrl arrows a second, and the two774 // The seek keys: bare arrows a frame, ctrl arrows a second, and the two
759 // ends of the timeline.775 // ends of the timeline.
@@ -769,6 +785,9 @@ mod tests {
769 assert_eq!(k.lookup("v", true), Some(ActionId::Paste));785 assert_eq!(k.lookup("v", true), Some(ActionId::Paste));
770 assert_eq!(k.lookup("c", false), Some(ActionId::Cut));786 assert_eq!(k.lookup("c", false), Some(ActionId::Cut));
771 assert_eq!(k.lookup("g", false), Some(ActionId::Regroup));787 assert_eq!(k.lookup("g", false), Some(ActionId::Regroup));
788+ // The take apart / put back pair beside it.
789+ assert_eq!(k.lookup("d", false), Some(ActionId::Detach));
790+ assert_eq!(k.lookup("g", true), Some(ActionId::Group));
772 // The keyboard's way onto a clip, and the pair that walks the lane.791 // The keyboard's way onto a clip, and the pair that walks the lane.
773 assert_eq!(k.lookup("tab", false), Some(ActionId::Select));792 assert_eq!(k.lookup("tab", false), Some(ActionId::Select));
774 assert_eq!(k.lookup("]", false), Some(ActionId::SelectNext));793 assert_eq!(k.lookup("]", false), Some(ActionId::SelectNext));
@@ -977,7 +996,7 @@ mod tests {
977 assert_eq!(whole("edith-keys 1\n").display(ActionId::Cut), "unbound");996 assert_eq!(whole("edith-keys 1\n").display(ActionId::Cut), "unbound");
978 // The label is the editor's column, and never the file's word for it.997 // The label is the editor's column, and never the file's word for it.
979 assert_eq!(ActionId::CancelExport.label(), "Cancel export");998 assert_eq!(ActionId::CancelExport.label(), "Cancel export");
980- assert_eq!(ActionId::ALL.len(), 29);999+ assert_eq!(ActionId::ALL.len(), 31);
981 }1000 }
982 1001
983 #[test]1002 #[test]
crates/app/src/main.rsmodified+164 -10
@@ -232,11 +232,13 @@ struct ContextMenu {
232 /// action a stroke already reaches -- the menu is a second way *to* the actions232 /// action a stroke already reaches -- the menu is a second way *to* the actions
233 /// and never a second version of them -- so both the label and the hint come233 /// and never a second version of them -- so both the label and the hint come
234 /// out of the keymap registry and the two can never disagree.234 /// out of the keymap registry and the two can never disagree.
235-const MENU_ITEMS: [ActionId; 8] = [235+const MENU_ITEMS: [ActionId; 10] = [
236 ActionId::Cut,236 ActionId::Cut,
237 ActionId::Delete,237 ActionId::Delete,
238 ActionId::Lift,238 ActionId::Lift,
239 ActionId::Regroup,239 ActionId::Regroup,
240+ ActionId::Detach,
241+ ActionId::Group,
240 ActionId::Equalizer,242 ActionId::Equalizer,
241 ActionId::Color,243 ActionId::Color,
242 ActionId::Fit,244 ActionId::Fit,
@@ -549,6 +551,8 @@ impl Player {
549 ActionId::Paste => self.paste(cx),551 ActionId::Paste => self.paste(cx),
550 ActionId::Cut => self.cut(cx),552 ActionId::Cut => self.cut(cx),
551 ActionId::Regroup => self.regroup(cx),553 ActionId::Regroup => self.regroup(cx),
554+ ActionId::Detach => self.detach(cx),
555+ ActionId::Group => self.group(cx),
552 ActionId::Select => self.select_under_playhead(cx),556 ActionId::Select => self.select_under_playhead(cx),
553 ActionId::SelectNext => self.select_step(true, cx),557 ActionId::SelectNext => self.select_step(true, cx),
554 ActionId::SelectPrev => self.select_step(false, cx),558 ActionId::SelectPrev => self.select_step(false, cx),
@@ -965,6 +969,63 @@ impl Player {
965 cx.notify();969 cx.notify();
966 }970 }
967 971
972+ /// Takes the selected clip out of its group, so the picture and the sound
973+ /// under it are edited apart from here on: each half selects, moves, trims
974+ /// and is removed alone, and both draw outlined instead of tinted. The
975+ /// selection stays -- the half that was clicked is still the half in hand.
976+ fn detach(&mut self, cx: &mut Context<Self>) {
977+ if self.exporting().is_some() {
978+ return;
979+ }
980+ match (&mut self.session, self.selected) {
981+ (Some(session), Some((lane, idx))) => {
982+ if !session.ungroup(lane, idx) {
983+ self.notice =
984+ Some("NOTHING DETACHED — that clip is not grouped with another".into());
985+ }
986+ }
987+ (Some(_), None) => {
988+ self.notice = Some("NOTHING DETACHED — click the take to take apart first".into())
989+ }
990+ (None, _) => {}
991+ }
992+ cx.notify();
993+ }
994+
995+ /// Puts the selected clip back in a group with the clip covering exactly the
996+ /// same frames on another track -- the way back from [`Player::detach`], and
997+ /// the way to group a picture with sound it was never opened with. The
998+ /// partner is not clicked because there is nothing to choose: a group id
999+ /// names one span, so only a clip covering these very frames could join it,
1000+ /// and the engine words what to do when none does.
1001+ fn group(&mut self, cx: &mut Context<Self>) {
1002+ if self.exporting().is_some() {
1003+ return;
1004+ }
1005+ let partner = match (&self.session, self.selected) {
1006+ (Some(session), Some((lane, idx))) => span_partner(session, lane, idx),
1007+ _ => None,
1008+ };
1009+ match (&mut self.session, self.selected, partner) {
1010+ (Some(session), Some((lane, idx)), Some((other, o_idx))) => {
1011+ if let Err(e) = session.group(lane, idx, other, o_idx) {
1012+ self.notice = Some(format!("NOT GROUPED — {e}").into());
1013+ }
1014+ }
1015+ (Some(_), Some(_), None) => {
1016+ self.notice = Some(
1017+ "NOTHING TO GROUP WITH — no clip on another track covers exactly these frames"
1018+ .into(),
1019+ )
1020+ }
1021+ (Some(_), None, _) => {
1022+ self.notice = Some("NOTHING GROUPED — click one of the halves first".into())
1023+ }
1024+ (None, ..) => {}
1025+ }
1026+ cx.notify();
1027+ }
1028+
968 /// Drops the selected clip and closes the hole: a whole take goes, both1029 /// Drops the selected clip and closes the hole: a whole take goes, both
969 /// lanes of it, and everything after it moves up. A half with no take under1030 /// lanes of it, and everything after it moves up. A half with no take under
970 /// it in the video lane -- what a lift leaves behind -- has nothing to1031 /// 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
3946 // at an edge of this clip. Whether those two halves were ever one take4007 // at an edge of this clip. Whether those two halves were ever one take
3947 // is the engine's question, and it words that refusal itself.4008 // is the engine's question, and it words that refusal itself.
3948 ActionId::Regroup => playhead == clip.start || playhead == clip.end(),4009 ActionId::Regroup => playhead == clip.start || playhead == clip.end(),
4010+ // Nothing to take apart in a clip that names no group at all. Whether
4011+ // the group it names still has another half is the engine's question,
4012+ // like the regroup above, and it words that refusal itself.
4013+ ActionId::Detach => clip.link.is_some(),
3949 _ => true,4014 _ => true,
3950 }4015 }
3951 }4016 }
@@ -4457,20 +4522,50 @@ fn whole_take(session: &PlaybackSession, lane: Lane, idx: usize) -> bool {
4457 let Some(clip) = session.lane_clips(lane).get(idx) else {4522 let Some(clip) = session.lane_clips(lane).get(idx) else {
4458 return false;4523 return false;
4459 };4524 };
4460- match (lane.kind, lane.ord) {4525+ let paired = || {
4461- (_, 1..) => false,4526+ session
4462- (LaneKind::Video, _) => true,
4463- // The sound of a take, only while the take is still there: its group is
4464- // carried by a clip on some other lane.
4465- (LaneKind::Audio, _) => session
4466 .lanes()4527 .lanes()
4467 .into_iter()4528 .into_iter()
4468 .filter(|&other| other != lane)4529 .filter(|&other| other != lane)
4469 .flat_map(|other| session.lane_clips(other))4530 .flat_map(|other| session.lane_clips(other))
4470- .any(|o| o.link.is_some() && o.link == clip.link),4531+ .any(|o| o.link.is_some() && o.link == clip.link)
4532+ };
4533+ match (lane.kind, lane.ord) {
4534+ (_, 1..) => false,
4535+ // The picture of a take -- unless the take has been taken apart: a
4536+ // detached picture (a group id no other lane carries, which is also what
4537+ // a lift of the sound leaves) is a half like the sound is, and a ripple
4538+ // under it would drag away the very half it was detached from. A clip in
4539+ // no group at all is not a half but a placement, and on `V1` a placement
4540+ // is the take there is.
4541+ (LaneKind::Video, _) => clip.link.is_none() || paired(),
4542+ // The sound of a take, only while the take is still there: its group is
4543+ // carried by a clip on some other lane.
4544+ (LaneKind::Audio, _) => paired(),
4471 }4545 }
4472 }4546 }
4473 4547
4548+/// The clip a Group would pair this one with: the first clip on another track,
4549+/// in the order the lanes are drawn, covering exactly the same frames and not in
4550+/// this clip's group already. Exactly the same frames because that is all a
4551+/// group id can mean (engine `links_are_consistent`), which is what leaves
4552+/// nothing for a second click to choose. `None` when no track has one, and the
4553+/// notice says so.
4554+fn span_partner(session: &PlaybackSession, lane: Lane, idx: usize) -> Option<(Lane, usize)> {
4555+ let clip = *session.lane_clips(lane).get(idx)?;
4556+ session
4557+ .lanes()
4558+ .into_iter()
4559+ .filter(|&other| other != lane)
4560+ .find_map(|other| {
4561+ let i = session.lane_clips(other).iter().position(|c| {
4562+ (c.start, c.end()) == (clip.start, clip.end())
4563+ && !(c.link.is_some() && c.link == clip.link)
4564+ })?;
4565+ Some((other, i))
4566+ })
4567+}
4568+
4474 /// Whether a click marks this clip: the clip that was clicked always, and the4569 /// Whether a click marks this clip: the clip that was clicked always, and the
4475 /// other lane's clip of the same group with it. A clip whose group has no other4570 /// other lane's clip of the same group with it. A clip whose group has no other
4476 /// half -- what a lift leaves behind -- marks only itself, which is what makes a4571 /// half -- what a lift leaves behind -- marks only itself, which is what makes a
@@ -4807,8 +4902,8 @@ mod tests {
4807 eq_y, export_path, export_settings, format_line, frac_along, frac_down, frame_at,4902 eq_y, export_path, export_settings, format_line, frac_along, frac_down, frame_at,
4808 is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked, menu_at, mp4_refusal,4903 is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked, menu_at, mp4_refusal,
4809 normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label,4904 normalise, panel_h, project_path, push_digit, retarget, scrub_due, show_label,
4810- source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources, whole_take,4905+ source_frames, source_tint, span_partner, start_frac, timecode, unseen_paths,
4811- width_frac, window_title,4906+ unseen_sources, whole_take, width_frac, window_title,
4812 };4907 };
4813 use super::{file_name, file_uri, library_rows};4908 use super::{file_name, file_uri, library_rows};
4814 4909
@@ -5057,6 +5152,16 @@ mod tests {
5057 assert!(applicable(&clip, v1, ActionId::Regroup, 30));5152 assert!(applicable(&clip, v1, ActionId::Regroup, 30));
5058 assert!(applicable(&clip, v1, ActionId::Regroup, 90));5153 assert!(applicable(&clip, v1, ActionId::Regroup, 90));
5059 assert!(!applicable(&clip, v1, ActionId::Regroup, 60));5154 assert!(!applicable(&clip, v1, ActionId::Regroup, 60));
5155+ // Detach is the clip's own business: nothing to take apart in one that
5156+ // names no group, and whether the group still has another half is the
5157+ // engine's question. Group is offered on every clip, for that reason.
5158+ assert!(!applicable(&clip, v1, ActionId::Detach, 0));
5159+ let grouped = Clip {
5160+ link: Some(3),
5161+ ..clip
5162+ };
5163+ assert!(applicable(&grouped, v1, ActionId::Detach, 60));
5164+ assert!(applicable(&clip, a1, ActionId::Group, 0));
5060 // The equalizer is the one item the *lane* decides: it filters samples,5165 // The equalizer is the one item the *lane* decides: it filters samples,
5061 // and a video clip has none of its own. Never the playhead's business.5166 // and a video clip has none of its own. Never the playhead's business.
5062 assert!(applicable(&clip, a1, ActionId::Equalizer, 0));5167 assert!(applicable(&clip, a1, ActionId::Equalizer, 0));
@@ -5098,6 +5203,55 @@ mod tests {
5098 }5203 }
5099 }5204 }
5100 5205
5206+ /// What the Detach and Group items do to a real timeline: a music video's
5207+ /// sound comes off its picture, Delete on the loose half takes that half
5208+ /// only, undo puts both back, and Group makes the two one take again --
5209+ /// whole-take delete and all.
5210+ #[test]
5211+ fn a_detached_half_is_removed_alone_and_groups_again() {
5212+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
5213+ session.set_gain(0.0);
5214+ assert!(whole_take(&session, Lane::V1, 0), "one take to start with");
5215+ assert!(whole_take(&session, Lane::A1, 0));
5216+ let frames = session.lane_clips(Lane::V1)[0].len();
5217+
5218+ // Detach audio: neither half is a whole take any more, so Delete on
5219+ // either leaves the other exactly where it is.
5220+ assert!(session.ungroup(Lane::V1, 0));
5221+ assert!(
5222+ !whole_take(&session, Lane::A1, 0),
5223+ "the sound is a half now"
5224+ );
5225+ assert!(!whole_take(&session, Lane::V1, 0), "and so is the picture");
5226+ assert!(session.lift_clip(Lane::A1, 0));
5227+ assert!(session.lane_clips(Lane::A1).is_empty(), "the sound went");
5228+ assert_eq!(session.lane_clips(Lane::V1).len(), 1, "the picture stayed");
5229+ assert_eq!(session.lane_clips(Lane::V1)[0].len(), frames, "untrimmed");
5230+
5231+ // One undo per edit, the removal then the detach.
5232+ assert!(session.undo());
5233+ assert_eq!(session.lane_clips(Lane::A1).len(), 1, "the sound is back");
5234+ assert!(session.undo());
5235+ assert!(whole_take(&session, Lane::A1, 0), "one take again");
5236+
5237+ // Group: the partner is the clip covering these very frames on the
5238+ // other track, which is what the item hands the engine.
5239+ assert!(session.ungroup(Lane::V1, 0));
5240+ assert_eq!(span_partner(&session, Lane::V1, 0), Some((Lane::A1, 0)));
5241+ session
5242+ .group(Lane::V1, 0, Lane::A1, 0)
5243+ .expect("both halves still cover the same frames");
5244+ assert!(
5245+ whole_take(&session, Lane::A1, 0),
5246+ "a take that ripples again"
5247+ );
5248+ assert_eq!(
5249+ span_partner(&session, Lane::V1, 0),
5250+ None,
5251+ "and nothing left on another track to group with"
5252+ );
5253+ }
5254+
5101 /// The refusal path, end to end against the real files: an incompatible5255 /// The refusal path, end to end against the real files: an incompatible
5102 /// import changes nothing, and the library mirrors `sources()` 1:1, so a5256 /// import changes nothing, and the library mirrors `sources()` 1:1, so a
5103 /// refused file cannot leave a row behind.5257 /// refused file cannot leave a row behind.
crates/engine/src/playback.rsmodified+15 -0
@@ -584,6 +584,21 @@ impl PlaybackSession {
584 .regroup(secs_to_frame(timeline_secs, self.meta.frame_rate))584 .regroup(secs_to_frame(timeline_secs, self.meta.frame_rate))
585 }585 }
586 586
587+ /// Takes that clip out of its group, so its picture and its sound are edited
588+ /// apart from here on. Metadata only like a cut -- no reseek -- and one undo
589+ /// step. `false` for a bad index and for a clip that is not grouped with
590+ /// anything, which is already detached.
591+ pub fn ungroup(&mut self, lane: Lane, idx: usize) -> bool {
592+ self.project.ungroup(lane, idx)
593+ }
594+
595+ /// Puts two clips covering the same frames back into one group: the regroup
596+ /// of what [`ungroup`](Self::ungroup) took apart. Metadata only, one undo
597+ /// step, and the error says why when it refuses.
598+ pub fn group(&mut self, a: Lane, a_idx: usize, b: Lane, b_idx: usize) -> crate::Result<()> {
599+ self.project.group(a, a_idx, b, b_idx)
600+ }
601+
587 /// What the clip at `idx` of `lane` is equalized with, or `None` for one602 /// What the clip at `idx` of `lane` is equalized with, or `None` for one
588 /// that plays flat -- what a card shows before it lets anyone drag a band.603 /// that plays flat -- what a card shows before it lets anyone drag a band.
589 pub fn eq_of(&self, lane: Lane, idx: usize) -> Option<&EqParams> {604 pub fn eq_of(&self, lane: Lane, idx: usize) -> Option<&EqParams> {
crates/engine/src/project.rsmodified+206 -0
@@ -971,6 +971,115 @@ impl Project {
971 true971 true
972 }972 }
973 973
974+ /// Take the clip at `idx` of `lane` out of its group: every clip carrying
975+ /// its id -- on however many lanes -- is handed an id of its own, so from
976+ /// here on each half moves, trims and is deleted alone. The music video
977+ /// whose sound is to be cut against its picture starts here.
978+ ///
979+ /// An id of its own rather than none at all: a half no other lane is grouped
980+ /// with is exactly what a [`lift`](Project::lift) leaves behind and is legal
981+ /// (see [`links_are_consistent`]), and it is what a front-end already draws
982+ /// as detached. `None` would instead say "was never part of a take", which
983+ /// is what a one-lane [`place`](Project::place) means.
984+ ///
985+ /// Metadata only, like a [`split`](Project::split): no mapping changes, so
986+ /// nothing has to be reseeked. One snapshot, so one [`Project::undo`] puts
987+ /// the group back. Refused (`false`, nothing changed) for an index that is
988+ /// not there, a clip in no group at all, and one whose group has no other
989+ /// half -- that one is already detached, and a refusal must not cost an undo
990+ /// step.
991+ pub fn ungroup(&mut self, lane: Lane, idx: usize) -> bool {
992+ let Some(id) = self.lane(lane).get(idx).and_then(|c| c.link) else {
993+ return false;
994+ };
995+ let members = self
996+ .lanes
997+ .iter()
998+ .flat_map(|l| &l.clips)
999+ .filter(|c| c.link == Some(id))
1000+ .count();
1001+ if members < 2 {
1002+ return false;
1003+ }
1004+ self.snapshot();
1005+ // Drawn before the walk: `new_link` takes the whole project, and the
1006+ // walk holds the lanes.
1007+ let mut fresh = (0..members).map(|_| self.new_link()).collect::<Vec<_>>();
1008+ for data in &mut self.lanes {
1009+ for c in data.clips.iter_mut().filter(|c| c.link == Some(id)) {
1010+ c.link = fresh.pop();
1011+ }
1012+ }
1013+ debug_assert!(links_are_consistent(&self.lanes).is_ok());
1014+ true
1015+ }
1016+
1017+ /// Put two clips on two lanes into one group, so what moves one moves the
1018+ /// other again -- the undo of a [`ungroup`](Project::ungroup) by hand, and
1019+ /// how a picture is regrouped with sound it was never opened with. Whatever
1020+ /// either of them was grouped with comes along: those clips cover this same
1021+ /// span already, so the result is one group and not two overlapping ones.
1022+ ///
1023+ /// Same frames or nothing: a group id names **one span** on however many
1024+ /// lanes ([`links_are_consistent`] refuses to load anything else), so two
1025+ /// clips that do not cover the same frames cannot be one take, and the
1026+ /// refusal says which bounds to trim to. Kinds are *not* checked: a take may
1027+ /// run on `V1` and `V2` at once, and picture-with-sound is the case people
1028+ /// mean, not the rule.
1029+ ///
1030+ /// Metadata only, one snapshot. The error says what is wrong, because
1031+ /// `false` would not: a bad index, one lane twice (a group is at most one
1032+ /// clip per lane), spans that disagree, and a pair that is already one take
1033+ /// -- nothing changes, and none of them costs an undo step.
1034+ pub fn group(&mut self, a: Lane, a_idx: usize, b: Lane, b_idx: usize) -> crate::Result<()> {
1035+ if a == b {
1036+ return Err(format!(
1037+ "a group is one clip per lane: pick the clip to group with on another track, not a second one on {}",
1038+ a.label()
1039+ )
1040+ .into());
1041+ }
1042+ let clip = |p: &Self, lane: Lane, idx: usize| -> crate::Result<Clip> {
1043+ p.lane(lane)
1044+ .get(idx)
1045+ .copied()
1046+ .ok_or_else(|| format!("there is no clip {idx} on {}", lane.label()).into())
1047+ };
1048+ let (x, y) = (clip(self, a, a_idx)?, clip(self, b, b_idx)?);
1049+ if (x.start, x.end()) != (y.start, y.end()) {
1050+ return Err(format!(
1051+ "{} covers [{}, {}) and {} covers [{}, {}): trim them to matching bounds first",
1052+ a.label(),
1053+ x.start,
1054+ x.end(),
1055+ b.label(),
1056+ y.start,
1057+ y.end()
1058+ )
1059+ .into());
1060+ }
1061+ if x.link.is_some() && x.link == y.link {
1062+ return Err("those two are one take already".into());
1063+ }
1064+ self.snapshot();
1065+ let id = self.new_link();
1066+ // Every clip either of them was grouped with, by the same rule: all of
1067+ // them cover this span, so one id over the lot stays consistent.
1068+ let old = [x.link, y.link];
1069+ for data in &mut self.lanes {
1070+ for c in data.clips.iter_mut().filter(|c| c.link.is_some()) {
1071+ if old.contains(&c.link) {
1072+ c.link = Some(id);
1073+ }
1074+ }
1075+ }
1076+ // And the two themselves, which a `place` may have left in no group.
1077+ self.lane_mut(a).expect("read above")[a_idx].link = Some(id);
1078+ self.lane_mut(b).expect("read above")[b_idx].link = Some(id);
1079+ debug_assert!(links_are_consistent(&self.lanes).is_ok());
1080+ Ok(())
1081+ }
1082+
974 /// Place `clip` in one lane at `timeline_frame`, overwriting whatever it1083 /// Place `clip` in one lane at `timeline_frame`, overwriting whatever it
975 /// lands on and leaving every other clip exactly where it is -- the1084 /// lands on and leaving every other clip exactly where it is -- the
976 /// per-lane paste. Anything already there is trimmed away (and split in two1085 /// per-lane paste. Anything already there is trimmed away (and split in two
@@ -1671,6 +1780,103 @@ mod tests {
1671 );1780 );
1672 }1781 }
1673 1782
1783+ /// The music video's own path: the take comes apart into halves that carry
1784+ /// ids of their own, one undo puts it back, and two clips over the same
1785+ /// frames become one take again by hand.
1786+ #[test]
1787+ fn a_take_comes_apart_and_goes_back_together() {
1788+ let mut p = Project::single(FILE, 9);
1789+ let one = p.clips()[0].link.expect("a fresh project is one take");
1790+ assert_eq!(p.lane(Lane::A1)[0].link, Some(one));
1791+
1792+ // A third lane in the same group -- and the merge case with it: a
1793+ // placement is in no group, and grouping it in leaves *one* group.
1794+ let v2 = p.add_lane(LaneKind::Video);
1795+ assert!(p.place(v2, 0, clip(0, 0, 9, 0)));
1796+ assert!(p.lane(v2)[0].link.is_none(), "a placement joins no group");
1797+ p.group(Lane::V1, 0, v2, 0)
1798+ .expect("the same frames, one lane over");
1799+ let id = p.clips()[0].link.expect("still a group");
1800+ assert!(
1801+ p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)),
1802+ "one id over the three, not two groups sharing a span"
1803+ );
1804+ links_are_consistent(&p.lanes).expect("one id, one span");
1805+
1806+ // Detached by the sound: every half of the group, not only the two.
1807+ assert!(p.ungroup(Lane::A1, 0));
1808+ let ids: Vec<Option<u32>> = p.lanes().into_iter().map(|l| p.lane(l)[0].link).collect();
1809+ assert!(
1810+ ids.iter().all(Option::is_some),
1811+ "a half keeps an id of its own"
1812+ );
1813+ assert!(
1814+ ids.iter()
1815+ .enumerate()
1816+ .all(|(i, a)| ids[..i].iter().all(|b| a != b)),
1817+ "and no two halves are the same group any more: {ids:?}"
1818+ );
1819+ links_are_consistent(&p.lanes).expect("a lone id is legal");
1820+
1821+ // One snapshot for the whole detach.
1822+ assert!(p.undo());
1823+ assert!(
1824+ p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)),
1825+ "one undo puts the whole group back"
1826+ );
1827+
1828+ // And back together by hand: the two the pointer named, and only them.
1829+ assert!(p.ungroup(Lane::V1, 0));
1830+ p.group(Lane::V1, 0, Lane::A1, 0)
1831+ .expect("both cover [0, 9)");
1832+ assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link);
1833+ assert_ne!(
1834+ p.lane(v2)[0].link,
1835+ p.lane(Lane::V1)[0].link,
1836+ "the third half stayed detached"
1837+ );
1838+ links_are_consistent(&p.lanes).expect("one id, one span");
1839+ }
1840+
1841+ /// What a group may not be, and what a detach has nothing to take apart --
1842+ /// none of which may cost an undo step.
1843+ #[test]
1844+ fn a_group_is_one_span_and_one_clip_per_lane() {
1845+ let mut p = three();
1846+ let before = shape(&p);
1847+ let why = |r: crate::Result<()>| r.expect_err("refused").to_string();
1848+
1849+ assert!(
1850+ why(p.group(Lane::V1, 0, Lane::V1, 1)).contains("one clip per lane"),
1851+ "two clips of one lane are never one take"
1852+ );
1853+ assert!(why(p.group(Lane::V1, 0, Lane::A1, 9)).contains("there is no clip 9 on A1"));
1854+ let spans = why(p.group(Lane::V1, 0, Lane::A1, 1));
1855+ assert!(
1856+ spans.contains("V1 covers [0, 3) and A1 covers [3, 5)"),
1857+ "{spans}"
1858+ );
1859+ assert!(
1860+ spans.contains("trim them to matching bounds first"),
1861+ "{spans}"
1862+ );
1863+ assert!(why(p.group(Lane::V1, 0, Lane::A1, 0)).contains("one take already"));
1864+
1865+ // A half nothing else is grouped with is already detached, a placement
1866+ // is in no group at all, and an index that is not there has nothing to
1867+ // detach either.
1868+ assert!(p.place(Lane::V1, 20, clip(20, 0, 3, 0)));
1869+ assert!(!p.ungroup(Lane::V1, 3), "a placement is in no group");
1870+ assert!(p.lift(Lane::A1, 0));
1871+ assert!(!p.ungroup(Lane::V1, 0), "its group has no other half");
1872+ assert!(!p.ungroup(Lane::V1, 9), "no clip there");
1873+
1874+ // Two undos, one per edit: not one of the refusals pushed a snapshot.
1875+ assert!(p.undo());
1876+ assert!(p.undo());
1877+ assert_eq!(shape(&p), before);
1878+ }
1879+
1674 #[test]1880 #[test]
1675 fn split_refused_at_zero_boundary_end_and_in_a_gap() {1881 fn split_refused_at_zero_boundary_end_and_in_a_gap() {
1676 let mut p = three();1882 let mut p = three();