forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-af22c4fbc9909ec45'

Browse files at this commit

ca3d7deeb70cb42caadf735d471d130bce36f2c1Ahmet Kaan GümüşParent: a3e9dc2ccd1ffa
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+228 -8
@@ -258,11 +258,13 @@ struct ContextMenu {
258 /// action a stroke already reaches -- the menu is a second way *to* the actions258 /// action a stroke already reaches -- the menu is a second way *to* the actions
259 /// and never a second version of them -- so both the label and the hint come259 /// and never a second version of them -- so both the label and the hint come
260 /// out of the keymap registry and the two can never disagree.260 /// out of the keymap registry and the two can never disagree.
261-const MENU_ITEMS: [ActionId; 8] = [261+const MENU_ITEMS: [ActionId; 10] = [
262 ActionId::Cut,262 ActionId::Cut,
263 ActionId::Delete,263 ActionId::Delete,
264 ActionId::Lift,264 ActionId::Lift,
265 ActionId::Regroup,265 ActionId::Regroup,
266+ ActionId::Detach,
267+ ActionId::Group,
266 ActionId::Equalizer,268 ActionId::Equalizer,
267 ActionId::Color,269 ActionId::Color,
268 ActionId::Fit,270 ActionId::Fit,
@@ -626,6 +628,8 @@ impl Player {
626 ActionId::Paste => self.paste(cx),628 ActionId::Paste => self.paste(cx),
627 ActionId::Cut => self.cut(cx),629 ActionId::Cut => self.cut(cx),
628 ActionId::Regroup => self.regroup(cx),630 ActionId::Regroup => self.regroup(cx),
631+ ActionId::Detach => self.detach(cx),
632+ ActionId::Group => self.group(cx),
629 ActionId::Select => self.select_under_playhead(cx),633 ActionId::Select => self.select_under_playhead(cx),
630 ActionId::SelectNext => self.select_step(true, cx),634 ActionId::SelectNext => self.select_step(true, cx),
631 ActionId::SelectPrev => self.select_step(false, cx),635 ActionId::SelectPrev => self.select_step(false, cx),
@@ -1084,6 +1088,63 @@ impl Player {
1084 cx.notify();1088 cx.notify();
1085 }1089 }
1086 1090
1091+ /// Takes the selected clip out of its group, so the picture and the sound
1092+ /// under it are edited apart from here on: each half selects, moves, trims
1093+ /// and is removed alone, and both draw outlined instead of tinted. The
1094+ /// selection stays -- the half that was clicked is still the half in hand.
1095+ fn detach(&mut self, cx: &mut Context<Self>) {
1096+ if self.exporting().is_some() {
1097+ return;
1098+ }
1099+ match (&mut self.session, self.selected) {
1100+ (Some(session), Some((lane, idx))) => {
1101+ if !session.ungroup(lane, idx) {
1102+ self.notice =
1103+ Some("NOTHING DETACHED — that clip is not grouped with another".into());
1104+ }
1105+ }
1106+ (Some(_), None) => {
1107+ self.notice = Some("NOTHING DETACHED — click the take to take apart first".into())
1108+ }
1109+ (None, _) => {}
1110+ }
1111+ cx.notify();
1112+ }
1113+
1114+ /// Puts the selected clip back in a group with the clip covering exactly the
1115+ /// same frames on another track -- the way back from [`Player::detach`], and
1116+ /// the way to group a picture with sound it was never opened with. The
1117+ /// partner is not clicked because there is nothing to choose: a group id
1118+ /// names one span, so only a clip covering these very frames could join it,
1119+ /// and the engine words what to do when none does.
1120+ fn group(&mut self, cx: &mut Context<Self>) {
1121+ if self.exporting().is_some() {
1122+ return;
1123+ }
1124+ let partner = match (&self.session, self.selected) {
1125+ (Some(session), Some((lane, idx))) => span_partner(session, lane, idx),
1126+ _ => None,
1127+ };
1128+ match (&mut self.session, self.selected, partner) {
1129+ (Some(session), Some((lane, idx)), Some((other, o_idx))) => {
1130+ if let Err(e) = session.group(lane, idx, other, o_idx) {
1131+ self.notice = Some(format!("NOT GROUPED — {e}").into());
1132+ }
1133+ }
1134+ (Some(_), Some(_), None) => {
1135+ self.notice = Some(
1136+ "NOTHING TO GROUP WITH — no clip on another track covers exactly these frames"
1137+ .into(),
1138+ )
1139+ }
1140+ (Some(_), None, _) => {
1141+ self.notice = Some("NOTHING GROUPED — click one of the halves first".into())
1142+ }
1143+ (None, ..) => {}
1144+ }
1145+ cx.notify();
1146+ }
1147+
1087 /// Drops the selected clip and closes the hole: a whole take goes, both1148 /// Drops the selected clip and closes the hole: a whole take goes, both
1088 /// lanes of it, and everything after it moves up. A half with no take under1149 /// lanes of it, and everything after it moves up. A half with no take under
1089 /// it in the video lane -- what a lift leaves behind -- has nothing to1150 /// it in the video lane -- what a lift leaves behind -- has nothing to
@@ -4287,6 +4348,10 @@ fn applicable(clip: &Clip, lane: Lane, action: ActionId, playhead: u32) -> bool
4287 // at an edge of this clip. Whether those two halves were ever one take4348 // at an edge of this clip. Whether those two halves were ever one take
4288 // is the engine's question, and it words that refusal itself.4349 // is the engine's question, and it words that refusal itself.
4289 ActionId::Regroup => playhead == clip.start || playhead == clip.end(),4350 ActionId::Regroup => playhead == clip.start || playhead == clip.end(),
4351+ // Nothing to take apart in a clip that names no group at all. Whether
4352+ // the group it names still has another half is the engine's question,
4353+ // like the regroup above, and it words that refusal itself.
4354+ ActionId::Detach => clip.link.is_some(),
4290 _ => true,4355 _ => true,
4291 }4356 }
4292 }4357 }
@@ -4810,18 +4875,55 @@ fn whole_take(session: &PlaybackSession, lane: Lane, idx: usize) -> bool {
4810 let Some(clip) = session.lane_clips(lane).get(idx) else {4875 let Some(clip) = session.lane_clips(lane).get(idx) else {
4811 return false;4876 return false;
4812 };4877 };
4878+ let paired = || {
4879+ session
4880+ .lanes()
4881+ .into_iter()
4882+ .filter(|&other| other != lane)
4883+ .flat_map(|other| session.lane_clips(other))
4884+ .any(|o| o.link.is_some() && o.link == clip.link)
4885+ };
4813 match (lane.kind, lane.ord) {4886 match (lane.kind, lane.ord) {
4814 (_, 1..) => false,4887 (_, 1..) => false,
4815- (LaneKind::Video, _) => true,4888+ // The picture of a take -- unless the take has been taken apart: a
4889+ // detached picture (a group id no other lane carries, which is also what
4890+ // a lift of the sound leaves) is a half like the sound is, and a ripple
4891+ // under it would drag away the very half it was detached from. A clip in
4892+ // no group at all is not a half but a placement, and on `V1` a placement
4893+ // is the take there is.
4894+ (LaneKind::Video, _) => clip.link.is_none() || paired(),
4816 // The sound of a take, only while the take is still there: its group is4895 // The sound of a take, only while the take is still there: its group is
4817 // carried by a clip on some other lane.4896 // carried by a clip on some other lane.
4818- (LaneKind::Audio, _) => session4897+ (LaneKind::Audio, _) => paired(),
4898+ }
4899+}
4900+
4901+/// The clip a Group would pair this one with: the first clip on another track,
4902+/// in the order the lanes are drawn, covering exactly the same frames and not in
4903+/// this clip's group already. Exactly the same frames because that is all a
4904+/// group id can mean (engine `links_are_consistent`), which is what leaves
4905+/// nothing for a second click to choose. `None` when no track has one, and the
4906+/// notice says so.
4907+fn span_partner(session: &PlaybackSession, lane: Lane, idx: usize) -> Option<(Lane, usize)> {
4908+ let clip = *session.lane_clips(lane).get(idx)?;
4909+ let matches = |other: Lane| {
4910+ let i = session.lane_clips(other).iter().position(|c| {
4911+ (c.start, c.end()) == (clip.start, clip.end())
4912+ && !(c.link.is_some() && c.link == clip.link)
4913+ })?;
4914+ Some((other, i))
4915+ };
4916+ // Sound before picture (and picture before sound): "group this" means the
4917+ // other half of the take, and a project whose audio lane was added after a
4918+ // second video one has that half *after* the layer in storage order -- which
4919+ // is the order the lanes come in. A same-kind lane is still groupable (V1
4920+ // and V2 may be one take), but only where no opposite one covers the span.
4921+ let (opposite, same): (Vec<Lane>, Vec<Lane>) = session
4819 .lanes()4922 .lanes()
4820 .into_iter()4923 .into_iter()
4821 .filter(|&other| other != lane)4924 .filter(|&other| other != lane)
4822- .flat_map(|other| session.lane_clips(other))4925+ .partition(|other| other.kind != lane.kind);
4823- .any(|o| o.link.is_some() && o.link == clip.link),4926+ opposite.into_iter().chain(same).find_map(matches)
4824- }
4825 }4927 }
4826 4928
4827 /// Whether a click marks this clip: the clip that was clicked always, and the4929 /// Whether a click marks this clip: the clip that was clicked always, and the
@@ -5228,8 +5330,8 @@ mod tests {
5228 envelope, eq_x, eq_y, export_path, export_settings, format_line, frac_along, frac_down,5330 envelope, eq_x, eq_y, export_path, export_settings, format_line, frac_along, frac_down,
5229 frame_at, histogram, is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked,5331 frame_at, histogram, is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked,
5230 menu_at, mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due,5332 menu_at, mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due,
5231- show_label, source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources,5333+ show_label, source_frames, source_tint, span_partner, start_frac, timecode, unseen_paths,
5232- whole_take, width_frac, window_title,5334+ unseen_sources, whole_take, width_frac, window_title,
5233 };5335 };
5234 use super::{file_name, file_uri, library_rows};5336 use super::{file_name, file_uri, library_rows};
5235 5337
@@ -5529,6 +5631,16 @@ mod tests {
5529 assert!(applicable(&clip, v1, ActionId::Regroup, 30));5631 assert!(applicable(&clip, v1, ActionId::Regroup, 30));
5530 assert!(applicable(&clip, v1, ActionId::Regroup, 90));5632 assert!(applicable(&clip, v1, ActionId::Regroup, 90));
5531 assert!(!applicable(&clip, v1, ActionId::Regroup, 60));5633 assert!(!applicable(&clip, v1, ActionId::Regroup, 60));
5634+ // Detach is the clip's own business: nothing to take apart in one that
5635+ // names no group, and whether the group still has another half is the
5636+ // engine's question. Group is offered on every clip, for that reason.
5637+ assert!(!applicable(&clip, v1, ActionId::Detach, 0));
5638+ let grouped = Clip {
5639+ link: Some(3),
5640+ ..clip
5641+ };
5642+ assert!(applicable(&grouped, v1, ActionId::Detach, 60));
5643+ assert!(applicable(&clip, a1, ActionId::Group, 0));
5532 // The equalizer is the one item the *lane* decides: it filters samples,5644 // The equalizer is the one item the *lane* decides: it filters samples,
5533 // and a video clip has none of its own. Never the playhead's business.5645 // and a video clip has none of its own. Never the playhead's business.
5534 assert!(applicable(&clip, a1, ActionId::Equalizer, 0));5646 assert!(applicable(&clip, a1, ActionId::Equalizer, 0));
@@ -5570,6 +5682,114 @@ mod tests {
5570 }5682 }
5571 }5683 }
5572 5684
5685+ /// What the Detach and Group items do to a real timeline: a music video's
5686+ /// sound comes off its picture, Delete on the loose half takes that half
5687+ /// only, undo puts both back, and Group makes the two one take again --
5688+ /// whole-take delete and all.
5689+ #[test]
5690+ fn a_detached_half_is_removed_alone_and_groups_again() {
5691+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
5692+ session.set_gain(0.0);
5693+ assert!(whole_take(&session, Lane::V1, 0), "one take to start with");
5694+ assert!(whole_take(&session, Lane::A1, 0));
5695+ let frames = session.lane_clips(Lane::V1)[0].len();
5696+
5697+ // Detach audio: neither half is a whole take any more, so Delete on
5698+ // either leaves the other exactly where it is.
5699+ assert!(session.ungroup(Lane::V1, 0));
5700+ assert!(
5701+ !whole_take(&session, Lane::A1, 0),
5702+ "the sound is a half now"
5703+ );
5704+ assert!(!whole_take(&session, Lane::V1, 0), "and so is the picture");
5705+ assert!(session.lift_clip(Lane::A1, 0));
5706+ assert!(session.lane_clips(Lane::A1).is_empty(), "the sound went");
5707+ assert_eq!(session.lane_clips(Lane::V1).len(), 1, "the picture stayed");
5708+ assert_eq!(session.lane_clips(Lane::V1)[0].len(), frames, "untrimmed");
5709+
5710+ // One undo per edit, the removal then the detach.
5711+ assert!(session.undo());
5712+ assert_eq!(session.lane_clips(Lane::A1).len(), 1, "the sound is back");
5713+ assert!(session.undo());
5714+ assert!(whole_take(&session, Lane::A1, 0), "one take again");
5715+
5716+ // Group: the partner is the clip covering these very frames on the
5717+ // other track, which is what the item hands the engine.
5718+ assert!(session.ungroup(Lane::V1, 0));
5719+ assert_eq!(span_partner(&session, Lane::V1, 0), Some((Lane::A1, 0)));
5720+ session
5721+ .group(Lane::V1, 0, Lane::A1, 0)
5722+ .expect("both halves still cover the same frames");
5723+ assert!(
5724+ whole_take(&session, Lane::A1, 0),
5725+ "a take that ripples again"
5726+ );
5727+ assert_eq!(
5728+ span_partner(&session, Lane::V1, 0),
5729+ None,
5730+ "and nothing left on another track to group with"
5731+ );
5732+ }
5733+
5734+ /// Which clip Group reaches when more than one covers the span: the sound,
5735+ /// whatever order the lanes are stored in. A project file may hold them in
5736+ /// any order -- a video layer *before* the audio lane among them -- and
5737+ /// "group this" means the other half of the take, never the layer above it.
5738+ #[test]
5739+ fn group_reaches_the_sound_before_a_video_layer_over_it() {
5740+ use engine::project::LaneKind;
5741+
5742+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
5743+ session.set_gain(0.0);
5744+ let v2 = session.add_lane(LaneKind::Video);
5745+ let path = session.sources()[0].path.clone();
5746+ let frames = source_frames(lane_clips(&session), session.sources(), &path);
5747+ assert!(
5748+ session
5749+ .place_stream_at(0.0, &path, 0, frames, Some(v2))
5750+ .expect("its own file is on this timeline"),
5751+ "a layer covering the same frames as the take"
5752+ );
5753+
5754+ // Saved and loaded back with the lanes in the order a hand-written
5755+ // project may hold them: the sound last, behind the layer.
5756+ let dir = std::env::temp_dir().join(format!("ve_group_{}", std::process::id()));
5757+ std::fs::create_dir_all(&dir).expect("a scratch directory");
5758+ let file = dir.join("lanes.edith");
5759+ session.save_project(&file).expect("save the project");
5760+ let text = std::fs::read_to_string(&file).expect("read it back");
5761+ let (sound, rest): (Vec<&str>, Vec<&str>) =
5762+ text.lines().partition(|l| l.starts_with("audio "));
5763+ std::fs::write(
5764+ &file,
5765+ format!("{}\n{}\n", rest.join("\n"), sound.join("\n")),
5766+ )
5767+ .expect("write the reordered project");
5768+ let mut session = PlaybackSession::open_project(&file).expect("it loads as it stands");
5769+ std::fs::remove_dir_all(&dir).ok();
5770+ assert_eq!(
5771+ session.lanes(),
5772+ vec![Lane::V1, v2, Lane::A1],
5773+ "the sound is the last lane there"
5774+ );
5775+
5776+ // Detached, so Group has a choice to get wrong: the layer covers these
5777+ // frames too, and it is the lane the walk meets first.
5778+ assert!(session.ungroup(Lane::V1, 0));
5779+ // Group on the picture reaches the sound, not that layer.
5780+ assert_eq!(span_partner(&session, Lane::V1, 0), Some((Lane::A1, 0)));
5781+ session
5782+ .group(Lane::V1, 0, Lane::A1, 0)
5783+ .expect("the two halves cover the same frames");
5784+ // ...and a lane of its own kind is still groupable, once the sound is
5785+ // spoken for: two video lanes may be one take.
5786+ assert_eq!(
5787+ span_partner(&session, Lane::V1, 0),
5788+ Some((v2, 0)),
5789+ "the layer is what is left to group with"
5790+ );
5791+ }
5792+
5573 /// The refusal path, end to end against the real files: an incompatible5793 /// The refusal path, end to end against the real files: an incompatible
5574 /// import changes nothing, and the library mirrors `sources()` 1:1, so a5794 /// import changes nothing, and the library mirrors `sources()` 1:1, so a
5575 /// refused file cannot leave a row behind.5795 /// refused file cannot leave a row behind.
crates/engine/src/playback.rsmodified+15 -0
@@ -696,6 +696,21 @@ impl PlaybackSession {
696 .regroup(secs_to_frame(timeline_secs, self.meta.frame_rate))696 .regroup(secs_to_frame(timeline_secs, self.meta.frame_rate))
697 }697 }
698 698
699+ /// Takes that clip out of its group, so its picture and its sound are edited
700+ /// apart from here on. Metadata only like a cut -- no reseek -- and one undo
701+ /// step. `false` for a bad index and for a clip that is not grouped with
702+ /// anything, which is already detached.
703+ pub fn ungroup(&mut self, lane: Lane, idx: usize) -> bool {
704+ self.project.ungroup(lane, idx)
705+ }
706+
707+ /// Puts two clips covering the same frames back into one group: the regroup
708+ /// of what [`ungroup`](Self::ungroup) took apart. Metadata only, one undo
709+ /// step, and the error says why when it refuses.
710+ pub fn group(&mut self, a: Lane, a_idx: usize, b: Lane, b_idx: usize) -> crate::Result<()> {
711+ self.project.group(a, a_idx, b, b_idx)
712+ }
713+
699 /// What the clip at `idx` of `lane` is equalized with, or `None` for one714 /// What the clip at `idx` of `lane` is equalized with, or `None` for one
700 /// that plays flat -- what a card shows before it lets anyone drag a band.715 /// that plays flat -- what a card shows before it lets anyone drag a band.
701 pub fn eq_of(&self, lane: Lane, idx: usize) -> Option<&EqParams> {716 pub fn eq_of(&self, lane: Lane, idx: usize) -> Option<&EqParams> {
crates/engine/src/project.rsmodified+206 -0
@@ -1002,6 +1002,115 @@ impl Project {
1002 true1002 true
1003 }1003 }
1004 1004
1005+ /// Take the clip at `idx` of `lane` out of its group: every clip carrying
1006+ /// its id -- on however many lanes -- is handed an id of its own, so from
1007+ /// here on each half moves, trims and is deleted alone. The music video
1008+ /// whose sound is to be cut against its picture starts here.
1009+ ///
1010+ /// An id of its own rather than none at all: a half no other lane is grouped
1011+ /// with is exactly what a [`lift`](Project::lift) leaves behind and is legal
1012+ /// (see [`links_are_consistent`]), and it is what a front-end already draws
1013+ /// as detached. `None` would instead say "was never part of a take", which
1014+ /// is what a one-lane [`place`](Project::place) means.
1015+ ///
1016+ /// Metadata only, like a [`split`](Project::split): no mapping changes, so
1017+ /// nothing has to be reseeked. One snapshot, so one [`Project::undo`] puts
1018+ /// the group back. Refused (`false`, nothing changed) for an index that is
1019+ /// not there, a clip in no group at all, and one whose group has no other
1020+ /// half -- that one is already detached, and a refusal must not cost an undo
1021+ /// step.
1022+ pub fn ungroup(&mut self, lane: Lane, idx: usize) -> bool {
1023+ let Some(id) = self.lane(lane).get(idx).and_then(|c| c.link) else {
1024+ return false;
1025+ };
1026+ let members = self
1027+ .lanes
1028+ .iter()
1029+ .flat_map(|l| &l.clips)
1030+ .filter(|c| c.link == Some(id))
1031+ .count();
1032+ if members < 2 {
1033+ return false;
1034+ }
1035+ self.snapshot();
1036+ // Drawn before the walk: `new_link` takes the whole project, and the
1037+ // walk holds the lanes.
1038+ let mut fresh = (0..members).map(|_| self.new_link()).collect::<Vec<_>>();
1039+ for data in &mut self.lanes {
1040+ for c in data.clips.iter_mut().filter(|c| c.link == Some(id)) {
1041+ c.link = fresh.pop();
1042+ }
1043+ }
1044+ debug_assert!(links_are_consistent(&self.lanes).is_ok());
1045+ true
1046+ }
1047+
1048+ /// Put two clips on two lanes into one group, so what moves one moves the
1049+ /// other again -- the undo of a [`ungroup`](Project::ungroup) by hand, and
1050+ /// how a picture is regrouped with sound it was never opened with. Whatever
1051+ /// either of them was grouped with comes along: those clips cover this same
1052+ /// span already, so the result is one group and not two overlapping ones.
1053+ ///
1054+ /// Same frames or nothing: a group id names **one span** on however many
1055+ /// lanes ([`links_are_consistent`] refuses to load anything else), so two
1056+ /// clips that do not cover the same frames cannot be one take, and the
1057+ /// refusal says which bounds to trim to. Kinds are *not* checked: a take may
1058+ /// run on `V1` and `V2` at once, and picture-with-sound is the case people
1059+ /// mean, not the rule.
1060+ ///
1061+ /// Metadata only, one snapshot. The error says what is wrong, because
1062+ /// `false` would not: a bad index, one lane twice (a group is at most one
1063+ /// clip per lane), spans that disagree, and a pair that is already one take
1064+ /// -- nothing changes, and none of them costs an undo step.
1065+ pub fn group(&mut self, a: Lane, a_idx: usize, b: Lane, b_idx: usize) -> crate::Result<()> {
1066+ if a == b {
1067+ return Err(format!(
1068+ "a group is one clip per lane: pick the clip to group with on another track, not a second one on {}",
1069+ a.label()
1070+ )
1071+ .into());
1072+ }
1073+ let clip = |p: &Self, lane: Lane, idx: usize| -> crate::Result<Clip> {
1074+ p.lane(lane)
1075+ .get(idx)
1076+ .copied()
1077+ .ok_or_else(|| format!("there is no clip {idx} on {}", lane.label()).into())
1078+ };
1079+ let (x, y) = (clip(self, a, a_idx)?, clip(self, b, b_idx)?);
1080+ if (x.start, x.end()) != (y.start, y.end()) {
1081+ return Err(format!(
1082+ "{} covers [{}, {}) and {} covers [{}, {}): trim them to matching bounds first",
1083+ a.label(),
1084+ x.start,
1085+ x.end(),
1086+ b.label(),
1087+ y.start,
1088+ y.end()
1089+ )
1090+ .into());
1091+ }
1092+ if x.link.is_some() && x.link == y.link {
1093+ return Err("those two are one take already".into());
1094+ }
1095+ self.snapshot();
1096+ let id = self.new_link();
1097+ // Every clip either of them was grouped with, by the same rule: all of
1098+ // them cover this span, so one id over the lot stays consistent.
1099+ let old = [x.link, y.link];
1100+ for data in &mut self.lanes {
1101+ for c in data.clips.iter_mut().filter(|c| c.link.is_some()) {
1102+ if old.contains(&c.link) {
1103+ c.link = Some(id);
1104+ }
1105+ }
1106+ }
1107+ // And the two themselves, which a `place` may have left in no group.
1108+ self.lane_mut(a).expect("read above")[a_idx].link = Some(id);
1109+ self.lane_mut(b).expect("read above")[b_idx].link = Some(id);
1110+ debug_assert!(links_are_consistent(&self.lanes).is_ok());
1111+ Ok(())
1112+ }
1113+
1005 /// Place `clip` in one lane at `timeline_frame`, overwriting whatever it1114 /// Place `clip` in one lane at `timeline_frame`, overwriting whatever it
1006 /// lands on and leaving every other clip exactly where it is -- the1115 /// lands on and leaving every other clip exactly where it is -- the
1007 /// per-lane paste. Anything already there is trimmed away (and split in two1116 /// per-lane paste. Anything already there is trimmed away (and split in two
@@ -1841,6 +1950,103 @@ mod tests {
1841 );1950 );
1842 }1951 }
1843 1952
1953+ /// The music video's own path: the take comes apart into halves that carry
1954+ /// ids of their own, one undo puts it back, and two clips over the same
1955+ /// frames become one take again by hand.
1956+ #[test]
1957+ fn a_take_comes_apart_and_goes_back_together() {
1958+ let mut p = Project::single(FILE, 9);
1959+ let one = p.clips()[0].link.expect("a fresh project is one take");
1960+ assert_eq!(p.lane(Lane::A1)[0].link, Some(one));
1961+
1962+ // A third lane in the same group -- and the merge case with it: a
1963+ // placement is in no group, and grouping it in leaves *one* group.
1964+ let v2 = p.add_lane(LaneKind::Video);
1965+ assert!(p.place(v2, 0, clip(0, 0, 9, 0)));
1966+ assert!(p.lane(v2)[0].link.is_none(), "a placement joins no group");
1967+ p.group(Lane::V1, 0, v2, 0)
1968+ .expect("the same frames, one lane over");
1969+ let id = p.clips()[0].link.expect("still a group");
1970+ assert!(
1971+ p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)),
1972+ "one id over the three, not two groups sharing a span"
1973+ );
1974+ links_are_consistent(&p.lanes).expect("one id, one span");
1975+
1976+ // Detached by the sound: every half of the group, not only the two.
1977+ assert!(p.ungroup(Lane::A1, 0));
1978+ let ids: Vec<Option<u32>> = p.lanes().into_iter().map(|l| p.lane(l)[0].link).collect();
1979+ assert!(
1980+ ids.iter().all(Option::is_some),
1981+ "a half keeps an id of its own"
1982+ );
1983+ assert!(
1984+ ids.iter()
1985+ .enumerate()
1986+ .all(|(i, a)| ids[..i].iter().all(|b| a != b)),
1987+ "and no two halves are the same group any more: {ids:?}"
1988+ );
1989+ links_are_consistent(&p.lanes).expect("a lone id is legal");
1990+
1991+ // One snapshot for the whole detach.
1992+ assert!(p.undo());
1993+ assert!(
1994+ p.lanes().into_iter().all(|l| p.lane(l)[0].link == Some(id)),
1995+ "one undo puts the whole group back"
1996+ );
1997+
1998+ // And back together by hand: the two the pointer named, and only them.
1999+ assert!(p.ungroup(Lane::V1, 0));
2000+ p.group(Lane::V1, 0, Lane::A1, 0)
2001+ .expect("both cover [0, 9)");
2002+ assert_eq!(p.lane(Lane::V1)[0].link, p.lane(Lane::A1)[0].link);
2003+ assert_ne!(
2004+ p.lane(v2)[0].link,
2005+ p.lane(Lane::V1)[0].link,
2006+ "the third half stayed detached"
2007+ );
2008+ links_are_consistent(&p.lanes).expect("one id, one span");
2009+ }
2010+
2011+ /// What a group may not be, and what a detach has nothing to take apart --
2012+ /// none of which may cost an undo step.
2013+ #[test]
2014+ fn a_group_is_one_span_and_one_clip_per_lane() {
2015+ let mut p = three();
2016+ let before = shape(&p);
2017+ let why = |r: crate::Result<()>| r.expect_err("refused").to_string();
2018+
2019+ assert!(
2020+ why(p.group(Lane::V1, 0, Lane::V1, 1)).contains("one clip per lane"),
2021+ "two clips of one lane are never one take"
2022+ );
2023+ assert!(why(p.group(Lane::V1, 0, Lane::A1, 9)).contains("there is no clip 9 on A1"));
2024+ let spans = why(p.group(Lane::V1, 0, Lane::A1, 1));
2025+ assert!(
2026+ spans.contains("V1 covers [0, 3) and A1 covers [3, 5)"),
2027+ "{spans}"
2028+ );
2029+ assert!(
2030+ spans.contains("trim them to matching bounds first"),
2031+ "{spans}"
2032+ );
2033+ assert!(why(p.group(Lane::V1, 0, Lane::A1, 0)).contains("one take already"));
2034+
2035+ // A half nothing else is grouped with is already detached, a placement
2036+ // is in no group at all, and an index that is not there has nothing to
2037+ // detach either.
2038+ assert!(p.place(Lane::V1, 20, clip(20, 0, 3, 0)));
2039+ assert!(!p.ungroup(Lane::V1, 3), "a placement is in no group");
2040+ assert!(p.lift(Lane::A1, 0));
2041+ assert!(!p.ungroup(Lane::V1, 0), "its group has no other half");
2042+ assert!(!p.ungroup(Lane::V1, 9), "no clip there");
2043+
2044+ // Two undos, one per edit: not one of the refusals pushed a snapshot.
2045+ assert!(p.undo());
2046+ assert!(p.undo());
2047+ assert_eq!(shape(&p), before);
2048+ }
2049+
1844 #[test]2050 #[test]
1845 fn split_refused_at_zero_boundary_end_and_in_a_gap() {2051 fn split_refused_at_zero_boundary_end_and_in_a_gap() {
1846 let mut p = three();2052 let mut p = three();