From 48f20f466948a5438d20f4c4421075ed13ceb43e Mon Sep 17 00:00:00 2001 From: Yoav Levavi Date: Wed, 21 Oct 2026 18:39:30 +0300 Subject: [PATCH 1/3] gpui: drag music from the library onto the timeline A library row can be dragged onto the timeline: dropped on an audio lane it lands there, anywhere else it gets a new lane, and it starts at the time under the pointer instead of the playhead. Co-Authored-By: Claude Opus 5.5 --- apps/desktop-gpui/src/editor_audio.rs | 63 ++++++++++++++++++++++---- apps/desktop-gpui/src/editor_window.rs | 51 ++++++++++++++++++++- 2 files changed, 103 insertions(+), 11 deletions(-) diff --git a/apps/desktop-gpui/src/editor_audio.rs b/apps/desktop-gpui/src/editor_audio.rs index 3279b2efe9a..f45ac8740ca 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,48 @@ 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() + } +} + +/// A library track being dragged toward the timeline. +#[derive(Debug, Clone, Copy)] +pub struct LibraryDrag { + pub id: &'static str, + pub name: &'static str, +} + +/// The chip that follows the pointer during a [`LibraryDrag`]. +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 +276,7 @@ fn render_library_row( id: &'static str, name: &'static str, importing: bool, + replacing: bool, cx: &mut Context, ) -> AnyElement { div() @@ -264,6 +299,16 @@ fn render_library_row( this.add_library_track(id, name, window, cx); })) }) + // Drag the row onto a timeline lane to add the track where it lands. + .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_window.rs b/apps/desktop-gpui/src/editor_window.rs index d19e1cd05c6..17c7dd9b248 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -1544,6 +1544,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, frame_controls: frame::FrameControls, add_track: Option, @@ -2112,6 +2115,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, frame_controls: frame::FrameControls::default(), add_track: None, @@ -5482,6 +5486,32 @@ impl EditorWindow { .detach(); } + /// A library track dropped on the timeline: added on the audio lane it + /// landed on (a new lane anywhere else), starting under the pointer. + 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; @@ -5528,7 +5558,12 @@ impl EditorWindow { }; match request.picker { crate::editor_audio::AudioPicker::Add { lane } => { - self.commit_audio_import(lane, imported, window, cx); + let dropped_at = self + .audio_drop_start + .take() + .filter(|(generation, _)| *generation == request.generation) + .map(|(_, time)| time); + self.commit_audio_import(lane, imported, dropped_at, window, cx); } crate::editor_audio::AudioPicker::Replace { index } => { self.replace_audio_segment( @@ -7725,6 +7760,7 @@ impl EditorWindow { &mut self, lane: u32, imported: ImportedAudio, + start_at: Option, window: &mut Window, cx: &mut Context, ) { @@ -7737,7 +7773,7 @@ impl EditorWindow { source_duration })); let max_start = (total - length).max(0.0); - let start = self.playhead.max(0.0).min(max_start); + let start = start_at.unwrap_or(self.playhead).max(0.0).min(max_start); let end = start + length; let path = imported.path.clone(); let inserted = self.edit( @@ -10686,6 +10722,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, From 7431f96c8615342b731808d8303f08cc106dcb80 Mon Sep 17 00:00:00 2001 From: Yoav Levavi Date: Sun, 25 Oct 2026 17:30:26 +0200 Subject: [PATCH 2/3] Library drag: keep the drop time, cut the track to fit; merge main - audio_import_placement keeps a dropped track's start (pulled in only far enough to leave a minimum segment) and shortens it to end with the project, instead of sliding it back to 0:00 when the track is longer than the project. Picker imports still start at the playhead and slide to fit. - finish_audio_import takes audio_drop_start on every path, so a failed or empty copy can't make a later click land at the old drop time. Co-Authored-By: Claude Opus 5.5 --- apps/desktop-gpui/src/editor_edits.rs | 51 ++++++++++++++++++++++++++ apps/desktop-gpui/src/editor_window.rs | 28 +++++++------- 2 files changed, 64 insertions(+), 15 deletions(-) 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 2346a9c2f2a..897ced9e53c 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -5643,6 +5643,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, @@ -5671,11 +5678,6 @@ impl EditorWindow { }; match request.picker { crate::editor_audio::AudioPicker::Add { lane } => { - let dropped_at = self - .audio_drop_start - .take() - .filter(|(generation, _)| *generation == request.generation) - .map(|(_, time)| time); self.commit_audio_import(lane, imported, dropped_at, window, cx); } crate::editor_audio::AudioPicker::Replace { index } => { @@ -7877,17 +7879,13 @@ impl EditorWindow { 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 = start_at.unwrap_or(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| { From a299a47c775294b6a79cda8c9002b712098effc9 Mon Sep 17 00:00:00 2001 From: Yoav Levavi Date: Tue, 3 Nov 2026 05:38:03 +0200 Subject: [PATCH 3/3] Library drag: drop comments that restate the code Co-Authored-By: Claude Opus 5.5 --- apps/desktop-gpui/src/editor_audio.rs | 3 --- apps/desktop-gpui/src/editor_window.rs | 2 -- 2 files changed, 5 deletions(-) diff --git a/apps/desktop-gpui/src/editor_audio.rs b/apps/desktop-gpui/src/editor_audio.rs index f45ac8740ca..b76c9dc3d56 100644 --- a/apps/desktop-gpui/src/editor_audio.rs +++ b/apps/desktop-gpui/src/editor_audio.rs @@ -235,14 +235,12 @@ impl EditorWindow { } } -/// A library track being dragged toward the timeline. #[derive(Debug, Clone, Copy)] pub struct LibraryDrag { pub id: &'static str, pub name: &'static str, } -/// The chip that follows the pointer during a [`LibraryDrag`]. struct LibraryDragPreview { theme: Theme, name: &'static str, @@ -299,7 +297,6 @@ fn render_library_row( this.add_library_track(id, name, window, cx); })) }) - // Drag the row onto a timeline lane to add the track where it lands. .when(!importing && !replacing, |row| { let preview_theme = *theme; row.on_drag(LibraryDrag { id, name }, move |drag, _, _, cx| { diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index 897ced9e53c..5500de302f7 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -5599,8 +5599,6 @@ impl EditorWindow { .detach(); } - /// A library track dropped on the timeline: added on the audio lane it - /// landed on (a new lane anywhere else), starting under the pointer. fn drop_library_track( &mut self, drag: &crate::editor_audio::LibraryDrag,