| 1 | mod keymap; |
| 2 | |
| 3 | use keymap::{ActionId, Keymap}; |
| 4 | |
| 5 | use std::cell::Cell; |
| 6 | use std::collections::HashMap; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | use std::rc::Rc; |
| 9 | use std::sync::Arc; |
| 10 | use std::time::{Duration, Instant}; |
| 11 | |
| 12 | use engine::audio::StreamInfo; |
| 13 | use engine::export::ExportSettings; |
| 14 | use engine::project::{Lane, Source}; |
| 15 | use engine::{Clip, ExportHandle, Frame, PlaybackSession}; |
| 16 | use gpui::{ |
| 17 | AnyElement, App, Application, Bounds, ClickEvent, Context, FocusHandle, KeyDownEvent, |
| 18 | MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, Point, |
| 19 | RenderImage, SharedString, Size, TitlebarOptions, Window, WindowBounds, WindowOptions, canvas, |
| 20 | div, img, point, prelude::*, px, relative, rgb, rgba, size, |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | |
| 25 | const LETTERBOX: u32 = 0x101010; |
| 26 | const CHROME: u32 = 0x242424; |
| 27 | const SURFACE: u32 = 0x333333; |
| 28 | const INK: u32 = 0xc8c8c8; |
| 29 | const ACCENT: u32 = 0x4a9eff; |
| 30 | |
| 31 | const SELECTED: u32 = 0x2a4a6b; |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | const SOURCE_TINTS: [u32; 4] = [SURFACE, 0x3b3329, 0x293b33, 0x33293b]; |
| 37 | |
| 38 | |
| 39 | |
| 40 | const HOVER: u32 = 0x3f3f3f; |
| 41 | const HOVER_DIM: u32 = 0x2c2c2c; |
| 42 | |
| 43 | |
| 44 | const INK_DIM: u32 = 0xa0a0a0; |
| 45 | |
| 46 | |
| 47 | |
| 48 | const HEADER_H: f32 = 32.; |
| 49 | |
| 50 | |
| 51 | |
| 52 | const PANEL_H: f32 = 220.; |
| 53 | const LANE_H: f32 = 48.; |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | const HEADER_W: f32 = 40.; |
| 59 | const HEADER_GAP: f32 = 4.; |
| 60 | |
| 61 | const LABEL_H: f32 = 15.; |
| 62 | |
| 63 | |
| 64 | const LABEL_MIN_W: f32 = 36.; |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | const WAVE_BPS: u32 = 40; |
| 70 | |
| 71 | |
| 72 | const WAVE_COL: f32 = 2.; |
| 73 | |
| 74 | |
| 75 | const HIT_MIN: f32 = 24.; |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | const LIBRARY_FRAC: f32 = 0.2; |
| 81 | const LIBRARY_MIN_W: f32 = 120.; |
| 82 | const LIBRARY_MAX_W: f32 = 220.; |
| 83 | |
| 84 | |
| 85 | const ROW_H: f32 = 32.; |
| 86 | |
| 87 | |
| 88 | const SWATCH_W: f32 = 4.; |
| 89 | const CONTROL_H: f32 = 28.; |
| 90 | const RULER_HIT_H: f32 = HIT_MIN; |
| 91 | |
| 92 | |
| 93 | const TIME_W: f32 = 200.; |
| 94 | |
| 95 | |
| 96 | const KEYS_W: f32 = 320.; |
| 97 | const KEYS_ROW_H: f32 = HIT_MIN; |
| 98 | |
| 99 | |
| 100 | |
| 101 | const KEYS_ROWS_H: f32 = 10. * KEYS_ROW_H; |
| 102 | |
| 103 | |
| 104 | |
| 105 | const MENU_W: f32 = 260.; |
| 106 | const MENU_ROW_H: f32 = HIT_MIN; |
| 107 | const MENU_PAD: f32 = 6.; |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | const ESCAPE: &str = "escape"; |
| 114 | |
| 115 | |
| 116 | |
| 117 | const NO_FILE: &str = "no file open"; |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 131 | struct Volume { |
| 132 | steps: u8, |
| 133 | muted: bool, |
| 134 | } |
| 135 | |
| 136 | impl Volume { |
| 137 | |
| 138 | const MAX_STEPS: u8 = 20; |
| 139 | |
| 140 | |
| 141 | |
| 142 | fn gain(self) -> f32 { |
| 143 | if self.muted { |
| 144 | 0. |
| 145 | } else { |
| 146 | f32::from(self.steps) / f32::from(Self::MAX_STEPS) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | fn step(&mut self, up: bool) { |
| 153 | self.steps = if up { |
| 154 | self.steps.saturating_add(1).min(Self::MAX_STEPS) |
| 155 | } else { |
| 156 | self.steps.saturating_sub(1) |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | fn label(self) -> String { |
| 163 | let percent = u32::from(self.steps) * 100 / u32::from(Self::MAX_STEPS); |
| 164 | if self.muted { |
| 165 | format!("Muted {percent}%") |
| 166 | } else { |
| 167 | format!("Vol {percent}%") |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | impl Default for Volume { |
| 173 | fn default() -> Self { |
| 174 | Self { |
| 175 | steps: Self::MAX_STEPS, |
| 176 | muted: false, |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | #[derive(Clone)] |
| 185 | enum Wave { |
| 186 | |
| 187 | Loading, |
| 188 | |
| 189 | Silent, |
| 190 | Peaks(Arc<Vec<(f32, f32)>>), |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | struct AssetDrag(PathBuf, usize); |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | #[derive(Clone, Copy)] |
| 203 | struct ContextMenu { |
| 204 | lane: Lane, |
| 205 | idx: usize, |
| 206 | at: Point<Pixels>, |
| 207 | details: bool, |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | const MENU_ITEMS: [ActionId; 5] = [ |
| 215 | ActionId::Cut, |
| 216 | ActionId::Delete, |
| 217 | ActionId::Lift, |
| 218 | ActionId::Regroup, |
| 219 | ActionId::ToggleMute, |
| 220 | ]; |
| 221 | |
| 222 | struct Player { |
| 223 | |
| 224 | |
| 225 | |
| 226 | session: Option<PlaybackSession>, |
| 227 | |
| 228 | |
| 229 | fps: f64, |
| 230 | name: SharedString, |
| 231 | image: Option<Arc<RenderImage>>, |
| 232 | |
| 233 | held: Option<Frame>, |
| 234 | |
| 235 | eos: bool, |
| 236 | |
| 237 | done: bool, |
| 238 | |
| 239 | |
| 240 | pending_seek: bool, |
| 241 | |
| 242 | |
| 243 | ruler: Rc<Cell<Bounds<Pixels>>>, |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | selected: Option<(Lane, usize)>, |
| 249 | |
| 250 | |
| 251 | |
| 252 | context_menu: Option<ContextMenu>, |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | selected_asset: Option<(PathBuf, usize)>, |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | waves: HashMap<(PathBuf, usize), Wave>, |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | streams: HashMap<PathBuf, Vec<StreamInfo>>, |
| 268 | |
| 269 | |
| 270 | clipboard: Option<Clip>, |
| 271 | |
| 272 | |
| 273 | scrubbing: bool, |
| 274 | last_scrub: Instant, |
| 275 | last_target: u32, |
| 276 | |
| 277 | export: Option<ExportHandle>, |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | cancelling: bool, |
| 283 | |
| 284 | |
| 285 | export_path: PathBuf, |
| 286 | |
| 287 | |
| 288 | |
| 289 | project_path: PathBuf, |
| 290 | |
| 291 | |
| 292 | keymap: Keymap, |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | volume: Volume, |
| 299 | |
| 300 | |
| 301 | |
| 302 | keys_open: bool, |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | export_open: bool, |
| 308 | |
| 309 | |
| 310 | |
| 311 | quality: Quality, |
| 312 | custom_mbps: u32, |
| 313 | |
| 314 | |
| 315 | rebinding: Option<ActionId>, |
| 316 | |
| 317 | |
| 318 | |
| 319 | notice: Option<SharedString>, |
| 320 | |
| 321 | |
| 322 | |
| 323 | titled: String, |
| 324 | displayed: u32, |
| 325 | dropped: u32, |
| 326 | |
| 327 | started: Option<Instant>, |
| 328 | focus: FocusHandle, |
| 329 | } |
| 330 | |
| 331 | impl Player { |
| 332 | |
| 333 | |
| 334 | |
| 335 | fn pump(&mut self, window: &mut Window) { |
| 336 | |
| 337 | |
| 338 | let Some(session) = &mut self.session else { |
| 339 | return; |
| 340 | }; |
| 341 | let target = session.now() * self.fps; |
| 342 | let mut newest: Option<Frame> = None; |
| 343 | loop { |
| 344 | let frame = match self.held.take() { |
| 345 | Some(frame) => frame, |
| 346 | |
| 347 | |
| 348 | |
| 349 | None => match session.try_frame() { |
| 350 | Some(frame) => frame, |
| 351 | None => { |
| 352 | self.eos = session.is_eos(); |
| 353 | break; |
| 354 | } |
| 355 | }, |
| 356 | }; |
| 357 | if f64::from(frame.index) <= target { |
| 358 | self.dropped += u32::from(newest.is_some()); |
| 359 | newest = Some(frame); |
| 360 | } else { |
| 361 | self.held = Some(frame); |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | if let Some(frame) = newest { |
| 367 | self.displayed += 1; |
| 368 | self.pending_seek = false; |
| 369 | self.started.get_or_insert_with(|| { |
| 370 | eprintln!("first frame displayed (index {})", frame.index); |
| 371 | Instant::now() |
| 372 | }); |
| 373 | let buf = image::RgbaImage::from_raw(frame.width, frame.height, frame.bgra) |
| 374 | .expect("frame buffer sized width*height*4"); |
| 375 | let next = Arc::new(RenderImage::new(vec![image::Frame::new(buf)])); |
| 376 | if let Some(old) = self.image.replace(next) { |
| 377 | |
| 378 | |
| 379 | let _ = window.drop_image(old); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if self.eos && self.held.is_none() && !self.done { |
| 384 | self.done = true; |
| 385 | |
| 386 | |
| 387 | self.pending_seek = false; |
| 388 | let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64()); |
| 389 | eprintln!( |
| 390 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", |
| 391 | self.displayed, |
| 392 | self.dropped, |
| 393 | session.now() |
| 394 | ); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | fn reset_after_reseek(&mut self) { |
| 403 | self.held = None; |
| 404 | self.eos = false; |
| 405 | self.done = false; |
| 406 | self.pending_seek = true; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | fn act(&mut self, action: ActionId, cx: &mut Context<Self>) { |
| 413 | match action { |
| 414 | ActionId::Play => self.toggle_or_restart(cx), |
| 415 | ActionId::Export => self.open_export(cx), |
| 416 | ActionId::Save => self.save_project(cx), |
| 417 | ActionId::Copy => self.copy_selected(), |
| 418 | ActionId::Paste => self.paste(cx), |
| 419 | ActionId::Cut => self.cut(cx), |
| 420 | ActionId::Regroup => self.regroup(cx), |
| 421 | ActionId::Delete => self.delete_selected(cx), |
| 422 | ActionId::Lift => self.lift_selected(cx), |
| 423 | ActionId::Undo => self.undo(cx), |
| 424 | ActionId::ToggleMute => self.set_volume(|volume| volume.muted = !volume.muted, cx), |
| 425 | ActionId::VolumeUp => self.set_volume(|volume| volume.step(true), cx), |
| 426 | ActionId::VolumeDown => self.set_volume(|volume| volume.step(false), cx), |
| 427 | |
| 428 | |
| 429 | ActionId::CancelExport => {} |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | fn modal(&self) -> bool { |
| 437 | self.keys_open || self.export_open || self.exporting().is_some() |
| 438 | } |
| 439 | |
| 440 | |
| 441 | fn seek(&mut self, t: f64, cx: &mut Context<Self>) { |
| 442 | if self.exporting().is_some() { |
| 443 | return; |
| 444 | } |
| 445 | let Some(session) = &mut self.session else { |
| 446 | return; |
| 447 | }; |
| 448 | session.seek(t); |
| 449 | self.reset_after_reseek(); |
| 450 | cx.notify(); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | |
| 455 | fn cut(&mut self, cx: &mut Context<Self>) { |
| 456 | if self.exporting().is_some() { |
| 457 | return; |
| 458 | } |
| 459 | if let Some(session) = &mut self.session { |
| 460 | session.cut_at(session.now()); |
| 461 | } |
| 462 | self.selected = None; |
| 463 | cx.notify(); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | fn regroup(&mut self, cx: &mut Context<Self>) { |
| 472 | if self.exporting().is_some() { |
| 473 | return; |
| 474 | } |
| 475 | if let Some(session) = &mut self.session { |
| 476 | if session.regroup_at(session.now()) { |
| 477 | self.selected = None; |
| 478 | } else { |
| 479 | self.notice = Some( |
| 480 | "NOTHING TO REGROUP — put the playhead where two clips meet, on frames that were cut apart" |
| 481 | .into(), |
| 482 | ); |
| 483 | } |
| 484 | } |
| 485 | cx.notify(); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | fn delete_selected(&mut self, cx: &mut Context<Self>) { |
| 494 | if self.exporting().is_some() { |
| 495 | return; |
| 496 | } |
| 497 | let deleted = match (&mut self.session, self.selected.take()) { |
| 498 | (Some(session), Some((Lane::Video, idx))) => session.delete_clip(idx), |
| 499 | (Some(session), Some((Lane::Audio, idx))) => match video_half(session, idx) { |
| 500 | Some(video) => session.delete_clip(video), |
| 501 | None => session.lift_clip(Lane::Audio, idx), |
| 502 | }, |
| 503 | _ => false, |
| 504 | }; |
| 505 | if deleted { |
| 506 | self.reset_after_reseek(); |
| 507 | } |
| 508 | cx.notify(); |
| 509 | } |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | fn lift_selected(&mut self, cx: &mut Context<Self>) { |
| 515 | if self.exporting().is_some() { |
| 516 | return; |
| 517 | } |
| 518 | match (&mut self.session, self.selected.take()) { |
| 519 | (Some(session), Some((lane, idx))) => { |
| 520 | if session.lift_clip(lane, idx) { |
| 521 | self.reset_after_reseek(); |
| 522 | } else { |
| 523 | self.notice = Some("NOTHING LIFTED — the timeline cannot be emptied".into()); |
| 524 | } |
| 525 | } |
| 526 | (Some(_), None) => { |
| 527 | self.notice = Some("NOTHING LIFTED — click the half to remove first".into()) |
| 528 | } |
| 529 | (None, _) => {} |
| 530 | } |
| 531 | cx.notify(); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | fn copy_selected(&mut self) { |
| 536 | let session = self.session.as_ref(); |
| 537 | |
| 538 | |
| 539 | |
| 540 | if let Some(clip) = self |
| 541 | .selected |
| 542 | .and_then(|(lane, idx)| session?.lane_clips(lane).get(idx).copied()) |
| 543 | { |
| 544 | self.clipboard = Some(clip); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | fn cache_media(&mut self, cx: &mut Context<Self>) { |
| 561 | let Some(session) = &self.session else { |
| 562 | return; |
| 563 | }; |
| 564 | |
| 565 | |
| 566 | |
| 567 | for path in unseen_paths(session.sources(), &self.streams) { |
| 568 | self.streams.insert(path.clone(), Vec::new()); |
| 569 | let probed = cx.background_executor().spawn({ |
| 570 | let path = path.clone(); |
| 571 | async move { engine::AudioSession::probe_streams(&path).unwrap_or_default() } |
| 572 | }); |
| 573 | cx.spawn(async move |this, cx| { |
| 574 | let probed = probed.await; |
| 575 | this.update(cx, |this, cx| { |
| 576 | this.streams.insert(path, probed); |
| 577 | cx.notify(); |
| 578 | }) |
| 579 | .ok(); |
| 580 | }) |
| 581 | .detach(); |
| 582 | } |
| 583 | for key in unseen_sources(session.sources(), &self.waves) { |
| 584 | self.waves.insert(key.clone(), Wave::Loading); |
| 585 | let decoded = cx.background_executor().spawn({ |
| 586 | let (path, stream) = key.clone(); |
| 587 | async move { |
| 588 | engine::waveform::peaks(&path, stream, WAVE_BPS) |
| 589 | .ok() |
| 590 | .flatten() |
| 591 | .map(|peaks| Arc::new(normalise(peaks))) |
| 592 | } |
| 593 | }); |
| 594 | cx.spawn(async move |this, cx| { |
| 595 | let decoded = decoded.await; |
| 596 | this.update(cx, |this, cx| { |
| 597 | this.waves.insert( |
| 598 | key, |
| 599 | match decoded { |
| 600 | Some(peaks) => Wave::Peaks(peaks), |
| 601 | |
| 602 | |
| 603 | |
| 604 | None => Wave::Silent, |
| 605 | }, |
| 606 | ); |
| 607 | cx.notify(); |
| 608 | }) |
| 609 | .ok(); |
| 610 | }) |
| 611 | .detach(); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | |
| 616 | fn selected_link(&self) -> Option<u32> { |
| 617 | let (lane, idx) = self.selected?; |
| 618 | self.session.as_ref()?.lane_clips(lane).get(idx)?.link |
| 619 | } |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | fn paste(&mut self, cx: &mut Context<Self>) { |
| 625 | let pasted = match (&mut self.session, self.clipboard) { |
| 626 | (Some(session), Some(clip)) => session.paste_at(session.now(), clip), |
| 627 | _ => false, |
| 628 | }; |
| 629 | if pasted { |
| 630 | self.selected = None; |
| 631 | self.reset_after_reseek(); |
| 632 | } |
| 633 | cx.notify(); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | fn insert_source(&mut self, path: &Path, stream: usize, cx: &mut Context<Self>) { |
| 644 | if self.exporting().is_some() { |
| 645 | return; |
| 646 | } |
| 647 | let frames = self.session.as_ref().map_or(0, |session| { |
| 648 | source_frames(lane_clips(session), session.sources(), path) |
| 649 | }); |
| 650 | |
| 651 | |
| 652 | |
| 653 | let placed = match (&mut self.session, frames) { |
| 654 | (Some(session), 1..) => session.place_stream_at(session.now(), path, stream, frames), |
| 655 | _ => Ok(false), |
| 656 | }; |
| 657 | match placed { |
| 658 | Ok(true) => { |
| 659 | self.selected = None; |
| 660 | self.reset_after_reseek(); |
| 661 | } |
| 662 | |
| 663 | |
| 664 | Err(e) => self.notice = Some(format!("NOTHING ADDED — {e}").into()), |
| 665 | Ok(false) => { |
| 666 | self.notice = Some( |
| 667 | "NOTHING ADDED — no clip on the timeline plays from that file any more".into(), |
| 668 | ) |
| 669 | } |
| 670 | } |
| 671 | cx.notify(); |
| 672 | } |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | fn timeline_audio(&self) -> Option<(u32, u16)> { |
| 678 | let first = self.session.as_ref()?.sources().first()?; |
| 679 | let info = self |
| 680 | .streams |
| 681 | .get(&first.path)? |
| 682 | .iter() |
| 683 | .find(|s| s.index == first.audio_stream)?; |
| 684 | Some((info.sample_rate, info.channels)) |
| 685 | } |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | fn import(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { |
| 692 | if self.exporting().is_some() { |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | |
| 698 | let text = match self.session.as_mut().map(|session| session.import(path)) { |
| 699 | Some(Ok(())) => { |
| 700 | self.reset_after_reseek(); |
| 701 | format!("IMPORTED {}", file_name(path)) |
| 702 | } |
| 703 | Some(Err(e)) => format!("IMPORT FAILED: {e}"), |
| 704 | None => self.open_media(path), |
| 705 | }; |
| 706 | eprintln!("{text}"); |
| 707 | self.notice = Some(text.into()); |
| 708 | cx.notify(); |
| 709 | } |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | fn open_media(&mut self, path: &std::path::Path) -> String { |
| 717 | match PlaybackSession::open(path) { |
| 718 | Ok(session) => { |
| 719 | self.fps = session.meta().frame_rate; |
| 720 | |
| 721 | |
| 722 | let silent = audio_notice(&session); |
| 723 | self.session = Some(session); |
| 724 | |
| 725 | |
| 726 | self.apply_volume(); |
| 727 | self.export_path = export_path(path); |
| 728 | self.project_path = project_path(path); |
| 729 | self.name = file_name(path).into(); |
| 730 | self.reset_after_reseek(); |
| 731 | format!("OPENED {}{}", file_name(path), silent.unwrap_or_default()) |
| 732 | } |
| 733 | Err(e) => format!("OPEN FAILED: {e}"), |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | fn pick_and_import(&mut self, cx: &mut Context<Self>) { |
| 742 | if self.exporting().is_some() { |
| 743 | return; |
| 744 | } |
| 745 | let picked = cx.background_executor().spawn(async { pick_file() }); |
| 746 | cx.spawn(async move |this, cx| { |
| 747 | let picked = picked.await; |
| 748 | this.update(cx, |this, cx| match picked { |
| 749 | |
| 750 | |
| 751 | Ok(Some(path)) if is_project(&path) => this.load_project(&path, cx), |
| 752 | Ok(Some(path)) => this.import(&path, cx), |
| 753 | |
| 754 | Ok(None) => {} |
| 755 | Err(text) => { |
| 756 | eprintln!("{text}"); |
| 757 | this.notice = Some(text.into()); |
| 758 | cx.notify(); |
| 759 | } |
| 760 | }) |
| 761 | .ok(); |
| 762 | }) |
| 763 | .detach(); |
| 764 | } |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | fn load_project(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { |
| 771 | if self.exporting().is_some() { |
| 772 | return; |
| 773 | } |
| 774 | let text = match PlaybackSession::open_project(path) { |
| 775 | Ok(session) => { |
| 776 | self.fps = session.meta().frame_rate; |
| 777 | let silent = audio_notice(&session); |
| 778 | |
| 779 | |
| 780 | self.export_path = export_path(&session.sources()[0].path); |
| 781 | self.session = Some(session); |
| 782 | self.apply_volume(); |
| 783 | self.project_path = path.to_path_buf(); |
| 784 | self.name = file_name(path).into(); |
| 785 | |
| 786 | |
| 787 | self.clipboard = None; |
| 788 | self.selected = None; |
| 789 | |
| 790 | |
| 791 | |
| 792 | self.context_menu = None; |
| 793 | |
| 794 | |
| 795 | self.selected_asset = None; |
| 796 | |
| 797 | |
| 798 | self.displayed = 0; |
| 799 | self.dropped = 0; |
| 800 | self.started = None; |
| 801 | |
| 802 | |
| 803 | |
| 804 | self.reset_after_reseek(); |
| 805 | format!("LOADED {}{}", file_name(path), silent.unwrap_or_default()) |
| 806 | } |
| 807 | Err(e) => format!("OPEN FAILED: {e}"), |
| 808 | }; |
| 809 | eprintln!("{text}"); |
| 810 | self.notice = Some(text.into()); |
| 811 | cx.notify(); |
| 812 | } |
| 813 | |
| 814 | |
| 815 | |
| 816 | fn save_project(&mut self, cx: &mut Context<Self>) { |
| 817 | let saved = self |
| 818 | .session |
| 819 | .as_ref() |
| 820 | .map(|session| session.save_project(&self.project_path)); |
| 821 | let text = match saved { |
| 822 | Some(Ok(())) => format!("SAVED {}", file_name(&self.project_path)), |
| 823 | Some(Err(e)) => format!("SAVE FAILED: {e}"), |
| 824 | None => "NOTHING TO SAVE — open a file first".to_string(), |
| 825 | }; |
| 826 | eprintln!("{text}"); |
| 827 | self.notice = Some(text.into()); |
| 828 | cx.notify(); |
| 829 | } |
| 830 | |
| 831 | fn undo(&mut self, cx: &mut Context<Self>) { |
| 832 | if self.session.as_mut().is_some_and(PlaybackSession::undo) { |
| 833 | self.reset_after_reseek(); |
| 834 | } |
| 835 | self.selected = None; |
| 836 | cx.notify(); |
| 837 | } |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | fn capture(&mut self, action: ActionId, key: &str, ctrl: bool) { |
| 847 | let chord = keymap::Chord { |
| 848 | key: key.to_string(), |
| 849 | ctrl, |
| 850 | }; |
| 851 | let text = match self.keymap.rebind_action(action, chord.clone()) { |
| 852 | Ok(()) => { |
| 853 | self.rebinding = None; |
| 854 | match self.keymap.save() { |
| 855 | Ok(()) => return, |
| 856 | Err(e) => format!( |
| 857 | "KEYBINDINGS NOT SAVED — {}: {e}", |
| 858 | Keymap::config_path().display() |
| 859 | ), |
| 860 | } |
| 861 | } |
| 862 | Err(holder) => format!("ALREADY BOUND — {} is {}", chord.pretty(), holder.label()), |
| 863 | }; |
| 864 | eprintln!("{text}"); |
| 865 | self.notice = Some(text.into()); |
| 866 | } |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | fn scrub_to(&mut self, x: Pixels, commit: bool, cx: &mut Context<Self>) { |
| 872 | let Some(session) = &self.session else { |
| 873 | return; |
| 874 | }; |
| 875 | let t = f64::from(frac_along(x, self.ruler.get())) * session.timeline_duration(); |
| 876 | let target = (t * self.fps) as u32; |
| 877 | if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { |
| 878 | self.last_target = target; |
| 879 | self.last_scrub = Instant::now(); |
| 880 | self.seek(t, cx); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | fn apply_volume(&self) { |
| 892 | if let Some(session) = &self.session { |
| 893 | session.set_gain(self.volume.gain()); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | fn set_volume(&mut self, change: impl FnOnce(&mut Volume), cx: &mut Context<Self>) { |
| 901 | change(&mut self.volume); |
| 902 | self.apply_volume(); |
| 903 | cx.notify(); |
| 904 | } |
| 905 | |
| 906 | fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { |
| 907 | if self.exporting().is_some() { |
| 908 | return; |
| 909 | } |
| 910 | if self.done { |
| 911 | self.seek(0., cx); |
| 912 | if let Some(session) = &mut self.session { |
| 913 | session.play(); |
| 914 | } |
| 915 | } else if let Some(session) = &mut self.session { |
| 916 | session.toggle(); |
| 917 | |
| 918 | cx.notify(); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | |
| 923 | |
| 924 | fn exporting(&self) -> Option<&ExportHandle> { |
| 925 | self.export.as_ref().filter(|_| !self.cancelling) |
| 926 | } |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | fn open_export(&mut self, cx: &mut Context<Self>) { |
| 932 | if self.export.is_some() { |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | |
| 937 | if self.session.is_none() { |
| 938 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); |
| 939 | cx.notify(); |
| 940 | return; |
| 941 | } |
| 942 | self.export_open = true; |
| 943 | |
| 944 | |
| 945 | self.keys_open = false; |
| 946 | self.rebinding = None; |
| 947 | cx.notify(); |
| 948 | } |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | fn pick_destination(&mut self, cx: &mut Context<Self>) { |
| 955 | let default = self.export_path.clone(); |
| 956 | let picked = cx |
| 957 | .background_executor() |
| 958 | .spawn(async move { pick_save(&default) }); |
| 959 | cx.spawn(async move |this, cx| { |
| 960 | let picked = picked.await; |
| 961 | this.update(cx, |this, cx| { |
| 962 | |
| 963 | |
| 964 | if this.export.is_some() { |
| 965 | return; |
| 966 | } |
| 967 | match picked { |
| 968 | Ok(Some(path)) => this.export_path = path, |
| 969 | |
| 970 | Ok(None) => {} |
| 971 | Err(text) => { |
| 972 | eprintln!("{text}"); |
| 973 | this.notice = Some(text.into()); |
| 974 | } |
| 975 | } |
| 976 | cx.notify(); |
| 977 | }) |
| 978 | .ok(); |
| 979 | }) |
| 980 | .detach(); |
| 981 | } |
| 982 | |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | fn start_export(&mut self, cx: &mut Context<Self>) { |
| 990 | if self.export.is_some() { |
| 991 | return; |
| 992 | } |
| 993 | let settings = export_settings(self.quality, self.custom_mbps); |
| 994 | let Some(session) = &mut self.session else { |
| 995 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); |
| 996 | cx.notify(); |
| 997 | return; |
| 998 | }; |
| 999 | session.pause(); |
| 1000 | self.export = Some(session.export_to_with(&self.export_path, &settings)); |
| 1001 | |
| 1002 | |
| 1003 | self.export_open = false; |
| 1004 | cx.notify(); |
| 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | fn cancel_export(&mut self) { |
| 1010 | if let Some(export) = &self.export { |
| 1011 | export.cancel(); |
| 1012 | self.cancelling = true; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | fn poll_export(&mut self) { |
| 1019 | let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else { |
| 1020 | return; |
| 1021 | }; |
| 1022 | self.export = None; |
| 1023 | |
| 1024 | |
| 1025 | if std::mem::take(&mut self.cancelling) { |
| 1026 | return; |
| 1027 | } |
| 1028 | let text = match result { |
| 1029 | Ok(()) => format!("EXPORT DONE → {}", file_name(&self.export_path)), |
| 1030 | Err(e) => format!("EXPORT FAILED: {e}"), |
| 1031 | }; |
| 1032 | eprintln!("{text}"); |
| 1033 | self.notice = Some(text.into()); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | impl Render for Player { |
| 1038 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | if let Some(session) = &mut self.session { |
| 1043 | session.tick(); |
| 1044 | } |
| 1045 | self.pump(window); |
| 1046 | self.poll_export(); |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | self.cache_media(cx); |
| 1051 | |
| 1052 | |
| 1053 | let title = window_title(&self.name); |
| 1054 | if title != self.titled { |
| 1055 | window.set_window_title(&title); |
| 1056 | self.titled = title; |
| 1057 | } |
| 1058 | |
| 1059 | let playing = self |
| 1060 | .session |
| 1061 | .as_ref() |
| 1062 | .is_some_and(PlaybackSession::is_playing); |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | if (playing && !self.done) || self.pending_seek || self.export.is_some() { |
| 1071 | window.request_animation_frame(); |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | let duration = self |
| 1077 | .session |
| 1078 | .as_ref() |
| 1079 | .map_or(0., PlaybackSession::timeline_duration); |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | let position = if self.done { |
| 1084 | duration |
| 1085 | } else { |
| 1086 | self.session |
| 1087 | .as_ref() |
| 1088 | .map_or(0., PlaybackSession::now) |
| 1089 | .clamp(0., duration) |
| 1090 | }; |
| 1091 | |
| 1092 | div() |
| 1093 | .track_focus(&self.focus) |
| 1094 | .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| { |
| 1095 | |
| 1096 | |
| 1097 | if event.is_held { |
| 1098 | return; |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | if this.notice.take().is_some() { |
| 1105 | cx.notify(); |
| 1106 | } |
| 1107 | let key = event.keystroke.key.as_str(); |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | if let Some(action) = this.rebinding { |
| 1113 | if key == ESCAPE { |
| 1114 | this.rebinding = None; |
| 1115 | } else if !is_bare_modifier(key) { |
| 1116 | this.capture(action, key, event.keystroke.modifiers.control); |
| 1117 | } |
| 1118 | cx.notify(); |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | let action = this.keymap.lookup(key, event.keystroke.modifiers.control); |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | if this.exporting().is_some() { |
| 1130 | if cancels_export(key, action) { |
| 1131 | this.cancel_export(); |
| 1132 | } |
| 1133 | cx.notify(); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | if this.keys_open { |
| 1140 | if key == ESCAPE { |
| 1141 | this.keys_open = false; |
| 1142 | cx.notify(); |
| 1143 | } |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | if this.export_open { |
| 1153 | if key == ESCAPE { |
| 1154 | this.export_open = false; |
| 1155 | } else if let Ok(digit) = key.parse::<u32>() { |
| 1156 | this.custom_mbps = push_digit(this.custom_mbps, digit); |
| 1157 | this.quality = Quality::Custom; |
| 1158 | } else if key == "backspace" { |
| 1159 | this.custom_mbps /= 10; |
| 1160 | this.quality = Quality::Custom; |
| 1161 | } |
| 1162 | cx.notify(); |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | if this.context_menu.take().is_some() { |
| 1170 | cx.notify(); |
| 1171 | if key == ESCAPE { |
| 1172 | return; |
| 1173 | } |
| 1174 | } |
| 1175 | if let Some(action) = action { |
| 1176 | this.act(action, cx); |
| 1177 | } |
| 1178 | })) |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | .on_drop(cx.listener(|this, paths: &gpui::ExternalPaths, _, cx| { |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | if this.keys_open || this.export_open { |
| 1191 | return; |
| 1192 | } |
| 1193 | for path in paths.paths() { |
| 1194 | |
| 1195 | if is_project(path) { |
| 1196 | this.load_project(path, cx); |
| 1197 | } else { |
| 1198 | this.import(path, cx); |
| 1199 | } |
| 1200 | } |
| 1201 | })) |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | .on_mouse_move(cx.listener(|this, event: &MouseMoveEvent, _, cx| { |
| 1206 | if !this.scrubbing { |
| 1207 | return; |
| 1208 | } |
| 1209 | if event.pressed_button == Some(MouseButton::Left) { |
| 1210 | this.scrub_to(event.position.x, false, cx); |
| 1211 | } else { |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | this.scrubbing = false; |
| 1216 | } |
| 1217 | })) |
| 1218 | .on_mouse_up( |
| 1219 | MouseButton::Left, |
| 1220 | cx.listener(|this, event: &MouseUpEvent, _, cx| { |
| 1221 | if std::mem::take(&mut this.scrubbing) { |
| 1222 | this.scrub_to(event.position.x, true, cx); |
| 1223 | } |
| 1224 | }), |
| 1225 | ) |
| 1226 | .size_full() |
| 1227 | .flex() |
| 1228 | .flex_col() |
| 1229 | .bg(rgb(LETTERBOX)) |
| 1230 | .text_color(rgb(INK)) |
| 1231 | .text_size(px(13.)) |
| 1232 | .child( |
| 1233 | div() |
| 1234 | .flex_none() |
| 1235 | .h(px(HEADER_H)) |
| 1236 | .flex() |
| 1237 | .items_center() |
| 1238 | .px(px(12.)) |
| 1239 | .bg(rgb(CHROME)) |
| 1240 | .child(self.name.clone()), |
| 1241 | ) |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | .child( |
| 1247 | div() |
| 1248 | .flex_1() |
| 1249 | .min_h(px(0.)) |
| 1250 | .flex() |
| 1251 | .child(self.library(library_w(f32::from(window.viewport_size().width)), cx)) |
| 1252 | .child( |
| 1253 | div() |
| 1254 | .flex_1() |
| 1255 | .min_w(px(0.)) |
| 1256 | .overflow_hidden() |
| 1257 | .flex() |
| 1258 | .justify_center() |
| 1259 | .items_center() |
| 1260 | .children( |
| 1261 | self.image |
| 1262 | .clone() |
| 1263 | .map(|i| { |
| 1264 | img(i) |
| 1265 | .size_full() |
| 1266 | .object_fit(gpui::ObjectFit::Contain) |
| 1267 | .into_any_element() |
| 1268 | }) |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | |
| 1273 | |
| 1274 | .or_else(|| { |
| 1275 | self.session |
| 1276 | .is_none() |
| 1277 | .then(|| empty_hint().into_any_element()) |
| 1278 | }), |
| 1279 | ), |
| 1280 | ), |
| 1281 | ) |
| 1282 | |
| 1283 | |
| 1284 | .children(self.notice_bar(cx)) |
| 1285 | .child(self.panel(position, duration, playing, cx)) |
| 1286 | |
| 1287 | |
| 1288 | .children(self.context_card(window.viewport_size(), cx)) |
| 1289 | |
| 1290 | |
| 1291 | .children(self.keys_overlay(cx)) |
| 1292 | .children(self.export_card(cx)) |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | impl Player { |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | fn library(&self, width: f32, cx: &mut Context<Self>) -> impl IntoElement { |
| 1309 | let exporting = self.exporting().is_some(); |
| 1310 | let sources = self |
| 1311 | .session |
| 1312 | .as_ref() |
| 1313 | .map_or(&[][..], PlaybackSession::sources); |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | let meta = self.session.as_ref().map(PlaybackSession::meta); |
| 1318 | let clips: Vec<Clip> = self |
| 1319 | .session |
| 1320 | .as_ref() |
| 1321 | .map_or_else(Vec::new, |session| lane_clips(session).copied().collect()); |
| 1322 | let rows: Vec<_> = library_rows(sources, &self.streams, self.timeline_audio(), |path| { |
| 1323 | source_frames(clips.iter(), sources, path) |
| 1324 | }) |
| 1325 | .into_iter() |
| 1326 | .enumerate() |
| 1327 | .map(|(i, row)| { |
| 1328 | let picked = self |
| 1329 | .selected_asset |
| 1330 | .as_ref() |
| 1331 | .is_some_and(|p| *p == (row.path.clone(), row.stream)); |
| 1332 | let name: SharedString = row.name.clone().into(); |
| 1333 | let tip: SharedString = match (&row.unusable, meta) { |
| 1334 | |
| 1335 | |
| 1336 | (Some(why), _) => format!("{} — {why}", row.path.display()), |
| 1337 | (None, Some(meta)) => format!( |
| 1338 | "{} — {}x{} @ {:.2} fps · drag onto a lane, or Add at playhead", |
| 1339 | row.path.display(), |
| 1340 | meta.width, |
| 1341 | meta.height, |
| 1342 | meta.frame_rate |
| 1343 | ), |
| 1344 | (None, None) => row.path.display().to_string(), |
| 1345 | } |
| 1346 | .into(); |
| 1347 | let ghost = name.clone(); |
| 1348 | |
| 1349 | |
| 1350 | let under = match &row.unusable { |
| 1351 | Some(why) => join_detail(&row.detail, why), |
| 1352 | None => join_detail( |
| 1353 | &row.detail, |
| 1354 | &timecode(f64::from(row.frames) / self.fps, self.fps), |
| 1355 | ), |
| 1356 | }; |
| 1357 | let usable = row.unusable.is_none(); |
| 1358 | let (path, stream) = (row.path.clone(), row.stream); |
| 1359 | let dragged = (path.clone(), stream); |
| 1360 | div() |
| 1361 | .id(("asset", i)) |
| 1362 | .flex_none() |
| 1363 | .h(px(ROW_H)) |
| 1364 | .flex() |
| 1365 | .items_center() |
| 1366 | .gap(px(6.)) |
| 1367 | .pr(px(6.)) |
| 1368 | .rounded(px(3.)) |
| 1369 | |
| 1370 | |
| 1371 | .when(!usable, |d| d.text_color(rgb(INK_DIM)).opacity(0.55)) |
| 1372 | .when(usable, |d| { |
| 1373 | d.cursor_pointer() |
| 1374 | .hover(|s| s.bg(rgb(HOVER))) |
| 1375 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 1376 | this.selected_asset = Some((path.clone(), stream)); |
| 1377 | cx.notify(); |
| 1378 | })) |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | .on_drag(AssetDrag(dragged.0, dragged.1), move |_, _, _, cx| { |
| 1384 | cx.new(|_| Tip(ghost.clone())) |
| 1385 | }) |
| 1386 | }) |
| 1387 | .when(picked, |d| d.bg(rgb(SELECTED)).border_1()) |
| 1388 | .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | .child( |
| 1393 | div() |
| 1394 | .flex_none() |
| 1395 | .w(px(SWATCH_W)) |
| 1396 | .h_full() |
| 1397 | .rounded(px(2.)) |
| 1398 | .bg(rgb(source_tint(row.tint))), |
| 1399 | ) |
| 1400 | .child( |
| 1401 | div() |
| 1402 | .flex_1() |
| 1403 | .min_w(px(0.)) |
| 1404 | .flex() |
| 1405 | .flex_col() |
| 1406 | |
| 1407 | |
| 1408 | |
| 1409 | .child(div().truncate().text_size(px(11.)).child(name)) |
| 1410 | .child( |
| 1411 | div() |
| 1412 | .truncate() |
| 1413 | .text_size(px(10.)) |
| 1414 | .text_color(rgb(INK_DIM)) |
| 1415 | .child(under), |
| 1416 | ), |
| 1417 | ) |
| 1418 | }) |
| 1419 | .collect(); |
| 1420 | div() |
| 1421 | .flex_none() |
| 1422 | .w(px(width)) |
| 1423 | .h_full() |
| 1424 | .flex() |
| 1425 | .flex_col() |
| 1426 | .gap(px(6.)) |
| 1427 | .p(px(8.)) |
| 1428 | .bg(rgb(CHROME)) |
| 1429 | .child( |
| 1430 | div() |
| 1431 | .flex_none() |
| 1432 | .flex() |
| 1433 | .items_center() |
| 1434 | .gap(px(6.)) |
| 1435 | .child( |
| 1436 | div() |
| 1437 | .flex_1() |
| 1438 | .min_w(px(0.)) |
| 1439 | .truncate() |
| 1440 | .text_size(px(11.)) |
| 1441 | .text_color(rgb(INK_DIM)) |
| 1442 | .child("Media"), |
| 1443 | ) |
| 1444 | .child(control( |
| 1445 | "import", |
| 1446 | None, |
| 1447 | "Import", |
| 1448 | "opens a file chooser — or drop a file on the window".to_string(), |
| 1449 | !exporting, |
| 1450 | cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_import(cx)), |
| 1451 | )), |
| 1452 | ) |
| 1453 | .child( |
| 1454 | div() |
| 1455 | .id("library-rows") |
| 1456 | .flex_1() |
| 1457 | .min_h(px(0.)) |
| 1458 | .flex() |
| 1459 | .flex_col() |
| 1460 | .gap(px(2.)) |
| 1461 | .overflow_y_scroll() |
| 1462 | |
| 1463 | |
| 1464 | .when(rows.is_empty(), |d| { |
| 1465 | d.child( |
| 1466 | div() |
| 1467 | .text_size(px(11.)) |
| 1468 | .text_color(rgb(INK_DIM)) |
| 1469 | .child("No media yet — Import, or drop a file on the window"), |
| 1470 | ) |
| 1471 | }) |
| 1472 | .children(rows), |
| 1473 | ) |
| 1474 | .child(control( |
| 1475 | "add-asset", |
| 1476 | None, |
| 1477 | "Add at playhead", |
| 1478 | match self.selected_asset { |
| 1479 | Some(_) => "inserts the picked file at the playhead".to_string(), |
| 1480 | None => "click a file above first — or drag one onto a lane".to_string(), |
| 1481 | }, |
| 1482 | can_add( |
| 1483 | self.selected_asset.as_ref(), |
| 1484 | self.session.is_some(), |
| 1485 | exporting, |
| 1486 | ), |
| 1487 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 1488 | if let Some((path, stream)) = this.selected_asset.clone() { |
| 1489 | this.insert_source(&path, stream, cx); |
| 1490 | } |
| 1491 | }), |
| 1492 | )) |
| 1493 | } |
| 1494 | |
| 1495 | |
| 1496 | fn panel( |
| 1497 | &self, |
| 1498 | position: f64, |
| 1499 | duration: f64, |
| 1500 | playing: bool, |
| 1501 | cx: &mut Context<Self>, |
| 1502 | ) -> impl IntoElement { |
| 1503 | let filled = if duration > 0. { |
| 1504 | (position / duration) as f32 |
| 1505 | } else { |
| 1506 | 0. |
| 1507 | }; |
| 1508 | |
| 1509 | |
| 1510 | |
| 1511 | let exporting = self.exporting().is_some(); |
| 1512 | |
| 1513 | |
| 1514 | let live = self.session.is_some() && !exporting; |
| 1515 | let key = |action| self.keymap.display(action); |
| 1516 | let (hint, filled) = if let Some(export) = self.exporting() { |
| 1517 | let progress = export.progress(); |
| 1518 | ( |
| 1519 | format!( |
| 1520 | "EXPORTING {}% — {} cancels", |
| 1521 | (progress * 100.) as u32, |
| 1522 | key(ActionId::CancelExport) |
| 1523 | ), |
| 1524 | progress, |
| 1525 | ) |
| 1526 | } else { |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | ( |
| 1532 | format!( |
| 1533 | "{} copy · {} paste · {} undo · click the bar to seek · drop a file to import", |
| 1534 | key(ActionId::Copy), |
| 1535 | key(ActionId::Paste), |
| 1536 | key(ActionId::Undo) |
| 1537 | ), |
| 1538 | filled, |
| 1539 | ) |
| 1540 | }; |
| 1541 | div() |
| 1542 | .flex_none() |
| 1543 | .h(px(PANEL_H)) |
| 1544 | .flex() |
| 1545 | .flex_col() |
| 1546 | .gap(px(8.)) |
| 1547 | .px(px(12.)) |
| 1548 | .py(px(8.)) |
| 1549 | .bg(rgb(CHROME)) |
| 1550 | |
| 1551 | |
| 1552 | .child( |
| 1553 | div() |
| 1554 | .flex_none() |
| 1555 | .flex() |
| 1556 | .items_center() |
| 1557 | .gap(px(8.)) |
| 1558 | .child(control( |
| 1559 | "transport", |
| 1560 | Some(transport_glyph(playing).into_any_element()), |
| 1561 | if playing { "Pause" } else { "Play" }, |
| 1562 | key(ActionId::Play), |
| 1563 | live, |
| 1564 | cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)), |
| 1565 | )) |
| 1566 | .child(separator()) |
| 1567 | .child(control( |
| 1568 | "cut", |
| 1569 | Some(cut_glyph().into_any_element()), |
| 1570 | "Cut", |
| 1571 | format!( |
| 1572 | "{} — splits the clip under the playhead", |
| 1573 | key(ActionId::Cut) |
| 1574 | ), |
| 1575 | live, |
| 1576 | cx.listener(|this, _: &ClickEvent, _, cx| this.cut(cx)), |
| 1577 | )) |
| 1578 | .child(control( |
| 1579 | "delete", |
| 1580 | Some(delete_glyph().into_any_element()), |
| 1581 | "Delete", |
| 1582 | if self.selected.is_some() { |
| 1583 | key(ActionId::Delete) |
| 1584 | } else { |
| 1585 | format!("{} — click a clip below first", key(ActionId::Delete)) |
| 1586 | }, |
| 1587 | live && self.selected.is_some(), |
| 1588 | cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)), |
| 1589 | )) |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 | .child(control( |
| 1594 | "volume", |
| 1595 | None, |
| 1596 | self.volume.label(), |
| 1597 | format!( |
| 1598 | "{} mutes · {} louder · {} quieter", |
| 1599 | key(ActionId::ToggleMute), |
| 1600 | key(ActionId::VolumeUp), |
| 1601 | key(ActionId::VolumeDown) |
| 1602 | ), |
| 1603 | live, |
| 1604 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 1605 | this.set_volume(|volume| volume.muted = !volume.muted, cx) |
| 1606 | }), |
| 1607 | )) |
| 1608 | .child(separator()) |
| 1609 | |
| 1610 | |
| 1611 | |
| 1612 | .child(control( |
| 1613 | "export", |
| 1614 | None, |
| 1615 | "Export", |
| 1616 | format!( |
| 1617 | "{} — quality and destination, then writes the timeline out", |
| 1618 | key(ActionId::Export) |
| 1619 | ), |
| 1620 | live && self.export.is_none(), |
| 1621 | cx.listener(|this, _: &ClickEvent, _, cx| this.open_export(cx)), |
| 1622 | )) |
| 1623 | .child(control( |
| 1624 | "save", |
| 1625 | None, |
| 1626 | "Save", |
| 1627 | format!("{} — writes the project file", key(ActionId::Save)), |
| 1628 | live, |
| 1629 | cx.listener(|this, _: &ClickEvent, _, cx| this.save_project(cx)), |
| 1630 | )) |
| 1631 | |
| 1632 | |
| 1633 | |
| 1634 | .child(control( |
| 1635 | "keys", |
| 1636 | None, |
| 1637 | "Keys", |
| 1638 | "show and change the keybindings".to_string(), |
| 1639 | !exporting, |
| 1640 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 1641 | this.keys_open = !this.keys_open; |
| 1642 | this.rebinding = None; |
| 1643 | |
| 1644 | this.export_open = false; |
| 1645 | cx.notify(); |
| 1646 | }), |
| 1647 | )), |
| 1648 | ) |
| 1649 | .child( |
| 1650 | div() |
| 1651 | .flex_none() |
| 1652 | .flex() |
| 1653 | .items_center() |
| 1654 | .gap(px(12.)) |
| 1655 | |
| 1656 | |
| 1657 | .child(div().flex_none().w(px(TIME_W)).truncate().child(format!( |
| 1658 | "{} / {}", |
| 1659 | timecode(position, self.fps), |
| 1660 | timecode(duration, self.fps) |
| 1661 | ))) |
| 1662 | .child( |
| 1663 | div() |
| 1664 | .flex_1() |
| 1665 | .min_w(px(0.)) |
| 1666 | .truncate() |
| 1667 | .text_size(px(11.)) |
| 1668 | .text_color(rgb(INK_DIM)) |
| 1669 | .child(hint), |
| 1670 | ), |
| 1671 | ) |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | .child( |
| 1677 | div() |
| 1678 | .flex_none() |
| 1679 | .flex() |
| 1680 | .gap(px(HEADER_GAP)) |
| 1681 | |
| 1682 | |
| 1683 | |
| 1684 | .child(div().flex_none().w(px(HEADER_W))) |
| 1685 | .child( |
| 1686 | div() |
| 1687 | .id("ruler") |
| 1688 | .flex_1() |
| 1689 | .min_w(px(0.)) |
| 1690 | .h(px(RULER_HIT_H)) |
| 1691 | .flex() |
| 1692 | .flex_col() |
| 1693 | .justify_center() |
| 1694 | .rounded(px(3.)) |
| 1695 | .cursor_pointer() |
| 1696 | .hover(|s| s.bg(rgb(HOVER_DIM))) |
| 1697 | |
| 1698 | |
| 1699 | .tooltip(|_, cx| cx.new(|_| Tip("Seek — click or drag".into())).into()) |
| 1700 | .on_mouse_down( |
| 1701 | MouseButton::Left, |
| 1702 | cx.listener(|this, event: &MouseDownEvent, _, cx| { |
| 1703 | this.scrubbing = true; |
| 1704 | this.scrub_to(event.position.x, true, cx); |
| 1705 | }), |
| 1706 | ) |
| 1707 | .child( |
| 1708 | div() |
| 1709 | .w_full() |
| 1710 | .h(px(6.)) |
| 1711 | .rounded(px(3.)) |
| 1712 | .bg(rgb(SURFACE)) |
| 1713 | .child(bounds_probe(self.ruler.clone())) |
| 1714 | .child( |
| 1715 | div() |
| 1716 | .h_full() |
| 1717 | .w(relative(filled)) |
| 1718 | .rounded(px(3.)) |
| 1719 | .bg(rgb(ACCENT)), |
| 1720 | ), |
| 1721 | ), |
| 1722 | ), |
| 1723 | ) |
| 1724 | .child(self.lane_row(Lane::Video, "V1", duration, filled, cx)) |
| 1725 | .child(self.lane_row(Lane::Audio, "A1", duration, filled, cx)) |
| 1726 | } |
| 1727 | |
| 1728 | |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | fn notice_bar(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 1733 | let notice = self.notice.clone()?; |
| 1734 | Some( |
| 1735 | div() |
| 1736 | .id("notice") |
| 1737 | .flex_none() |
| 1738 | .flex() |
| 1739 | .items_start() |
| 1740 | .gap(px(12.)) |
| 1741 | .px(px(12.)) |
| 1742 | .py(px(6.)) |
| 1743 | .bg(rgb(SURFACE)) |
| 1744 | .cursor_pointer() |
| 1745 | .hover(|s| s.bg(rgb(HOVER))) |
| 1746 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 1747 | this.notice = None; |
| 1748 | cx.notify(); |
| 1749 | })) |
| 1750 | .child(div().flex_1().min_w(px(0.)).child(notice)) |
| 1751 | .child( |
| 1752 | div() |
| 1753 | .flex_none() |
| 1754 | .text_size(px(11.)) |
| 1755 | .text_color(rgb(INK_DIM)) |
| 1756 | .child("click or press any key to dismiss"), |
| 1757 | ), |
| 1758 | ) |
| 1759 | } |
| 1760 | |
| 1761 | |
| 1762 | |
| 1763 | |
| 1764 | |
| 1765 | |
| 1766 | |
| 1767 | |
| 1768 | |
| 1769 | |
| 1770 | fn keys_overlay(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 1771 | if !self.keys_open { |
| 1772 | return None; |
| 1773 | } |
| 1774 | |
| 1775 | let out = keymap::Chord { |
| 1776 | key: ESCAPE.to_string(), |
| 1777 | ctrl: false, |
| 1778 | } |
| 1779 | .pretty(); |
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 1786 | for category in keymap::Category::ALL { |
| 1787 | let actions = ActionId::ALL |
| 1788 | .into_iter() |
| 1789 | .enumerate() |
| 1790 | .filter(|(_, a)| a.category() == category); |
| 1791 | let fixed = keymap::FIXED.iter().filter(|f| f.category == category); |
| 1792 | let mut headed = false; |
| 1793 | let mut head = |rows: &mut Vec<AnyElement>| { |
| 1794 | if !std::mem::replace(&mut headed, true) { |
| 1795 | rows.push( |
| 1796 | div() |
| 1797 | .flex_none() |
| 1798 | .px(px(6.)) |
| 1799 | .pt(px(4.)) |
| 1800 | .text_size(px(11.)) |
| 1801 | .text_color(rgb(INK_DIM)) |
| 1802 | .child(category.label()) |
| 1803 | .into_any_element(), |
| 1804 | ); |
| 1805 | } |
| 1806 | }; |
| 1807 | for (i, action) in actions { |
| 1808 | head(&mut rows); |
| 1809 | let capturing = self.rebinding == Some(action); |
| 1810 | let out = out.clone(); |
| 1811 | rows.push( |
| 1812 | div() |
| 1813 | .id(("bind", i)) |
| 1814 | .flex() |
| 1815 | |
| 1816 | |
| 1817 | .min_h(px(KEYS_ROW_H)) |
| 1818 | .items_center() |
| 1819 | .justify_between() |
| 1820 | .gap(px(12.)) |
| 1821 | .px(px(6.)) |
| 1822 | .rounded(px(3.)) |
| 1823 | .cursor_pointer() |
| 1824 | .hover(|s| s.bg(rgb(HOVER))) |
| 1825 | .when(capturing, |d| d.bg(rgb(SELECTED))) |
| 1826 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 1827 | this.rebinding = Some(action); |
| 1828 | cx.notify(); |
| 1829 | })) |
| 1830 | .child(action.label()) |
| 1831 | .child(if capturing { |
| 1832 | div() |
| 1833 | .text_color(rgb(INK_DIM)) |
| 1834 | .child(format!("press a key — {out} cancels")) |
| 1835 | } else { |
| 1836 | div().child(self.keymap.display(action)) |
| 1837 | }) |
| 1838 | .into_any_element(), |
| 1839 | ); |
| 1840 | } |
| 1841 | for f in fixed { |
| 1842 | head(&mut rows); |
| 1843 | rows.push( |
| 1844 | div() |
| 1845 | .flex() |
| 1846 | .min_h(px(KEYS_ROW_H)) |
| 1847 | .items_center() |
| 1848 | .justify_between() |
| 1849 | .gap(px(12.)) |
| 1850 | .px(px(6.)) |
| 1851 | .child(f.label) |
| 1852 | |
| 1853 | .child(div().text_color(rgb(INK_DIM)).child(f.chord)) |
| 1854 | .into_any_element(), |
| 1855 | ); |
| 1856 | } |
| 1857 | } |
| 1858 | Some( |
| 1859 | div() |
| 1860 | .absolute() |
| 1861 | .inset_0() |
| 1862 | .flex() |
| 1863 | .justify_center() |
| 1864 | .items_center() |
| 1865 | |
| 1866 | |
| 1867 | .bg(rgba(0x101010cc)) |
| 1868 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 1869 | cx.stop_propagation() |
| 1870 | }) |
| 1871 | .child( |
| 1872 | div() |
| 1873 | .w(px(KEYS_W)) |
| 1874 | .flex() |
| 1875 | .flex_col() |
| 1876 | .gap(px(2.)) |
| 1877 | .p(px(12.)) |
| 1878 | .rounded(px(6.)) |
| 1879 | .bg(rgb(SURFACE)) |
| 1880 | |
| 1881 | |
| 1882 | |
| 1883 | .child(div().flex_none().px(px(6.)).child("Keybindings")) |
| 1884 | |
| 1885 | |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | .child( |
| 1893 | div() |
| 1894 | .flex_none() |
| 1895 | .px(px(6.)) |
| 1896 | .text_size(px(11.)) |
| 1897 | .text_color(rgb(INK_DIM)) |
| 1898 | .child( |
| 1899 | self.notice |
| 1900 | .clone() |
| 1901 | .unwrap_or_else(|| "click a row, then press a key".into()), |
| 1902 | ), |
| 1903 | ) |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | .child( |
| 1908 | div() |
| 1909 | .id("keys-rows") |
| 1910 | .flex() |
| 1911 | .flex_col() |
| 1912 | .gap(px(2.)) |
| 1913 | .max_h(px(KEYS_ROWS_H)) |
| 1914 | .overflow_y_scroll() |
| 1915 | .children(rows), |
| 1916 | ), |
| 1917 | ), |
| 1918 | ) |
| 1919 | } |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | |
| 1924 | |
| 1925 | |
| 1926 | |
| 1927 | fn export_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 1928 | if !self.export_open { |
| 1929 | return None; |
| 1930 | } |
| 1931 | let row = |id: (&'static str, usize)| { |
| 1932 | div() |
| 1933 | .id(id) |
| 1934 | .flex() |
| 1935 | |
| 1936 | |
| 1937 | .min_h(px(KEYS_ROW_H)) |
| 1938 | .items_center() |
| 1939 | .justify_between() |
| 1940 | .gap(px(12.)) |
| 1941 | .px(px(6.)) |
| 1942 | .rounded(px(3.)) |
| 1943 | .cursor_pointer() |
| 1944 | .hover(|s| s.bg(rgb(HOVER))) |
| 1945 | }; |
| 1946 | let rows: Vec<_> = Quality::ALL |
| 1947 | .into_iter() |
| 1948 | .enumerate() |
| 1949 | .map(|(i, quality)| { |
| 1950 | row(("quality", i)) |
| 1951 | .when(self.quality == quality, |d| d.bg(rgb(SELECTED))) |
| 1952 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 1953 | this.quality = quality; |
| 1954 | cx.notify(); |
| 1955 | })) |
| 1956 | .child(quality.label()) |
| 1957 | .child( |
| 1958 | div() |
| 1959 | .text_size(px(11.)) |
| 1960 | .text_color(rgb(INK_DIM)) |
| 1961 | .child(quality.detail(self.custom_mbps)), |
| 1962 | ) |
| 1963 | }) |
| 1964 | .collect(); |
| 1965 | Some( |
| 1966 | div() |
| 1967 | .absolute() |
| 1968 | .inset_0() |
| 1969 | .flex() |
| 1970 | .justify_center() |
| 1971 | .items_center() |
| 1972 | .bg(rgba(0x101010cc)) |
| 1973 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 1974 | cx.stop_propagation() |
| 1975 | }) |
| 1976 | .child( |
| 1977 | div() |
| 1978 | .w(px(KEYS_W)) |
| 1979 | .flex() |
| 1980 | .flex_col() |
| 1981 | .gap(px(2.)) |
| 1982 | .p(px(12.)) |
| 1983 | .rounded(px(6.)) |
| 1984 | .bg(rgb(SURFACE)) |
| 1985 | .child(div().flex_none().px(px(6.)).child("Export")) |
| 1986 | |
| 1987 | |
| 1988 | |
| 1989 | .child( |
| 1990 | div() |
| 1991 | .flex_none() |
| 1992 | .px(px(6.)) |
| 1993 | .text_size(px(11.)) |
| 1994 | .text_color(rgb(INK_DIM)) |
| 1995 | .child(self.notice.clone().unwrap_or_else(|| { |
| 1996 | "pick a quality, then Export — esc closes".into() |
| 1997 | })), |
| 1998 | ) |
| 1999 | .child( |
| 2000 | row(("destination", 0)) |
| 2001 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2002 | this.pick_destination(cx) |
| 2003 | })) |
| 2004 | .child("Destination") |
| 2005 | .child( |
| 2006 | div() |
| 2007 | .text_size(px(11.)) |
| 2008 | .text_color(rgb(INK_DIM)) |
| 2009 | .child(file_name(&self.export_path)), |
| 2010 | ), |
| 2011 | ) |
| 2012 | .children(rows) |
| 2013 | |
| 2014 | |
| 2015 | |
| 2016 | |
| 2017 | .child( |
| 2018 | div() |
| 2019 | .flex_none() |
| 2020 | .px(px(6.)) |
| 2021 | .text_size(px(11.)) |
| 2022 | .text_color(rgb(INK_DIM)) |
| 2023 | .child("H.264 · MP4 · moov at end"), |
| 2024 | ) |
| 2025 | .child( |
| 2026 | div() |
| 2027 | .id("export-confirm") |
| 2028 | .mt(px(4.)) |
| 2029 | .flex() |
| 2030 | .h(px(CONTROL_H)) |
| 2031 | .items_center() |
| 2032 | .justify_center() |
| 2033 | .rounded(px(3.)) |
| 2034 | .bg(rgb(SELECTED)) |
| 2035 | .cursor_pointer() |
| 2036 | .hover(|s| s.bg(rgb(HOVER))) |
| 2037 | .on_click( |
| 2038 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2039 | this.start_export(cx) |
| 2040 | }), |
| 2041 | ) |
| 2042 | .child("Export"), |
| 2043 | ), |
| 2044 | ), |
| 2045 | ) |
| 2046 | } |
| 2047 | |
| 2048 | |
| 2049 | |
| 2050 | |
| 2051 | |
| 2052 | |
| 2053 | |
| 2054 | |
| 2055 | |
| 2056 | |
| 2057 | |
| 2058 | fn context_card( |
| 2059 | &self, |
| 2060 | viewport: Size<Pixels>, |
| 2061 | cx: &mut Context<Self>, |
| 2062 | ) -> Option<impl IntoElement> { |
| 2063 | let menu = self.context_menu?; |
| 2064 | let session = self.session.as_ref()?; |
| 2065 | let clip = *session.lane_clips(menu.lane).get(menu.idx)?; |
| 2066 | let playhead = frame_at(session.now(), self.fps); |
| 2067 | let source = session.sources().get(clip.source).cloned()?; |
| 2068 | let secs = |frames: u32| timecode(f64::from(frames) / self.fps, self.fps); |
| 2069 | let row = |n: usize| { |
| 2070 | div() |
| 2071 | .id(("menu", n)) |
| 2072 | .flex() |
| 2073 | |
| 2074 | |
| 2075 | .min_h(px(MENU_ROW_H)) |
| 2076 | .items_center() |
| 2077 | .justify_between() |
| 2078 | .gap(px(12.)) |
| 2079 | .px(px(6.)) |
| 2080 | .rounded(px(3.)) |
| 2081 | }; |
| 2082 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 2083 | if menu.details { |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | for (label, value) in [ |
| 2088 | ("File", file_name(&source.path)), |
| 2089 | ("Path", source.path.display().to_string()), |
| 2090 | ( |
| 2091 | "Source range", |
| 2092 | format!("{} – {}", secs(clip.in_frame), secs(clip.out_frame)), |
| 2093 | ), |
| 2094 | ("This clip", secs(clip.len())), |
| 2095 | ( |
| 2096 | "Source duration", |
| 2097 | secs(source_frames( |
| 2098 | lane_clips(session), |
| 2099 | session.sources(), |
| 2100 | &source.path, |
| 2101 | )), |
| 2102 | ), |
| 2103 | ] { |
| 2104 | rows.push( |
| 2105 | row(rows.len()) |
| 2106 | .child(label) |
| 2107 | .child( |
| 2108 | div() |
| 2109 | .min_w(px(0.)) |
| 2110 | .truncate() |
| 2111 | |
| 2112 | |
| 2113 | |
| 2114 | .text_size(px(11.)) |
| 2115 | .text_color(rgb(INK_DIM)) |
| 2116 | .child(value), |
| 2117 | ) |
| 2118 | .into_any_element(), |
| 2119 | ); |
| 2120 | } |
| 2121 | } else { |
| 2122 | for action in MENU_ITEMS { |
| 2123 | let enabled = applicable(&clip, action, playhead); |
| 2124 | |
| 2125 | |
| 2126 | |
| 2127 | let label = if action == ActionId::ToggleMute { |
| 2128 | format!("{} (global)", action.label()) |
| 2129 | } else { |
| 2130 | action.label().to_string() |
| 2131 | }; |
| 2132 | rows.push( |
| 2133 | row(rows.len()) |
| 2134 | .child(label) |
| 2135 | .child( |
| 2136 | div() |
| 2137 | .text_color(rgb(INK_DIM)) |
| 2138 | .child(self.keymap.display(action)), |
| 2139 | ) |
| 2140 | .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed()) |
| 2141 | .when(enabled, |d| { |
| 2142 | d.cursor_pointer() |
| 2143 | .hover(|s| s.bg(rgb(HOVER))) |
| 2144 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 2145 | |
| 2146 | |
| 2147 | this.context_menu = None; |
| 2148 | this.act(action, cx); |
| 2149 | })) |
| 2150 | }) |
| 2151 | .into_any_element(), |
| 2152 | ); |
| 2153 | } |
| 2154 | rows.push( |
| 2155 | row(rows.len()) |
| 2156 | .cursor_pointer() |
| 2157 | .hover(|s| s.bg(rgb(HOVER))) |
| 2158 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2159 | if let Some(menu) = &mut this.context_menu { |
| 2160 | menu.details = true; |
| 2161 | } |
| 2162 | cx.notify(); |
| 2163 | })) |
| 2164 | .child("Properties") |
| 2165 | |
| 2166 | |
| 2167 | .child(div().text_color(rgb(INK_DIM)).child("…")) |
| 2168 | .into_any_element(), |
| 2169 | ); |
| 2170 | } |
| 2171 | let (x, y) = menu_at( |
| 2172 | menu.at, |
| 2173 | viewport, |
| 2174 | MENU_PAD * 2. + rows.len() as f32 * MENU_ROW_H, |
| 2175 | ); |
| 2176 | let full: SharedString = source.path.display().to_string().into(); |
| 2177 | Some( |
| 2178 | div() |
| 2179 | .absolute() |
| 2180 | .inset_0() |
| 2181 | |
| 2182 | |
| 2183 | |
| 2184 | |
| 2185 | .on_mouse_down( |
| 2186 | MouseButton::Left, |
| 2187 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 2188 | this.context_menu = None; |
| 2189 | cx.notify(); |
| 2190 | cx.stop_propagation(); |
| 2191 | }), |
| 2192 | ) |
| 2193 | .on_mouse_down( |
| 2194 | MouseButton::Right, |
| 2195 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 2196 | this.context_menu = None; |
| 2197 | cx.notify(); |
| 2198 | cx.stop_propagation(); |
| 2199 | }), |
| 2200 | ) |
| 2201 | .child( |
| 2202 | div() |
| 2203 | |
| 2204 | |
| 2205 | .id("menu-card") |
| 2206 | .absolute() |
| 2207 | .left(px(x)) |
| 2208 | .top(px(y)) |
| 2209 | .w(px(MENU_W)) |
| 2210 | .flex() |
| 2211 | .flex_col() |
| 2212 | .p(px(MENU_PAD)) |
| 2213 | .rounded(px(6.)) |
| 2214 | .bg(rgb(SURFACE)) |
| 2215 | |
| 2216 | |
| 2217 | |
| 2218 | |
| 2219 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 2220 | cx.stop_propagation() |
| 2221 | }) |
| 2222 | .on_mouse_down(MouseButton::Right, |_: &MouseDownEvent, _, cx| { |
| 2223 | cx.stop_propagation() |
| 2224 | }) |
| 2225 | |
| 2226 | .when(menu.details, |d| { |
| 2227 | d.tooltip(move |_, cx| cx.new(|_| Tip(full.clone())).into()) |
| 2228 | }) |
| 2229 | .children(rows), |
| 2230 | ), |
| 2231 | ) |
| 2232 | } |
| 2233 | |
| 2234 | |
| 2235 | |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | fn lane_row( |
| 2242 | &self, |
| 2243 | lane: Lane, |
| 2244 | name: &'static str, |
| 2245 | duration: f64, |
| 2246 | filled: f32, |
| 2247 | cx: &mut Context<Self>, |
| 2248 | ) -> impl IntoElement { |
| 2249 | |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | let bed_w = f32::from(self.ruler.get().size.width); |
| 2254 | let (clips, others) = match &self.session { |
| 2255 | Some(session) => ( |
| 2256 | session.lane_clips(lane), |
| 2257 | session.lane_clips(match lane { |
| 2258 | Lane::Video => Lane::Audio, |
| 2259 | Lane::Audio => Lane::Video, |
| 2260 | }), |
| 2261 | ), |
| 2262 | None => (&[][..], &[][..]), |
| 2263 | }; |
| 2264 | let sources = self |
| 2265 | .session |
| 2266 | .as_ref() |
| 2267 | .map_or(&[][..], PlaybackSession::sources); |
| 2268 | let (sel, sel_link) = (self.selected, self.selected_link()); |
| 2269 | let audio = lane == Lane::Audio; |
| 2270 | let tip: SharedString = format!( |
| 2271 | "Select — {} removes the take, {} leaves a gap, {} rejoins a cut", |
| 2272 | self.keymap.display(ActionId::Delete), |
| 2273 | self.keymap.display(ActionId::Lift), |
| 2274 | self.keymap.display(ActionId::Regroup) |
| 2275 | ) |
| 2276 | .into(); |
| 2277 | div() |
| 2278 | .flex_none() |
| 2279 | .h(px(LANE_H)) |
| 2280 | .flex() |
| 2281 | .gap(px(HEADER_GAP)) |
| 2282 | |
| 2283 | |
| 2284 | .child( |
| 2285 | div() |
| 2286 | .flex_none() |
| 2287 | .w(px(HEADER_W)) |
| 2288 | .h_full() |
| 2289 | .flex() |
| 2290 | .items_center() |
| 2291 | .justify_center() |
| 2292 | .rounded(px(3.)) |
| 2293 | .bg(rgb(SURFACE)) |
| 2294 | .text_size(px(11.)) |
| 2295 | .text_color(rgb(INK_DIM)) |
| 2296 | .child(name), |
| 2297 | ) |
| 2298 | .child( |
| 2299 | |
| 2300 | |
| 2301 | |
| 2302 | div() |
| 2303 | .relative() |
| 2304 | .flex_1() |
| 2305 | .min_w(px(0.)) |
| 2306 | .h_full() |
| 2307 | .rounded(px(3.)) |
| 2308 | .bg(rgb(LETTERBOX)) |
| 2309 | .overflow_hidden() |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | |
| 2314 | .on_drop(cx.listener(|this, drag: &AssetDrag, _, cx| { |
| 2315 | this.insert_source(&drag.0.clone(), drag.1, cx) |
| 2316 | })) |
| 2317 | .drag_over::<AssetDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM))) |
| 2318 | .children(clips.iter().enumerate().map(|(i, clip)| { |
| 2319 | let (start, len) = ( |
| 2320 | f64::from(clip.start) / self.fps, |
| 2321 | f64::from(clip.len()) / self.fps, |
| 2322 | ); |
| 2323 | let on = marked((lane, i), clip.link, sel, sel_link); |
| 2324 | |
| 2325 | |
| 2326 | |
| 2327 | let grouped = |
| 2328 | clip.link.is_some() && others.iter().any(|o| o.link == clip.link); |
| 2329 | |
| 2330 | |
| 2331 | |
| 2332 | let tint = source_tint( |
| 2333 | sources |
| 2334 | .get(clip.source) |
| 2335 | .and_then(|s| sources.iter().position(|o| o.path == s.path)) |
| 2336 | .unwrap_or(clip.source), |
| 2337 | ); |
| 2338 | let width = width_frac(len, duration); |
| 2339 | let label = sources.get(clip.source).map(|s| file_name(&s.path)); |
| 2340 | let wave = sources |
| 2341 | .get(clip.source) |
| 2342 | .and_then(|s| self.waves.get(&(s.path.clone(), s.audio_stream))) |
| 2343 | .cloned(); |
| 2344 | let (from, to) = ( |
| 2345 | f64::from(clip.in_frame) / self.fps, |
| 2346 | f64::from(clip.out_frame) / self.fps, |
| 2347 | ); |
| 2348 | let tip = tip.clone(); |
| 2349 | div() |
| 2350 | .id((if audio { "aclip" } else { "vclip" }, i)) |
| 2351 | .absolute() |
| 2352 | .top_0() |
| 2353 | .h_full() |
| 2354 | .left(relative(start_frac(start, duration))) |
| 2355 | .w(relative(width)) |
| 2356 | .overflow_hidden() |
| 2357 | .rounded(px(3.)) |
| 2358 | .border_1() |
| 2359 | .border_color(rgb(if on { |
| 2360 | ACCENT |
| 2361 | } else if grouped { |
| 2362 | tint |
| 2363 | } else { |
| 2364 | INK_DIM |
| 2365 | })) |
| 2366 | .bg(rgb(if on { SELECTED } else { tint })) |
| 2367 | .cursor_pointer() |
| 2368 | .hover(|s| s.border_color(rgb(ACCENT))) |
| 2369 | .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) |
| 2370 | .on_mouse_down( |
| 2371 | MouseButton::Left, |
| 2372 | cx.listener(move |this, _: &MouseDownEvent, _, cx| { |
| 2373 | this.selected = Some((lane, i)); |
| 2374 | cx.notify(); |
| 2375 | }), |
| 2376 | ) |
| 2377 | |
| 2378 | |
| 2379 | |
| 2380 | |
| 2381 | .on_mouse_down( |
| 2382 | MouseButton::Right, |
| 2383 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { |
| 2384 | if this.modal() { |
| 2385 | return; |
| 2386 | } |
| 2387 | this.selected = Some((lane, i)); |
| 2388 | this.context_menu = Some(ContextMenu { |
| 2389 | lane, |
| 2390 | idx: i, |
| 2391 | at: event.position, |
| 2392 | details: false, |
| 2393 | }); |
| 2394 | cx.notify(); |
| 2395 | }), |
| 2396 | ) |
| 2397 | |
| 2398 | .children(wave.filter(|_| audio).and_then(|wave| { |
| 2399 | let inner: AnyElement = match wave { |
| 2400 | Wave::Peaks(peaks) => { |
| 2401 | waveform(peaks, from, to).into_any_element() |
| 2402 | } |
| 2403 | |
| 2404 | |
| 2405 | |
| 2406 | |
| 2407 | |
| 2408 | Wave::Loading => div() |
| 2409 | .h_full() |
| 2410 | .flex() |
| 2411 | .items_center() |
| 2412 | .child(div().w_full().h(px(1.)).bg(rgb(HOVER))) |
| 2413 | .into_any_element(), |
| 2414 | |
| 2415 | Wave::Silent => return None, |
| 2416 | }; |
| 2417 | Some( |
| 2418 | div() |
| 2419 | .absolute() |
| 2420 | .left_0() |
| 2421 | .right_0() |
| 2422 | .top(px(LABEL_H)) |
| 2423 | .bottom_0() |
| 2424 | .child(inner), |
| 2425 | ) |
| 2426 | })) |
| 2427 | .when_some(label.filter(|_| show_label(bed_w * width)), |d, label| { |
| 2428 | d.child( |
| 2429 | div() |
| 2430 | .relative() |
| 2431 | .h(px(LABEL_H)) |
| 2432 | .px(px(4.)) |
| 2433 | .truncate() |
| 2434 | .text_size(px(10.)) |
| 2435 | .child(label), |
| 2436 | ) |
| 2437 | }) |
| 2438 | })) |
| 2439 | |
| 2440 | |
| 2441 | .child( |
| 2442 | div() |
| 2443 | .absolute() |
| 2444 | .top_0() |
| 2445 | .h_full() |
| 2446 | .left(relative(filled)) |
| 2447 | .w(px(1.)) |
| 2448 | .bg(rgb(ACCENT)), |
| 2449 | ), |
| 2450 | ) |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | |
| 2455 | |
| 2456 | |
| 2457 | const SCRUB_GAP: Duration = Duration::from_millis(100); |
| 2458 | |
| 2459 | fn scrub_due(target: u32, last_target: u32, since: Duration) -> bool { |
| 2460 | target != last_target && since >= SCRUB_GAP |
| 2461 | } |
| 2462 | |
| 2463 | |
| 2464 | |
| 2465 | |
| 2466 | |
| 2467 | |
| 2468 | |
| 2469 | fn run_picker(pickers: [(&str, Vec<String>); 2]) -> Option<Option<PathBuf>> { |
| 2470 | for (bin, args) in pickers { |
| 2471 | |
| 2472 | |
| 2473 | let Ok(out) = std::process::Command::new(bin).args(args).output() else { |
| 2474 | continue; |
| 2475 | }; |
| 2476 | let path = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 2477 | return Some((!path.is_empty()).then(|| PathBuf::from(path))); |
| 2478 | } |
| 2479 | None |
| 2480 | } |
| 2481 | |
| 2482 | fn pick_file() -> Result<Option<PathBuf>, &'static str> { |
| 2483 | run_picker([ |
| 2484 | ( |
| 2485 | "zenity", |
| 2486 | vec!["--file-selection".into(), "--title=edith — import".into()], |
| 2487 | ), |
| 2488 | ("kdialog", vec!["--getopenfilename".into()]), |
| 2489 | ]) |
| 2490 | .ok_or( |
| 2491 | "NO FILE CHOOSER — install zenity or kdialog, or drag the file onto this window to import it", |
| 2492 | ) |
| 2493 | } |
| 2494 | |
| 2495 | |
| 2496 | |
| 2497 | |
| 2498 | fn pick_save(default: &std::path::Path) -> Result<Option<PathBuf>, &'static str> { |
| 2499 | let default = default.to_string_lossy().into_owned(); |
| 2500 | run_picker([ |
| 2501 | ( |
| 2502 | "zenity", |
| 2503 | vec![ |
| 2504 | "--file-selection".into(), |
| 2505 | |
| 2506 | |
| 2507 | "--save".into(), |
| 2508 | "--title=edith — export to".into(), |
| 2509 | format!("--filename={default}"), |
| 2510 | ], |
| 2511 | ), |
| 2512 | ("kdialog", vec!["--getsavefilename".into(), default]), |
| 2513 | ]) |
| 2514 | .ok_or( |
| 2515 | "NO FILE CHOOSER — install zenity or kdialog to choose where; exporting beside the source", |
| 2516 | ) |
| 2517 | } |
| 2518 | |
| 2519 | |
| 2520 | |
| 2521 | fn export_path(source: impl Into<PathBuf>) -> PathBuf { |
| 2522 | let mut path = source.into(); |
| 2523 | path.set_extension("export.mp4"); |
| 2524 | path |
| 2525 | } |
| 2526 | |
| 2527 | |
| 2528 | |
| 2529 | |
| 2530 | |
| 2531 | fn project_path(source: impl Into<PathBuf>) -> PathBuf { |
| 2532 | let mut path = source.into(); |
| 2533 | path.set_extension("edith"); |
| 2534 | path |
| 2535 | } |
| 2536 | |
| 2537 | |
| 2538 | |
| 2539 | |
| 2540 | fn is_project(path: &std::path::Path) -> bool { |
| 2541 | path.extension().is_some_and(|e| e == "edith") |
| 2542 | } |
| 2543 | |
| 2544 | |
| 2545 | |
| 2546 | fn file_name(path: &std::path::Path) -> String { |
| 2547 | path.file_name().map_or_else( |
| 2548 | || path.display().to_string(), |
| 2549 | |n| n.to_string_lossy().into(), |
| 2550 | ) |
| 2551 | } |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | |
| 2556 | fn audio_notice(session: &PlaybackSession) -> Option<String> { |
| 2557 | session |
| 2558 | .audio_disabled_reason() |
| 2559 | .map(|reason| format!(" — NO AUDIO: {reason}")) |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | |
| 2564 | |
| 2565 | fn unseen_sources( |
| 2566 | sources: &[Source], |
| 2567 | waves: &HashMap<(PathBuf, usize), Wave>, |
| 2568 | ) -> Vec<(PathBuf, usize)> { |
| 2569 | sources |
| 2570 | .iter() |
| 2571 | .map(|s| (s.path.clone(), s.audio_stream)) |
| 2572 | .filter(|key| !waves.contains_key(key)) |
| 2573 | .collect() |
| 2574 | } |
| 2575 | |
| 2576 | |
| 2577 | |
| 2578 | fn unseen_paths(sources: &[Source], streams: &HashMap<PathBuf, Vec<StreamInfo>>) -> Vec<PathBuf> { |
| 2579 | let mut out: Vec<PathBuf> = Vec::new(); |
| 2580 | for s in sources { |
| 2581 | if !streams.contains_key(&s.path) && !out.contains(&s.path) { |
| 2582 | out.push(s.path.clone()); |
| 2583 | } |
| 2584 | } |
| 2585 | out |
| 2586 | } |
| 2587 | |
| 2588 | |
| 2589 | fn lane_clips(session: &PlaybackSession) -> impl Iterator<Item = &Clip> { |
| 2590 | session |
| 2591 | .lane_clips(Lane::Video) |
| 2592 | .iter() |
| 2593 | .chain(session.lane_clips(Lane::Audio)) |
| 2594 | } |
| 2595 | |
| 2596 | |
| 2597 | |
| 2598 | |
| 2599 | |
| 2600 | |
| 2601 | |
| 2602 | |
| 2603 | |
| 2604 | |
| 2605 | |
| 2606 | |
| 2607 | |
| 2608 | |
| 2609 | fn source_frames<'a>( |
| 2610 | clips: impl Iterator<Item = &'a Clip>, |
| 2611 | sources: &[Source], |
| 2612 | path: &Path, |
| 2613 | ) -> u32 { |
| 2614 | clips |
| 2615 | .filter(|clip| sources.get(clip.source).is_some_and(|s| s.path == path)) |
| 2616 | .map(|clip| clip.out_frame) |
| 2617 | .max() |
| 2618 | .unwrap_or(0) |
| 2619 | } |
| 2620 | |
| 2621 | |
| 2622 | |
| 2623 | |
| 2624 | |
| 2625 | |
| 2626 | fn frame_at(secs: f64, fps: f64) -> u32 { |
| 2627 | (secs * fps + 1e-6).floor().max(0.) as u32 |
| 2628 | } |
| 2629 | |
| 2630 | |
| 2631 | |
| 2632 | |
| 2633 | fn applicable(clip: &Clip, action: ActionId, playhead: u32) -> bool { |
| 2634 | match action { |
| 2635 | |
| 2636 | |
| 2637 | ActionId::Cut => clip.start < playhead && playhead < clip.end(), |
| 2638 | |
| 2639 | |
| 2640 | |
| 2641 | ActionId::Regroup => playhead == clip.start || playhead == clip.end(), |
| 2642 | _ => true, |
| 2643 | } |
| 2644 | } |
| 2645 | |
| 2646 | |
| 2647 | |
| 2648 | |
| 2649 | |
| 2650 | fn menu_at(at: Point<Pixels>, viewport: Size<Pixels>, height: f32) -> (f32, f32) { |
| 2651 | let fit = |v: f32, size: f32, room: f32| v.min(room - size).max(0.); |
| 2652 | ( |
| 2653 | fit(f32::from(at.x), MENU_W, f32::from(viewport.width)), |
| 2654 | fit(f32::from(at.y), height, f32::from(viewport.height)), |
| 2655 | ) |
| 2656 | } |
| 2657 | |
| 2658 | |
| 2659 | |
| 2660 | |
| 2661 | fn can_add(picked: Option<&(PathBuf, usize)>, timeline: bool, exporting: bool) -> bool { |
| 2662 | picked.is_some() && timeline && !exporting |
| 2663 | } |
| 2664 | |
| 2665 | |
| 2666 | |
| 2667 | #[derive(Debug, PartialEq)] |
| 2668 | struct Row { |
| 2669 | path: PathBuf, |
| 2670 | stream: usize, |
| 2671 | |
| 2672 | name: String, |
| 2673 | |
| 2674 | detail: String, |
| 2675 | |
| 2676 | |
| 2677 | unusable: Option<String>, |
| 2678 | |
| 2679 | frames: u32, |
| 2680 | |
| 2681 | |
| 2682 | tint: usize, |
| 2683 | } |
| 2684 | |
| 2685 | |
| 2686 | |
| 2687 | |
| 2688 | |
| 2689 | |
| 2690 | |
| 2691 | |
| 2692 | |
| 2693 | |
| 2694 | |
| 2695 | fn library_rows( |
| 2696 | sources: &[Source], |
| 2697 | streams: &HashMap<PathBuf, Vec<StreamInfo>>, |
| 2698 | timeline_audio: Option<(u32, u16)>, |
| 2699 | frames: impl Fn(&Path) -> u32, |
| 2700 | ) -> Vec<Row> { |
| 2701 | let mut rows = Vec::new(); |
| 2702 | for (i, source) in sources.iter().enumerate() { |
| 2703 | let of_file = streams.get(&source.path).map_or(&[][..], Vec::as_slice); |
| 2704 | let info = of_file.iter().find(|s| s.index == source.audio_stream); |
| 2705 | let tint = sources |
| 2706 | .iter() |
| 2707 | .position(|s| s.path == source.path) |
| 2708 | .expect("a source finds itself"); |
| 2709 | rows.push(Row { |
| 2710 | path: source.path.clone(), |
| 2711 | stream: source.audio_stream, |
| 2712 | name: row_name(&source.path, source.audio_stream, of_file.len() > 1), |
| 2713 | |
| 2714 | |
| 2715 | |
| 2716 | detail: info |
| 2717 | .filter(|_| of_file.len() > 1) |
| 2718 | .map_or_else(String::new, stream_detail), |
| 2719 | |
| 2720 | |
| 2721 | unusable: None, |
| 2722 | frames: frames(&source.path), |
| 2723 | tint, |
| 2724 | }); |
| 2725 | |
| 2726 | |
| 2727 | if sources[i + 1..].iter().any(|s| s.path == source.path) { |
| 2728 | continue; |
| 2729 | } |
| 2730 | for info in of_file { |
| 2731 | if sources |
| 2732 | .iter() |
| 2733 | .any(|s| s.path == source.path && s.audio_stream == info.index) |
| 2734 | { |
| 2735 | continue; |
| 2736 | } |
| 2737 | rows.push(Row { |
| 2738 | path: source.path.clone(), |
| 2739 | stream: info.index, |
| 2740 | name: row_name(&source.path, info.index, true), |
| 2741 | detail: stream_detail(info), |
| 2742 | unusable: unusable(info, timeline_audio), |
| 2743 | frames: frames(&source.path), |
| 2744 | tint, |
| 2745 | }); |
| 2746 | } |
| 2747 | } |
| 2748 | rows |
| 2749 | } |
| 2750 | |
| 2751 | |
| 2752 | |
| 2753 | |
| 2754 | fn join_detail(detail: &str, tail: &str) -> String { |
| 2755 | match detail.is_empty() { |
| 2756 | true => tail.to_string(), |
| 2757 | false => format!("{detail} · {tail}"), |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | |
| 2762 | |
| 2763 | |
| 2764 | fn row_name(path: &Path, stream: usize, several: bool) -> String { |
| 2765 | match several { |
| 2766 | false => file_name(path), |
| 2767 | true => format!("{} [audio {}]", file_name(path), stream + 1), |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | |
| 2772 | |
| 2773 | |
| 2774 | |
| 2775 | fn stream_detail(info: &StreamInfo) -> String { |
| 2776 | let mut parts: Vec<String> = Vec::new(); |
| 2777 | if let Some(lang) = &info.lang { |
| 2778 | parts.push(lang.clone()); |
| 2779 | } |
| 2780 | if info.sample_rate > 0 { |
| 2781 | parts.push(format!("{} kHz", f64::from(info.sample_rate) / 1000.)); |
| 2782 | } |
| 2783 | parts.extend(layout(info.channels)); |
| 2784 | parts.join(" ") |
| 2785 | } |
| 2786 | |
| 2787 | fn layout(channels: u16) -> Option<String> { |
| 2788 | match channels { |
| 2789 | 0 => None, |
| 2790 | 1 => Some("mono".to_string()), |
| 2791 | 2 => Some("stereo".to_string()), |
| 2792 | n => Some(format!("{n} ch")), |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | |
| 2797 | |
| 2798 | |
| 2799 | |
| 2800 | fn unusable(info: &StreamInfo, timeline_audio: Option<(u32, u16)>) -> Option<String> { |
| 2801 | if !info.decodable { |
| 2802 | |
| 2803 | |
| 2804 | |
| 2805 | return Some(match info.codec.as_str() { |
| 2806 | "unknown" => "unsupported codec".to_string(), |
| 2807 | codec => format!("{codec} is not supported"), |
| 2808 | }); |
| 2809 | } |
| 2810 | let (rate, channels) = timeline_audio?; |
| 2811 | ((info.sample_rate, info.channels) != (rate, channels)).then(|| { |
| 2812 | format!( |
| 2813 | "the timeline is {} kHz {}", |
| 2814 | f64::from(rate) / 1000., |
| 2815 | layout(channels).unwrap_or_else(|| "silent".to_string()) |
| 2816 | ) |
| 2817 | }) |
| 2818 | } |
| 2819 | |
| 2820 | |
| 2821 | |
| 2822 | |
| 2823 | |
| 2824 | fn library_w(window_w: f32) -> f32 { |
| 2825 | (window_w * LIBRARY_FRAC) |
| 2826 | .clamp(LIBRARY_MIN_W, LIBRARY_MAX_W) |
| 2827 | .min(window_w / 3.) |
| 2828 | } |
| 2829 | |
| 2830 | |
| 2831 | |
| 2832 | |
| 2833 | fn window_title(name: &str) -> String { |
| 2834 | if name == NO_FILE { |
| 2835 | "edith".to_string() |
| 2836 | } else { |
| 2837 | format!("{name} — edith") |
| 2838 | } |
| 2839 | } |
| 2840 | |
| 2841 | |
| 2842 | |
| 2843 | |
| 2844 | fn source_tint(source: usize) -> u32 { |
| 2845 | SOURCE_TINTS[source % SOURCE_TINTS.len()] |
| 2846 | } |
| 2847 | |
| 2848 | |
| 2849 | |
| 2850 | |
| 2851 | |
| 2852 | #[derive(Clone, Copy, PartialEq, Debug)] |
| 2853 | enum Quality { |
| 2854 | Auto, |
| 2855 | Low, |
| 2856 | Medium, |
| 2857 | High, |
| 2858 | Custom, |
| 2859 | } |
| 2860 | |
| 2861 | impl Quality { |
| 2862 | const ALL: [Quality; 5] = [ |
| 2863 | Quality::Auto, |
| 2864 | Quality::Low, |
| 2865 | Quality::Medium, |
| 2866 | Quality::High, |
| 2867 | Quality::Custom, |
| 2868 | ]; |
| 2869 | |
| 2870 | fn label(self) -> &'static str { |
| 2871 | match self { |
| 2872 | Quality::Auto => "Auto", |
| 2873 | Quality::Low => "Low", |
| 2874 | Quality::Medium => "Medium", |
| 2875 | Quality::High => "High", |
| 2876 | Quality::Custom => "Custom", |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | |
| 2881 | fn detail(self, custom_mbps: u32) -> String { |
| 2882 | match self { |
| 2883 | Quality::Auto => "from the picture size and frame rate".to_string(), |
| 2884 | Quality::Custom => format!("{custom_mbps} Mbps — type a number, 1–20"), |
| 2885 | other => format!( |
| 2886 | "{} Mbps", |
| 2887 | export_settings(other, 0).bitrate.unwrap_or_default() / 1_000_000 |
| 2888 | ), |
| 2889 | } |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | |
| 2894 | |
| 2895 | |
| 2896 | |
| 2897 | |
| 2898 | fn export_settings(quality: Quality, custom_mbps: u32) -> ExportSettings { |
| 2899 | ExportSettings { |
| 2900 | bitrate: match quality { |
| 2901 | Quality::Auto => None, |
| 2902 | Quality::Low => Some(2_000_000), |
| 2903 | Quality::Medium => Some(6_000_000), |
| 2904 | Quality::High => Some(12_000_000), |
| 2905 | Quality::Custom => Some(u64::from(custom_mbps) * 1_000_000), |
| 2906 | }, |
| 2907 | |
| 2908 | |
| 2909 | |
| 2910 | force_sw: false, |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | |
| 2915 | |
| 2916 | |
| 2917 | |
| 2918 | fn push_digit(mbps: u32, digit: u32) -> u32 { |
| 2919 | let next = mbps * 10 + digit; |
| 2920 | if next <= 99 { next } else { mbps } |
| 2921 | } |
| 2922 | |
| 2923 | |
| 2924 | |
| 2925 | |
| 2926 | |
| 2927 | |
| 2928 | |
| 2929 | fn cancels_export(key: &str, action: Option<ActionId>) -> bool { |
| 2930 | key == ESCAPE || action == Some(ActionId::CancelExport) |
| 2931 | } |
| 2932 | |
| 2933 | |
| 2934 | |
| 2935 | |
| 2936 | |
| 2937 | fn is_bare_modifier(key: &str) -> bool { |
| 2938 | matches!( |
| 2939 | key, |
| 2940 | "control" | "shift" | "alt" | "super" | "platform" | "function" | "fn" | "meta" | "command" |
| 2941 | ) |
| 2942 | } |
| 2943 | |
| 2944 | |
| 2945 | |
| 2946 | fn width_frac(len: f64, total: f64) -> f32 { |
| 2947 | if total > 0. { (len / total) as f32 } else { 1. } |
| 2948 | } |
| 2949 | |
| 2950 | |
| 2951 | |
| 2952 | |
| 2953 | fn start_frac(start: f64, total: f64) -> f32 { |
| 2954 | if total > 0. { |
| 2955 | (start / total).clamp(0., 1.) as f32 |
| 2956 | } else { |
| 2957 | 0. |
| 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | |
| 2962 | |
| 2963 | |
| 2964 | fn video_half(session: &PlaybackSession, audio: usize) -> Option<usize> { |
| 2965 | let link = session.lane_clips(Lane::Audio).get(audio)?.link?; |
| 2966 | session |
| 2967 | .lane_clips(Lane::Video) |
| 2968 | .iter() |
| 2969 | .position(|clip| clip.link == Some(link)) |
| 2970 | } |
| 2971 | |
| 2972 | |
| 2973 | |
| 2974 | |
| 2975 | |
| 2976 | fn marked( |
| 2977 | here: (Lane, usize), |
| 2978 | link: Option<u32>, |
| 2979 | sel: Option<(Lane, usize)>, |
| 2980 | sel_link: Option<u32>, |
| 2981 | ) -> bool { |
| 2982 | sel == Some(here) || (link.is_some() && link == sel_link) |
| 2983 | } |
| 2984 | |
| 2985 | |
| 2986 | fn show_label(w: f32) -> bool { |
| 2987 | w >= LABEL_MIN_W |
| 2988 | } |
| 2989 | |
| 2990 | |
| 2991 | |
| 2992 | |
| 2993 | |
| 2994 | fn normalise(mut peaks: Vec<(f32, f32)>) -> Vec<(f32, f32)> { |
| 2995 | let loudest = peaks.iter().fold(0f32, |m, &(lo, hi)| m.max(-lo).max(hi)); |
| 2996 | if loudest > 0. { |
| 2997 | for (lo, hi) in &mut peaks { |
| 2998 | *lo /= loudest; |
| 2999 | *hi /= loudest; |
| 3000 | } |
| 3001 | } |
| 3002 | peaks |
| 3003 | } |
| 3004 | |
| 3005 | |
| 3006 | |
| 3007 | |
| 3008 | |
| 3009 | |
| 3010 | fn envelope(peaks: &[(f32, f32)], from: f64, to: f64, w: f32, h: f32) -> Vec<(f32, f32, f32)> { |
| 3011 | if peaks.is_empty() || w <= 0. || h <= 0. { |
| 3012 | return Vec::new(); |
| 3013 | } |
| 3014 | let cols = (w / WAVE_COL).ceil().max(1.) as usize; |
| 3015 | let mid = h / 2.; |
| 3016 | (0..=cols) |
| 3017 | .map(|col| { |
| 3018 | let along = col as f64 / cols as f64; |
| 3019 | let at = from + (to - from) * along; |
| 3020 | |
| 3021 | |
| 3022 | let bucket = ((at * f64::from(WAVE_BPS)) as usize).min(peaks.len() - 1); |
| 3023 | let (lo, hi) = peaks[bucket]; |
| 3024 | let top = (mid - hi.clamp(0., 1.) * mid).min(mid - 0.5); |
| 3025 | let bottom = (mid - lo.clamp(-1., 0.) * mid).max(mid + 0.5); |
| 3026 | (w * along as f32, top.max(0.), bottom.min(h)) |
| 3027 | }) |
| 3028 | .collect() |
| 3029 | } |
| 3030 | |
| 3031 | |
| 3032 | |
| 3033 | |
| 3034 | fn waveform(peaks: Arc<Vec<(f32, f32)>>, from: f64, to: f64) -> impl IntoElement { |
| 3035 | canvas( |
| 3036 | |_, _, _| (), |
| 3037 | move |bounds, _, window, _| { |
| 3038 | let (o, s) = (bounds.origin, bounds.size); |
| 3039 | let cols = envelope(&peaks, from, to, f32::from(s.width), f32::from(s.height)); |
| 3040 | if cols.len() < 2 { |
| 3041 | return; |
| 3042 | } |
| 3043 | |
| 3044 | |
| 3045 | let mut points: Vec<Point<Pixels>> = cols |
| 3046 | .iter() |
| 3047 | .map(|&(x, top, _)| point(o.x + px(x), o.y + px(top))) |
| 3048 | .collect(); |
| 3049 | points.extend( |
| 3050 | cols.iter() |
| 3051 | .rev() |
| 3052 | .map(|&(x, _, bottom)| point(o.x + px(x), o.y + px(bottom))), |
| 3053 | ); |
| 3054 | let mut path = PathBuilder::fill(); |
| 3055 | path.add_polygon(&points, true); |
| 3056 | if let Ok(path) = path.build() { |
| 3057 | window.paint_path(path, rgb(INK_DIM)); |
| 3058 | } |
| 3059 | }, |
| 3060 | ) |
| 3061 | .size_full() |
| 3062 | } |
| 3063 | |
| 3064 | |
| 3065 | |
| 3066 | |
| 3067 | |
| 3068 | |
| 3069 | |
| 3070 | fn control( |
| 3071 | id: &'static str, |
| 3072 | glyph: Option<AnyElement>, |
| 3073 | |
| 3074 | label: impl Into<SharedString>, |
| 3075 | shortcut: String, |
| 3076 | enabled: bool, |
| 3077 | on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 3078 | ) -> impl IntoElement { |
| 3079 | let label = label.into(); |
| 3080 | let tip: SharedString = format!("{label} — {shortcut}").into(); |
| 3081 | div() |
| 3082 | .id(id) |
| 3083 | .flex_none() |
| 3084 | .h(px(CONTROL_H)) |
| 3085 | .flex() |
| 3086 | .items_center() |
| 3087 | .gap(px(6.)) |
| 3088 | .px(px(8.)) |
| 3089 | .rounded(px(3.)) |
| 3090 | .bg(rgb(SURFACE)) |
| 3091 | .children(glyph) |
| 3092 | .child(label) |
| 3093 | .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) |
| 3094 | .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed()) |
| 3095 | .when(enabled, |d| { |
| 3096 | d.cursor_pointer() |
| 3097 | .hover(|s| s.bg(rgb(HOVER))) |
| 3098 | .on_click(on_click) |
| 3099 | }) |
| 3100 | } |
| 3101 | |
| 3102 | |
| 3103 | fn separator() -> impl IntoElement { |
| 3104 | div() |
| 3105 | .flex_none() |
| 3106 | .mx(px(4.)) |
| 3107 | .w(px(1.)) |
| 3108 | .h(px(18.)) |
| 3109 | .bg(rgb(HOVER)) |
| 3110 | } |
| 3111 | |
| 3112 | |
| 3113 | |
| 3114 | |
| 3115 | struct Tip(SharedString); |
| 3116 | |
| 3117 | impl Render for Tip { |
| 3118 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 3119 | div() |
| 3120 | .px(px(6.)) |
| 3121 | .py(px(3.)) |
| 3122 | .rounded(px(3.)) |
| 3123 | .border_1() |
| 3124 | .border_color(rgb(SURFACE)) |
| 3125 | .bg(rgb(CHROME)) |
| 3126 | .text_color(rgb(INK)) |
| 3127 | .text_size(px(12.)) |
| 3128 | .child(self.0.clone()) |
| 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | |
| 3133 | |
| 3134 | |
| 3135 | fn cut_glyph() -> impl IntoElement { |
| 3136 | canvas( |
| 3137 | |_, _, _| (), |
| 3138 | |bounds, _, window, _| { |
| 3139 | let (o, s) = (bounds.origin, bounds.size); |
| 3140 | let mut path = PathBuilder::stroke(px(1.5)); |
| 3141 | path.move_to(point(o.x + s.width * 0.15, o.y + s.height)); |
| 3142 | path.line_to(point(o.x + s.width * 0.9, o.y + s.height * 0.1)); |
| 3143 | path.move_to(point(o.x + s.width * 0.85, o.y + s.height)); |
| 3144 | path.line_to(point(o.x + s.width * 0.1, o.y + s.height * 0.1)); |
| 3145 | if let Ok(path) = path.build() { |
| 3146 | window.paint_path(path, rgb(INK)); |
| 3147 | } |
| 3148 | }, |
| 3149 | ) |
| 3150 | .w(px(13.)) |
| 3151 | .h(px(13.)) |
| 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | fn delete_glyph() -> impl IntoElement { |
| 3156 | div() |
| 3157 | .flex() |
| 3158 | .flex_col() |
| 3159 | .items_center() |
| 3160 | .gap(px(2.)) |
| 3161 | .child(div().w(px(13.)).h(px(2.)).bg(rgb(INK))) |
| 3162 | .child(div().w(px(9.)).h(px(9.)).bg(rgb(INK))) |
| 3163 | } |
| 3164 | |
| 3165 | |
| 3166 | |
| 3167 | |
| 3168 | fn bounds_probe(into: Rc<Cell<Bounds<Pixels>>>) -> impl IntoElement { |
| 3169 | canvas(move |bounds, _, _| into.set(bounds), |_, _, _, _| ()) |
| 3170 | .absolute() |
| 3171 | .size_full() |
| 3172 | } |
| 3173 | |
| 3174 | |
| 3175 | |
| 3176 | fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 { |
| 3177 | if bounds.size.width <= px(0.) { |
| 3178 | return 0.; |
| 3179 | } |
| 3180 | ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) |
| 3181 | } |
| 3182 | |
| 3183 | |
| 3184 | |
| 3185 | |
| 3186 | fn empty_hint() -> impl IntoElement { |
| 3187 | div() |
| 3188 | .flex() |
| 3189 | .flex_col() |
| 3190 | .items_center() |
| 3191 | .gap(px(6.)) |
| 3192 | .text_color(rgb(INK_DIM)) |
| 3193 | .child("Drop a video or an .edith project here") |
| 3194 | .child( |
| 3195 | div() |
| 3196 | .text_size(px(11.)) |
| 3197 | .child("or click Import in the media list"), |
| 3198 | ) |
| 3199 | } |
| 3200 | |
| 3201 | |
| 3202 | |
| 3203 | fn transport_glyph(playing: bool) -> impl IntoElement { |
| 3204 | div() |
| 3205 | .flex() |
| 3206 | .items_center() |
| 3207 | .gap(px(4.)) |
| 3208 | .when(playing, |d| { |
| 3209 | d.child(div().w(px(3.)).h(px(12.)).bg(rgb(INK))) |
| 3210 | .child(div().w(px(3.)).h(px(12.)).bg(rgb(INK))) |
| 3211 | }) |
| 3212 | .when(!playing, |d| { |
| 3213 | d.child( |
| 3214 | canvas( |
| 3215 | |_, _, _| (), |
| 3216 | |bounds, _, window, _| { |
| 3217 | let (o, s) = (bounds.origin, bounds.size); |
| 3218 | let mut path = PathBuilder::fill(); |
| 3219 | path.move_to(o); |
| 3220 | path.line_to(point(o.x + s.width, o.y + s.height / 2.)); |
| 3221 | path.line_to(point(o.x, o.y + s.height)); |
| 3222 | path.close(); |
| 3223 | if let Ok(path) = path.build() { |
| 3224 | window.paint_path(path, rgb(INK)); |
| 3225 | } |
| 3226 | }, |
| 3227 | ) |
| 3228 | .w(px(11.)) |
| 3229 | .h(px(13.)), |
| 3230 | ) |
| 3231 | }) |
| 3232 | } |
| 3233 | |
| 3234 | |
| 3235 | fn timecode(t: f64, fps: f64) -> String { |
| 3236 | let t = t.max(0.); |
| 3237 | let secs = t as u64; |
| 3238 | let last = (fps.ceil() as u64).saturating_sub(1); |
| 3239 | let frame = ((t - secs as f64) * fps) as u64; |
| 3240 | format!( |
| 3241 | "{:02}:{:02}:{:02}:{:02}", |
| 3242 | secs / 3600, |
| 3243 | secs / 60 % 60, |
| 3244 | secs % 60, |
| 3245 | frame.min(last) |
| 3246 | ) |
| 3247 | } |
| 3248 | |
| 3249 | #[cfg(test)] |
| 3250 | mod tests { |
| 3251 | use super::{ |
| 3252 | ACCENT, CONTROL_H, Clip, HEADER_GAP, HEADER_W, HIT_MIN, INK, INK_DIM, KEYS_ROW_H, |
| 3253 | KEYS_ROWS_H, KEYS_W, LABEL_H, LABEL_MIN_W, LANE_H, LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, |
| 3254 | Lane, MENU_ITEMS, MENU_W, NO_FILE, PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, |
| 3255 | SOURCE_TINTS, SURFACE, SWATCH_W, Source, StreamInfo, Volume, WAVE_BPS, WAVE_COL, Wave, |
| 3256 | applicable, can_add, cancels_export, envelope, export_path, export_settings, frac_along, |
| 3257 | frame_at, is_bare_modifier, is_project, keymap, lane_clips, marked, menu_at, normalise, |
| 3258 | project_path, push_digit, scrub_due, show_label, source_frames, source_tint, start_frac, |
| 3259 | timecode, unseen_paths, unseen_sources, width_frac, window_title, |
| 3260 | }; |
| 3261 | use super::{file_name, library_rows}; |
| 3262 | use engine::PlaybackSession; |
| 3263 | use gpui::{Bounds, Pixels, point, px, size}; |
| 3264 | use std::collections::HashMap; |
| 3265 | use std::path::{Path, PathBuf}; |
| 3266 | use std::time::Duration; |
| 3267 | |
| 3268 | fn asset(name: &str) -> PathBuf { |
| 3269 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 3270 | .join("../../assets") |
| 3271 | .join(name) |
| 3272 | } |
| 3273 | |
| 3274 | |
| 3275 | |
| 3276 | fn clip(source: usize, frames: u32) -> Clip { |
| 3277 | Clip { |
| 3278 | start: 0, |
| 3279 | in_frame: 0, |
| 3280 | out_frame: frames, |
| 3281 | source, |
| 3282 | link: None, |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | |
| 3287 | fn source(path: &str, stream: usize) -> Source { |
| 3288 | Source { |
| 3289 | path: PathBuf::from(path), |
| 3290 | audio_stream: stream, |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | |
| 3295 | |
| 3296 | |
| 3297 | |
| 3298 | #[test] |
| 3299 | fn a_rows_swatch_and_duration_come_off_the_clips_that_name_it() { |
| 3300 | |
| 3301 | |
| 3302 | |
| 3303 | let sources: Vec<Source> = (0..4).map(|i| source(&format!("/m/{i}.mp4"), 0)).collect(); |
| 3304 | let video = [clip(0, 150), clip(1, 60)]; |
| 3305 | let audio = [clip(0, 150), clip(1, 30), clip(2, 90)]; |
| 3306 | for (row, frames) in [(0, 150), (1, 60), (2, 90), (3, 0)] { |
| 3307 | assert_eq!( |
| 3308 | source_frames(video.iter().chain(&audio), &sources, &sources[row].path), |
| 3309 | frames, |
| 3310 | "row {row}" |
| 3311 | ); |
| 3312 | |
| 3313 | |
| 3314 | assert_eq!(source_tint(row), SOURCE_TINTS[row % SOURCE_TINTS.len()]); |
| 3315 | } |
| 3316 | |
| 3317 | |
| 3318 | let one = [source("/m/0.mp4", 0)]; |
| 3319 | let path = &one[0].path; |
| 3320 | assert_eq!( |
| 3321 | source_frames([clip(0, 10), clip(0, 90)].iter(), &one, path), |
| 3322 | 90 |
| 3323 | ); |
| 3324 | assert_eq!( |
| 3325 | source_frames([clip(0, 90), clip(0, 10)].iter(), &one, path), |
| 3326 | 90 |
| 3327 | ); |
| 3328 | assert_eq!(source_frames([].iter(), &one, path), 0); |
| 3329 | |
| 3330 | |
| 3331 | |
| 3332 | let two = [source("/m/0.mp4", 0), source("/m/0.mp4", 1)]; |
| 3333 | assert_eq!(source_frames([clip(0, 90)].iter(), &two, &two[1].path), 90); |
| 3334 | } |
| 3335 | |
| 3336 | fn info( |
| 3337 | index: usize, |
| 3338 | rate: u32, |
| 3339 | channels: u16, |
| 3340 | lang: Option<&str>, |
| 3341 | decodable: bool, |
| 3342 | ) -> StreamInfo { |
| 3343 | StreamInfo { |
| 3344 | index, |
| 3345 | codec: if decodable { "aac" } else { "unknown" }.into(), |
| 3346 | channels, |
| 3347 | sample_rate: rate, |
| 3348 | lang: lang.map(str::to_string), |
| 3349 | decodable, |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | |
| 3354 | |
| 3355 | |
| 3356 | |
| 3357 | #[test] |
| 3358 | fn every_audio_stream_of_a_file_is_a_row_usable_or_not() { |
| 3359 | let multi = PathBuf::from("/m/movie.mp4"); |
| 3360 | let sources = [source("/m/movie.mp4", 0)]; |
| 3361 | let mut streams = HashMap::new(); |
| 3362 | streams.insert( |
| 3363 | multi.clone(), |
| 3364 | vec![ |
| 3365 | info(0, 44_100, 2, None, true), |
| 3366 | info(1, 44_100, 2, Some("fra"), true), |
| 3367 | info(2, 22_050, 1, Some("deu"), true), |
| 3368 | info(3, 0, 0, None, false), |
| 3369 | ], |
| 3370 | ); |
| 3371 | let rows = library_rows(&sources, &streams, Some((44_100, 2)), |_| 90); |
| 3372 | assert_eq!( |
| 3373 | rows.iter().map(|r| r.stream).collect::<Vec<_>>(), |
| 3374 | [0, 1, 2, 3], |
| 3375 | "one row per audio stream, in file order" |
| 3376 | ); |
| 3377 | assert!( |
| 3378 | rows.iter().all(|r| r.path == multi && r.tint == 0), |
| 3379 | "every stream of one file wears the file's own tint" |
| 3380 | ); |
| 3381 | assert_eq!(rows[0].name, "movie.mp4 [audio 1]"); |
| 3382 | assert_eq!(rows[1].name, "movie.mp4 [audio 2]"); |
| 3383 | assert_eq!(rows[1].detail, "fra 44.1 kHz stereo"); |
| 3384 | |
| 3385 | |
| 3386 | |
| 3387 | |
| 3388 | assert_eq!((&rows[0].unusable, &rows[1].unusable), (&None, &None)); |
| 3389 | assert_eq!( |
| 3390 | rows[2].unusable.as_deref(), |
| 3391 | Some("the timeline is 44.1 kHz stereo") |
| 3392 | ); |
| 3393 | assert_eq!(rows[3].unusable.as_deref(), Some("unsupported codec")); |
| 3394 | assert_eq!( |
| 3395 | rows[3].detail, "", |
| 3396 | "a stream we cannot parse claims nothing" |
| 3397 | ); |
| 3398 | |
| 3399 | assert!(rows.iter().all(|r| r.frames == 90)); |
| 3400 | |
| 3401 | |
| 3402 | |
| 3403 | let plain = [source("/m/plain.mp4", 0)]; |
| 3404 | let mut one = HashMap::new(); |
| 3405 | one.insert( |
| 3406 | PathBuf::from("/m/plain.mp4"), |
| 3407 | vec![info(0, 44_100, 2, None, true)], |
| 3408 | ); |
| 3409 | let rows = library_rows(&plain, &one, Some((44_100, 2)), |_| 90); |
| 3410 | assert_eq!(rows.len(), 1); |
| 3411 | assert_eq!(rows[0].name, "plain.mp4"); |
| 3412 | assert_eq!( |
| 3413 | rows[0].detail, "", |
| 3414 | "one audio track is the row it has always been: name and length" |
| 3415 | ); |
| 3416 | |
| 3417 | let mut silent = HashMap::new(); |
| 3418 | silent.insert(PathBuf::from("/m/plain.mp4"), Vec::new()); |
| 3419 | for probe in [silent, HashMap::new()] { |
| 3420 | let rows = library_rows(&plain, &probe, None, |_| 90); |
| 3421 | assert_eq!(rows.len(), 1); |
| 3422 | assert_eq!(rows[0].name, "plain.mp4"); |
| 3423 | assert_eq!(rows[0].detail, ""); |
| 3424 | } |
| 3425 | |
| 3426 | |
| 3427 | |
| 3428 | |
| 3429 | let placed = [ |
| 3430 | source("/m/movie.mp4", 0), |
| 3431 | source("/m/other.mp4", 0), |
| 3432 | source("/m/movie.mp4", 2), |
| 3433 | ]; |
| 3434 | streams.insert( |
| 3435 | PathBuf::from("/m/other.mp4"), |
| 3436 | vec![info(0, 44_100, 2, None, true)], |
| 3437 | ); |
| 3438 | let rows = library_rows(&placed, &streams, Some((44_100, 2)), |_| 90); |
| 3439 | assert_eq!( |
| 3440 | rows.iter() |
| 3441 | .map(|r| (file_name(&r.path), r.stream)) |
| 3442 | .collect::<Vec<_>>(), |
| 3443 | [ |
| 3444 | ("movie.mp4".to_string(), 0), |
| 3445 | ("other.mp4".to_string(), 0), |
| 3446 | ("movie.mp4".to_string(), 2), |
| 3447 | ("movie.mp4".to_string(), 1), |
| 3448 | ("movie.mp4".to_string(), 3), |
| 3449 | ] |
| 3450 | ); |
| 3451 | assert!( |
| 3452 | rows[2].unusable.is_none(), |
| 3453 | "a stream already on the timeline is playing, whatever a probe says" |
| 3454 | ); |
| 3455 | assert_eq!(rows[1].tint, 1, "the other file is the other tint"); |
| 3456 | } |
| 3457 | |
| 3458 | |
| 3459 | |
| 3460 | |
| 3461 | |
| 3462 | #[test] |
| 3463 | fn the_clip_menu_dims_what_the_playhead_is_not_on_and_stays_in_the_window() { |
| 3464 | use keymap::{ActionId, Keymap}; |
| 3465 | |
| 3466 | let clip = Clip { |
| 3467 | start: 30, |
| 3468 | in_frame: 0, |
| 3469 | out_frame: 60, |
| 3470 | source: 0, |
| 3471 | link: None, |
| 3472 | }; |
| 3473 | assert_eq!(clip.end(), 90); |
| 3474 | |
| 3475 | assert!(applicable(&clip, ActionId::Cut, 31)); |
| 3476 | assert!(applicable(&clip, ActionId::Cut, 89)); |
| 3477 | assert!(!applicable(&clip, ActionId::Cut, 30)); |
| 3478 | assert!(!applicable(&clip, ActionId::Cut, 90)); |
| 3479 | assert!(!applicable(&clip, ActionId::Cut, 200)); |
| 3480 | |
| 3481 | assert!(applicable(&clip, ActionId::Regroup, 30)); |
| 3482 | assert!(applicable(&clip, ActionId::Regroup, 90)); |
| 3483 | assert!(!applicable(&clip, ActionId::Regroup, 60)); |
| 3484 | |
| 3485 | |
| 3486 | for action in [ActionId::Delete, ActionId::Lift, ActionId::ToggleMute] { |
| 3487 | assert!(applicable(&clip, action, 0)); |
| 3488 | assert!(applicable(&clip, action, 60)); |
| 3489 | } |
| 3490 | |
| 3491 | assert_eq!(frame_at(1.0, 30.), 30); |
| 3492 | assert_eq!(frame_at(0.0, 30.), 0); |
| 3493 | assert_eq!(frame_at(-1.0, 30.), 0); |
| 3494 | |
| 3495 | let viewport = size(px(800.), px(400.)); |
| 3496 | assert_eq!(menu_at(point(px(10.), px(10.)), viewport, 150.), (10., 10.)); |
| 3497 | assert_eq!( |
| 3498 | menu_at(point(px(700.), px(380.)), viewport, 150.), |
| 3499 | (800. - MENU_W, 250.) |
| 3500 | ); |
| 3501 | |
| 3502 | assert_eq!( |
| 3503 | menu_at(point(px(90.), px(40.)), size(px(100.), px(50.)), 150.), |
| 3504 | (0., 0.) |
| 3505 | ); |
| 3506 | |
| 3507 | |
| 3508 | |
| 3509 | let keymap = Keymap::defaults(); |
| 3510 | for action in MENU_ITEMS { |
| 3511 | assert!(ActionId::ALL.contains(&action), "{action:?} is not listed"); |
| 3512 | assert_ne!(keymap.display(action), "unbound", "{action:?}"); |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | |
| 3517 | |
| 3518 | |
| 3519 | #[test] |
| 3520 | fn a_refused_import_leaves_no_row_and_an_accepted_one_is_whole() { |
| 3521 | let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); |
| 3522 | |
| 3523 | session.set_gain(0.0); |
| 3524 | assert_eq!(session.sources().len(), 1); |
| 3525 | let refusal = session |
| 3526 | .import(&asset("test_mismatch.mp4")) |
| 3527 | .expect_err("640x360 must not join a 1280x720 timeline") |
| 3528 | .to_string(); |
| 3529 | assert!(refusal.contains("640"), "refusal must name it: {refusal}"); |
| 3530 | assert_eq!(session.sources().len(), 1, "a refusal added a row"); |
| 3531 | |
| 3532 | |
| 3533 | session.import(&asset("test_av2.mp4")).expect("av2 matches"); |
| 3534 | assert_eq!(session.sources().len(), 2); |
| 3535 | let second = session.sources()[1].path.clone(); |
| 3536 | assert_eq!( |
| 3537 | source_frames(lane_clips(&session), session.sources(), &second), |
| 3538 | 120 |
| 3539 | ); |
| 3540 | assert_eq!(timecode(120. / 30., 30.), "00:00:04:00"); |
| 3541 | } |
| 3542 | |
| 3543 | |
| 3544 | |
| 3545 | |
| 3546 | #[test] |
| 3547 | fn adding_a_row_drops_the_whole_source_in_at_the_playhead() { |
| 3548 | let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); |
| 3549 | session.set_gain(0.0); |
| 3550 | session.import(&asset("test_av2.mp4")).expect("av2 matches"); |
| 3551 | assert_eq!(session.timeline_duration(), 9.0); |
| 3552 | |
| 3553 | session.seek(2.0); |
| 3554 | let second = session.sources()[1].path.clone(); |
| 3555 | let frames = source_frames(lane_clips(&session), session.sources(), &second); |
| 3556 | |
| 3557 | |
| 3558 | assert!( |
| 3559 | session |
| 3560 | .place_stream_at(2.0, &second, 0, frames) |
| 3561 | .expect("av2 is already on the timeline") |
| 3562 | ); |
| 3563 | |
| 3564 | |
| 3565 | assert_eq!(session.timeline_duration(), 13.0); |
| 3566 | let (video, audio) = ( |
| 3567 | session.lane_clips(Lane::Video), |
| 3568 | session.lane_clips(Lane::Audio), |
| 3569 | ); |
| 3570 | |
| 3571 | |
| 3572 | let at = |lane: &[Clip]| { |
| 3573 | *lane |
| 3574 | .iter() |
| 3575 | .find(|c| c.start == 60) |
| 3576 | .expect("inserted at 2 s") |
| 3577 | }; |
| 3578 | assert_eq!(at(video), at(audio)); |
| 3579 | assert_eq!(at(video).source, 1); |
| 3580 | assert_eq!(at(video).len(), frames); |
| 3581 | assert!(at(video).link.is_some()); |
| 3582 | assert_eq!(video.len(), audio.len()); |
| 3583 | |
| 3584 | |
| 3585 | |
| 3586 | |
| 3587 | let mut session = |
| 3588 | PlaybackSession::open(asset("test_multilang.mp4")).expect("open the fixture"); |
| 3589 | session.set_gain(0.0); |
| 3590 | let path = session.sources()[0].path.clone(); |
| 3591 | let frames = source_frames(lane_clips(&session), session.sources(), &path); |
| 3592 | let end = session.timeline_duration(); |
| 3593 | assert!( |
| 3594 | session |
| 3595 | .place_stream_at(end, &path, 1, frames) |
| 3596 | .expect("the French track shares the timeline's parameters") |
| 3597 | ); |
| 3598 | assert_eq!(session.sources()[1].audio_stream, 1); |
| 3599 | assert_eq!(session.timeline_duration(), end * 2.0); |
| 3600 | |
| 3601 | |
| 3602 | assert_eq!( |
| 3603 | source_frames(lane_clips(&session), session.sources(), &path), |
| 3604 | frames |
| 3605 | ); |
| 3606 | } |
| 3607 | |
| 3608 | #[test] |
| 3609 | fn a_source_is_only_ever_asked_for_its_peaks_once() { |
| 3610 | let (a, b) = (asset("test_av.mp4"), asset("test_av2.mp4")); |
| 3611 | |
| 3612 | |
| 3613 | let sources = [ |
| 3614 | Source { |
| 3615 | path: a.clone(), |
| 3616 | audio_stream: 0, |
| 3617 | }, |
| 3618 | Source { |
| 3619 | path: a.clone(), |
| 3620 | audio_stream: 1, |
| 3621 | }, |
| 3622 | Source { |
| 3623 | path: b.clone(), |
| 3624 | audio_stream: 0, |
| 3625 | }, |
| 3626 | ]; |
| 3627 | let keys = |s: &[Source]| { |
| 3628 | s.iter() |
| 3629 | .map(|s| (s.path.clone(), s.audio_stream)) |
| 3630 | .collect::<Vec<_>>() |
| 3631 | }; |
| 3632 | let mut waves: HashMap<(PathBuf, usize), Wave> = HashMap::new(); |
| 3633 | assert_eq!(unseen_sources(&sources, &waves), keys(&sources)); |
| 3634 | |
| 3635 | |
| 3636 | |
| 3637 | waves.insert((a.clone(), 0), Wave::Loading); |
| 3638 | assert_eq!( |
| 3639 | unseen_sources(&sources, &waves), |
| 3640 | keys(&sources[1..]), |
| 3641 | "the file's other stream is a key of its own" |
| 3642 | ); |
| 3643 | |
| 3644 | waves.insert((a, 1), Wave::Silent); |
| 3645 | waves.insert((b.clone(), 0), Wave::Silent); |
| 3646 | assert!(unseen_sources(&sources, &waves).is_empty()); |
| 3647 | |
| 3648 | let mut streams: HashMap<PathBuf, Vec<StreamInfo>> = HashMap::new(); |
| 3649 | assert_eq!( |
| 3650 | unseen_paths(&sources, &streams), |
| 3651 | vec![asset("test_av.mp4"), b] |
| 3652 | ); |
| 3653 | streams.insert(asset("test_av.mp4"), Vec::new()); |
| 3654 | assert_eq!(unseen_paths(&sources, &streams).len(), 1); |
| 3655 | } |
| 3656 | |
| 3657 | #[test] |
| 3658 | fn the_add_button_is_dead_unless_it_would_do_something() { |
| 3659 | let picked = (PathBuf::from("/m/0.mp4"), 0); |
| 3660 | assert!(can_add(Some(&picked), true, false)); |
| 3661 | |
| 3662 | |
| 3663 | assert!(!can_add(None, true, false)); |
| 3664 | assert!(!can_add(Some(&picked), false, false)); |
| 3665 | assert!(!can_add(Some(&picked), true, true)); |
| 3666 | } |
| 3667 | |
| 3668 | #[test] |
| 3669 | fn the_media_column_never_takes_the_picture_over() { |
| 3670 | for window in [360., 640., 1280., 1920., 3840.] { |
| 3671 | let w = super::library_w(window); |
| 3672 | |
| 3673 | |
| 3674 | assert!(w <= window / 3., "{window}px window gave the list {w}px"); |
| 3675 | assert!(w >= LIBRARY_MIN_W.min(window / 3.), "{window}px: {w}px"); |
| 3676 | assert!(w <= LIBRARY_MAX_W); |
| 3677 | } |
| 3678 | |
| 3679 | assert!(super::library_w(640.) < super::library_w(1280.)); |
| 3680 | |
| 3681 | |
| 3682 | assert!(ROW_H >= HIT_MIN); |
| 3683 | assert!(SWATCH_W < LIBRARY_MIN_W); |
| 3684 | } |
| 3685 | |
| 3686 | #[test] |
| 3687 | fn the_window_is_named_after_the_program_and_what_is_open() { |
| 3688 | assert_eq!(window_title("test_av.mp4"), "test_av.mp4 — edith"); |
| 3689 | |
| 3690 | assert_eq!(window_title(NO_FILE), "edith"); |
| 3691 | } |
| 3692 | |
| 3693 | #[test] |
| 3694 | fn frac_along_measures_from_the_elements_own_left_edge() { |
| 3695 | |
| 3696 | let bar: Bounds<Pixels> = Bounds { |
| 3697 | origin: point(px(12.), px(400.)), |
| 3698 | size: size(px(200.), px(6.)), |
| 3699 | }; |
| 3700 | assert_eq!(frac_along(px(12.), bar), 0.); |
| 3701 | assert_eq!(frac_along(px(112.), bar), 0.5); |
| 3702 | assert_eq!(frac_along(px(212.), bar), 1.); |
| 3703 | |
| 3704 | assert_eq!(frac_along(px(0.), bar), 0.); |
| 3705 | assert_eq!(frac_along(px(9999.), bar), 1.); |
| 3706 | |
| 3707 | assert_eq!(frac_along(px(50.), Bounds::default()), 0.); |
| 3708 | } |
| 3709 | |
| 3710 | #[test] |
| 3711 | fn timecode_counts_frames_inside_the_second() { |
| 3712 | assert_eq!(timecode(0., 30.), "00:00:00:00"); |
| 3713 | assert_eq!(timecode(-1., 30.), "00:00:00:00"); |
| 3714 | assert_eq!(timecode(4.9667, 30.), "00:00:04:29"); |
| 3715 | assert_eq!(timecode(5., 30.), "00:00:05:00"); |
| 3716 | assert_eq!(timecode(3661.5, 30.), "01:01:01:15"); |
| 3717 | |
| 3718 | assert_eq!(timecode(1. - f64::EPSILON, 30.), "00:00:00:29"); |
| 3719 | assert_eq!(timecode(0.999, 29.97), "00:00:00:29"); |
| 3720 | } |
| 3721 | |
| 3722 | #[test] |
| 3723 | fn scrub_seeks_only_on_a_new_frame_and_only_every_100ms() { |
| 3724 | let (slow, fast) = (Duration::from_millis(100), Duration::from_millis(99)); |
| 3725 | |
| 3726 | |
| 3727 | assert!(scrub_due(31, 30, slow)); |
| 3728 | assert!(!scrub_due(30, 30, slow)); |
| 3729 | assert!(!scrub_due(31, 30, fast)); |
| 3730 | assert!(!scrub_due(30, 30, fast)); |
| 3731 | |
| 3732 | assert!(scrub_due(1, 0, Duration::from_secs(9))); |
| 3733 | } |
| 3734 | |
| 3735 | #[test] |
| 3736 | fn export_lands_beside_the_source_and_never_on_it() { |
| 3737 | |
| 3738 | assert_eq!( |
| 3739 | export_path("assets/test_baseline.mp4"), |
| 3740 | std::path::Path::new("assets/test_baseline.export.mp4") |
| 3741 | ); |
| 3742 | |
| 3743 | assert_eq!( |
| 3744 | export_path("a.export.mp4"), |
| 3745 | std::path::Path::new("a.export.export.mp4") |
| 3746 | ); |
| 3747 | |
| 3748 | assert_eq!(export_path("clip"), std::path::Path::new("clip.export.mp4")); |
| 3749 | assert_eq!( |
| 3750 | export_path("/v.1/clip.MP4"), |
| 3751 | std::path::Path::new("/v.1/clip.export.mp4") |
| 3752 | ); |
| 3753 | } |
| 3754 | |
| 3755 | #[test] |
| 3756 | fn the_first_save_lands_beside_the_media() { |
| 3757 | assert_eq!( |
| 3758 | project_path("assets/test_av.mp4"), |
| 3759 | std::path::Path::new("assets/test_av.edith") |
| 3760 | ); |
| 3761 | |
| 3762 | assert_eq!( |
| 3763 | project_path("a.export.mp4"), |
| 3764 | std::path::Path::new("a.export.edith") |
| 3765 | ); |
| 3766 | assert_eq!(project_path("clip"), std::path::Path::new("clip.edith")); |
| 3767 | |
| 3768 | assert_eq!(project_path("a.edith"), std::path::Path::new("a.edith")); |
| 3769 | } |
| 3770 | |
| 3771 | #[test] |
| 3772 | fn only_an_exact_edith_extension_is_a_project() { |
| 3773 | let p = std::path::Path::new; |
| 3774 | assert!(is_project(p("a.edith"))); |
| 3775 | |
| 3776 | assert!(is_project(p("/v.mp4/a.edith"))); |
| 3777 | assert!(!is_project(p("a.mp4"))); |
| 3778 | assert!(!is_project(p("/v.edith/a.mp4"))); |
| 3779 | |
| 3780 | |
| 3781 | assert!(!is_project(p("a.EDITH"))); |
| 3782 | |
| 3783 | assert!(!is_project(p("edith"))); |
| 3784 | assert!(!is_project(p("a.edith.mp4"))); |
| 3785 | } |
| 3786 | |
| 3787 | |
| 3788 | |
| 3789 | |
| 3790 | |
| 3791 | #[test] |
| 3792 | fn no_stroke_is_missing_from_the_keys_menu() { |
| 3793 | use keymap::{ActionId, Keymap}; |
| 3794 | |
| 3795 | |
| 3796 | let handler = include_str!("main.rs") |
| 3797 | .split("#[cfg(test)]") |
| 3798 | .next() |
| 3799 | .unwrap(); |
| 3800 | let keymap = Keymap::defaults(); |
| 3801 | let listed = |key: &str| { |
| 3802 | let pretty = keymap::Chord { |
| 3803 | key: key.to_string(), |
| 3804 | ctrl: false, |
| 3805 | } |
| 3806 | .pretty(); |
| 3807 | keymap.lookup(key, false).is_some() || keymap::FIXED.iter().any(|f| f.chord == pretty) |
| 3808 | }; |
| 3809 | let mut compared = 0; |
| 3810 | for (at, needle) in handler.match_indices("key == ") { |
| 3811 | let rest = &handler[at + needle.len()..]; |
| 3812 | let key = match rest.strip_prefix('"') { |
| 3813 | Some(literal) => { |
| 3814 | literal[..literal.find('"').expect("unterminated key")].to_string() |
| 3815 | } |
| 3816 | |
| 3817 | None if rest.starts_with("ESCAPE") => super::ESCAPE.to_string(), |
| 3818 | None => panic!("a key compared against something this cannot read: {rest:.20}"), |
| 3819 | }; |
| 3820 | assert!( |
| 3821 | listed(&key), |
| 3822 | "the handler answers to {key:?} and the keys menu never says so" |
| 3823 | ); |
| 3824 | compared += 1; |
| 3825 | } |
| 3826 | |
| 3827 | |
| 3828 | assert!( |
| 3829 | compared >= 4, |
| 3830 | "the key comparisons moved; this scan is blind" |
| 3831 | ); |
| 3832 | |
| 3833 | assert!(handler.contains("key.parse::<u32>()")); |
| 3834 | assert!(keymap::FIXED.iter().any(|f| f.chord == "0–9")); |
| 3835 | |
| 3836 | for action in ActionId::ALL { |
| 3837 | assert!(keymap::Category::ALL.contains(&action.category())); |
| 3838 | } |
| 3839 | for fixed in &keymap::FIXED { |
| 3840 | assert!(keymap::Category::ALL.contains(&fixed.category)); |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | #[test] |
| 3845 | fn clip_boxes_split_the_lane_by_duration() { |
| 3846 | |
| 3847 | assert_eq!(width_frac(1., 4.), 0.25); |
| 3848 | assert_eq!(width_frac(3., 4.), 0.75); |
| 3849 | assert_eq!(width_frac(4., 4.), 1.); |
| 3850 | |
| 3851 | assert_eq!(width_frac(0., 0.), 1.); |
| 3852 | } |
| 3853 | |
| 3854 | #[test] |
| 3855 | fn escape_gets_out_of_an_export_whatever_is_held_with_it() { |
| 3856 | use keymap::ActionId; |
| 3857 | |
| 3858 | |
| 3859 | |
| 3860 | assert!(cancels_export("escape", None)); |
| 3861 | assert!(cancels_export("escape", Some(ActionId::CancelExport))); |
| 3862 | |
| 3863 | |
| 3864 | assert!(cancels_export("q", Some(ActionId::CancelExport))); |
| 3865 | |
| 3866 | assert!(!cancels_export("e", Some(ActionId::Export))); |
| 3867 | assert!(!cancels_export("space", Some(ActionId::Play))); |
| 3868 | assert!(!cancels_export("q", None)); |
| 3869 | } |
| 3870 | |
| 3871 | #[test] |
| 3872 | fn a_capture_waits_through_a_lone_modifier() { |
| 3873 | |
| 3874 | |
| 3875 | for key in [ |
| 3876 | "control", "shift", "alt", "super", "platform", "function", "fn", "meta", "command", |
| 3877 | ] { |
| 3878 | assert!(is_bare_modifier(key), "{key}"); |
| 3879 | } |
| 3880 | |
| 3881 | |
| 3882 | for key in ["c", "x", "space", "escape", "delete", "f1", "z"] { |
| 3883 | assert!(!is_bare_modifier(key), "{key}"); |
| 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | #[test] |
| 3888 | fn the_keybindings_card_fits_the_smallest_window() { |
| 3889 | |
| 3890 | |
| 3891 | |
| 3892 | let title = 17.; |
| 3893 | let status = 28.; |
| 3894 | let gaps = 3. * 2.; |
| 3895 | let padding = 24.; |
| 3896 | assert!( |
| 3897 | title + status + KEYS_ROWS_H + gaps + padding <= 360., |
| 3898 | "card too tall" |
| 3899 | ); |
| 3900 | |
| 3901 | |
| 3902 | |
| 3903 | assert!( |
| 3904 | KEYS_ROWS_H / KEYS_ROW_H >= 8., |
| 3905 | "too few rows visible to scan" |
| 3906 | ); |
| 3907 | assert!(KEYS_W <= 640., "card too wide"); |
| 3908 | |
| 3909 | |
| 3910 | assert!(KEYS_ROW_H >= HIT_MIN); |
| 3911 | } |
| 3912 | |
| 3913 | |
| 3914 | |
| 3915 | #[test] |
| 3916 | fn muting_keeps_the_level_it_comes_back_to() { |
| 3917 | let mut volume = Volume::default(); |
| 3918 | assert_eq!(volume.gain(), 1.0); |
| 3919 | assert_eq!(volume.label(), "Vol 100%"); |
| 3920 | |
| 3921 | |
| 3922 | |
| 3923 | for _ in 0..4 { |
| 3924 | volume.step(false); |
| 3925 | } |
| 3926 | assert_eq!(volume.gain(), 0.8); |
| 3927 | volume.muted = true; |
| 3928 | assert_eq!(volume.gain(), 0.0); |
| 3929 | assert_eq!(volume.label(), "Muted 80%"); |
| 3930 | |
| 3931 | |
| 3932 | |
| 3933 | volume.step(false); |
| 3934 | assert_eq!(volume.gain(), 0.0); |
| 3935 | assert!(volume.muted); |
| 3936 | |
| 3937 | |
| 3938 | volume.muted = false; |
| 3939 | assert_eq!(volume.gain(), 0.75); |
| 3940 | } |
| 3941 | |
| 3942 | |
| 3943 | |
| 3944 | #[test] |
| 3945 | fn the_volume_stops_at_both_ends() { |
| 3946 | let mut volume = Volume::default(); |
| 3947 | for _ in 0..40 { |
| 3948 | volume.step(true); |
| 3949 | } |
| 3950 | assert_eq!(volume.gain(), 1.0); |
| 3951 | assert_eq!(volume.steps, Volume::MAX_STEPS); |
| 3952 | |
| 3953 | for _ in 0..40 { |
| 3954 | volume.step(false); |
| 3955 | } |
| 3956 | assert_eq!(volume.gain(), 0.0); |
| 3957 | assert_eq!(volume.label(), "Vol 0%"); |
| 3958 | |
| 3959 | |
| 3960 | |
| 3961 | assert!(!volume.muted); |
| 3962 | volume.step(true); |
| 3963 | assert_eq!(volume.gain(), 0.05); |
| 3964 | } |
| 3965 | |
| 3966 | #[test] |
| 3967 | fn a_quality_row_is_the_bitrate_it_promises() { |
| 3968 | |
| 3969 | |
| 3970 | assert_eq!(export_settings(Quality::Auto, 7).bitrate, None); |
| 3971 | assert_eq!(export_settings(Quality::Low, 0).bitrate, Some(2_000_000)); |
| 3972 | assert_eq!(export_settings(Quality::Medium, 0).bitrate, Some(6_000_000)); |
| 3973 | assert_eq!(export_settings(Quality::High, 0).bitrate, Some(12_000_000)); |
| 3974 | |
| 3975 | assert_eq!(export_settings(Quality::Custom, 7).bitrate, Some(7_000_000)); |
| 3976 | assert_eq!(Quality::Low.detail(0), "2 Mbps"); |
| 3977 | |
| 3978 | |
| 3979 | for quality in Quality::ALL { |
| 3980 | let settings = export_settings(quality, 7); |
| 3981 | if let Some(bitrate) = settings.bitrate { |
| 3982 | assert!( |
| 3983 | (1_000_000..=20_000_000).contains(&bitrate), |
| 3984 | "{quality:?} outside the engine clamp" |
| 3985 | ); |
| 3986 | } |
| 3987 | |
| 3988 | assert!(!settings.force_sw); |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | #[test] |
| 3993 | fn a_typed_bitrate_stops_at_two_digits() { |
| 3994 | assert_eq!(push_digit(0, 8), 8); |
| 3995 | assert_eq!(push_digit(1, 2), 12); |
| 3996 | |
| 3997 | |
| 3998 | |
| 3999 | assert_eq!(push_digit(12, 3), 12); |
| 4000 | assert_eq!(push_digit(99, 9), 99); |
| 4001 | |
| 4002 | assert!(u64::from(push_digit(99, 9)) * 1_000_000 < u64::from(u32::MAX)); |
| 4003 | } |
| 4004 | |
| 4005 | #[test] |
| 4006 | fn the_export_card_fits_the_smallest_window() { |
| 4007 | |
| 4008 | |
| 4009 | |
| 4010 | let rows = Quality::ALL.len() as f32 + 1.; |
| 4011 | let title = 17.; |
| 4012 | let status = 28.; |
| 4013 | let fixed = 17.; |
| 4014 | let gaps = (rows + 3.) * 2.; |
| 4015 | let padding = 24.; |
| 4016 | assert!( |
| 4017 | title + status + rows * KEYS_ROW_H + fixed + CONTROL_H + 4. + gaps + padding <= 360., |
| 4018 | "card too tall" |
| 4019 | ); |
| 4020 | |
| 4021 | assert!(KEYS_ROW_H >= HIT_MIN); |
| 4022 | assert!(CONTROL_H >= HIT_MIN); |
| 4023 | } |
| 4024 | |
| 4025 | #[test] |
| 4026 | fn nothing_clickable_is_smaller_than_the_wcag_minimum() { |
| 4027 | |
| 4028 | |
| 4029 | assert!(CONTROL_H >= HIT_MIN); |
| 4030 | assert!(RULER_HIT_H >= HIT_MIN); |
| 4031 | assert!(LANE_H >= HIT_MIN); |
| 4032 | } |
| 4033 | |
| 4034 | |
| 4035 | fn luminance(colour: u32) -> f64 { |
| 4036 | let channel = |shift: u32| { |
| 4037 | let s = f64::from((colour >> shift) & 0xff) / 255.; |
| 4038 | if s <= 0.03928 { |
| 4039 | s / 12.92 |
| 4040 | } else { |
| 4041 | ((s + 0.055) / 1.055).powf(2.4) |
| 4042 | } |
| 4043 | }; |
| 4044 | 0.2126 * channel(16) + 0.7152 * channel(8) + 0.0722 * channel(0) |
| 4045 | } |
| 4046 | |
| 4047 | |
| 4048 | fn contrast(a: u32, b: u32) -> f64 { |
| 4049 | let (a, b) = (luminance(a), luminance(b)); |
| 4050 | (a.max(b) + 0.05) / (a.min(b) + 0.05) |
| 4051 | } |
| 4052 | |
| 4053 | #[test] |
| 4054 | fn a_lane_row_is_a_fixed_header_and_a_bed_that_can_be_hit() { |
| 4055 | |
| 4056 | |
| 4057 | assert!(HEADER_W > 0. && HEADER_GAP >= 0.); |
| 4058 | |
| 4059 | |
| 4060 | assert!( |
| 4061 | CONTROL_H + RULER_HIT_H + 2. * LANE_H + 17. + 4. * 8. + 16. <= PANEL_H, |
| 4062 | "the second lane does not fit the panel" |
| 4063 | ); |
| 4064 | |
| 4065 | |
| 4066 | assert!(LANE_H >= HIT_MIN); |
| 4067 | |
| 4068 | assert!(LABEL_H < LANE_H / 2.); |
| 4069 | } |
| 4070 | |
| 4071 | #[test] |
| 4072 | fn a_click_marks_the_whole_group_and_nothing_else() { |
| 4073 | let (v, a) = ((Lane::Video, 0), (Lane::Audio, 0)); |
| 4074 | |
| 4075 | assert!(marked(v, Some(1), Some(v), Some(1))); |
| 4076 | assert!(marked(a, Some(1), Some(v), Some(1))); |
| 4077 | |
| 4078 | assert!(!marked((Lane::Video, 1), Some(2), Some(v), Some(1))); |
| 4079 | assert!(!marked((Lane::Audio, 1), Some(2), Some(v), Some(1))); |
| 4080 | |
| 4081 | |
| 4082 | |
| 4083 | assert!(marked(a, None, Some(a), None)); |
| 4084 | assert!(!marked(v, None, Some(a), None)); |
| 4085 | |
| 4086 | assert!(!marked(v, Some(1), None, None)); |
| 4087 | } |
| 4088 | |
| 4089 | #[test] |
| 4090 | fn a_name_is_dropped_rather_than_smeared_across_a_thin_clip() { |
| 4091 | assert!(show_label(LABEL_MIN_W)); |
| 4092 | assert!(show_label(400.)); |
| 4093 | assert!(!show_label(LABEL_MIN_W - 0.1)); |
| 4094 | |
| 4095 | assert_eq!(start_frac(3., 0.), 0.); |
| 4096 | assert_eq!(start_frac(1., 4.), 0.25); |
| 4097 | |
| 4098 | assert_eq!(start_frac(8., 4.), 1.); |
| 4099 | } |
| 4100 | |
| 4101 | #[test] |
| 4102 | fn an_envelope_stays_inside_the_box_it_is_drawn_in() { |
| 4103 | |
| 4104 | let peaks: Vec<(f32, f32)> = (0..40) |
| 4105 | .map(|i| (-(i as f32) / 39., i as f32 / 39.)) |
| 4106 | .collect(); |
| 4107 | let (w, h) = (100., 30.); |
| 4108 | let cols = envelope(&peaks, 0., 1., w, h); |
| 4109 | assert_eq!(cols.len(), (w / WAVE_COL) as usize + 1); |
| 4110 | for &(x, top, bottom) in &cols { |
| 4111 | assert!((0. ..=w).contains(&x), "x {x} outside 0..{w}"); |
| 4112 | assert!((0. ..=h).contains(&top), "top {top} outside 0..{h}"); |
| 4113 | assert!( |
| 4114 | (0. ..=h).contains(&bottom), |
| 4115 | "bottom {bottom} outside 0..{h}" |
| 4116 | ); |
| 4117 | |
| 4118 | |
| 4119 | assert!( |
| 4120 | bottom - top >= 1., |
| 4121 | "column {top}..{bottom} is thinner than a pixel" |
| 4122 | ); |
| 4123 | } |
| 4124 | |
| 4125 | let height = |&(_, top, bottom): &(f32, f32, f32)| bottom - top; |
| 4126 | assert!(height(cols.last().unwrap()) > height(cols.first().unwrap()) + 5.); |
| 4127 | |
| 4128 | assert!(envelope(&[], 0., 1., w, h).is_empty()); |
| 4129 | assert!(envelope(&peaks, 0., 1., 0., h).is_empty()); |
| 4130 | |
| 4131 | assert!(!envelope(&peaks, 0., 99., w, h).is_empty()); |
| 4132 | } |
| 4133 | |
| 4134 | #[test] |
| 4135 | fn a_quiet_source_still_draws_as_a_shape() { |
| 4136 | |
| 4137 | let quiet: Vec<(f32, f32)> = vec![(-0.125, 0.125), (-0.0625, 0.0625)]; |
| 4138 | let loud = normalise(quiet.clone()); |
| 4139 | assert_eq!(loud[0], (-1., 1.)); |
| 4140 | assert_eq!(loud[1], (-0.5, 0.5)); |
| 4141 | |
| 4142 | |
| 4143 | assert_eq!(normalise(vec![(0., 0.)]), vec![(0., 0.)]); |
| 4144 | assert!(normalise(Vec::new()).is_empty()); |
| 4145 | } |
| 4146 | |
| 4147 | |
| 4148 | |
| 4149 | #[test] |
| 4150 | fn the_fixtures_waveform_reaches_the_lane_as_a_shape() { |
| 4151 | let asset = |name: &str| { |
| 4152 | std::path::Path::new(env!("CARGO_MANIFEST_DIR")) |
| 4153 | .join("../../assets") |
| 4154 | .join(name) |
| 4155 | }; |
| 4156 | let peaks = normalise( |
| 4157 | engine::waveform::peaks(asset("test_av.mp4"), 0, WAVE_BPS) |
| 4158 | .expect("open the fixture") |
| 4159 | .expect("test_av.mp4 has audio"), |
| 4160 | ); |
| 4161 | |
| 4162 | assert!(peaks.len().abs_diff(5 * WAVE_BPS as usize) <= WAVE_BPS as usize); |
| 4163 | let cols = envelope(&peaks, 0., 5., 600., 30.); |
| 4164 | let height = |&(_, top, bottom): &(f32, f32, f32)| bottom - top; |
| 4165 | let tallest = cols.iter().map(height).fold(0., f32::max); |
| 4166 | let flattest = cols.iter().map(height).fold(f32::MAX, f32::min); |
| 4167 | |
| 4168 | |
| 4169 | assert!(tallest > 25., "loudest column only {tallest} px of 30"); |
| 4170 | assert!( |
| 4171 | flattest < 8., |
| 4172 | "quietest column {flattest} px -- no dips drawn" |
| 4173 | ); |
| 4174 | |
| 4175 | assert!( |
| 4176 | engine::waveform::peaks(asset("test_baseline.mp4"), 0, WAVE_BPS) |
| 4177 | .expect("open the fixture") |
| 4178 | .is_none() |
| 4179 | ); |
| 4180 | } |
| 4181 | |
| 4182 | #[test] |
| 4183 | fn every_mark_on_a_clip_is_legible_on_it() { |
| 4184 | for (i, tint) in SOURCE_TINTS.iter().enumerate() { |
| 4185 | |
| 4186 | assert!( |
| 4187 | contrast(INK, *tint) >= 4.5, |
| 4188 | "source {i}: label contrast {:.2}", |
| 4189 | contrast(INK, *tint) |
| 4190 | ); |
| 4191 | |
| 4192 | assert!( |
| 4193 | contrast(INK_DIM, *tint) >= 3., |
| 4194 | "source {i}: waveform contrast {:.2}", |
| 4195 | contrast(INK_DIM, *tint) |
| 4196 | ); |
| 4197 | } |
| 4198 | |
| 4199 | assert!(contrast(INK, SELECTED) >= 4.5); |
| 4200 | assert!(contrast(INK_DIM, SELECTED) >= 3.); |
| 4201 | |
| 4202 | |
| 4203 | assert!(contrast(LETTERBOX, SURFACE) >= 1.5); |
| 4204 | assert!(contrast(ACCENT, LETTERBOX) >= 3.); |
| 4205 | |
| 4206 | assert!((contrast(0xffffff, 0x000000) - 21.).abs() < 0.01); |
| 4207 | } |
| 4208 | |
| 4209 | #[test] |
| 4210 | fn source_tints_differ_per_source_and_cycle() { |
| 4211 | |
| 4212 | assert_eq!(source_tint(0), SURFACE); |
| 4213 | |
| 4214 | assert_ne!(source_tint(0), source_tint(1)); |
| 4215 | assert_ne!(source_tint(1), source_tint(2)); |
| 4216 | assert_ne!(source_tint(2), source_tint(3)); |
| 4217 | |
| 4218 | assert_eq!(source_tint(4), source_tint(0)); |
| 4219 | assert_eq!(source_tint(9), source_tint(1)); |
| 4220 | assert_eq!(source_tint(usize::MAX), SOURCE_TINTS[usize::MAX % 4]); |
| 4221 | } |
| 4222 | } |
| 4223 | |
| 4224 | fn main() { |
| 4225 | let arg = std::env::args().nth(1).map(PathBuf::from); |
| 4226 | |
| 4227 | |
| 4228 | |
| 4229 | |
| 4230 | |
| 4231 | let mut session = match &arg { |
| 4232 | Some(arg) => { |
| 4233 | let opened = if is_project(arg) { |
| 4234 | PlaybackSession::open_project(arg) |
| 4235 | } else { |
| 4236 | PlaybackSession::open(arg) |
| 4237 | }; |
| 4238 | match opened { |
| 4239 | Ok(v) => Some(v), |
| 4240 | Err(e) => { |
| 4241 | |
| 4242 | |
| 4243 | |
| 4244 | eprintln!("cannot open {}: {e}", arg.display()); |
| 4245 | std::process::exit(1); |
| 4246 | } |
| 4247 | } |
| 4248 | } |
| 4249 | None => None, |
| 4250 | }; |
| 4251 | |
| 4252 | |
| 4253 | |
| 4254 | let (keymap, mut notice) = Keymap::load(); |
| 4255 | if let Some(text) = ¬ice { |
| 4256 | eprintln!("{text}"); |
| 4257 | } |
| 4258 | |
| 4259 | |
| 4260 | |
| 4261 | for extra in std::env::args().skip(2) { |
| 4262 | |
| 4263 | if let Some(session) = &mut session |
| 4264 | && let Err(e) = session.import(std::path::Path::new(&extra)) |
| 4265 | { |
| 4266 | let text = format!("IMPORT FAILED: {e}"); |
| 4267 | eprintln!("{extra}: {text}"); |
| 4268 | notice.get_or_insert(text); |
| 4269 | } |
| 4270 | } |
| 4271 | |
| 4272 | |
| 4273 | if let Some(reason) = session |
| 4274 | .as_ref() |
| 4275 | .and_then(PlaybackSession::audio_disabled_reason) |
| 4276 | { |
| 4277 | let text = format!("NO AUDIO: {reason}"); |
| 4278 | eprintln!("{text}"); |
| 4279 | notice.get_or_insert(text); |
| 4280 | } |
| 4281 | let meta = session.as_ref().map(|session| *session.meta()); |
| 4282 | |
| 4283 | |
| 4284 | let out = session.as_ref().map_or_else(PathBuf::new, |session| { |
| 4285 | export_path(&session.sources()[0].path) |
| 4286 | }); |
| 4287 | let project = match &arg { |
| 4288 | Some(arg) if is_project(arg) => arg.clone(), |
| 4289 | Some(arg) => project_path(arg), |
| 4290 | |
| 4291 | None => PathBuf::new(), |
| 4292 | }; |
| 4293 | let name: SharedString = arg |
| 4294 | .as_deref() |
| 4295 | .map_or_else(|| NO_FILE.into(), |arg| file_name(arg).into()); |
| 4296 | if let (Some(arg), Some(meta)) = (&arg, &meta) { |
| 4297 | println!( |
| 4298 | "{}: {}x{} @ {:.2} fps, {} samples", |
| 4299 | arg.display(), |
| 4300 | meta.width, |
| 4301 | meta.height, |
| 4302 | meta.frame_rate, |
| 4303 | meta.frame_count |
| 4304 | ); |
| 4305 | } |
| 4306 | |
| 4307 | Application::new().run(move |cx: &mut App| { |
| 4308 | |
| 4309 | |
| 4310 | let (w, h) = meta.map_or((1280., 720.), |meta| { |
| 4311 | (meta.width as f32, meta.height as f32) |
| 4312 | }); |
| 4313 | let bounds = Bounds::centered(None, size(px(w), px(h)), cx); |
| 4314 | cx.open_window( |
| 4315 | WindowOptions { |
| 4316 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 4317 | titlebar: Some(TitlebarOptions { |
| 4318 | title: Some("edith".into()), |
| 4319 | ..Default::default() |
| 4320 | }), |
| 4321 | |
| 4322 | |
| 4323 | |
| 4324 | |
| 4325 | |
| 4326 | app_id: Some("edith".to_string()), |
| 4327 | ..Default::default() |
| 4328 | }, |
| 4329 | |window, cx| { |
| 4330 | let player = cx.new(|cx| Player { |
| 4331 | |
| 4332 | |
| 4333 | |
| 4334 | |
| 4335 | pending_seek: session.is_some(), |
| 4336 | session, |
| 4337 | |
| 4338 | |
| 4339 | volume: Volume::default(), |
| 4340 | |
| 4341 | |
| 4342 | fps: meta.map_or(30., |meta| meta.frame_rate), |
| 4343 | name: name.clone(), |
| 4344 | image: None, |
| 4345 | held: None, |
| 4346 | eos: false, |
| 4347 | done: false, |
| 4348 | ruler: Rc::default(), |
| 4349 | selected: None, |
| 4350 | context_menu: None, |
| 4351 | selected_asset: None, |
| 4352 | waves: HashMap::new(), |
| 4353 | streams: HashMap::new(), |
| 4354 | clipboard: None, |
| 4355 | scrubbing: false, |
| 4356 | last_scrub: Instant::now(), |
| 4357 | last_target: 0, |
| 4358 | export: None, |
| 4359 | cancelling: false, |
| 4360 | export_path: out.clone(), |
| 4361 | project_path: project.clone(), |
| 4362 | keymap: keymap.clone(), |
| 4363 | keys_open: false, |
| 4364 | export_open: false, |
| 4365 | |
| 4366 | |
| 4367 | quality: Quality::Auto, |
| 4368 | custom_mbps: 0, |
| 4369 | rebinding: None, |
| 4370 | notice: notice.clone().map(SharedString::from), |
| 4371 | |
| 4372 | |
| 4373 | titled: String::new(), |
| 4374 | displayed: 0, |
| 4375 | dropped: 0, |
| 4376 | started: None, |
| 4377 | focus: cx.focus_handle(), |
| 4378 | }); |
| 4379 | |
| 4380 | |
| 4381 | window.focus(&player.read(cx).focus); |
| 4382 | player |
| 4383 | }, |
| 4384 | ) |
| 4385 | .unwrap(); |
| 4386 | cx.activate(true); |
| 4387 | }); |
| 4388 | } |