diff --git a/apps/desktop-gpui/src/editor_audio.rs b/apps/desktop-gpui/src/editor_audio.rs index 3279b2efe9a..b76c9dc3d56 100644 --- a/apps/desktop-gpui/src/editor_audio.rs +++ b/apps/desktop-gpui/src/editor_audio.rs @@ -3,8 +3,9 @@ use std::path::{Path, PathBuf}; use cap_enc_ffmpeg::remux::get_media_duration; use cap_project::{ProjectConfiguration, TimelineConfiguration}; use gpui::{ - AnyElement, Context, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, - SharedString, StatefulInteractiveElement, Styled, div, prelude::FluentBuilder, px, svg, + AnyElement, AppContext, Context, FontWeight, Hsla, InteractiveElement, IntoElement, + ParentElement, Render, SharedString, StatefulInteractiveElement, Styled, Window, div, + prelude::FluentBuilder, px, svg, }; use crate::{editor_window::EditorWindow, theme::Theme, ui}; @@ -225,15 +226,46 @@ impl EditorWindow { this.import_audio_from_picker(window, cx); })), ) + .child(div().flex().flex_col().gap(px(6.)).children( + AUDIO_LIBRARY.iter().copied().map(|(id, name)| { + render_library_row(&theme, id, name, importing, is_replace, cx) + }), + )) + .into_any_element() + } +} + +#[derive(Debug, Clone, Copy)] +pub struct LibraryDrag { + pub id: &'static str, + pub name: &'static str, +} + +struct LibraryDragPreview { + theme: Theme, + name: &'static str, +} + +impl Render for LibraryDragPreview { + fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { + div() + .flex() + .flex_row() + .items_center() + .gap(px(6.)) + .px(px(8.)) + .py(px(4.)) + .rounded(px(6.)) + .bg(Hsla::from(self.theme.gray_12)) + .text_color(Hsla::from(self.theme.gray_1)) + .text_size(px(12.)) .child( - div().flex().flex_col().gap(px(6.)).children( - AUDIO_LIBRARY - .iter() - .copied() - .map(|(id, name)| render_library_row(&theme, id, name, importing, cx)), - ), + svg() + .path("icons/music.svg") + .size(px(12.)) + .text_color(Hsla::from(self.theme.gray_1)), ) - .into_any_element() + .child(self.name) } } @@ -242,6 +274,7 @@ fn render_library_row( id: &'static str, name: &'static str, importing: bool, + replacing: bool, cx: &mut Context, ) -> AnyElement { div() @@ -264,6 +297,15 @@ fn render_library_row( this.add_library_track(id, name, window, cx); })) }) + .when(!importing && !replacing, |row| { + let preview_theme = *theme; + row.on_drag(LibraryDrag { id, name }, move |drag, _, _, cx| { + cx.new(|_| LibraryDragPreview { + theme: preview_theme, + name: drag.name, + }) + }) + }) .child( div() .flex() diff --git a/apps/desktop-gpui/src/editor_edits.rs b/apps/desktop-gpui/src/editor_edits.rs index 14bdd11a14a..95e15cebd10 100644 --- a/apps/desktop-gpui/src/editor_edits.rs +++ b/apps/desktop-gpui/src/editor_edits.rs @@ -1659,6 +1659,32 @@ pub const DEFAULT_ZOOM_AMOUNT: f64 = 1.5; pub const MIN_AUDIO_SEGMENT_DURATION: f64 = 0.5; +/// Where an imported audio track goes, as `(start, end)`. From the picker it +/// starts at the playhead and slides back so the whole track fits. A drop +/// keeps the time it landed on (pulled in only far enough to leave a minimum +/// segment) and the segment is shortened to end with the project instead. +pub fn audio_import_placement( + total: f64, + source_duration: Option, + playhead: f64, + dropped_at: Option, +) -> (f64, f64) { + let source = source_duration.unwrap_or(total); + let length = + MIN_AUDIO_SEGMENT_DURATION.max(source.min(if total > 0.0 { total } else { source })); + match dropped_at { + Some(dropped) => { + let start = dropped.clamp(0.0, (total - MIN_AUDIO_SEGMENT_DURATION).max(0.0)); + let length = length.min((total - start).max(MIN_AUDIO_SEGMENT_DURATION)); + (start, start + length) + } + None => { + let start = playhead.max(0.0).min((total - length).max(0.0)); + (start, start + length) + } + } +} + pub fn used_lane_count(segments: &[T]) -> u32 { segments .iter() @@ -2893,6 +2919,31 @@ mod tests { // -- Selection ---------------------------------------------------------- + #[test] + fn a_dropped_track_keeps_its_drop_time_and_is_cut_to_fit() { + // A 120s library track dropped at 0:20 on a 60s project. + let (start, end) = audio_import_placement(60.0, Some(120.0), 5.0, Some(20.0)); + assert_eq!((start, end), (20.0, 60.0)); + // Near the end it keeps a minimum segment inside the project. + let (start, end) = audio_import_placement(60.0, Some(120.0), 5.0, Some(59.99)); + assert!(start <= 60.0 - MIN_AUDIO_SEGMENT_DURATION + 1e-9); + assert!((end - 60.0).abs() < 1e-9); + // A short track keeps its own length. + assert_eq!( + audio_import_placement(60.0, Some(10.0), 5.0, Some(30.0)), + (30.0, 40.0) + ); + // From the picker it still slides back to fit, starting at the playhead. + assert_eq!( + audio_import_placement(60.0, Some(10.0), 55.0, None), + (50.0, 60.0) + ); + assert_eq!( + audio_import_placement(60.0, Some(120.0), 30.0, None), + (0.0, 60.0) + ); + } + #[test] fn a_plain_click_selects_one_segment() { let selection = click_selection(None, TrackKind::Zoom, 3, false, false); diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index ecbc4b4254b..5500de302f7 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -1556,6 +1556,9 @@ pub struct EditorWindow { preview_quality: crate::store::EditorPreviewQuality, pub(crate) tracks: TrackLanes, + /// Where a library track dropped on the timeline goes: the import's + /// generation and the output time under the pointer at the drop. + audio_drop_start: Option<(u64, f64)>, toolbar_menu: Option, text_menu: Option, text_style_clipboard: Option, @@ -2134,6 +2137,7 @@ impl EditorWindow { snap_guides: Vec::new(), preview_quality: crate::store::GeneralSettings::load().editor_preview_quality, tracks: TrackLanes::from_project(&ProjectConfiguration::default(), false), + audio_drop_start: None, toolbar_menu: None, text_menu: None, text_style_clipboard: None, @@ -5595,6 +5599,30 @@ impl EditorWindow { .detach(); } + fn drop_library_track( + &mut self, + drag: &crate::editor_audio::LibraryDrag, + lane: Option, + window: &mut Window, + cx: &mut Context, + ) { + if !self.project_ready() + || matches!( + self.audio_picker, + Some(crate::editor_audio::AudioPicker::Replace { .. }) + ) + { + return; + } + let viewport_width: f32 = window.viewport_size().width.into(); + let x = f32::from(window.mouse_position().x).max(timeline::content_left()); + let time = self.time_at(x, viewport_width).max(0.); + let lane = lane.unwrap_or(self.tracks.audio); + self.open_audio_picker(lane, cx); + self.audio_drop_start = Some((self.audio_picker_generation, time)); + self.add_library_track(drag.id, drag.name, window, cx); + } + pub(crate) fn import_audio_from_picker(&mut self, window: &mut Window, cx: &mut Context) { let Some(request) = self.begin_audio_import(window, cx) else { return; @@ -5613,6 +5641,13 @@ impl EditorWindow { return; } self.audio_import_pending = None; + // Taken on every path, so a failed or empty import can't leave the + // drop time behind for the next click on a library row. + let dropped_at = self + .audio_drop_start + .take() + .filter(|(generation, _)| *generation == request.generation) + .map(|(_, time)| time); if !request.accepts( self.audio_picker, self.audio_picker_generation, @@ -5641,7 +5676,7 @@ impl EditorWindow { }; match request.picker { crate::editor_audio::AudioPicker::Add { lane } => { - self.commit_audio_import(lane, imported, window, cx); + self.commit_audio_import(lane, imported, dropped_at, window, cx); } crate::editor_audio::AudioPicker::Replace { index } => { self.replace_audio_segment( @@ -7838,20 +7873,17 @@ impl EditorWindow { &mut self, lane: u32, imported: ImportedAudio, + start_at: Option, window: &mut Window, cx: &mut Context, ) { - let total = self.total_duration(); let has_source = imported.duration > 0.0; - let source_duration = if has_source { imported.duration } else { total }; - let length = edits::MIN_AUDIO_SEGMENT_DURATION.max(source_duration.min(if total > 0.0 { - total - } else { - source_duration - })); - let max_start = (total - length).max(0.0); - let start = self.playhead.max(0.0).min(max_start); - let end = start + length; + let (start, end) = edits::audio_import_placement( + self.total_duration(), + has_source.then_some(imported.duration), + self.playhead, + start_at, + ); let path = imported.path.clone(); let inserted = self.edit( |timeline| { @@ -11045,6 +11077,17 @@ impl EditorWindow { } }), ) + .on_drop(cx.listener( + move |this, drag: &crate::editor_audio::LibraryDrag, window, cx| { + let lane = (kind == TrackKind::Audio).then_some(lane); + this.drop_library_track(drag, lane, window, cx); + }, + )) + .when(kind == TrackKind::Audio, |row| { + row.drag_over::(|style, _, _, _| { + style.opacity(0.7) + }) + }) .child(timeline::render_row( &theme, model,