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 | //! Editing the timeline itself: the cuts, the clipboard, the drags, the |
| 2 | //! snapping and the trims. |
| 3 | |
| 4 | use crate::ui::kit::NumberEdit; |
| 5 | use crate::ui::widgets::CueEdit; |
| 6 | use crate::*; |
| 7 | use crate::ui::bench_stance::TrackSize; |
| 8 | use crate::ui::bench_stance::clip_envelope::{self, EnvHit}; |
| 9 | |
| 10 | /// One of a lane's three head switches (DESIGN §7): the eye hides a video or |
| 11 | /// subtitle lane, the note mutes an audio lane, the lock refuses edits to any. |
| 12 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 13 | pub(crate) enum Switch { |
| 14 | Hidden, |
| 15 | Muted, |
| 16 | Locked, |
| 17 | } |
| 18 | |
| 19 | /// Whether a seam that just lost its drag owes a disk write -- exactly the |
| 20 | /// two persisted seams ([`Split::PERSISTED`]), same rule `drag_release`'s own |
| 21 | /// `matches!` already lived by. Pulled out to its own free function, taking |
| 22 | /// no `Context`, so [`Player::drag_left_window`]'s save-on-leave guard is |
| 23 | /// checkable without a `TestAppContext` this test binary has none of (see |
| 24 | /// `tests/media.rs`'s own note on the same limit) -- the wiring that calls it |
| 25 | /// from a live `MouseExitEvent` still cannot be, and is proven by driving |
| 26 | /// instead. |
| 27 | pub(crate) fn split_drag_owes_save(split: Option<Split>) -> bool { |
| 28 | split.is_some_and(|split| Split::PERSISTED.contains(&split)) |
| 29 | } |
| 30 | |
| 31 | /// How far (px) the pointer must travel from the press before a seam follows it. |
| 32 | pub(crate) const SPLIT_CLICK_SLOP: f32 = 3.; |
| 33 | |
| 34 | /// Whether the pointer has gone far enough from where a seam was pressed to call it a drag. |
| 35 | pub(crate) fn split_travelled(origin: Point<Pixels>, at: Point<Pixels>) -> bool { |
| 36 | let d = at - origin; |
| 37 | f32::from(d.x).hypot(f32::from(d.y)) >= SPLIT_CLICK_SLOP |
| 38 | } |
| 39 | |
| 40 | impl Player { |
| 41 | /// Whether the seam being held has been dragged, not merely clicked. |
| 42 | fn split_moved(&self) -> bool { |
| 43 | self.split_press.is_some_and(|(_, moved)| moved) |
| 44 | } |
| 45 | |
| 46 | /// Splits the clip under the playhead. Metadata only: the timeline->source |
| 47 | /// mapping is unchanged, so nothing reseeks and no flag is touched. |
| 48 | pub(crate) fn cut(&mut self, cx: &mut Context<Self>) { |
| 49 | if self.exporting().is_some() { |
| 50 | return; |
| 51 | } |
| 52 | if let Some(lane) = self.marked_locked_lane() { |
| 53 | self.lane_locked_refusal(lane); |
| 54 | return; |
| 55 | } |
| 56 | // Snapped to the source's own grid where one is within reach |
| 57 | // ([`Player::cut_frame`]): a cut a third of a second off a sync point |
| 58 | // looks identical on the bed and turns an export that copies its |
| 59 | // picture in minutes into one that codes every frame of it for hours. |
| 60 | // The playhead goes with it -- what was cut has to be where the line |
| 61 | // is, or the next stroke acts a few frames from where it looks. |
| 62 | let Some(session) = &self.session else { |
| 63 | return; |
| 64 | }; |
| 65 | let now = frame_at(session.now(), self.fps); |
| 66 | let at = self.cut_frame(now); |
| 67 | if at != now { |
| 68 | self.seek(f64::from(at) / self.fps, cx); |
| 69 | } |
| 70 | if let Some(session) = &mut self.session { |
| 71 | // The ledger's last action (DESIGN §5) reads the notice queue's |
| 72 | // back, and a split used to leave nothing there -- two clips |
| 73 | // appeared on the bench while the ledger still named whatever |
| 74 | // came before. `NOTICE_TELL`'s grey "told you" (§8) self-fades, |
| 75 | // so this does not linger over the picture; it just gives the |
| 76 | // ledger something true to say. Held `,`/`.`/`[`/`]` (walk, trim) |
| 77 | // stay silent on purpose -- those fire many times a second and a |
| 78 | // notice per keystroke would turn the ledger into a firehose, |
| 79 | // exactly what §8's "one at a time" is against; a single `s` is |
| 80 | // not that. |
| 81 | let cut = session.cut_at(f64::from(at) / self.fps); |
| 82 | if cut { |
| 83 | // Only a split that happened drops the selection: a refusal is |
| 84 | // "Nothing to split", and the clip in hand is still the clip the |
| 85 | // next Delete, `x` or menu acts on -- clearing it there left the |
| 86 | // user with nothing picked and told them nothing about it. A |
| 87 | // real split is the other way round: the cut it leaves makes the |
| 88 | // picked index name a different clip, so the mark has to go. |
| 89 | // [`Player::regroup`] below has always cleared inside its own |
| 90 | // success branch; this is the same rule on the other half of the |
| 91 | // pair. |
| 92 | self.selected.clear(); |
| 93 | self.notify_user("Split".into()); |
| 94 | } else { |
| 95 | self.notify_user("Nothing to split — the playhead is already on a cut".into()); |
| 96 | } |
| 97 | } |
| 98 | cx.notify(); |
| 99 | } |
| 100 | |
| 101 | /// Rejoins whatever meets under the playhead and puts it back in one group |
| 102 | /// -- the inverse of [`Player::cut`], and metadata only like it. The engine |
| 103 | /// decides what is joinable; a refusal is worded here, because `false` is |
| 104 | /// all it says and a key that looks broken is worse than one that explains |
| 105 | /// itself. |
| 106 | pub(crate) fn regroup(&mut self, cx: &mut Context<Self>) { |
| 107 | if self.exporting().is_some() { |
| 108 | return; |
| 109 | } |
| 110 | if let Some(session) = &mut self.session { |
| 111 | if session.regroup_at(session.now()) { |
| 112 | self.selected.clear(); |
| 113 | } else { |
| 114 | self.notify_user("Nothing to rejoin — no cut under the playhead".into()); |
| 115 | } |
| 116 | } |
| 117 | cx.notify(); |
| 118 | } |
| 119 | |
| 120 | /// Takes the selected clip out of its group, so the picture and the sound |
| 121 | /// under it are edited apart from here on: each half selects, moves, trims |
| 122 | /// and is removed alone, and both draw outlined instead of tinted. The |
| 123 | /// selection stays -- the half that was clicked is still the half in hand. |
| 124 | pub(crate) fn detach(&mut self, cx: &mut Context<Self>) { |
| 125 | if self.exporting().is_some() { |
| 126 | return; |
| 127 | } |
| 128 | match (&mut self.session, self.selected.anchor()) { |
| 129 | (Some(session), Some((lane, idx))) => { |
| 130 | if !session.ungroup(lane, idx) { |
| 131 | self.notify_user( |
| 132 | "Nothing detached — that clip is not grouped with another".into(), |
| 133 | ); |
| 134 | } |
| 135 | } |
| 136 | (Some(_), None) => self.notify_user("Nothing detached — nothing selected".into()), |
| 137 | (None, _) => {} |
| 138 | } |
| 139 | cx.notify(); |
| 140 | } |
| 141 | |
| 142 | /// Puts the selection into one group. Two paths, by how much of a |
| 143 | /// selection there is: |
| 144 | /// |
| 145 | /// * **One pick** -- the way back from [`Player::detach`], and the way to |
| 146 | /// group a picture with sound it was never opened with. The partner is |
| 147 | /// not clicked because there is nothing to choose: the clip covering |
| 148 | /// exactly the same frames on another track, and the engine words what to |
| 149 | /// do when none does. |
| 150 | /// |
| 151 | /// * **A ctrl-click selection** -- the group a hand builds: every pick it |
| 152 | /// names, clips and captions alike, into one id, whatever frames each of |
| 153 | /// them covers. The members keep their own offsets, which is the point: |
| 154 | /// from here they move, trim and delete together. |
| 155 | pub(crate) fn group(&mut self, cx: &mut Context<Self>) { |
| 156 | if self.exporting().is_some() { |
| 157 | return; |
| 158 | } |
| 159 | // The picks the selection holds that still name something: a stale |
| 160 | // index (a stroke nobody saw) is not a thing to group. |
| 161 | let picks = self.marks().0; |
| 162 | match (&mut self.session, self.selected.anchor(), picks.len()) { |
| 163 | (_, None, _) => self.notify_user("Nothing grouped — nothing selected".into()), |
| 164 | // The hand's group: every pick, one id. |
| 165 | (Some(session), _, 2..) => { |
| 166 | if let Err(e) = session.group_all(&picks) { |
| 167 | self.notify_user(format!("Not grouped — {e}").into()); |
| 168 | } |
| 169 | } |
| 170 | // One pick that no longer names anything. |
| 171 | (Some(_), Some(_), 0) => { |
| 172 | self.notify_user("Nothing grouped — that clip is no longer there".into()) |
| 173 | } |
| 174 | // One pick: the partner flow it always was. |
| 175 | (Some(session), Some((lane, idx)), _) => match span_partner(session, lane, idx) { |
| 176 | Some((other, o_idx)) => { |
| 177 | if let Err(e) = session.group(lane, idx, other, o_idx) { |
| 178 | self.notify_user(format!("Not grouped — {e}").into()); |
| 179 | } |
| 180 | } |
| 181 | None => { |
| 182 | self.notify_user("Nothing to group with — no clip covers these frames".into()) |
| 183 | } |
| 184 | }, |
| 185 | (None, ..) => {} |
| 186 | } |
| 187 | cx.notify(); |
| 188 | } |
| 189 | |
| 190 | /// Drops the selected clip and closes the hole: a whole take goes, both |
| 191 | /// lanes of it, and everything after it moves up. A half with no take under |
| 192 | /// it in the video lane -- what a lift leaves behind -- has nothing to |
| 193 | /// ripple, so that one is lifted instead. The engine reseeks itself, so all |
| 194 | /// this owes is the flag reset. |
| 195 | pub(crate) fn delete_selected(&mut self, cx: &mut Context<Self>) { |
| 196 | if self.exporting().is_some() { |
| 197 | return; |
| 198 | } |
| 199 | if let Some(lane) = self.marked_locked_lane() { |
| 200 | self.lane_locked_refusal(lane); |
| 201 | return; |
| 202 | } |
| 203 | // A title selected in the Clip tab (no clip picked) is what the key takes off. |
| 204 | if self.title_target().is_some() { |
| 205 | self.remove_title_selected(cx); |
| 206 | return; |
| 207 | } |
| 208 | let selected = self.selected.anchor(); |
| 209 | self.selected.clear(); |
| 210 | // A caption is marked in its own lane's index space -- that lane holds |
| 211 | // no `Clip` -- so the same key reaches it through the one removal a |
| 212 | // placed subtitle has ([`Player::lift_sub`], one undo step), unless a |
| 213 | // hand put it in a group: then it goes the way its group goes. |
| 214 | // Parity: the box on a subtitle lane goes by the stroke every other box |
| 215 | // goes by. |
| 216 | if let Some((lane, idx)) = selected.filter(|(lane, _)| lane.kind == LaneKind::Subtitle) { |
| 217 | // A caption carrying a group id is a member, and a member goes the |
| 218 | // way its group goes -- the engine's own door decides how much of |
| 219 | // that is media (and so a reseek) and how much is a lift. |
| 220 | let grouped = self.session.as_ref().is_some_and(|session| { |
| 221 | session |
| 222 | .sub_lane(lane) |
| 223 | .get(idx) |
| 224 | .is_some_and(|s| s.link.is_some()) |
| 225 | }); |
| 226 | match grouped { |
| 227 | true => { |
| 228 | if self |
| 229 | .session |
| 230 | .as_mut() |
| 231 | .is_some_and(|session| session.delete_sub(lane, idx)) |
| 232 | { |
| 233 | self.reset_after_reseek(); |
| 234 | self.notify_user_action("Caption deleted".into(), ToastAction::Undo); |
| 235 | } |
| 236 | } |
| 237 | false => self.lift_sub(lane, idx, cx), |
| 238 | } |
| 239 | cx.notify(); |
| 240 | return; |
| 241 | } |
| 242 | // Whichever lane it was clicked in: the index is that lane's own, and |
| 243 | // the engine cuts what the clip covers out of the lanes it covers -- a |
| 244 | // lone clip's span out of every lane, a grouped member's own span out |
| 245 | // of its own lane and its caption members off their lanes. What is not |
| 246 | // a whole take is lifted instead, which is what reaches a clip on an |
| 247 | // added track ([`whole_take`]). |
| 248 | let deleted = match (&mut self.session, selected) { |
| 249 | (Some(session), Some((lane, idx))) => match whole_take(session, lane, idx) { |
| 250 | true => session.delete_clip(lane, idx).then_some("Clip deleted"), |
| 251 | false => session.lift_clip(lane, idx).then_some("Clip lifted"), |
| 252 | }, |
| 253 | _ => None, |
| 254 | }; |
| 255 | if selected.is_some() && deleted.is_none() { |
| 256 | self.notify_user("Nothing deleted — that clip is no longer there".into()); |
| 257 | } |
| 258 | if let Some(text) = deleted { |
| 259 | self.reset_after_reseek(); |
| 260 | self.notify_user_action(text.into(), ToastAction::Undo); |
| 261 | } |
| 262 | cx.notify(); |
| 263 | } |
| 264 | |
| 265 | /// The placements linked to `target` -- one take with it -- on every lane |
| 266 | /// they sit on, `target` itself left out. Empty for a clip in no group. |
| 267 | pub(crate) fn linked_partners(&self, target: (Lane, usize)) -> Vec<(Lane, usize)> { |
| 268 | let Some(session) = self.session.as_ref() else { |
| 269 | return Vec::new(); |
| 270 | }; |
| 271 | let link = match target.0.kind { |
| 272 | LaneKind::Subtitle => session.sub_lane(target.0).get(target.1).and_then(|s| s.link), |
| 273 | LaneKind::Video | LaneKind::Audio => { |
| 274 | session.lane_clips(target.0).get(target.1).and_then(|c| c.link) |
| 275 | } |
| 276 | }; |
| 277 | let Some(link) = link else { |
| 278 | return Vec::new(); |
| 279 | }; |
| 280 | let mut out = Vec::new(); |
| 281 | for lane in session.lanes() { |
| 282 | if session.lane_locked(lane) { |
| 283 | continue; |
| 284 | } |
| 285 | match lane.kind { |
| 286 | LaneKind::Subtitle => out.extend( |
| 287 | session |
| 288 | .sub_lane(lane) |
| 289 | .iter() |
| 290 | .enumerate() |
| 291 | .filter(|(_, s)| s.link == Some(link)) |
| 292 | .map(|(i, _)| (lane, i)), |
| 293 | ), |
| 294 | LaneKind::Video | LaneKind::Audio => out.extend( |
| 295 | session |
| 296 | .lane_clips(lane) |
| 297 | .iter() |
| 298 | .enumerate() |
| 299 | .filter(|(_, c)| c.link == Some(link)) |
| 300 | .map(|(i, _)| (lane, i)), |
| 301 | ), |
| 302 | } |
| 303 | } |
| 304 | out.retain(|&p| p != target); |
| 305 | out |
| 306 | } |
| 307 | |
| 308 | /// A plain click on a clip selects its whole take: a click on either half |
| 309 | /// picks both, with the one clicked last, so Delete and Lift act on the pair. |
| 310 | /// `alt` picks the one half alone, as Premiere does. |
| 311 | pub(crate) fn select_take(&mut self, target: (Lane, usize), alt: bool, cx: &mut Context<Self>) { |
| 312 | self.select(target, cx); |
| 313 | // A refused select (a locked lane) leaves the selection as it was, and so does Alt. |
| 314 | if alt || self.selected.anchor() != Some(target) { |
| 315 | return; |
| 316 | } |
| 317 | let mut picks = Selection::new(); |
| 318 | for partner in self.linked_partners(target) { |
| 319 | picks.add(partner); |
| 320 | } |
| 321 | picks.add(target); |
| 322 | self.selected = picks; |
| 323 | cx.notify(); |
| 324 | } |
| 325 | |
| 326 | /// A press on a clip: a press on a clip already picked keeps the set (a set |
| 327 | /// drag needs it), and any other plain press selects the take as a click does. |
| 328 | /// Ctrl and Alt presses go through [`Player::pick`] as they always did. |
| 329 | pub(crate) fn press_take(&mut self, target: (Lane, usize), ctrl: bool, alt: bool, cx: &mut Context<Self>) { |
| 330 | if ctrl || alt || self.selected.contains(target) { |
| 331 | self.pick(target, ctrl, cx); |
| 332 | return; |
| 333 | } |
| 334 | self.select_take(target, false, cx); |
| 335 | } |
| 336 | |
| 337 | /// Lifts the selected half out and leaves the hole: black picture there if |
| 338 | /// it was the video lane, silence if it was the audio one, and nothing else |
| 339 | /// moves. A half of a take lifts its partners too, in one undo step, so the |
| 340 | /// take is never left with a hole on one lane only. What Delete is not. |
| 341 | pub(crate) fn lift_selected(&mut self, cx: &mut Context<Self>) { |
| 342 | if self.exporting().is_some() { |
| 343 | return; |
| 344 | } |
| 345 | if let Some(lane) = self.marked_locked_lane() { |
| 346 | self.lane_locked_refusal(lane); |
| 347 | return; |
| 348 | } |
| 349 | let selected = self.selected.anchor(); |
| 350 | let lifts: Vec<(Lane, usize)> = match selected { |
| 351 | Some(anchor) => std::iter::once(anchor).chain(self.linked_partners(anchor)).collect(), |
| 352 | None => Vec::new(), |
| 353 | }; |
| 354 | self.selected.clear(); |
| 355 | match (&mut self.session, selected) { |
| 356 | (Some(session), Some(_)) => { |
| 357 | session.begin_batch(); |
| 358 | let lifted = lifts |
| 359 | .iter() |
| 360 | .filter(|(lane, _)| lane.kind != LaneKind::Subtitle) |
| 361 | .fold(false, |any, &(lane, idx)| session.lift_clip(lane, idx) | any); |
| 362 | session.end_batch(); |
| 363 | if lifted { |
| 364 | self.reset_after_reseek(); |
| 365 | self.notify_user_action("Clip lifted".into(), ToastAction::Undo); |
| 366 | } else { |
| 367 | self.notify_user("Nothing lifted — that half is no longer there".into()); |
| 368 | } |
| 369 | } |
| 370 | (Some(_), None) => self.notify_user("Nothing lifted — nothing selected".into()), |
| 371 | (None, _) => {} |
| 372 | } |
| 373 | cx.notify(); |
| 374 | } |
| 375 | |
| 376 | /// Copies the selection: one entry for a plain click, the whole |
| 377 | /// ctrl-click set -- every pick, its own lane and its own frames -- for |
| 378 | /// one made over more than a clip. Out of the lane each was clicked in: |
| 379 | /// the audio half of a group is a different clip from the video one, and |
| 380 | /// copying the wrong lane's frames is a paste of the wrong thing. Nothing |
| 381 | /// on screen changes, so no notify. Empty rather than touched at all for |
| 382 | /// a selection naming nothing any more, so a stale Copy leaves the |
| 383 | /// clipboard exactly as a stale Paste would find it. |
| 384 | pub(crate) fn copy_selected(&mut self) { |
| 385 | let Some(session) = self.session.as_ref() else { |
| 386 | return; |
| 387 | }; |
| 388 | let picks: Vec<(Lane, Clip)> = self |
| 389 | .selected |
| 390 | .picks() |
| 391 | .iter() |
| 392 | .filter_map(|&(lane, idx)| Some((lane, session.lane_clips(lane).get(idx).cloned()?))) |
| 393 | .collect(); |
| 394 | if !picks.is_empty() { |
| 395 | self.clipboard = picks; |
| 396 | self.copied_look = self.capture_look(); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /// The picks and their group ids, in click order: the two halves of what |
| 401 | /// [`marked`] asks about every box on the bed -- which picks there are, and |
| 402 | /// which groups they carry (a caption's included, which is what marks the |
| 403 | /// clip it was pinned to). Picks an edit has left stale name nothing and |
| 404 | /// contribute no id. |
| 405 | pub(crate) fn marks(&self) -> (Vec<(Lane, usize)>, Vec<Option<u32>>) { |
| 406 | let Some(session) = &self.session else { |
| 407 | return (Vec::new(), Vec::new()); |
| 408 | }; |
| 409 | let (mut picks, mut links) = (Vec::new(), Vec::new()); |
| 410 | for &(lane, idx) in self.selected.picks() { |
| 411 | let link = match lane.kind { |
| 412 | LaneKind::Subtitle => session.sub_lane(lane).get(idx).map(|s| s.link), |
| 413 | _ => session.lane_clips(lane).get(idx).map(|c| c.link), |
| 414 | }; |
| 415 | if let Some(link) = link { |
| 416 | picks.push((lane, idx)); |
| 417 | links.push(link); |
| 418 | } |
| 419 | } |
| 420 | (picks, links) |
| 421 | } |
| 422 | |
| 423 | /// Drops the copied clip -- or the whole copied set -- in at the |
| 424 | /// playhead. A single clipboard entry takes the door it always has |
| 425 | /// ([`PlaybackSession::paste_at`]): across `V1`/`A1`, splitting whatever |
| 426 | /// it lands inside of and rippling the room open, byte-identical to |
| 427 | /// before a set could be copied at all. More than one takes |
| 428 | /// [`PlaybackSession::paste_set_at`] instead: every member lands on the |
| 429 | /// lane it was copied off, at the same distance from the others it had on |
| 430 | /// the bed it was copied from, refused whole rather than opening room -- |
| 431 | /// see [`Project::paste_set`] for why a set-paste cannot ripple. The |
| 432 | /// engine reseeks itself either way, so like a delete this owes the flag |
| 433 | /// reset -- and the selection, whose index the insert has just moved. |
| 434 | pub(crate) fn paste(&mut self, cx: &mut Context<Self>) { |
| 435 | // Paste lands on the lanes the copied items came from, so one of them |
| 436 | // being locked refuses the whole paste. |
| 437 | let locked = self |
| 438 | .clipboard |
| 439 | .iter() |
| 440 | .map(|&(lane, _)| lane) |
| 441 | .find(|&lane| self.session.as_ref().is_some_and(|s| s.lane_locked(lane))); |
| 442 | if let Some(lane) = locked { |
| 443 | self.lane_locked_refusal(lane); |
| 444 | return; |
| 445 | } |
| 446 | let pasted = match (&mut self.session, self.clipboard.as_slice()) { |
| 447 | (Some(session), [(_, clip)]) => session.paste_at(session.now(), clip.clone()), |
| 448 | (Some(session), items) if !items.is_empty() => { |
| 449 | session.paste_set_at(session.now(), items) |
| 450 | } |
| 451 | _ => false, |
| 452 | }; |
| 453 | if pasted { |
| 454 | self.selected.clear(); |
| 455 | self.reset_after_reseek(); |
| 456 | } else if self.clipboard.len() > 1 { |
| 457 | // The set-paste's own refusal, worded like a set-drag's |
| 458 | // ([`Player::move_clip`]) -- a single-clip paste stays silent on |
| 459 | // failure exactly as it always has. |
| 460 | self.notify_user( |
| 461 | "Not pasted — another clip already covers where one of the copied clips would \ |
| 462 | land" |
| 463 | .into(), |
| 464 | ); |
| 465 | } |
| 466 | cx.notify(); |
| 467 | } |
| 468 | |
| 469 | /// A clip let go at window `x` over lane `to`: it lands with its head where |
| 470 | /// the hand is carrying it ([`Player::drop_frame`]), on the track it was |
| 471 | /// dropped on, taking its whole take with it -- one undo step for the |
| 472 | /// gesture. Dropped back where it was picked up it is not an edit at all, so |
| 473 | /// nothing is said about it. The engine reseeks, so all this owes is the |
| 474 | /// flag reset -- and the selection, whose index was that lane's own and now |
| 475 | /// names a different clip there. |
| 476 | /// |
| 477 | /// A dragged clip that is *itself* one of the selection's picks carries the |
| 478 | /// whole selection with it: every other pick moves the same frame delta, |
| 479 | /// all-or-nothing ([`PlaybackSession::move_selection_to`]), and the picks |
| 480 | /// survive the move -- their indices remapped rather than cleared, since a |
| 481 | /// pick that travels still names the clip it named before. A clip dragged |
| 482 | /// from outside the selection moves alone, exactly as it always has. |
| 483 | pub(crate) fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, x: Pixels, cx: &mut Context<Self>) { |
| 484 | self.named_step("Move clip", |this| this.move_clip_now(from, idx, to, x, cx)); |
| 485 | } |
| 486 | |
| 487 | fn move_clip_now(&mut self, from: Lane, idx: usize, to: Lane, x: Pixels, cx: &mut Context<Self>) { |
| 488 | // The drop ends the gesture whatever it does: the original is no |
| 489 | // longer carried, and the line promised a landing that is now made. |
| 490 | self.drag_src = None; |
| 491 | self.snap_cue = None; |
| 492 | // A shift-drag is a slip or a slide and its release lands in |
| 493 | // `drag_release`: the gpui drag the body also starts must not move it. |
| 494 | if self.exporting().is_some() || self.pro_drag.is_some() { |
| 495 | return; |
| 496 | } |
| 497 | // Lifted off a locked lane or dropped onto one: both are refused. |
| 498 | if self.lane_locked_refusal(from) || self.lane_locked_refusal(to) { |
| 499 | return; |
| 500 | } |
| 501 | let (Some((start, _)), Some(was)) = ( |
| 502 | self.drop_frame(from, idx, x), |
| 503 | self.session |
| 504 | .as_ref() |
| 505 | .and_then(|session| session.lane_clips(from).get(idx).map(|c| c.start)), |
| 506 | ) else { |
| 507 | return; |
| 508 | }; |
| 509 | // A clip let go on another lane lands where the hand asked or not at all. The |
| 510 | // engine would clamp the travel into the gap instead, and that clamp moves the |
| 511 | // clip's linked partners in time along with it: a drop onto a lane beside |
| 512 | // another clip must not shift anything else. |
| 513 | if to != from { |
| 514 | let len = self |
| 515 | .session |
| 516 | .as_ref() |
| 517 | .and_then(|session| session.lane_clips(from).get(idx).map(|c| c.frames())) |
| 518 | .unwrap_or(0); |
| 519 | let neighbours = self.lane_neighbours(from, idx, to); |
| 520 | if !Self::cross_lane_fits(start, len, &neighbours) { |
| 521 | self.drag_lane = None; |
| 522 | self.notify_user( |
| 523 | format!("Not moved — another clip already covers those frames on {}", to.label()) |
| 524 | .into(), |
| 525 | ); |
| 526 | cx.notify(); |
| 527 | return; |
| 528 | } |
| 529 | } |
| 530 | let set_move = self.selected.contains((from, idx)) && self.selected.len() > 1; |
| 531 | let picks: Vec<(Lane, usize)> = self.selected.picks().to_vec(); |
| 532 | // Every pick's own start, read before the move touches anything -- the |
| 533 | // only way to find a pick again afterwards, since a lane change or an |
| 534 | // insert can move the index it was named by. |
| 535 | let pre_starts: Vec<Option<u32>> = picks |
| 536 | .iter() |
| 537 | .map(|&(lane, i)| { |
| 538 | self.session |
| 539 | .as_ref() |
| 540 | .and_then(|session| session.lane_clips(lane).get(i).map(|c| c.start)) |
| 541 | }) |
| 542 | .collect(); |
| 543 | let moved = self.session.as_mut().is_some_and(|session| match set_move { |
| 544 | true => session.move_selection_to(&picks, from, idx, to, start), |
| 545 | false => session.move_clip_to(from, idx, to, start), |
| 546 | }); |
| 547 | let (kind, lanes) = match from.kind { |
| 548 | LaneKind::Video => ("picture", "video"), |
| 549 | LaneKind::Audio => ("sound", "audio"), |
| 550 | // Never dragged from here yet -- a subtitle lane holds no `Clip`, |
| 551 | // so `lane_clips` is empty and the drag above never starts on one. |
| 552 | LaneKind::Subtitle => ("caption", "subtitle"), |
| 553 | }; |
| 554 | match moved { |
| 555 | true => { |
| 556 | // The arm rides the door's own answer: `move_clip_to` / |
| 557 | // `move_selection_to` answer `false` for a clip let go where it |
| 558 | // was picked up (`Project::move_selection` refuses before its |
| 559 | // snapshot) and for a lane of the wrong kind, and the arms below |
| 560 | // say both in words. Armed before the call it dirtied a project |
| 561 | // nobody had touched -- the ledger's `unsaved` ghost and the |
| 562 | // save-on-close question over an unchanged file. |
| 563 | self.mark_dirty(); |
| 564 | // Every pick named a `(lane, ...)` by index into that lane's |
| 565 | // clips, sorted by start -- exactly what an insert or a lane |
| 566 | // change reorders. Re-read by the frame each pick's clip now |
| 567 | // starts at (recorded before the move moved anything), so the |
| 568 | // selection survives naming the clips it named, not the slots |
| 569 | // they used to sit in. |
| 570 | if set_move { |
| 571 | self.selected = |
| 572 | self.remap_selection(&picks, &pre_starts, from, idx, to, (was, start)); |
| 573 | } else { |
| 574 | self.selected.clear(); |
| 575 | } |
| 576 | self.reset_after_reseek(); |
| 577 | // The strip names the edit that just happened: a move that |
| 578 | // lands silently leaves the last word (`Split`) standing, and |
| 579 | // an editor reads that as the drag having done nothing. |
| 580 | self.notify_user("Moved".into()); |
| 581 | } |
| 582 | // The three ways a drag is refused, told apart by what the |
| 583 | // front-end already knows: a lane's kind, and where the clip was. |
| 584 | // Everything else that could refuse (a clip that is not there) |
| 585 | // cannot be dragged. |
| 586 | false if from.kind != to.kind => self.notify_user( |
| 587 | format!("Not on {} — a {kind} clip takes a {lanes} lane", to.label()).into(), |
| 588 | ), |
| 589 | // Picked up and put back down where it was: a click, and a click |
| 590 | // says nothing. |
| 591 | false if from == to && start == was => {} |
| 592 | false if set_move => self.notify_user( |
| 593 | format!( |
| 594 | "Not moved — {} clips selected, and another clip already covers where one of \ |
| 595 | them would land on {}", |
| 596 | picks.len(), |
| 597 | to.label() |
| 598 | ) |
| 599 | .into(), |
| 600 | ), |
| 601 | false => self.notify_user( |
| 602 | format!( |
| 603 | "Not moved — another clip already covers those frames on {}", |
| 604 | to.label() |
| 605 | ) |
| 606 | .into(), |
| 607 | ), |
| 608 | } |
| 609 | // The gesture is over: the lane it ended over is nobody's answer any |
| 610 | // more (`Player::drag_lane`, read by `bench-content`'s catch-all). |
| 611 | self.drag_lane = None; |
| 612 | cx.notify(); |
| 613 | } |
| 614 | |
| 615 | /// What a selection's picks are called after a set-move that shifted every |
| 616 | /// one of them by `landed - was` timeline frames (the delta the dragged |
| 617 | /// clip's own head travelled, which every pick travelled too -- see |
| 618 | /// [`Project::move_selection`]): the clip a pick named is found again by |
| 619 | /// where it now starts, on the lane it stays on (or `to`, for the one |
| 620 | /// that changed lane), rather than by the index it had, which a lane |
| 621 | /// change or an insert can have moved. A pick whose clip cannot be found |
| 622 | /// there any more (a bad index handed in) is dropped rather than guessed |
| 623 | /// at. |
| 624 | fn remap_selection( |
| 625 | &self, |
| 626 | picks: &[(Lane, usize)], |
| 627 | pre_starts: &[Option<u32>], |
| 628 | from: Lane, |
| 629 | dragged: usize, |
| 630 | to: Lane, |
| 631 | (was, landed): (u32, u32), |
| 632 | ) -> Selection { |
| 633 | let mut out = Selection::new(); |
| 634 | let Some(session) = self.session.as_ref() else { |
| 635 | return out; |
| 636 | }; |
| 637 | let delta = i64::from(landed) - i64::from(was); |
| 638 | for (&pick, &old_start) in picks.iter().zip(pre_starts) { |
| 639 | let Some(old_start) = old_start else { |
| 640 | continue; |
| 641 | }; |
| 642 | let want = (i64::from(old_start) + delta) as u32; |
| 643 | // Every pick but the dragged clip itself stays on the lane it was |
| 644 | // already on; only the exact clip the hand let go of changed |
| 645 | // lane, so only its own pick is found on `to` rather than `from`. |
| 646 | let now_lane = if pick == (from, dragged) { to } else { pick.0 }; |
| 647 | if let Some(found) = session |
| 648 | .lane_clips(now_lane) |
| 649 | .iter() |
| 650 | .position(|c| c.start == want) |
| 651 | { |
| 652 | out.add((now_lane, found)); |
| 653 | } |
| 654 | } |
| 655 | out |
| 656 | } |
| 657 | |
| 658 | /// The whole of a palette track as a placement: from its own first |
| 659 | /// microsecond to its last cue, and as many timeline frames as that is worth |
| 660 | /// at this rate. What a row dragged out of the Subtitles list carries, and |
| 661 | /// what the ghost is drawn from -- `start` is ignored by |
| 662 | /// [`PlaybackSession::place_sub`], which takes the frame the hand let go on. |
| 663 | /// |
| 664 | /// `None` for a track with nothing to place: one that could not be read, and |
| 665 | /// one that genuinely has no cues. |
| 666 | pub(crate) fn sub_of_track(&self, track: usize) -> Option<SubClip> { |
| 667 | let cues = &self.session.as_ref()?.subtitles().get(track)?.cues; |
| 668 | let end_us = cues.iter().map(|c| c.end_us).max()?; |
| 669 | // A palette row arrives in no group: pinning its words to a clip is |
| 670 | // a hand's decision, made with the Group row afterwards. |
| 671 | Some(SubClip::whole_track(track, end_us, self.fps)) |
| 672 | } |
| 673 | |
| 674 | /// A palette row let go at window `x` over subtitle lane `to`: the whole |
| 675 | /// track goes down where the hand left it, one undo step |
| 676 | /// ([`PlaybackSession::place_sub`]). Every refusal there is words -- an |
| 677 | /// overlap, a lane that is not a subtitle lane, a track that is not there -- |
| 678 | /// and they are shown as the engine worded them. |
| 679 | pub(crate) fn place_sub(&mut self, track: usize, to: Lane, x: Pixels, cx: &mut Context<Self>) { |
| 680 | if self.exporting().is_some() || self.lane_locked_refusal(to) { |
| 681 | return; |
| 682 | } |
| 683 | self.snap_cue = None; |
| 684 | self.ghost.clear(); |
| 685 | let at = self.place_frame(x).0; |
| 686 | // What the mark is on, before the lane is renumbered: a caption's start |
| 687 | // frame is its name on its lane ([`sub_mark`]). |
| 688 | let marked = self |
| 689 | .selected |
| 690 | .anchor() |
| 691 | .filter(|&(lane, _)| lane == to) |
| 692 | .and_then(|(lane, idx)| Some(self.session.as_ref()?.sub_lane(lane).get(idx)?.start)); |
| 693 | let text = match (self.sub_of_track(track), &mut self.session) { |
| 694 | (Some(sub), Some(session)) => match session.place_sub(to, at, sub) { |
| 695 | Ok(()) => None, |
| 696 | Err(e) => Some(format!("Not placed — {e}")), |
| 697 | }, |
| 698 | // The row is greyed and says why in the list; here it says why at |
| 699 | // the moment somebody tried to use it anyway. |
| 700 | (None, Some(_)) => Some("Not placed — that subtitle track has no cues".to_string()), |
| 701 | (_, None) => Some("Not placed — no file open".to_string()), |
| 702 | }; |
| 703 | // The arm rides the door's own answer: `text` is `None` exactly when |
| 704 | // `Project::place_sub` took the track, and every refusal -- an overlap, |
| 705 | // a lane that is not a subtitle lane, "no file open", "no cues" -- |
| 706 | // leaves the project byte-for-byte what it was. Armed ahead of the |
| 707 | // answer it wrote an autosave sidecar, the ledger's `unsaved` ghost and |
| 708 | // the save-on-close question over a project nobody had changed. |
| 709 | if text.is_none() { |
| 710 | self.mark_dirty(); |
| 711 | } |
| 712 | match (text, marked) { |
| 713 | (Some(text), _) => self.notify_user(text.into()), |
| 714 | // It went down, and a lane holds its captions in start order |
| 715 | // ([`Project::place_sub`]'s sorted insert), so one placed *before* |
| 716 | // the marked caption slid it one along and the index left behind |
| 717 | // names its new neighbour -- the caption the next Delete would take. |
| 718 | // The mark goes back on the caption it was on, every *other* pick |
| 719 | // this lane held goes with the indices that moved (they name |
| 720 | // neighbours now), and picks on other lanes keep what they name: |
| 721 | // a placement renumbers this lane alone. |
| 722 | (None, Some(start)) => { |
| 723 | let mark = self |
| 724 | .session |
| 725 | .as_ref() |
| 726 | .and_then(|session| sub_mark(session.sub_lane(to), start)) |
| 727 | .map(|i| (to, i)); |
| 728 | let mut kept = Selection::new(); |
| 729 | for &(lane, idx) in self.selected.picks() { |
| 730 | if lane != to { |
| 731 | kept.add((lane, idx)); |
| 732 | } |
| 733 | } |
| 734 | if let Some(mark) = mark { |
| 735 | kept.add(mark); |
| 736 | } |
| 737 | self.selected = kept; |
| 738 | } |
| 739 | // Nothing marked on this lane, but the placement renumbered it all |
| 740 | // the same: a pick left on a slid index names the wrong caption, |
| 741 | // and the discipline of every renumbering edit applies. |
| 742 | (None, None) => { |
| 743 | let mut kept = Selection::new(); |
| 744 | for &(lane, idx) in self.selected.picks() { |
| 745 | if lane != to { |
| 746 | kept.add((lane, idx)); |
| 747 | } |
| 748 | } |
| 749 | self.selected = kept; |
| 750 | } |
| 751 | } |
| 752 | cx.notify(); |
| 753 | } |
| 754 | |
| 755 | /// Which index its lane holds the dragged placement at *now* |
| 756 | /// ([`Player::dragged`]'s twin, through the same [`live_idx`]): a stroke |
| 757 | /// during the gesture -- an undo, another drop -- moves the indices gpui |
| 758 | /// froze into the payload. |
| 759 | pub(crate) fn dragged_sub(&self, drag: &SubDrag) -> Option<usize> { |
| 760 | live_idx( |
| 761 | self.session.as_ref()?.sub_lane(drag.lane), |
| 762 | drag.idx, |
| 763 | &drag.sub, |
| 764 | ) |
| 765 | } |
| 766 | |
| 767 | /// A placed caption let go at window `x` over subtitle lane `to`: it lands |
| 768 | /// with its head where the hand carried it, on the lane it was dropped on -- |
| 769 | /// one undo step, and none at all for a drop that changed nothing, which is |
| 770 | /// why an `Ok` is never a toast ([`Project::move_sub`]). An overlap and a |
| 771 | /// lane of the wrong kind are refused in the engine's own words. |
| 772 | pub(crate) fn move_sub(&mut self, drag: &SubDrag, to: Lane, x: Pixels, cx: &mut Context<Self>) { |
| 773 | // The drop ends the gesture whatever it does, as `move_clip`'s does: the |
| 774 | // caption stops being dimmed where it stands. |
| 775 | self.drag_src = None; |
| 776 | if self.exporting().is_some() |
| 777 | || self.lane_locked_refusal(drag.lane) |
| 778 | || self.lane_locked_refusal(to) |
| 779 | { |
| 780 | return; |
| 781 | } |
| 782 | self.snap_cue = None; |
| 783 | self.ghost.clear(); |
| 784 | let Some(idx) = self.dragged_sub(drag) else { |
| 785 | return; |
| 786 | }; |
| 787 | // Where the caption stands *now*, so the arm below can tell a move from |
| 788 | // a drag let go where it began: `Project::move_sub` answers `Ok` for a |
| 789 | // pick-up-put-back too ("`Ok`, and no undo step, because nothing |
| 790 | // changed"), so its own answer is not a change report. |
| 791 | let was = self |
| 792 | .session |
| 793 | .as_ref() |
| 794 | .and_then(|session| session.sub_lane(drag.lane).get(idx).map(|sub| sub.start)); |
| 795 | // Asked before the edit, on the indices the press captured: the |
| 796 | // engine's own grouped-with-clips answer, which is exactly when its |
| 797 | // move reseeks -- a caption grouped only with other captions moves no |
| 798 | // media, and a flag reset without a delivered frame would latch the |
| 799 | // seek wait forever. |
| 800 | let grouped = self |
| 801 | .session |
| 802 | .as_ref() |
| 803 | .is_some_and(|session| session.caption_grouped_with_clips(drag.lane, idx)); |
| 804 | let start = self.sub_drop_frame(drag.sub, x).0; |
| 805 | match self |
| 806 | .session |
| 807 | .as_mut() |
| 808 | .map(|session| session.move_sub(drag.lane, idx, to, start)) |
| 809 | { |
| 810 | Some(Err(e)) => { |
| 811 | // Nothing moved ([`Project::move_sub`] refuses before its |
| 812 | // snapshot), so nothing was renumbered and the mark is left |
| 813 | // exactly where it was. Re-reading it off the frame the drop |
| 814 | // *asked* for used to hand it to whatever caption already |
| 815 | // covered that frame -- the drop at the left edge saturates to |
| 816 | // 0 ([`landing`]), so a refused drag marked, and the next |
| 817 | // Delete lifted, a caption the hand never touched. |
| 818 | self.notify_user(format!("Not moved — {e}").into()); |
| 819 | } |
| 820 | // A lane holds its captions in start order, so the drop moved the |
| 821 | // mark as well as the box: it is re-read off where the caption |
| 822 | // landed rather than left on the index it had, which after the move |
| 823 | // is a neighbour's. A grouped-with-clips caption dragged media with |
| 824 | // it -- the engine reseeks itself, and this owes the flag reset a |
| 825 | // clip's drop pays at the same moment. |
| 826 | Some(Ok(())) => { |
| 827 | let mark = self |
| 828 | .session |
| 829 | .as_ref() |
| 830 | .and_then(|session| sub_mark(session.sub_lane(to), start)) |
| 831 | .map(|i| (to, i)); |
| 832 | // The arm rides the caption's own place having moved -- the |
| 833 | // engine's `Ok` covers the put-back that takes no undo step at |
| 834 | // all (`Project::move_sub`), and a project nobody has touched |
| 835 | // must not light the `unsaved` ghost. |
| 836 | let landed = mark.and_then(|(lane, i)| { |
| 837 | self.session |
| 838 | .as_ref()? |
| 839 | .sub_lane(lane) |
| 840 | .get(i) |
| 841 | .map(|sub| (lane, sub.start)) |
| 842 | }); |
| 843 | if landed != was.map(|start| (drag.lane, start)) { |
| 844 | self.mark_dirty(); |
| 845 | } |
| 846 | self.selected = match mark { |
| 847 | Some(mark) => { |
| 848 | let mut sel = Selection::new(); |
| 849 | sel.set_one(mark); |
| 850 | sel |
| 851 | } |
| 852 | None => Selection::new(), |
| 853 | }; |
| 854 | if grouped { |
| 855 | self.reset_after_reseek(); |
| 856 | } |
| 857 | } |
| 858 | None => {} |
| 859 | } |
| 860 | cx.notify(); |
| 861 | } |
| 862 | |
| 863 | /// A caption off its lane, leaving the gap, one undo step |
| 864 | /// ([`PlaybackSession::lift_sub`]). The palette row it played stays in the |
| 865 | /// list, which is what makes this a lift and not a removal -- and the way a |
| 866 | /// subtitle lane is emptied so it can be taken off at all. |
| 867 | /// |
| 868 | /// The one door out for a placed caption: the Delete key |
| 869 | /// ([`Player::delete_selected`]) and the Delete row of its own menu both |
| 870 | /// land here, so there is one removal and one undo step however it is asked |
| 871 | /// for. |
| 872 | pub(crate) fn lift_sub(&mut self, lane: Lane, idx: usize, cx: &mut Context<Self>) { |
| 873 | if self.exporting().is_some() || self.lane_locked_refusal(lane) { |
| 874 | return; |
| 875 | } |
| 876 | let lifted = self |
| 877 | .session |
| 878 | .as_mut() |
| 879 | .is_some_and(|session| session.lift_sub(lane, idx)); |
| 880 | let text = match lifted { |
| 881 | true => format!( |
| 882 | "Caption lifted · undo {}", |
| 883 | self.keymap.display(ActionId::Undo) |
| 884 | ), |
| 885 | false => "Nothing lifted — that caption is not there any more".to_string(), |
| 886 | }; |
| 887 | if lifted { |
| 888 | // The arm rides the door's own answer: `false` is "Nothing lifted — |
| 889 | // that caption is not there any more", and the project is exactly |
| 890 | // what it was, so the flag, the autosave sidecar and the |
| 891 | // save-on-close question must stay off it. |
| 892 | self.mark_dirty(); |
| 893 | // Everything after it on that lane slid down one ([`Vec::remove`]), |
| 894 | // so a mark or an open menu left at or past it names a *different* |
| 895 | // caption now -- and the next Delete would take that one. Both go |
| 896 | // with the caption they were on. |
| 897 | if self |
| 898 | .selected |
| 899 | .anchor() |
| 900 | .is_some_and(|(l, i)| l == lane && i >= idx) |
| 901 | { |
| 902 | self.selected.clear(); |
| 903 | } |
| 904 | self.context_menu = None; |
| 905 | } |
| 906 | // A lifted caption's toast offers Undo (DESIGN §8); a refusal has |
| 907 | // nothing to take back. |
| 908 | match lifted { |
| 909 | true => self.notify_user_action(text.into(), ToastAction::Undo), |
| 910 | false => self.notify_user(text.into()), |
| 911 | } |
| 912 | cx.notify(); |
| 913 | } |
| 914 | |
| 915 | /// A subtitle lane's dot: the one lane whose captions are over the picture. |
| 916 | /// A pick and not a toggle -- picking one puts the one that was showing |
| 917 | /// away, because two lanes of different words at one moment is two plates |
| 918 | /// nobody can read, and it is said out loud for that reason: what the click |
| 919 | /// did *and* what it undid. |
| 920 | /// |
| 921 | /// The lanes it takes off the picture keep their captions and are still |
| 922 | /// exported, every one of them ([`Player::start_export`]). |
| 923 | pub(crate) fn show_sub_lane(&mut self, lane: Lane, cx: &mut Context<Self>) { |
| 924 | let was = self.active_sub_lane(); |
| 925 | self.sub_lane = Some(lane); |
| 926 | let text = match was.filter(|&old| old != lane) { |
| 927 | Some(old) => format!( |
| 928 | "{} is shown — {} is off the picture; every subtitle track is still exported", |
| 929 | lane.label(), |
| 930 | old.label() |
| 931 | ), |
| 932 | None => format!( |
| 933 | "{} is shown — its captions are over the picture", |
| 934 | lane.label() |
| 935 | ), |
| 936 | }; |
| 937 | self.notify_user(text.into()); |
| 938 | cx.notify(); |
| 939 | } |
| 940 | |
| 941 | /// The subtitle lane whose captions are drawn: the pick resolved against the |
| 942 | /// lanes the timeline has right now ([`active_lane`]), so it can never name |
| 943 | /// a lane that is not there. `None` with no subtitle lane at all. |
| 944 | pub(crate) fn active_sub_lane(&self) -> Option<Lane> { |
| 945 | let lanes = self.session.as_ref()?.subtitle_lanes(); |
| 946 | active_lane(self.sub_lane, &lanes) |
| 947 | } |
| 948 | |
| 949 | /// Whether that subtitle lane's captions are drawn -- read by the header |
| 950 | /// that offers the pick and by the plate over the picture. |
| 951 | pub(crate) fn sub_lane_on(&self, lane: Lane) -> bool { |
| 952 | self.active_sub_lane() == Some(lane) |
| 953 | } |
| 954 | |
| 955 | /// Whether one of `lane`'s three head switches is on (DESIGN §7). |
| 956 | pub(crate) fn lane_switch(&self, lane: Lane, which: Switch) -> bool { |
| 957 | let Some(session) = &self.session else { |
| 958 | return false; |
| 959 | }; |
| 960 | match which { |
| 961 | Switch::Hidden => session.lane_hidden(lane), |
| 962 | Switch::Muted => session.lane_muted(lane), |
| 963 | Switch::Locked => session.lane_locked(lane), |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | /// Whether the source patch sends the source monitor's picture (video lane) or sound (audio |
| 968 | /// lane) to `lane` on Insert and Overwrite. A subtitle lane is never patched. |
| 969 | pub(crate) fn source_patch_on(&self, lane: Lane) -> bool { |
| 970 | match lane.kind { |
| 971 | LaneKind::Video => self.patch_video == lane, |
| 972 | LaneKind::Audio => self.patch_audio == lane, |
| 973 | LaneKind::Subtitle => false, |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | /// Whether the source patch boxes show on the lane heads: only while the source monitor has |
| 978 | /// an item loaded. |
| 979 | pub(crate) fn source_patch_shown(&self) -> bool { |
| 980 | self.preview_session.is_some() |
| 981 | } |
| 982 | |
| 983 | /// Points the source patch at `lane`: its kind's half of an Insert or Overwrite goes there |
| 984 | /// from now on. Nothing is edited, so nothing is undone. |
| 985 | pub(crate) fn set_source_patch(&mut self, lane: Lane, cx: &mut Context<Self>) { |
| 986 | match lane.kind { |
| 987 | LaneKind::Video => self.patch_video = lane, |
| 988 | LaneKind::Audio => self.patch_audio = lane, |
| 989 | LaneKind::Subtitle => return, |
| 990 | } |
| 991 | cx.notify(); |
| 992 | } |
| 993 | |
| 994 | /// Flips one of a lane's head switches to `on`. One undo step, from the |
| 995 | /// engine. Deliberately not refused on a locked lane: locking is what |
| 996 | /// turns the lock off again, and hiding or muting a lane edits nothing. |
| 997 | pub(crate) fn set_lane_switch( |
| 998 | &mut self, |
| 999 | lane: Lane, |
| 1000 | which: Switch, |
| 1001 | on: bool, |
| 1002 | cx: &mut Context<Self>, |
| 1003 | ) { |
| 1004 | if self.exporting().is_some() { |
| 1005 | return; |
| 1006 | } |
| 1007 | let changed = match self.session.as_mut() { |
| 1008 | Some(session) => match which { |
| 1009 | Switch::Hidden => session.set_lane_hidden(lane, on), |
| 1010 | Switch::Muted => session.set_lane_muted(lane, on), |
| 1011 | Switch::Locked => session.set_lane_locked(lane, on), |
| 1012 | }, |
| 1013 | None => false, |
| 1014 | }; |
| 1015 | if changed { |
| 1016 | self.mark_dirty(); |
| 1017 | } |
| 1018 | cx.notify(); |
| 1019 | } |
| 1020 | |
| 1021 | /// The lock's door, for every entry point that would edit a lane's items |
| 1022 | /// (DESIGN §7). `true`, and a toast offering Unlock, when `lane` is locked: |
| 1023 | /// the caller returns before it touches anything. |
| 1024 | pub(crate) fn lane_locked_refusal(&mut self, lane: Lane) -> bool { |
| 1025 | let locked = self.session.as_ref().is_some_and(|s| s.lane_locked(lane)); |
| 1026 | if locked { |
| 1027 | self.notify_user_action( |
| 1028 | format!("{} is locked", lane.label()).into(), |
| 1029 | ToastAction::Unlock(lane), |
| 1030 | ); |
| 1031 | } |
| 1032 | locked |
| 1033 | } |
| 1034 | |
| 1035 | /// The first locked lane among the marked items, for the edits that act on |
| 1036 | /// the whole selection (delete, lift, paste's lanes). |
| 1037 | pub(crate) fn marked_locked_lane(&self) -> Option<Lane> { |
| 1038 | let session = self.session.as_ref()?; |
| 1039 | self.marks() |
| 1040 | .0 |
| 1041 | .iter() |
| 1042 | .map(|&(lane, _)| lane) |
| 1043 | .find(|&lane| session.lane_locked(lane)) |
| 1044 | } |
| 1045 | |
| 1046 | /// Where a caption let go at window `x` wants its head: [`Player::drop_frame`]'s |
| 1047 | /// twin, and the same [`landing`] -- the grab offset the press noted, and the |
| 1048 | /// snap onto the edges the rest of the timeline offers, so a caption lands on |
| 1049 | /// the cut it is spoken over. |
| 1050 | pub(crate) fn sub_drop_frame(&self, sub: SubClip, x: Pixels) -> (u32, Option<u32>) { |
| 1051 | let marks = self.snap_targets(None); |
| 1052 | landing( |
| 1053 | self.frame_under(x), |
| 1054 | self.grab, |
| 1055 | sub.frames, |
| 1056 | self.snap_live(), |
| 1057 | self.drop_snap_frames(), |
| 1058 | &marks, |
| 1059 | ) |
| 1060 | } |
| 1061 | |
| 1062 | /// The shadow a caption in the hand would fill, on the lane the pointer is |
| 1063 | /// over: [`Player::preview_ghost`]'s twin, refused for any lane that is not a |
| 1064 | /// subtitle lane -- which is the answer [`Project::move_sub`] gives at the |
| 1065 | /// release. |
| 1066 | pub(crate) fn preview_ghost_sub( |
| 1067 | &mut self, |
| 1068 | drag: &SubDrag, |
| 1069 | to: Lane, |
| 1070 | x: Pixels, |
| 1071 | cx: &mut Context<Self>, |
| 1072 | ) { |
| 1073 | let (start, cue) = self.sub_drop_frame(drag.sub, x); |
| 1074 | let ghost = Ghost { |
| 1075 | lane: to, |
| 1076 | start, |
| 1077 | frames: drag.sub.frames, |
| 1078 | tint: CLIP_TEXT(), |
| 1079 | refused: to.kind != LaneKind::Subtitle, |
| 1080 | }; |
| 1081 | self.set_cue(cue, x, cx); |
| 1082 | self.set_ghost(vec![ghost], cx); |
| 1083 | } |
| 1084 | |
| 1085 | /// The same for a palette row on its way down: it lands at the frame it is |
| 1086 | /// let go on ([`Player::place_frame`]) and is as long as the whole track it |
| 1087 | /// names. |
| 1088 | pub(crate) fn preview_ghost_pick( |
| 1089 | &mut self, |
| 1090 | track: usize, |
| 1091 | to: Lane, |
| 1092 | x: Pixels, |
| 1093 | cx: &mut Context<Self>, |
| 1094 | ) { |
| 1095 | let (start, cue) = self.place_frame(x); |
| 1096 | let ghost = Ghost { |
| 1097 | lane: to, |
| 1098 | start, |
| 1099 | frames: self.sub_of_track(track).map_or(0, |sub| sub.frames), |
| 1100 | tint: CLIP_TEXT(), |
| 1101 | refused: to.kind != LaneKind::Subtitle, |
| 1102 | }; |
| 1103 | self.set_cue(cue, x, cx); |
| 1104 | self.set_ghost(vec![ghost], cx); |
| 1105 | } |
| 1106 | |
| 1107 | /// Where a clip let go at window `x` over lane `to` wants its head: the |
| 1108 | /// frame under the pointer, less however far into the box the hand grabbed |
| 1109 | /// it (so the clip does not jump under the pointer), pulled onto a |
| 1110 | /// neighbouring edge when it lands within [`Player::drop_snap_frames`] of one. `None` when |
| 1111 | /// there is no such clip to move. The engine has the last word on where it |
| 1112 | /// may actually go -- this is the ask, not the answer. |
| 1113 | /// |
| 1114 | /// corner-cut: the bed now runs past the last frame whenever the timeline is |
| 1115 | /// shorter than the view ([`Scale::time_at`] clamps at the head only), so a |
| 1116 | /// clip *can* be dragged out there. Zoomed in against the far end it cannot: |
| 1117 | /// the scroll clamp pins the bed's right edge to the duration, and the |
| 1118 | /// pointer has no pixel past it. The upgrade is to let the scroll clamp |
| 1119 | /// leave a screen of empty bed after the end, the way every NLE does. |
| 1120 | pub(crate) fn drop_frame( |
| 1121 | &self, |
| 1122 | from: Lane, |
| 1123 | idx: usize, |
| 1124 | x: Pixels, |
| 1125 | ) -> Option<(u32, Option<u32>)> { |
| 1126 | let clip = self.session.as_ref()?.lane_clips(from).get(idx).cloned()?; |
| 1127 | let marks = self.snap_targets(Some((from, idx))); |
| 1128 | Some(landing( |
| 1129 | self.frame_under(x), |
| 1130 | self.grab, |
| 1131 | clip.frames(), |
| 1132 | self.snap_live(), |
| 1133 | self.drop_snap_frames(), |
| 1134 | &marks, |
| 1135 | )) |
| 1136 | } |
| 1137 | |
| 1138 | /// The spans on `to` a clip dragged from `from`/`idx` has to fit between: every |
| 1139 | /// clip on `to` but the dragged one and its link group. |
| 1140 | pub(crate) fn lane_neighbours(&self, from: Lane, idx: usize, to: Lane) -> Vec<(u32, u32)> { |
| 1141 | let Some(session) = self.session.as_ref() else { |
| 1142 | return Vec::new(); |
| 1143 | }; |
| 1144 | let dragged = session.lane_clips(from).get(idx).cloned(); |
| 1145 | session |
| 1146 | .lane_clips(to) |
| 1147 | .iter() |
| 1148 | .enumerate() |
| 1149 | .filter(|&(i, clip)| { |
| 1150 | (to, i) != (from, idx) |
| 1151 | && !(clip.link.is_some() |
| 1152 | && dragged.as_ref().is_some_and(|d| clip.link == d.link)) |
| 1153 | }) |
| 1154 | .map(|(_, clip)| (clip.start, clip.end())) |
| 1155 | .collect() |
| 1156 | } |
| 1157 | |
| 1158 | /// Whether a clip `len` frames long, let go on another lane with its head at `at`, |
| 1159 | /// lands exactly there: [`cross_room`] answers `at` itself, not a clamped spot. |
| 1160 | pub(crate) fn cross_lane_fits(at: u32, len: u32, neighbours: &[(u32, u32)]) -> bool { |
| 1161 | cross_room(at, len, neighbours) == Some(at) |
| 1162 | } |
| 1163 | |
| 1164 | /// The same answer for a library row on its way down: nothing is in the hand |
| 1165 | /// yet, so there is no grab offset to take off and no length to snap by -- |
| 1166 | /// the file's own is not known until the engine has placed it -- and only |
| 1167 | /// its head lands. Asked by the line, by the ghost and by the drop itself |
| 1168 | /// ([`Player::insert_source`]), so all three name one frame. |
| 1169 | pub(crate) fn place_frame(&self, x: Pixels) -> (u32, Option<u32>) { |
| 1170 | let marks = self.snap_targets(None); |
| 1171 | landing( |
| 1172 | self.frame_under(x), |
| 1173 | 0, |
| 1174 | 0, |
| 1175 | self.snap_live(), |
| 1176 | self.drop_snap_frames(), |
| 1177 | &marks, |
| 1178 | ) |
| 1179 | } |
| 1180 | |
| 1181 | /// Which index the clip in the hand is at *now*: [`live_idx`] against the |
| 1182 | /// lane the drag named, since a stroke during the gesture moves the indices |
| 1183 | /// gpui froze into the payload. Both halves of a drag ask it -- the line |
| 1184 | /// drawn in flight and the drop that commits -- so the promise and the |
| 1185 | /// landing are made about one clip. |
| 1186 | pub(crate) fn dragged(&self, drag: &ClipDrag) -> Option<usize> { |
| 1187 | let session = self.session.as_ref()?; |
| 1188 | live_idx(session.lane_clips(drag.lane), drag.idx, &drag.clip) |
| 1189 | } |
| 1190 | |
| 1191 | /// The shadow the clip in the hand would fill, on the lane the pointer is |
| 1192 | /// over: its head where [`Player::drop_frame`] says the release will put it |
| 1193 | /// -- the same call the drop makes, so the box drawn and the box committed |
| 1194 | /// are one answer -- and its own length at this zoom. A lane of the other |
| 1195 | /// kind refuses the drop ([`Project::move_clip`]), and the shadow says so |
| 1196 | /// before the release does. |
| 1197 | /// |
| 1198 | /// When the dragged clip is itself one of a multi-pick selection (the same |
| 1199 | /// `set_move` test [`Player::move_clip`] commits by), every other pick |
| 1200 | /// draws its own shadow too, at the delta the anchor above is landing at -- |
| 1201 | /// [`Project::move_selection`]'s own clamp is not reread here, so a wall |
| 1202 | /// that would narrow the group's travel is seen only at the release, not |
| 1203 | /// in the shadow; the anchor's own room is still exact; corner-cut, ceiling |
| 1204 | /// a shadow a member or two too wide on a tight bed, upgrade is exposing |
| 1205 | /// `move_room` to preview against. |
| 1206 | pub(crate) fn preview_ghost( |
| 1207 | &mut self, |
| 1208 | drag: &ClipDrag, |
| 1209 | to: Lane, |
| 1210 | x: Pixels, |
| 1211 | cx: &mut Context<Self>, |
| 1212 | ) { |
| 1213 | let Some(idx) = self.dragged(drag) else { |
| 1214 | self.set_cue(None, x, cx); |
| 1215 | self.set_ghost(Vec::new(), cx); |
| 1216 | return; |
| 1217 | }; |
| 1218 | let Some((asked, cue)) = self.drop_frame(drag.lane, idx, x) else { |
| 1219 | self.set_cue(None, x, cx); |
| 1220 | self.set_ghost(Vec::new(), cx); |
| 1221 | return; |
| 1222 | }; |
| 1223 | // What the release would refuse: a lane of the other kind, and a head |
| 1224 | // let go on top of a take already there ([`Project::move_selection`] |
| 1225 | // changes nothing rather than overwrite one). Drawn as refused so a |
| 1226 | // drop that will not happen is seen before the hand lets go, never as a |
| 1227 | // silent no-op. The neighbours are the destination lane's clips less |
| 1228 | // whatever travels with this one -- itself and its link group. |
| 1229 | let neighbours = self.lane_neighbours(drag.lane, idx, to); |
| 1230 | // Staying on its own lane, the engine clamps the travel into the gap |
| 1231 | // the clip sits in rather than refusing it, so the shadow rests where |
| 1232 | // the release will: flush against the take it was dragged into. Landing |
| 1233 | // on *another* lane there is no clamp: a head that does not fit exactly |
| 1234 | // where the hand asked is refused, because a clamp would shift the |
| 1235 | // clip's linked partners in time and a vertical drag must move nothing |
| 1236 | // in time ([`Player::cross_lane_fits`]). |
| 1237 | let len = drag.clip.frames(); |
| 1238 | let (start, refused) = match to == drag.lane { |
| 1239 | true => (gap_clamp(drag.clip.start, len, asked, &neighbours), false), |
| 1240 | false => (asked, !Self::cross_lane_fits(asked, len, &neighbours)), |
| 1241 | }; |
| 1242 | // The line only stands where the head really lands on the mark: a |
| 1243 | // neighbour's clamp or a refusal moved it off the frame the line names. |
| 1244 | self.set_cue(cue.filter(|_| start == asked && !refused), x, cx); |
| 1245 | let anchor = Ghost { |
| 1246 | lane: to, |
| 1247 | start, |
| 1248 | frames: len, |
| 1249 | tint: self.clip_tint(drag.clip.source), |
| 1250 | refused: drag.lane.kind != to.kind || refused, |
| 1251 | }; |
| 1252 | let mut ghosts = vec![anchor]; |
| 1253 | if self.selected.contains((drag.lane, idx)) && self.selected.len() > 1 { |
| 1254 | let delta = i64::from(start) - i64::from(drag.clip.start); |
| 1255 | for &(lane, i) in self.selected.picks() { |
| 1256 | if (lane, i) == (drag.lane, idx) { |
| 1257 | continue; |
| 1258 | } |
| 1259 | let Some(clip) = self |
| 1260 | .session |
| 1261 | .as_ref() |
| 1262 | .and_then(|session| session.lane_clips(lane).get(i).cloned()) |
| 1263 | else { |
| 1264 | continue; |
| 1265 | }; |
| 1266 | let want = (i64::from(clip.start) + delta).max(0) as u32; |
| 1267 | ghosts.push(Ghost { |
| 1268 | lane, |
| 1269 | start: want, |
| 1270 | frames: clip.frames(), |
| 1271 | tint: self.clip_tint(clip.source), |
| 1272 | refused: anchor.refused, |
| 1273 | }); |
| 1274 | } |
| 1275 | } |
| 1276 | self.set_ghost(ghosts, cx); |
| 1277 | } |
| 1278 | |
| 1279 | /// The line the track in the hand would drop into, on the row the pointer |
| 1280 | /// is over: at that row's top edge when the header is coming up from below |
| 1281 | /// and at its bottom edge when it is going down, which is the slot |
| 1282 | /// [`Player::reorder_lane`] commits to at the release. Nothing at all over |
| 1283 | /// its own row, where a release changes nothing. |
| 1284 | pub(crate) fn preview_lane_drop(&mut self, from: Lane, onto: Lane, cx: &mut Context<Self>) { |
| 1285 | let lanes = self |
| 1286 | .session |
| 1287 | .as_ref() |
| 1288 | .map_or_else(Vec::new, PlaybackSession::lanes); |
| 1289 | let at = |lane: Lane| lanes.iter().position(|&l| l == lane); |
| 1290 | let next = match (at(from), at(onto)) { |
| 1291 | (Some(i), Some(j)) if i != j => Some(LaneDrop { |
| 1292 | lane: onto, |
| 1293 | above: j < i, |
| 1294 | }), |
| 1295 | _ => None, |
| 1296 | }; |
| 1297 | // Only when it has actually changed: a drag move fires on every painted |
| 1298 | // frame, and a redraw per frame that draws the same line is a redraw |
| 1299 | // for nothing. |
| 1300 | if self.lane_drop != next { |
| 1301 | self.lane_drop = next; |
| 1302 | cx.notify(); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | /// The line taken back down again, by the row that drew it and by no other: |
| 1307 | /// the pointer has been carried off `lane`, so the slot it was promising is |
| 1308 | /// no longer the one a release would commit to. |
| 1309 | pub(crate) fn forget_lane_drop(&mut self, lane: Lane, cx: &mut Context<Self>) { |
| 1310 | if self.lane_drop.is_some_and(|d| d.lane == lane) { |
| 1311 | self.lane_drop = None; |
| 1312 | cx.notify(); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /// The same shadow for a library row: its head at [`Player::place_frame`], |
| 1317 | /// which is where the drop inserts it, and the file's own length for its |
| 1318 | /// width -- the length the library row already reports. A file this lane |
| 1319 | /// cannot hold ([`lane_refuses`]) is tinted as refused, which is the answer |
| 1320 | /// the release would give in words. |
| 1321 | pub(crate) fn preview_ghost_asset( |
| 1322 | &mut self, |
| 1323 | path: &Path, |
| 1324 | to: Lane, |
| 1325 | x: Pixels, |
| 1326 | cx: &mut Context<Self>, |
| 1327 | ) { |
| 1328 | let (start, cue) = self.place_frame(x); |
| 1329 | let ghost = Ghost { |
| 1330 | lane: to, |
| 1331 | start, |
| 1332 | frames: self |
| 1333 | .session |
| 1334 | .as_ref() |
| 1335 | .map_or(0, |session| session.file_frames(path)), |
| 1336 | // A path with no source entry has no colour of its own, and the |
| 1337 | // shadow wears the lane's own instead of borrowing another file's. |
| 1338 | tint: file_tint(self.sources(), path).unwrap_or(BG_RAISED()), |
| 1339 | refused: lane_refuses(path, to, self.has_video(path)).is_some(), |
| 1340 | }; |
| 1341 | self.set_cue(cue, x, cx); |
| 1342 | self.set_ghost(vec![ghost], cx); |
| 1343 | } |
| 1344 | |
| 1345 | /// Sets the shadow, or takes it away, repainting only when it moved -- the |
| 1346 | /// listeners below run it on every pointer sample of a drag. Cleared by the |
| 1347 | /// root and set again by the lane under the pointer, in that order (gpui |
| 1348 | /// runs the capture phase parent-first), so a pointer over no lane at all |
| 1349 | /// leaves nothing drawn. |
| 1350 | pub(crate) fn set_ghost(&mut self, ghost: Vec<Ghost>, cx: &mut Context<Self>) { |
| 1351 | if ghost != self.ghost { |
| 1352 | self.ghost = ghost; |
| 1353 | cx.notify(); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | /// The swatch a clip from source `n` wears: [`source_tint`] over the first |
| 1358 | /// source entry naming that *file*, since two audio streams of one file are |
| 1359 | /// two sources and one colour. Every box on a lane and every ghost a drag |
| 1360 | /// draws asks this, so the shadow is recognisably the thing in the hand. |
| 1361 | pub(crate) fn clip_tint(&self, source: usize) -> u32 { |
| 1362 | self.sources() |
| 1363 | .get(source) |
| 1364 | .and_then(|entry| file_tint(self.sources(), &entry.path)) |
| 1365 | .unwrap_or_else(|| source_tint(source)) |
| 1366 | } |
| 1367 | |
| 1368 | pub(crate) fn sources(&self) -> &[Source] { |
| 1369 | self.session |
| 1370 | .as_ref() |
| 1371 | .map_or(&[][..], PlaybackSession::sources) |
| 1372 | } |
| 1373 | |
| 1374 | /// Sets the line, or takes it away, and repaints only when it moved: a |
| 1375 | /// pointer dragged off the bed (up to the library, say) is not promising a |
| 1376 | /// landing any more. |
| 1377 | pub(crate) fn set_cue(&mut self, cue: Option<u32>, x: Pixels, cx: &mut Context<Self>) { |
| 1378 | let bed = self.ruler.get(); |
| 1379 | let cue = cue.filter(|_| x >= bed.left() && x <= bed.right()); |
| 1380 | if cue != self.snap_cue { |
| 1381 | self.snap_cue = cue; |
| 1382 | cx.notify(); |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | /// Every edge this timeline offers a gesture: [`snap_marks`] over all of its |
| 1387 | /// lanes, so a clip meets a take one track over as readily as one beside it. |
| 1388 | pub(crate) fn snap_targets(&self, skip: Option<(Lane, usize)>) -> Vec<u32> { |
| 1389 | let Some(session) = &self.session else { |
| 1390 | return Vec::new(); |
| 1391 | }; |
| 1392 | let lanes = session.lanes(); |
| 1393 | let clips: Vec<&[Clip]> = lanes.iter().map(|&lane| session.lane_clips(lane)).collect(); |
| 1394 | // The dragged placement's group, whatever kind it is: the clips of the |
| 1395 | // group travel with a clip's drag, and a caption's drag -- which has no |
| 1396 | // clip of its own to skip by -- must not snap onto the very clips it is |
| 1397 | // carrying. |
| 1398 | let skip_link = skip.and_then(|(lane, idx)| { |
| 1399 | match lane.kind { |
| 1400 | LaneKind::Subtitle => session.sub_lane(lane).get(idx).map(|s| s.link), |
| 1401 | _ => session.lane_clips(lane).get(idx).map(|c| c.link), |
| 1402 | } |
| 1403 | .flatten() |
| 1404 | }); |
| 1405 | let skip = skip.and_then(|(lane, idx)| Some((lanes.iter().position(|&l| l == lane)?, idx))); |
| 1406 | // The comment markers' edges and the chapter starts: a drag lands on a note |
| 1407 | // or a chapter as readily as on a cut. |
| 1408 | let mut notes: Vec<u32> = session |
| 1409 | .markers() |
| 1410 | .iter() |
| 1411 | .flat_map(|m| [m.frame, m.frame.saturating_add(m.length)]) |
| 1412 | .collect(); |
| 1413 | notes.extend(session.chapters().iter().map(|c| c.frame)); |
| 1414 | snap_marks( |
| 1415 | &clips, |
| 1416 | skip, |
| 1417 | skip_link, |
| 1418 | frame_at(session.now(), self.fps), |
| 1419 | self.range, |
| 1420 | ¬es, |
| 1421 | ) |
| 1422 | } |
| 1423 | |
| 1424 | /// Every timeline frame that is a *source* sync point: each clip's own |
| 1425 | /// grid ([`Player::syncs`]), moved onto the frames the clip plays it at. |
| 1426 | /// Ascending, because the clips are and each grid is. |
| 1427 | /// |
| 1428 | /// This is the difference between an export that copies its picture and one |
| 1429 | /// that decodes and re-codes every frame of a feature film. A cut anywhere |
| 1430 | /// else leaves the copy path with a region that begins between two sync |
| 1431 | /// points -- pictures whose references are not in the file -- and the whole |
| 1432 | /// export falls back to the encoder ([`engine::export`] states the rule). |
| 1433 | /// |
| 1434 | /// Only clips at their own speed, and only video lanes: a re-timed clip is |
| 1435 | /// resampled pictures, which is not a copy at any cut, and a sound lane has |
| 1436 | /// no groups of pictures to begin with. |
| 1437 | pub(crate) fn sync_frames(&self) -> Vec<u32> { |
| 1438 | let Some(session) = &self.session else { |
| 1439 | return Vec::new(); |
| 1440 | }; |
| 1441 | let sources = session.sources(); |
| 1442 | let mut marks = Vec::new(); |
| 1443 | for lane in session.lanes() { |
| 1444 | if lane.kind != LaneKind::Video { |
| 1445 | continue; |
| 1446 | } |
| 1447 | for clip in session.lane_clips(lane) { |
| 1448 | let Some(keys) = sources |
| 1449 | .get(clip.source) |
| 1450 | .and_then(|entry| self.syncs.get(&entry.path)) |
| 1451 | .filter(|_| clip.speed.is_normal() && clip.speed_keys.is_empty()) |
| 1452 | else { |
| 1453 | continue; |
| 1454 | }; |
| 1455 | marks.extend( |
| 1456 | keys.iter() |
| 1457 | .filter(|&&key| key >= clip.in_frame && key < clip.out_frame) |
| 1458 | .map(|&key| clip.start + (key - clip.in_frame)), |
| 1459 | ); |
| 1460 | } |
| 1461 | } |
| 1462 | marks.sort_unstable(); |
| 1463 | marks |
| 1464 | } |
| 1465 | |
| 1466 | /// The frame a cut asked for at `raw` really lands on: the nearest source |
| 1467 | /// sync point within the snap's own tolerance, or `raw` itself where the |
| 1468 | /// magnet is off, where nothing is near enough, or where the source has no |
| 1469 | /// grid to offer (the walk has not answered yet, or the file is not one |
| 1470 | /// this project can copy at all). |
| 1471 | /// |
| 1472 | /// The same tolerance the clip-edge snap uses, so one switch and one |
| 1473 | /// distance govern every landing on this timeline. |
| 1474 | pub(crate) fn cut_frame(&self, raw: u32) -> u32 { |
| 1475 | if !self.snap { |
| 1476 | return raw; |
| 1477 | } |
| 1478 | let tol = self.snap_frames(); |
| 1479 | self.sync_frames() |
| 1480 | .into_iter() |
| 1481 | .filter(|mark| mark.abs_diff(raw) <= tol) |
| 1482 | .min_by_key(|mark| mark.abs_diff(raw)) |
| 1483 | .unwrap_or(raw) |
| 1484 | } |
| 1485 | |
| 1486 | /// Puts the playhead on the sync point before or after it -- the keyboard's |
| 1487 | /// half of placing a cut where the export can copy it, and the only way to |
| 1488 | /// reach one exactly on a timeline zoomed out to a whole film, where one |
| 1489 | /// pixel is seconds. |
| 1490 | pub(crate) fn jump_sync(&mut self, forward: bool, cx: &mut Context<Self>) { |
| 1491 | let Some(session) = &self.session else { |
| 1492 | return; |
| 1493 | }; |
| 1494 | let now = frame_at(session.now(), self.fps); |
| 1495 | let marks = self.sync_frames(); |
| 1496 | let mark = match forward { |
| 1497 | true => marks.iter().find(|&&mark| mark > now).copied(), |
| 1498 | false => marks.iter().rev().find(|&&mark| mark < now).copied(), |
| 1499 | }; |
| 1500 | match mark { |
| 1501 | Some(mark) => self.seek(f64::from(mark) / self.fps, cx), |
| 1502 | // Said rather than swallowed: the two most likely reasons are a walk |
| 1503 | // that has not answered yet and a source with no grid at all, and a |
| 1504 | // key that does nothing looks broken either way. |
| 1505 | None => self.notify_user(match marks.is_empty() { |
| 1506 | true => "No sync points — this source has no keyframe grid to jump by (or it is \ |
| 1507 | still being read)" |
| 1508 | .into(), |
| 1509 | false => "No sync point that way — the playhead is past the last one".into(), |
| 1510 | }), |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /// Whether the magnet is on for the gesture happening *now*: the switch |
| 1515 | /// ([`Player::snap`]) unless `alt` is held ([`Player::drag_alt`]), which |
| 1516 | /// turns it off for this drag or trim alone -- the temporary override |
| 1517 | /// Resolve, Premiere and Final Cut all put on the same key. The switch is |
| 1518 | /// not touched, so the stroke that follows the drag finds it where it was. |
| 1519 | /// Every landing on this timeline asks it, so the shadow, the line and the |
| 1520 | /// drop can never disagree about whether a magnet was on. |
| 1521 | pub(crate) fn snap_live(&self) -> bool { |
| 1522 | snap_live(self.snap, self.drag_alt) |
| 1523 | } |
| 1524 | |
| 1525 | /// [`SNAP_PX`] in timeline frames at the scale the bed is drawn at: the |
| 1526 | /// bed's own width drops out of it, since a pixel is now worth the same |
| 1527 | /// stretch of timeline wherever the view sits. The edge trims' magnet -- |
| 1528 | /// a drop asks [`Player::drop_snap_frames`]. |
| 1529 | pub(crate) fn snap_frames(&self) -> u32 { |
| 1530 | self.scale.snap_frames(self.fps) |
| 1531 | } |
| 1532 | |
| 1533 | /// [`Player::snap_frames`]'s twin for a drag's own release |
| 1534 | /// ([`DROP_SNAP_PX`]): every `place_frame`/`drop_frame`/`sub_drop_frame` |
| 1535 | /// asks this instead, so a dropped clip is aimed with the wider magnet |
| 1536 | /// while an edge trim keeps the finer one. |
| 1537 | pub(crate) fn drop_snap_frames(&self) -> u32 { |
| 1538 | self.scale.drop_snap_frames(self.fps) |
| 1539 | } |
| 1540 | |
| 1541 | /// Opens the clip menu on the box under the pointer, from the right button |
| 1542 | /// wherever it was pressed on that box -- its middle or one of its edge |
| 1543 | /// strips, which cover the middle's own listener. Selecting first is part of |
| 1544 | /// it: every item acts on the clip the menu names -- and a right-click on |
| 1545 | /// one of the clips a ctrl-click selection already holds keeps that |
| 1546 | /// selection, so the menu's Group is about the whole of it. A right-click |
| 1547 | /// anywhere else is the single mark, exactly as a left press is. |
| 1548 | pub(crate) fn open_menu( |
| 1549 | &mut self, |
| 1550 | lane: Lane, |
| 1551 | idx: usize, |
| 1552 | at: Point<Pixels>, |
| 1553 | cx: &mut Context<Self>, |
| 1554 | ) { |
| 1555 | if !self.menu_allowed() { |
| 1556 | return; |
| 1557 | } |
| 1558 | if self.selected.contains((lane, idx)) { |
| 1559 | // Kept, but this clip becomes the anchor: Equalizer, Speed and the other |
| 1560 | // one-clip items read the anchor, and inside a linked pair it could be the |
| 1561 | // other half -- the video one, which the equalizer then refused. |
| 1562 | self.selected.promote((lane, idx)); |
| 1563 | } else { |
| 1564 | self.select((lane, idx), cx); |
| 1565 | } |
| 1566 | self.context_menu = Some(ContextMenu { |
| 1567 | lane, |
| 1568 | on: MenuOn::Clip(idx), |
| 1569 | at, |
| 1570 | details: false, |
| 1571 | sub: Sub::Closed, |
| 1572 | }); |
| 1573 | cx.notify(); |
| 1574 | } |
| 1575 | |
| 1576 | /// Opens the same menu on the gap under the pointer instead of a clip -- |
| 1577 | /// the empty-bench-space door to [`Player::close_gap`]. Nothing is |
| 1578 | /// selected: a gap owns no clip to select, unlike [`Player::open_menu`]. |
| 1579 | pub(crate) fn open_gap_menu( |
| 1580 | &mut self, |
| 1581 | lane: Lane, |
| 1582 | start: u32, |
| 1583 | frames: u32, |
| 1584 | at: Point<Pixels>, |
| 1585 | cx: &mut Context<Self>, |
| 1586 | ) { |
| 1587 | if !self.menu_allowed() { |
| 1588 | return; |
| 1589 | } |
| 1590 | self.context_menu = Some(ContextMenu { |
| 1591 | lane, |
| 1592 | on: MenuOn::Gap(start, frames), |
| 1593 | at, |
| 1594 | details: false, |
| 1595 | sub: Sub::Closed, |
| 1596 | }); |
| 1597 | cx.notify(); |
| 1598 | } |
| 1599 | |
| 1600 | /// Opens the bench's own menu: what a right-click on the ruler, or on the |
| 1601 | /// empty stretch under the last lane, names -- the timeline rather than |
| 1602 | /// anything on it ([`BENCH_ITEMS`]). No lane and no selection: the lane |
| 1603 | /// field is `V1` because the struct has one, and no row reads it. |
| 1604 | /// |
| 1605 | /// Refused with no timeline open, like every menu here: a card that paints |
| 1606 | /// nothing while `overlaid()` swallows the keys is an invisible modal |
| 1607 | /// (`tests/cards.rs`'s FAULT 2 rule). |
| 1608 | /// The ruler's menu, opened on the frame under the pointer ([`Self::menu_frame`]). |
| 1609 | pub(crate) fn open_bench_menu(&mut self, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 1610 | if !self.menu_allowed() || self.session.is_none() { |
| 1611 | return; |
| 1612 | } |
| 1613 | self.menu_frame = Some(self.frame_under(at.x)); |
| 1614 | self.context_menu = Some(ContextMenu { |
| 1615 | lane: Lane::V1, |
| 1616 | on: MenuOn::Bench, |
| 1617 | at, |
| 1618 | details: false, |
| 1619 | sub: Sub::Closed, |
| 1620 | }); |
| 1621 | cx.notify(); |
| 1622 | } |
| 1623 | |
| 1624 | /// Opens the viewer's menu ([`VIEWER_ITEMS`]) at the pointer. Refused with |
| 1625 | /// no timeline open, like every menu here. |
| 1626 | /// An empty stretch of `lane`'s bed with no gap to close: the paste and track adds |
| 1627 | /// ([`MenuOn::Empty`]), opened on the frame under the pointer. |
| 1628 | pub(crate) fn open_empty_menu(&mut self, lane: Lane, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 1629 | if !self.menu_allowed() || self.session.is_none() { |
| 1630 | return; |
| 1631 | } |
| 1632 | self.menu_frame = Some(self.frame_under(at.x)); |
| 1633 | self.context_menu = Some(ContextMenu { |
| 1634 | lane, |
| 1635 | on: MenuOn::Empty, |
| 1636 | at, |
| 1637 | details: false, |
| 1638 | sub: Sub::Closed, |
| 1639 | }); |
| 1640 | cx.notify(); |
| 1641 | } |
| 1642 | |
| 1643 | /// "Move playhead here": seeks to the frame the ruler or empty-track menu was opened on. |
| 1644 | pub(crate) fn playhead_at_menu(&mut self, cx: &mut Context<Self>) { |
| 1645 | if let Some(frame) = self.menu_frame { |
| 1646 | self.seek(f64::from(frame) / self.fps, cx); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | pub(crate) fn open_viewer_menu(&mut self, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 1651 | if !self.menu_allowed() || self.session.is_none() { |
| 1652 | return; |
| 1653 | } |
| 1654 | self.context_menu = Some(ContextMenu { |
| 1655 | lane: Lane::V1, |
| 1656 | on: MenuOn::Viewer, |
| 1657 | at, |
| 1658 | details: false, |
| 1659 | sub: Sub::Closed, |
| 1660 | }); |
| 1661 | cx.notify(); |
| 1662 | } |
| 1663 | |
| 1664 | /// Opens a sequence tab's menu ([`SEQ_TAB_ITEMS`]) at the pointer. The tab is opened first, |
| 1665 | /// so the verbs act on the sequence that was right-clicked. |
| 1666 | pub(crate) fn open_seq_tab_menu(&mut self, id: u32, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 1667 | if !self.menu_allowed() || self.session.is_none() { |
| 1668 | return; |
| 1669 | } |
| 1670 | self.switch_sequence_tab(id, cx); |
| 1671 | // Hung from the tab's foot like the menu bar's popups, so the menu never covers the |
| 1672 | // tab strip it was opened from. |
| 1673 | let at = crate::ui::anchor::control_at(at).map_or(at, |tab| crate::ui::anchor::hang(tab).0); |
| 1674 | self.context_menu = Some(ContextMenu { |
| 1675 | lane: Lane::V1, |
| 1676 | on: MenuOn::SeqTab, |
| 1677 | at, |
| 1678 | details: false, |
| 1679 | sub: Sub::Closed, |
| 1680 | }); |
| 1681 | cx.notify(); |
| 1682 | } |
| 1683 | |
| 1684 | /// Opens a lane head's menu on the track that was right-clicked: the tracks |
| 1685 | /// a project can gain, and the one remove that applies to this lane's kind |
| 1686 | /// ([`oracle::lane_items`]). Nothing is selected -- a head owns no clip -- |
| 1687 | /// which is [`Player::open_gap_menu`]'s rule on the other empty target. |
| 1688 | pub(crate) fn open_head_menu(&mut self, lane: Lane, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 1689 | if !self.menu_allowed() || self.session.is_none() { |
| 1690 | return; |
| 1691 | } |
| 1692 | self.context_menu = Some(ContextMenu { |
| 1693 | lane, |
| 1694 | on: MenuOn::Head, |
| 1695 | at, |
| 1696 | details: false, |
| 1697 | sub: Sub::Closed, |
| 1698 | }); |
| 1699 | cx.notify(); |
| 1700 | } |
| 1701 | |
| 1702 | /// Closes the gap `(start, frames)` -- Premiere's "Close Gap", scoped to |
| 1703 | /// `lane` and, when the gap borders a take, to the take's other lane too |
| 1704 | /// ([`engine::PlaybackSession::gap_take_scope`]): a lane not sharing the |
| 1705 | /// take never moves, so a neighbour that meant to keep its own silence is |
| 1706 | /// untouched, but the two halves of a linked A/V take close together -- |
| 1707 | /// the alternative is a ripple that pulls a take's picture and sound out |
| 1708 | /// of step with each other, which no lane's own gap is worth. A take |
| 1709 | /// whose gap does not match on its other lane is refused in words, not |
| 1710 | /// silence ([`engine::Project::gap_take_scope`]). |
| 1711 | pub(crate) fn close_gap( |
| 1712 | &mut self, |
| 1713 | lane: Lane, |
| 1714 | start: u32, |
| 1715 | frames: u32, |
| 1716 | cx: &mut Context<Self>, |
| 1717 | ) { |
| 1718 | let Some(session) = self.session.as_mut() else { |
| 1719 | return; |
| 1720 | }; |
| 1721 | match session.close_gap_at(lane, start, frames) { |
| 1722 | Ok(scope) => { |
| 1723 | self.mark_dirty(); |
| 1724 | // The ripple moves every index after the gap on the scoped |
| 1725 | // lanes, the same reason a delete drops the selection. |
| 1726 | self.selected.clear(); |
| 1727 | self.reset_after_reseek(); |
| 1728 | let where_ = if scope.len() > 1 { |
| 1729 | "its take".to_string() |
| 1730 | } else { |
| 1731 | lane.label().to_string() |
| 1732 | }; |
| 1733 | self.notify_user( |
| 1734 | format!( |
| 1735 | "Gap closed on {where_} · undo {}", |
| 1736 | self.keymap.display(ActionId::Undo) |
| 1737 | ) |
| 1738 | .into(), |
| 1739 | ); |
| 1740 | } |
| 1741 | Err(e) => self.notify_user(e.to_string().into()), |
| 1742 | } |
| 1743 | cx.notify(); |
| 1744 | } |
| 1745 | |
| 1746 | /// The gap that is picked and still there, if one is. A gap owns no clip |
| 1747 | /// index, so nothing else keeps it honest: an edit or an undo can move or |
| 1748 | /// remove the hole under it, and a clip mark means the pointer is on a clip |
| 1749 | /// instead. Both answer `None` here, which is what keeps Delete, the |
| 1750 | /// ledger's readout and the bed's outline from acting on a stale hole. |
| 1751 | pub(crate) fn gap_pick(&self) -> Option<GapPick> { |
| 1752 | if self.selected.anchor().is_some() { |
| 1753 | return None; |
| 1754 | } |
| 1755 | live_gap(self.session.as_ref()?, self.gap?) |
| 1756 | } |
| 1757 | |
| 1758 | /// Picks the gap `(start, frames)` on `lane`: the clip marks go, since the |
| 1759 | /// two are one selection and not two (a gap and a clip are never both picked). |
| 1760 | pub(crate) fn select_gap( |
| 1761 | &mut self, |
| 1762 | lane: Lane, |
| 1763 | start: u32, |
| 1764 | frames: u32, |
| 1765 | cx: &mut Context<Self>, |
| 1766 | ) { |
| 1767 | self.selected.clear(); |
| 1768 | self.drop_title_selection(cx); |
| 1769 | self.drop_sel_cue(); |
| 1770 | self.gap = Some(GapPick { |
| 1771 | lane, |
| 1772 | start, |
| 1773 | frames, |
| 1774 | }); |
| 1775 | cx.notify(); |
| 1776 | } |
| 1777 | |
| 1778 | /// Drops the gap pick, if any. A click on a clip or on bare bench does this. |
| 1779 | pub(crate) fn clear_gap(&mut self, cx: &mut Context<Self>) { |
| 1780 | if self.gap.take().is_some() { |
| 1781 | cx.notify(); |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | /// Delete with a gap picked: closes it through [`Player::close_gap`], so |
| 1786 | /// the ripple and its one undo step are the ones a right-click gives. The |
| 1787 | /// pick is dropped only when the hole is gone, so a refused close (a take |
| 1788 | /// that does not match on its other lane) keeps the gap under the mark. |
| 1789 | pub(crate) fn close_selected_gap(&mut self, cx: &mut Context<Self>) { |
| 1790 | let Some(pick) = self.gap_pick() else { |
| 1791 | self.gap = None; |
| 1792 | return; |
| 1793 | }; |
| 1794 | if self.exporting().is_some() { |
| 1795 | return; |
| 1796 | } |
| 1797 | self.close_gap(pick.lane, pick.start, pick.frames, cx); |
| 1798 | if self.gap_pick().is_none() { |
| 1799 | self.gap = None; |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | /// Closes every bounded gap on `lane`, as one gesture. The scope stays the |
| 1804 | /// clicked track's scope -- not the whole timeline -- and each linked take is |
| 1805 | /// widened only when its other half has the same empty stretch. Unsafe gaps |
| 1806 | /// are left open and counted in the notice, so the sweep never fails as a |
| 1807 | /// silent no-op. |
| 1808 | pub(crate) fn close_all_gaps_on_lane(&mut self, lane: Lane, cx: &mut Context<Self>) { |
| 1809 | let Some(session) = self.session.as_mut() else { |
| 1810 | return; |
| 1811 | }; |
| 1812 | let report = match session.close_all_gaps_on_lane(lane) { |
| 1813 | Ok(report) => report, |
| 1814 | Err(e) => { |
| 1815 | self.notify_user(e.to_string().into()); |
| 1816 | cx.notify(); |
| 1817 | return; |
| 1818 | } |
| 1819 | }; |
| 1820 | if report.closed > 0 { |
| 1821 | self.mark_dirty(); |
| 1822 | self.selected.clear(); |
| 1823 | self.reset_after_reseek(); |
| 1824 | } |
| 1825 | let skipped = report.skipped.len(); |
| 1826 | let notice = match (report.closed, skipped) { |
| 1827 | (0, 0) => format!("No gaps to close on {}", lane.label()), |
| 1828 | (0, 1) => format!( |
| 1829 | "No gaps closed on {} — 1 linked gap skipped at {}", |
| 1830 | lane.label(), |
| 1831 | report.skipped[0].start |
| 1832 | ), |
| 1833 | (0, n) => format!( |
| 1834 | "No gaps closed on {} — {n} linked gaps skipped", |
| 1835 | lane.label() |
| 1836 | ), |
| 1837 | (n, 0) => format!( |
| 1838 | "{n} gaps closed on {} — {} takes them back", |
| 1839 | lane.label(), |
| 1840 | self.keymap.display(ActionId::Undo) |
| 1841 | ), |
| 1842 | (n, 1) => format!( |
| 1843 | "{n} gaps closed on {}; 1 skipped at {} — match linked gaps or detach", |
| 1844 | lane.label(), |
| 1845 | report.skipped[0].start |
| 1846 | ), |
| 1847 | (n, skipped) => format!( |
| 1848 | "{n} gaps closed on {}; {skipped} skipped — match linked gaps or detach", |
| 1849 | lane.label() |
| 1850 | ), |
| 1851 | }; |
| 1852 | self.notify_user(notice.into()); |
| 1853 | cx.notify(); |
| 1854 | } |
| 1855 | |
| 1856 | /// A press on an edge: the start of the drag that changes how much of a |
| 1857 | /// source it plays. It picks the placement as a press anywhere else on the |
| 1858 | /// box does -- the edge strip covers the box's own listener (`occlude`), so |
| 1859 | /// this is the only one that fires there -- and ctrl makes that pick the |
| 1860 | /// toggle it is everywhere else on the bed. |
| 1861 | pub(crate) fn start_trim( |
| 1862 | &mut self, |
| 1863 | lane: Lane, |
| 1864 | idx: usize, |
| 1865 | edge: Edge, |
| 1866 | ctrl: bool, |
| 1867 | cx: &mut Context<Self>, |
| 1868 | ) { |
| 1869 | if self.covers_room() || self.exporting().is_some() || self.lane_locked_refusal(lane) { |
| 1870 | return; |
| 1871 | } |
| 1872 | // A caption's edge, on a lane that holds no `Clip` at all: the same |
| 1873 | // gesture, the same `Trim`, and the branch is the lane's kind wherever |
| 1874 | // the drag is asked about ([`Player::trim_to`], |
| 1875 | // [`Player::commit_trim`]). A caption *is* markable -- the mark is its |
| 1876 | // lane and its index like a clip's ([`Player::pick`]) -- and a caption |
| 1877 | // in a group carries its link, so its trim drags the group with it. |
| 1878 | if lane.kind == LaneKind::Subtitle { |
| 1879 | let Some(sub) = self |
| 1880 | .session |
| 1881 | .as_ref() |
| 1882 | .and_then(|session| session.sub_lane(lane).get(idx).copied()) |
| 1883 | else { |
| 1884 | return; |
| 1885 | }; |
| 1886 | self.pick((lane, idx), ctrl, cx); |
| 1887 | self.trim = Some(Trim { |
| 1888 | lane, |
| 1889 | idx, |
| 1890 | edge, |
| 1891 | // Where the edge already is: a press that never moves is not an |
| 1892 | // edit, and the engine refuses exactly that. |
| 1893 | from: match edge { |
| 1894 | Edge::Start => sub.start, |
| 1895 | Edge::End => sub.end(), |
| 1896 | }, |
| 1897 | to: match edge { |
| 1898 | Edge::Start => sub.start, |
| 1899 | Edge::End => sub.end(), |
| 1900 | }, |
| 1901 | link: sub.link, |
| 1902 | }); |
| 1903 | cx.notify(); |
| 1904 | return; |
| 1905 | } |
| 1906 | let Some(clip) = self |
| 1907 | .session |
| 1908 | .as_ref() |
| 1909 | .and_then(|session| session.lane_clips(lane).get(idx).cloned()) |
| 1910 | else { |
| 1911 | return; |
| 1912 | }; |
| 1913 | self.pick((lane, idx), ctrl, cx); |
| 1914 | self.trim = Some(Trim { |
| 1915 | lane, |
| 1916 | idx, |
| 1917 | edge, |
| 1918 | // Where the edge already is: a press that never moves is not an |
| 1919 | // edit, and `Project::trim` refuses exactly that. |
| 1920 | from: match edge { |
| 1921 | Edge::Start => clip.start, |
| 1922 | Edge::End => clip.end(), |
| 1923 | }, |
| 1924 | to: match edge { |
| 1925 | Edge::Start => clip.start, |
| 1926 | Edge::End => clip.end(), |
| 1927 | }, |
| 1928 | link: clip.link, |
| 1929 | }); |
| 1930 | cx.notify(); |
| 1931 | } |
| 1932 | |
| 1933 | /// Where the pointer has pulled the edge to, clamped to the room the engine |
| 1934 | /// says that edge has. Along the same bed the ruler is measured on and |
| 1935 | /// against the same duration the boxes are drawn to, so the edge tracks the |
| 1936 | /// pointer exactly. |
| 1937 | pub(crate) fn trim_to(&mut self, x: Pixels, cx: &mut Context<Self>) { |
| 1938 | let Some(trim) = self.trim else { |
| 1939 | return; |
| 1940 | }; |
| 1941 | // The edge is pulled onto the same marks a whole clip is, by itself: |
| 1942 | // there is no other end travelling with it, so it snaps at length zero. |
| 1943 | let marks = self.snap_targets(Some((trim.lane, trim.idx))); |
| 1944 | // The same 10 px of aim a drop is given ([`DROP_SNAP_PX`]): one |
| 1945 | // threshold for every gesture on this bed, so an edge and a whole clip |
| 1946 | // grip a cut at the same distance from it. |
| 1947 | let (at, cue) = snap_cue( |
| 1948 | self.snap_live(), |
| 1949 | self.frame_under(x), |
| 1950 | 0, |
| 1951 | self.drop_snap_frames(), |
| 1952 | &marks, |
| 1953 | ); |
| 1954 | let Some((lo, hi)) = self.session.as_ref().and_then(|session| { |
| 1955 | match trim.lane.kind == LaneKind::Subtitle { |
| 1956 | // The walls a caption's edge has -- its neighbour, its own |
| 1957 | // track's first microsecond and its last cue -- asked at this |
| 1958 | // timeline's rate, which is why the session is the door and not |
| 1959 | // the project. |
| 1960 | true => session.trim_sub_room(trim.lane, trim.idx, trim.edge), |
| 1961 | false => session.trim_room(trim.lane, trim.idx, trim.edge), |
| 1962 | } |
| 1963 | }) else { |
| 1964 | return; |
| 1965 | }; |
| 1966 | let to = at.clamp(lo, hi); |
| 1967 | // The line only stands where the edge actually stopped: a mark the |
| 1968 | // engine's own room clamped away was never reached. |
| 1969 | self.set_cue(cue.filter(|_| to == at), x, cx); |
| 1970 | // A clip's edge that the pointer pulled past the source's own end |
| 1971 | // flashes its handle warn -- the one stop a hand cannot see on the |
| 1972 | // picture. Captions have no media wall ([`PlaybackSession::trim_sub_room`] |
| 1973 | // is all they are asked), so they never flash. |
| 1974 | let walls = match trim.lane.kind == LaneKind::Subtitle { |
| 1975 | true => None, |
| 1976 | false => self |
| 1977 | .session |
| 1978 | .as_ref() |
| 1979 | .and_then(|session| session.trim_walls(trim.lane, trim.idx, trim.edge)), |
| 1980 | }; |
| 1981 | match walls.and_then(|walls| media_wall_stop(at, to, walls)) { |
| 1982 | // Armed once per stop: a pointer that stays past the same wall is |
| 1983 | // one flash, not one every sample, and the timer below is the only |
| 1984 | // repaint the flash needs. |
| 1985 | Some(wall) => { |
| 1986 | let pinned = self.trim_flash.is_some_and(|flash| { |
| 1987 | flash.lane == trim.lane |
| 1988 | && flash.idx == trim.idx |
| 1989 | && flash.edge == trim.edge |
| 1990 | && flash.wall == wall |
| 1991 | }); |
| 1992 | if !pinned { |
| 1993 | self.trim_flash = Some(TrimFlash { |
| 1994 | lane: trim.lane, |
| 1995 | idx: trim.idx, |
| 1996 | edge: trim.edge, |
| 1997 | wall, |
| 1998 | since: Instant::now(), |
| 1999 | }); |
| 2000 | cx.spawn(async move |this, cx| { |
| 2001 | cx.background_executor().timer(TRIM_FLASH).await; |
| 2002 | let _ = this.update(cx, |_, cx| cx.notify()); |
| 2003 | }) |
| 2004 | .detach(); |
| 2005 | } |
| 2006 | } |
| 2007 | // Back inside the room, or held by something other than the media: |
| 2008 | // the next stop at a wall is a new one. |
| 2009 | None => self.trim_flash = None, |
| 2010 | } |
| 2011 | self.trim = Some(Trim { to, ..trim }); |
| 2012 | cx.notify(); |
| 2013 | } |
| 2014 | |
| 2015 | /// The timeline frame a pointer at window x is on: along the same bed the |
| 2016 | /// ruler is measured on, through the same [`Scale`] every box is drawn |
| 2017 | /// through, so a zoomed-in panel answers with the frame under the pointer |
| 2018 | /// and not with the one that would have been there unzoomed. The one |
| 2019 | /// question a trim, a grab and a drop all ask. |
| 2020 | pub(crate) fn frame_under(&self, x: Pixels) -> u32 { |
| 2021 | frame_at(self.scale.time_at(px_along(x, self.ruler.get())), self.fps) |
| 2022 | } |
| 2023 | |
| 2024 | /// The release: the whole drag reaches the engine as one edit, so it is one |
| 2025 | /// undo step. The selection survives it -- a trim inserts and removes |
| 2026 | /// nothing, so every index a lane had still names the clip it named. |
| 2027 | pub(crate) fn commit_trim(&mut self, cx: &mut Context<Self>) { |
| 2028 | self.named_step("Trim", |this| this.commit_trim_now(cx)); |
| 2029 | } |
| 2030 | |
| 2031 | fn commit_trim_now(&mut self, cx: &mut Context<Self>) { |
| 2032 | let Some(trim) = self.trim.take() else { |
| 2033 | return; |
| 2034 | }; |
| 2035 | // The strip the drag started on may be gone or moved: its hover is over with the drag. |
| 2036 | self.hover_hint = None; |
| 2037 | // The arm rides each door's own answer or the caption's own edges -- |
| 2038 | // never the call: a trim released where the edge already stood is |
| 2039 | // `Ok`/`true` with no undo step, and it must not leave the project |
| 2040 | // looking edited to autosave, the `unsaved` ghost or the close |
| 2041 | // question. |
| 2042 | // A caption's edge reaches the engine through its own door, at this |
| 2043 | // timeline's rate. An `Ok` is *not* "something changed" -- an edge that |
| 2044 | // stopped at a wall it already stood against is `Ok` with no undo step |
| 2045 | // ([`Project::trim_sub`]) -- so only the refusal is ever said out loud. |
| 2046 | // A lone caption's trim -- and one grouped only with other captions -- |
| 2047 | // moves nothing that plays and reseeks nothing; a caption grouped with |
| 2048 | // clips trimmed them with it and owes the flag reset a clip's trim |
| 2049 | // pays. The grouped question is asked before the edit moves indices, |
| 2050 | // in the engine's own words, so the two answers can never disagree. |
| 2051 | if trim.lane.kind == LaneKind::Subtitle { |
| 2052 | let grouped = self |
| 2053 | .session |
| 2054 | .as_ref() |
| 2055 | .is_some_and(|session| session.caption_grouped_with_clips(trim.lane, trim.idx)); |
| 2056 | // The caption's own window, before and after: the only reading that |
| 2057 | // can tell a trim from an edge released against the wall it already |
| 2058 | // stood on. |
| 2059 | let edges = |player: &Self| { |
| 2060 | player.session.as_ref().and_then(|session| { |
| 2061 | session |
| 2062 | .sub_lane(trim.lane) |
| 2063 | .get(trim.idx) |
| 2064 | .map(|sub| (sub.start, sub.frames, sub.in_us, sub.out_us)) |
| 2065 | }) |
| 2066 | }; |
| 2067 | let before = edges(self); |
| 2068 | let trimmed = self |
| 2069 | .session |
| 2070 | .as_mut() |
| 2071 | .map(|session| session.trim_sub(trim.lane, trim.idx, trim.edge, trim.to)); |
| 2072 | match trimmed { |
| 2073 | Some(Err(e)) => self.notify_user(format!("Not trimmed — {e}").into()), |
| 2074 | Some(Ok(())) => { |
| 2075 | if edges(self) != before { |
| 2076 | self.mark_dirty(); |
| 2077 | } |
| 2078 | if grouped { |
| 2079 | self.reset_after_reseek(); |
| 2080 | } |
| 2081 | } |
| 2082 | None => {} |
| 2083 | } |
| 2084 | cx.notify(); |
| 2085 | return; |
| 2086 | } |
| 2087 | let trimmed = self |
| 2088 | .session |
| 2089 | .as_mut() |
| 2090 | .is_some_and(|session| session.trim_clip(trim.lane, trim.idx, trim.edge, trim.to)); |
| 2091 | if trimmed { |
| 2092 | // The arm rides the door's own answer: `trim_clip` is `false` for an |
| 2093 | // edge already where it was asked to go, which is no undo step and |
| 2094 | // no edit. |
| 2095 | self.mark_dirty(); |
| 2096 | self.reset_after_reseek(); |
| 2097 | } |
| 2098 | cx.notify(); |
| 2099 | } |
| 2100 | |
| 2101 | /// A press on a fade handle: the start of the drag that lengthens or |
| 2102 | /// shortens the ramp at that end of an audio clip. Ctrl-free, unlike a |
| 2103 | /// trim's press -- a fade handle sits inside the trim strip's own column |
| 2104 | /// ([`FADE_HANDLE_W`]) and a ctrl-click there would be read as picking the |
| 2105 | /// clip up rather than as the toggle it means on a trim. |
| 2106 | pub(crate) fn start_fade_drag( |
| 2107 | &mut self, |
| 2108 | lane: Lane, |
| 2109 | idx: usize, |
| 2110 | is_in: bool, |
| 2111 | x: Pixels, |
| 2112 | cx: &mut Context<Self>, |
| 2113 | ) { |
| 2114 | if self.covers_room() || self.exporting().is_some() || self.lane_locked_refusal(lane) { |
| 2115 | return; |
| 2116 | } |
| 2117 | let Some(clip) = self |
| 2118 | .session |
| 2119 | .as_ref() |
| 2120 | .and_then(|session| session.lane_clips(lane).get(idx).cloned()) |
| 2121 | else { |
| 2122 | return; |
| 2123 | }; |
| 2124 | self.pick((lane, idx), false, cx); |
| 2125 | let start = match is_in { |
| 2126 | true => clip.fade_in, |
| 2127 | false => clip.fade_out, |
| 2128 | }; |
| 2129 | self.fade_drag = Some(FadeDrag { |
| 2130 | lane, |
| 2131 | idx, |
| 2132 | is_in, |
| 2133 | press_x: x, |
| 2134 | start, |
| 2135 | to: start, |
| 2136 | cap: clip.frames(), |
| 2137 | }); |
| 2138 | cx.notify(); |
| 2139 | } |
| 2140 | |
| 2141 | /// Where the pointer has pulled a fade handle to, in the fade's own |
| 2142 | /// frames -- pulling away from the clip's edge lengthens the ramp for |
| 2143 | /// both handles alike, which is why the tail's own delta is negated: its |
| 2144 | /// handle sits at the *right* of the box, so a hand dragging left (toward |
| 2145 | /// the body) is the same "make it longer" motion the head's handle reads |
| 2146 | /// dragging right. |
| 2147 | pub(crate) fn fade_drag_to(&mut self, x: Pixels, cx: &mut Context<Self>) { |
| 2148 | let Some(fade) = self.fade_drag else { |
| 2149 | return; |
| 2150 | }; |
| 2151 | let dx = f32::from(x) - f32::from(fade.press_x); |
| 2152 | let dx = match fade.is_in { |
| 2153 | true => dx, |
| 2154 | false => -dx, |
| 2155 | }; |
| 2156 | let delta = fade_delta_frames(dx, self.scale.pps, self.fps); |
| 2157 | let to = (i64::from(fade.start) + delta).clamp(0, i64::from(fade.cap)) as u32; |
| 2158 | self.fade_drag = Some(FadeDrag { to, ..fade }); |
| 2159 | cx.notify(); |
| 2160 | } |
| 2161 | |
| 2162 | /// The release: one edit, one undo step, exactly as [`Player::commit_trim`] |
| 2163 | /// pays for a trim. Autosave's own reason a fade drag has to call |
| 2164 | /// [`Player::mark_dirty`] same as every other edit that reaches the |
| 2165 | /// engine. |
| 2166 | pub(crate) fn commit_fade(&mut self, cx: &mut Context<Self>) { |
| 2167 | let Some(fade) = self.fade_drag.take() else { |
| 2168 | return; |
| 2169 | }; |
| 2170 | // The ramp the clip is carrying *now*, so the arm below can tell a drag |
| 2171 | // from a press-and-release that left the handle where it was: `set_fade_in` |
| 2172 | // / `set_fade_out` answer `true` for any index that exists and take an |
| 2173 | // undo step either way, so their own answer is not a change report. |
| 2174 | let was = match fade.is_in { |
| 2175 | true => self |
| 2176 | .session |
| 2177 | .as_ref() |
| 2178 | .map(|session| session.fade_in_of(fade.lane, fade.idx)), |
| 2179 | false => self |
| 2180 | .session |
| 2181 | .as_ref() |
| 2182 | .map(|session| session.fade_out_of(fade.lane, fade.idx)), |
| 2183 | }; |
| 2184 | let set = self |
| 2185 | .session |
| 2186 | .as_mut() |
| 2187 | .is_some_and(|session| match fade.is_in { |
| 2188 | true => session.set_fade_in(fade.lane, fade.idx, fade.to), |
| 2189 | false => session.set_fade_out(fade.lane, fade.idx, fade.to), |
| 2190 | }); |
| 2191 | if set { |
| 2192 | let now = match fade.is_in { |
| 2193 | true => self |
| 2194 | .session |
| 2195 | .as_ref() |
| 2196 | .map(|session| session.fade_in_of(fade.lane, fade.idx)), |
| 2197 | false => self |
| 2198 | .session |
| 2199 | .as_ref() |
| 2200 | .map(|session| session.fade_out_of(fade.lane, fade.idx)), |
| 2201 | }; |
| 2202 | if now != was { |
| 2203 | self.mark_dirty(); |
| 2204 | } |
| 2205 | cx.notify(); |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | /// The clip's fade-in as the drag is showing it: display only, same as |
| 2210 | /// [`Player::trimmed`] -- the engine hears about it once, at the release. |
| 2211 | pub(crate) fn shown_fade_in(&self, lane: Lane, idx: usize, clip: &Clip) -> u32 { |
| 2212 | match self.fade_drag { |
| 2213 | Some(f) if f.is_in && (f.lane, f.idx) == (lane, idx) => f.to, |
| 2214 | _ => clip.fade_in, |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | /// [`Player::shown_fade_in`]'s twin, for the tail. |
| 2219 | pub(crate) fn shown_fade_out(&self, lane: Lane, idx: usize, clip: &Clip) -> u32 { |
| 2220 | match self.fade_drag { |
| 2221 | Some(f) if !f.is_in && (f.lane, f.idx) == (lane, idx) => f.to, |
| 2222 | _ => clip.fade_out, |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | /// Crossfades across the join the selection names: two picks on one audio |
| 2227 | /// lane take the pair, one pick takes it and its right-hand neighbour. The |
| 2228 | /// engine owns adjacency ([`Project::crossfade`]); a refusal is worded |
| 2229 | /// here, in [`Player::regroup`]'s voice, because `false` is all it says. |
| 2230 | pub(crate) fn crossfade_selected(&mut self, cx: &mut Context<Self>) { |
| 2231 | if self.exporting().is_some() { |
| 2232 | return; |
| 2233 | } |
| 2234 | let Some((lane, idx)) = (match self.selected.picks() { |
| 2235 | [a, b] if a.0 == b.0 && a.1.abs_diff(b.1) == 1 => Some((a.0, a.1.min(b.1))), |
| 2236 | _ => self.selected.anchor(), |
| 2237 | }) else { |
| 2238 | self.notify_user("Nothing to crossfade — no audio clip selected".into()); |
| 2239 | cx.notify(); |
| 2240 | return; |
| 2241 | }; |
| 2242 | let frames = self.fps.round().max(1.) as u32; |
| 2243 | if self.session.is_none() { |
| 2244 | return; |
| 2245 | } |
| 2246 | // The arm rides the write, and rides the join's own edges as well as |
| 2247 | // the door's answer: `Project::crossfade` answers true for any two |
| 2248 | // adjacent clips, so a crossfade already at the room the two clips |
| 2249 | // offer writes the value the join already carried and still says yes. |
| 2250 | // This door carries its own arm rather than leaning on the action |
| 2251 | // table's list, which fires before any door is asked. |
| 2252 | let before = self.transition_edges(lane, idx); |
| 2253 | let took = match &mut self.session { |
| 2254 | Some(session) => session.crossfade(lane, idx, frames), |
| 2255 | None => false, |
| 2256 | }; |
| 2257 | if took && self.transition_edges(lane, idx) != before { |
| 2258 | self.mark_dirty(); |
| 2259 | } else if !took { |
| 2260 | self.notify_user("Nothing to crossfade — no neighbour end to end".into()); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | /// Dissolves the join the selection names, into or back out of it: two |
| 2265 | /// picks on one video lane take the pair, one pick takes it and its |
| 2266 | /// right-hand neighbour. If the leading clip already carries a dissolve |
| 2267 | /// this removes it (`0` frames) instead of widening it -- a toggle, same |
| 2268 | /// key either way. The engine owns adjacency ([`Project::set_transition_out`]); |
| 2269 | /// a refusal is worded here, in [`Player::crossfade_selected`]'s voice. |
| 2270 | pub(crate) fn dissolve_selected(&mut self, cx: &mut Context<Self>) { |
| 2271 | if self.exporting().is_some() { |
| 2272 | return; |
| 2273 | } |
| 2274 | let Some((lane, idx)) = (match self.selected.picks() { |
| 2275 | [a, b] if a.0 == b.0 && a.1.abs_diff(b.1) == 1 => Some((a.0, a.1.min(b.1))), |
| 2276 | _ => self.selected.anchor(), |
| 2277 | }) else { |
| 2278 | self.notify_user("Nothing to dissolve — no video clip selected".into()); |
| 2279 | cx.notify(); |
| 2280 | return; |
| 2281 | }; |
| 2282 | if self.session.is_none() { |
| 2283 | cx.notify(); |
| 2284 | return; |
| 2285 | } |
| 2286 | let removing = self.transition_out_of_selected(lane, idx); |
| 2287 | let frames = if removing { |
| 2288 | 0 |
| 2289 | } else { |
| 2290 | self.fps.round().max(1.) as u32 |
| 2291 | }; |
| 2292 | // The arm rides the write, and the join's own edges as well as the |
| 2293 | // door's answer -- `set_transition_out`'s own rule |
| 2294 | // ([`Player::crossfade_selected`]'s). This door carries its own arm |
| 2295 | // rather than leaning on the action table's list, which fires before |
| 2296 | // any door is asked: the clip-menu row |
| 2297 | // ([`crate::ui::bench_stance`]) reaches this door directly, never |
| 2298 | // through the table at all, and was armed by nothing. |
| 2299 | let before = self.transition_edges(lane, idx); |
| 2300 | let took = match &mut self.session { |
| 2301 | Some(session) => session.set_transition_out(lane, idx, frames), |
| 2302 | None => false, |
| 2303 | }; |
| 2304 | if took && self.transition_edges(lane, idx) != before { |
| 2305 | self.mark_dirty(); |
| 2306 | if removing { |
| 2307 | self.notify_user("Dissolve removed — the clips cut again".into()); |
| 2308 | } else { |
| 2309 | self.notify_user_action("Dissolve added".into(), ToastAction::Undo); |
| 2310 | } |
| 2311 | } else if !took { |
| 2312 | self.notify_user("Nothing to dissolve — no neighbour end to end".into()); |
| 2313 | } |
| 2314 | cx.notify(); |
| 2315 | } |
| 2316 | |
| 2317 | /// Sets how the join the selection names is drawn |
| 2318 | /// ([`engine::transitions::TransitionKind`]), making it a second long if the |
| 2319 | /// clip has none: the picker's and the palette's door. Two picks on one |
| 2320 | /// video lane take the pair, one pick takes it and its right-hand neighbour, |
| 2321 | /// as [`Player::dissolve_selected`] does. One undo step; a join that already |
| 2322 | /// wears the kind writes nothing. |
| 2323 | pub(crate) fn set_transition_kind( |
| 2324 | &mut self, |
| 2325 | kind: engine::transitions::TransitionKind, |
| 2326 | cx: &mut Context<Self>, |
| 2327 | ) { |
| 2328 | if self.exporting().is_some() { |
| 2329 | return; |
| 2330 | } |
| 2331 | let Some((lane, idx)) = (match self.selected.picks() { |
| 2332 | [a, b] if a.0 == b.0 && a.1.abs_diff(b.1) == 1 => Some((a.0, a.1.min(b.1))), |
| 2333 | _ => self.selected.anchor(), |
| 2334 | }) else { |
| 2335 | self.notify_user("Nothing to transition — no video clip selected".into()); |
| 2336 | cx.notify(); |
| 2337 | return; |
| 2338 | }; |
| 2339 | let Some(session) = &self.session else { |
| 2340 | cx.notify(); |
| 2341 | return; |
| 2342 | }; |
| 2343 | let current = session.transition_out_of(lane, idx); |
| 2344 | let frames = if current > 0 { |
| 2345 | current |
| 2346 | } else { |
| 2347 | self.fps.round().max(1.) as u32 |
| 2348 | }; |
| 2349 | let before = self.transition_edges(lane, idx); |
| 2350 | let took = match &mut self.session { |
| 2351 | Some(session) => session.set_transition(lane, idx, kind, frames), |
| 2352 | None => false, |
| 2353 | }; |
| 2354 | if took && self.transition_edges(lane, idx) != before { |
| 2355 | self.mark_dirty(); |
| 2356 | } else if !took { |
| 2357 | self.notify_user("Nothing to transition — no neighbour end to end".into()); |
| 2358 | } |
| 2359 | cx.notify(); |
| 2360 | } |
| 2361 | |
| 2362 | /// The dissolve the join at `idx` of `lane` already carries, read before |
| 2363 | /// the session is borrowed mutably: `0` for a lane, index or join that has |
| 2364 | /// none, and `false` if there is no session to ask. |
| 2365 | fn transition_out_of_selected(&self, lane: Lane, idx: usize) -> bool { |
| 2366 | self.session |
| 2367 | .as_ref() |
| 2368 | .is_some_and(|session| session.transition_out_of(lane, idx) > 0) |
| 2369 | } |
| 2370 | |
| 2371 | /// The transition's absolute length, through the setter the kind and |
| 2372 | /// length controls share (`set_transition_out`/`crossfade`, each already |
| 2373 | /// clamping to the shorter neighbour's frames), so the typed length |
| 2374 | /// ([`Player::commit_transition`]) and the picker's "None" land a number |
| 2375 | /// the same way, and undo and persistence never tell the doors apart. |
| 2376 | pub(crate) fn set_transition_frames( |
| 2377 | &mut self, |
| 2378 | lane: Lane, |
| 2379 | idx: usize, |
| 2380 | frames: u32, |
| 2381 | cx: &mut Context<Self>, |
| 2382 | ) { |
| 2383 | if self.exporting().is_some() { |
| 2384 | return; |
| 2385 | } |
| 2386 | if self.session.is_none() { |
| 2387 | return; |
| 2388 | } |
| 2389 | // The engine's bool answers "the two clips are adjacent" -- both |
| 2390 | // setters snapshot and clamp, and a `frames` already in force (a step |
| 2391 | // past the cap, a number retyped in the field) writes the value the |
| 2392 | // clip already carried and still says true. The arm therefore rides |
| 2393 | // the join's own edges read back, the same before/after comparison |
| 2394 | // `move_sub` and `commit_fade` make. |
| 2395 | let before = self.transition_edges(lane, idx); |
| 2396 | let ok = match &mut self.session { |
| 2397 | Some(session) => match lane.kind { |
| 2398 | LaneKind::Video => session.set_transition_out(lane, idx, frames), |
| 2399 | LaneKind::Audio => session.crossfade(lane, idx, frames), |
| 2400 | _ => false, |
| 2401 | }, |
| 2402 | None => false, |
| 2403 | }; |
| 2404 | if ok && self.transition_edges(lane, idx) != before { |
| 2405 | self.mark_dirty(); |
| 2406 | } |
| 2407 | cx.notify(); |
| 2408 | } |
| 2409 | |
| 2410 | /// What the join at `idx` of `lane` carries on both sides, for the doors |
| 2411 | /// that only the *pair* can answer for: a crossfade writes `idx`'s |
| 2412 | /// `fade_out` and its neighbour's `fade_in`, so a comparison of the |
| 2413 | /// anchor's own field alone would miss a neighbour-side no-op. `None` |
| 2414 | /// for a lane with no such pair, which is a value no write can equal. |
| 2415 | fn transition_edges(&self, lane: Lane, idx: usize) -> Option<(u32, u32)> { |
| 2416 | let session = self.session.as_ref()?; |
| 2417 | match lane.kind { |
| 2418 | LaneKind::Video => Some(( |
| 2419 | session.transition_out_of(lane, idx), |
| 2420 | session.transition_kind_of(lane, idx).ordinal() + 1, |
| 2421 | )), |
| 2422 | LaneKind::Audio => { |
| 2423 | let clips = session.lane_clips(lane); |
| 2424 | Some((clips.get(idx)?.fade_out, clips.get(idx + 1)?.fade_in)) |
| 2425 | } |
| 2426 | _ => None, |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | /// Opens the transition length's field on the frame count the block is |
| 2431 | /// carrying, in seconds -- the row's own click, `Player::edit_mbps`'s |
| 2432 | /// convention. The engine clamps a typed length to what the two clips |
| 2433 | /// offer ([`Player::set_transition_frames`]), so the field needs no cap. |
| 2434 | pub(crate) fn edit_transition(&mut self, frames: u32) { |
| 2435 | let fps = self.fps.max(1.); |
| 2436 | self.viz_hex_edit = None; |
| 2437 | self.transition_edit = Some(NumberEdit::begin(&format!("{:.2}", f64::from(frames) / fps))); |
| 2438 | } |
| 2439 | |
| 2440 | /// One key while the transition length is typed: digits, `.` and `-` |
| 2441 | /// are typed, Enter writes the length, Escape drops it, and backspace |
| 2442 | /// erases. Any other key is ignored: the field keeps the keyboard, so no |
| 2443 | /// shortcut runs from a stroke typed into it. `false` only when no length |
| 2444 | /// is being typed. |
| 2445 | pub(crate) fn transition_key(&mut self, key: &str, cx: &mut Context<Self>) -> bool { |
| 2446 | use crate::player::text_field::FieldKey; |
| 2447 | let Some(mut edit) = self.transition_edit.take() else { |
| 2448 | return false; |
| 2449 | }; |
| 2450 | let typed = key.parse::<u32>().is_ok() || matches!(key, "." | "," | "-"); |
| 2451 | // A stroke that is not a number or an editing key is ignored, not passed on: the field |
| 2452 | // holds the keyboard until it is left. |
| 2453 | if !typed && !matches!(key, "enter" | "escape" | "backspace") { |
| 2454 | self.transition_edit = Some(edit); |
| 2455 | return true; |
| 2456 | } |
| 2457 | match edit.key(key, false, false, typed.then_some(key)) { |
| 2458 | FieldKey::Edited => self.transition_edit = Some(edit), |
| 2459 | FieldKey::Cancel => {} |
| 2460 | FieldKey::Commit => { |
| 2461 | self.transition_edit = Some(edit); |
| 2462 | self.commit_transition(cx); |
| 2463 | } |
| 2464 | } |
| 2465 | cx.notify(); |
| 2466 | true |
| 2467 | } |
| 2468 | |
| 2469 | /// The typed length's enter: the seconds become frames at the project's |
| 2470 | /// rate and go through [`Player::set_transition_frames`], which clamps |
| 2471 | /// into the neighbour's room. A buffer that is not a number writes nothing. |
| 2472 | pub(crate) fn commit_transition(&mut self, cx: &mut Context<Self>) { |
| 2473 | let Some(edit) = self.transition_edit.take() else { |
| 2474 | return; |
| 2475 | }; |
| 2476 | let fps = self.fps.max(1.); |
| 2477 | if let (Some(secs), Some((lane, idx))) = (edit.commit(0., f64::from(u32::MAX) / fps), self.selected.anchor()) { |
| 2478 | self.set_transition_frames(lane, idx, (secs * fps).round() as u32, cx); |
| 2479 | } |
| 2480 | cx.notify(); |
| 2481 | } |
| 2482 | |
| 2483 | /// Opens the in-place editor on one cue block: a double-click on it |
| 2484 | /// (DESIGN §7). The buffer starts on the words the cue has now, with the |
| 2485 | /// caret at the end. Refused on a locked lane, with the lock's own toast, |
| 2486 | /// and not offered for a picture cue, which has no words to type over. One |
| 2487 | /// inline editor at a time, so the colour and transition fields give way. |
| 2488 | pub(crate) fn open_cue_edit( |
| 2489 | &mut self, |
| 2490 | lane: Lane, |
| 2491 | track: usize, |
| 2492 | cue: usize, |
| 2493 | cx: &mut Context<Self>, |
| 2494 | ) { |
| 2495 | if self.exporting().is_some() || self.lane_locked_refusal(lane) { |
| 2496 | return; |
| 2497 | } |
| 2498 | let words = self |
| 2499 | .session |
| 2500 | .as_ref() |
| 2501 | .and_then(|s| s.subtitles().get(track)) |
| 2502 | .and_then(|t| t.cues.get(cue)) |
| 2503 | .filter(|c| c.image.is_none()) |
| 2504 | .map(|c| c.text.clone()); |
| 2505 | let Some(words) = words else { |
| 2506 | return; |
| 2507 | }; |
| 2508 | self.viz_hex_edit = None; |
| 2509 | self.transition_edit = None; |
| 2510 | self.cue_edit = Some(CueEdit::new(lane, track, cue, &words)); |
| 2511 | cx.notify(); |
| 2512 | } |
| 2513 | |
| 2514 | /// The in-place editor's enter. Writes the words only when they differ from |
| 2515 | /// the cue's ([`CueEdit::commit`]), as one undo step through |
| 2516 | /// [`PlaybackSession::set_cue_text`]; an edit that changed nothing leaves the |
| 2517 | /// project as it was. The editor is closed either way, and a lane that was |
| 2518 | /// locked while the block was open writes nothing. |
| 2519 | pub(crate) fn commit_cue_edit(&mut self, cx: &mut Context<Self>) { |
| 2520 | let Some(edit) = self.cue_edit.take() else { |
| 2521 | return; |
| 2522 | }; |
| 2523 | if self.lane_locked_refusal(edit.lane) { |
| 2524 | cx.notify(); |
| 2525 | return; |
| 2526 | } |
| 2527 | let current = self |
| 2528 | .session |
| 2529 | .as_ref() |
| 2530 | .and_then(|s| s.subtitles().get(edit.track)) |
| 2531 | .and_then(|t| t.cues.get(edit.cue)) |
| 2532 | .map(|c| c.text.clone()); |
| 2533 | if let Some(words) = current.and_then(|current| edit.commit(¤t)) |
| 2534 | && let Some(session) = self.session.as_mut() |
| 2535 | && session.set_cue_text(edit.track, edit.cue, &words) |
| 2536 | { |
| 2537 | self.mark_dirty(); |
| 2538 | } |
| 2539 | cx.notify(); |
| 2540 | } |
| 2541 | |
| 2542 | /// A press anywhere outside the in-place editor's plate ends the edit the |
| 2543 | /// way Enter does, so a click away is never a silent discard of the words. |
| 2544 | /// The plate stops its own presses before they reach the root. |
| 2545 | pub(crate) fn cue_edit_press_outside( |
| 2546 | &mut self, |
| 2547 | _: &MouseDownEvent, |
| 2548 | _: &mut Window, |
| 2549 | cx: &mut Context<Self>, |
| 2550 | ) { |
| 2551 | if self.cue_edit.is_some() { |
| 2552 | self.commit_cue_edit(cx); |
| 2553 | } |
| 2554 | // A panel number typed and left is written the way Enter writes it. |
| 2555 | if let Some((which, edit)) = self.cue_num.take() { |
| 2556 | if let Some(secs) = edit.commit(0., 36_000.) { |
| 2557 | self.set_cue_num(which, (secs * 1e6).round() as i64, cx); |
| 2558 | } |
| 2559 | cx.notify(); |
| 2560 | } |
| 2561 | // The title's words are written the same way; the text field stops its own presses. |
| 2562 | self.commit_title_draft(cx); |
| 2563 | } |
| 2564 | |
| 2565 | /// The clip as the drag is showing it: an edge under the pointer moves its |
| 2566 | /// own box, and the boxes of everything linked to it, before anything is |
| 2567 | /// committed. Display only -- the project is not touched until the release. |
| 2568 | /// A member of the group follows by the same delta, clamped to its own |
| 2569 | /// room, which is the arithmetic the release commits -- so a box let go of |
| 2570 | /// is the box that lands. |
| 2571 | pub(crate) fn trimmed(&self, lane: Lane, idx: usize, clip: Clip) -> Clip { |
| 2572 | let Some(trim) = self.trim.filter(|t| { |
| 2573 | (t.lane, t.idx) == (lane, idx) || (t.link.is_some() && t.link == clip.link) |
| 2574 | }) else { |
| 2575 | return clip; |
| 2576 | }; |
| 2577 | let to = self.followed( |
| 2578 | trim, |
| 2579 | (lane, idx), |
| 2580 | match trim.edge { |
| 2581 | Edge::Start => clip.start, |
| 2582 | Edge::End => clip.end(), |
| 2583 | }, |
| 2584 | ); |
| 2585 | let still = self.session.as_ref().is_some_and(|session| { |
| 2586 | session |
| 2587 | .sources() |
| 2588 | .get(clip.source) |
| 2589 | .is_some_and(|s| engine::is_image(&s.path)) |
| 2590 | }); |
| 2591 | trimmed_clip(clip, trim.edge, to, still) |
| 2592 | } |
| 2593 | |
| 2594 | /// The caption as the drag is showing it, [`Player::trimmed`]'s twin: |
| 2595 | /// display only, and the engine hears about it once, at the release. A |
| 2596 | /// caption in the dragged group follows by the same delta, against its own |
| 2597 | /// walls. |
| 2598 | pub(crate) fn trimmed_sub(&self, lane: Lane, idx: usize, sub: SubClip) -> SubClip { |
| 2599 | let Some(trim) = self |
| 2600 | .trim |
| 2601 | .filter(|t| (t.lane, t.idx) == (lane, idx) || (t.link.is_some() && t.link == sub.link)) |
| 2602 | else { |
| 2603 | return sub; |
| 2604 | }; |
| 2605 | let to = self.followed( |
| 2606 | trim, |
| 2607 | (lane, idx), |
| 2608 | match trim.edge { |
| 2609 | Edge::Start => sub.start, |
| 2610 | Edge::End => sub.end(), |
| 2611 | }, |
| 2612 | ); |
| 2613 | trimmed_sub(sub, trim.edge, to) |
| 2614 | } |
| 2615 | |
| 2616 | /// The frame a trim is showing for the placement at `here`, whose edge |
| 2617 | /// stands at `at`: the drag's own `to` when `here` is the placement the |
| 2618 | /// press started on, and that `to`'s delta from where the press started, |
| 2619 | /// clamped to this placement's own room, when it is a member following. |
| 2620 | fn followed(&self, trim: Trim, here: (Lane, usize), at: u32) -> u32 { |
| 2621 | if (trim.lane, trim.idx) == here { |
| 2622 | return trim.to; |
| 2623 | } |
| 2624 | let delta = i64::from(trim.to) - i64::from(trim.from); |
| 2625 | let room = match here.0.kind { |
| 2626 | LaneKind::Subtitle => self |
| 2627 | .session |
| 2628 | .as_ref() |
| 2629 | .and_then(|session| session.trim_sub_room(here.0, here.1, trim.edge)), |
| 2630 | _ => self |
| 2631 | .session |
| 2632 | .as_ref() |
| 2633 | .and_then(|session| session.trim_room(here.0, here.1, trim.edge)), |
| 2634 | }; |
| 2635 | room.map_or(trim.to, |(lo, hi)| { |
| 2636 | (i64::from(at) + delta).clamp(i64::from(lo), i64::from(hi)) as u32 |
| 2637 | }) |
| 2638 | } |
| 2639 | |
| 2640 | /// How long the timeline is *drawn* as: its own length, and while a tail is |
| 2641 | /// being dragged the furthest that tail may reach. A bed that ends exactly |
| 2642 | /// at the last frame has nowhere to put a pointer that means "longer", so |
| 2643 | /// without this the last clip on the timeline could be pulled in and never |
| 2644 | /// let back out. |
| 2645 | /// |
| 2646 | /// Scroll room only, now that a second is an absolute number of pixels |
| 2647 | /// ([`Scale`]): the extra length loosens [`View::settled`]'s clamp, which is |
| 2648 | /// where the pixels past the last frame come from, and moves no box by a |
| 2649 | /// pixel. It is still the *only* headroom at the tail -- zoomed in against |
| 2650 | /// the end, that clamp pins the bed's right edge to the duration and an |
| 2651 | /// End-trim of the last clip would have nowhere to be dragged to. What it |
| 2652 | /// must not do is be read as a length anyone is told: the timecode reads |
| 2653 | /// `PlaybackSession::timeline_duration` for exactly that reason. |
| 2654 | pub(crate) fn drawn_duration(&self) -> f64 { |
| 2655 | let Some(session) = &self.session else { |
| 2656 | return 0.; |
| 2657 | }; |
| 2658 | let duration = session.timeline_duration(); |
| 2659 | match self.trim { |
| 2660 | Some(trim) if trim.edge == Edge::End => { |
| 2661 | let (_, hi) = session |
| 2662 | .trim_room(trim.lane, trim.idx, trim.edge) |
| 2663 | .unwrap_or((0, 0)); |
| 2664 | duration.max(f64::from(hi) / self.fps) |
| 2665 | } |
| 2666 | _ => duration, |
| 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | /// Where the playhead is, as the panel draws it: pinned to the out point |
| 2671 | /// once playback is done, and clamped to the drawn duration otherwise -- a |
| 2672 | /// tail being dragged draws past the timeline it is about to become. |
| 2673 | /// |
| 2674 | /// `self.transport()` answers for whichever session is active, which is |
| 2675 | /// the preview while one is showing ([`Player::active_session`]) -- so a |
| 2676 | /// preview reaching its own end must not be read as *this* (the |
| 2677 | /// timeline's) playhead running out and snapping to `duration`. Gated on |
| 2678 | /// there being no preview session, so the ruler keeps tracking |
| 2679 | /// `self.session` -- untouched by a preview's own clock -- for as long as |
| 2680 | /// one is up. |
| 2681 | pub(crate) fn playhead(&self, duration: f64) -> f64 { |
| 2682 | let now = self.session.as_ref().map_or(0., PlaybackSession::now); |
| 2683 | // A take runs the clock past the last frame, and the timecode goes on with it. |
| 2684 | if self.record_running() { |
| 2685 | return now; |
| 2686 | } |
| 2687 | let ended = self.preview_session.is_none() && self.transport() == Transport::Ended; |
| 2688 | playhead_position(now, ended, duration) |
| 2689 | } |
| 2690 | |
| 2691 | /// One sample of whatever drag is in the hand: the equalizer's handle, a |
| 2692 | /// clip's edge, a colour bar, the speed bar, the volume slider or the |
| 2693 | /// playhead. Each of those starts on a strip a few pixels wide that the |
| 2694 | /// pointer leaves immediately, so none of them can be tracked from the |
| 2695 | /// element it started on -- the gesture is followed here instead, on a |
| 2696 | /// hitbox that covers everything the hand can reach. |
| 2697 | /// |
| 2698 | /// Registered on the root *and* on the scrim of every card that holds a |
| 2699 | /// slider ([`Player::drag_scrim`]). An occluding sheet ends gpui's hit test |
| 2700 | /// where it sits (`Hitbox::is_hovered`, window.rs:788), so while a card is |
| 2701 | /// up the root is not hovered anywhere under it and hears none of this: the |
| 2702 | /// press set a value and the drag then froze on it. |
| 2703 | /// The one event a release outside the window's own surface is |
| 2704 | /// guaranteed to raise: Wayland (and X11) tell a client its pointer has |
| 2705 | /// left, unconditionally, whether or not a button is down |
| 2706 | /// (`wl_pointer::Event::Leave`, gpui's `platform/linux/wayland/client.rs`) |
| 2707 | /// -- but a `Button::Released` that lands *after* that Leave is never |
| 2708 | /// forwarded at all, because gpui only dispatches it to the window that |
| 2709 | /// is still `mouse_focused_window`, and Leave just cleared that. So the |
| 2710 | /// seam's own release handler (`drag_release`, bound to `on_mouse_up`) |
| 2711 | /// can be skipped entirely by a drag that ends outside the frame, and |
| 2712 | /// `drag_move`'s "next incidental motion" fallback above only fires once |
| 2713 | /// the pointer comes back -- which a restart before that motion, or a |
| 2714 | /// release the user never revisits, may never do. This is the one |
| 2715 | /// dependable earlier moment: the size a `Split::Dock`/`Split::Bench` |
| 2716 | /// drag is holding right now is exactly what a release beyond this |
| 2717 | /// instant would have kept, since no further sample can arrive from |
| 2718 | /// outside the surface, so it is saved here rather than left to chance. |
| 2719 | /// Wired at the root via [`Player::mount_mouse_exit_listener`]'s |
| 2720 | /// `window.on_mouse_event`, the low-level door `Interactivity`'s fluent |
| 2721 | /// `on_mouse_*` builders do not expose for `MouseExitEvent`. |
| 2722 | pub(crate) fn drag_left_window(&mut self, cx: &mut Context<Self>) { |
| 2723 | if split_drag_owes_save(self.split_drag) && self.split_moved() { |
| 2724 | save_stance_splits(&self.splits); |
| 2725 | } |
| 2726 | // A number being scrubbed (pan, title fields) is committed here too: its |
| 2727 | // release outside the window never arrives, and it would otherwise be |
| 2728 | // dropped on the next move back in. |
| 2729 | self.pan_scrub_end(cx); |
| 2730 | self.title_scrub_end(cx); |
| 2731 | self.cue_num_scrub_end(cx); |
| 2732 | cx.notify(); |
| 2733 | } |
| 2734 | |
| 2735 | /// Sets a lane's height from the pointer while its edge is held: the |
| 2736 | /// pointer's y less the grab `interact::lane_resize_band` took at the press, |
| 2737 | /// held to the lane range. Live, so the lane follows the hand; the file is |
| 2738 | /// written on release. |
| 2739 | pub(crate) fn drag_lane_resize( |
| 2740 | &mut self, |
| 2741 | lane: Lane, |
| 2742 | grab: f32, |
| 2743 | y: Pixels, |
| 2744 | cx: &mut Context<Self>, |
| 2745 | ) { |
| 2746 | let h = crate::ui::bench_stance::clamp_lane_h(f32::from(y) - grab); |
| 2747 | let h = crate::ui::bench_stance::snap_lane_h(lane.kind, h); |
| 2748 | crate::ui::bench_stance::save_lane_h(&mut self.lane_h, lane, h); |
| 2749 | cx.notify(); |
| 2750 | } |
| 2751 | |
| 2752 | /// A double-click on a lane's edge: the lane goes back to its kind's |
| 2753 | /// default, and the file forgets the height it was dragged to. |
| 2754 | pub(crate) fn reset_lane_h(&mut self, lane: Lane, cx: &mut Context<Self>) { |
| 2755 | self.lane_resize = None; |
| 2756 | crate::ui::bench_stance::forget_lane_h(&mut self.lane_h, lane); |
| 2757 | self.save_view_state(); |
| 2758 | cx.notify(); |
| 2759 | } |
| 2760 | |
| 2761 | /// Sets one lane to a height size from its header's menu, and keeps it |
| 2762 | /// in the project's view state like a drag does. |
| 2763 | pub(crate) fn set_lane_size(&mut self, lane: Lane, size: TrackSize, cx: &mut Context<Self>) { |
| 2764 | self.lane_resize = None; |
| 2765 | crate::ui::bench_stance::set_track_size(&mut self.lane_h, lane, size); |
| 2766 | self.save_view_state(); |
| 2767 | cx.notify(); |
| 2768 | } |
| 2769 | |
| 2770 | /// Sets every lane to one size: the Timeline menu's "All tracks" rows. |
| 2771 | pub(crate) fn set_all_track_sizes(&mut self, size: TrackSize, cx: &mut Context<Self>) { |
| 2772 | let lanes = self.session.as_ref().map_or_else(Vec::new, PlaybackSession::lanes); |
| 2773 | for lane in lanes { |
| 2774 | crate::ui::bench_stance::set_track_size(&mut self.lane_h, lane, size); |
| 2775 | } |
| 2776 | self.save_view_state(); |
| 2777 | cx.notify(); |
| 2778 | } |
| 2779 | |
| 2780 | /// One size taller or shorter for every lane, from each lane's own size: |
| 2781 | /// the keys that make the tracks larger or smaller. |
| 2782 | pub(crate) fn step_track_sizes(&mut self, up: bool, cx: &mut Context<Self>) { |
| 2783 | let lanes = self.session.as_ref().map_or_else(Vec::new, PlaybackSession::lanes); |
| 2784 | let sizes: Vec<(Lane, TrackSize)> = lanes |
| 2785 | .into_iter() |
| 2786 | .map(|lane| (lane, crate::ui::bench_stance::track_size(self, lane).step(up))) |
| 2787 | .collect(); |
| 2788 | for (lane, size) in sizes { |
| 2789 | crate::ui::bench_stance::set_track_size(&mut self.lane_h, lane, size); |
| 2790 | } |
| 2791 | self.save_view_state(); |
| 2792 | cx.notify(); |
| 2793 | } |
| 2794 | |
| 2795 | pub(crate) fn drag_move( |
| 2796 | &mut self, |
| 2797 | event: &MouseMoveEvent, |
| 2798 | window: &mut Window, |
| 2799 | cx: &mut Context<Self>, |
| 2800 | ) { |
| 2801 | if self.pan_scrub.is_some() { |
| 2802 | match event.pressed_button { |
| 2803 | Some(MouseButton::Left) => self.pan_scrub_move(f32::from(event.position.x), cx), |
| 2804 | _ => { |
| 2805 | self.pan_scrub_end(cx); |
| 2806 | } |
| 2807 | } |
| 2808 | return; |
| 2809 | } |
| 2810 | // An envelope drag follows the pointer; a button-up move ends it with its commit. |
| 2811 | if self.gain_edit.is_some() { |
| 2812 | match event.pressed_button { |
| 2813 | Some(MouseButton::Left) => self.gain_move(event.position, cx), |
| 2814 | _ => self.gain_release(cx), |
| 2815 | } |
| 2816 | return; |
| 2817 | } |
| 2818 | // A title number or block being turned follows the pointer; a button-up move ends it. |
| 2819 | if self.title_scrub.is_some() { |
| 2820 | match event.pressed_button { |
| 2821 | Some(MouseButton::Left) => self.title_scrub_move(f32::from(event.position.x), cx), |
| 2822 | _ => { |
| 2823 | self.title_scrub_end(cx); |
| 2824 | } |
| 2825 | } |
| 2826 | return; |
| 2827 | } |
| 2828 | // A caption panel number dragged sideways follows the pointer the same way. |
| 2829 | if self.cue_num_scrub.is_some() { |
| 2830 | match event.pressed_button { |
| 2831 | Some(MouseButton::Left) => self.cue_num_scrub_move(f32::from(event.position.x), event.modifiers.shift, cx), |
| 2832 | _ => { |
| 2833 | self.cue_num_scrub_end(cx); |
| 2834 | } |
| 2835 | } |
| 2836 | return; |
| 2837 | } |
| 2838 | // A box select follows the pointer wherever it goes; a button-up move |
| 2839 | // (released off the window) ends it without selecting. |
| 2840 | if let Some(m) = self.marquee.as_mut() { |
| 2841 | match event.pressed_button { |
| 2842 | Some(MouseButton::Left) => { |
| 2843 | m.to = event.position; |
| 2844 | cx.notify(); |
| 2845 | } |
| 2846 | _ => { |
| 2847 | self.marquee = None; |
| 2848 | cx.notify(); |
| 2849 | } |
| 2850 | } |
| 2851 | return; |
| 2852 | } |
| 2853 | // The scrollbar's thumb first: the pointer leaves the 6 px strip on |
| 2854 | // the first move, so the gesture is followed here for the ruler's own |
| 2855 | // reason -- and it must outrank every other drag, because a hand |
| 2856 | // holding the view is not holding anything on it. |
| 2857 | if self.scroll_drag.is_some() { |
| 2858 | match event.pressed_button { |
| 2859 | Some(MouseButton::Left) => self.scroll_drag_to(event.position.x, cx), |
| 2860 | // A release outside the window never reaches the handler |
| 2861 | // below, so the first button-up move is when we learn the |
| 2862 | // drag is over. |
| 2863 | _ => self.scroll_drag = None, |
| 2864 | } |
| 2865 | return; |
| 2866 | } |
| 2867 | // The lane stack's own thumb, on exactly the same terms: the pointer |
| 2868 | // leaves the strip on the first move, so the root carries the gesture |
| 2869 | // -- and a hand holding the stack is not holding anything on it |
| 2870 | // either. |
| 2871 | if self.lanes_drag.is_some() { |
| 2872 | match event.pressed_button { |
| 2873 | Some(MouseButton::Left) => self.lanes_drag_to(event.position.y, cx), |
| 2874 | _ => self.lanes_drag = None, |
| 2875 | } |
| 2876 | return; |
| 2877 | } |
| 2878 | // A lane's bottom edge is held the same way: its band is the only thing |
| 2879 | // under the pointer, and a release off the window ends it here, with the |
| 2880 | // save `drag_release` would have paid. |
| 2881 | if let Some((lane, grab)) = self.lane_resize { |
| 2882 | match event.pressed_button { |
| 2883 | Some(MouseButton::Left) => { |
| 2884 | self.drag_lane_resize(lane, grab, event.position.y, cx); |
| 2885 | } |
| 2886 | _ => { |
| 2887 | self.lane_resize = None; |
| 2888 | self.save_view_state(); |
| 2889 | } |
| 2890 | } |
| 2891 | return; |
| 2892 | } |
| 2893 | // A divider is answered before every gesture below it: it is pressed on |
| 2894 | // a strip of its own that nothing else is under, so neither can swallow |
| 2895 | // the other -- a seam over the timeline never scrubs, and a scrub is |
| 2896 | // never mistaken for a resize. |
| 2897 | if let Some(split) = self.split_drag { |
| 2898 | match event.pressed_button { |
| 2899 | Some(MouseButton::Left) => self.drag_split(split, event.position, window, cx), |
| 2900 | // Released outside the window: the up below never came, so this |
| 2901 | // is where the gesture ends. The size is already written to |
| 2902 | // `self.splits`, but a persisted seam still owes the save |
| 2903 | // `drag_release` would have paid -- without it the drag |
| 2904 | // holds for the session and reverts on restart. |
| 2905 | _ => { |
| 2906 | self.split_drag = None; |
| 2907 | if split_drag_owes_save(Some(split)) && self.split_moved() { |
| 2908 | save_stance_splits(&self.splits); |
| 2909 | } |
| 2910 | self.split_press = None; |
| 2911 | } |
| 2912 | } |
| 2913 | return; |
| 2914 | } |
| 2915 | // A handle is 10 px across and the pointer leaves it at once, so |
| 2916 | // the equalizer drag is tracked here for the ruler's reason. |
| 2917 | if self.eq_dragging { |
| 2918 | if event.pressed_button == Some(MouseButton::Left) { |
| 2919 | self.drag_band(event.position, cx); |
| 2920 | } else { |
| 2921 | // Released outside the window: the up below never came, |
| 2922 | // so this is where the gesture ends -- and it still owes |
| 2923 | // the one write the whole drag is worth. |
| 2924 | self.eq_dragging = false; |
| 2925 | self.commit_eq(cx); |
| 2926 | } |
| 2927 | return; |
| 2928 | } |
| 2929 | // A clip edge is 6 px wide and the pointer leaves it on the |
| 2930 | // first drag, so the gesture is tracked here for the same |
| 2931 | // reason -- and it ends here too when the button came up |
| 2932 | // outside the window, still owing its one edit. |
| 2933 | if self.pro_drag.is_some() { |
| 2934 | match event.pressed_button { |
| 2935 | Some(MouseButton::Left) => self.pro_to(event.position.x, cx), |
| 2936 | _ => self.commit_pro(cx), |
| 2937 | } |
| 2938 | return; |
| 2939 | } |
| 2940 | // A caption held by its edge or body is tracked here too, for the same reason. |
| 2941 | if self.cue_drag.is_some() { |
| 2942 | match event.pressed_button { |
| 2943 | Some(MouseButton::Left) => self.cue_drag_to(event.position.x, cx), |
| 2944 | _ => self.commit_cue_drag(cx), |
| 2945 | } |
| 2946 | return; |
| 2947 | } |
| 2948 | if self.trim.is_some() { |
| 2949 | match event.pressed_button { |
| 2950 | Some(MouseButton::Left) => { |
| 2951 | self.drag_alt = event.modifiers.alt; |
| 2952 | self.trim_to(event.position.x, cx); |
| 2953 | } |
| 2954 | _ => self.commit_trim(cx), |
| 2955 | } |
| 2956 | return; |
| 2957 | } |
| 2958 | // A fade handle is [`FADE_HANDLE_W`] wide and the pointer leaves it |
| 2959 | // on the first drag, same reason and same shape as a trim's own |
| 2960 | // branch just above. |
| 2961 | if self.fade_drag.is_some() { |
| 2962 | match event.pressed_button { |
| 2963 | Some(MouseButton::Left) => self.fade_drag_to(event.position.x, cx), |
| 2964 | _ => self.commit_fade(cx), |
| 2965 | } |
| 2966 | return; |
| 2967 | } |
| 2968 | // A colour slider is 4 px tall and the pointer leaves it just as |
| 2969 | // fast; every sample is live, so the release owes no write of |
| 2970 | // its own -- what the last sample set is what the clip carries. |
| 2971 | if self.color_dragging { |
| 2972 | if event.pressed_button == Some(MouseButton::Left) { |
| 2973 | self.drag_color(event.position.x, false, cx); |
| 2974 | } else { |
| 2975 | // The release happened outside the window, so this is |
| 2976 | // where the gesture ends -- and it may not end on a |
| 2977 | // sample the worker was too busy to take. |
| 2978 | self.color_dragging = false; |
| 2979 | self.flush_drag(cx); |
| 2980 | } |
| 2981 | return; |
| 2982 | } |
| 2983 | // An easing curve's handle follows the pointer over the whole window, the way the |
| 2984 | // sliders do; a button-up move ends it with its write. |
| 2985 | if self.curve_drag.is_some() { |
| 2986 | match event.pressed_button { |
| 2987 | Some(MouseButton::Left) => self.drag_curve(event.position, cx), |
| 2988 | _ => self.end_curve_drag(event.position, cx), |
| 2989 | } |
| 2990 | return; |
| 2991 | } |
| 2992 | // A colour wheel's puck or master bar: the same live writes. |
| 2993 | if let Some((wheel, master)) = self.color_wheel_drag { |
| 2994 | if event.pressed_button == Some(MouseButton::Left) { |
| 2995 | self.drag_wheel(wheel, master, event.position, false, cx); |
| 2996 | } else { |
| 2997 | self.color_wheel_drag = None; |
| 2998 | self.flush_drag(cx); |
| 2999 | } |
| 3000 | return; |
| 3001 | } |
| 3002 | // The ink wheel, on the root for the same 4 px reason and gated on the |
| 3003 | // worker like the bars above: one sample here is a whole picture |
| 3004 | // rebuilt, so what is written is one ink per frame the worker delivers |
| 3005 | // rather than one per pixel the pointer crossed. |
| 3006 | if self.viz_hs_dragging { |
| 3007 | if event.pressed_button == Some(MouseButton::Left) { |
| 3008 | self.drag_viz_hs(event.position, false, cx); |
| 3009 | } else { |
| 3010 | // The release happened outside the window, so the sample the |
| 3011 | // hand let go on is owed like any other ending. |
| 3012 | self.viz_hs_dragging = false; |
| 3013 | self.flush_drag(cx); |
| 3014 | } |
| 3015 | return; |
| 3016 | } |
| 3017 | // The transform card's sliders, [`Self::color_dragging`]'s own reason. |
| 3018 | if self.transform_dragging { |
| 3019 | if event.pressed_button == Some(MouseButton::Left) { |
| 3020 | self.drag_transform(event.position.x, false, cx); |
| 3021 | } else { |
| 3022 | self.transform_dragging = false; |
| 3023 | self.flush_drag(cx); |
| 3024 | } |
| 3025 | return; |
| 3026 | } |
| 3027 | // The accent sliders in Settings: live while held, kept on release. |
| 3028 | if let Some(slider) = self.accent_drag { |
| 3029 | if event.pressed_button == Some(MouseButton::Left) { |
| 3030 | self.drag_accent(slider, event.position.x, cx); |
| 3031 | } else { |
| 3032 | self.end_accent_drag(); |
| 3033 | } |
| 3034 | return; |
| 3035 | } |
| 3036 | // The speed bar, the same 4 px and the same live writes: the |
| 3037 | // press took the undo step and every sample since is live. |
| 3038 | if self.speed_dragging { |
| 3039 | if event.pressed_button == Some(MouseButton::Left) { |
| 3040 | self.drag_speed(event.position.x, false, cx); |
| 3041 | } else { |
| 3042 | self.speed_dragging = false; |
| 3043 | self.flush_drag(cx); |
| 3044 | } |
| 3045 | return; |
| 3046 | } |
| 3047 | // The effects bars hold a draft and write it once, on release. |
| 3048 | if self.effects_drag.is_some() { |
| 3049 | if event.pressed_button == Some(MouseButton::Left) { |
| 3050 | self.drag_effect(event.position.x, cx); |
| 3051 | } else { |
| 3052 | self.release_effect(cx); |
| 3053 | } |
| 3054 | return; |
| 3055 | } |
| 3056 | // The volume slider, the same live writes: what the hand is on |
| 3057 | // is what the speakers are doing, and there is nothing to undo. |
| 3058 | if self.volume_dragging { |
| 3059 | if event.pressed_button == Some(MouseButton::Left) { |
| 3060 | self.drag_volume(event.position.x, cx); |
| 3061 | } else { |
| 3062 | self.volume_dragging = false; |
| 3063 | } |
| 3064 | return; |
| 3065 | } |
| 3066 | // The preview's own scrub bar, tracked on the root for `scrubbing`'s |
| 3067 | // reason: it is a short bar and the pointer leaves it at once. |
| 3068 | if self.preview_scrubbing { |
| 3069 | if event.pressed_button == Some(MouseButton::Left) { |
| 3070 | self.preview_scrub_to(event.position.x, false, cx); |
| 3071 | } else { |
| 3072 | self.preview_scrubbing = false; |
| 3073 | } |
| 3074 | return; |
| 3075 | } |
| 3076 | if !self.scrubbing { |
| 3077 | return; |
| 3078 | } |
| 3079 | if event.pressed_button == Some(MouseButton::Left) { |
| 3080 | self.scrub_to(event.position.x, false, cx); |
| 3081 | } else { |
| 3082 | // A release outside the window never reaches the handler |
| 3083 | // below, so the first button-up move is when we learn the |
| 3084 | // drag is over. Without this the next hover would scrub. |
| 3085 | self.scrubbing = false; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | /// Takes away what a clip drag shows while it is in the air: the dimmed |
| 3090 | /// source, the snap line and the shadow. Escape calls it, because a drag |
| 3091 | /// cancelled without a release sends no release to clear them. Repaints |
| 3092 | /// only when something was up. |
| 3093 | pub(crate) fn end_drag_preview(&mut self, cx: &mut Context<Self>) { |
| 3094 | let showing = self.drag_src.take().is_some() |
| 3095 | | self.snap_cue.take().is_some() |
| 3096 | | !self.ghost.is_empty(); |
| 3097 | self.ghost.clear(); |
| 3098 | if showing { |
| 3099 | cx.notify(); |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | /// Where a drag ends: the release lands exactly, and whatever the gesture |
| 3104 | /// owes -- one undo step for the equalizer and the trim, a flush for the |
| 3105 | /// live-writing bars -- is paid here. On the root and on a card's scrim |
| 3106 | /// both, for [`Player::drag_move`]'s reason: a release over an open card |
| 3107 | /// never reaches the root. |
| 3108 | pub(crate) fn drag_release( |
| 3109 | &mut self, |
| 3110 | event: &MouseUpEvent, |
| 3111 | window: &mut Window, |
| 3112 | cx: &mut Context<Self>, |
| 3113 | ) { |
| 3114 | // The panel is left exactly where the hand let go, for the reason every |
| 3115 | // other release here lands one last sample. |
| 3116 | // Any release ends a clip move in flight, including one let go off |
| 3117 | // every lane (no drop fires there): the original stops being dimmed |
| 3118 | // and the line stops promising a landing. A trim keeps its line until |
| 3119 | // its own commit below. |
| 3120 | self.drag_src = None; |
| 3121 | if self.gain_edit.is_some() { |
| 3122 | self.gain_release(cx); |
| 3123 | return; |
| 3124 | } |
| 3125 | // The curve's handle lands where the hand let go, as one undo step. |
| 3126 | if self.curve_drag.is_some() { |
| 3127 | self.end_curve_drag(event.position, cx); |
| 3128 | return; |
| 3129 | } |
| 3130 | if self.pan_scrub_end(cx) || self.title_scrub_end(cx) || self.cue_num_scrub_end(cx) { |
| 3131 | return; |
| 3132 | } |
| 3133 | if self.trim.is_none() && self.snap_cue.take().is_some() { |
| 3134 | cx.notify(); |
| 3135 | } |
| 3136 | if let Some(mut m) = self.marquee.take() { |
| 3137 | m.to = event.position; |
| 3138 | self.finish_marquee(m, event.modifiers.control, cx); |
| 3139 | return; |
| 3140 | } |
| 3141 | if self.scroll_drag.take().is_some() { |
| 3142 | self.scroll_drag_to(event.position.x, cx); |
| 3143 | return; |
| 3144 | } |
| 3145 | if self.lanes_drag.take().is_some() { |
| 3146 | self.lanes_drag_to(event.position.y, cx); |
| 3147 | return; |
| 3148 | } |
| 3149 | if let Some((lane, grab)) = self.lane_resize.take() { |
| 3150 | // The height lands where the hand let go, and the view is written |
| 3151 | // once here -- the same round trip a seam's release makes. |
| 3152 | self.drag_lane_resize(lane, grab, event.position.y, cx); |
| 3153 | self.save_view_state(); |
| 3154 | return; |
| 3155 | } |
| 3156 | if let Some(split) = self.split_drag.take() { |
| 3157 | self.drag_split(split, event.position, window, cx); |
| 3158 | let moved = self.split_moved(); |
| 3159 | self.split_press = None; |
| 3160 | // Every seam outlives the window: written once, on the release |
| 3161 | // that ends the gesture, the same small-file round trip |
| 3162 | // `ui::dock_stance`'s tab pick uses -- not on every sample in |
| 3163 | // between, which `drag_move` above never owed a write. |
| 3164 | if split_drag_owes_save(Some(split)) && moved { |
| 3165 | save_stance_splits(&self.splits); |
| 3166 | } |
| 3167 | return; |
| 3168 | } |
| 3169 | if std::mem::take(&mut self.eq_dragging) { |
| 3170 | // The release lands exactly, then the gesture is written |
| 3171 | // once -- the append-only table's whole reason. |
| 3172 | self.drag_band(event.position, cx); |
| 3173 | self.commit_eq(cx); |
| 3174 | return; |
| 3175 | } |
| 3176 | if self.pro_drag.is_some() { |
| 3177 | self.pro_to(event.position.x, cx); |
| 3178 | self.commit_pro(cx); |
| 3179 | return; |
| 3180 | } |
| 3181 | if self.cue_drag.is_some() { |
| 3182 | self.cue_drag_to(event.position.x, cx); |
| 3183 | self.commit_cue_drag(cx); |
| 3184 | return; |
| 3185 | } |
| 3186 | if self.trim.is_some() { |
| 3187 | // The release lands exactly, then the gesture is |
| 3188 | // written once -- one edit, one undo step. |
| 3189 | self.drag_alt = event.modifiers.alt; |
| 3190 | self.trim_to(event.position.x, cx); |
| 3191 | self.trim_flash = None; |
| 3192 | self.commit_trim(cx); |
| 3193 | return; |
| 3194 | } |
| 3195 | if self.fade_drag.is_some() { |
| 3196 | self.fade_drag_to(event.position.x, cx); |
| 3197 | self.commit_fade(cx); |
| 3198 | return; |
| 3199 | } |
| 3200 | if std::mem::take(&mut self.color_dragging) { |
| 3201 | // The release lands exactly where the hand let go, and |
| 3202 | // it is a live write like every other sample: the undo |
| 3203 | // step the gesture rolls back to was the press's. The |
| 3204 | // flush is what makes "exactly" true while the worker is |
| 3205 | // still busy -- the sample above would only be held. |
| 3206 | self.drag_color(event.position.x, false, cx); |
| 3207 | self.flush_drag(cx); |
| 3208 | return; |
| 3209 | } |
| 3210 | if let Some((wheel, master)) = self.color_wheel_drag.take() { |
| 3211 | self.drag_wheel(wheel, master, event.position, false, cx); |
| 3212 | self.flush_drag(cx); |
| 3213 | return; |
| 3214 | } |
| 3215 | if std::mem::take(&mut self.viz_hs_dragging) { |
| 3216 | // The release lands exactly where the hand let go, and the flush |
| 3217 | // is what makes that true while the worker is still busy: the |
| 3218 | // sample above would only be held. |
| 3219 | self.drag_viz_hs(event.position, false, cx); |
| 3220 | self.flush_drag(cx); |
| 3221 | return; |
| 3222 | } |
| 3223 | if std::mem::take(&mut self.transform_dragging) { |
| 3224 | self.drag_transform(event.position.x, false, cx); |
| 3225 | self.flush_drag(cx); |
| 3226 | return; |
| 3227 | } |
| 3228 | if let Some(slider) = self.accent_drag { |
| 3229 | self.drag_accent(slider, event.position.x, cx); |
| 3230 | self.end_accent_drag(); |
| 3231 | return; |
| 3232 | } |
| 3233 | if std::mem::take(&mut self.speed_dragging) { |
| 3234 | self.drag_speed(event.position.x, false, cx); |
| 3235 | self.flush_drag(cx); |
| 3236 | return; |
| 3237 | } |
| 3238 | if self.effects_drag.is_some() { |
| 3239 | self.drag_effect(event.position.x, cx); |
| 3240 | self.release_effect(cx); |
| 3241 | return; |
| 3242 | } |
| 3243 | if std::mem::take(&mut self.volume_dragging) { |
| 3244 | self.drag_volume(event.position.x, cx); |
| 3245 | return; |
| 3246 | } |
| 3247 | if std::mem::take(&mut self.preview_scrubbing) { |
| 3248 | self.preview_scrub_to(event.position.x, true, cx); |
| 3249 | return; |
| 3250 | } |
| 3251 | if std::mem::take(&mut self.scrubbing) { |
| 3252 | self.scrub_to(event.position.x, true, cx); |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | /// Where the hand has taken a divider, live: the panel it belongs to is set |
| 3257 | /// to what the pointer says and the next frame draws it there. Nothing else |
| 3258 | /// is written -- a layout is not an edit, so there is no undo step to take |
| 3259 | /// and no worker to flush. The clamps are the *reader's* |
| 3260 | /// ([`split_size`]), so a size set in one window is still a size in the |
| 3261 | /// next one it is drawn in. |
| 3262 | fn drag_split( |
| 3263 | &mut self, |
| 3264 | split: Split, |
| 3265 | at: Point<Pixels>, |
| 3266 | window: &mut Window, |
| 3267 | cx: &mut Context<Self>, |
| 3268 | ) { |
| 3269 | // A press that has not travelled is a click, not a drag: the grab zone |
| 3270 | // is wider than the seam, so taking the pointer's y as the seam's |
| 3271 | // would move it by the offset of the click. |
| 3272 | if let Some((origin, moved)) = &mut self.split_press |
| 3273 | && !*moved |
| 3274 | { |
| 3275 | if !split_travelled(*origin, at) { |
| 3276 | return; |
| 3277 | } |
| 3278 | *moved = true; |
| 3279 | } |
| 3280 | let viewport = window.viewport_size(); |
| 3281 | let raw = split_drag_size(split, at, viewport); |
| 3282 | // Clamped here, through the same door [`Player::split_px`] draws |
| 3283 | // through, at the one place a hand's pick ever reaches |
| 3284 | // `self.splits` -- so what a later save writes to disk is never a |
| 3285 | // number the seam itself would have refused to draw at. A floor |
| 3286 | // enforced only on read still leaves a `bench=-1` line in the file. |
| 3287 | let lanes = self |
| 3288 | .session |
| 3289 | .as_ref() |
| 3290 | .map_or(2, |session| session.lanes().len()); |
| 3291 | let view = self.view(); |
| 3292 | let scroll = view.duration > view.span(); |
| 3293 | self.splits |
| 3294 | .set(split, split_size(split, Some(raw), lanes, viewport, scroll)); |
| 3295 | cx.notify(); |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | /// `pick`, if the session still has that exact hole on its lane: the same |
| 3300 | /// `(start, frames)` answered by [`PlaybackSession::gap_at`] at its first frame. |
| 3301 | /// A hole that moved, shrank or closed answers `None`, which is how a stale |
| 3302 | /// pick is dropped. Free, and not a method, so a test can ask it of a session. |
| 3303 | pub(crate) fn live_gap(session: &PlaybackSession, pick: GapPick) -> Option<GapPick> { |
| 3304 | (session.gap_at(pick.lane, pick.start) == Some((pick.start, pick.frames))).then_some(pick) |
| 3305 | } |
| 3306 | |
| 3307 | /// The volume envelope's gestures on an audio clip: a press adds or grabs a key, a drag moves |
| 3308 | /// it (or a segment of the line), a release commits the whole drag as one undo step, and Delete |
| 3309 | /// or a double-click takes a key out. The geometry is [`clip_envelope`]'s own, so what is drawn |
| 3310 | /// is what is hit. |
| 3311 | impl Player { |
| 3312 | /// The clip's envelope geometry at the timeline's current zoom: its keys, its clip gain, its |
| 3313 | /// source range and speed, and the pixels per timeline frame. `None` for a video clip or a |
| 3314 | /// clip that is not there. |
| 3315 | fn gain_geometry(&self, lane: Lane, idx: usize) -> Option<(Vec<engine::GainKey>, f32, u32, u32, f64, f32)> { |
| 3316 | let session = self.session.as_ref()?; |
| 3317 | if lane.kind != LaneKind::Audio { |
| 3318 | return None; |
| 3319 | } |
| 3320 | let clip = session.lane_clips(lane).get(idx)?; |
| 3321 | let ppf = (self.scale.pps / self.fps) as f32; |
| 3322 | Some((clip.gain_keys.clone(), clip.gain_db, clip.in_frame, clip.out_frame, clip.speed.as_f64(), ppf)) |
| 3323 | } |
| 3324 | |
| 3325 | /// The envelope's keys as the drag shows them, for the clip that is being dragged. |
| 3326 | pub(crate) fn shown_gain_keys(&self, lane: Lane, idx: usize) -> Option<Vec<engine::GainKey>> { |
| 3327 | self.gain_edit |
| 3328 | .as_ref() |
| 3329 | .filter(|e| (e.lane, e.idx) == (lane, idx)) |
| 3330 | .map(|e| e.shown.clone()) |
| 3331 | } |
| 3332 | |
| 3333 | /// Whether the pointer is over a key or the line of this clip's envelope: what makes its |
| 3334 | /// layer take the press and show its cursor. |
| 3335 | pub(crate) fn gain_hot(&self, lane: Lane, idx: usize) -> bool { |
| 3336 | matches!(self.gain_hover, Some((l, i, _)) if (l, i) == (lane, idx)) |
| 3337 | } |
| 3338 | |
| 3339 | /// The envelope's hit under the clip-local point `local` in a box of `size`, or `None`. |
| 3340 | fn gain_hit_at(&self, lane: Lane, idx: usize, local: Point<Pixels>, size: Size<Pixels>) -> Option<EnvHit> { |
| 3341 | let (keys, gain, in_frame, _, speed, ppf) = self.gain_geometry(lane, idx)?; |
| 3342 | clip_envelope::hit( |
| 3343 | &keys, |
| 3344 | gain, |
| 3345 | in_frame, |
| 3346 | speed, |
| 3347 | ppf, |
| 3348 | (f32::from(size.width), f32::from(size.height)), |
| 3349 | (f32::from(local.x), f32::from(local.y)), |
| 3350 | ) |
| 3351 | } |
| 3352 | |
| 3353 | /// The pointer moved over a clip's envelope layer: the hover that sets its cursor. |
| 3354 | pub(crate) fn gain_hover_at(&mut self, lane: Lane, idx: usize, local: Point<Pixels>, size: Size<Pixels>, cx: &mut Context<Self>) { |
| 3355 | let hit = self.gain_hit_at(lane, idx, local, size).filter(|h| matches!(h, EnvHit::Key(_)) || matches!(h, EnvHit::Line(_))); |
| 3356 | let next = hit.map(|h| (lane, idx, h)); |
| 3357 | if self.gain_hover != next { |
| 3358 | self.gain_hover = next; |
| 3359 | cx.notify(); |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | /// The pointer left a clip's envelope layer. |
| 3364 | pub(crate) fn gain_hover_end(&mut self, lane: Lane, idx: usize, cx: &mut Context<Self>) { |
| 3365 | if self.gain_hot(lane, idx) { |
| 3366 | self.gain_hover = None; |
| 3367 | cx.notify(); |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | /// A press on a clip's envelope layer. Returns whether it was taken here (so the clip body |
| 3372 | /// under it does not also select). Ctrl on the line adds a key where the line is; a press on a |
| 3373 | /// key grabs it (two clicks take it out); a press on the line grabs its segment. |
| 3374 | pub(crate) fn gain_press_at( |
| 3375 | &mut self, |
| 3376 | lane: Lane, |
| 3377 | idx: usize, |
| 3378 | event: &MouseDownEvent, |
| 3379 | bounds: Bounds<Pixels>, |
| 3380 | cx: &mut Context<Self>, |
| 3381 | ) -> bool { |
| 3382 | let (window, origin, size) = (event.position, bounds.origin, bounds.size); |
| 3383 | let (ctrl, clicks) = (event.modifiers.control, event.click_count); |
| 3384 | if self.covers_room() || self.exporting().is_some() || self.lane_locked_refusal(lane) { |
| 3385 | return false; |
| 3386 | } |
| 3387 | let local = point(window.x - origin.x, window.y - origin.y); |
| 3388 | let Some(hit) = self.gain_hit_at(lane, idx, local, size) else { |
| 3389 | return false; |
| 3390 | }; |
| 3391 | let Some((keys, gain, in_frame, out_frame, speed, ppf)) = self.gain_geometry(lane, idx) else { |
| 3392 | return false; |
| 3393 | }; |
| 3394 | let (lo, hi) = (engine::project::MIN_CLIP_GAIN_DB, engine::project::MAX_CLIP_GAIN_DB); |
| 3395 | match (hit, ctrl) { |
| 3396 | (EnvHit::Line(_), true) => { |
| 3397 | // Where the line is, so the key adds no change of sound. |
| 3398 | let src = f64::from(in_frame) + f64::from(local.x) / f64::from(ppf) * speed; |
| 3399 | let at = (src.round().max(0.) as u32).clamp(in_frame, out_frame); |
| 3400 | let db = engine::project::gain_keys_db_at(&keys, f64::from(at)).clamp(lo, hi); |
| 3401 | if self.session.as_mut().is_some_and(|s| s.add_gain_key(lane, idx, engine::GainKey::new(at, db))) { |
| 3402 | self.mark_dirty(); |
| 3403 | } |
| 3404 | self.gain_pick = Some((lane, idx, at)); |
| 3405 | self.pick((lane, idx), false, cx); |
| 3406 | cx.notify(); |
| 3407 | true |
| 3408 | } |
| 3409 | (EnvHit::Key(i), _) => { |
| 3410 | let at = keys[i].at; |
| 3411 | if clicks >= 2 { |
| 3412 | if self.session.as_mut().is_some_and(|s| s.remove_gain_key(lane, idx, at)) { |
| 3413 | self.mark_dirty(); |
| 3414 | } |
| 3415 | self.gain_pick = None; |
| 3416 | cx.notify(); |
| 3417 | return true; |
| 3418 | } |
| 3419 | self.gain_pick = Some((lane, idx, at)); |
| 3420 | self.pick((lane, idx), false, cx); |
| 3421 | let grab_db = (gain + keys[i].db) - clip_envelope::db_of_y(f32::from(local.y), f32::from(size.height)); |
| 3422 | self.gain_edit = Some(GainEdit { |
| 3423 | lane, |
| 3424 | idx, |
| 3425 | grab: GainGrab::Key(i), |
| 3426 | press: window, |
| 3427 | origin, |
| 3428 | size, |
| 3429 | shown: keys.clone(), |
| 3430 | keys, |
| 3431 | clip_gain: gain, |
| 3432 | in_frame, |
| 3433 | out_frame, |
| 3434 | speed, |
| 3435 | px_per_frame: ppf, |
| 3436 | grab_db, |
| 3437 | }); |
| 3438 | cx.notify(); |
| 3439 | true |
| 3440 | } |
| 3441 | (EnvHit::Line(Some(i)), false) => { |
| 3442 | let grab_db = clip_envelope::db_of_y(f32::from(local.y), f32::from(size.height)); |
| 3443 | self.pick((lane, idx), false, cx); |
| 3444 | self.gain_edit = Some(GainEdit { |
| 3445 | lane, |
| 3446 | idx, |
| 3447 | grab: GainGrab::Segment(i), |
| 3448 | press: window, |
| 3449 | origin, |
| 3450 | size, |
| 3451 | shown: keys.clone(), |
| 3452 | keys, |
| 3453 | clip_gain: gain, |
| 3454 | in_frame, |
| 3455 | out_frame, |
| 3456 | speed, |
| 3457 | px_per_frame: ppf, |
| 3458 | grab_db, |
| 3459 | }); |
| 3460 | cx.notify(); |
| 3461 | true |
| 3462 | } |
| 3463 | // The flat line past the keys is the clip's own, not the envelope's: the press |
| 3464 | // falls through to the body. |
| 3465 | (EnvHit::Line(_), false) => false, |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | /// The held envelope drag follows the pointer: a key moves in time and dB within its |
| 3470 | /// neighbours and the clip's source range, a segment moves both keys by one dB amount. |
| 3471 | pub(crate) fn gain_move(&mut self, position: Point<Pixels>, cx: &mut Context<Self>) { |
| 3472 | let Some(edit) = self.gain_edit.as_mut() else { |
| 3473 | return; |
| 3474 | }; |
| 3475 | let local_y = f32::from(position.y - edit.origin.y); |
| 3476 | let height = f32::from(edit.size.height); |
| 3477 | let (lo_db, hi_db) = (engine::project::MIN_CLIP_GAIN_DB, engine::project::MAX_CLIP_GAIN_DB); |
| 3478 | let dx = f32::from(position.x - edit.press.x); |
| 3479 | let mut shown = edit.keys.clone(); |
| 3480 | match edit.grab { |
| 3481 | GainGrab::Key(i) => { |
| 3482 | let k = edit.keys[i]; |
| 3483 | let prev = i.checked_sub(1).map(|j| edit.keys[j].at + 1).unwrap_or(edit.in_frame); |
| 3484 | let next = edit.keys.get(i + 1).map(|n| n.at.saturating_sub(1)).unwrap_or(edit.out_frame); |
| 3485 | let (lo, hi) = (prev.max(edit.in_frame), next.min(edit.out_frame)); |
| 3486 | let src = f64::from(k.at) + f64::from(dx) / f64::from(edit.px_per_frame) * edit.speed; |
| 3487 | let at = if lo <= hi { (src.round().max(0.) as u32).clamp(lo, hi) } else { k.at }; |
| 3488 | let disp = clip_envelope::db_of_y(local_y, height) + edit.grab_db; |
| 3489 | let db = (disp - edit.clip_gain).clamp(lo_db, hi_db); |
| 3490 | shown[i] = engine::GainKey { at, db, ..k }; |
| 3491 | } |
| 3492 | GainGrab::Segment(i) => { |
| 3493 | let delta = clip_envelope::db_of_y(local_y, height) - edit.grab_db; |
| 3494 | let pair = [edit.keys[i], edit.keys[i + 1]]; |
| 3495 | let most_up = pair.iter().map(|k| hi_db - k.db).fold(f32::INFINITY, f32::min); |
| 3496 | let most_down = pair.iter().map(|k| lo_db - k.db).fold(f32::NEG_INFINITY, f32::max); |
| 3497 | let delta = if most_down <= most_up { delta.clamp(most_down, most_up) } else { 0. }; |
| 3498 | for j in [i, i + 1] { |
| 3499 | shown[j] = engine::GainKey { db: edit.keys[j].db + delta, ..edit.keys[j] }; |
| 3500 | } |
| 3501 | } |
| 3502 | } |
| 3503 | edit.shown = shown; |
| 3504 | cx.notify(); |
| 3505 | } |
| 3506 | |
| 3507 | /// The release of an envelope drag: one edit, one undo step, and nothing at all when the |
| 3508 | /// hand came back to where it was. |
| 3509 | pub(crate) fn gain_release(&mut self, cx: &mut Context<Self>) { |
| 3510 | let Some(edit) = self.gain_edit.take() else { |
| 3511 | return; |
| 3512 | }; |
| 3513 | let moved = edit.shown.iter().zip(&edit.keys).any(|(a, b)| a.at != b.at || a.db != b.db); |
| 3514 | if moved && self.session.as_mut().is_some_and(|s| s.set_gain_keys(edit.lane, edit.idx, edit.shown)) { |
| 3515 | self.mark_dirty(); |
| 3516 | } |
| 3517 | cx.notify(); |
| 3518 | } |
| 3519 | |
| 3520 | /// Delete with a key picked takes that key out, one undo step. `false` when no key is picked. |
| 3521 | pub(crate) fn gain_delete_pick(&mut self, cx: &mut Context<Self>) -> bool { |
| 3522 | let Some((lane, idx, at)) = self.gain_pick.take() else { |
| 3523 | return false; |
| 3524 | }; |
| 3525 | if self.session.as_mut().is_some_and(|s| s.remove_gain_key(lane, idx, at)) { |
| 3526 | self.mark_dirty(); |
| 3527 | } |
| 3528 | cx.notify(); |
| 3529 | true |
| 3530 | } |
| 3531 | } |