Skip to content
Merged
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
16 changes: 13 additions & 3 deletions apps/desktop-gpui/src/controls_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ impl ControlsWindow {
})
}

/// `formatTime`: `M:SS`, hours folded into minutes the same way.
/// `formatTime`, through the editor clock's formatter, so a recording past
/// an hour reads `H:MM:SS` where the source folds the hours into minutes.
fn format_elapsed(elapsed: Duration) -> String {
let total = elapsed.as_secs();
format!("{}:{:02}", total / 60, total % 60)
crate::editor_timeline::format_time(elapsed.as_secs_f64())
}

fn action_button(
Expand Down Expand Up @@ -609,6 +609,16 @@ mod repaint_tests {
assert_eq!(until_next_second(Duration::ZERO), Duration::from_secs(1));
}

#[test]
fn the_timer_shows_hours_from_an_hour_on() {
let label = |seconds| ControlsWindow::format_elapsed(Duration::from_secs_f64(seconds));
assert_eq!(label(0.), "0:00");
assert_eq!(label(95.7), "1:35");
assert_eq!(label(3_599.9), "59:59");
assert_eq!(label(3_600.), "1:00:00");
assert_eq!(label(3_905.), "1:05:05");
}

#[test]
fn a_resumed_timer_wakes_at_the_next_second_and_a_paused_one_idles() {
assert_eq!(
Expand Down
15 changes: 9 additions & 6 deletions apps/desktop-gpui/src/editor_clips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use gpui::{
use crate::{
app_windows,
editor_edits::{self as edits, TrackSegmentOps},
editor_timeline::clip_timeline_offsets,
editor_timeline::{clip_timeline_offsets, format_time},
editor_window::EditorWindow,
session::{Phase, RecordingSession},
theme::Theme,
Expand All @@ -53,13 +53,13 @@ use crate::{
// Naming (`ClipsSidebar.tsx:558-609`)
// ---------------------------------------------------------------------------

/// `formatClipDuration` (`ClipsSidebar.tsx:79-85`).
/// `formatClipDuration` (`ClipsSidebar.tsx:79-85`), with hours from an hour on
/// like the editor's clock, where the source counts minutes past 59.
pub(crate) fn format_clip_duration(seconds: f64) -> String {
if !seconds.is_finite() || seconds <= 0.0 {
return "0:00".to_string();
}
let total = seconds.round() as i64;
format!("{}:{:02}", total / 60, total % 60)
format_time(seconds.round())
}

/// `clipLabel` (`:563`).
Expand Down Expand Up @@ -119,8 +119,8 @@ fn display_name_at(segments: &[TimelineSegment], index: usize) -> String {
}
}

/// `segmentDescription` (`:598-609`): `m:ss`, prefixed with the parent clip's
/// label for a split.
/// `segmentDescription` (`:598-609`): `m:ss` (`h:mm:ss` from an hour on),
/// prefixed with the parent clip's label for a split.
pub(crate) fn segment_description(segments: &[TimelineSegment], index: usize) -> String {
let formatted = format_clip_duration(segments[index].duration());
if segment_split_number(segments, index) > 1 {
Expand Down Expand Up @@ -3878,6 +3878,9 @@ mod tests {
assert_eq!(format_clip_duration(0.6), "0:01");
assert_eq!(format_clip_duration(59.6), "1:00");
assert_eq!(format_clip_duration(75.0), "1:15");
assert_eq!(format_clip_duration(3_599.4), "59:59");
assert_eq!(format_clip_duration(3_599.6), "1:00:00");
assert_eq!(format_clip_duration(3_905.0), "1:05:05");
assert_eq!(format_clip_duration(600.0), "10:00");
}

Expand Down
14 changes: 12 additions & 2 deletions apps/desktop-gpui/src/editor_panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6230,12 +6230,19 @@ impl EditorWindow {
}
}

/// `formatTime` -- the `M:SS` clock the timing cards print.
/// `formatTime` -- the `M:SS` clock the timing cards print, as `H:MM:SS` from
/// an hour on.
fn format_time(seconds: f64) -> String {
let seconds = seconds.max(0.);
let hours = (seconds / 3600.).floor() as u32;
let seconds = seconds - f64::from(hours) * 3600.;
let minutes = (seconds / 60.).floor() as u32;
let rest = seconds - f64::from(minutes) * 60.;
format!("{minutes}:{rest:04.1}")
if hours > 0 {
format!("{hours}:{minutes:02}:{rest:04.1}")
} else {
format!("{minutes}:{rest:04.1}")
}
}

impl EditorWindow {
Expand Down Expand Up @@ -8420,6 +8427,9 @@ mod tests {
assert_eq!(format_time(0.), "0:00.0");
assert_eq!(format_time(9.25), "0:09.2");
assert_eq!(format_time(75.5), "1:15.5");
assert_eq!(format_time(3599.5), "59:59.5");
assert_eq!(format_time(3600.), "1:00:00.0");
assert_eq!(format_time(3905.5), "1:05:05.5");
}

// -- Text presets ------------------------------------------------------
Expand Down
31 changes: 24 additions & 7 deletions apps/desktop-gpui/src/editor_timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,17 @@ pub fn marking_resolution(zoom: f64) -> f64 {
}

/// `formatTime` (`routes/editor/utils.ts:1-13`) -- `M:SS`, which is both the
/// transport clock and the **ruler's** label (`TL/index.tsx:48, 1597`).
/// transport clock and the **ruler's** label (`TL/index.tsx:48, 1597`). One
/// deliberate deviation: from an hour on it prints `H:MM:SS`, where the source
/// prints a minute count such as `65:05`.
pub fn format_time(seconds: f64) -> String {
let seconds = seconds.max(0.0);
let minutes = (seconds / 60.0).floor() as u64;
let secs = (seconds % 60.0).floor() as u64;
format!("{minutes}:{secs:02}")
let total = seconds.max(0.0).floor() as u64;
let (hours, minutes, secs) = (total / 3600, total / 60 % 60, total % 60);
if hours > 0 {
format!("{hours}:{minutes:02}:{secs:02}")
} else {
format!("{minutes}:{secs:02}")
}
}

/// The *other* `formatTime`, the clip track's own (`TL/ClipTrack.tsx:129-141`):
Expand Down Expand Up @@ -1947,8 +1952,8 @@ pub fn render_ruler(theme: &Theme, view: TimelineView, viewport_width: f32) -> A
// overhang into the icon gutter
// (`TL/index.tsx:1591-1594`). gpui has no
// transform, so the label sits in a fixed box wide
// enough for `M:SS` at any minute count and is
// centred inside it.
// enough for `M:SS`, or `H:MM:SS` below ten hours,
// and is centred inside it.
.when(second != 0., |this| this.left(px(-22.)))
.w(px(44.))
.flex()
Expand Down Expand Up @@ -3792,6 +3797,18 @@ mod tests {

// -- The ruler ----------------------------------------------------------

#[test]
fn the_clock_and_ruler_show_hours_from_an_hour_on() {
assert_eq!(format_time(0.), "0:00");
assert_eq!(format_time(95.7), "1:35");
assert_eq!(format_time(3599.9), "59:59");
assert_eq!(format_time(3600.), "1:00:00");
assert_eq!(format_time(3905.), "1:05:05");
assert_eq!(format_time(5400.), "1:30:00");
assert_eq!(format_time(36000.), "10:00:00");
assert_eq!(format_time(-3.), "0:00");
}

#[test]
fn clip_markings_stop_at_the_clip_edge() {
// A 30s clip 10 minutes into a fully zoomed-out 30-minute timeline:
Expand Down
Loading