Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 51 additions & 9 deletions apps/desktop-gpui/src/editor_audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Self>) -> 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)
}
}

Expand All @@ -242,6 +274,7 @@ fn render_library_row(
id: &'static str,
name: &'static str,
importing: bool,
replacing: bool,
cx: &mut Context<EditorWindow>,
) -> AnyElement {
div()
Expand All @@ -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()
Expand Down
51 changes: 51 additions & 0 deletions apps/desktop-gpui/src/editor_edits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>,
playhead: f64,
dropped_at: Option<f64>,
) -> (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<T: TrackSegmentOps>(segments: &[T]) -> u32 {
segments
.iter()
Expand Down Expand Up @@ -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);
Expand Down
65 changes: 54 additions & 11 deletions apps/desktop-gpui/src/editor_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenToolbarMenu>,
text_menu: Option<OpenTextMenu>,
text_style_clipboard: Option<crate::editor_panels::TextStyle>,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -5595,6 +5599,30 @@ impl EditorWindow {
.detach();
}

fn drop_library_track(
&mut self,
drag: &crate::editor_audio::LibraryDrag,
lane: Option<u32>,
window: &mut Window,
cx: &mut Context<Self>,
) {
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<Self>) {
let Some(request) = self.begin_audio_import(window, cx) else {
return;
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -7838,20 +7873,17 @@ impl EditorWindow {
&mut self,
lane: u32,
imported: ImportedAudio,
start_at: Option<f64>,
window: &mut Window,
cx: &mut Context<Self>,
) {
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| {
Expand Down Expand Up @@ -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::<crate::editor_audio::LibraryDrag>(|style, _, _, _| {
style.opacity(0.7)
})
})
.child(timeline::render_row(
&theme,
model,
Expand Down
Loading