| 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::color::ColorParams; |
| 14 | use engine::eq::{Band, BandKind, EqParams}; |
| 15 | use engine::export::{ExportSettings, Format}; |
| 16 | use engine::project::{Lane, LaneKind, Source}; |
| 17 | use engine::scale::FitPolicy; |
| 18 | use engine::{Clip, ExportHandle, Frame, PlaybackSession}; |
| 19 | use gpui::{ |
| 20 | AnyElement, App, Application, Bounds, ClickEvent, Context, Div, FocusHandle, KeyDownEvent, |
| 21 | MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, Point, |
| 22 | RenderImage, SharedString, Size, Stateful, TitlebarOptions, Window, WindowBounds, |
| 23 | WindowOptions, canvas, div, img, point, prelude::*, px, relative, rgb, rgba, size, |
| 24 | }; |
| 25 | |
| 26 | |
| 27 | |
| 28 | const LETTERBOX: u32 = 0x101010; |
| 29 | const CHROME: u32 = 0x242424; |
| 30 | const SURFACE: u32 = 0x333333; |
| 31 | const INK: u32 = 0xc8c8c8; |
| 32 | const ACCENT: u32 = 0x4a9eff; |
| 33 | |
| 34 | const SELECTED: u32 = 0x2a4a6b; |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | const SOURCE_TINTS: [u32; 4] = [SURFACE, 0x3b3329, 0x293b33, 0x33293b]; |
| 40 | |
| 41 | |
| 42 | |
| 43 | const HOVER: u32 = 0x3f3f3f; |
| 44 | const HOVER_DIM: u32 = 0x2c2c2c; |
| 45 | |
| 46 | |
| 47 | const INK_DIM: u32 = 0xa0a0a0; |
| 48 | |
| 49 | |
| 50 | |
| 51 | const HEADER_H: f32 = 32.; |
| 52 | |
| 53 | |
| 54 | |
| 55 | const PANEL_H: f32 = 220.; |
| 56 | |
| 57 | |
| 58 | |
| 59 | const LANES_MAX: usize = 6; |
| 60 | const LANE_H: f32 = 48.; |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | const HEADER_W: f32 = 40.; |
| 66 | const HEADER_GAP: f32 = 4.; |
| 67 | |
| 68 | const LABEL_H: f32 = 15.; |
| 69 | |
| 70 | |
| 71 | const LABEL_MIN_W: f32 = 36.; |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | const WAVE_BPS: u32 = 40; |
| 77 | |
| 78 | |
| 79 | const WAVE_COL: f32 = 2.; |
| 80 | |
| 81 | |
| 82 | const HIT_MIN: f32 = 24.; |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | const LIBRARY_FRAC: f32 = 0.2; |
| 88 | const LIBRARY_MIN_W: f32 = 120.; |
| 89 | const LIBRARY_MAX_W: f32 = 220.; |
| 90 | |
| 91 | |
| 92 | const ROW_H: f32 = 32.; |
| 93 | |
| 94 | |
| 95 | const SWATCH_W: f32 = 4.; |
| 96 | const CONTROL_H: f32 = 28.; |
| 97 | const RULER_HIT_H: f32 = HIT_MIN; |
| 98 | |
| 99 | |
| 100 | const TIME_W: f32 = 200.; |
| 101 | |
| 102 | |
| 103 | const KEYS_W: f32 = 320.; |
| 104 | const KEYS_ROW_H: f32 = HIT_MIN; |
| 105 | |
| 106 | |
| 107 | |
| 108 | const KEYS_ROWS_H: f32 = 10. * KEYS_ROW_H; |
| 109 | |
| 110 | |
| 111 | |
| 112 | const EXPORT_ROWS_H: f32 = 8. * KEYS_ROW_H; |
| 113 | |
| 114 | |
| 115 | |
| 116 | const MENU_W: f32 = 260.; |
| 117 | const MENU_ROW_H: f32 = HIT_MIN; |
| 118 | const MENU_PAD: f32 = 6.; |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | const ESCAPE: &str = "escape"; |
| 125 | |
| 126 | |
| 127 | |
| 128 | const NO_FILE: &str = "no file open"; |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 142 | struct Volume { |
| 143 | steps: u8, |
| 144 | muted: bool, |
| 145 | } |
| 146 | |
| 147 | impl Volume { |
| 148 | |
| 149 | const MAX_STEPS: u8 = 20; |
| 150 | |
| 151 | |
| 152 | |
| 153 | fn gain(self) -> f32 { |
| 154 | if self.muted { |
| 155 | 0. |
| 156 | } else { |
| 157 | f32::from(self.steps) / f32::from(Self::MAX_STEPS) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | fn step(&mut self, up: bool) { |
| 164 | self.steps = if up { |
| 165 | self.steps.saturating_add(1).min(Self::MAX_STEPS) |
| 166 | } else { |
| 167 | self.steps.saturating_sub(1) |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
| 173 | fn label(self) -> String { |
| 174 | let percent = u32::from(self.steps) * 100 / u32::from(Self::MAX_STEPS); |
| 175 | if self.muted { |
| 176 | format!("Muted {percent}%") |
| 177 | } else { |
| 178 | format!("Vol {percent}%") |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | impl Default for Volume { |
| 184 | fn default() -> Self { |
| 185 | Self { |
| 186 | steps: Self::MAX_STEPS, |
| 187 | muted: false, |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | #[derive(Clone)] |
| 196 | enum Wave { |
| 197 | |
| 198 | Loading, |
| 199 | |
| 200 | Silent, |
| 201 | Peaks(Arc<Vec<(f32, f32)>>), |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | struct AssetDrag(PathBuf, usize); |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | #[derive(Clone, Copy)] |
| 214 | struct ContextMenu { |
| 215 | lane: Lane, |
| 216 | idx: usize, |
| 217 | at: Point<Pixels>, |
| 218 | details: bool, |
| 219 | } |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | const MENU_ITEMS: [ActionId; 8] = [ |
| 226 | ActionId::Cut, |
| 227 | ActionId::Delete, |
| 228 | ActionId::Lift, |
| 229 | ActionId::Regroup, |
| 230 | ActionId::Equalizer, |
| 231 | ActionId::Color, |
| 232 | ActionId::Fit, |
| 233 | ActionId::ToggleMute, |
| 234 | ]; |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | const RESOLUTIONS: [(u32, u32); 4] = [(3840, 2160), (1920, 1080), (1280, 720), (854, 480)]; |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | const EQ_GAIN_LIMIT: f32 = 12.; |
| 246 | |
| 247 | |
| 248 | |
| 249 | const EQ_STEP: f32 = 1.; |
| 250 | |
| 251 | |
| 252 | const EQ_BAR_H: f32 = 6.; |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | const COLOR_BANDS: [(&str, f32, f32); 4] = [ |
| 258 | ("Brightness", -1., 1.), |
| 259 | ("Contrast", 0., 2.), |
| 260 | ("Saturation", 0., 2.), |
| 261 | ("Tint (cool–warm)", -1., 1.), |
| 262 | ]; |
| 263 | |
| 264 | |
| 265 | |
| 266 | const COLOR_STEP: f32 = 0.05; |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | const COLOR_W: f32 = 460.; |
| 272 | |
| 273 | const COLOR_BAR_W: f32 = 140.; |
| 274 | |
| 275 | struct Player { |
| 276 | |
| 277 | |
| 278 | |
| 279 | session: Option<PlaybackSession>, |
| 280 | |
| 281 | |
| 282 | fps: f64, |
| 283 | name: SharedString, |
| 284 | image: Option<Arc<RenderImage>>, |
| 285 | |
| 286 | held: Option<Frame>, |
| 287 | |
| 288 | eos: bool, |
| 289 | |
| 290 | done: bool, |
| 291 | |
| 292 | |
| 293 | pending_seek: bool, |
| 294 | |
| 295 | |
| 296 | ruler: Rc<Cell<Bounds<Pixels>>>, |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | selected: Option<(Lane, usize)>, |
| 302 | |
| 303 | |
| 304 | |
| 305 | context_menu: Option<ContextMenu>, |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | selected_asset: Option<(PathBuf, usize)>, |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | waves: HashMap<(PathBuf, usize), Wave>, |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | streams: HashMap<PathBuf, Vec<StreamInfo>>, |
| 321 | |
| 322 | |
| 323 | clipboard: Option<Clip>, |
| 324 | |
| 325 | |
| 326 | scrubbing: bool, |
| 327 | last_scrub: Instant, |
| 328 | last_target: u32, |
| 329 | |
| 330 | export: Option<ExportHandle>, |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | cancelling: bool, |
| 336 | |
| 337 | |
| 338 | export_path: PathBuf, |
| 339 | |
| 340 | |
| 341 | |
| 342 | project_path: PathBuf, |
| 343 | |
| 344 | |
| 345 | keymap: Keymap, |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | volume: Volume, |
| 352 | |
| 353 | |
| 354 | |
| 355 | keys_open: bool, |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | export_open: bool, |
| 361 | |
| 362 | |
| 363 | |
| 364 | quality: Quality, |
| 365 | custom_mbps: u32, |
| 366 | |
| 367 | |
| 368 | format: Format, |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | eq_open: Option<(Lane, usize)>, |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | eq_params: EqParams, |
| 380 | |
| 381 | eq_band: usize, |
| 382 | |
| 383 | |
| 384 | eq_dragging: bool, |
| 385 | |
| 386 | |
| 387 | |
| 388 | eq_bar: Rc<Cell<Bounds<Pixels>>>, |
| 389 | |
| 390 | |
| 391 | |
| 392 | color_open: Option<(Lane, usize)>, |
| 393 | |
| 394 | |
| 395 | color_band: usize, |
| 396 | |
| 397 | |
| 398 | rebinding: Option<ActionId>, |
| 399 | |
| 400 | |
| 401 | |
| 402 | notice: Option<SharedString>, |
| 403 | |
| 404 | |
| 405 | |
| 406 | titled: String, |
| 407 | displayed: u32, |
| 408 | dropped: u32, |
| 409 | |
| 410 | started: Option<Instant>, |
| 411 | focus: FocusHandle, |
| 412 | } |
| 413 | |
| 414 | impl Player { |
| 415 | |
| 416 | |
| 417 | |
| 418 | fn pump(&mut self, window: &mut Window) { |
| 419 | |
| 420 | |
| 421 | let Some(session) = &mut self.session else { |
| 422 | return; |
| 423 | }; |
| 424 | let target = session.now() * self.fps; |
| 425 | let mut newest: Option<Frame> = None; |
| 426 | loop { |
| 427 | let frame = match self.held.take() { |
| 428 | Some(frame) => frame, |
| 429 | |
| 430 | |
| 431 | |
| 432 | None => match session.try_frame() { |
| 433 | Some(frame) => frame, |
| 434 | None => { |
| 435 | self.eos = session.is_eos(); |
| 436 | break; |
| 437 | } |
| 438 | }, |
| 439 | }; |
| 440 | if f64::from(frame.index) <= target { |
| 441 | self.dropped += u32::from(newest.is_some()); |
| 442 | newest = Some(frame); |
| 443 | } else { |
| 444 | self.held = Some(frame); |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | if let Some(frame) = newest { |
| 450 | self.displayed += 1; |
| 451 | self.pending_seek = false; |
| 452 | self.started.get_or_insert_with(|| { |
| 453 | eprintln!("first frame displayed (index {})", frame.index); |
| 454 | Instant::now() |
| 455 | }); |
| 456 | let buf = image::RgbaImage::from_raw(frame.width, frame.height, frame.bgra) |
| 457 | .expect("frame buffer sized width*height*4"); |
| 458 | let next = Arc::new(RenderImage::new(vec![image::Frame::new(buf)])); |
| 459 | if let Some(old) = self.image.replace(next) { |
| 460 | |
| 461 | |
| 462 | let _ = window.drop_image(old); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if self.eos && self.held.is_none() && !self.done { |
| 467 | self.done = true; |
| 468 | |
| 469 | |
| 470 | self.pending_seek = false; |
| 471 | let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64()); |
| 472 | eprintln!( |
| 473 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", |
| 474 | self.displayed, |
| 475 | self.dropped, |
| 476 | session.now() |
| 477 | ); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | fn reset_after_reseek(&mut self) { |
| 486 | self.held = None; |
| 487 | self.eos = false; |
| 488 | self.done = false; |
| 489 | self.pending_seek = true; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | fn act(&mut self, action: ActionId, cx: &mut Context<Self>) { |
| 496 | match action { |
| 497 | ActionId::Play => self.toggle_or_restart(cx), |
| 498 | ActionId::Export => self.open_export(cx), |
| 499 | ActionId::Save => self.save_project(cx), |
| 500 | ActionId::Copy => self.copy_selected(), |
| 501 | ActionId::Paste => self.paste(cx), |
| 502 | ActionId::Cut => self.cut(cx), |
| 503 | ActionId::Regroup => self.regroup(cx), |
| 504 | ActionId::Select => self.select_under_playhead(cx), |
| 505 | ActionId::SelectNext => self.select_step(true, cx), |
| 506 | ActionId::SelectPrev => self.select_step(false, cx), |
| 507 | ActionId::Delete => self.delete_selected(cx), |
| 508 | ActionId::Lift => self.lift_selected(cx), |
| 509 | ActionId::Color => self.open_color(cx), |
| 510 | ActionId::Fit => self.cycle_fit(cx), |
| 511 | ActionId::Resolution => self.cycle_resolution(cx), |
| 512 | ActionId::Undo => self.undo(cx), |
| 513 | ActionId::AddVideoLane => self.add_lane(LaneKind::Video, cx), |
| 514 | ActionId::AddAudioLane => self.add_lane(LaneKind::Audio, cx), |
| 515 | ActionId::ToggleMute => self.set_volume(|volume| volume.muted = !volume.muted, cx), |
| 516 | ActionId::VolumeUp => self.set_volume(|volume| volume.step(true), cx), |
| 517 | ActionId::VolumeDown => self.set_volume(|volume| volume.step(false), cx), |
| 518 | ActionId::Equalizer => self.open_eq(cx), |
| 519 | |
| 520 | |
| 521 | ActionId::CancelExport => {} |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | fn select(&mut self, target: (Lane, usize), cx: &mut Context<Self>) { |
| 530 | self.selected = Some(target); |
| 531 | cx.notify(); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | fn under_playhead(&self) -> Vec<(Lane, usize)> { |
| 538 | let Some(session) = &self.session else { |
| 539 | return Vec::new(); |
| 540 | }; |
| 541 | let now = session.now(); |
| 542 | session |
| 543 | .lanes() |
| 544 | .into_iter() |
| 545 | .filter_map(|lane| Some((lane, session.lane_clip_at(lane, now)?))) |
| 546 | .collect() |
| 547 | } |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | fn select_under_playhead(&mut self, cx: &mut Context<Self>) { |
| 554 | let under = self.under_playhead(); |
| 555 | let Some(&first) = under.first() else { |
| 556 | self.notice = Some("NOTHING UNDER THE PLAYHEAD — move it onto a clip first".into()); |
| 557 | cx.notify(); |
| 558 | return; |
| 559 | }; |
| 560 | |
| 561 | |
| 562 | let next = self |
| 563 | .selected |
| 564 | .and_then(|sel| under.iter().position(|&clip| clip == sel)) |
| 565 | .map_or(first, |at| under[(at + 1) % under.len()]); |
| 566 | self.select(next, cx); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | fn select_step(&mut self, forward: bool, cx: &mut Context<Self>) { |
| 573 | let clips = self |
| 574 | .selected |
| 575 | .zip(self.session.as_ref()) |
| 576 | .map_or(0, |((lane, _), session)| session.lane_clips(lane).len()); |
| 577 | match (self.selected, clips) { |
| 578 | |
| 579 | |
| 580 | (Some((lane, idx)), len) if len > 0 => { |
| 581 | let next = if forward { |
| 582 | (idx + 1) % len |
| 583 | } else { |
| 584 | (idx + len - 1) % len |
| 585 | }; |
| 586 | self.select((lane, next), cx); |
| 587 | } |
| 588 | _ => self.select_under_playhead(cx), |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | fn cycle_fit(&mut self, cx: &mut Context<Self>) { |
| 602 | if self.exporting().is_some() { |
| 603 | return; |
| 604 | } |
| 605 | let Some(session) = &mut self.session else { |
| 606 | self.notice = Some("no timeline to fit — open a file first".into()); |
| 607 | cx.notify(); |
| 608 | return; |
| 609 | }; |
| 610 | let target = self |
| 611 | .selected |
| 612 | .filter(|(lane, _)| lane.kind == LaneKind::Video) |
| 613 | .or_else(|| session.video_clip_at(session.now())); |
| 614 | let Some((lane, idx)) = target else { |
| 615 | self.notice = Some("no clip under the playhead to fit".into()); |
| 616 | cx.notify(); |
| 617 | return; |
| 618 | }; |
| 619 | let next = next_fit(session.fit_of(lane, idx)); |
| 620 | if session.set_fit(lane, idx, next) { |
| 621 | let (w, h) = session.resolution(); |
| 622 | self.notice = Some(format!("FIT POLICY: {} on {w}x{h}", fit_label(next)).into()); |
| 623 | self.reset_after_reseek(); |
| 624 | } |
| 625 | cx.notify(); |
| 626 | } |
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | fn cycle_resolution(&mut self, cx: &mut Context<Self>) { |
| 634 | if self.exporting().is_some() { |
| 635 | return; |
| 636 | } |
| 637 | let Some(session) = &mut self.session else { |
| 638 | self.notice = Some("no timeline to resize — open a file first".into()); |
| 639 | cx.notify(); |
| 640 | return; |
| 641 | }; |
| 642 | let (width, height) = next_resolution(session.resolution(), session.native_resolution()); |
| 643 | if session.set_resolution(width, height) { |
| 644 | self.notice = Some(format!("PROJECT: {width}x{height}").into()); |
| 645 | self.reset_after_reseek(); |
| 646 | } |
| 647 | cx.notify(); |
| 648 | } |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | fn open_color(&mut self, cx: &mut Context<Self>) { |
| 657 | if self.exporting().is_some() { |
| 658 | return; |
| 659 | } |
| 660 | let Some(session) = &self.session else { |
| 661 | self.notice = Some("no timeline to grade — open a file first".into()); |
| 662 | cx.notify(); |
| 663 | return; |
| 664 | }; |
| 665 | let target = self |
| 666 | .selected |
| 667 | .filter(|(lane, _)| lane.kind == LaneKind::Video) |
| 668 | .or_else(|| session.video_clip_at(session.now())); |
| 669 | match target { |
| 670 | Some(clip) => { |
| 671 | self.color_open = Some(clip); |
| 672 | self.color_band = 0; |
| 673 | |
| 674 | self.keys_open = false; |
| 675 | self.export_open = false; |
| 676 | self.context_menu = None; |
| 677 | } |
| 678 | None => self.notice = Some("no clip under the playhead to grade".into()), |
| 679 | } |
| 680 | cx.notify(); |
| 681 | } |
| 682 | |
| 683 | |
| 684 | |
| 685 | fn color_params(&self) -> ColorParams { |
| 686 | self.color_open |
| 687 | .zip(self.session.as_ref()) |
| 688 | .and_then(|((lane, idx), session)| session.color_of(lane, idx).copied()) |
| 689 | .unwrap_or_default() |
| 690 | } |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | fn set_color(&mut self, params: ColorParams, cx: &mut Context<Self>) { |
| 699 | let Some((lane, idx)) = self.color_open else { |
| 700 | return; |
| 701 | }; |
| 702 | let Some(session) = &mut self.session else { |
| 703 | return; |
| 704 | }; |
| 705 | let grade = Some(params).filter(|p| !p.is_identity()); |
| 706 | if session.set_color(lane, idx, grade) { |
| 707 | self.reset_after_reseek(); |
| 708 | } |
| 709 | cx.notify(); |
| 710 | } |
| 711 | |
| 712 | |
| 713 | |
| 714 | fn nudge_color(&mut self, steps: f32, cx: &mut Context<Self>) { |
| 715 | let mut params = self.color_params(); |
| 716 | let (_, low, high) = COLOR_BANDS[self.color_band]; |
| 717 | let value = band_mut(&mut params, self.color_band); |
| 718 | *value = (*value + steps * COLOR_STEP).clamp(low, high); |
| 719 | self.set_color(params, cx); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | fn modal(&self) -> bool { |
| 726 | self.keys_open |
| 727 | || self.export_open |
| 728 | || self.eq_open.is_some() |
| 729 | || self.color_open.is_some() |
| 730 | || self.exporting().is_some() |
| 731 | } |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | fn open_eq(&mut self, cx: &mut Context<Self>) { |
| 738 | if self.exporting().is_some() { |
| 739 | return; |
| 740 | } |
| 741 | let refusal = match (self.selected, &self.session) { |
| 742 | (_, None) => Some("NO TIMELINE — open a file first".to_string()), |
| 743 | (None, _) => Some(format!( |
| 744 | "NOTHING SELECTED — click an audio clip or press {}, then ask again", |
| 745 | self.keymap.display(ActionId::Select) |
| 746 | )), |
| 747 | (Some((lane, _)), _) if lane.kind != LaneKind::Audio => Some( |
| 748 | "NOT AN AUDIO CLIP — the equalizer works on the sound, so pick a clip in an audio lane".to_string(), |
| 749 | ), |
| 750 | _ => None, |
| 751 | }; |
| 752 | if let Some(refusal) = refusal { |
| 753 | self.notice = Some(refusal.into()); |
| 754 | cx.notify(); |
| 755 | return; |
| 756 | } |
| 757 | let (lane, idx) = self.selected.expect("checked above"); |
| 758 | let session = self.session.as_ref().expect("checked above"); |
| 759 | |
| 760 | |
| 761 | |
| 762 | self.eq_params = session |
| 763 | .eq_of(lane, idx) |
| 764 | .cloned() |
| 765 | .unwrap_or_else(EqParams::default_layout); |
| 766 | self.eq_band = 0; |
| 767 | self.eq_dragging = false; |
| 768 | self.eq_open = Some((lane, idx)); |
| 769 | |
| 770 | self.keys_open = false; |
| 771 | self.export_open = false; |
| 772 | self.context_menu = None; |
| 773 | cx.notify(); |
| 774 | } |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | fn commit_eq(&mut self, cx: &mut Context<Self>) { |
| 784 | let Some((lane, idx)) = self.eq_open else { |
| 785 | return; |
| 786 | }; |
| 787 | let params = (!self.eq_params.is_identity()).then(|| self.eq_params.clone()); |
| 788 | if let Some(session) = &mut self.session { |
| 789 | session.set_eq(lane, idx, params); |
| 790 | } |
| 791 | |
| 792 | |
| 793 | self.reset_after_reseek(); |
| 794 | cx.notify(); |
| 795 | } |
| 796 | |
| 797 | |
| 798 | |
| 799 | fn set_band(&mut self, band: usize, gain_db: f32) -> bool { |
| 800 | let gain_db = gain_db.clamp(-EQ_GAIN_LIMIT, EQ_GAIN_LIMIT); |
| 801 | match self.eq_params.bands.get_mut(band) { |
| 802 | Some(b) if b.gain_db != gain_db => { |
| 803 | b.gain_db = gain_db; |
| 804 | true |
| 805 | } |
| 806 | _ => false, |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | |
| 811 | |
| 812 | fn nudge_band(&mut self, by: f32, cx: &mut Context<Self>) { |
| 813 | let gain = self |
| 814 | .eq_params |
| 815 | .bands |
| 816 | .get(self.eq_band) |
| 817 | .map_or(0., |b| b.gain_db); |
| 818 | if self.set_band(self.eq_band, gain + by) { |
| 819 | self.commit_eq(cx); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | |
| 824 | |
| 825 | fn drag_band(&mut self, x: Pixels, cx: &mut Context<Self>) { |
| 826 | let gain = (frac_along(x, self.eq_bar.get()) * 2. - 1.) * EQ_GAIN_LIMIT; |
| 827 | if self.set_band(self.eq_band, gain) { |
| 828 | cx.notify(); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | |
| 833 | fn seek(&mut self, t: f64, cx: &mut Context<Self>) { |
| 834 | if self.exporting().is_some() { |
| 835 | return; |
| 836 | } |
| 837 | let Some(session) = &mut self.session else { |
| 838 | return; |
| 839 | }; |
| 840 | session.seek(t); |
| 841 | self.reset_after_reseek(); |
| 842 | cx.notify(); |
| 843 | } |
| 844 | |
| 845 | |
| 846 | |
| 847 | fn cut(&mut self, cx: &mut Context<Self>) { |
| 848 | if self.exporting().is_some() { |
| 849 | return; |
| 850 | } |
| 851 | if let Some(session) = &mut self.session { |
| 852 | session.cut_at(session.now()); |
| 853 | } |
| 854 | self.selected = None; |
| 855 | cx.notify(); |
| 856 | } |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | fn regroup(&mut self, cx: &mut Context<Self>) { |
| 864 | if self.exporting().is_some() { |
| 865 | return; |
| 866 | } |
| 867 | if let Some(session) = &mut self.session { |
| 868 | if session.regroup_at(session.now()) { |
| 869 | self.selected = None; |
| 870 | } else { |
| 871 | self.notice = Some( |
| 872 | "NOTHING TO REGROUP — put the playhead where two clips meet, on frames that were cut apart" |
| 873 | .into(), |
| 874 | ); |
| 875 | } |
| 876 | } |
| 877 | cx.notify(); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | fn delete_selected(&mut self, cx: &mut Context<Self>) { |
| 886 | if self.exporting().is_some() { |
| 887 | return; |
| 888 | } |
| 889 | let selected = self.selected.take(); |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | let deleted = match (&mut self.session, selected) { |
| 896 | (Some(session), Some((lane, idx))) => match whole_take(session, lane, idx) { |
| 897 | true => session.delete_clip(lane, idx), |
| 898 | false => session.lift_clip(lane, idx), |
| 899 | }, |
| 900 | _ => false, |
| 901 | }; |
| 902 | if selected.is_some() && !deleted { |
| 903 | self.notice = Some("NOTHING DELETED — the timeline cannot be emptied".into()); |
| 904 | } |
| 905 | if deleted { |
| 906 | self.reset_after_reseek(); |
| 907 | } |
| 908 | cx.notify(); |
| 909 | } |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | fn lift_selected(&mut self, cx: &mut Context<Self>) { |
| 915 | if self.exporting().is_some() { |
| 916 | return; |
| 917 | } |
| 918 | match (&mut self.session, self.selected.take()) { |
| 919 | (Some(session), Some((lane, idx))) => { |
| 920 | if session.lift_clip(lane, idx) { |
| 921 | self.reset_after_reseek(); |
| 922 | } else { |
| 923 | self.notice = Some("NOTHING LIFTED — the timeline cannot be emptied".into()); |
| 924 | } |
| 925 | } |
| 926 | (Some(_), None) => { |
| 927 | self.notice = Some("NOTHING LIFTED — click the half to remove first".into()) |
| 928 | } |
| 929 | (None, _) => {} |
| 930 | } |
| 931 | cx.notify(); |
| 932 | } |
| 933 | |
| 934 | |
| 935 | fn copy_selected(&mut self) { |
| 936 | let session = self.session.as_ref(); |
| 937 | |
| 938 | |
| 939 | |
| 940 | if let Some(clip) = self |
| 941 | .selected |
| 942 | .and_then(|(lane, idx)| session?.lane_clips(lane).get(idx).copied()) |
| 943 | { |
| 944 | self.clipboard = Some(clip); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
| 959 | |
| 960 | fn cache_media(&mut self, cx: &mut Context<Self>) { |
| 961 | let Some(session) = &self.session else { |
| 962 | return; |
| 963 | }; |
| 964 | |
| 965 | |
| 966 | |
| 967 | for path in unseen_paths(session.sources(), &self.streams) { |
| 968 | self.streams.insert(path.clone(), Vec::new()); |
| 969 | let probed = cx.background_executor().spawn({ |
| 970 | let path = path.clone(); |
| 971 | async move { engine::AudioSession::probe_streams(&path).unwrap_or_default() } |
| 972 | }); |
| 973 | cx.spawn(async move |this, cx| { |
| 974 | let probed = probed.await; |
| 975 | this.update(cx, |this, cx| { |
| 976 | this.streams.insert(path, probed); |
| 977 | cx.notify(); |
| 978 | }) |
| 979 | .ok(); |
| 980 | }) |
| 981 | .detach(); |
| 982 | } |
| 983 | for key in unseen_sources(session.sources(), &self.waves) { |
| 984 | self.waves.insert(key.clone(), Wave::Loading); |
| 985 | let decoded = cx.background_executor().spawn({ |
| 986 | let (path, stream) = key.clone(); |
| 987 | async move { |
| 988 | engine::waveform::peaks(&path, stream, WAVE_BPS) |
| 989 | .ok() |
| 990 | .flatten() |
| 991 | .map(|peaks| Arc::new(normalise(peaks))) |
| 992 | } |
| 993 | }); |
| 994 | cx.spawn(async move |this, cx| { |
| 995 | let decoded = decoded.await; |
| 996 | this.update(cx, |this, cx| { |
| 997 | this.waves.insert( |
| 998 | key, |
| 999 | match decoded { |
| 1000 | Some(peaks) => Wave::Peaks(peaks), |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | None => Wave::Silent, |
| 1005 | }, |
| 1006 | ); |
| 1007 | cx.notify(); |
| 1008 | }) |
| 1009 | .ok(); |
| 1010 | }) |
| 1011 | .detach(); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | fn selected_link(&self) -> Option<u32> { |
| 1017 | let (lane, idx) = self.selected?; |
| 1018 | self.session.as_ref()?.lane_clips(lane).get(idx)?.link |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | fn paste(&mut self, cx: &mut Context<Self>) { |
| 1025 | let pasted = match (&mut self.session, self.clipboard) { |
| 1026 | (Some(session), Some(clip)) => session.paste_at(session.now(), clip), |
| 1027 | _ => false, |
| 1028 | }; |
| 1029 | if pasted { |
| 1030 | self.selected = None; |
| 1031 | self.reset_after_reseek(); |
| 1032 | } |
| 1033 | cx.notify(); |
| 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | |
| 1043 | fn insert_source( |
| 1044 | &mut self, |
| 1045 | path: &Path, |
| 1046 | stream: usize, |
| 1047 | onto: Option<Lane>, |
| 1048 | cx: &mut Context<Self>, |
| 1049 | ) { |
| 1050 | if self.exporting().is_some() { |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | if engine::is_audio(path) && onto.is_some_and(|lane| lane.kind == LaneKind::Video) { |
| 1058 | let name = file_name(path); |
| 1059 | let lane = onto.expect("checked above").label(); |
| 1060 | self.notice = Some( |
| 1061 | format!("NOT ON {lane} — {name} has no picture; drop it on an audio lane").into(), |
| 1062 | ); |
| 1063 | cx.notify(); |
| 1064 | return; |
| 1065 | } |
| 1066 | let frames = self.session.as_ref().map_or(0, |session| { |
| 1067 | source_frames(lane_clips(session), session.sources(), path) |
| 1068 | }); |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | let placed = match (&mut self.session, frames) { |
| 1073 | (Some(session), 1..) => { |
| 1074 | session.place_stream_at(session.now(), path, stream, frames, onto) |
| 1075 | } |
| 1076 | _ => Ok(false), |
| 1077 | }; |
| 1078 | match placed { |
| 1079 | Ok(true) => { |
| 1080 | self.selected = None; |
| 1081 | self.reset_after_reseek(); |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | Err(e) => self.notice = Some(format!("NOTHING ADDED — {e}").into()), |
| 1086 | Ok(false) => { |
| 1087 | self.notice = Some( |
| 1088 | "NOTHING ADDED — no clip on the timeline plays from that file any more".into(), |
| 1089 | ) |
| 1090 | } |
| 1091 | } |
| 1092 | cx.notify(); |
| 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | fn timeline_audio(&self) -> Option<(u32, u16)> { |
| 1099 | let first = self.session.as_ref()?.sources().first()?; |
| 1100 | let info = self |
| 1101 | .streams |
| 1102 | .get(&first.path)? |
| 1103 | .iter() |
| 1104 | .find(|s| s.index == first.audio_stream)?; |
| 1105 | Some((info.sample_rate, info.channels)) |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | fn import(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { |
| 1113 | if self.exporting().is_some() { |
| 1114 | return; |
| 1115 | } |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | let text = match self.session.as_mut().map(|session| session.import(path)) { |
| 1120 | Some(Ok(())) => { |
| 1121 | self.reset_after_reseek(); |
| 1122 | format!("IMPORTED {}", file_name(path)) |
| 1123 | } |
| 1124 | Some(Err(e)) => format!("IMPORT FAILED: {e}"), |
| 1125 | None => self.open_media(path), |
| 1126 | }; |
| 1127 | eprintln!("{text}"); |
| 1128 | self.notice = Some(text.into()); |
| 1129 | cx.notify(); |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | |
| 1137 | fn open_media(&mut self, path: &std::path::Path) -> String { |
| 1138 | match PlaybackSession::open(path) { |
| 1139 | Ok(session) => { |
| 1140 | self.fps = session.meta().frame_rate; |
| 1141 | |
| 1142 | |
| 1143 | let silent = audio_notice(&session); |
| 1144 | self.session = Some(session); |
| 1145 | |
| 1146 | |
| 1147 | self.apply_volume(); |
| 1148 | |
| 1149 | |
| 1150 | self.export_path = retarget(&export_path(path), self.format); |
| 1151 | self.project_path = project_path(path); |
| 1152 | self.name = file_name(path).into(); |
| 1153 | self.reset_after_reseek(); |
| 1154 | format!("OPENED {}{}", file_name(path), silent.unwrap_or_default()) |
| 1155 | } |
| 1156 | Err(e) => format!("OPEN FAILED: {e}"), |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | fn pick_and_import(&mut self, cx: &mut Context<Self>) { |
| 1165 | if self.exporting().is_some() { |
| 1166 | return; |
| 1167 | } |
| 1168 | let picked = cx.background_executor().spawn(async { pick_file() }); |
| 1169 | cx.spawn(async move |this, cx| { |
| 1170 | let picked = picked.await; |
| 1171 | this.update(cx, |this, cx| match picked { |
| 1172 | |
| 1173 | |
| 1174 | Ok(Some(path)) if is_project(&path) => this.load_project(&path, cx), |
| 1175 | Ok(Some(path)) => this.import(&path, cx), |
| 1176 | |
| 1177 | Ok(None) => {} |
| 1178 | Err(text) => { |
| 1179 | eprintln!("{text}"); |
| 1180 | this.notice = Some(text.into()); |
| 1181 | cx.notify(); |
| 1182 | } |
| 1183 | }) |
| 1184 | .ok(); |
| 1185 | }) |
| 1186 | .detach(); |
| 1187 | } |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | fn load_project(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { |
| 1194 | if self.exporting().is_some() { |
| 1195 | return; |
| 1196 | } |
| 1197 | let text = match PlaybackSession::open_project(path) { |
| 1198 | Ok(session) => { |
| 1199 | self.fps = session.meta().frame_rate; |
| 1200 | let silent = audio_notice(&session); |
| 1201 | |
| 1202 | |
| 1203 | self.export_path = retarget(&export_path(&session.sources()[0].path), self.format); |
| 1204 | self.session = Some(session); |
| 1205 | self.apply_volume(); |
| 1206 | self.project_path = path.to_path_buf(); |
| 1207 | self.name = file_name(path).into(); |
| 1208 | |
| 1209 | |
| 1210 | self.clipboard = None; |
| 1211 | self.selected = None; |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | self.context_menu = None; |
| 1217 | self.eq_open = None; |
| 1218 | self.color_open = None; |
| 1219 | |
| 1220 | |
| 1221 | self.selected_asset = None; |
| 1222 | |
| 1223 | |
| 1224 | self.displayed = 0; |
| 1225 | self.dropped = 0; |
| 1226 | self.started = None; |
| 1227 | |
| 1228 | |
| 1229 | |
| 1230 | self.reset_after_reseek(); |
| 1231 | format!("LOADED {}{}", file_name(path), silent.unwrap_or_default()) |
| 1232 | } |
| 1233 | Err(e) => format!("OPEN FAILED: {e}"), |
| 1234 | }; |
| 1235 | eprintln!("{text}"); |
| 1236 | self.notice = Some(text.into()); |
| 1237 | cx.notify(); |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | fn save_project(&mut self, cx: &mut Context<Self>) { |
| 1243 | let saved = self |
| 1244 | .session |
| 1245 | .as_ref() |
| 1246 | .map(|session| session.save_project(&self.project_path)); |
| 1247 | let text = match saved { |
| 1248 | Some(Ok(())) => format!("SAVED {}", file_name(&self.project_path)), |
| 1249 | Some(Err(e)) => format!("SAVE FAILED: {e}"), |
| 1250 | None => "NOTHING TO SAVE — open a file first".to_string(), |
| 1251 | }; |
| 1252 | eprintln!("{text}"); |
| 1253 | self.notice = Some(text.into()); |
| 1254 | cx.notify(); |
| 1255 | } |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | fn add_lane(&mut self, kind: LaneKind, cx: &mut Context<Self>) { |
| 1262 | if self.exporting().is_some() { |
| 1263 | return; |
| 1264 | } |
| 1265 | match &mut self.session { |
| 1266 | Some(session) => { |
| 1267 | let lane = session.add_lane(kind); |
| 1268 | self.notice = Some( |
| 1269 | format!( |
| 1270 | "{} ADDED — drag a clip onto it, {} takes it back", |
| 1271 | lane.label(), |
| 1272 | self.keymap.display(ActionId::Undo) |
| 1273 | ) |
| 1274 | .into(), |
| 1275 | ); |
| 1276 | } |
| 1277 | None => self.notice = Some("NO TRACK ADDED — open a file first".into()), |
| 1278 | } |
| 1279 | cx.notify(); |
| 1280 | } |
| 1281 | |
| 1282 | fn undo(&mut self, cx: &mut Context<Self>) { |
| 1283 | if self.session.as_mut().is_some_and(PlaybackSession::undo) { |
| 1284 | self.reset_after_reseek(); |
| 1285 | } |
| 1286 | self.selected = None; |
| 1287 | cx.notify(); |
| 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | |
| 1297 | fn capture(&mut self, action: ActionId, key: &str, ctrl: bool) { |
| 1298 | let chord = keymap::Chord { |
| 1299 | key: key.to_string(), |
| 1300 | ctrl, |
| 1301 | }; |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | if !chord.bindable() { |
| 1308 | let text = format!("THAT KEY CANNOT BE BOUND — {}", chord.pretty()); |
| 1309 | eprintln!("{text}"); |
| 1310 | self.notice = Some(text.into()); |
| 1311 | return; |
| 1312 | } |
| 1313 | let text = match self.keymap.rebind_action(action, chord.clone()) { |
| 1314 | Ok(()) => { |
| 1315 | self.rebinding = None; |
| 1316 | match self.keymap.save() { |
| 1317 | Ok(()) => return, |
| 1318 | Err(e) => format!( |
| 1319 | "KEYBINDINGS NOT SAVED — {}: {e}", |
| 1320 | Keymap::config_path().display() |
| 1321 | ), |
| 1322 | } |
| 1323 | } |
| 1324 | Err(holder) => format!("ALREADY BOUND — {} is {}", chord.pretty(), holder.label()), |
| 1325 | }; |
| 1326 | eprintln!("{text}"); |
| 1327 | self.notice = Some(text.into()); |
| 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | |
| 1332 | |
| 1333 | fn scrub_to(&mut self, x: Pixels, commit: bool, cx: &mut Context<Self>) { |
| 1334 | let Some(session) = &self.session else { |
| 1335 | return; |
| 1336 | }; |
| 1337 | let t = f64::from(frac_along(x, self.ruler.get())) * session.timeline_duration(); |
| 1338 | let target = (t * self.fps) as u32; |
| 1339 | if commit || scrub_due(target, self.last_target, self.last_scrub.elapsed()) { |
| 1340 | self.last_target = target; |
| 1341 | self.last_scrub = Instant::now(); |
| 1342 | self.seek(t, cx); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | fn apply_volume(&self) { |
| 1354 | if let Some(session) = &self.session { |
| 1355 | session.set_gain(self.volume.gain()); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | fn set_volume(&mut self, change: impl FnOnce(&mut Volume), cx: &mut Context<Self>) { |
| 1363 | change(&mut self.volume); |
| 1364 | self.apply_volume(); |
| 1365 | cx.notify(); |
| 1366 | } |
| 1367 | |
| 1368 | fn toggle_or_restart(&mut self, cx: &mut Context<Self>) { |
| 1369 | if self.exporting().is_some() { |
| 1370 | return; |
| 1371 | } |
| 1372 | if self.done { |
| 1373 | self.seek(0., cx); |
| 1374 | if let Some(session) = &mut self.session { |
| 1375 | session.play(); |
| 1376 | } |
| 1377 | } else if let Some(session) = &mut self.session { |
| 1378 | session.toggle(); |
| 1379 | |
| 1380 | cx.notify(); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | fn exporting(&self) -> Option<&ExportHandle> { |
| 1387 | self.export.as_ref().filter(|_| !self.cancelling) |
| 1388 | } |
| 1389 | |
| 1390 | |
| 1391 | |
| 1392 | |
| 1393 | fn open_export(&mut self, cx: &mut Context<Self>) { |
| 1394 | if self.export.is_some() { |
| 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | |
| 1399 | if self.session.is_none() { |
| 1400 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); |
| 1401 | cx.notify(); |
| 1402 | return; |
| 1403 | } |
| 1404 | self.export_open = true; |
| 1405 | |
| 1406 | |
| 1407 | self.keys_open = false; |
| 1408 | self.rebinding = None; |
| 1409 | cx.notify(); |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | |
| 1415 | fn set_format(&mut self, format: Format) { |
| 1416 | |
| 1417 | |
| 1418 | if let (Format::Mp4, Some(why)) = (format, self.session.as_ref().and_then(mp4_refusal)) { |
| 1419 | self.notice = Some(format!("NOT MP4 — {why}").into()); |
| 1420 | return; |
| 1421 | } |
| 1422 | self.format = format; |
| 1423 | self.export_path = retarget(&self.export_path, format); |
| 1424 | } |
| 1425 | |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | |
| 1430 | fn pick_destination(&mut self, cx: &mut Context<Self>) { |
| 1431 | let default = self.export_path.clone(); |
| 1432 | let picked = cx |
| 1433 | .background_executor() |
| 1434 | .spawn(async move { pick_save(&default) }); |
| 1435 | cx.spawn(async move |this, cx| { |
| 1436 | let picked = picked.await; |
| 1437 | this.update(cx, |this, cx| { |
| 1438 | |
| 1439 | |
| 1440 | if this.export.is_some() { |
| 1441 | return; |
| 1442 | } |
| 1443 | match picked { |
| 1444 | |
| 1445 | |
| 1446 | Ok(Some(path)) => this.export_path = retarget(&path, this.format), |
| 1447 | |
| 1448 | Ok(None) => {} |
| 1449 | Err(text) => { |
| 1450 | eprintln!("{text}"); |
| 1451 | this.notice = Some(text.into()); |
| 1452 | } |
| 1453 | } |
| 1454 | cx.notify(); |
| 1455 | }) |
| 1456 | .ok(); |
| 1457 | }) |
| 1458 | .detach(); |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | |
| 1467 | fn start_export(&mut self, cx: &mut Context<Self>) { |
| 1468 | if self.export.is_some() { |
| 1469 | return; |
| 1470 | } |
| 1471 | let settings = export_settings(self.quality, self.custom_mbps, self.format); |
| 1472 | let Some(session) = &mut self.session else { |
| 1473 | self.notice = Some("NOTHING TO EXPORT — open a file first".into()); |
| 1474 | cx.notify(); |
| 1475 | return; |
| 1476 | }; |
| 1477 | session.pause(); |
| 1478 | self.export = Some(session.export_to_with(&self.export_path, &settings)); |
| 1479 | |
| 1480 | |
| 1481 | self.export_open = false; |
| 1482 | cx.notify(); |
| 1483 | } |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | fn cancel_export(&mut self) { |
| 1488 | if let Some(export) = &self.export { |
| 1489 | export.cancel(); |
| 1490 | self.cancelling = true; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | fn poll_export(&mut self) { |
| 1497 | let Some(result) = self.export.as_ref().and_then(ExportHandle::result) else { |
| 1498 | return; |
| 1499 | }; |
| 1500 | self.export = None; |
| 1501 | |
| 1502 | |
| 1503 | if std::mem::take(&mut self.cancelling) { |
| 1504 | return; |
| 1505 | } |
| 1506 | let text = match result { |
| 1507 | Ok(()) => format!("EXPORT DONE → {}", file_name(&self.export_path)), |
| 1508 | Err(e) => format!("EXPORT FAILED: {e}"), |
| 1509 | }; |
| 1510 | eprintln!("{text}"); |
| 1511 | self.notice = Some(text.into()); |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | impl Render for Player { |
| 1516 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 1517 | |
| 1518 | |
| 1519 | |
| 1520 | if let Some(session) = &mut self.session { |
| 1521 | session.tick(); |
| 1522 | } |
| 1523 | self.pump(window); |
| 1524 | self.poll_export(); |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | self.cache_media(cx); |
| 1529 | |
| 1530 | |
| 1531 | let title = window_title(&self.name); |
| 1532 | if title != self.titled { |
| 1533 | window.set_window_title(&title); |
| 1534 | self.titled = title; |
| 1535 | } |
| 1536 | |
| 1537 | let playing = self |
| 1538 | .session |
| 1539 | .as_ref() |
| 1540 | .is_some_and(PlaybackSession::is_playing); |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | |
| 1548 | if (playing && !self.done) || self.pending_seek || self.export.is_some() { |
| 1549 | window.request_animation_frame(); |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | |
| 1554 | let duration = self |
| 1555 | .session |
| 1556 | .as_ref() |
| 1557 | .map_or(0., PlaybackSession::timeline_duration); |
| 1558 | |
| 1559 | |
| 1560 | |
| 1561 | let position = if self.done { |
| 1562 | duration |
| 1563 | } else { |
| 1564 | self.session |
| 1565 | .as_ref() |
| 1566 | .map_or(0., PlaybackSession::now) |
| 1567 | .clamp(0., duration) |
| 1568 | }; |
| 1569 | |
| 1570 | div() |
| 1571 | .track_focus(&self.focus) |
| 1572 | .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| { |
| 1573 | |
| 1574 | |
| 1575 | if event.is_held { |
| 1576 | return; |
| 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | |
| 1581 | |
| 1582 | if this.notice.take().is_some() { |
| 1583 | cx.notify(); |
| 1584 | } |
| 1585 | let key = event.keystroke.key.as_str(); |
| 1586 | |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | if let Some(action) = this.rebinding { |
| 1591 | if key == ESCAPE { |
| 1592 | this.rebinding = None; |
| 1593 | } else if !is_bare_modifier(key) { |
| 1594 | this.capture(action, key, event.keystroke.modifiers.control); |
| 1595 | } |
| 1596 | cx.notify(); |
| 1597 | return; |
| 1598 | } |
| 1599 | |
| 1600 | |
| 1601 | |
| 1602 | |
| 1603 | let action = this.keymap.lookup(key, event.keystroke.modifiers.control); |
| 1604 | |
| 1605 | |
| 1606 | |
| 1607 | if this.exporting().is_some() { |
| 1608 | if cancels_export(key, action) { |
| 1609 | this.cancel_export(); |
| 1610 | } |
| 1611 | cx.notify(); |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | |
| 1616 | |
| 1617 | if this.keys_open { |
| 1618 | if key == ESCAPE { |
| 1619 | this.keys_open = false; |
| 1620 | cx.notify(); |
| 1621 | } |
| 1622 | return; |
| 1623 | } |
| 1624 | |
| 1625 | |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | |
| 1630 | if this.export_open { |
| 1631 | if key == ESCAPE { |
| 1632 | this.export_open = false; |
| 1633 | } else if key == "enter" { |
| 1634 | |
| 1635 | |
| 1636 | this.start_export(cx); |
| 1637 | } else if let Some(format) = format_key(key) { |
| 1638 | |
| 1639 | |
| 1640 | |
| 1641 | |
| 1642 | |
| 1643 | |
| 1644 | this.set_format(format); |
| 1645 | } else if let Ok(digit) = key.parse::<u32>() { |
| 1646 | this.custom_mbps = push_digit(this.custom_mbps, digit); |
| 1647 | this.quality = Quality::Custom; |
| 1648 | } else if key == "backspace" { |
| 1649 | this.custom_mbps /= 10; |
| 1650 | this.quality = Quality::Custom; |
| 1651 | } |
| 1652 | cx.notify(); |
| 1653 | return; |
| 1654 | } |
| 1655 | |
| 1656 | |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | if this.eq_open.is_some() { |
| 1661 | if key == ESCAPE { |
| 1662 | |
| 1663 | |
| 1664 | this.eq_open = None; |
| 1665 | this.eq_dragging = false; |
| 1666 | } else if key == "up" { |
| 1667 | this.nudge_band(EQ_STEP, cx); |
| 1668 | } else if key == "down" { |
| 1669 | this.nudge_band(-EQ_STEP, cx); |
| 1670 | } else if key == "r" { |
| 1671 | for band in &mut this.eq_params.bands { |
| 1672 | band.gain_db = 0.; |
| 1673 | } |
| 1674 | this.commit_eq(cx); |
| 1675 | } else if let Ok(digit) = key.parse::<usize>() { |
| 1676 | |
| 1677 | |
| 1678 | if let Some(band) = digit.checked_sub(1) |
| 1679 | && band < this.eq_params.bands.len() |
| 1680 | { |
| 1681 | this.eq_band = band; |
| 1682 | } |
| 1683 | } |
| 1684 | cx.notify(); |
| 1685 | return; |
| 1686 | } |
| 1687 | |
| 1688 | |
| 1689 | |
| 1690 | |
| 1691 | |
| 1692 | if this.color_open.is_some() { |
| 1693 | match color_key(key) { |
| 1694 | Some(ColorKey::Close) => this.color_open = None, |
| 1695 | Some(ColorKey::Band(step)) => { |
| 1696 | this.color_band = (this.color_band + step) % COLOR_BANDS.len(); |
| 1697 | } |
| 1698 | Some(ColorKey::Nudge(steps)) => this.nudge_color(steps, cx), |
| 1699 | Some(ColorKey::Reset) => { |
| 1700 | this.set_color(ColorParams::default(), cx); |
| 1701 | } |
| 1702 | None => {} |
| 1703 | } |
| 1704 | cx.notify(); |
| 1705 | return; |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | |
| 1711 | if this.context_menu.take().is_some() { |
| 1712 | cx.notify(); |
| 1713 | if key == ESCAPE { |
| 1714 | return; |
| 1715 | } |
| 1716 | } |
| 1717 | if let Some(action) = action { |
| 1718 | this.act(action, cx); |
| 1719 | } |
| 1720 | })) |
| 1721 | |
| 1722 | |
| 1723 | |
| 1724 | |
| 1725 | .on_drop(cx.listener(|this, paths: &gpui::ExternalPaths, _, cx| { |
| 1726 | |
| 1727 | |
| 1728 | |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | if this.modal() { |
| 1733 | return; |
| 1734 | } |
| 1735 | for path in paths.paths() { |
| 1736 | |
| 1737 | if is_project(path) { |
| 1738 | this.load_project(path, cx); |
| 1739 | } else { |
| 1740 | this.import(path, cx); |
| 1741 | } |
| 1742 | } |
| 1743 | })) |
| 1744 | |
| 1745 | |
| 1746 | |
| 1747 | .on_mouse_move(cx.listener(|this, event: &MouseMoveEvent, _, cx| { |
| 1748 | |
| 1749 | |
| 1750 | if this.eq_dragging { |
| 1751 | if event.pressed_button == Some(MouseButton::Left) { |
| 1752 | this.drag_band(event.position.x, cx); |
| 1753 | } else { |
| 1754 | |
| 1755 | |
| 1756 | |
| 1757 | this.eq_dragging = false; |
| 1758 | this.commit_eq(cx); |
| 1759 | } |
| 1760 | return; |
| 1761 | } |
| 1762 | if !this.scrubbing { |
| 1763 | return; |
| 1764 | } |
| 1765 | if event.pressed_button == Some(MouseButton::Left) { |
| 1766 | this.scrub_to(event.position.x, false, cx); |
| 1767 | } else { |
| 1768 | |
| 1769 | |
| 1770 | |
| 1771 | this.scrubbing = false; |
| 1772 | } |
| 1773 | })) |
| 1774 | .on_mouse_up( |
| 1775 | MouseButton::Left, |
| 1776 | cx.listener(|this, event: &MouseUpEvent, _, cx| { |
| 1777 | if std::mem::take(&mut this.eq_dragging) { |
| 1778 | |
| 1779 | |
| 1780 | this.drag_band(event.position.x, cx); |
| 1781 | this.commit_eq(cx); |
| 1782 | return; |
| 1783 | } |
| 1784 | if std::mem::take(&mut this.scrubbing) { |
| 1785 | this.scrub_to(event.position.x, true, cx); |
| 1786 | } |
| 1787 | }), |
| 1788 | ) |
| 1789 | .size_full() |
| 1790 | .flex() |
| 1791 | .flex_col() |
| 1792 | .bg(rgb(LETTERBOX)) |
| 1793 | .text_color(rgb(INK)) |
| 1794 | .text_size(px(13.)) |
| 1795 | .child( |
| 1796 | div() |
| 1797 | .flex_none() |
| 1798 | .h(px(HEADER_H)) |
| 1799 | .flex() |
| 1800 | .items_center() |
| 1801 | .px(px(12.)) |
| 1802 | .bg(rgb(CHROME)) |
| 1803 | .child(self.name.clone()), |
| 1804 | ) |
| 1805 | |
| 1806 | |
| 1807 | |
| 1808 | |
| 1809 | .child( |
| 1810 | div() |
| 1811 | .flex_1() |
| 1812 | .min_h(px(0.)) |
| 1813 | .flex() |
| 1814 | .child(self.library(library_w(f32::from(window.viewport_size().width)), cx)) |
| 1815 | .child( |
| 1816 | div() |
| 1817 | .flex_1() |
| 1818 | .min_w(px(0.)) |
| 1819 | .overflow_hidden() |
| 1820 | .flex() |
| 1821 | .justify_center() |
| 1822 | .items_center() |
| 1823 | .children( |
| 1824 | self.image |
| 1825 | .clone() |
| 1826 | .map(|i| { |
| 1827 | img(i) |
| 1828 | .size_full() |
| 1829 | .object_fit(gpui::ObjectFit::Contain) |
| 1830 | .into_any_element() |
| 1831 | }) |
| 1832 | |
| 1833 | |
| 1834 | |
| 1835 | |
| 1836 | |
| 1837 | .or_else(|| { |
| 1838 | self.session |
| 1839 | .is_none() |
| 1840 | .then(|| empty_hint().into_any_element()) |
| 1841 | }), |
| 1842 | ), |
| 1843 | ), |
| 1844 | ) |
| 1845 | |
| 1846 | |
| 1847 | .children(self.notice_bar(cx)) |
| 1848 | .child(self.panel(position, duration, playing, cx)) |
| 1849 | |
| 1850 | |
| 1851 | .children(self.context_card(window.viewport_size(), cx)) |
| 1852 | |
| 1853 | |
| 1854 | .children(self.keys_overlay(cx)) |
| 1855 | .children(self.export_card(cx)) |
| 1856 | .children(self.eq_card(cx)) |
| 1857 | .children(self.color_card(cx)) |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | impl Player { |
| 1862 | |
| 1863 | |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | |
| 1873 | fn library(&self, width: f32, cx: &mut Context<Self>) -> impl IntoElement { |
| 1874 | let exporting = self.exporting().is_some(); |
| 1875 | let sources = self |
| 1876 | .session |
| 1877 | .as_ref() |
| 1878 | .map_or(&[][..], PlaybackSession::sources); |
| 1879 | |
| 1880 | |
| 1881 | |
| 1882 | let meta = self.session.as_ref().map(PlaybackSession::meta); |
| 1883 | let clips: Vec<Clip> = self |
| 1884 | .session |
| 1885 | .as_ref() |
| 1886 | .map_or_else(Vec::new, |session| lane_clips(session).copied().collect()); |
| 1887 | let rows: Vec<_> = library_rows(sources, &self.streams, self.timeline_audio(), |path| { |
| 1888 | source_frames(clips.iter(), sources, path) |
| 1889 | }) |
| 1890 | .into_iter() |
| 1891 | .enumerate() |
| 1892 | .map(|(i, row)| { |
| 1893 | let picked = self |
| 1894 | .selected_asset |
| 1895 | .as_ref() |
| 1896 | .is_some_and(|p| *p == (row.path.clone(), row.stream)); |
| 1897 | let name: SharedString = row.name.clone().into(); |
| 1898 | let tip: SharedString = match (&row.unusable, meta) { |
| 1899 | |
| 1900 | |
| 1901 | (Some(why), _) => format!("{} — {why}", row.path.display()), |
| 1902 | |
| 1903 | |
| 1904 | |
| 1905 | (None, _) if engine::is_audio(&row.path) => format!( |
| 1906 | "{} — audio only · drag onto the audio lane, or Add at playhead", |
| 1907 | row.path.display() |
| 1908 | ), |
| 1909 | (None, Some(meta)) => format!( |
| 1910 | "{} — {}x{} @ {:.2} fps · drag onto a lane, or Add at playhead", |
| 1911 | row.path.display(), |
| 1912 | meta.width, |
| 1913 | meta.height, |
| 1914 | meta.frame_rate |
| 1915 | ), |
| 1916 | (None, None) => row.path.display().to_string(), |
| 1917 | } |
| 1918 | .into(); |
| 1919 | let ghost = name.clone(); |
| 1920 | |
| 1921 | |
| 1922 | let under = match &row.unusable { |
| 1923 | Some(why) => join_detail(&row.detail, why), |
| 1924 | None => join_detail( |
| 1925 | &row.detail, |
| 1926 | &timecode(f64::from(row.frames) / self.fps, self.fps), |
| 1927 | ), |
| 1928 | }; |
| 1929 | let usable = row.unusable.is_none(); |
| 1930 | let (path, stream) = (row.path.clone(), row.stream); |
| 1931 | let dragged = (path.clone(), stream); |
| 1932 | div() |
| 1933 | .id(("asset", i)) |
| 1934 | .flex_none() |
| 1935 | .h(px(ROW_H)) |
| 1936 | .flex() |
| 1937 | .items_center() |
| 1938 | .gap(px(6.)) |
| 1939 | .pr(px(6.)) |
| 1940 | .rounded(px(3.)) |
| 1941 | |
| 1942 | |
| 1943 | .when(!usable, |d| d.text_color(rgb(INK_DIM)).opacity(0.55)) |
| 1944 | .when(usable, |d| { |
| 1945 | d.cursor_pointer() |
| 1946 | .hover(|s| s.bg(rgb(HOVER))) |
| 1947 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 1948 | this.selected_asset = Some((path.clone(), stream)); |
| 1949 | cx.notify(); |
| 1950 | })) |
| 1951 | |
| 1952 | |
| 1953 | |
| 1954 | |
| 1955 | .on_drag(AssetDrag(dragged.0, dragged.1), move |_, _, _, cx| { |
| 1956 | cx.new(|_| Tip(ghost.clone())) |
| 1957 | }) |
| 1958 | }) |
| 1959 | .when(picked, |d| d.bg(rgb(SELECTED)).border_1()) |
| 1960 | .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) |
| 1961 | |
| 1962 | |
| 1963 | |
| 1964 | .child( |
| 1965 | div() |
| 1966 | .flex_none() |
| 1967 | .w(px(SWATCH_W)) |
| 1968 | .h_full() |
| 1969 | .rounded(px(2.)) |
| 1970 | .bg(rgb(source_tint(row.tint))), |
| 1971 | ) |
| 1972 | .child( |
| 1973 | div() |
| 1974 | .flex_1() |
| 1975 | .min_w(px(0.)) |
| 1976 | .flex() |
| 1977 | .flex_col() |
| 1978 | |
| 1979 | |
| 1980 | |
| 1981 | .child(div().truncate().text_size(px(11.)).child(name)) |
| 1982 | .child( |
| 1983 | div() |
| 1984 | .truncate() |
| 1985 | .text_size(px(10.)) |
| 1986 | .text_color(rgb(INK_DIM)) |
| 1987 | .child(under), |
| 1988 | ), |
| 1989 | ) |
| 1990 | }) |
| 1991 | .collect(); |
| 1992 | div() |
| 1993 | .flex_none() |
| 1994 | .w(px(width)) |
| 1995 | .h_full() |
| 1996 | .flex() |
| 1997 | .flex_col() |
| 1998 | .gap(px(6.)) |
| 1999 | .p(px(8.)) |
| 2000 | .bg(rgb(CHROME)) |
| 2001 | .child( |
| 2002 | div() |
| 2003 | .flex_none() |
| 2004 | .flex() |
| 2005 | .items_center() |
| 2006 | .gap(px(6.)) |
| 2007 | .child( |
| 2008 | div() |
| 2009 | .flex_1() |
| 2010 | .min_w(px(0.)) |
| 2011 | .truncate() |
| 2012 | .text_size(px(11.)) |
| 2013 | .text_color(rgb(INK_DIM)) |
| 2014 | .child("Media"), |
| 2015 | ) |
| 2016 | .child(control( |
| 2017 | "import", |
| 2018 | None, |
| 2019 | "Import", |
| 2020 | "opens a file chooser — or drop a file on the window".to_string(), |
| 2021 | !exporting, |
| 2022 | cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_import(cx)), |
| 2023 | )), |
| 2024 | ) |
| 2025 | .child( |
| 2026 | div() |
| 2027 | .id("library-rows") |
| 2028 | .flex_1() |
| 2029 | .min_h(px(0.)) |
| 2030 | .flex() |
| 2031 | .flex_col() |
| 2032 | .gap(px(2.)) |
| 2033 | .overflow_y_scroll() |
| 2034 | |
| 2035 | |
| 2036 | .when(rows.is_empty(), |d| { |
| 2037 | d.child( |
| 2038 | div() |
| 2039 | .text_size(px(11.)) |
| 2040 | .text_color(rgb(INK_DIM)) |
| 2041 | .child("No media yet — Import, or drop a file on the window"), |
| 2042 | ) |
| 2043 | }) |
| 2044 | .children(rows), |
| 2045 | ) |
| 2046 | .child(control( |
| 2047 | "add-asset", |
| 2048 | None, |
| 2049 | "Add at playhead", |
| 2050 | match self.selected_asset { |
| 2051 | Some(_) => "inserts the picked file at the playhead".to_string(), |
| 2052 | None => "click a file above first — or drag one onto a lane".to_string(), |
| 2053 | }, |
| 2054 | can_add( |
| 2055 | self.selected_asset.as_ref(), |
| 2056 | self.session.is_some(), |
| 2057 | exporting, |
| 2058 | ), |
| 2059 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2060 | if let Some((path, stream)) = this.selected_asset.clone() { |
| 2061 | |
| 2062 | |
| 2063 | this.insert_source(&path, stream, None, cx); |
| 2064 | } |
| 2065 | }), |
| 2066 | )) |
| 2067 | } |
| 2068 | |
| 2069 | |
| 2070 | fn panel( |
| 2071 | &self, |
| 2072 | position: f64, |
| 2073 | duration: f64, |
| 2074 | playing: bool, |
| 2075 | cx: &mut Context<Self>, |
| 2076 | ) -> impl IntoElement { |
| 2077 | let filled = if duration > 0. { |
| 2078 | (position / duration) as f32 |
| 2079 | } else { |
| 2080 | 0. |
| 2081 | }; |
| 2082 | |
| 2083 | |
| 2084 | |
| 2085 | let exporting = self.exporting().is_some(); |
| 2086 | |
| 2087 | |
| 2088 | let live = self.session.is_some() && !exporting; |
| 2089 | let key = |action| self.keymap.display(action); |
| 2090 | |
| 2091 | |
| 2092 | let lanes = self |
| 2093 | .session |
| 2094 | .as_ref() |
| 2095 | .map_or_else(|| vec![Lane::V1, Lane::A1], PlaybackSession::lanes); |
| 2096 | |
| 2097 | |
| 2098 | let mut rows = Vec::new(); |
| 2099 | for &lane in &lanes { |
| 2100 | rows.push(self.lane_row(lane, duration, filled, cx)); |
| 2101 | } |
| 2102 | let (hint, filled) = if let Some(export) = self.exporting() { |
| 2103 | let progress = export.progress(); |
| 2104 | ( |
| 2105 | format!( |
| 2106 | "EXPORTING {}% — {} cancels", |
| 2107 | (progress * 100.) as u32, |
| 2108 | key(ActionId::CancelExport) |
| 2109 | ), |
| 2110 | progress, |
| 2111 | ) |
| 2112 | } else { |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | |
| 2117 | ( |
| 2118 | format!( |
| 2119 | "{} copy · {} paste · {} undo · click the bar to seek · drop a file to import", |
| 2120 | key(ActionId::Copy), |
| 2121 | key(ActionId::Paste), |
| 2122 | key(ActionId::Undo) |
| 2123 | ), |
| 2124 | filled, |
| 2125 | ) |
| 2126 | }; |
| 2127 | div() |
| 2128 | .flex_none() |
| 2129 | .h(px(panel_h(lanes.len()))) |
| 2130 | .flex() |
| 2131 | .flex_col() |
| 2132 | .gap(px(8.)) |
| 2133 | .px(px(12.)) |
| 2134 | .py(px(8.)) |
| 2135 | .bg(rgb(CHROME)) |
| 2136 | |
| 2137 | |
| 2138 | .child( |
| 2139 | div() |
| 2140 | .flex_none() |
| 2141 | .flex() |
| 2142 | .items_center() |
| 2143 | .gap(px(8.)) |
| 2144 | .child(control( |
| 2145 | "transport", |
| 2146 | Some(transport_glyph(playing).into_any_element()), |
| 2147 | if playing { "Pause" } else { "Play" }, |
| 2148 | key(ActionId::Play), |
| 2149 | live, |
| 2150 | cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_or_restart(cx)), |
| 2151 | )) |
| 2152 | .child(separator()) |
| 2153 | .child(control( |
| 2154 | "cut", |
| 2155 | Some(cut_glyph().into_any_element()), |
| 2156 | "Cut", |
| 2157 | format!( |
| 2158 | "{} — splits the clip under the playhead", |
| 2159 | key(ActionId::Cut) |
| 2160 | ), |
| 2161 | live, |
| 2162 | cx.listener(|this, _: &ClickEvent, _, cx| this.cut(cx)), |
| 2163 | )) |
| 2164 | .child(control( |
| 2165 | "delete", |
| 2166 | Some(delete_glyph().into_any_element()), |
| 2167 | "Delete", |
| 2168 | if self.selected.is_some() { |
| 2169 | key(ActionId::Delete) |
| 2170 | } else { |
| 2171 | format!("{} — click a clip below first", key(ActionId::Delete)) |
| 2172 | }, |
| 2173 | live && self.selected.is_some(), |
| 2174 | cx.listener(|this, _: &ClickEvent, _, cx| this.delete_selected(cx)), |
| 2175 | )) |
| 2176 | |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | .child(control( |
| 2181 | "add-video-lane", |
| 2182 | None, |
| 2183 | "+ V", |
| 2184 | format!( |
| 2185 | "{} — adds a video track under the ones there", |
| 2186 | key(ActionId::AddVideoLane) |
| 2187 | ), |
| 2188 | live, |
| 2189 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2190 | this.add_lane(LaneKind::Video, cx) |
| 2191 | }), |
| 2192 | )) |
| 2193 | .child(control( |
| 2194 | "add-audio-lane", |
| 2195 | None, |
| 2196 | "+ A", |
| 2197 | format!( |
| 2198 | "{} — adds an audio track under the ones there", |
| 2199 | key(ActionId::AddAudioLane) |
| 2200 | ), |
| 2201 | live, |
| 2202 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2203 | this.add_lane(LaneKind::Audio, cx) |
| 2204 | }), |
| 2205 | )) |
| 2206 | |
| 2207 | |
| 2208 | |
| 2209 | .child(control( |
| 2210 | "volume", |
| 2211 | None, |
| 2212 | self.volume.label(), |
| 2213 | format!( |
| 2214 | "{} mutes · {} louder · {} quieter", |
| 2215 | key(ActionId::ToggleMute), |
| 2216 | key(ActionId::VolumeUp), |
| 2217 | key(ActionId::VolumeDown) |
| 2218 | ), |
| 2219 | live, |
| 2220 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2221 | this.set_volume(|volume| volume.muted = !volume.muted, cx) |
| 2222 | }), |
| 2223 | )) |
| 2224 | .child(separator()) |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | .child(control( |
| 2229 | "export", |
| 2230 | None, |
| 2231 | "Export", |
| 2232 | format!( |
| 2233 | "{} — quality and destination, then writes the timeline out", |
| 2234 | key(ActionId::Export) |
| 2235 | ), |
| 2236 | live && self.export.is_none(), |
| 2237 | cx.listener(|this, _: &ClickEvent, _, cx| this.open_export(cx)), |
| 2238 | )) |
| 2239 | .child(control( |
| 2240 | "save", |
| 2241 | None, |
| 2242 | "Save", |
| 2243 | format!("{} — writes the project file", key(ActionId::Save)), |
| 2244 | live, |
| 2245 | cx.listener(|this, _: &ClickEvent, _, cx| this.save_project(cx)), |
| 2246 | )) |
| 2247 | |
| 2248 | |
| 2249 | |
| 2250 | .child(control( |
| 2251 | "keys", |
| 2252 | None, |
| 2253 | "Keys", |
| 2254 | "show and change the keybindings".to_string(), |
| 2255 | !exporting, |
| 2256 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2257 | this.keys_open = !this.keys_open; |
| 2258 | this.rebinding = None; |
| 2259 | |
| 2260 | this.export_open = false; |
| 2261 | cx.notify(); |
| 2262 | }), |
| 2263 | )), |
| 2264 | ) |
| 2265 | .child( |
| 2266 | div() |
| 2267 | .flex_none() |
| 2268 | .flex() |
| 2269 | .items_center() |
| 2270 | .gap(px(12.)) |
| 2271 | |
| 2272 | |
| 2273 | .child(div().flex_none().w(px(TIME_W)).truncate().child(format!( |
| 2274 | "{} / {}", |
| 2275 | timecode(position, self.fps), |
| 2276 | timecode(duration, self.fps) |
| 2277 | ))) |
| 2278 | .child( |
| 2279 | div() |
| 2280 | .flex_1() |
| 2281 | .min_w(px(0.)) |
| 2282 | .truncate() |
| 2283 | .text_size(px(11.)) |
| 2284 | .text_color(rgb(INK_DIM)) |
| 2285 | .child(hint), |
| 2286 | ), |
| 2287 | ) |
| 2288 | |
| 2289 | |
| 2290 | |
| 2291 | |
| 2292 | .child( |
| 2293 | div() |
| 2294 | .flex_none() |
| 2295 | .flex() |
| 2296 | .gap(px(HEADER_GAP)) |
| 2297 | |
| 2298 | |
| 2299 | |
| 2300 | .child(div().flex_none().w(px(HEADER_W))) |
| 2301 | .child( |
| 2302 | div() |
| 2303 | .id("ruler") |
| 2304 | .flex_1() |
| 2305 | .min_w(px(0.)) |
| 2306 | .h(px(RULER_HIT_H)) |
| 2307 | .flex() |
| 2308 | .flex_col() |
| 2309 | .justify_center() |
| 2310 | .rounded(px(3.)) |
| 2311 | .cursor_pointer() |
| 2312 | .hover(|s| s.bg(rgb(HOVER_DIM))) |
| 2313 | |
| 2314 | |
| 2315 | .tooltip(|_, cx| cx.new(|_| Tip("Seek — click or drag".into())).into()) |
| 2316 | .on_mouse_down( |
| 2317 | MouseButton::Left, |
| 2318 | cx.listener(|this, event: &MouseDownEvent, _, cx| { |
| 2319 | this.scrubbing = true; |
| 2320 | this.scrub_to(event.position.x, true, cx); |
| 2321 | }), |
| 2322 | ) |
| 2323 | .child( |
| 2324 | div() |
| 2325 | .w_full() |
| 2326 | .h(px(6.)) |
| 2327 | .rounded(px(3.)) |
| 2328 | .bg(rgb(SURFACE)) |
| 2329 | .child(bounds_probe(self.ruler.clone())) |
| 2330 | .child( |
| 2331 | div() |
| 2332 | .h_full() |
| 2333 | .w(relative(filled)) |
| 2334 | .rounded(px(3.)) |
| 2335 | .bg(rgb(ACCENT)), |
| 2336 | ), |
| 2337 | ), |
| 2338 | ), |
| 2339 | ) |
| 2340 | |
| 2341 | |
| 2342 | |
| 2343 | |
| 2344 | |
| 2345 | .child( |
| 2346 | div() |
| 2347 | .id("lanes") |
| 2348 | .flex_none() |
| 2349 | .flex() |
| 2350 | .flex_col() |
| 2351 | .gap(px(8.)) |
| 2352 | .max_h(px(lanes_h(LANES_MAX))) |
| 2353 | .overflow_y_scroll() |
| 2354 | .children(rows), |
| 2355 | ) |
| 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | |
| 2360 | |
| 2361 | |
| 2362 | fn notice_bar(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 2363 | let notice = self.notice.clone()?; |
| 2364 | Some( |
| 2365 | div() |
| 2366 | .id("notice") |
| 2367 | .flex_none() |
| 2368 | .flex() |
| 2369 | .items_start() |
| 2370 | .gap(px(12.)) |
| 2371 | .px(px(12.)) |
| 2372 | .py(px(6.)) |
| 2373 | .bg(rgb(SURFACE)) |
| 2374 | .cursor_pointer() |
| 2375 | .hover(|s| s.bg(rgb(HOVER))) |
| 2376 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2377 | this.notice = None; |
| 2378 | cx.notify(); |
| 2379 | })) |
| 2380 | .child(div().flex_1().min_w(px(0.)).child(notice)) |
| 2381 | .child( |
| 2382 | div() |
| 2383 | .flex_none() |
| 2384 | .text_size(px(11.)) |
| 2385 | .text_color(rgb(INK_DIM)) |
| 2386 | .child("click or press any key to dismiss"), |
| 2387 | ), |
| 2388 | ) |
| 2389 | } |
| 2390 | |
| 2391 | |
| 2392 | |
| 2393 | |
| 2394 | |
| 2395 | |
| 2396 | |
| 2397 | |
| 2398 | |
| 2399 | |
| 2400 | fn keys_overlay(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 2401 | if !self.keys_open { |
| 2402 | return None; |
| 2403 | } |
| 2404 | |
| 2405 | let out = keymap::Chord { |
| 2406 | key: ESCAPE.to_string(), |
| 2407 | ctrl: false, |
| 2408 | } |
| 2409 | .pretty(); |
| 2410 | |
| 2411 | |
| 2412 | |
| 2413 | |
| 2414 | |
| 2415 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 2416 | for category in keymap::Category::ALL { |
| 2417 | let actions = ActionId::ALL |
| 2418 | .into_iter() |
| 2419 | .enumerate() |
| 2420 | .filter(|(_, a)| a.category() == category); |
| 2421 | let fixed = keymap::FIXED.iter().filter(|f| f.category == category); |
| 2422 | let mut headed = false; |
| 2423 | let mut head = |rows: &mut Vec<AnyElement>| { |
| 2424 | if !std::mem::replace(&mut headed, true) { |
| 2425 | rows.push( |
| 2426 | div() |
| 2427 | .flex_none() |
| 2428 | .px(px(6.)) |
| 2429 | .pt(px(4.)) |
| 2430 | .text_size(px(11.)) |
| 2431 | .text_color(rgb(INK_DIM)) |
| 2432 | .child(category.label()) |
| 2433 | .into_any_element(), |
| 2434 | ); |
| 2435 | } |
| 2436 | }; |
| 2437 | for (i, action) in actions { |
| 2438 | head(&mut rows); |
| 2439 | let capturing = self.rebinding == Some(action); |
| 2440 | let out = out.clone(); |
| 2441 | rows.push( |
| 2442 | div() |
| 2443 | .id(("bind", i)) |
| 2444 | .flex() |
| 2445 | |
| 2446 | |
| 2447 | .min_h(px(KEYS_ROW_H)) |
| 2448 | .items_center() |
| 2449 | .justify_between() |
| 2450 | .gap(px(12.)) |
| 2451 | .px(px(6.)) |
| 2452 | .rounded(px(3.)) |
| 2453 | .cursor_pointer() |
| 2454 | .hover(|s| s.bg(rgb(HOVER))) |
| 2455 | .when(capturing, |d| d.bg(rgb(SELECTED))) |
| 2456 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 2457 | this.rebinding = Some(action); |
| 2458 | cx.notify(); |
| 2459 | })) |
| 2460 | .child(action.label()) |
| 2461 | .child(if capturing { |
| 2462 | div() |
| 2463 | .text_color(rgb(INK_DIM)) |
| 2464 | .child(format!("press a key — {out} cancels")) |
| 2465 | } else { |
| 2466 | div().child(self.keymap.display(action)) |
| 2467 | }) |
| 2468 | .into_any_element(), |
| 2469 | ); |
| 2470 | } |
| 2471 | for f in fixed { |
| 2472 | head(&mut rows); |
| 2473 | rows.push( |
| 2474 | div() |
| 2475 | .flex() |
| 2476 | .min_h(px(KEYS_ROW_H)) |
| 2477 | .items_center() |
| 2478 | .justify_between() |
| 2479 | .gap(px(12.)) |
| 2480 | .px(px(6.)) |
| 2481 | .child(f.label) |
| 2482 | |
| 2483 | .child(div().text_color(rgb(INK_DIM)).child(f.chord)) |
| 2484 | .into_any_element(), |
| 2485 | ); |
| 2486 | } |
| 2487 | } |
| 2488 | Some( |
| 2489 | div() |
| 2490 | .absolute() |
| 2491 | .inset_0() |
| 2492 | .flex() |
| 2493 | .justify_center() |
| 2494 | .items_center() |
| 2495 | |
| 2496 | |
| 2497 | .bg(rgba(0x101010cc)) |
| 2498 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 2499 | cx.stop_propagation() |
| 2500 | }) |
| 2501 | .child( |
| 2502 | div() |
| 2503 | .w(px(KEYS_W)) |
| 2504 | .flex() |
| 2505 | .flex_col() |
| 2506 | .gap(px(2.)) |
| 2507 | .p(px(12.)) |
| 2508 | .rounded(px(6.)) |
| 2509 | .bg(rgb(SURFACE)) |
| 2510 | |
| 2511 | |
| 2512 | |
| 2513 | .child(div().flex_none().px(px(6.)).child("Keybindings")) |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | |
| 2518 | |
| 2519 | |
| 2520 | |
| 2521 | |
| 2522 | .child( |
| 2523 | div() |
| 2524 | .flex_none() |
| 2525 | .px(px(6.)) |
| 2526 | .text_size(px(11.)) |
| 2527 | .text_color(rgb(INK_DIM)) |
| 2528 | .child( |
| 2529 | self.notice |
| 2530 | .clone() |
| 2531 | .unwrap_or_else(|| "click a row, then press a key".into()), |
| 2532 | ), |
| 2533 | ) |
| 2534 | |
| 2535 | |
| 2536 | |
| 2537 | .child( |
| 2538 | div() |
| 2539 | .id("keys-rows") |
| 2540 | .flex() |
| 2541 | .flex_col() |
| 2542 | .gap(px(2.)) |
| 2543 | .max_h(px(KEYS_ROWS_H)) |
| 2544 | .overflow_y_scroll() |
| 2545 | .children(rows), |
| 2546 | ), |
| 2547 | ), |
| 2548 | ) |
| 2549 | } |
| 2550 | |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | |
| 2556 | |
| 2557 | fn export_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 2558 | if !self.export_open { |
| 2559 | return None; |
| 2560 | } |
| 2561 | let row = |id: (&'static str, usize)| { |
| 2562 | div() |
| 2563 | .id(id) |
| 2564 | .flex() |
| 2565 | |
| 2566 | |
| 2567 | .min_h(px(KEYS_ROW_H)) |
| 2568 | .items_center() |
| 2569 | .justify_between() |
| 2570 | .gap(px(12.)) |
| 2571 | .px(px(6.)) |
| 2572 | .rounded(px(3.)) |
| 2573 | }; |
| 2574 | |
| 2575 | |
| 2576 | let live = |d: Stateful<Div>, enabled: bool| { |
| 2577 | d.when(!enabled, |d| d.opacity(0.4).cursor_not_allowed()) |
| 2578 | .when(enabled, |d| d.cursor_pointer().hover(|s| s.bg(rgb(HOVER)))) |
| 2579 | }; |
| 2580 | |
| 2581 | |
| 2582 | |
| 2583 | |
| 2584 | |
| 2585 | let refused = self.session.as_ref().and_then(mp4_refusal); |
| 2586 | let formats: Vec<_> = FORMATS |
| 2587 | .into_iter() |
| 2588 | .enumerate() |
| 2589 | .map(|(i, (format, label, detail))| { |
| 2590 | let blocked = format == Some(Format::Mp4) && refused.is_some(); |
| 2591 | let detail: SharedString = match &refused { |
| 2592 | Some(why) if blocked => why.clone().into(), |
| 2593 | _ => detail.into(), |
| 2594 | }; |
| 2595 | let picked = format == Some(self.format) && !blocked; |
| 2596 | let format = format.filter(|_| !blocked); |
| 2597 | live(row(("format", i)), format.is_some()) |
| 2598 | .when(picked, |d| d.bg(rgb(SELECTED))) |
| 2599 | .when_some(format, |d, format| { |
| 2600 | d.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 2601 | this.set_format(format); |
| 2602 | cx.notify(); |
| 2603 | })) |
| 2604 | }) |
| 2605 | .child(label) |
| 2606 | .child( |
| 2607 | div() |
| 2608 | .text_size(px(11.)) |
| 2609 | .text_color(rgb(INK_DIM)) |
| 2610 | .child(detail), |
| 2611 | ) |
| 2612 | }) |
| 2613 | .collect(); |
| 2614 | let video = self.format.has_video(); |
| 2615 | let rows: Vec<_> = Quality::ALL |
| 2616 | .into_iter() |
| 2617 | .enumerate() |
| 2618 | .map(|(i, quality)| { |
| 2619 | live(row(("quality", i)), video) |
| 2620 | .when(video && self.quality == quality, |d| d.bg(rgb(SELECTED))) |
| 2621 | .when(video, |d| { |
| 2622 | d.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 2623 | this.quality = quality; |
| 2624 | cx.notify(); |
| 2625 | })) |
| 2626 | }) |
| 2627 | .child(quality.label()) |
| 2628 | .child( |
| 2629 | div() |
| 2630 | .text_size(px(11.)) |
| 2631 | .text_color(rgb(INK_DIM)) |
| 2632 | .child(quality.detail(self.custom_mbps)), |
| 2633 | ) |
| 2634 | }) |
| 2635 | .collect(); |
| 2636 | Some( |
| 2637 | div() |
| 2638 | .absolute() |
| 2639 | .inset_0() |
| 2640 | .flex() |
| 2641 | .justify_center() |
| 2642 | .items_center() |
| 2643 | .bg(rgba(0x101010cc)) |
| 2644 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 2645 | cx.stop_propagation() |
| 2646 | }) |
| 2647 | .child( |
| 2648 | div() |
| 2649 | .w(px(KEYS_W)) |
| 2650 | .flex() |
| 2651 | .flex_col() |
| 2652 | .gap(px(2.)) |
| 2653 | .p(px(12.)) |
| 2654 | .rounded(px(6.)) |
| 2655 | .bg(rgb(SURFACE)) |
| 2656 | .child(div().flex_none().px(px(6.)).child("Export")) |
| 2657 | |
| 2658 | |
| 2659 | |
| 2660 | .child( |
| 2661 | div() |
| 2662 | .flex_none() |
| 2663 | .px(px(6.)) |
| 2664 | .text_size(px(11.)) |
| 2665 | .text_color(rgb(INK_DIM)) |
| 2666 | .child(self.notice.clone().unwrap_or_else(|| { |
| 2667 | "pick a format (m/w/f), then Export — esc closes".into() |
| 2668 | })), |
| 2669 | ) |
| 2670 | |
| 2671 | |
| 2672 | |
| 2673 | |
| 2674 | .child( |
| 2675 | div() |
| 2676 | .id("export-rows") |
| 2677 | .flex() |
| 2678 | .flex_col() |
| 2679 | .gap(px(2.)) |
| 2680 | .max_h(px(EXPORT_ROWS_H)) |
| 2681 | .overflow_y_scroll() |
| 2682 | .child( |
| 2683 | live(row(("destination", 0)), true) |
| 2684 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2685 | this.pick_destination(cx) |
| 2686 | })) |
| 2687 | .child("Destination") |
| 2688 | .child( |
| 2689 | div() |
| 2690 | .text_size(px(11.)) |
| 2691 | .text_color(rgb(INK_DIM)) |
| 2692 | .child(file_name(&self.export_path)), |
| 2693 | ), |
| 2694 | ) |
| 2695 | .children(formats) |
| 2696 | .children(rows), |
| 2697 | ) |
| 2698 | |
| 2699 | |
| 2700 | |
| 2701 | |
| 2702 | |
| 2703 | |
| 2704 | |
| 2705 | .child( |
| 2706 | div() |
| 2707 | .flex_none() |
| 2708 | .px(px(6.)) |
| 2709 | .text_size(px(11.)) |
| 2710 | .text_color(rgb(INK_DIM)) |
| 2711 | .child(export_line(self.format, self.session.as_ref())), |
| 2712 | ) |
| 2713 | .child( |
| 2714 | div() |
| 2715 | .id("export-confirm") |
| 2716 | .mt(px(4.)) |
| 2717 | .flex() |
| 2718 | .h(px(CONTROL_H)) |
| 2719 | .items_center() |
| 2720 | .justify_center() |
| 2721 | .rounded(px(3.)) |
| 2722 | .bg(rgb(SELECTED)) |
| 2723 | .cursor_pointer() |
| 2724 | .hover(|s| s.bg(rgb(HOVER))) |
| 2725 | .on_click( |
| 2726 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2727 | this.start_export(cx) |
| 2728 | }), |
| 2729 | ) |
| 2730 | .child("Export"), |
| 2731 | ), |
| 2732 | ), |
| 2733 | ) |
| 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | |
| 2738 | |
| 2739 | |
| 2740 | |
| 2741 | |
| 2742 | |
| 2743 | |
| 2744 | |
| 2745 | |
| 2746 | fn eq_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 2747 | let (lane, idx) = self.eq_open?; |
| 2748 | let rows: Vec<_> = self |
| 2749 | .eq_params |
| 2750 | .bands |
| 2751 | .iter() |
| 2752 | .enumerate() |
| 2753 | .map(|(i, band)| { |
| 2754 | let (left, width) = eq_fill(band.gain_db); |
| 2755 | div() |
| 2756 | .id(("eq-band", i)) |
| 2757 | .flex() |
| 2758 | .min_h(px(KEYS_ROW_H)) |
| 2759 | .items_center() |
| 2760 | .gap(px(12.)) |
| 2761 | .px(px(6.)) |
| 2762 | .rounded(px(3.)) |
| 2763 | .cursor_pointer() |
| 2764 | .when(i == self.eq_band, |d| d.bg(rgb(SELECTED))) |
| 2765 | .hover(|s| s.bg(rgb(HOVER))) |
| 2766 | |
| 2767 | |
| 2768 | .on_mouse_down( |
| 2769 | MouseButton::Left, |
| 2770 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { |
| 2771 | this.eq_band = i; |
| 2772 | this.eq_dragging = true; |
| 2773 | this.drag_band(event.position.x, cx); |
| 2774 | }), |
| 2775 | ) |
| 2776 | .child( |
| 2777 | div() |
| 2778 | .flex_none() |
| 2779 | |
| 2780 | |
| 2781 | .w(px(112.)) |
| 2782 | .text_size(px(11.)) |
| 2783 | .child(format!("{} {}", i + 1, band_label(band))), |
| 2784 | ) |
| 2785 | .child( |
| 2786 | div() |
| 2787 | .flex_1() |
| 2788 | .h(px(EQ_BAR_H)) |
| 2789 | .rounded(px(3.)) |
| 2790 | .bg(rgb(SURFACE)) |
| 2791 | .child(bounds_probe(self.eq_bar.clone())) |
| 2792 | .child( |
| 2793 | div() |
| 2794 | .absolute() |
| 2795 | .left(relative(left)) |
| 2796 | .w(relative(width)) |
| 2797 | .h_full() |
| 2798 | .rounded(px(3.)) |
| 2799 | .bg(rgb(ACCENT)), |
| 2800 | ), |
| 2801 | ) |
| 2802 | .child( |
| 2803 | div() |
| 2804 | .flex_none() |
| 2805 | |
| 2806 | |
| 2807 | .w(px(56.)) |
| 2808 | .text_size(px(11.)) |
| 2809 | .text_color(rgb(INK_DIM)) |
| 2810 | .child(format!("{:+.1} dB", band.gain_db)), |
| 2811 | ) |
| 2812 | }) |
| 2813 | .collect(); |
| 2814 | Some( |
| 2815 | div() |
| 2816 | .absolute() |
| 2817 | .inset_0() |
| 2818 | .flex() |
| 2819 | .justify_center() |
| 2820 | .items_center() |
| 2821 | .bg(rgba(0x101010cc)) |
| 2822 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 2823 | cx.stop_propagation() |
| 2824 | }) |
| 2825 | .child( |
| 2826 | div() |
| 2827 | .w(px(KEYS_W)) |
| 2828 | .flex() |
| 2829 | .flex_col() |
| 2830 | .gap(px(2.)) |
| 2831 | .p(px(12.)) |
| 2832 | .rounded(px(6.)) |
| 2833 | .bg(rgb(SURFACE)) |
| 2834 | |
| 2835 | |
| 2836 | .child(div().flex_none().px(px(6.)).child(format!( |
| 2837 | "Equalizer — {} clip {}", |
| 2838 | lane.label(), |
| 2839 | idx + 1 |
| 2840 | ))) |
| 2841 | .child( |
| 2842 | div() |
| 2843 | .flex_none() |
| 2844 | .px(px(6.)) |
| 2845 | .text_size(px(11.)) |
| 2846 | .text_color(rgb(INK_DIM)) |
| 2847 | .child(self.notice.clone().unwrap_or_else(|| { |
| 2848 | "drag a band, or 1–5 then up/down — esc closes".into() |
| 2849 | })), |
| 2850 | ) |
| 2851 | |
| 2852 | |
| 2853 | |
| 2854 | .child( |
| 2855 | div() |
| 2856 | .id("eq-rows") |
| 2857 | .flex() |
| 2858 | .flex_col() |
| 2859 | .gap(px(2.)) |
| 2860 | .max_h(px(EXPORT_ROWS_H)) |
| 2861 | .overflow_y_scroll() |
| 2862 | .children(rows), |
| 2863 | ) |
| 2864 | .child( |
| 2865 | div() |
| 2866 | .id("eq-reset") |
| 2867 | .mt(px(4.)) |
| 2868 | .flex() |
| 2869 | .h(px(CONTROL_H)) |
| 2870 | .items_center() |
| 2871 | .justify_center() |
| 2872 | .rounded(px(3.)) |
| 2873 | .bg(rgb(SELECTED)) |
| 2874 | .cursor_pointer() |
| 2875 | .hover(|s| s.bg(rgb(HOVER))) |
| 2876 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 2877 | for band in &mut this.eq_params.bands { |
| 2878 | band.gain_db = 0.; |
| 2879 | } |
| 2880 | this.commit_eq(cx); |
| 2881 | })) |
| 2882 | .child("Flatten"), |
| 2883 | ), |
| 2884 | ), |
| 2885 | ) |
| 2886 | } |
| 2887 | |
| 2888 | |
| 2889 | |
| 2890 | |
| 2891 | |
| 2892 | |
| 2893 | |
| 2894 | |
| 2895 | |
| 2896 | fn color_card(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 2897 | let (lane, idx) = self.color_open?; |
| 2898 | let params = self.color_params(); |
| 2899 | let rows: Vec<_> = COLOR_BANDS |
| 2900 | .iter() |
| 2901 | .enumerate() |
| 2902 | .map(|(i, &(label, low, high))| { |
| 2903 | let mut read = params; |
| 2904 | let value = *band_mut(&mut read, i); |
| 2905 | let frac = ((value - low) / (high - low)).clamp(0., 1.); |
| 2906 | let picked = i == self.color_band; |
| 2907 | |
| 2908 | |
| 2909 | let step = |name: &'static str, steps: f32| { |
| 2910 | div() |
| 2911 | .id((name, i)) |
| 2912 | .w(px(CONTROL_H)) |
| 2913 | .h(px(CONTROL_H)) |
| 2914 | .flex() |
| 2915 | .items_center() |
| 2916 | .justify_center() |
| 2917 | .rounded(px(3.)) |
| 2918 | .bg(rgb(CHROME)) |
| 2919 | .cursor_pointer() |
| 2920 | .hover(|s| s.bg(rgb(HOVER))) |
| 2921 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 2922 | this.color_band = i; |
| 2923 | this.nudge_color(steps, cx); |
| 2924 | })) |
| 2925 | .child(if steps < 0. { "–" } else { "+" }) |
| 2926 | }; |
| 2927 | div() |
| 2928 | .id(("color-row", i)) |
| 2929 | .flex() |
| 2930 | .min_h(px(KEYS_ROW_H)) |
| 2931 | .items_center() |
| 2932 | .gap(px(8.)) |
| 2933 | .px(px(6.)) |
| 2934 | .rounded(px(3.)) |
| 2935 | .cursor_pointer() |
| 2936 | .when(picked, |d| d.bg(rgb(SELECTED))) |
| 2937 | .hover(|s| s.bg(rgb(HOVER))) |
| 2938 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 2939 | this.color_band = i; |
| 2940 | cx.notify(); |
| 2941 | })) |
| 2942 | .child(div().flex_1().min_w(px(0.)).truncate().child(label)) |
| 2943 | .child(step("color-down", -1.)) |
| 2944 | .child( |
| 2945 | div() |
| 2946 | .w(px(COLOR_BAR_W)) |
| 2947 | .h(px(4.)) |
| 2948 | .rounded(px(2.)) |
| 2949 | .bg(rgb(CHROME)) |
| 2950 | .child( |
| 2951 | div() |
| 2952 | .h_full() |
| 2953 | .w(relative(frac)) |
| 2954 | .rounded(px(2.)) |
| 2955 | .bg(rgb(ACCENT)), |
| 2956 | ), |
| 2957 | ) |
| 2958 | .child(step("color-up", 1.)) |
| 2959 | .child( |
| 2960 | div() |
| 2961 | .w(px(44.)) |
| 2962 | .text_size(px(11.)) |
| 2963 | .text_color(rgb(INK_DIM)) |
| 2964 | .child(format!("{value:.2}")), |
| 2965 | ) |
| 2966 | }) |
| 2967 | .collect(); |
| 2968 | Some( |
| 2969 | div() |
| 2970 | .absolute() |
| 2971 | .inset_0() |
| 2972 | .flex() |
| 2973 | .justify_center() |
| 2974 | .items_center() |
| 2975 | .bg(rgba(0x101010cc)) |
| 2976 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 2977 | cx.stop_propagation() |
| 2978 | }) |
| 2979 | .child( |
| 2980 | div() |
| 2981 | .w(px(COLOR_W)) |
| 2982 | .flex() |
| 2983 | .flex_col() |
| 2984 | .gap(px(2.)) |
| 2985 | .p(px(12.)) |
| 2986 | .rounded(px(6.)) |
| 2987 | .bg(rgb(SURFACE)) |
| 2988 | .child(div().flex_none().px(px(6.)).child(format!( |
| 2989 | "Colour — {} clip {}", |
| 2990 | lane.label(), |
| 2991 | idx + 1 |
| 2992 | ))) |
| 2993 | .child( |
| 2994 | div() |
| 2995 | .flex_none() |
| 2996 | .px(px(6.)) |
| 2997 | .text_size(px(11.)) |
| 2998 | .text_color(rgb(INK_DIM)) |
| 2999 | .child("↑↓ picks a slider, ←→ moves it, r resets — esc closes"), |
| 3000 | ) |
| 3001 | .children(rows) |
| 3002 | .child( |
| 3003 | div() |
| 3004 | .id("color-reset") |
| 3005 | .mt(px(4.)) |
| 3006 | .flex() |
| 3007 | .h(px(CONTROL_H)) |
| 3008 | .items_center() |
| 3009 | .justify_center() |
| 3010 | .rounded(px(3.)) |
| 3011 | .bg(rgb(SELECTED)) |
| 3012 | .cursor_pointer() |
| 3013 | .hover(|s| s.bg(rgb(HOVER))) |
| 3014 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 3015 | this.set_color(ColorParams::default(), cx); |
| 3016 | })) |
| 3017 | .child("Reset"), |
| 3018 | ), |
| 3019 | ), |
| 3020 | ) |
| 3021 | } |
| 3022 | |
| 3023 | |
| 3024 | |
| 3025 | |
| 3026 | |
| 3027 | |
| 3028 | |
| 3029 | |
| 3030 | |
| 3031 | |
| 3032 | |
| 3033 | fn context_card( |
| 3034 | &self, |
| 3035 | viewport: Size<Pixels>, |
| 3036 | cx: &mut Context<Self>, |
| 3037 | ) -> Option<impl IntoElement> { |
| 3038 | let menu = self.context_menu?; |
| 3039 | let session = self.session.as_ref()?; |
| 3040 | let clip = *session.lane_clips(menu.lane).get(menu.idx)?; |
| 3041 | let playhead = frame_at(session.now(), self.fps); |
| 3042 | let source = session.sources().get(clip.source).cloned()?; |
| 3043 | let secs = |frames: u32| timecode(f64::from(frames) / self.fps, self.fps); |
| 3044 | let row = |n: usize| { |
| 3045 | div() |
| 3046 | .id(("menu", n)) |
| 3047 | .flex() |
| 3048 | |
| 3049 | |
| 3050 | .min_h(px(MENU_ROW_H)) |
| 3051 | .items_center() |
| 3052 | .justify_between() |
| 3053 | .gap(px(12.)) |
| 3054 | .px(px(6.)) |
| 3055 | .rounded(px(3.)) |
| 3056 | }; |
| 3057 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 3058 | if menu.details { |
| 3059 | |
| 3060 | |
| 3061 | |
| 3062 | for (label, value) in [ |
| 3063 | ("File", file_name(&source.path)), |
| 3064 | ("Path", source.path.display().to_string()), |
| 3065 | ( |
| 3066 | "Source range", |
| 3067 | format!("{} – {}", secs(clip.in_frame), secs(clip.out_frame)), |
| 3068 | ), |
| 3069 | ("This clip", secs(clip.len())), |
| 3070 | ( |
| 3071 | "Source duration", |
| 3072 | secs(source_frames( |
| 3073 | lane_clips(session), |
| 3074 | session.sources(), |
| 3075 | &source.path, |
| 3076 | )), |
| 3077 | ), |
| 3078 | ] { |
| 3079 | rows.push( |
| 3080 | row(rows.len()) |
| 3081 | .child(label) |
| 3082 | .child( |
| 3083 | div() |
| 3084 | .min_w(px(0.)) |
| 3085 | .truncate() |
| 3086 | |
| 3087 | |
| 3088 | |
| 3089 | .text_size(px(11.)) |
| 3090 | .text_color(rgb(INK_DIM)) |
| 3091 | .child(value), |
| 3092 | ) |
| 3093 | .into_any_element(), |
| 3094 | ); |
| 3095 | } |
| 3096 | } else { |
| 3097 | for action in MENU_ITEMS { |
| 3098 | let enabled = applicable(&clip, menu.lane, action, playhead); |
| 3099 | |
| 3100 | |
| 3101 | |
| 3102 | let label = if action == ActionId::ToggleMute { |
| 3103 | format!("{} (global)", action.label()) |
| 3104 | } else { |
| 3105 | action.label().to_string() |
| 3106 | }; |
| 3107 | rows.push( |
| 3108 | row(rows.len()) |
| 3109 | .child(label) |
| 3110 | .child( |
| 3111 | div() |
| 3112 | .text_color(rgb(INK_DIM)) |
| 3113 | .child(self.keymap.display(action)), |
| 3114 | ) |
| 3115 | .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed()) |
| 3116 | .when(enabled, |d| { |
| 3117 | d.cursor_pointer() |
| 3118 | .hover(|s| s.bg(rgb(HOVER))) |
| 3119 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 3120 | |
| 3121 | |
| 3122 | this.context_menu = None; |
| 3123 | this.act(action, cx); |
| 3124 | })) |
| 3125 | }) |
| 3126 | .into_any_element(), |
| 3127 | ); |
| 3128 | } |
| 3129 | rows.push( |
| 3130 | row(rows.len()) |
| 3131 | .cursor_pointer() |
| 3132 | .hover(|s| s.bg(rgb(HOVER))) |
| 3133 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 3134 | if let Some(menu) = &mut this.context_menu { |
| 3135 | menu.details = true; |
| 3136 | } |
| 3137 | cx.notify(); |
| 3138 | })) |
| 3139 | .child("Properties") |
| 3140 | |
| 3141 | |
| 3142 | .child(div().text_color(rgb(INK_DIM)).child("…")) |
| 3143 | .into_any_element(), |
| 3144 | ); |
| 3145 | } |
| 3146 | let (x, y) = menu_at( |
| 3147 | menu.at, |
| 3148 | viewport, |
| 3149 | MENU_PAD * 2. + rows.len() as f32 * MENU_ROW_H, |
| 3150 | ); |
| 3151 | let full: SharedString = source.path.display().to_string().into(); |
| 3152 | Some( |
| 3153 | div() |
| 3154 | .absolute() |
| 3155 | .inset_0() |
| 3156 | |
| 3157 | |
| 3158 | |
| 3159 | |
| 3160 | .on_mouse_down( |
| 3161 | MouseButton::Left, |
| 3162 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 3163 | this.context_menu = None; |
| 3164 | cx.notify(); |
| 3165 | cx.stop_propagation(); |
| 3166 | }), |
| 3167 | ) |
| 3168 | .on_mouse_down( |
| 3169 | MouseButton::Right, |
| 3170 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 3171 | this.context_menu = None; |
| 3172 | cx.notify(); |
| 3173 | cx.stop_propagation(); |
| 3174 | }), |
| 3175 | ) |
| 3176 | .child( |
| 3177 | div() |
| 3178 | |
| 3179 | |
| 3180 | .id("menu-card") |
| 3181 | .absolute() |
| 3182 | .left(px(x)) |
| 3183 | .top(px(y)) |
| 3184 | .w(px(MENU_W)) |
| 3185 | .flex() |
| 3186 | .flex_col() |
| 3187 | .p(px(MENU_PAD)) |
| 3188 | .rounded(px(6.)) |
| 3189 | .bg(rgb(SURFACE)) |
| 3190 | |
| 3191 | |
| 3192 | |
| 3193 | |
| 3194 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 3195 | cx.stop_propagation() |
| 3196 | }) |
| 3197 | .on_mouse_down(MouseButton::Right, |_: &MouseDownEvent, _, cx| { |
| 3198 | cx.stop_propagation() |
| 3199 | }) |
| 3200 | |
| 3201 | .when(menu.details, |d| { |
| 3202 | d.tooltip(move |_, cx| cx.new(|_| Tip(full.clone())).into()) |
| 3203 | }) |
| 3204 | .children(rows), |
| 3205 | ), |
| 3206 | ) |
| 3207 | } |
| 3208 | |
| 3209 | |
| 3210 | |
| 3211 | |
| 3212 | |
| 3213 | |
| 3214 | |
| 3215 | |
| 3216 | fn lane_row( |
| 3217 | &self, |
| 3218 | lane: Lane, |
| 3219 | duration: f64, |
| 3220 | filled: f32, |
| 3221 | cx: &mut Context<Self>, |
| 3222 | |
| 3223 | |
| 3224 | |
| 3225 | ) -> impl IntoElement + use<> { |
| 3226 | |
| 3227 | |
| 3228 | |
| 3229 | |
| 3230 | let bed_w = f32::from(self.ruler.get().size.width); |
| 3231 | let clips = self |
| 3232 | .session |
| 3233 | .as_ref() |
| 3234 | .map_or(&[][..], |session| session.lane_clips(lane)); |
| 3235 | |
| 3236 | |
| 3237 | |
| 3238 | let others: Vec<u32> = self.session.as_ref().map_or_else(Vec::new, |session| { |
| 3239 | session |
| 3240 | .lanes() |
| 3241 | .into_iter() |
| 3242 | .filter(|&other| other != lane) |
| 3243 | .flat_map(|other| session.lane_clips(other)) |
| 3244 | .filter_map(|clip| clip.link) |
| 3245 | .collect() |
| 3246 | }); |
| 3247 | let name = lane.label(); |
| 3248 | let row_id: SharedString = format!("{name}-clip").into(); |
| 3249 | let sources = self |
| 3250 | .session |
| 3251 | .as_ref() |
| 3252 | .map_or(&[][..], PlaybackSession::sources); |
| 3253 | let (sel, sel_link) = (self.selected, self.selected_link()); |
| 3254 | let audio = lane.kind == LaneKind::Audio; |
| 3255 | let tip: SharedString = format!( |
| 3256 | "Select (or {} under the playhead, {}/{} along the lane) — {} removes the take, {} leaves a gap, {} rejoins a cut", |
| 3257 | self.keymap.display(ActionId::Select), |
| 3258 | self.keymap.display(ActionId::SelectPrev), |
| 3259 | self.keymap.display(ActionId::SelectNext), |
| 3260 | self.keymap.display(ActionId::Delete), |
| 3261 | self.keymap.display(ActionId::Lift), |
| 3262 | self.keymap.display(ActionId::Regroup) |
| 3263 | ) |
| 3264 | .into(); |
| 3265 | div() |
| 3266 | .flex_none() |
| 3267 | .h(px(LANE_H)) |
| 3268 | .flex() |
| 3269 | .gap(px(HEADER_GAP)) |
| 3270 | |
| 3271 | |
| 3272 | .child( |
| 3273 | div() |
| 3274 | .flex_none() |
| 3275 | .w(px(HEADER_W)) |
| 3276 | .h_full() |
| 3277 | .flex() |
| 3278 | .items_center() |
| 3279 | .justify_center() |
| 3280 | .rounded(px(3.)) |
| 3281 | .bg(rgb(SURFACE)) |
| 3282 | .text_size(px(11.)) |
| 3283 | .text_color(rgb(INK_DIM)) |
| 3284 | .child(name), |
| 3285 | ) |
| 3286 | .child( |
| 3287 | |
| 3288 | |
| 3289 | |
| 3290 | div() |
| 3291 | .relative() |
| 3292 | .flex_1() |
| 3293 | .min_w(px(0.)) |
| 3294 | .h_full() |
| 3295 | .rounded(px(3.)) |
| 3296 | .bg(rgb(LETTERBOX)) |
| 3297 | .overflow_hidden() |
| 3298 | |
| 3299 | |
| 3300 | |
| 3301 | |
| 3302 | .on_drop(cx.listener(move |this, drag: &AssetDrag, _, cx| { |
| 3303 | this.insert_source(&drag.0.clone(), drag.1, Some(lane), cx) |
| 3304 | })) |
| 3305 | .drag_over::<AssetDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM))) |
| 3306 | .children(clips.iter().enumerate().map(|(i, clip)| { |
| 3307 | let (start, len) = ( |
| 3308 | f64::from(clip.start) / self.fps, |
| 3309 | f64::from(clip.len()) / self.fps, |
| 3310 | ); |
| 3311 | let on = marked((lane, i), clip.link, sel, sel_link); |
| 3312 | |
| 3313 | |
| 3314 | |
| 3315 | let grouped = clip.link.is_some_and(|link| others.contains(&link)); |
| 3316 | |
| 3317 | |
| 3318 | |
| 3319 | let tint = source_tint( |
| 3320 | sources |
| 3321 | .get(clip.source) |
| 3322 | .and_then(|s| sources.iter().position(|o| o.path == s.path)) |
| 3323 | .unwrap_or(clip.source), |
| 3324 | ); |
| 3325 | let width = width_frac(len, duration); |
| 3326 | let label = sources.get(clip.source).map(|s| file_name(&s.path)); |
| 3327 | let wave = sources |
| 3328 | .get(clip.source) |
| 3329 | .and_then(|s| self.waves.get(&(s.path.clone(), s.audio_stream))) |
| 3330 | .cloned(); |
| 3331 | let (from, to) = ( |
| 3332 | f64::from(clip.in_frame) / self.fps, |
| 3333 | f64::from(clip.out_frame) / self.fps, |
| 3334 | ); |
| 3335 | let tip = tip.clone(); |
| 3336 | div() |
| 3337 | |
| 3338 | |
| 3339 | .id((row_id.clone(), i)) |
| 3340 | .absolute() |
| 3341 | .top_0() |
| 3342 | .h_full() |
| 3343 | .left(relative(start_frac(start, duration))) |
| 3344 | .w(relative(width)) |
| 3345 | .overflow_hidden() |
| 3346 | .rounded(px(3.)) |
| 3347 | .border_1() |
| 3348 | .border_color(rgb(if on { |
| 3349 | ACCENT |
| 3350 | } else if grouped { |
| 3351 | tint |
| 3352 | } else { |
| 3353 | INK_DIM |
| 3354 | })) |
| 3355 | .bg(rgb(if on { SELECTED } else { tint })) |
| 3356 | .cursor_pointer() |
| 3357 | .hover(|s| s.border_color(rgb(ACCENT))) |
| 3358 | .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) |
| 3359 | .on_mouse_down( |
| 3360 | MouseButton::Left, |
| 3361 | cx.listener(move |this, _: &MouseDownEvent, _, cx| { |
| 3362 | this.select((lane, i), cx); |
| 3363 | }), |
| 3364 | ) |
| 3365 | |
| 3366 | |
| 3367 | |
| 3368 | |
| 3369 | .on_mouse_down( |
| 3370 | MouseButton::Right, |
| 3371 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { |
| 3372 | if this.modal() { |
| 3373 | return; |
| 3374 | } |
| 3375 | this.select((lane, i), cx); |
| 3376 | this.context_menu = Some(ContextMenu { |
| 3377 | lane, |
| 3378 | idx: i, |
| 3379 | at: event.position, |
| 3380 | details: false, |
| 3381 | }); |
| 3382 | cx.notify(); |
| 3383 | }), |
| 3384 | ) |
| 3385 | |
| 3386 | .children(wave.filter(|_| audio).and_then(|wave| { |
| 3387 | let inner: AnyElement = match wave { |
| 3388 | Wave::Peaks(peaks) => { |
| 3389 | waveform(peaks, from, to).into_any_element() |
| 3390 | } |
| 3391 | |
| 3392 | |
| 3393 | |
| 3394 | |
| 3395 | |
| 3396 | Wave::Loading => div() |
| 3397 | .h_full() |
| 3398 | .flex() |
| 3399 | .items_center() |
| 3400 | .child(div().w_full().h(px(1.)).bg(rgb(HOVER))) |
| 3401 | .into_any_element(), |
| 3402 | |
| 3403 | Wave::Silent => return None, |
| 3404 | }; |
| 3405 | Some( |
| 3406 | div() |
| 3407 | .absolute() |
| 3408 | .left_0() |
| 3409 | .right_0() |
| 3410 | .top(px(LABEL_H)) |
| 3411 | .bottom_0() |
| 3412 | .child(inner), |
| 3413 | ) |
| 3414 | })) |
| 3415 | .when_some(label.filter(|_| show_label(bed_w * width)), |d, label| { |
| 3416 | d.child( |
| 3417 | div() |
| 3418 | .relative() |
| 3419 | .h(px(LABEL_H)) |
| 3420 | .px(px(4.)) |
| 3421 | .truncate() |
| 3422 | .text_size(px(10.)) |
| 3423 | .child(label), |
| 3424 | ) |
| 3425 | }) |
| 3426 | })) |
| 3427 | |
| 3428 | |
| 3429 | .child( |
| 3430 | div() |
| 3431 | .absolute() |
| 3432 | .top_0() |
| 3433 | .h_full() |
| 3434 | .left(relative(filled)) |
| 3435 | .w(px(1.)) |
| 3436 | .bg(rgb(ACCENT)), |
| 3437 | ), |
| 3438 | ) |
| 3439 | } |
| 3440 | } |
| 3441 | |
| 3442 | |
| 3443 | |
| 3444 | |
| 3445 | const SCRUB_GAP: Duration = Duration::from_millis(100); |
| 3446 | |
| 3447 | fn scrub_due(target: u32, last_target: u32, since: Duration) -> bool { |
| 3448 | target != last_target && since >= SCRUB_GAP |
| 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | |
| 3453 | |
| 3454 | |
| 3455 | |
| 3456 | |
| 3457 | fn run_picker(pickers: [(&str, Vec<String>); 2]) -> Option<Option<PathBuf>> { |
| 3458 | for (bin, args) in pickers { |
| 3459 | |
| 3460 | |
| 3461 | let Ok(out) = std::process::Command::new(bin).args(args).output() else { |
| 3462 | continue; |
| 3463 | }; |
| 3464 | let path = String::from_utf8_lossy(&out.stdout).trim().to_string(); |
| 3465 | return Some((!path.is_empty()).then(|| PathBuf::from(path))); |
| 3466 | } |
| 3467 | None |
| 3468 | } |
| 3469 | |
| 3470 | fn pick_file() -> Result<Option<PathBuf>, &'static str> { |
| 3471 | run_picker([ |
| 3472 | ( |
| 3473 | "zenity", |
| 3474 | vec!["--file-selection".into(), "--title=edith — import".into()], |
| 3475 | ), |
| 3476 | ("kdialog", vec!["--getopenfilename".into()]), |
| 3477 | ]) |
| 3478 | .ok_or( |
| 3479 | "NO FILE CHOOSER — install zenity or kdialog, or drag the file onto this window to import it", |
| 3480 | ) |
| 3481 | } |
| 3482 | |
| 3483 | |
| 3484 | |
| 3485 | |
| 3486 | fn pick_save(default: &std::path::Path) -> Result<Option<PathBuf>, &'static str> { |
| 3487 | let default = default.to_string_lossy().into_owned(); |
| 3488 | run_picker([ |
| 3489 | ( |
| 3490 | "zenity", |
| 3491 | vec![ |
| 3492 | "--file-selection".into(), |
| 3493 | |
| 3494 | |
| 3495 | "--save".into(), |
| 3496 | "--title=edith — export to".into(), |
| 3497 | format!("--filename={default}"), |
| 3498 | ], |
| 3499 | ), |
| 3500 | ("kdialog", vec!["--getsavefilename".into(), default]), |
| 3501 | ]) |
| 3502 | .ok_or( |
| 3503 | "NO FILE CHOOSER — install zenity or kdialog to choose where; exporting beside the source", |
| 3504 | ) |
| 3505 | } |
| 3506 | |
| 3507 | |
| 3508 | |
| 3509 | fn export_path(source: impl Into<PathBuf>) -> PathBuf { |
| 3510 | let mut path = source.into(); |
| 3511 | path.set_extension("export.mp4"); |
| 3512 | path |
| 3513 | } |
| 3514 | |
| 3515 | |
| 3516 | |
| 3517 | |
| 3518 | |
| 3519 | fn project_path(source: impl Into<PathBuf>) -> PathBuf { |
| 3520 | let mut path = source.into(); |
| 3521 | path.set_extension("edith"); |
| 3522 | path |
| 3523 | } |
| 3524 | |
| 3525 | |
| 3526 | |
| 3527 | |
| 3528 | fn is_project(path: &std::path::Path) -> bool { |
| 3529 | path.extension().is_some_and(|e| e == "edith") |
| 3530 | } |
| 3531 | |
| 3532 | |
| 3533 | |
| 3534 | fn file_name(path: &std::path::Path) -> String { |
| 3535 | path.file_name().map_or_else( |
| 3536 | || path.display().to_string(), |
| 3537 | |n| n.to_string_lossy().into(), |
| 3538 | ) |
| 3539 | } |
| 3540 | |
| 3541 | |
| 3542 | |
| 3543 | |
| 3544 | fn audio_notice(session: &PlaybackSession) -> Option<String> { |
| 3545 | session |
| 3546 | .audio_disabled_reason() |
| 3547 | .map(|reason| format!(" — NO AUDIO: {reason}")) |
| 3548 | } |
| 3549 | |
| 3550 | |
| 3551 | |
| 3552 | |
| 3553 | fn unseen_sources( |
| 3554 | sources: &[Source], |
| 3555 | waves: &HashMap<(PathBuf, usize), Wave>, |
| 3556 | ) -> Vec<(PathBuf, usize)> { |
| 3557 | sources |
| 3558 | .iter() |
| 3559 | .map(|s| (s.path.clone(), s.audio_stream)) |
| 3560 | .filter(|key| !waves.contains_key(key)) |
| 3561 | .collect() |
| 3562 | } |
| 3563 | |
| 3564 | |
| 3565 | |
| 3566 | fn unseen_paths(sources: &[Source], streams: &HashMap<PathBuf, Vec<StreamInfo>>) -> Vec<PathBuf> { |
| 3567 | let mut out: Vec<PathBuf> = Vec::new(); |
| 3568 | for s in sources { |
| 3569 | if !streams.contains_key(&s.path) && !out.contains(&s.path) { |
| 3570 | out.push(s.path.clone()); |
| 3571 | } |
| 3572 | } |
| 3573 | out |
| 3574 | } |
| 3575 | |
| 3576 | |
| 3577 | |
| 3578 | |
| 3579 | fn lane_clips(session: &PlaybackSession) -> impl Iterator<Item = &Clip> { |
| 3580 | session |
| 3581 | .lanes() |
| 3582 | .into_iter() |
| 3583 | .flat_map(|lane| session.lane_clips(lane)) |
| 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | |
| 3588 | |
| 3589 | |
| 3590 | |
| 3591 | |
| 3592 | |
| 3593 | |
| 3594 | |
| 3595 | |
| 3596 | |
| 3597 | |
| 3598 | |
| 3599 | fn source_frames<'a>( |
| 3600 | clips: impl Iterator<Item = &'a Clip>, |
| 3601 | sources: &[Source], |
| 3602 | path: &Path, |
| 3603 | ) -> u32 { |
| 3604 | clips |
| 3605 | .filter(|clip| sources.get(clip.source).is_some_and(|s| s.path == path)) |
| 3606 | .map(|clip| clip.out_frame) |
| 3607 | .max() |
| 3608 | .unwrap_or(0) |
| 3609 | } |
| 3610 | |
| 3611 | |
| 3612 | |
| 3613 | |
| 3614 | |
| 3615 | |
| 3616 | fn frame_at(secs: f64, fps: f64) -> u32 { |
| 3617 | (secs * fps + 1e-6).floor().max(0.) as u32 |
| 3618 | } |
| 3619 | |
| 3620 | |
| 3621 | |
| 3622 | |
| 3623 | |
| 3624 | |
| 3625 | |
| 3626 | fn eq_fill(gain_db: f32) -> (f32, f32) { |
| 3627 | let at = (gain_db / EQ_GAIN_LIMIT).clamp(-1., 1.) / 2. + 0.5; |
| 3628 | match at >= 0.5 { |
| 3629 | true => (0.5, at - 0.5), |
| 3630 | false => (at, 0.5 - at), |
| 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | |
| 3635 | |
| 3636 | |
| 3637 | fn band_label(band: &Band) -> String { |
| 3638 | let freq = match band.freq_hz >= 1000. { |
| 3639 | true => format!("{:.0} kHz", band.freq_hz / 1000.), |
| 3640 | false => format!("{:.0} Hz", band.freq_hz), |
| 3641 | }; |
| 3642 | match band.kind { |
| 3643 | BandKind::LowShelf => format!("{freq} low shelf"), |
| 3644 | BandKind::HighShelf => format!("{freq} high shelf"), |
| 3645 | BandKind::Peak => freq, |
| 3646 | } |
| 3647 | } |
| 3648 | |
| 3649 | |
| 3650 | |
| 3651 | |
| 3652 | |
| 3653 | fn applicable(clip: &Clip, lane: Lane, action: ActionId, playhead: u32) -> bool { |
| 3654 | match action { |
| 3655 | |
| 3656 | |
| 3657 | ActionId::Equalizer => lane.kind == LaneKind::Audio, |
| 3658 | |
| 3659 | |
| 3660 | |
| 3661 | ActionId::Color | ActionId::Fit => lane.kind == LaneKind::Video, |
| 3662 | |
| 3663 | |
| 3664 | ActionId::Cut => clip.start < playhead && playhead < clip.end(), |
| 3665 | |
| 3666 | |
| 3667 | |
| 3668 | ActionId::Regroup => playhead == clip.start || playhead == clip.end(), |
| 3669 | _ => true, |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | |
| 3674 | |
| 3675 | |
| 3676 | |
| 3677 | fn menu_at(at: Point<Pixels>, viewport: Size<Pixels>, height: f32) -> (f32, f32) { |
| 3678 | let fit = |v: f32, size: f32, room: f32| v.min(room - size).max(0.); |
| 3679 | ( |
| 3680 | fit(f32::from(at.x), MENU_W, f32::from(viewport.width)), |
| 3681 | fit(f32::from(at.y), height, f32::from(viewport.height)), |
| 3682 | ) |
| 3683 | } |
| 3684 | |
| 3685 | |
| 3686 | |
| 3687 | |
| 3688 | fn can_add(picked: Option<&(PathBuf, usize)>, timeline: bool, exporting: bool) -> bool { |
| 3689 | picked.is_some() && timeline && !exporting |
| 3690 | } |
| 3691 | |
| 3692 | |
| 3693 | |
| 3694 | #[derive(Debug, PartialEq)] |
| 3695 | struct Row { |
| 3696 | path: PathBuf, |
| 3697 | stream: usize, |
| 3698 | |
| 3699 | name: String, |
| 3700 | |
| 3701 | detail: String, |
| 3702 | |
| 3703 | |
| 3704 | unusable: Option<String>, |
| 3705 | |
| 3706 | frames: u32, |
| 3707 | |
| 3708 | |
| 3709 | tint: usize, |
| 3710 | } |
| 3711 | |
| 3712 | |
| 3713 | |
| 3714 | |
| 3715 | |
| 3716 | |
| 3717 | |
| 3718 | |
| 3719 | |
| 3720 | |
| 3721 | |
| 3722 | fn library_rows( |
| 3723 | sources: &[Source], |
| 3724 | streams: &HashMap<PathBuf, Vec<StreamInfo>>, |
| 3725 | timeline_audio: Option<(u32, u16)>, |
| 3726 | frames: impl Fn(&Path) -> u32, |
| 3727 | ) -> Vec<Row> { |
| 3728 | let mut rows = Vec::new(); |
| 3729 | for (i, source) in sources.iter().enumerate() { |
| 3730 | let of_file = streams.get(&source.path).map_or(&[][..], Vec::as_slice); |
| 3731 | let info = of_file.iter().find(|s| s.index == source.audio_stream); |
| 3732 | let tint = sources |
| 3733 | .iter() |
| 3734 | .position(|s| s.path == source.path) |
| 3735 | .expect("a source finds itself"); |
| 3736 | rows.push(Row { |
| 3737 | path: source.path.clone(), |
| 3738 | stream: source.audio_stream, |
| 3739 | name: row_name(&source.path, source.audio_stream, of_file.len() > 1), |
| 3740 | |
| 3741 | |
| 3742 | |
| 3743 | detail: info |
| 3744 | .filter(|_| of_file.len() > 1) |
| 3745 | .map_or_else(String::new, stream_detail), |
| 3746 | |
| 3747 | |
| 3748 | unusable: None, |
| 3749 | frames: frames(&source.path), |
| 3750 | tint, |
| 3751 | }); |
| 3752 | |
| 3753 | |
| 3754 | if sources[i + 1..].iter().any(|s| s.path == source.path) { |
| 3755 | continue; |
| 3756 | } |
| 3757 | for info in of_file { |
| 3758 | if sources |
| 3759 | .iter() |
| 3760 | .any(|s| s.path == source.path && s.audio_stream == info.index) |
| 3761 | { |
| 3762 | continue; |
| 3763 | } |
| 3764 | rows.push(Row { |
| 3765 | path: source.path.clone(), |
| 3766 | stream: info.index, |
| 3767 | name: row_name(&source.path, info.index, true), |
| 3768 | detail: stream_detail(info), |
| 3769 | unusable: unusable(info, timeline_audio), |
| 3770 | frames: frames(&source.path), |
| 3771 | tint, |
| 3772 | }); |
| 3773 | } |
| 3774 | } |
| 3775 | rows |
| 3776 | } |
| 3777 | |
| 3778 | |
| 3779 | |
| 3780 | |
| 3781 | fn join_detail(detail: &str, tail: &str) -> String { |
| 3782 | match detail.is_empty() { |
| 3783 | true => tail.to_string(), |
| 3784 | false => format!("{detail} · {tail}"), |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | |
| 3789 | |
| 3790 | |
| 3791 | fn row_name(path: &Path, stream: usize, several: bool) -> String { |
| 3792 | match several { |
| 3793 | false => file_name(path), |
| 3794 | true => format!("{} [audio {}]", file_name(path), stream + 1), |
| 3795 | } |
| 3796 | } |
| 3797 | |
| 3798 | |
| 3799 | |
| 3800 | |
| 3801 | |
| 3802 | fn stream_detail(info: &StreamInfo) -> String { |
| 3803 | let mut parts: Vec<String> = Vec::new(); |
| 3804 | if let Some(lang) = &info.lang { |
| 3805 | parts.push(lang.clone()); |
| 3806 | } |
| 3807 | if info.sample_rate > 0 { |
| 3808 | parts.push(format!("{} kHz", f64::from(info.sample_rate) / 1000.)); |
| 3809 | } |
| 3810 | parts.extend(layout(info.channels)); |
| 3811 | parts.join(" ") |
| 3812 | } |
| 3813 | |
| 3814 | fn layout(channels: u16) -> Option<String> { |
| 3815 | match channels { |
| 3816 | 0 => None, |
| 3817 | 1 => Some("mono".to_string()), |
| 3818 | 2 => Some("stereo".to_string()), |
| 3819 | n => Some(format!("{n} ch")), |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | |
| 3824 | |
| 3825 | |
| 3826 | |
| 3827 | fn unusable(info: &StreamInfo, timeline_audio: Option<(u32, u16)>) -> Option<String> { |
| 3828 | if !info.decodable { |
| 3829 | |
| 3830 | |
| 3831 | |
| 3832 | return Some(match info.codec.as_str() { |
| 3833 | "unknown" => "unsupported codec".to_string(), |
| 3834 | codec => format!("{codec} is not supported"), |
| 3835 | }); |
| 3836 | } |
| 3837 | let (rate, channels) = timeline_audio?; |
| 3838 | ((info.sample_rate, info.channels) != (rate, channels)).then(|| { |
| 3839 | format!( |
| 3840 | "the timeline is {} kHz {}", |
| 3841 | f64::from(rate) / 1000., |
| 3842 | layout(channels).unwrap_or_else(|| "silent".to_string()) |
| 3843 | ) |
| 3844 | }) |
| 3845 | } |
| 3846 | |
| 3847 | |
| 3848 | |
| 3849 | |
| 3850 | |
| 3851 | fn library_w(window_w: f32) -> f32 { |
| 3852 | (window_w * LIBRARY_FRAC) |
| 3853 | .clamp(LIBRARY_MIN_W, LIBRARY_MAX_W) |
| 3854 | .min(window_w / 3.) |
| 3855 | } |
| 3856 | |
| 3857 | |
| 3858 | |
| 3859 | |
| 3860 | fn window_title(name: &str) -> String { |
| 3861 | if name == NO_FILE { |
| 3862 | "edith".to_string() |
| 3863 | } else { |
| 3864 | format!("{name} — edith") |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | |
| 3869 | |
| 3870 | |
| 3871 | fn source_tint(source: usize) -> u32 { |
| 3872 | SOURCE_TINTS[source % SOURCE_TINTS.len()] |
| 3873 | } |
| 3874 | |
| 3875 | |
| 3876 | |
| 3877 | |
| 3878 | |
| 3879 | #[derive(Clone, Copy, PartialEq, Debug)] |
| 3880 | enum Quality { |
| 3881 | Auto, |
| 3882 | Low, |
| 3883 | Medium, |
| 3884 | High, |
| 3885 | Custom, |
| 3886 | } |
| 3887 | |
| 3888 | impl Quality { |
| 3889 | const ALL: [Quality; 5] = [ |
| 3890 | Quality::Auto, |
| 3891 | Quality::Low, |
| 3892 | Quality::Medium, |
| 3893 | Quality::High, |
| 3894 | Quality::Custom, |
| 3895 | ]; |
| 3896 | |
| 3897 | fn label(self) -> &'static str { |
| 3898 | match self { |
| 3899 | Quality::Auto => "Auto", |
| 3900 | Quality::Low => "Low", |
| 3901 | Quality::Medium => "Medium", |
| 3902 | Quality::High => "High", |
| 3903 | Quality::Custom => "Custom", |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | |
| 3908 | fn detail(self, custom_mbps: u32) -> String { |
| 3909 | match self { |
| 3910 | Quality::Auto => "from the picture size and frame rate".to_string(), |
| 3911 | Quality::Custom => format!("{custom_mbps} Mbps — type a number, 1–20"), |
| 3912 | other => format!( |
| 3913 | "{} Mbps", |
| 3914 | export_settings(other, 0, Format::Mp4) |
| 3915 | .bitrate |
| 3916 | .unwrap_or_default() |
| 3917 | / 1_000_000 |
| 3918 | ), |
| 3919 | } |
| 3920 | } |
| 3921 | } |
| 3922 | |
| 3923 | |
| 3924 | |
| 3925 | |
| 3926 | |
| 3927 | |
| 3928 | |
| 3929 | |
| 3930 | |
| 3931 | |
| 3932 | const FORMATS: [(Option<Format>, &str, &str); 6] = [ |
| 3933 | (Some(Format::Mp4), "MP4", "H.264 picture + copied AAC"), |
| 3934 | (Some(Format::Wav), "WAV", "16-bit PCM — audio only"), |
| 3935 | (Some(Format::Flac), "FLAC", "lossless — audio only"), |
| 3936 | (None, "MP3", "encoder is LGPL — licence decision pending"), |
| 3937 | (None, "OGG", "no pure-Rust Vorbis or Opus encoder"), |
| 3938 | (None, "AAC", "no pure-Rust AAC encoder"), |
| 3939 | ]; |
| 3940 | |
| 3941 | |
| 3942 | |
| 3943 | |
| 3944 | fn format_key(key: &str) -> Option<Format> { |
| 3945 | FORMATS |
| 3946 | .into_iter() |
| 3947 | .filter_map(|(format, label, _)| format.zip(label.get(..1))) |
| 3948 | .find(|(_, initial)| initial.eq_ignore_ascii_case(key)) |
| 3949 | .map(|(format, _)| format) |
| 3950 | } |
| 3951 | |
| 3952 | |
| 3953 | |
| 3954 | |
| 3955 | |
| 3956 | enum ColorKey { |
| 3957 | Close, |
| 3958 | |
| 3959 | Band(usize), |
| 3960 | |
| 3961 | Nudge(f32), |
| 3962 | Reset, |
| 3963 | } |
| 3964 | |
| 3965 | fn color_key(key: &str) -> Option<ColorKey> { |
| 3966 | Some(match key { |
| 3967 | ESCAPE => ColorKey::Close, |
| 3968 | "down" => ColorKey::Band(1), |
| 3969 | "up" => ColorKey::Band(COLOR_BANDS.len() - 1), |
| 3970 | "right" => ColorKey::Nudge(1.), |
| 3971 | "left" => ColorKey::Nudge(-1.), |
| 3972 | "r" => ColorKey::Reset, |
| 3973 | _ => return None, |
| 3974 | }) |
| 3975 | } |
| 3976 | |
| 3977 | |
| 3978 | |
| 3979 | fn band_mut(params: &mut ColorParams, band: usize) -> &mut f32 { |
| 3980 | match band { |
| 3981 | 0 => &mut params.brightness, |
| 3982 | 1 => &mut params.contrast, |
| 3983 | 2 => &mut params.saturation, |
| 3984 | _ => &mut params.tint, |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | |
| 3989 | |
| 3990 | |
| 3991 | fn next_fit(fit: FitPolicy) -> FitPolicy { |
| 3992 | match fit { |
| 3993 | FitPolicy::Fit => FitPolicy::Fill, |
| 3994 | FitPolicy::Fill => FitPolicy::Stretch, |
| 3995 | FitPolicy::Stretch => FitPolicy::Center, |
| 3996 | FitPolicy::Center => FitPolicy::Fit, |
| 3997 | } |
| 3998 | } |
| 3999 | |
| 4000 | |
| 4001 | fn fit_label(fit: FitPolicy) -> &'static str { |
| 4002 | match fit { |
| 4003 | FitPolicy::Fit => "fit (whole picture, bars)", |
| 4004 | FitPolicy::Fill => "fill (cropped, no bars)", |
| 4005 | FitPolicy::Stretch => "stretch (aspect broken)", |
| 4006 | FitPolicy::Center => "centre (1:1, no resample)", |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | |
| 4011 | |
| 4012 | |
| 4013 | |
| 4014 | fn next_resolution(current: (u32, u32), native: (u32, u32)) -> (u32, u32) { |
| 4015 | let mut sizes: Vec<(u32, u32)> = RESOLUTIONS.to_vec(); |
| 4016 | if !sizes.contains(&native) { |
| 4017 | |
| 4018 | |
| 4019 | let at = sizes |
| 4020 | .iter() |
| 4021 | .position(|&(w, h)| { |
| 4022 | u64::from(w) * u64::from(h) < u64::from(native.0) * u64::from(native.1) |
| 4023 | }) |
| 4024 | .unwrap_or(sizes.len()); |
| 4025 | sizes.insert(at, native); |
| 4026 | } |
| 4027 | let at = sizes.iter().position(|&s| s == current); |
| 4028 | |
| 4029 | |
| 4030 | sizes[at.map_or(0, |at| (at + 1) % sizes.len())] |
| 4031 | } |
| 4032 | |
| 4033 | |
| 4034 | |
| 4035 | |
| 4036 | fn export_line(format: Format, session: Option<&PlaybackSession>) -> String { |
| 4037 | let line = format_line(format); |
| 4038 | match session.filter(|_| format.has_video()) { |
| 4039 | Some(session) => { |
| 4040 | let (w, h) = session.resolution(); |
| 4041 | format!("{line} · project {w}x{h}") |
| 4042 | } |
| 4043 | None => line.to_string(), |
| 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | fn format_line(format: Format) -> &'static str { |
| 4048 | match format { |
| 4049 | Format::Mp4 => "H.264 · MP4 · moov at end", |
| 4050 | Format::Wav => "16-bit PCM · WAV · timeline audio only", |
| 4051 | Format::Flac => "FLAC · lossless · timeline audio only", |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | |
| 4056 | |
| 4057 | |
| 4058 | fn retarget(path: &std::path::Path, format: Format) -> PathBuf { |
| 4059 | let mut path = path.to_path_buf(); |
| 4060 | path.set_extension(format.ext()); |
| 4061 | path |
| 4062 | } |
| 4063 | |
| 4064 | |
| 4065 | |
| 4066 | |
| 4067 | |
| 4068 | |
| 4069 | |
| 4070 | |
| 4071 | |
| 4072 | fn export_settings(quality: Quality, custom_mbps: u32, format: Format) -> ExportSettings { |
| 4073 | ExportSettings { |
| 4074 | format, |
| 4075 | bitrate: match quality { |
| 4076 | Quality::Auto => None, |
| 4077 | Quality::Low => Some(2_000_000), |
| 4078 | Quality::Medium => Some(6_000_000), |
| 4079 | Quality::High => Some(12_000_000), |
| 4080 | Quality::Custom => Some(u64::from(custom_mbps) * 1_000_000), |
| 4081 | }, |
| 4082 | |
| 4083 | |
| 4084 | |
| 4085 | force_sw: false, |
| 4086 | } |
| 4087 | } |
| 4088 | |
| 4089 | |
| 4090 | |
| 4091 | |
| 4092 | |
| 4093 | fn push_digit(mbps: u32, digit: u32) -> u32 { |
| 4094 | let next = mbps * 10 + digit; |
| 4095 | if next <= 99 { next } else { mbps } |
| 4096 | } |
| 4097 | |
| 4098 | |
| 4099 | |
| 4100 | |
| 4101 | |
| 4102 | |
| 4103 | |
| 4104 | fn cancels_export(key: &str, action: Option<ActionId>) -> bool { |
| 4105 | key == ESCAPE || action == Some(ActionId::CancelExport) |
| 4106 | } |
| 4107 | |
| 4108 | |
| 4109 | |
| 4110 | |
| 4111 | |
| 4112 | fn is_bare_modifier(key: &str) -> bool { |
| 4113 | matches!( |
| 4114 | key, |
| 4115 | "control" | "shift" | "alt" | "super" | "platform" | "function" | "fn" | "meta" | "command" |
| 4116 | ) |
| 4117 | } |
| 4118 | |
| 4119 | |
| 4120 | |
| 4121 | |
| 4122 | |
| 4123 | |
| 4124 | |
| 4125 | |
| 4126 | fn mp4_refusal(session: &PlaybackSession) -> Option<String> { |
| 4127 | let lanes = session |
| 4128 | .lanes() |
| 4129 | .into_iter() |
| 4130 | .filter(|&lane| lane.kind == LaneKind::Audio && !session.lane_clips(lane).is_empty()) |
| 4131 | .count(); |
| 4132 | (lanes > 1).then(|| format!("{lanes} audio lanes — an mp4 copies one")) |
| 4133 | } |
| 4134 | |
| 4135 | |
| 4136 | |
| 4137 | fn lanes_h(lanes: usize) -> f32 { |
| 4138 | match lanes { |
| 4139 | 0 => 0., |
| 4140 | n => n as f32 * LANE_H + (n - 1) as f32 * 8., |
| 4141 | } |
| 4142 | } |
| 4143 | |
| 4144 | |
| 4145 | |
| 4146 | |
| 4147 | |
| 4148 | fn panel_h(lanes: usize) -> f32 { |
| 4149 | PANEL_H + lanes_h(lanes.clamp(2, LANES_MAX)) - lanes_h(2) |
| 4150 | } |
| 4151 | |
| 4152 | fn width_frac(len: f64, total: f64) -> f32 { |
| 4153 | if total > 0. { (len / total) as f32 } else { 1. } |
| 4154 | } |
| 4155 | |
| 4156 | |
| 4157 | |
| 4158 | |
| 4159 | fn start_frac(start: f64, total: f64) -> f32 { |
| 4160 | if total > 0. { |
| 4161 | (start / total).clamp(0., 1.) as f32 |
| 4162 | } else { |
| 4163 | 0. |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | |
| 4168 | |
| 4169 | |
| 4170 | |
| 4171 | |
| 4172 | |
| 4173 | |
| 4174 | |
| 4175 | |
| 4176 | fn whole_take(session: &PlaybackSession, lane: Lane, idx: usize) -> bool { |
| 4177 | let Some(clip) = session.lane_clips(lane).get(idx) else { |
| 4178 | return false; |
| 4179 | }; |
| 4180 | match (lane.kind, lane.ord) { |
| 4181 | (_, 1..) => false, |
| 4182 | (LaneKind::Video, _) => true, |
| 4183 | |
| 4184 | |
| 4185 | (LaneKind::Audio, _) => session |
| 4186 | .lanes() |
| 4187 | .into_iter() |
| 4188 | .filter(|&other| other != lane) |
| 4189 | .flat_map(|other| session.lane_clips(other)) |
| 4190 | .any(|o| o.link.is_some() && o.link == clip.link), |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | |
| 4195 | |
| 4196 | |
| 4197 | |
| 4198 | fn marked( |
| 4199 | here: (Lane, usize), |
| 4200 | link: Option<u32>, |
| 4201 | sel: Option<(Lane, usize)>, |
| 4202 | sel_link: Option<u32>, |
| 4203 | ) -> bool { |
| 4204 | sel == Some(here) || (link.is_some() && link == sel_link) |
| 4205 | } |
| 4206 | |
| 4207 | |
| 4208 | fn show_label(w: f32) -> bool { |
| 4209 | w >= LABEL_MIN_W |
| 4210 | } |
| 4211 | |
| 4212 | |
| 4213 | |
| 4214 | |
| 4215 | |
| 4216 | fn normalise(mut peaks: Vec<(f32, f32)>) -> Vec<(f32, f32)> { |
| 4217 | let loudest = peaks.iter().fold(0f32, |m, &(lo, hi)| m.max(-lo).max(hi)); |
| 4218 | if loudest > 0. { |
| 4219 | for (lo, hi) in &mut peaks { |
| 4220 | *lo /= loudest; |
| 4221 | *hi /= loudest; |
| 4222 | } |
| 4223 | } |
| 4224 | peaks |
| 4225 | } |
| 4226 | |
| 4227 | |
| 4228 | |
| 4229 | |
| 4230 | |
| 4231 | |
| 4232 | fn envelope(peaks: &[(f32, f32)], from: f64, to: f64, w: f32, h: f32) -> Vec<(f32, f32, f32)> { |
| 4233 | if peaks.is_empty() || w <= 0. || h <= 0. { |
| 4234 | return Vec::new(); |
| 4235 | } |
| 4236 | let cols = (w / WAVE_COL).ceil().max(1.) as usize; |
| 4237 | let mid = h / 2.; |
| 4238 | (0..=cols) |
| 4239 | .map(|col| { |
| 4240 | let along = col as f64 / cols as f64; |
| 4241 | let at = from + (to - from) * along; |
| 4242 | |
| 4243 | |
| 4244 | let bucket = ((at * f64::from(WAVE_BPS)) as usize).min(peaks.len() - 1); |
| 4245 | let (lo, hi) = peaks[bucket]; |
| 4246 | let top = (mid - hi.clamp(0., 1.) * mid).min(mid - 0.5); |
| 4247 | let bottom = (mid - lo.clamp(-1., 0.) * mid).max(mid + 0.5); |
| 4248 | (w * along as f32, top.max(0.), bottom.min(h)) |
| 4249 | }) |
| 4250 | .collect() |
| 4251 | } |
| 4252 | |
| 4253 | |
| 4254 | |
| 4255 | |
| 4256 | fn waveform(peaks: Arc<Vec<(f32, f32)>>, from: f64, to: f64) -> impl IntoElement { |
| 4257 | canvas( |
| 4258 | |_, _, _| (), |
| 4259 | move |bounds, _, window, _| { |
| 4260 | let (o, s) = (bounds.origin, bounds.size); |
| 4261 | let cols = envelope(&peaks, from, to, f32::from(s.width), f32::from(s.height)); |
| 4262 | if cols.len() < 2 { |
| 4263 | return; |
| 4264 | } |
| 4265 | |
| 4266 | |
| 4267 | let mut points: Vec<Point<Pixels>> = cols |
| 4268 | .iter() |
| 4269 | .map(|&(x, top, _)| point(o.x + px(x), o.y + px(top))) |
| 4270 | .collect(); |
| 4271 | points.extend( |
| 4272 | cols.iter() |
| 4273 | .rev() |
| 4274 | .map(|&(x, _, bottom)| point(o.x + px(x), o.y + px(bottom))), |
| 4275 | ); |
| 4276 | let mut path = PathBuilder::fill(); |
| 4277 | path.add_polygon(&points, true); |
| 4278 | if let Ok(path) = path.build() { |
| 4279 | window.paint_path(path, rgb(INK_DIM)); |
| 4280 | } |
| 4281 | }, |
| 4282 | ) |
| 4283 | .size_full() |
| 4284 | } |
| 4285 | |
| 4286 | |
| 4287 | |
| 4288 | |
| 4289 | |
| 4290 | |
| 4291 | |
| 4292 | fn control( |
| 4293 | id: &'static str, |
| 4294 | glyph: Option<AnyElement>, |
| 4295 | |
| 4296 | label: impl Into<SharedString>, |
| 4297 | shortcut: String, |
| 4298 | enabled: bool, |
| 4299 | on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, |
| 4300 | ) -> impl IntoElement { |
| 4301 | let label = label.into(); |
| 4302 | let tip: SharedString = format!("{label} — {shortcut}").into(); |
| 4303 | div() |
| 4304 | .id(id) |
| 4305 | .flex_none() |
| 4306 | .h(px(CONTROL_H)) |
| 4307 | .flex() |
| 4308 | .items_center() |
| 4309 | .gap(px(6.)) |
| 4310 | .px(px(8.)) |
| 4311 | .rounded(px(3.)) |
| 4312 | .bg(rgb(SURFACE)) |
| 4313 | .children(glyph) |
| 4314 | .child(label) |
| 4315 | .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) |
| 4316 | .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed()) |
| 4317 | .when(enabled, |d| { |
| 4318 | d.cursor_pointer() |
| 4319 | .hover(|s| s.bg(rgb(HOVER))) |
| 4320 | .on_click(on_click) |
| 4321 | }) |
| 4322 | } |
| 4323 | |
| 4324 | |
| 4325 | fn separator() -> impl IntoElement { |
| 4326 | div() |
| 4327 | .flex_none() |
| 4328 | .mx(px(4.)) |
| 4329 | .w(px(1.)) |
| 4330 | .h(px(18.)) |
| 4331 | .bg(rgb(HOVER)) |
| 4332 | } |
| 4333 | |
| 4334 | |
| 4335 | |
| 4336 | |
| 4337 | struct Tip(SharedString); |
| 4338 | |
| 4339 | impl Render for Tip { |
| 4340 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 4341 | div() |
| 4342 | .px(px(6.)) |
| 4343 | .py(px(3.)) |
| 4344 | .rounded(px(3.)) |
| 4345 | .border_1() |
| 4346 | .border_color(rgb(SURFACE)) |
| 4347 | .bg(rgb(CHROME)) |
| 4348 | .text_color(rgb(INK)) |
| 4349 | .text_size(px(12.)) |
| 4350 | .child(self.0.clone()) |
| 4351 | } |
| 4352 | } |
| 4353 | |
| 4354 | |
| 4355 | |
| 4356 | |
| 4357 | fn cut_glyph() -> impl IntoElement { |
| 4358 | canvas( |
| 4359 | |_, _, _| (), |
| 4360 | |bounds, _, window, _| { |
| 4361 | let (o, s) = (bounds.origin, bounds.size); |
| 4362 | let mut path = PathBuilder::stroke(px(1.5)); |
| 4363 | path.move_to(point(o.x + s.width * 0.15, o.y + s.height)); |
| 4364 | path.line_to(point(o.x + s.width * 0.9, o.y + s.height * 0.1)); |
| 4365 | path.move_to(point(o.x + s.width * 0.85, o.y + s.height)); |
| 4366 | path.line_to(point(o.x + s.width * 0.1, o.y + s.height * 0.1)); |
| 4367 | if let Ok(path) = path.build() { |
| 4368 | window.paint_path(path, rgb(INK)); |
| 4369 | } |
| 4370 | }, |
| 4371 | ) |
| 4372 | .w(px(13.)) |
| 4373 | .h(px(13.)) |
| 4374 | } |
| 4375 | |
| 4376 | |
| 4377 | fn delete_glyph() -> impl IntoElement { |
| 4378 | div() |
| 4379 | .flex() |
| 4380 | .flex_col() |
| 4381 | .items_center() |
| 4382 | .gap(px(2.)) |
| 4383 | .child(div().w(px(13.)).h(px(2.)).bg(rgb(INK))) |
| 4384 | .child(div().w(px(9.)).h(px(9.)).bg(rgb(INK))) |
| 4385 | } |
| 4386 | |
| 4387 | |
| 4388 | |
| 4389 | |
| 4390 | fn bounds_probe(into: Rc<Cell<Bounds<Pixels>>>) -> impl IntoElement { |
| 4391 | canvas(move |bounds, _, _| into.set(bounds), |_, _, _, _| ()) |
| 4392 | .absolute() |
| 4393 | .size_full() |
| 4394 | } |
| 4395 | |
| 4396 | |
| 4397 | |
| 4398 | fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 { |
| 4399 | if bounds.size.width <= px(0.) { |
| 4400 | return 0.; |
| 4401 | } |
| 4402 | ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) |
| 4403 | } |
| 4404 | |
| 4405 | |
| 4406 | |
| 4407 | |
| 4408 | fn empty_hint() -> impl IntoElement { |
| 4409 | div() |
| 4410 | .flex() |
| 4411 | .flex_col() |
| 4412 | .items_center() |
| 4413 | .gap(px(6.)) |
| 4414 | .text_color(rgb(INK_DIM)) |
| 4415 | .child("Drop a video or an .edith project here") |
| 4416 | .child( |
| 4417 | div() |
| 4418 | .text_size(px(11.)) |
| 4419 | .child("or click Import in the media list"), |
| 4420 | ) |
| 4421 | } |
| 4422 | |
| 4423 | |
| 4424 | |
| 4425 | fn transport_glyph(playing: bool) -> impl IntoElement { |
| 4426 | div() |
| 4427 | .flex() |
| 4428 | .items_center() |
| 4429 | .gap(px(4.)) |
| 4430 | .when(playing, |d| { |
| 4431 | d.child(div().w(px(3.)).h(px(12.)).bg(rgb(INK))) |
| 4432 | .child(div().w(px(3.)).h(px(12.)).bg(rgb(INK))) |
| 4433 | }) |
| 4434 | .when(!playing, |d| { |
| 4435 | d.child( |
| 4436 | canvas( |
| 4437 | |_, _, _| (), |
| 4438 | |bounds, _, window, _| { |
| 4439 | let (o, s) = (bounds.origin, bounds.size); |
| 4440 | let mut path = PathBuilder::fill(); |
| 4441 | path.move_to(o); |
| 4442 | path.line_to(point(o.x + s.width, o.y + s.height / 2.)); |
| 4443 | path.line_to(point(o.x, o.y + s.height)); |
| 4444 | path.close(); |
| 4445 | if let Ok(path) = path.build() { |
| 4446 | window.paint_path(path, rgb(INK)); |
| 4447 | } |
| 4448 | }, |
| 4449 | ) |
| 4450 | .w(px(11.)) |
| 4451 | .h(px(13.)), |
| 4452 | ) |
| 4453 | }) |
| 4454 | } |
| 4455 | |
| 4456 | |
| 4457 | fn timecode(t: f64, fps: f64) -> String { |
| 4458 | let t = t.max(0.); |
| 4459 | let secs = t as u64; |
| 4460 | let last = (fps.ceil() as u64).saturating_sub(1); |
| 4461 | let frame = ((t - secs as f64) * fps) as u64; |
| 4462 | format!( |
| 4463 | "{:02}:{:02}:{:02}:{:02}", |
| 4464 | secs / 3600, |
| 4465 | secs / 60 % 60, |
| 4466 | secs % 60, |
| 4467 | frame.min(last) |
| 4468 | ) |
| 4469 | } |
| 4470 | |
| 4471 | #[cfg(test)] |
| 4472 | mod tests { |
| 4473 | use super::{ |
| 4474 | ACCENT, CONTROL_H, Clip, EQ_BAR_H, EQ_GAIN_LIMIT, EXPORT_ROWS_H, FORMATS, Format, |
| 4475 | HEADER_GAP, HEADER_W, HIT_MIN, INK, INK_DIM, KEYS_ROW_H, KEYS_ROWS_H, KEYS_W, LABEL_H, |
| 4476 | LABEL_MIN_W, LANE_H, LANES_MAX, LETTERBOX, LIBRARY_MAX_W, LIBRARY_MIN_W, Lane, MENU_ITEMS, |
| 4477 | MENU_W, NO_FILE, PANEL_H, Quality, ROW_H, RULER_HIT_H, SELECTED, SOURCE_TINTS, SURFACE, |
| 4478 | SWATCH_W, Source, StreamInfo, Volume, WAVE_BPS, WAVE_COL, Wave, applicable, band_label, |
| 4479 | can_add, cancels_export, envelope, eq_fill, export_path, export_settings, format_line, |
| 4480 | frac_along, frame_at, is_bare_modifier, is_project, keymap, lane_clips, lanes_h, marked, |
| 4481 | menu_at, mp4_refusal, normalise, panel_h, project_path, push_digit, retarget, scrub_due, |
| 4482 | show_label, source_frames, source_tint, start_frac, timecode, unseen_paths, unseen_sources, |
| 4483 | whole_take, width_frac, window_title, |
| 4484 | }; |
| 4485 | use super::{file_name, library_rows}; |
| 4486 | use engine::PlaybackSession; |
| 4487 | use engine::scale::FitPolicy; |
| 4488 | use gpui::{Bounds, Pixels, point, px, size}; |
| 4489 | use std::collections::HashMap; |
| 4490 | use std::path::{Path, PathBuf}; |
| 4491 | use std::time::Duration; |
| 4492 | |
| 4493 | fn asset(name: &str) -> PathBuf { |
| 4494 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 4495 | .join("../../assets") |
| 4496 | .join(name) |
| 4497 | } |
| 4498 | |
| 4499 | |
| 4500 | |
| 4501 | fn clip(source: usize, frames: u32) -> Clip { |
| 4502 | Clip { |
| 4503 | start: 0, |
| 4504 | in_frame: 0, |
| 4505 | out_frame: frames, |
| 4506 | source, |
| 4507 | link: None, |
| 4508 | eq: None, |
| 4509 | color: None, |
| 4510 | fit: FitPolicy::default(), |
| 4511 | } |
| 4512 | } |
| 4513 | |
| 4514 | |
| 4515 | fn source(path: &str, stream: usize) -> Source { |
| 4516 | Source { |
| 4517 | path: PathBuf::from(path), |
| 4518 | audio_stream: stream, |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | |
| 4523 | |
| 4524 | |
| 4525 | |
| 4526 | #[test] |
| 4527 | fn a_rows_swatch_and_duration_come_off_the_clips_that_name_it() { |
| 4528 | |
| 4529 | |
| 4530 | |
| 4531 | let sources: Vec<Source> = (0..4).map(|i| source(&format!("/m/{i}.mp4"), 0)).collect(); |
| 4532 | let video = [clip(0, 150), clip(1, 60)]; |
| 4533 | let audio = [clip(0, 150), clip(1, 30), clip(2, 90)]; |
| 4534 | for (row, frames) in [(0, 150), (1, 60), (2, 90), (3, 0)] { |
| 4535 | assert_eq!( |
| 4536 | source_frames(video.iter().chain(&audio), &sources, &sources[row].path), |
| 4537 | frames, |
| 4538 | "row {row}" |
| 4539 | ); |
| 4540 | |
| 4541 | |
| 4542 | assert_eq!(source_tint(row), SOURCE_TINTS[row % SOURCE_TINTS.len()]); |
| 4543 | } |
| 4544 | |
| 4545 | |
| 4546 | let one = [source("/m/0.mp4", 0)]; |
| 4547 | let path = &one[0].path; |
| 4548 | assert_eq!( |
| 4549 | source_frames([clip(0, 10), clip(0, 90)].iter(), &one, path), |
| 4550 | 90 |
| 4551 | ); |
| 4552 | assert_eq!( |
| 4553 | source_frames([clip(0, 90), clip(0, 10)].iter(), &one, path), |
| 4554 | 90 |
| 4555 | ); |
| 4556 | assert_eq!(source_frames([].iter(), &one, path), 0); |
| 4557 | |
| 4558 | |
| 4559 | |
| 4560 | let two = [source("/m/0.mp4", 0), source("/m/0.mp4", 1)]; |
| 4561 | assert_eq!(source_frames([clip(0, 90)].iter(), &two, &two[1].path), 90); |
| 4562 | } |
| 4563 | |
| 4564 | fn info( |
| 4565 | index: usize, |
| 4566 | rate: u32, |
| 4567 | channels: u16, |
| 4568 | lang: Option<&str>, |
| 4569 | decodable: bool, |
| 4570 | ) -> StreamInfo { |
| 4571 | StreamInfo { |
| 4572 | index, |
| 4573 | codec: if decodable { "aac" } else { "unknown" }.into(), |
| 4574 | channels, |
| 4575 | sample_rate: rate, |
| 4576 | lang: lang.map(str::to_string), |
| 4577 | decodable, |
| 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | |
| 4582 | |
| 4583 | |
| 4584 | |
| 4585 | #[test] |
| 4586 | fn every_audio_stream_of_a_file_is_a_row_usable_or_not() { |
| 4587 | let multi = PathBuf::from("/m/movie.mp4"); |
| 4588 | let sources = [source("/m/movie.mp4", 0)]; |
| 4589 | let mut streams = HashMap::new(); |
| 4590 | streams.insert( |
| 4591 | multi.clone(), |
| 4592 | vec![ |
| 4593 | info(0, 44_100, 2, None, true), |
| 4594 | info(1, 44_100, 2, Some("fra"), true), |
| 4595 | info(2, 22_050, 1, Some("deu"), true), |
| 4596 | info(3, 0, 0, None, false), |
| 4597 | ], |
| 4598 | ); |
| 4599 | let rows = library_rows(&sources, &streams, Some((44_100, 2)), |_| 90); |
| 4600 | assert_eq!( |
| 4601 | rows.iter().map(|r| r.stream).collect::<Vec<_>>(), |
| 4602 | [0, 1, 2, 3], |
| 4603 | "one row per audio stream, in file order" |
| 4604 | ); |
| 4605 | assert!( |
| 4606 | rows.iter().all(|r| r.path == multi && r.tint == 0), |
| 4607 | "every stream of one file wears the file's own tint" |
| 4608 | ); |
| 4609 | assert_eq!(rows[0].name, "movie.mp4 [audio 1]"); |
| 4610 | assert_eq!(rows[1].name, "movie.mp4 [audio 2]"); |
| 4611 | assert_eq!(rows[1].detail, "fra 44.1 kHz stereo"); |
| 4612 | |
| 4613 | |
| 4614 | |
| 4615 | |
| 4616 | assert_eq!((&rows[0].unusable, &rows[1].unusable), (&None, &None)); |
| 4617 | assert_eq!( |
| 4618 | rows[2].unusable.as_deref(), |
| 4619 | Some("the timeline is 44.1 kHz stereo") |
| 4620 | ); |
| 4621 | assert_eq!(rows[3].unusable.as_deref(), Some("unsupported codec")); |
| 4622 | assert_eq!( |
| 4623 | rows[3].detail, "", |
| 4624 | "a stream we cannot parse claims nothing" |
| 4625 | ); |
| 4626 | |
| 4627 | assert!(rows.iter().all(|r| r.frames == 90)); |
| 4628 | |
| 4629 | |
| 4630 | |
| 4631 | let plain = [source("/m/plain.mp4", 0)]; |
| 4632 | let mut one = HashMap::new(); |
| 4633 | one.insert( |
| 4634 | PathBuf::from("/m/plain.mp4"), |
| 4635 | vec![info(0, 44_100, 2, None, true)], |
| 4636 | ); |
| 4637 | let rows = library_rows(&plain, &one, Some((44_100, 2)), |_| 90); |
| 4638 | assert_eq!(rows.len(), 1); |
| 4639 | assert_eq!(rows[0].name, "plain.mp4"); |
| 4640 | assert_eq!( |
| 4641 | rows[0].detail, "", |
| 4642 | "one audio track is the row it has always been: name and length" |
| 4643 | ); |
| 4644 | |
| 4645 | let mut silent = HashMap::new(); |
| 4646 | silent.insert(PathBuf::from("/m/plain.mp4"), Vec::new()); |
| 4647 | for probe in [silent, HashMap::new()] { |
| 4648 | let rows = library_rows(&plain, &probe, None, |_| 90); |
| 4649 | assert_eq!(rows.len(), 1); |
| 4650 | assert_eq!(rows[0].name, "plain.mp4"); |
| 4651 | assert_eq!(rows[0].detail, ""); |
| 4652 | } |
| 4653 | |
| 4654 | |
| 4655 | |
| 4656 | |
| 4657 | let placed = [ |
| 4658 | source("/m/movie.mp4", 0), |
| 4659 | source("/m/other.mp4", 0), |
| 4660 | source("/m/movie.mp4", 2), |
| 4661 | ]; |
| 4662 | streams.insert( |
| 4663 | PathBuf::from("/m/other.mp4"), |
| 4664 | vec![info(0, 44_100, 2, None, true)], |
| 4665 | ); |
| 4666 | let rows = library_rows(&placed, &streams, Some((44_100, 2)), |_| 90); |
| 4667 | assert_eq!( |
| 4668 | rows.iter() |
| 4669 | .map(|r| (file_name(&r.path), r.stream)) |
| 4670 | .collect::<Vec<_>>(), |
| 4671 | [ |
| 4672 | ("movie.mp4".to_string(), 0), |
| 4673 | ("other.mp4".to_string(), 0), |
| 4674 | ("movie.mp4".to_string(), 2), |
| 4675 | ("movie.mp4".to_string(), 1), |
| 4676 | ("movie.mp4".to_string(), 3), |
| 4677 | ] |
| 4678 | ); |
| 4679 | assert!( |
| 4680 | rows[2].unusable.is_none(), |
| 4681 | "a stream already on the timeline is playing, whatever a probe says" |
| 4682 | ); |
| 4683 | assert_eq!(rows[1].tint, 1, "the other file is the other tint"); |
| 4684 | } |
| 4685 | |
| 4686 | |
| 4687 | |
| 4688 | |
| 4689 | |
| 4690 | #[test] |
| 4691 | fn the_clip_menu_dims_what_the_playhead_is_not_on_and_stays_in_the_window() { |
| 4692 | use keymap::{ActionId, Keymap}; |
| 4693 | |
| 4694 | let clip = Clip { |
| 4695 | start: 30, |
| 4696 | in_frame: 0, |
| 4697 | out_frame: 60, |
| 4698 | source: 0, |
| 4699 | link: None, |
| 4700 | eq: None, |
| 4701 | color: None, |
| 4702 | fit: FitPolicy::default(), |
| 4703 | }; |
| 4704 | assert_eq!(clip.end(), 90); |
| 4705 | |
| 4706 | let a1 = Lane::A1; |
| 4707 | let v1 = Lane::V1; |
| 4708 | assert!(applicable(&clip, v1, ActionId::Cut, 31)); |
| 4709 | assert!(applicable(&clip, v1, ActionId::Cut, 89)); |
| 4710 | assert!(!applicable(&clip, v1, ActionId::Cut, 30)); |
| 4711 | assert!(!applicable(&clip, v1, ActionId::Cut, 90)); |
| 4712 | assert!(!applicable(&clip, v1, ActionId::Cut, 200)); |
| 4713 | |
| 4714 | assert!(applicable(&clip, v1, ActionId::Regroup, 30)); |
| 4715 | assert!(applicable(&clip, v1, ActionId::Regroup, 90)); |
| 4716 | assert!(!applicable(&clip, v1, ActionId::Regroup, 60)); |
| 4717 | |
| 4718 | |
| 4719 | assert!(applicable(&clip, a1, ActionId::Equalizer, 0)); |
| 4720 | assert!(applicable(&clip, a1, ActionId::Equalizer, 60)); |
| 4721 | assert!(!applicable(&clip, v1, ActionId::Equalizer, 60)); |
| 4722 | |
| 4723 | |
| 4724 | for action in [ActionId::Delete, ActionId::Lift, ActionId::ToggleMute] { |
| 4725 | assert!(applicable(&clip, v1, action, 0)); |
| 4726 | assert!(applicable(&clip, a1, action, 60)); |
| 4727 | } |
| 4728 | |
| 4729 | |
| 4730 | assert!(applicable(&clip, v1, ActionId::Color, 0)); |
| 4731 | assert!(!applicable(&clip, a1, ActionId::Color, 60)); |
| 4732 | |
| 4733 | assert_eq!(frame_at(1.0, 30.), 30); |
| 4734 | assert_eq!(frame_at(0.0, 30.), 0); |
| 4735 | assert_eq!(frame_at(-1.0, 30.), 0); |
| 4736 | |
| 4737 | let viewport = size(px(800.), px(400.)); |
| 4738 | assert_eq!(menu_at(point(px(10.), px(10.)), viewport, 150.), (10., 10.)); |
| 4739 | assert_eq!( |
| 4740 | menu_at(point(px(700.), px(380.)), viewport, 150.), |
| 4741 | (800. - MENU_W, 250.) |
| 4742 | ); |
| 4743 | |
| 4744 | assert_eq!( |
| 4745 | menu_at(point(px(90.), px(40.)), size(px(100.), px(50.)), 150.), |
| 4746 | (0., 0.) |
| 4747 | ); |
| 4748 | |
| 4749 | |
| 4750 | |
| 4751 | let keymap = Keymap::defaults(); |
| 4752 | for action in MENU_ITEMS { |
| 4753 | assert!(ActionId::ALL.contains(&action), "{action:?} is not listed"); |
| 4754 | assert_ne!(keymap.display(action), "unbound", "{action:?}"); |
| 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | |
| 4759 | |
| 4760 | |
| 4761 | #[test] |
| 4762 | fn a_refused_import_leaves_no_row_and_an_accepted_one_is_whole() { |
| 4763 | let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); |
| 4764 | |
| 4765 | session.set_gain(0.0); |
| 4766 | assert_eq!(session.sources().len(), 1); |
| 4767 | |
| 4768 | |
| 4769 | let refusal = session |
| 4770 | .import(&asset("test_mismatch.mp4")) |
| 4771 | .expect_err("a silent file must not join a timeline with audio") |
| 4772 | .to_string(); |
| 4773 | assert!(refusal.contains("audio"), "refusal must name it: {refusal}"); |
| 4774 | assert_eq!(session.sources().len(), 1, "a refusal added a row"); |
| 4775 | |
| 4776 | |
| 4777 | session.import(&asset("test_av2.mp4")).expect("av2 matches"); |
| 4778 | assert_eq!(session.sources().len(), 2); |
| 4779 | let second = session.sources()[1].path.clone(); |
| 4780 | assert_eq!( |
| 4781 | source_frames(lane_clips(&session), session.sources(), &second), |
| 4782 | 120 |
| 4783 | ); |
| 4784 | assert_eq!(timecode(120. / 30., 30.), "00:00:04:00"); |
| 4785 | } |
| 4786 | |
| 4787 | |
| 4788 | |
| 4789 | |
| 4790 | #[test] |
| 4791 | fn adding_a_row_drops_the_whole_source_in_at_the_playhead() { |
| 4792 | let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); |
| 4793 | session.set_gain(0.0); |
| 4794 | session.import(&asset("test_av2.mp4")).expect("av2 matches"); |
| 4795 | assert_eq!(session.timeline_duration(), 9.0); |
| 4796 | |
| 4797 | session.seek(2.0); |
| 4798 | let second = session.sources()[1].path.clone(); |
| 4799 | let frames = source_frames(lane_clips(&session), session.sources(), &second); |
| 4800 | |
| 4801 | |
| 4802 | assert!( |
| 4803 | session |
| 4804 | .place_stream_at(2.0, &second, 0, frames, None) |
| 4805 | .expect("av2 is already on the timeline") |
| 4806 | ); |
| 4807 | |
| 4808 | |
| 4809 | assert_eq!(session.timeline_duration(), 13.0); |
| 4810 | let (video, audio) = (session.lane_clips(Lane::V1), session.lane_clips(Lane::A1)); |
| 4811 | |
| 4812 | |
| 4813 | let at = |lane: &[Clip]| { |
| 4814 | *lane |
| 4815 | .iter() |
| 4816 | .find(|c| c.start == 60) |
| 4817 | .expect("inserted at 2 s") |
| 4818 | }; |
| 4819 | assert_eq!(at(video), at(audio)); |
| 4820 | assert_eq!(at(video).source, 1); |
| 4821 | assert_eq!(at(video).len(), frames); |
| 4822 | assert!(at(video).link.is_some()); |
| 4823 | assert_eq!(video.len(), audio.len()); |
| 4824 | |
| 4825 | |
| 4826 | |
| 4827 | |
| 4828 | let mut session = |
| 4829 | PlaybackSession::open(asset("test_multilang.mp4")).expect("open the fixture"); |
| 4830 | session.set_gain(0.0); |
| 4831 | let path = session.sources()[0].path.clone(); |
| 4832 | let frames = source_frames(lane_clips(&session), session.sources(), &path); |
| 4833 | let end = session.timeline_duration(); |
| 4834 | assert!( |
| 4835 | session |
| 4836 | .place_stream_at(end, &path, 1, frames, None) |
| 4837 | .expect("the French track shares the timeline's parameters") |
| 4838 | ); |
| 4839 | assert_eq!(session.sources()[1].audio_stream, 1); |
| 4840 | assert_eq!(session.timeline_duration(), end * 2.0); |
| 4841 | |
| 4842 | |
| 4843 | assert_eq!( |
| 4844 | source_frames(lane_clips(&session), session.sources(), &path), |
| 4845 | frames |
| 4846 | ); |
| 4847 | } |
| 4848 | |
| 4849 | |
| 4850 | |
| 4851 | |
| 4852 | |
| 4853 | #[test] |
| 4854 | fn a_track_can_be_added_dropped_on_edited_and_taken_back() { |
| 4855 | use engine::project::LaneKind; |
| 4856 | |
| 4857 | let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); |
| 4858 | session.set_gain(0.0); |
| 4859 | assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1]); |
| 4860 | |
| 4861 | let v2 = session.add_lane(LaneKind::Video); |
| 4862 | assert_eq!(v2.label(), "V2"); |
| 4863 | assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1, v2]); |
| 4864 | assert!(session.lane_clips(v2).is_empty()); |
| 4865 | |
| 4866 | |
| 4867 | let path = session.sources()[0].path.clone(); |
| 4868 | let frames = source_frames(lane_clips(&session), session.sources(), &path); |
| 4869 | assert!( |
| 4870 | session |
| 4871 | .place_stream_at(1.0, &path, 0, frames, Some(v2)) |
| 4872 | .expect("its own file is on this timeline") |
| 4873 | ); |
| 4874 | assert_eq!(session.lane_clips(v2).len(), 1, "the drop landed on V2"); |
| 4875 | assert_eq!(session.lane_clips(v2)[0].start, 30, "at the playhead"); |
| 4876 | |
| 4877 | assert_eq!(session.lane_clips(Lane::V1).len(), 1); |
| 4878 | assert_eq!(session.lane_clips(Lane::A1).len(), 1); |
| 4879 | |
| 4880 | |
| 4881 | |
| 4882 | assert!(!whole_take(&session, v2, 0)); |
| 4883 | assert!(whole_take(&session, Lane::V1, 0)); |
| 4884 | assert!(whole_take(&session, Lane::A1, 0)); |
| 4885 | assert!(session.lift_clip(v2, 0)); |
| 4886 | assert!(session.lane_clips(v2).is_empty()); |
| 4887 | assert_eq!(session.lane_clips(Lane::V1).len(), 1, "V1 stayed put"); |
| 4888 | |
| 4889 | |
| 4890 | |
| 4891 | assert_eq!(mp4_refusal(&session), None); |
| 4892 | let a2 = session.add_lane(LaneKind::Audio); |
| 4893 | assert_eq!(a2.label(), "A2"); |
| 4894 | assert_eq!(mp4_refusal(&session), None, "an empty lane carries nothing"); |
| 4895 | assert!( |
| 4896 | session |
| 4897 | .place_stream_at(0.0, &path, 0, frames, Some(a2)) |
| 4898 | .expect("its own file is on this timeline") |
| 4899 | ); |
| 4900 | assert_eq!( |
| 4901 | mp4_refusal(&session).as_deref(), |
| 4902 | Some("2 audio lanes — an mp4 copies one") |
| 4903 | ); |
| 4904 | |
| 4905 | |
| 4906 | |
| 4907 | |
| 4908 | for lanes in [4, 3, 3, 3, 2] { |
| 4909 | assert!(session.undo()); |
| 4910 | assert_eq!(session.lanes().len(), lanes); |
| 4911 | } |
| 4912 | assert_eq!(session.lanes(), vec![Lane::V1, Lane::A1]); |
| 4913 | assert_eq!(session.lane_clips(Lane::V1).len(), 1); |
| 4914 | assert_eq!(mp4_refusal(&session), None); |
| 4915 | } |
| 4916 | |
| 4917 | #[test] |
| 4918 | fn a_source_is_only_ever_asked_for_its_peaks_once() { |
| 4919 | let (a, b) = (asset("test_av.mp4"), asset("test_av2.mp4")); |
| 4920 | |
| 4921 | |
| 4922 | let sources = [ |
| 4923 | Source { |
| 4924 | path: a.clone(), |
| 4925 | audio_stream: 0, |
| 4926 | }, |
| 4927 | Source { |
| 4928 | path: a.clone(), |
| 4929 | audio_stream: 1, |
| 4930 | }, |
| 4931 | Source { |
| 4932 | path: b.clone(), |
| 4933 | audio_stream: 0, |
| 4934 | }, |
| 4935 | ]; |
| 4936 | let keys = |s: &[Source]| { |
| 4937 | s.iter() |
| 4938 | .map(|s| (s.path.clone(), s.audio_stream)) |
| 4939 | .collect::<Vec<_>>() |
| 4940 | }; |
| 4941 | let mut waves: HashMap<(PathBuf, usize), Wave> = HashMap::new(); |
| 4942 | assert_eq!(unseen_sources(&sources, &waves), keys(&sources)); |
| 4943 | |
| 4944 | |
| 4945 | |
| 4946 | waves.insert((a.clone(), 0), Wave::Loading); |
| 4947 | assert_eq!( |
| 4948 | unseen_sources(&sources, &waves), |
| 4949 | keys(&sources[1..]), |
| 4950 | "the file's other stream is a key of its own" |
| 4951 | ); |
| 4952 | |
| 4953 | waves.insert((a, 1), Wave::Silent); |
| 4954 | waves.insert((b.clone(), 0), Wave::Silent); |
| 4955 | assert!(unseen_sources(&sources, &waves).is_empty()); |
| 4956 | |
| 4957 | let mut streams: HashMap<PathBuf, Vec<StreamInfo>> = HashMap::new(); |
| 4958 | assert_eq!( |
| 4959 | unseen_paths(&sources, &streams), |
| 4960 | vec![asset("test_av.mp4"), b] |
| 4961 | ); |
| 4962 | streams.insert(asset("test_av.mp4"), Vec::new()); |
| 4963 | assert_eq!(unseen_paths(&sources, &streams).len(), 1); |
| 4964 | } |
| 4965 | |
| 4966 | #[test] |
| 4967 | fn the_add_button_is_dead_unless_it_would_do_something() { |
| 4968 | let picked = (PathBuf::from("/m/0.mp4"), 0); |
| 4969 | assert!(can_add(Some(&picked), true, false)); |
| 4970 | |
| 4971 | |
| 4972 | assert!(!can_add(None, true, false)); |
| 4973 | assert!(!can_add(Some(&picked), false, false)); |
| 4974 | assert!(!can_add(Some(&picked), true, true)); |
| 4975 | } |
| 4976 | |
| 4977 | #[test] |
| 4978 | fn the_media_column_never_takes_the_picture_over() { |
| 4979 | for window in [360., 640., 1280., 1920., 3840.] { |
| 4980 | let w = super::library_w(window); |
| 4981 | |
| 4982 | |
| 4983 | assert!(w <= window / 3., "{window}px window gave the list {w}px"); |
| 4984 | assert!(w >= LIBRARY_MIN_W.min(window / 3.), "{window}px: {w}px"); |
| 4985 | assert!(w <= LIBRARY_MAX_W); |
| 4986 | } |
| 4987 | |
| 4988 | assert!(super::library_w(640.) < super::library_w(1280.)); |
| 4989 | |
| 4990 | |
| 4991 | assert!(ROW_H >= HIT_MIN); |
| 4992 | assert!(SWATCH_W < LIBRARY_MIN_W); |
| 4993 | } |
| 4994 | |
| 4995 | #[test] |
| 4996 | fn the_window_is_named_after_the_program_and_what_is_open() { |
| 4997 | assert_eq!(window_title("test_av.mp4"), "test_av.mp4 — edith"); |
| 4998 | |
| 4999 | assert_eq!(window_title(NO_FILE), "edith"); |
| 5000 | } |
| 5001 | |
| 5002 | #[test] |
| 5003 | fn frac_along_measures_from_the_elements_own_left_edge() { |
| 5004 | |
| 5005 | let bar: Bounds<Pixels> = Bounds { |
| 5006 | origin: point(px(12.), px(400.)), |
| 5007 | size: size(px(200.), px(6.)), |
| 5008 | }; |
| 5009 | assert_eq!(frac_along(px(12.), bar), 0.); |
| 5010 | assert_eq!(frac_along(px(112.), bar), 0.5); |
| 5011 | assert_eq!(frac_along(px(212.), bar), 1.); |
| 5012 | |
| 5013 | assert_eq!(frac_along(px(0.), bar), 0.); |
| 5014 | assert_eq!(frac_along(px(9999.), bar), 1.); |
| 5015 | |
| 5016 | assert_eq!(frac_along(px(50.), Bounds::default()), 0.); |
| 5017 | } |
| 5018 | |
| 5019 | #[test] |
| 5020 | fn timecode_counts_frames_inside_the_second() { |
| 5021 | assert_eq!(timecode(0., 30.), "00:00:00:00"); |
| 5022 | assert_eq!(timecode(-1., 30.), "00:00:00:00"); |
| 5023 | assert_eq!(timecode(4.9667, 30.), "00:00:04:29"); |
| 5024 | assert_eq!(timecode(5., 30.), "00:00:05:00"); |
| 5025 | assert_eq!(timecode(3661.5, 30.), "01:01:01:15"); |
| 5026 | |
| 5027 | assert_eq!(timecode(1. - f64::EPSILON, 30.), "00:00:00:29"); |
| 5028 | assert_eq!(timecode(0.999, 29.97), "00:00:00:29"); |
| 5029 | } |
| 5030 | |
| 5031 | #[test] |
| 5032 | fn scrub_seeks_only_on_a_new_frame_and_only_every_100ms() { |
| 5033 | let (slow, fast) = (Duration::from_millis(100), Duration::from_millis(99)); |
| 5034 | |
| 5035 | |
| 5036 | assert!(scrub_due(31, 30, slow)); |
| 5037 | assert!(!scrub_due(30, 30, slow)); |
| 5038 | assert!(!scrub_due(31, 30, fast)); |
| 5039 | assert!(!scrub_due(30, 30, fast)); |
| 5040 | |
| 5041 | assert!(scrub_due(1, 0, Duration::from_secs(9))); |
| 5042 | } |
| 5043 | |
| 5044 | #[test] |
| 5045 | fn export_lands_beside_the_source_and_never_on_it() { |
| 5046 | |
| 5047 | assert_eq!( |
| 5048 | export_path("assets/test_baseline.mp4"), |
| 5049 | std::path::Path::new("assets/test_baseline.export.mp4") |
| 5050 | ); |
| 5051 | |
| 5052 | assert_eq!( |
| 5053 | export_path("a.export.mp4"), |
| 5054 | std::path::Path::new("a.export.export.mp4") |
| 5055 | ); |
| 5056 | |
| 5057 | assert_eq!(export_path("clip"), std::path::Path::new("clip.export.mp4")); |
| 5058 | assert_eq!( |
| 5059 | export_path("/v.1/clip.MP4"), |
| 5060 | std::path::Path::new("/v.1/clip.export.mp4") |
| 5061 | ); |
| 5062 | } |
| 5063 | |
| 5064 | #[test] |
| 5065 | fn the_first_save_lands_beside_the_media() { |
| 5066 | assert_eq!( |
| 5067 | project_path("assets/test_av.mp4"), |
| 5068 | std::path::Path::new("assets/test_av.edith") |
| 5069 | ); |
| 5070 | |
| 5071 | assert_eq!( |
| 5072 | project_path("a.export.mp4"), |
| 5073 | std::path::Path::new("a.export.edith") |
| 5074 | ); |
| 5075 | assert_eq!(project_path("clip"), std::path::Path::new("clip.edith")); |
| 5076 | |
| 5077 | assert_eq!(project_path("a.edith"), std::path::Path::new("a.edith")); |
| 5078 | } |
| 5079 | |
| 5080 | #[test] |
| 5081 | fn only_an_exact_edith_extension_is_a_project() { |
| 5082 | let p = std::path::Path::new; |
| 5083 | assert!(is_project(p("a.edith"))); |
| 5084 | |
| 5085 | assert!(is_project(p("/v.mp4/a.edith"))); |
| 5086 | assert!(!is_project(p("a.mp4"))); |
| 5087 | assert!(!is_project(p("/v.edith/a.mp4"))); |
| 5088 | |
| 5089 | |
| 5090 | assert!(!is_project(p("a.EDITH"))); |
| 5091 | |
| 5092 | assert!(!is_project(p("edith"))); |
| 5093 | assert!(!is_project(p("a.edith.mp4"))); |
| 5094 | } |
| 5095 | |
| 5096 | |
| 5097 | |
| 5098 | |
| 5099 | |
| 5100 | #[test] |
| 5101 | fn no_stroke_is_missing_from_the_keys_menu() { |
| 5102 | use keymap::{ActionId, Keymap}; |
| 5103 | |
| 5104 | |
| 5105 | let handler = include_str!("main.rs") |
| 5106 | .split("#[cfg(test)]") |
| 5107 | .next() |
| 5108 | .unwrap(); |
| 5109 | let keymap = Keymap::defaults(); |
| 5110 | let listed = |key: &str| { |
| 5111 | let pretty = keymap::Chord { |
| 5112 | key: key.to_string(), |
| 5113 | ctrl: false, |
| 5114 | } |
| 5115 | .pretty(); |
| 5116 | keymap.lookup(key, false).is_some() || keymap::FIXED.iter().any(|f| f.chord == pretty) |
| 5117 | }; |
| 5118 | let mut compared = 0; |
| 5119 | for (at, needle) in handler.match_indices("key == ") { |
| 5120 | let rest = &handler[at + needle.len()..]; |
| 5121 | let key = match rest.strip_prefix('"') { |
| 5122 | Some(literal) => { |
| 5123 | literal[..literal.find('"').expect("unterminated key")].to_string() |
| 5124 | } |
| 5125 | |
| 5126 | None if rest.starts_with("ESCAPE") => super::ESCAPE.to_string(), |
| 5127 | None => panic!("a key compared against something this cannot read: {rest:.20}"), |
| 5128 | }; |
| 5129 | assert!( |
| 5130 | listed(&key), |
| 5131 | "the handler answers to {key:?} and the keys menu never says so" |
| 5132 | ); |
| 5133 | compared += 1; |
| 5134 | } |
| 5135 | |
| 5136 | |
| 5137 | assert!( |
| 5138 | compared >= 4, |
| 5139 | "the key comparisons moved; this scan is blind" |
| 5140 | ); |
| 5141 | |
| 5142 | assert!(handler.contains("key.parse::<u32>()")); |
| 5143 | assert!(keymap::FIXED.iter().any(|f| f.chord == "0–9")); |
| 5144 | |
| 5145 | for action in ActionId::ALL { |
| 5146 | assert!(keymap::Category::ALL.contains(&action.category())); |
| 5147 | } |
| 5148 | for fixed in &keymap::FIXED { |
| 5149 | assert!(keymap::Category::ALL.contains(&fixed.category)); |
| 5150 | } |
| 5151 | } |
| 5152 | |
| 5153 | #[test] |
| 5154 | fn clip_boxes_split_the_lane_by_duration() { |
| 5155 | |
| 5156 | assert_eq!(width_frac(1., 4.), 0.25); |
| 5157 | assert_eq!(width_frac(3., 4.), 0.75); |
| 5158 | assert_eq!(width_frac(4., 4.), 1.); |
| 5159 | |
| 5160 | assert_eq!(width_frac(0., 0.), 1.); |
| 5161 | } |
| 5162 | |
| 5163 | #[test] |
| 5164 | fn escape_gets_out_of_an_export_whatever_is_held_with_it() { |
| 5165 | use keymap::ActionId; |
| 5166 | |
| 5167 | |
| 5168 | |
| 5169 | assert!(cancels_export("escape", None)); |
| 5170 | assert!(cancels_export("escape", Some(ActionId::CancelExport))); |
| 5171 | |
| 5172 | |
| 5173 | assert!(cancels_export("q", Some(ActionId::CancelExport))); |
| 5174 | |
| 5175 | assert!(!cancels_export("e", Some(ActionId::Export))); |
| 5176 | assert!(!cancels_export("space", Some(ActionId::Play))); |
| 5177 | assert!(!cancels_export("q", None)); |
| 5178 | } |
| 5179 | |
| 5180 | #[test] |
| 5181 | fn a_capture_waits_through_a_lone_modifier() { |
| 5182 | |
| 5183 | |
| 5184 | for key in [ |
| 5185 | "control", "shift", "alt", "super", "platform", "function", "fn", "meta", "command", |
| 5186 | ] { |
| 5187 | assert!(is_bare_modifier(key), "{key}"); |
| 5188 | } |
| 5189 | |
| 5190 | |
| 5191 | for key in ["c", "x", "space", "escape", "delete", "f1", "z"] { |
| 5192 | assert!(!is_bare_modifier(key), "{key}"); |
| 5193 | } |
| 5194 | } |
| 5195 | |
| 5196 | #[test] |
| 5197 | fn the_keybindings_card_fits_the_smallest_window() { |
| 5198 | |
| 5199 | |
| 5200 | |
| 5201 | let title = 17.; |
| 5202 | let status = 28.; |
| 5203 | let gaps = 3. * 2.; |
| 5204 | let padding = 24.; |
| 5205 | assert!( |
| 5206 | title + status + KEYS_ROWS_H + gaps + padding <= 360., |
| 5207 | "card too tall" |
| 5208 | ); |
| 5209 | |
| 5210 | |
| 5211 | |
| 5212 | assert!( |
| 5213 | KEYS_ROWS_H / KEYS_ROW_H >= 8., |
| 5214 | "too few rows visible to scan" |
| 5215 | ); |
| 5216 | assert!(KEYS_W <= 640., "card too wide"); |
| 5217 | |
| 5218 | |
| 5219 | assert!(KEYS_ROW_H >= HIT_MIN); |
| 5220 | } |
| 5221 | |
| 5222 | |
| 5223 | |
| 5224 | |
| 5225 | |
| 5226 | |
| 5227 | #[test] |
| 5228 | fn the_equalizer_bar_grows_out_of_flat_and_the_card_fits_the_smallest_window() { |
| 5229 | use engine::eq::{Band, BandKind, EqParams}; |
| 5230 | |
| 5231 | |
| 5232 | assert_eq!(eq_fill(0.), (0.5, 0.)); |
| 5233 | |
| 5234 | assert_eq!(eq_fill(EQ_GAIN_LIMIT), (0.5, 0.5)); |
| 5235 | assert_eq!(eq_fill(-EQ_GAIN_LIMIT), (0., 0.5)); |
| 5236 | assert_eq!(eq_fill(EQ_GAIN_LIMIT / 2.), (0.5, 0.25)); |
| 5237 | assert_eq!(eq_fill(-EQ_GAIN_LIMIT / 2.), (0.25, 0.25)); |
| 5238 | |
| 5239 | |
| 5240 | assert_eq!(eq_fill(400.), (0.5, 0.5)); |
| 5241 | assert_eq!(eq_fill(-400.), (0., 0.5)); |
| 5242 | |
| 5243 | |
| 5244 | |
| 5245 | for gain in [-12., -6., 0., 6., 12.] { |
| 5246 | let (left, width) = eq_fill(gain); |
| 5247 | let at = if gain >= 0. { left + width } else { left }; |
| 5248 | let read = (at * 2. - 1.) * EQ_GAIN_LIMIT; |
| 5249 | assert!((read - gain).abs() < 1e-4, "{gain} read back as {read}"); |
| 5250 | } |
| 5251 | |
| 5252 | |
| 5253 | |
| 5254 | let labels: Vec<String> = EqParams::default_layout() |
| 5255 | .bands |
| 5256 | .iter() |
| 5257 | .map(band_label) |
| 5258 | .collect(); |
| 5259 | assert_eq!( |
| 5260 | labels, |
| 5261 | [ |
| 5262 | "80 Hz low shelf", |
| 5263 | "250 Hz", |
| 5264 | "1 kHz", |
| 5265 | "4 kHz", |
| 5266 | "12 kHz high shelf" |
| 5267 | ] |
| 5268 | ); |
| 5269 | assert_eq!( |
| 5270 | band_label(&Band { |
| 5271 | freq_hz: 2600., |
| 5272 | gain_db: 0., |
| 5273 | q: 1., |
| 5274 | kind: BandKind::Peak |
| 5275 | }), |
| 5276 | "3 kHz", |
| 5277 | "rounded, because a band label is not a measurement" |
| 5278 | ); |
| 5279 | |
| 5280 | |
| 5281 | let (title, status, gaps, padding) = (17., 28., 3. * 2., 24.); |
| 5282 | assert!( |
| 5283 | title + status + EXPORT_ROWS_H + gaps + padding + CONTROL_H <= 360., |
| 5284 | "card too tall" |
| 5285 | ); |
| 5286 | assert!(KEYS_W <= 640., "card too wide"); |
| 5287 | |
| 5288 | |
| 5289 | assert!(KEYS_ROW_H >= HIT_MIN); |
| 5290 | assert!(EQ_BAR_H < KEYS_ROW_H); |
| 5291 | } |
| 5292 | |
| 5293 | |
| 5294 | |
| 5295 | #[test] |
| 5296 | fn muting_keeps_the_level_it_comes_back_to() { |
| 5297 | let mut volume = Volume::default(); |
| 5298 | assert_eq!(volume.gain(), 1.0); |
| 5299 | assert_eq!(volume.label(), "Vol 100%"); |
| 5300 | |
| 5301 | |
| 5302 | |
| 5303 | for _ in 0..4 { |
| 5304 | volume.step(false); |
| 5305 | } |
| 5306 | assert_eq!(volume.gain(), 0.8); |
| 5307 | volume.muted = true; |
| 5308 | assert_eq!(volume.gain(), 0.0); |
| 5309 | assert_eq!(volume.label(), "Muted 80%"); |
| 5310 | |
| 5311 | |
| 5312 | |
| 5313 | volume.step(false); |
| 5314 | assert_eq!(volume.gain(), 0.0); |
| 5315 | assert!(volume.muted); |
| 5316 | |
| 5317 | |
| 5318 | volume.muted = false; |
| 5319 | assert_eq!(volume.gain(), 0.75); |
| 5320 | } |
| 5321 | |
| 5322 | |
| 5323 | |
| 5324 | #[test] |
| 5325 | fn the_volume_stops_at_both_ends() { |
| 5326 | let mut volume = Volume::default(); |
| 5327 | for _ in 0..40 { |
| 5328 | volume.step(true); |
| 5329 | } |
| 5330 | assert_eq!(volume.gain(), 1.0); |
| 5331 | assert_eq!(volume.steps, Volume::MAX_STEPS); |
| 5332 | |
| 5333 | for _ in 0..40 { |
| 5334 | volume.step(false); |
| 5335 | } |
| 5336 | assert_eq!(volume.gain(), 0.0); |
| 5337 | assert_eq!(volume.label(), "Vol 0%"); |
| 5338 | |
| 5339 | |
| 5340 | |
| 5341 | assert!(!volume.muted); |
| 5342 | volume.step(true); |
| 5343 | assert_eq!(volume.gain(), 0.05); |
| 5344 | } |
| 5345 | |
| 5346 | #[test] |
| 5347 | fn a_quality_row_is_the_bitrate_it_promises() { |
| 5348 | |
| 5349 | |
| 5350 | let mp4 = Format::Mp4; |
| 5351 | assert_eq!(export_settings(Quality::Auto, 7, mp4).bitrate, None); |
| 5352 | assert_eq!( |
| 5353 | export_settings(Quality::Low, 0, mp4).bitrate, |
| 5354 | Some(2_000_000) |
| 5355 | ); |
| 5356 | assert_eq!( |
| 5357 | export_settings(Quality::Medium, 0, mp4).bitrate, |
| 5358 | Some(6_000_000) |
| 5359 | ); |
| 5360 | assert_eq!( |
| 5361 | export_settings(Quality::High, 0, mp4).bitrate, |
| 5362 | Some(12_000_000) |
| 5363 | ); |
| 5364 | |
| 5365 | assert_eq!( |
| 5366 | export_settings(Quality::Custom, 7, mp4).bitrate, |
| 5367 | Some(7_000_000) |
| 5368 | ); |
| 5369 | assert_eq!(Quality::Low.detail(0), "2 Mbps"); |
| 5370 | |
| 5371 | |
| 5372 | for format in [Format::Mp4, Format::Wav, Format::Flac] { |
| 5373 | assert_eq!(export_settings(Quality::Auto, 0, format).format, format); |
| 5374 | } |
| 5375 | |
| 5376 | |
| 5377 | for quality in Quality::ALL { |
| 5378 | let settings = export_settings(quality, 7, mp4); |
| 5379 | if let Some(bitrate) = settings.bitrate { |
| 5380 | assert!( |
| 5381 | (1_000_000..=20_000_000).contains(&bitrate), |
| 5382 | "{quality:?} outside the engine clamp" |
| 5383 | ); |
| 5384 | } |
| 5385 | |
| 5386 | assert!(!settings.force_sw); |
| 5387 | } |
| 5388 | } |
| 5389 | |
| 5390 | #[test] |
| 5391 | fn a_typed_bitrate_stops_at_two_digits() { |
| 5392 | assert_eq!(push_digit(0, 8), 8); |
| 5393 | assert_eq!(push_digit(1, 2), 12); |
| 5394 | |
| 5395 | |
| 5396 | |
| 5397 | assert_eq!(push_digit(12, 3), 12); |
| 5398 | assert_eq!(push_digit(99, 9), 99); |
| 5399 | |
| 5400 | assert!(u64::from(push_digit(99, 9)) * 1_000_000 < u64::from(u32::MAX)); |
| 5401 | } |
| 5402 | |
| 5403 | #[test] |
| 5404 | fn the_export_card_fits_the_smallest_window() { |
| 5405 | |
| 5406 | |
| 5407 | |
| 5408 | let title = 17.; |
| 5409 | let status = 28.; |
| 5410 | let fixed = 17.; |
| 5411 | let gaps = 4. * 2.; |
| 5412 | let padding = 24.; |
| 5413 | assert!( |
| 5414 | title + status + EXPORT_ROWS_H + fixed + CONTROL_H + 4. + gaps + padding <= 360., |
| 5415 | "card too tall" |
| 5416 | ); |
| 5417 | |
| 5418 | |
| 5419 | assert!(EXPORT_ROWS_H / KEYS_ROW_H >= 6.); |
| 5420 | |
| 5421 | assert!(KEYS_ROW_H >= HIT_MIN); |
| 5422 | assert!(CONTROL_H >= HIT_MIN); |
| 5423 | } |
| 5424 | |
| 5425 | #[test] |
| 5426 | fn every_format_row_is_offered_or_says_why_not() { |
| 5427 | |
| 5428 | |
| 5429 | let offered: Vec<Format> = FORMATS.iter().filter_map(|&(f, ..)| f).collect(); |
| 5430 | assert_eq!(offered, vec![Format::Mp4, Format::Wav, Format::Flac]); |
| 5431 | for (format, label, _) in FORMATS { |
| 5432 | match format { |
| 5433 | Some(format) => assert_eq!(format.ext(), label.to_lowercase()), |
| 5434 | |
| 5435 | |
| 5436 | None => assert!(!label.is_empty()), |
| 5437 | } |
| 5438 | } |
| 5439 | for (format, label, detail) in FORMATS { |
| 5440 | assert!(!detail.is_empty(), "{label} says nothing"); |
| 5441 | |
| 5442 | |
| 5443 | assert_eq!( |
| 5444 | format.is_some_and(Format::has_video), |
| 5445 | format == Some(Format::Mp4) |
| 5446 | ); |
| 5447 | } |
| 5448 | |
| 5449 | assert_eq!( |
| 5450 | retarget(std::path::Path::new("/a/take.export.mp4"), Format::Wav), |
| 5451 | std::path::Path::new("/a/take.export.wav") |
| 5452 | ); |
| 5453 | assert_eq!( |
| 5454 | retarget(std::path::Path::new("/a/take.export.wav"), Format::Mp4), |
| 5455 | std::path::Path::new("/a/take.export.mp4") |
| 5456 | ); |
| 5457 | assert!(format_line(Format::Flac).contains("audio only")); |
| 5458 | } |
| 5459 | |
| 5460 | #[test] |
| 5461 | fn nothing_clickable_is_smaller_than_the_wcag_minimum() { |
| 5462 | |
| 5463 | |
| 5464 | assert!(CONTROL_H >= HIT_MIN); |
| 5465 | assert!(RULER_HIT_H >= HIT_MIN); |
| 5466 | assert!(LANE_H >= HIT_MIN); |
| 5467 | } |
| 5468 | |
| 5469 | |
| 5470 | fn luminance(colour: u32) -> f64 { |
| 5471 | let channel = |shift: u32| { |
| 5472 | let s = f64::from((colour >> shift) & 0xff) / 255.; |
| 5473 | if s <= 0.03928 { |
| 5474 | s / 12.92 |
| 5475 | } else { |
| 5476 | ((s + 0.055) / 1.055).powf(2.4) |
| 5477 | } |
| 5478 | }; |
| 5479 | 0.2126 * channel(16) + 0.7152 * channel(8) + 0.0722 * channel(0) |
| 5480 | } |
| 5481 | |
| 5482 | |
| 5483 | fn contrast(a: u32, b: u32) -> f64 { |
| 5484 | let (a, b) = (luminance(a), luminance(b)); |
| 5485 | (a.max(b) + 0.05) / (a.min(b) + 0.05) |
| 5486 | } |
| 5487 | |
| 5488 | #[test] |
| 5489 | fn a_lane_row_is_a_fixed_header_and_a_bed_that_can_be_hit() { |
| 5490 | |
| 5491 | |
| 5492 | assert!(HEADER_W > 0. && HEADER_GAP >= 0.); |
| 5493 | |
| 5494 | |
| 5495 | assert!( |
| 5496 | CONTROL_H + RULER_HIT_H + 2. * LANE_H + 17. + 4. * 8. + 16. <= PANEL_H, |
| 5497 | "the second lane does not fit the panel" |
| 5498 | ); |
| 5499 | |
| 5500 | |
| 5501 | assert!(LANE_H >= HIT_MIN); |
| 5502 | |
| 5503 | assert!(LABEL_H < LANE_H / 2.); |
| 5504 | |
| 5505 | |
| 5506 | assert_eq!(panel_h(2), PANEL_H); |
| 5507 | assert_eq!(panel_h(1), PANEL_H); |
| 5508 | assert_eq!(panel_h(3), PANEL_H + LANE_H + 8.); |
| 5509 | assert_eq!( |
| 5510 | panel_h(LANES_MAX), |
| 5511 | PANEL_H + lanes_h(LANES_MAX) - lanes_h(2) |
| 5512 | ); |
| 5513 | |
| 5514 | |
| 5515 | assert_eq!(panel_h(LANES_MAX + 1), panel_h(LANES_MAX)); |
| 5516 | assert_eq!(panel_h(50), panel_h(LANES_MAX)); |
| 5517 | assert_eq!(lanes_h(0), 0.); |
| 5518 | assert_eq!(lanes_h(1), LANE_H); |
| 5519 | assert_eq!(lanes_h(2), 2. * LANE_H + 8.); |
| 5520 | } |
| 5521 | |
| 5522 | #[test] |
| 5523 | fn a_click_marks_the_whole_group_and_nothing_else() { |
| 5524 | let (v, a) = ((Lane::V1, 0), (Lane::A1, 0)); |
| 5525 | |
| 5526 | assert!(marked(v, Some(1), Some(v), Some(1))); |
| 5527 | assert!(marked(a, Some(1), Some(v), Some(1))); |
| 5528 | |
| 5529 | assert!(!marked((Lane::V1, 1), Some(2), Some(v), Some(1))); |
| 5530 | assert!(!marked((Lane::A1, 1), Some(2), Some(v), Some(1))); |
| 5531 | |
| 5532 | |
| 5533 | |
| 5534 | assert!(marked(a, None, Some(a), None)); |
| 5535 | assert!(!marked(v, None, Some(a), None)); |
| 5536 | |
| 5537 | assert!(!marked(v, Some(1), None, None)); |
| 5538 | } |
| 5539 | |
| 5540 | #[test] |
| 5541 | fn a_name_is_dropped_rather_than_smeared_across_a_thin_clip() { |
| 5542 | assert!(show_label(LABEL_MIN_W)); |
| 5543 | assert!(show_label(400.)); |
| 5544 | assert!(!show_label(LABEL_MIN_W - 0.1)); |
| 5545 | |
| 5546 | assert_eq!(start_frac(3., 0.), 0.); |
| 5547 | assert_eq!(start_frac(1., 4.), 0.25); |
| 5548 | |
| 5549 | assert_eq!(start_frac(8., 4.), 1.); |
| 5550 | } |
| 5551 | |
| 5552 | #[test] |
| 5553 | fn an_envelope_stays_inside_the_box_it_is_drawn_in() { |
| 5554 | |
| 5555 | let peaks: Vec<(f32, f32)> = (0..40) |
| 5556 | .map(|i| (-(i as f32) / 39., i as f32 / 39.)) |
| 5557 | .collect(); |
| 5558 | let (w, h) = (100., 30.); |
| 5559 | let cols = envelope(&peaks, 0., 1., w, h); |
| 5560 | assert_eq!(cols.len(), (w / WAVE_COL) as usize + 1); |
| 5561 | for &(x, top, bottom) in &cols { |
| 5562 | assert!((0. ..=w).contains(&x), "x {x} outside 0..{w}"); |
| 5563 | assert!((0. ..=h).contains(&top), "top {top} outside 0..{h}"); |
| 5564 | assert!( |
| 5565 | (0. ..=h).contains(&bottom), |
| 5566 | "bottom {bottom} outside 0..{h}" |
| 5567 | ); |
| 5568 | |
| 5569 | |
| 5570 | assert!( |
| 5571 | bottom - top >= 1., |
| 5572 | "column {top}..{bottom} is thinner than a pixel" |
| 5573 | ); |
| 5574 | } |
| 5575 | |
| 5576 | let height = |&(_, top, bottom): &(f32, f32, f32)| bottom - top; |
| 5577 | assert!(height(cols.last().unwrap()) > height(cols.first().unwrap()) + 5.); |
| 5578 | |
| 5579 | assert!(envelope(&[], 0., 1., w, h).is_empty()); |
| 5580 | assert!(envelope(&peaks, 0., 1., 0., h).is_empty()); |
| 5581 | |
| 5582 | assert!(!envelope(&peaks, 0., 99., w, h).is_empty()); |
| 5583 | } |
| 5584 | |
| 5585 | #[test] |
| 5586 | fn a_quiet_source_still_draws_as_a_shape() { |
| 5587 | |
| 5588 | let quiet: Vec<(f32, f32)> = vec![(-0.125, 0.125), (-0.0625, 0.0625)]; |
| 5589 | let loud = normalise(quiet.clone()); |
| 5590 | assert_eq!(loud[0], (-1., 1.)); |
| 5591 | assert_eq!(loud[1], (-0.5, 0.5)); |
| 5592 | |
| 5593 | |
| 5594 | assert_eq!(normalise(vec![(0., 0.)]), vec![(0., 0.)]); |
| 5595 | assert!(normalise(Vec::new()).is_empty()); |
| 5596 | } |
| 5597 | |
| 5598 | |
| 5599 | |
| 5600 | #[test] |
| 5601 | fn the_fixtures_waveform_reaches_the_lane_as_a_shape() { |
| 5602 | let asset = |name: &str| { |
| 5603 | std::path::Path::new(env!("CARGO_MANIFEST_DIR")) |
| 5604 | .join("../../assets") |
| 5605 | .join(name) |
| 5606 | }; |
| 5607 | let peaks = normalise( |
| 5608 | engine::waveform::peaks(asset("test_av.mp4"), 0, WAVE_BPS) |
| 5609 | .expect("open the fixture") |
| 5610 | .expect("test_av.mp4 has audio"), |
| 5611 | ); |
| 5612 | |
| 5613 | assert!(peaks.len().abs_diff(5 * WAVE_BPS as usize) <= WAVE_BPS as usize); |
| 5614 | let cols = envelope(&peaks, 0., 5., 600., 30.); |
| 5615 | let height = |&(_, top, bottom): &(f32, f32, f32)| bottom - top; |
| 5616 | let tallest = cols.iter().map(height).fold(0., f32::max); |
| 5617 | let flattest = cols.iter().map(height).fold(f32::MAX, f32::min); |
| 5618 | |
| 5619 | |
| 5620 | assert!(tallest > 25., "loudest column only {tallest} px of 30"); |
| 5621 | assert!( |
| 5622 | flattest < 8., |
| 5623 | "quietest column {flattest} px -- no dips drawn" |
| 5624 | ); |
| 5625 | |
| 5626 | assert!( |
| 5627 | engine::waveform::peaks(asset("test_baseline.mp4"), 0, WAVE_BPS) |
| 5628 | .expect("open the fixture") |
| 5629 | .is_none() |
| 5630 | ); |
| 5631 | } |
| 5632 | |
| 5633 | #[test] |
| 5634 | fn every_mark_on_a_clip_is_legible_on_it() { |
| 5635 | for (i, tint) in SOURCE_TINTS.iter().enumerate() { |
| 5636 | |
| 5637 | assert!( |
| 5638 | contrast(INK, *tint) >= 4.5, |
| 5639 | "source {i}: label contrast {:.2}", |
| 5640 | contrast(INK, *tint) |
| 5641 | ); |
| 5642 | |
| 5643 | assert!( |
| 5644 | contrast(INK_DIM, *tint) >= 3., |
| 5645 | "source {i}: waveform contrast {:.2}", |
| 5646 | contrast(INK_DIM, *tint) |
| 5647 | ); |
| 5648 | } |
| 5649 | |
| 5650 | assert!(contrast(INK, SELECTED) >= 4.5); |
| 5651 | assert!(contrast(INK_DIM, SELECTED) >= 3.); |
| 5652 | |
| 5653 | |
| 5654 | assert!(contrast(LETTERBOX, SURFACE) >= 1.5); |
| 5655 | assert!(contrast(ACCENT, LETTERBOX) >= 3.); |
| 5656 | |
| 5657 | assert!((contrast(0xffffff, 0x000000) - 21.).abs() < 0.01); |
| 5658 | } |
| 5659 | |
| 5660 | #[test] |
| 5661 | fn source_tints_differ_per_source_and_cycle() { |
| 5662 | |
| 5663 | assert_eq!(source_tint(0), SURFACE); |
| 5664 | |
| 5665 | assert_ne!(source_tint(0), source_tint(1)); |
| 5666 | assert_ne!(source_tint(1), source_tint(2)); |
| 5667 | assert_ne!(source_tint(2), source_tint(3)); |
| 5668 | |
| 5669 | assert_eq!(source_tint(4), source_tint(0)); |
| 5670 | assert_eq!(source_tint(9), source_tint(1)); |
| 5671 | assert_eq!(source_tint(usize::MAX), SOURCE_TINTS[usize::MAX % 4]); |
| 5672 | } |
| 5673 | } |
| 5674 | |
| 5675 | fn main() { |
| 5676 | let arg = std::env::args().nth(1).map(PathBuf::from); |
| 5677 | |
| 5678 | |
| 5679 | |
| 5680 | |
| 5681 | |
| 5682 | let mut session = match &arg { |
| 5683 | Some(arg) => { |
| 5684 | let opened = if is_project(arg) { |
| 5685 | PlaybackSession::open_project(arg) |
| 5686 | } else { |
| 5687 | PlaybackSession::open(arg) |
| 5688 | }; |
| 5689 | match opened { |
| 5690 | Ok(v) => Some(v), |
| 5691 | Err(e) => { |
| 5692 | |
| 5693 | |
| 5694 | |
| 5695 | eprintln!("cannot open {}: {e}", arg.display()); |
| 5696 | std::process::exit(1); |
| 5697 | } |
| 5698 | } |
| 5699 | } |
| 5700 | None => None, |
| 5701 | }; |
| 5702 | |
| 5703 | |
| 5704 | |
| 5705 | let (keymap, mut notice) = Keymap::load(); |
| 5706 | if let Some(text) = ¬ice { |
| 5707 | eprintln!("{text}"); |
| 5708 | } |
| 5709 | |
| 5710 | |
| 5711 | |
| 5712 | for extra in std::env::args().skip(2) { |
| 5713 | |
| 5714 | if let Some(session) = &mut session |
| 5715 | && let Err(e) = session.import(std::path::Path::new(&extra)) |
| 5716 | { |
| 5717 | let text = format!("IMPORT FAILED: {e}"); |
| 5718 | eprintln!("{extra}: {text}"); |
| 5719 | notice.get_or_insert(text); |
| 5720 | } |
| 5721 | } |
| 5722 | |
| 5723 | |
| 5724 | if let Some(reason) = session |
| 5725 | .as_ref() |
| 5726 | .and_then(PlaybackSession::audio_disabled_reason) |
| 5727 | { |
| 5728 | let text = format!("NO AUDIO: {reason}"); |
| 5729 | eprintln!("{text}"); |
| 5730 | notice.get_or_insert(text); |
| 5731 | } |
| 5732 | let meta = session.as_ref().map(|session| *session.meta()); |
| 5733 | |
| 5734 | |
| 5735 | let out = session.as_ref().map_or_else(PathBuf::new, |session| { |
| 5736 | export_path(&session.sources()[0].path) |
| 5737 | }); |
| 5738 | let project = match &arg { |
| 5739 | Some(arg) if is_project(arg) => arg.clone(), |
| 5740 | Some(arg) => project_path(arg), |
| 5741 | |
| 5742 | None => PathBuf::new(), |
| 5743 | }; |
| 5744 | let name: SharedString = arg |
| 5745 | .as_deref() |
| 5746 | .map_or_else(|| NO_FILE.into(), |arg| file_name(arg).into()); |
| 5747 | if let (Some(arg), Some(meta)) = (&arg, &meta) { |
| 5748 | println!( |
| 5749 | "{}: {}x{} @ {:.2} fps, {} samples", |
| 5750 | arg.display(), |
| 5751 | meta.width, |
| 5752 | meta.height, |
| 5753 | meta.frame_rate, |
| 5754 | meta.frame_count |
| 5755 | ); |
| 5756 | } |
| 5757 | |
| 5758 | Application::new().run(move |cx: &mut App| { |
| 5759 | |
| 5760 | |
| 5761 | let (w, h) = meta.map_or((1280., 720.), |meta| { |
| 5762 | (meta.width as f32, meta.height as f32) |
| 5763 | }); |
| 5764 | let bounds = Bounds::centered(None, size(px(w), px(h)), cx); |
| 5765 | cx.open_window( |
| 5766 | WindowOptions { |
| 5767 | window_bounds: Some(WindowBounds::Windowed(bounds)), |
| 5768 | titlebar: Some(TitlebarOptions { |
| 5769 | title: Some("edith".into()), |
| 5770 | ..Default::default() |
| 5771 | }), |
| 5772 | |
| 5773 | |
| 5774 | |
| 5775 | |
| 5776 | |
| 5777 | app_id: Some("edith".to_string()), |
| 5778 | ..Default::default() |
| 5779 | }, |
| 5780 | |window, cx| { |
| 5781 | let player = cx.new(|cx| Player { |
| 5782 | |
| 5783 | |
| 5784 | |
| 5785 | |
| 5786 | pending_seek: session.is_some(), |
| 5787 | session, |
| 5788 | |
| 5789 | |
| 5790 | volume: Volume::default(), |
| 5791 | |
| 5792 | |
| 5793 | fps: meta.map_or(30., |meta| meta.frame_rate), |
| 5794 | name: name.clone(), |
| 5795 | image: None, |
| 5796 | held: None, |
| 5797 | eos: false, |
| 5798 | done: false, |
| 5799 | ruler: Rc::default(), |
| 5800 | selected: None, |
| 5801 | context_menu: None, |
| 5802 | selected_asset: None, |
| 5803 | waves: HashMap::new(), |
| 5804 | streams: HashMap::new(), |
| 5805 | clipboard: None, |
| 5806 | scrubbing: false, |
| 5807 | last_scrub: Instant::now(), |
| 5808 | last_target: 0, |
| 5809 | export: None, |
| 5810 | cancelling: false, |
| 5811 | export_path: out.clone(), |
| 5812 | project_path: project.clone(), |
| 5813 | keymap: keymap.clone(), |
| 5814 | keys_open: false, |
| 5815 | export_open: false, |
| 5816 | eq_open: None, |
| 5817 | |
| 5818 | |
| 5819 | eq_params: EqParams::default(), |
| 5820 | eq_band: 0, |
| 5821 | eq_dragging: false, |
| 5822 | eq_bar: Rc::default(), |
| 5823 | color_open: None, |
| 5824 | color_band: 0, |
| 5825 | |
| 5826 | |
| 5827 | quality: Quality::Auto, |
| 5828 | custom_mbps: 0, |
| 5829 | |
| 5830 | |
| 5831 | format: Format::default(), |
| 5832 | rebinding: None, |
| 5833 | notice: notice.clone().map(SharedString::from), |
| 5834 | |
| 5835 | |
| 5836 | titled: String::new(), |
| 5837 | displayed: 0, |
| 5838 | dropped: 0, |
| 5839 | started: None, |
| 5840 | focus: cx.focus_handle(), |
| 5841 | }); |
| 5842 | |
| 5843 | |
| 5844 | window.focus(&player.read(cx).focus); |
| 5845 | player |
| 5846 | }, |
| 5847 | ) |
| 5848 | .unwrap(); |
| 5849 | cx.activate(true); |
| 5850 | }); |
| 5851 | } |