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 | //! The timeline-editing verbs every NLE has and edith lacked: close the gap |
| 2 | //! under the playhead, freeze a frame, paste attributes, match frame, replace |
| 3 | //! edit, select forward, split edits and the nudge. Each is one [`ActionId`], so |
| 4 | //! the menu row, the stroke and the palette are the same door, and each is one |
| 5 | //! undo step (a verb that touches several clips wraps them in a batch). |
| 6 | |
| 7 | use crate::*; |
| 8 | |
| 9 | pub(crate) use engine::editkeys::{matched_source, nudge, paste_look, look_of, replace_plan, split_edit_in, CopiedLook, Part, PasteReport}; |
| 10 | |
| 11 | /// The clips that have not ended by `frame` on each of `lanes`: what select |
| 12 | /// track forward marks. A locked lane is left out, as Select All leaves it. |
| 13 | pub(crate) fn forward_picks(session: &PlaybackSession, frame: u32, lanes: &[Lane]) -> Selection { |
| 14 | let mut picked = Selection::new(); |
| 15 | for &l in lanes.iter().filter(|l| l.kind != LaneKind::Subtitle) { |
| 16 | if session.lane_locked(l) { |
| 17 | continue; |
| 18 | } |
| 19 | for (i, c) in session.lane_clips(l).iter().enumerate() { |
| 20 | if c.end() > frame { |
| 21 | picked.add((l, i)); |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | picked |
| 26 | } |
| 27 | |
| 28 | /// The selection a box makes: every clip or caption crossing frames `f1..=f2` |
| 29 | /// on the lanes whose bed spans overlap `rows` (top, bottom). `beds` is each |
| 30 | /// lane's `(top, bottom)`. `base` is kept when the box adds to the selection. |
| 31 | pub(crate) fn marquee_picks( |
| 32 | session: &PlaybackSession, |
| 33 | beds: &[(Lane, f32, f32)], |
| 34 | rows: (f32, f32), |
| 35 | frames: (u32, u32), |
| 36 | base: Selection, |
| 37 | ) -> Selection { |
| 38 | let mut picked = base; |
| 39 | for &(lane, top, bottom) in beds { |
| 40 | if bottom <= rows.0 || top >= rows.1 || session.lane_locked(lane) { |
| 41 | continue; |
| 42 | } |
| 43 | match lane.kind { |
| 44 | LaneKind::Subtitle => { |
| 45 | for (i, s) in session.sub_lane(lane).iter().enumerate() { |
| 46 | if s.start <= frames.1 && s.end() > frames.0 { |
| 47 | picked.add((lane, i)); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | _ => { |
| 52 | for (i, c) in session.lane_clips(lane).iter().enumerate() { |
| 53 | if c.start <= frames.1 && c.end() > frames.0 { |
| 54 | picked.add((lane, i)); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | picked |
| 61 | } |
| 62 | |
| 63 | /// The first gap `frame` stands in, lanes in the order they are drawn. |
| 64 | pub(crate) fn gap_under(session: &PlaybackSession, frame: u32) -> Option<(Lane, u32, u32)> { |
| 65 | session |
| 66 | .lanes() |
| 67 | .into_iter() |
| 68 | .filter(|l| l.kind != LaneKind::Subtitle) |
| 69 | .find_map(|l| session.closable_gap_at(l, frame).map(|(start, frames)| (l, start, frames))) |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /// How long a freeze frame holds, in seconds. |
| 74 | pub(crate) const FREEZE_SECS: f64 = 2.; |
| 75 | /// How far a freeze may move the playhead to the nearest frame a slowed clip can be cut at. |
| 76 | const FREEZE_REACH: u32 = 12; |
| 77 | |
| 78 | /// The file a still is kept in: `title.png`, or `title 2.png`, `title 3.png`... where a name |
| 79 | /// holds other pixels. A name already holding these bytes is reused, so a repeat freeze of |
| 80 | /// the same frame adds no file. |
| 81 | fn still_path(dir: &Path, title: &str, bytes: &[u8]) -> Result<PathBuf, String> { |
| 82 | std::fs::create_dir_all(dir).map_err(|e| format!("cannot create {}: {e}", dir.display()))?; |
| 83 | let mut n = 1; |
| 84 | loop { |
| 85 | let path = dir.join(still_name(title, n)); |
| 86 | match std::fs::read(&path) { |
| 87 | Ok(old) if old == bytes => return Ok(path), |
| 88 | Ok(_) => n += 1, |
| 89 | Err(_) => { |
| 90 | std::fs::write(&path, bytes).map_err(|e| format!("cannot write {}: {e}", path.display()))?; |
| 91 | return Ok(path); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// The name the `n`th still with `title` is kept under: the first is the title alone. |
| 98 | pub(crate) fn still_name(title: &str, n: u32) -> String { |
| 99 | match n { |
| 100 | 1 => format!("{title}.png"), |
| 101 | n => format!("{title} {n}.png"), |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | impl Player { |
| 106 | fn playhead_frame(&self) -> u32 { |
| 107 | self.session.as_ref().map_or(0, |s| frame_at(s.now(), self.fps)) |
| 108 | } |
| 109 | |
| 110 | /// The gap Close gap acts on: the picked one, else the first gap the |
| 111 | /// playhead stands in, lanes in the order they are drawn. |
| 112 | pub(crate) fn gap_here(&self) -> Option<(Lane, u32, u32)> { |
| 113 | if let Some(pick) = self.gap_pick() { |
| 114 | return Some((pick.lane, pick.start, pick.frames)); |
| 115 | } |
| 116 | gap_under(self.session.as_ref()?, self.playhead_frame()) |
| 117 | } |
| 118 | |
| 119 | /// Close gap: ripples shut the gap under the playhead (or the picked one), |
| 120 | /// scoped like the right-click and Delete-on-a-gap doors. |
| 121 | pub(crate) fn close_gap_here(&mut self, cx: &mut Context<Self>) { |
| 122 | if self.exporting().is_some() { |
| 123 | return; |
| 124 | } |
| 125 | let Some((lane, start, frames)) = self.gap_here() else { |
| 126 | self.notify_user("Close gap — no gap under the playhead".into()); |
| 127 | cx.notify(); |
| 128 | return; |
| 129 | }; |
| 130 | if self.lane_locked_refusal(lane) { |
| 131 | cx.notify(); |
| 132 | return; |
| 133 | } |
| 134 | self.close_gap(lane, start, frames, cx); |
| 135 | if self.gap_pick().is_none() { |
| 136 | self.gap = None; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /// The clip a playhead verb acts on: the selected one when the playhead is |
| 141 | /// over it, else the first clip under the playhead, video first. |
| 142 | fn clip_here(&self) -> Option<(Lane, usize)> { |
| 143 | let under = self.under_playhead(); |
| 144 | self.selected |
| 145 | .anchor() |
| 146 | .filter(|pick| under.contains(pick)) |
| 147 | .or_else(|| under.first().copied()) |
| 148 | } |
| 149 | |
| 150 | /// Freeze frame: holds the picture the playhead shows for [`FREEZE_SECS`]. |
| 151 | /// Every lane is cut at the playhead and what follows moves later, as the |
| 152 | /// MCP `freeze` op does with `insert: true`; the still is composed from the |
| 153 | /// timeline (grade, effects and all) and kept in `freeze/` beside the |
| 154 | /// project. One undo step. |
| 155 | pub(crate) fn freeze_frame(&mut self, cx: &mut Context<Self>) { |
| 156 | if self.exporting().is_some() { |
| 157 | return; |
| 158 | } |
| 159 | // The picture the hand is holding (the selected clip when the playhead is over it), else |
| 160 | // the first picture under the playhead: a PiP above a clip does not take the freeze. |
| 161 | let under = self.under_playhead(); |
| 162 | let Some((lane, idx)) = self |
| 163 | .clip_here() |
| 164 | .filter(|(l, _)| l.kind == LaneKind::Video) |
| 165 | .or_else(|| under.iter().copied().find(|(l, _)| l.kind == LaneKind::Video)) |
| 166 | else { |
| 167 | self.notify_user("Freeze frame — no picture under the playhead".into()); |
| 168 | cx.notify(); |
| 169 | return; |
| 170 | }; |
| 171 | let Some(dir) = self.project_path.parent().filter(|_| !self.project_path.as_os_str().is_empty()) else { |
| 172 | self.notify_user("Freeze frame — the project is unsaved, and the still is kept beside it".into()); |
| 173 | cx.notify(); |
| 174 | return; |
| 175 | }; |
| 176 | let dir = dir.join("freeze"); |
| 177 | let playhead = self.playhead_frame(); |
| 178 | let at = playhead; |
| 179 | let fps = self.fps; |
| 180 | let dur = (FREEZE_SECS * fps).round() as u32; |
| 181 | let Some(session) = self.session.as_ref() else { |
| 182 | return; |
| 183 | }; |
| 184 | // A slowed clip can be cut only where its rate lands on a whole source frame: the still |
| 185 | // is taken at the nearest such frame, so the held picture and the cut are the same frame. |
| 186 | let at = session.cut_frame_near(at, FREEZE_REACH).unwrap_or(at); |
| 187 | // The still is named for the clip it holds, so the timeline reads "Freeze – <clip>". |
| 188 | let title = session |
| 189 | .lane_clips(lane) |
| 190 | .get(idx) |
| 191 | .and_then(|c| session.sources().get(c.source)) |
| 192 | .and_then(|src| src.path.file_stem()) |
| 193 | .map_or_else(|| "Freeze".to_string(), |s| format!("Freeze \u{2013} {}", s.to_string_lossy())); |
| 194 | if let Some(locked) = session.lanes().into_iter().find(|&l| session.lane_locked(l)) { |
| 195 | self.lane_locked_refusal(locked); |
| 196 | cx.notify(); |
| 197 | return; |
| 198 | } |
| 199 | let (proj, meta) = session.export_snapshot(); |
| 200 | let png = match engine::export::compose_at(&proj, &meta, at, meta.width, meta.height) |
| 201 | .map_err(|e| e.to_string()) |
| 202 | .and_then(|frame| { |
| 203 | let bytes = engine::decode::to_png_bytes(&frame).map_err(|e| e.to_string())?; |
| 204 | still_path(&dir, &title, &bytes) |
| 205 | }) { |
| 206 | Ok(path) => path, |
| 207 | Err(e) => { |
| 208 | self.notify_user(format!("Freeze frame — {e}").into()); |
| 209 | cx.notify(); |
| 210 | return; |
| 211 | } |
| 212 | }; |
| 213 | let Some(session) = self.session.as_mut() else { |
| 214 | return; |
| 215 | }; |
| 216 | session.begin_batch(); |
| 217 | let done = Self::freeze_edit(session, at, dur, fps, lane, &png); |
| 218 | session.end_batch(); |
| 219 | match done { |
| 220 | Ok(moved) => { |
| 221 | self.selected.clear(); |
| 222 | self.reset_after_reseek(); |
| 223 | self.mark_dirty(); |
| 224 | // Said plainly when the still landed off the playhead, so the picture the person sees |
| 225 | // and the frame the toast names are never different without a word about it. When the |
| 226 | // clips after it moved, the toast says so: the hold is a ripple, not an overwrite. |
| 227 | let held = format!("for {FREEZE_SECS:.1} s"); |
| 228 | let ripple = match moved { |
| 229 | true => format!(" \u{00b7} later clips moved {FREEZE_SECS:.1} s"), |
| 230 | false => String::new(), |
| 231 | }; |
| 232 | let msg = match at == playhead { |
| 233 | true => format!("Froze {} {held}{ripple}", timecode(f64::from(at) / fps, fps)), |
| 234 | false => format!( |
| 235 | "Freeze frame added at {} (nearest frame the speed ramp allows){ripple}", |
| 236 | timecode(f64::from(at) / fps, fps) |
| 237 | ), |
| 238 | }; |
| 239 | self.notify_user_action(msg.into(), ToastAction::Undo); |
| 240 | } |
| 241 | Err(e) => { |
| 242 | // A refusal part-way (a slowed clip that cannot be cut here) leaves its cuts in the |
| 243 | // batch's one step: take that step back so a refused freeze changes nothing. |
| 244 | if let Some(session) = self.session.as_mut() { |
| 245 | session.undo(); |
| 246 | } |
| 247 | self.reset_after_reseek(); |
| 248 | self.notify_user(format!("Freeze frame — {e}").into()); |
| 249 | } |
| 250 | } |
| 251 | cx.notify(); |
| 252 | } |
| 253 | |
| 254 | /// The edit of a freeze, inside the caller's batch. |
| 255 | pub(crate) fn freeze_edit( |
| 256 | session: &mut PlaybackSession, |
| 257 | at: u32, |
| 258 | dur: u32, |
| 259 | fps: f64, |
| 260 | lane: Lane, |
| 261 | png: &Path, |
| 262 | ) -> Result<bool, String> { |
| 263 | let n = match session.sources().iter().position(|s| s.path == png) { |
| 264 | Some(i) => i, |
| 265 | None => session.import(png).map_err(|e| format!("cannot import the still: {e}"))?, |
| 266 | }; |
| 267 | if dur > session.file_frames(png) { |
| 268 | return Err("that is longer than a still can be held".into()); |
| 269 | } |
| 270 | let straddles = |s: &PlaybackSession| { |
| 271 | s.lanes().into_iter().filter(|l| l.kind != LaneKind::Subtitle).any(|l| { |
| 272 | s.lane_clips(l).iter().any(|c| c.start < at && at < c.end()) |
| 273 | }) |
| 274 | }; |
| 275 | if straddles(session) { |
| 276 | session.cut_at(f64::from(at) / fps); |
| 277 | if straddles(session) { |
| 278 | return Err("a slowed clip cannot be cut at this frame".into()); |
| 279 | } |
| 280 | } |
| 281 | let mut picks: Vec<(Lane, usize)> = Vec::new(); |
| 282 | for l in session.lanes() { |
| 283 | match l.kind { |
| 284 | LaneKind::Subtitle => { |
| 285 | picks.extend(session.sub_lane(l).iter().enumerate().filter(|(_, c)| c.start >= at).map(|(i, _)| (l, i))); |
| 286 | } |
| 287 | _ => picks.extend(session.lane_clips(l).iter().enumerate().filter(|(_, c)| c.start >= at).map(|(i, _)| (l, i))), |
| 288 | } |
| 289 | } |
| 290 | let first_media = picks.iter().copied().find(|(l, _)| l.kind != LaneKind::Subtitle); |
| 291 | match first_media { |
| 292 | Some((dl, di)) => { |
| 293 | let start = session.lane_clips(dl)[di].start + dur; |
| 294 | if !session.move_selection_to(&picks, dl, di, dl, start) { |
| 295 | return Err("what follows cannot all move later".into()); |
| 296 | } |
| 297 | } |
| 298 | None => { |
| 299 | for &(l, i) in picks.iter().rev() { |
| 300 | let start = session.sub_lane(l)[i].start + dur; |
| 301 | session.move_sub(l, i, l, start).map_err(|e| e.to_string())?; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | let tail: Vec<usize> = |
| 306 | session.titles().iter().enumerate().filter(|(_, t)| t.start >= at).map(|(i, _)| i).collect(); |
| 307 | for &i in tail.iter().rev() { |
| 308 | let mut t = session.titles()[i].clone(); |
| 309 | t.start += dur; |
| 310 | session.set_title(i, t); |
| 311 | } |
| 312 | session.remap_chapters(|f| if f >= at { f + dur } else { f }); |
| 313 | session.remap_markers(|f| if f >= at { f + dur } else { f }); |
| 314 | if !session.place_at(lane, f64::from(at) / fps, Clip::source_range(n, 0, dur)) { |
| 315 | return Err("the still would not go there".into()); |
| 316 | } |
| 317 | // Whether anything after the still moved to make room for it. |
| 318 | Ok(!picks.is_empty()) |
| 319 | } |
| 320 | |
| 321 | /// The look of the selected clip, read for a copy. |
| 322 | pub(crate) fn capture_look(&self) -> Option<CopiedLook> { |
| 323 | look_of(self.session.as_ref()?, self.selected.anchor()?) |
| 324 | } |
| 325 | |
| 326 | /// Paste attributes: the copied clip's `parts` onto every selected clip. |
| 327 | /// A picture part reaches the picture of each pick (the video half of an |
| 328 | /// audio pick's take), the audio part the sound. One undo step. |
| 329 | pub(crate) fn paste_attributes(&mut self, parts: &[Part], cx: &mut Context<Self>) { |
| 330 | if self.exporting().is_some() { |
| 331 | return; |
| 332 | } |
| 333 | let Some(look) = self.copied_look.clone() else { |
| 334 | self.notify_user("Paste attributes — nothing copied yet".into()); |
| 335 | cx.notify(); |
| 336 | return; |
| 337 | }; |
| 338 | let picks: Vec<(Lane, usize)> = |
| 339 | self.marks().0.into_iter().filter(|(l, _)| l.kind != LaneKind::Subtitle).collect(); |
| 340 | if picks.is_empty() { |
| 341 | self.notify_user("Paste attributes — select the clips to paste onto".into()); |
| 342 | cx.notify(); |
| 343 | return; |
| 344 | } |
| 345 | if let Some(lane) = self.marked_locked_lane() { |
| 346 | self.lane_locked_refusal(lane); |
| 347 | cx.notify(); |
| 348 | return; |
| 349 | } |
| 350 | let Some(session) = self.session.as_mut() else { |
| 351 | return; |
| 352 | }; |
| 353 | let PasteReport { touched, words, why } = paste_look(session, &look, &picks, parts); |
| 354 | self.reset_after_reseek(); |
| 355 | if touched > 0 { |
| 356 | self.mark_dirty(); |
| 357 | let mut text = format!("Pasted {} onto {} clip(s)", words.join(", "), picks.len()); |
| 358 | if !why.is_empty() { |
| 359 | text.push_str(&format!(" — {}", why.join("; "))); |
| 360 | } |
| 361 | self.notify_user_action(text.into(), ToastAction::Undo); |
| 362 | } else if why.is_empty() { |
| 363 | self.notify_user("Paste attributes — they already match".into()); |
| 364 | } else { |
| 365 | self.notify_user(format!("Paste attributes — {}", why.join("; ")).into()); |
| 366 | } |
| 367 | cx.notify(); |
| 368 | } |
| 369 | |
| 370 | /// Match frame: opens the source of the clip under the playhead in the |
| 371 | /// library preview, parked on the frame the playhead shows. |
| 372 | pub(crate) fn match_frame(&mut self, cx: &mut Context<Self>) { |
| 373 | if self.exporting().is_some() { |
| 374 | return; |
| 375 | } |
| 376 | let Some((lane, idx)) = self.clip_here() else { |
| 377 | self.notify_user("Match frame — nothing under the playhead".into()); |
| 378 | cx.notify(); |
| 379 | return; |
| 380 | }; |
| 381 | let (frame, fps) = (self.playhead_frame(), self.fps); |
| 382 | let Some(session) = self.session.as_ref() else { |
| 383 | return; |
| 384 | }; |
| 385 | let Some((path, stream, secs)) = matched_source(session, (lane, idx), frame, fps) else { |
| 386 | self.notify_user("Match frame — a compound clip plays a sequence, not a file".into()); |
| 387 | cx.notify(); |
| 388 | return; |
| 389 | }; |
| 390 | self.open_preview(&path, stream, cx); |
| 391 | if self.preview_session.is_none() { |
| 392 | return; |
| 393 | } |
| 394 | self.selected_asset = Some((path.clone(), stream)); |
| 395 | self.seek(secs, cx); |
| 396 | if let Some(p) = self.preview_session.as_mut() { |
| 397 | p.pause(); |
| 398 | } |
| 399 | self.notify_user( |
| 400 | format!("Matched frame {} of {}; esc returns to the timeline", timecode(secs, fps), file_name(&path)) |
| 401 | .into(), |
| 402 | ); |
| 403 | cx.notify(); |
| 404 | } |
| 405 | |
| 406 | /// Replace edit: the clip under the playhead (with its linked sound) is |
| 407 | /// replaced by the selected library item, from its in mark (or the start), |
| 408 | /// keeping the place and the duration. One undo step. |
| 409 | pub(crate) fn replace_edit(&mut self, cx: &mut Context<Self>) { |
| 410 | if self.exporting().is_some() { |
| 411 | return; |
| 412 | } |
| 413 | let Some((path, stream)) = self.selected_asset.clone().or_else(|| self.source_target.clone()) else { |
| 414 | self.notify_user("Replace edit — no library item selected".into()); |
| 415 | cx.notify(); |
| 416 | return; |
| 417 | }; |
| 418 | let Some((lane, idx)) = self.clip_here() else { |
| 419 | self.notify_user("Replace edit — nothing under the playhead".into()); |
| 420 | cx.notify(); |
| 421 | return; |
| 422 | }; |
| 423 | if self.lane_locked_refusal(lane) { |
| 424 | cx.notify(); |
| 425 | return; |
| 426 | } |
| 427 | let fps = self.fps; |
| 428 | let Some(session) = self.session.as_ref() else { |
| 429 | return; |
| 430 | }; |
| 431 | let canon = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf()); |
| 432 | let want = canon(&path); |
| 433 | let Some(source) = session |
| 434 | .sources() |
| 435 | .iter() |
| 436 | .position(|s| s.audio_stream == stream && (s.path == path || canon(&s.path) == want)) |
| 437 | else { |
| 438 | self.notify_user(format!("Replace edit — {} is not in this project", file_name(&path)).into()); |
| 439 | cx.notify(); |
| 440 | return; |
| 441 | }; |
| 442 | let still = engine::is_image(&path); |
| 443 | let picture = still || (!engine::is_audio(&path) && engine::probe_media(&path).video.is_some()); |
| 444 | let sound = !still |
| 445 | && engine::AudioSession::probe_streams(&path).is_ok_and(|streams| streams.len() > stream); |
| 446 | let total = if picture { |
| 447 | session.file_frames(&path) |
| 448 | } else { |
| 449 | engine::AudioSession::stream_duration_secs(&path, stream) |
| 450 | .ok() |
| 451 | .flatten() |
| 452 | .map_or(0, |secs| (secs * fps).ceil() as u32) |
| 453 | }; |
| 454 | let in_f = match still { |
| 455 | true => 0, |
| 456 | false => self |
| 457 | .source_marks |
| 458 | .get(&(path.clone(), stream)) |
| 459 | .and_then(|m| m.0) |
| 460 | .map_or(0, |t| (t.max(0.) * fps).round() as u32), |
| 461 | }; |
| 462 | let (at, len, items) = match replace_plan(session, (lane, idx), source, in_f, total, (picture, sound, still)) { |
| 463 | Ok(plan) => plan, |
| 464 | Err(why) => { |
| 465 | self.notify_user(format!("Replace edit — {why}").into()); |
| 466 | cx.notify(); |
| 467 | return; |
| 468 | } |
| 469 | }; |
| 470 | let done = match self.session.as_mut() { |
| 471 | Some(s) => s.edit_from_source_at(engine::SourceEdit::Overwrite, f64::from(at) / fps, &items), |
| 472 | None => return, |
| 473 | }; |
| 474 | match done { |
| 475 | Ok(()) => { |
| 476 | self.selected.clear(); |
| 477 | self.reset_after_reseek(); |
| 478 | self.mark_dirty(); |
| 479 | self.notify_user_action( |
| 480 | format!("Replaced with {} for {}", file_name(&path), timecode(f64::from(len) / fps, fps)).into(), |
| 481 | ToastAction::Undo, |
| 482 | ); |
| 483 | } |
| 484 | Err(e) => self.notify_user(format!("Replace edit refused — {e}").into()), |
| 485 | } |
| 486 | cx.notify(); |
| 487 | } |
| 488 | |
| 489 | /// Select track forward (`all` false) or all tracks forward: every clip |
| 490 | /// that has not ended by the playhead, on the selected clip's track (else |
| 491 | /// the track under the playhead, else V1) or on every unlocked media track. |
| 492 | pub(crate) fn select_forward(&mut self, all: bool, cx: &mut Context<Self>) { |
| 493 | let frame = self.playhead_frame(); |
| 494 | let lane = self |
| 495 | .selected |
| 496 | .anchor() |
| 497 | .map(|(l, _)| l) |
| 498 | .or_else(|| self.under_playhead().first().map(|(l, _)| *l)) |
| 499 | .unwrap_or(Lane::V1); |
| 500 | let Some(session) = self.session.as_ref() else { |
| 501 | return; |
| 502 | }; |
| 503 | let lanes: Vec<Lane> = match all { |
| 504 | true => session.lanes(), |
| 505 | false => vec![lane], |
| 506 | }; |
| 507 | let picked = forward_picks(session, frame, &lanes); |
| 508 | if picked.is_empty() { |
| 509 | let word = if all { "on any track".to_string() } else { format!("on {}", lane.label()) }; |
| 510 | self.notify_user(format!("Nothing after the playhead {word}").into()); |
| 511 | } |
| 512 | self.selected = picked; |
| 513 | self.gap = None; |
| 514 | cx.notify(); |
| 515 | } |
| 516 | |
| 517 | /// Split edit: moves the audio's (or the picture's) edge of the selected |
| 518 | /// take to the playhead while the other half stays put -- an L-cut when the |
| 519 | /// playhead is past the cut, a J-cut when before it. The pair is taken out |
| 520 | /// of its group first (a group trims together), as every NLE's split edit |
| 521 | /// unlinks. One undo step. |
| 522 | pub(crate) fn split_edit(&mut self, audio: bool, cx: &mut Context<Self>) { |
| 523 | if self.exporting().is_some() { |
| 524 | return; |
| 525 | } |
| 526 | if let Some(lane) = self.marked_locked_lane() { |
| 527 | self.lane_locked_refusal(lane); |
| 528 | cx.notify(); |
| 529 | return; |
| 530 | } |
| 531 | let frame = self.playhead_frame(); |
| 532 | let Some(anchor) = self.selected.anchor() else { |
| 533 | self.notify_user("Split edit — no clip selected".into()); |
| 534 | cx.notify(); |
| 535 | return; |
| 536 | }; |
| 537 | let Some(session) = self.session.as_mut() else { |
| 538 | return; |
| 539 | }; |
| 540 | match split_edit_in(session, anchor, audio, frame) { |
| 541 | Ok((lane, idx, _, now)) => { |
| 542 | self.selected.set_one((lane, idx)); |
| 543 | self.reset_after_reseek(); |
| 544 | self.mark_dirty(); |
| 545 | let fps = self.fps; |
| 546 | let word = if audio { "Audio" } else { "Video" }; |
| 547 | self.notify_user_action( |
| 548 | format!("{word} edge moved to {} — the pair is unlinked", timecode(f64::from(now) / fps, fps)) |
| 549 | .into(), |
| 550 | ToastAction::Undo, |
| 551 | ); |
| 552 | } |
| 553 | Err(e) => self.notify_user(format!("Split edit — {e}").into()), |
| 554 | } |
| 555 | cx.notify(); |
| 556 | } |
| 557 | |
| 558 | /// Nudge: the selected clips (and their groups) `frames` along the |
| 559 | /// timeline, as one move, refused whole when a neighbour is in the way. |
| 560 | pub(crate) fn nudge_selected(&mut self, frames: i64, cx: &mut Context<Self>) { |
| 561 | if self.exporting().is_some() { |
| 562 | return; |
| 563 | } |
| 564 | if let Some(lane) = self.marked_locked_lane() { |
| 565 | self.lane_locked_refusal(lane); |
| 566 | cx.notify(); |
| 567 | return; |
| 568 | } |
| 569 | let picks: Vec<(Lane, usize)> = |
| 570 | self.marks().0.into_iter().filter(|(l, _)| l.kind != LaneKind::Subtitle).collect(); |
| 571 | let Some(session) = self.session.as_mut() else { |
| 572 | return; |
| 573 | }; |
| 574 | match nudge(session, &picks, frames) { |
| 575 | Ok(()) => { |
| 576 | self.reset_after_reseek(); |
| 577 | self.mark_dirty(); |
| 578 | } |
| 579 | Err(why) => self.notify_user(format!("Nudge — {why}").into()), |
| 580 | } |
| 581 | cx.notify(); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | impl Player { |
| 586 | /// A left press on the bed of `lane` at `frame`: starts a box select unless |
| 587 | /// a clip or caption is under the pointer (that press is the clip's own). |
| 588 | pub(crate) fn begin_marquee(&mut self, lane: Lane, frame: u32, at: Point<Pixels>) { |
| 589 | // The title and caption picks are let go on the release ([`Player::finish_marquee`]), once |
| 590 | // the press is known to be a click on bare bed and not a drag that may still add to the |
| 591 | // picks. A title left picked past the click is what Backspace then removed unseen. |
| 592 | let Some(session) = self.session.as_ref() else { |
| 593 | return; |
| 594 | }; |
| 595 | let covered = match lane.kind { |
| 596 | LaneKind::Subtitle => session.sub_lane(lane).iter().any(|s| s.start <= frame && frame < s.end()), |
| 597 | _ => session.lane_clips(lane).iter().any(|c| c.start <= frame && frame < c.end()), |
| 598 | }; |
| 599 | if !covered && self.context_menu.is_none() { |
| 600 | self.marquee = Some(Marquee { from: at, to: at }); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /// The release of a box select: every clip the rectangle touches, on every |
| 605 | /// lane it crosses, becomes the selection (ctrl adds to it). A box that |
| 606 | /// never travelled is a click and selects nothing. |
| 607 | pub(crate) fn finish_marquee(&mut self, m: Marquee, add: bool, cx: &mut Context<Self>) { |
| 608 | // One selection model: a press on bare bed is the one thing picked next. A title and a |
| 609 | // caption go at once, and a plain click empties the clips too (a ctrl-click adds to them). |
| 610 | self.drop_title_selection(cx); |
| 611 | self.drop_sel_cue(); |
| 612 | if !m.live() { |
| 613 | if !add { |
| 614 | self.selected = Selection::new(); |
| 615 | } |
| 616 | cx.notify(); |
| 617 | return; |
| 618 | } |
| 619 | let (left, top, right, bottom) = m.edges(); |
| 620 | let (f1, f2) = (self.frame_under(left), self.frame_under(right)); |
| 621 | let Some(session) = self.session.as_ref() else { |
| 622 | return; |
| 623 | }; |
| 624 | let beds: Vec<(Lane, f32, f32)> = self |
| 625 | .lane_beds |
| 626 | .borrow() |
| 627 | .iter() |
| 628 | .map(|(l, b)| (*l, f32::from(b.top()), f32::from(b.bottom()))) |
| 629 | .collect(); |
| 630 | let base = if add { self.selected.clone() } else { Selection::new() }; |
| 631 | self.selected = marquee_picks(session, &beds, (f32::from(top), f32::from(bottom)), (f1, f2), base); |
| 632 | self.gap = None; |
| 633 | cx.notify(); |
| 634 | } |
| 635 | } |