edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! Lane A's timeline-editing verbs: close gap, freeze frame, paste attributes, |
| 2 | //! match frame, replace edit, select forward, split edits, nudge and the box |
| 3 | //! select. The window has no `TestAppContext`, so each is driven through the |
| 4 | //! session-level function the `Player` method calls, plus the oracle row, the |
| 5 | //! default stroke and the menu door. |
| 6 | |
| 7 | use super::*; |
| 8 | use crate::player::editkeys::{ |
| 9 | Part, forward_picks, gap_under, look_of, marquee_picks, matched_source, nudge, paste_look, replace_plan, |
| 10 | split_edit_in, still_name, |
| 11 | }; |
| 12 | use crate::{Marquee, Player, Selection}; |
| 13 | use engine::color::ColorParams; |
| 14 | use keymap::ActionId; |
| 15 | |
| 16 | /// A one-take session: `test_av.mp4`, V1 and A1 linked, 150 frames at 30 fps. |
| 17 | fn av() -> PlaybackSession { |
| 18 | let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); |
| 19 | session.set_gain(0.0); |
| 20 | session |
| 21 | } |
| 22 | |
| 23 | /// The take cut in two at frame 60 and the second half moved 30 frames later, |
| 24 | /// leaving a gap `60..90` on both lanes. |
| 25 | fn av_with_gap() -> PlaybackSession { |
| 26 | let mut session = av(); |
| 27 | assert!(session.cut_at(2.0)); |
| 28 | let (lane, idx) = (Lane::V1, 1); |
| 29 | assert!(session.move_clip_to(lane, idx, lane, 90)); |
| 30 | session |
| 31 | } |
| 32 | |
| 33 | fn live(action: ActionId, ctx: Ctx) -> Enable { |
| 34 | enable(action, Ctx { timeline: true, playable: true, ..ctx }) |
| 35 | } |
| 36 | |
| 37 | fn key_of(action: ActionId) -> Vec<String> { |
| 38 | keymap::Keymap::defaults() |
| 39 | .entries() |
| 40 | .iter() |
| 41 | .filter(|b| b.action == action) |
| 42 | .map(|b| b.chord.pretty()) |
| 43 | .collect() |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn close_gap_closes_the_gap_under_the_playhead_in_one_undo_step() { |
| 48 | let mut session = av_with_gap(); |
| 49 | assert_eq!(gap_under(&session, 30), None, "inside a clip is no gap"); |
| 50 | let (lane, start, frames) = gap_under(&session, 75).expect("the playhead stands in the hole"); |
| 51 | assert_eq!((lane, start, frames), (Lane::V1, 60, 30)); |
| 52 | let scope = session.gap_take_scope(lane, start, frames).expect("a scope"); |
| 53 | let depth = session.undo_depth(); |
| 54 | session.cut_regions(&[(start, frames)], &scope).expect("the hole closes"); |
| 55 | assert_eq!(session.undo_depth(), depth + 1, "one undo step"); |
| 56 | assert_eq!(session.lane_clips(Lane::V1)[1].start, 60); |
| 57 | assert_eq!(session.lane_clips(Lane::A1)[1].start, 60, "the linked sound closed with it"); |
| 58 | assert_eq!(gap_under(&session, 75), None); |
| 59 | assert!(session.undo()); |
| 60 | assert_eq!(session.lane_clips(Lane::V1)[1].start, 90); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn close_gap_is_dimmed_with_a_reason_when_no_gap_is_there() { |
| 65 | assert_eq!(live(ActionId::CloseGap, Ctx::default()), Enable::No("no gap under the playhead")); |
| 66 | assert_eq!(live(ActionId::CloseGap, Ctx { gap_here: true, ..Ctx::default() }), Enable::Yes); |
| 67 | assert_eq!(key_of(ActionId::CloseGap), ["Shift+Delete"]); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn freeze_frame_holds_the_frame_and_pushes_the_rest_in_one_undo_step() { |
| 72 | let mut session = av(); |
| 73 | assert!(session.cut_at(2.0)); |
| 74 | let (proj, meta) = session.export_snapshot(); |
| 75 | let frame = engine::export::compose_at(&proj, &meta, 30, meta.width, meta.height).expect("a frame"); |
| 76 | let dir = std::env::temp_dir().join(format!("edith-freeze-{}", std::process::id())); |
| 77 | std::fs::create_dir_all(&dir).unwrap(); |
| 78 | let png = dir.join("hold.png"); |
| 79 | std::fs::write(&png, engine::decode::to_png_bytes(&frame).unwrap()).unwrap(); |
| 80 | let before = session.lane_clips(Lane::V1).to_vec(); |
| 81 | let depth = session.undo_depth(); |
| 82 | let (at, dur) = (30, 60); |
| 83 | session.begin_batch(); |
| 84 | let moved = Player::freeze_edit(&mut session, at, dur, 30., Lane::V1, &png).expect("the freeze lands"); |
| 85 | assert!(moved, "the clips after the hold moved, and the toast says so"); |
| 86 | session.end_batch(); |
| 87 | assert_eq!(session.undo_depth(), depth + 1, "one undo step"); |
| 88 | let v1 = session.lane_clips(Lane::V1); |
| 89 | assert_eq!(v1.len(), 4, "the first half is cut at the frame, the still sits between the halves"); |
| 90 | assert_eq!((v1[1].start, v1[1].frames()), (30, 60), "the still holds the frame for the duration"); |
| 91 | assert_eq!(v1[2].start, 90, "the cut-off half of the first clip moved later by the hold"); |
| 92 | assert_eq!(v1[3].start, before[1].start + dur, "everything after moves later"); |
| 93 | assert!(engine::is_image(&session.sources()[v1[1].source].path)); |
| 94 | assert!(session.undo()); |
| 95 | assert_eq!(session.lane_clips(Lane::V1).len(), before.len(), "undo takes the whole freeze back"); |
| 96 | let _ = std::fs::remove_dir_all(&dir); |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn freeze_frame_needs_a_clip_under_the_playhead_and_has_a_key() { |
| 101 | assert_eq!(live(ActionId::Freeze, Ctx::default()), Enable::No("nothing under the playhead")); |
| 102 | assert_eq!(live(ActionId::Freeze, Ctx { clip_here: true, ..Ctx::default() }), Enable::Yes); |
| 103 | assert_eq!(key_of(ActionId::Freeze), ["Shift+R"]); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn paste_attributes_carries_colour_speed_and_audio_in_one_undo_step() { |
| 108 | let mut session = av(); |
| 109 | assert!(session.cut_at(2.0)); |
| 110 | let grade = ColorParams { brightness: 0.25, ..ColorParams::default() }; |
| 111 | assert!(session.set_color(Lane::V1, 0, Some(grade))); |
| 112 | assert!(session.set_clip_gain(Lane::A1, 0, 6.)); |
| 113 | let look = look_of(&session, (Lane::V1, 0)).expect("a look"); |
| 114 | let depth = session.undo_depth(); |
| 115 | // Onto the second take, picked by its picture half: the sound half takes the audio part. |
| 116 | let done = paste_look(&mut session, &look, &[(Lane::V1, 1)], &[Part::Color, Part::Audio]); |
| 117 | assert_eq!(done.touched, 2); |
| 118 | assert_eq!(done.words, ["colour", "audio"]); |
| 119 | assert_eq!(session.undo_depth(), depth + 1, "colour and audio are one undo step"); |
| 120 | assert_eq!(session.color_of(Lane::V1, 1).copied(), Some(grade)); |
| 121 | assert_eq!(session.clip_gain_of(Lane::A1, 1), Some(6.)); |
| 122 | assert!(session.undo()); |
| 123 | assert_eq!(session.color_of(Lane::V1, 1), None); |
| 124 | assert_eq!(session.clip_gain_of(Lane::A1, 1), Some(0.)); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn paste_attributes_speed_reaches_the_pick_and_effects_say_when_none_apply() { |
| 129 | let mut session = av(); |
| 130 | assert!(session.cut_at(2.0)); |
| 131 | assert!(session.set_speed(Lane::V1, 0, Speed::from_permille(2000)).is_ok()); |
| 132 | let look = look_of(&session, (Lane::V1, 0)).expect("a look"); |
| 133 | let done = paste_look(&mut session, &look, &[(Lane::V1, 1)], &[Part::Speed]); |
| 134 | assert_eq!(done.touched, 1); |
| 135 | assert_eq!(session.speed_of(Lane::V1, 1), Speed::from_permille(2000)); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn paste_attributes_rows_are_dimmed_until_something_is_copied_and_selected() { |
| 140 | for action in [ |
| 141 | ActionId::PasteAttributes, |
| 142 | ActionId::PasteColor, |
| 143 | ActionId::PasteEffects, |
| 144 | ActionId::PasteTransform, |
| 145 | ActionId::PasteSpeed, |
| 146 | ActionId::PasteAudio, |
| 147 | ] { |
| 148 | assert_eq!(live(action, Ctx::default()), Enable::No("nothing copied yet"), "{action:?}"); |
| 149 | assert_eq!( |
| 150 | live(action, Ctx { clipboard: true, ..Ctx::default() }), |
| 151 | Enable::No("no clips selected"), |
| 152 | "{action:?}" |
| 153 | ); |
| 154 | assert_eq!(live(action, Ctx { clipboard: true, picks: 1, ..Ctx::default() }), Enable::Yes, "{action:?}"); |
| 155 | } |
| 156 | assert_eq!(key_of(ActionId::PasteAttributes), ["Ctrl+Alt+V"]); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn match_frame_finds_the_source_frame_under_the_playhead() { |
| 161 | let mut session = av(); |
| 162 | assert!(session.cut_at(2.0)); |
| 163 | // The second half plays the file from frame 60: the playhead 10 frames in shows frame 70. |
| 164 | let (path, stream, secs) = matched_source(&session, (Lane::V1, 1), 70, 30.).expect("a source"); |
| 165 | assert_eq!(path, session.sources()[0].path); |
| 166 | assert_eq!(stream, 0); |
| 167 | assert!((secs - 70. / 30.).abs() < 1e-9, "{secs}"); |
| 168 | assert_eq!(live(ActionId::MatchFrame, Ctx::default()), Enable::No("nothing under the playhead")); |
| 169 | assert_eq!(live(ActionId::MatchFrame, Ctx { clip_here: true, ..Ctx::default() }), Enable::Yes); |
| 170 | assert_eq!(key_of(ActionId::MatchFrame), ["Shift+F"]); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn replace_edit_keeps_the_place_and_the_duration_in_one_undo_step() { |
| 175 | let mut session = av(); |
| 176 | assert!(session.cut_at(2.0)); |
| 177 | // The replacement is the take's own file from frame 10: same length, same place. |
| 178 | let before = session.lane_clips(Lane::V1)[1].clone(); |
| 179 | let (at, len, items) = |
| 180 | replace_plan(&session, (Lane::V1, 1), 0, 10, 150, (true, true, false)).expect("a plan"); |
| 181 | assert_eq!((at, len), (before.start, before.end() - before.start)); |
| 182 | assert_eq!(items.len(), 2, "the linked sound is replaced with it"); |
| 183 | let depth = session.undo_depth(); |
| 184 | session |
| 185 | .edit_from_source_at(engine::SourceEdit::Overwrite, f64::from(at) / 30., &items) |
| 186 | .expect("the overwrite lands"); |
| 187 | assert_eq!(session.undo_depth(), depth + 1); |
| 188 | let now = session.lane_clips(Lane::V1)[1].clone(); |
| 189 | assert_eq!((now.start, now.end()), (before.start, before.end()), "same place, same duration"); |
| 190 | assert_eq!(now.in_frame, 10, "from the chosen in point"); |
| 191 | assert!(session.undo()); |
| 192 | assert_eq!(session.lane_clips(Lane::V1)[1].in_frame, before.in_frame); |
| 193 | // A source shorter than the clip is refused in words. |
| 194 | assert!(replace_plan(&session, (Lane::V1, 1), 0, 10, 40, (true, true, false)).is_err()); |
| 195 | // A picture-less item cannot replace a picture. |
| 196 | assert!(replace_plan(&session, (Lane::V1, 1), 0, 0, 150, (false, true, false)).is_err()); |
| 197 | assert_eq!(live(ActionId::ReplaceEdit, Ctx { clip_here: true, ..Ctx::default() }), Enable::No("no library item selected")); |
| 198 | assert_eq!(live(ActionId::ReplaceEdit, Ctx { clip_here: true, asset: true, ..Ctx::default() }), Enable::Yes); |
| 199 | assert_eq!(key_of(ActionId::ReplaceEdit), ["F8"]); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn select_forward_marks_what_has_not_ended_by_the_playhead() { |
| 204 | let mut session = av(); |
| 205 | assert!(session.cut_at(2.0)); |
| 206 | assert!(session.cut_at(4.0)); |
| 207 | let lanes = session.lanes(); |
| 208 | let one = forward_picks(&session, 70, &[Lane::V1]); |
| 209 | assert_eq!(one.picks(), [(Lane::V1, 1), (Lane::V1, 2)], "this track, from the clip the playhead is in"); |
| 210 | let all = forward_picks(&session, 70, &lanes); |
| 211 | assert_eq!(all.len(), 4, "every track, V1 and A1"); |
| 212 | assert!(all.contains((Lane::A1, 2))); |
| 213 | assert!(forward_picks(&session, 150, &lanes).is_empty(), "nothing past the end"); |
| 214 | assert_eq!(key_of(ActionId::SelectForward), ["Alt+Y"]); |
| 215 | assert_eq!(key_of(ActionId::SelectForwardAll), ["Shift+A"]); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn split_edit_extends_the_sound_past_the_picture_in_one_undo_step() { |
| 220 | let mut session = av(); |
| 221 | // Shorten the take (both halves trim together), then pull only the sound's end out to 100. |
| 222 | assert!(session.trim_clip(Lane::V1, 0, Edge::End, 60)); |
| 223 | assert_eq!(session.lane_clips(Lane::A1)[0].end(), 60); |
| 224 | let depth = session.undo_depth(); |
| 225 | let (lane, idx, edge, now) = split_edit_in(&mut session, (Lane::V1, 0), true, 100).expect("an L-cut"); |
| 226 | assert_eq!((lane, idx, edge, now), (Lane::A1, 0, Edge::End, 100)); |
| 227 | assert_eq!(session.undo_depth(), depth + 1, "one undo step"); |
| 228 | assert_eq!(session.lane_clips(Lane::V1)[0].end(), 60, "the picture stays"); |
| 229 | assert_eq!(session.lane_clips(Lane::A1)[0].end(), 100, "the sound runs on"); |
| 230 | assert!(session.undo()); |
| 231 | assert_eq!(session.lane_clips(Lane::A1)[0].end(), 60); |
| 232 | assert!(session.lane_clips(Lane::A1)[0].link.is_some(), "undo restores the link"); |
| 233 | // The J-cut: the picture's start is pulled earlier while the sound stays. |
| 234 | assert!(session.trim_clip(Lane::V1, 0, Edge::Start, 30)); |
| 235 | let (lane, _, edge, _) = split_edit_in(&mut session, (Lane::A1, 0), false, 10).expect("a J-cut"); |
| 236 | assert_eq!((lane, edge), (Lane::V1, Edge::Start)); |
| 237 | assert_eq!(session.lane_clips(Lane::V1)[0].start, 10); |
| 238 | assert_eq!(session.lane_clips(Lane::A1)[0].start, 30); |
| 239 | } |
| 240 | |
| 241 | #[test] |
| 242 | fn split_edit_with_a_named_edge_moves_that_edge_and_refuses_the_other_side() { |
| 243 | use engine::editkeys::split_edit_at; |
| 244 | let mut session = av(); |
| 245 | assert!(session.trim_clip(Lane::V1, 0, Edge::End, 60)); |
| 246 | // Named out edge on the sound: moves to 40 whatever the nearer edge would have been. |
| 247 | let (lane, _, edge, now) = split_edit_at(&mut session, (Lane::V1, 0), true, Some(Edge::End), 40).expect("an out edge"); |
| 248 | assert_eq!((lane, edge, now), (Lane::A1, Edge::End, 40)); |
| 249 | assert_eq!(session.lane_clips(Lane::V1)[0].end(), 60, "the picture stays"); |
| 250 | // An in edge given a frame past its out edge is refused, and nothing moves. |
| 251 | assert!(session.undo()); |
| 252 | assert!(split_edit_at(&mut session, (Lane::V1, 0), true, Some(Edge::Start), 61).is_err()); |
| 253 | assert!(split_edit_at(&mut session, (Lane::V1, 0), true, Some(Edge::End), 0).is_err()); |
| 254 | assert_eq!(session.lane_clips(Lane::A1)[0].end(), 60, "a refused split changes nothing"); |
| 255 | assert!(session.lane_clips(Lane::A1)[0].link.is_some(), "a refused split keeps the link"); |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn split_edit_rows_say_what_to_select() { |
| 260 | assert_eq!(live(ActionId::ExtendAudio, Ctx::default()), Enable::No("no clip selected")); |
| 261 | let video = (Clip::source_range(0, 0, 50), Lane::V1); |
| 262 | let unlinked = Ctx { clip: Some(video.clone()), ..Ctx::default() }; |
| 263 | assert!(matches!(live(ActionId::ExtendAudio, unlinked.clone()), Enable::No(_))); |
| 264 | assert_eq!(live(ActionId::ExtendVideo, unlinked), Enable::Yes, "the picture's own edge needs no partner"); |
| 265 | let mut linked = video.0.clone(); |
| 266 | linked.link = Some(3); |
| 267 | assert_eq!(live(ActionId::ExtendAudio, Ctx { clip: Some((linked, Lane::V1)), ..Ctx::default() }), Enable::Yes); |
| 268 | assert_eq!(key_of(ActionId::ExtendAudio), ["Shift+Alt+L"]); |
| 269 | assert_eq!(key_of(ActionId::ExtendVideo), ["Shift+Alt+J"]); |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn nudge_moves_a_take_by_one_frame_and_stops_at_a_neighbour() { |
| 274 | let mut session = av_with_gap(); |
| 275 | let depth = session.undo_depth(); |
| 276 | nudge(&mut session, &[(Lane::V1, 1)], 1).expect("room to the right"); |
| 277 | assert_eq!(session.undo_depth(), depth + 1, "one undo step"); |
| 278 | assert_eq!(session.lane_clips(Lane::V1)[1].start, 91); |
| 279 | assert_eq!(session.lane_clips(Lane::A1)[1].start, 91, "the linked sound moves with it"); |
| 280 | nudge(&mut session, &[(Lane::V1, 1)], -10).expect("ten frames back"); |
| 281 | assert_eq!(session.lane_clips(Lane::V1)[1].start, 81); |
| 282 | nudge(&mut session, &[(Lane::V1, 1)], -50).expect("a long nudge stops flush against the neighbour"); |
| 283 | assert_eq!(session.lane_clips(Lane::V1)[1].start, 60); |
| 284 | assert!(nudge(&mut session, &[(Lane::V1, 1)], -1).is_err(), "the first take is in the way"); |
| 285 | assert_eq!(session.lane_clips(Lane::V1)[1].start, 60, "a refusal changes nothing"); |
| 286 | assert!(nudge(&mut session, &[(Lane::V1, 0)], -1).is_err(), "already at the start"); |
| 287 | assert_eq!(live(ActionId::NudgeForward, Ctx::default()), Enable::No("no clips selected")); |
| 288 | assert_eq!(live(ActionId::NudgeForward10, Ctx { picks: 2, ..Ctx::default() }), Enable::Yes); |
| 289 | assert_eq!(key_of(ActionId::NudgeBack), ["Ctrl+Alt+←"]); |
| 290 | assert_eq!(key_of(ActionId::NudgeForward10), ["Ctrl+Shift+Alt+→"]); |
| 291 | } |
| 292 | |
| 293 | #[test] |
| 294 | fn box_select_marks_what_the_rectangle_touches_on_the_lanes_it_crosses() { |
| 295 | let mut session = av(); |
| 296 | assert!(session.cut_at(2.0)); |
| 297 | let beds = [(Lane::V1, 0., 48.), (Lane::A1, 50., 98.)]; |
| 298 | let v1 = marquee_picks(&session, &beds, (10., 30.), (50, 80), Selection::new()); |
| 299 | assert_eq!(v1.picks(), [(Lane::V1, 0), (Lane::V1, 1)], "only the lane the box crosses, both clips it touches"); |
| 300 | let both = marquee_picks(&session, &beds, (10., 70.), (100, 120), Selection::new()); |
| 301 | assert_eq!(both.picks(), [(Lane::V1, 1), (Lane::A1, 1)]); |
| 302 | let mut base = Selection::new(); |
| 303 | base.add((Lane::A1, 0)); |
| 304 | let added = marquee_picks(&session, &beds, (10., 30.), (0, 10), base); |
| 305 | assert_eq!(added.picks(), [(Lane::A1, 0), (Lane::V1, 0)], "a ctrl-box adds to what is selected"); |
| 306 | assert!(marquee_picks(&session, &beds, (200., 300.), (0, 150), Selection::new()).is_empty()); |
| 307 | let box_ = Marquee { from: point(px(10.), px(10.)), to: point(px(12.), px(11.)) }; |
| 308 | assert!(!box_.live(), "under the slop it is still a click"); |
| 309 | assert!(Marquee { to: point(px(40.), px(11.)), ..box_ }.live()); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn every_new_verb_is_on_a_menu_and_the_menus_still_fit() { |
| 314 | use crate::ui::menubar::{Entry, Title}; |
| 315 | let door = |a: ActionId| { |
| 316 | Title::ALL.iter().any(|t| { |
| 317 | t.entries().iter().any(|e| matches!(e, Entry::Act(x) | Entry::Switch(x) | Entry::Choice(x) if *x == a)) |
| 318 | }) |
| 319 | }; |
| 320 | for a in [ |
| 321 | ActionId::CloseGap, |
| 322 | ActionId::Freeze, |
| 323 | ActionId::PasteAttributes, |
| 324 | ActionId::PasteColor, |
| 325 | ActionId::PasteEffects, |
| 326 | ActionId::PasteTransform, |
| 327 | ActionId::PasteSpeed, |
| 328 | ActionId::PasteAudio, |
| 329 | ActionId::MatchFrame, |
| 330 | ActionId::ReplaceEdit, |
| 331 | ActionId::SelectForward, |
| 332 | ActionId::SelectForwardAll, |
| 333 | ActionId::ExtendAudio, |
| 334 | ActionId::ExtendVideo, |
| 335 | ActionId::NudgeBack, |
| 336 | ActionId::NudgeForward, |
| 337 | ActionId::NudgeBack10, |
| 338 | ActionId::NudgeForward10, |
| 339 | ] { |
| 340 | assert!(door(a), "{a:?} has a menu row"); |
| 341 | } |
| 342 | // `every_menubar_menu_fits_1280_by_720` walks every list, flyouts included. |
| 343 | } |
| 344 | |
| 345 | /// A freeze's still is named for its clip, and a second one of the same clip gets a number, |
| 346 | /// never the first one's name: the timeline reads "Freeze – <clip>" for the first. |
| 347 | #[test] |
| 348 | fn a_freeze_still_is_named_for_its_clip_and_numbered_after_that() { |
| 349 | assert_eq!(still_name("Freeze \u{2013} test_av", 1), "Freeze \u{2013} test_av.png"); |
| 350 | assert_eq!(still_name("Freeze \u{2013} test_av", 2), "Freeze \u{2013} test_av 2.png"); |
| 351 | } |