|
| 1 | +import Foundation |
| 2 | + |
| 3 | +enum TimelineTrim { |
| 4 | + enum Edge { |
| 5 | + case start |
| 6 | + case end |
| 7 | + } |
| 8 | + |
| 9 | + enum DragTarget: Equatable { |
| 10 | + case start |
| 11 | + case end |
| 12 | + case playhead |
| 13 | + } |
| 14 | + |
| 15 | + static let minimumDuration = 0.1 |
| 16 | + |
| 17 | + static func clampedRange(start: Double, end: Double, duration: Double) -> (start: Double, end: Double) { |
| 18 | + let duration = max(0, duration) |
| 19 | + guard duration > 0 else { return (0, 0) } |
| 20 | + |
| 21 | + let minDuration = min(minimumDuration, duration) |
| 22 | + var clampedStart = clamp(start, lower: 0, upper: duration) |
| 23 | + var clampedEnd = clamp(end > 0 ? end : duration, lower: 0, upper: duration) |
| 24 | + |
| 25 | + if clampedEnd <= clampedStart { |
| 26 | + clampedEnd = duration |
| 27 | + } |
| 28 | + |
| 29 | + if clampedEnd - clampedStart < minDuration { |
| 30 | + if clampedStart + minDuration <= duration { |
| 31 | + clampedEnd = clampedStart + minDuration |
| 32 | + } else { |
| 33 | + clampedStart = max(0, duration - minDuration) |
| 34 | + clampedEnd = duration |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return (clampedStart, clampedEnd) |
| 39 | + } |
| 40 | + |
| 41 | + static func updatedRange( |
| 42 | + moving edge: Edge, |
| 43 | + to proposedTime: Double, |
| 44 | + start: Double, |
| 45 | + end: Double, |
| 46 | + duration: Double |
| 47 | + ) -> (start: Double, end: Double) { |
| 48 | + let duration = max(0, duration) |
| 49 | + guard duration > 0 else { return (0, 0) } |
| 50 | + |
| 51 | + let minDuration = min(minimumDuration, duration) |
| 52 | + switch edge { |
| 53 | + case .start: |
| 54 | + let clampedEnd = clamp(end, lower: minDuration, upper: duration) |
| 55 | + let clampedStart = clamp(proposedTime, lower: 0, upper: clampedEnd - minDuration) |
| 56 | + return (clampedStart, clampedEnd) |
| 57 | + case .end: |
| 58 | + let clampedStart = clamp(start, lower: 0, upper: max(0, duration - minDuration)) |
| 59 | + let clampedEnd = clamp(proposedTime, lower: clampedStart + minDuration, upper: duration) |
| 60 | + return (clampedStart, clampedEnd) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static func clampedTime(_ seconds: Double, start: Double, end: Double, duration: Double) -> Double { |
| 65 | + let range = clampedRange(start: start, end: end, duration: duration) |
| 66 | + return clamp(seconds, lower: range.start, upper: range.end) |
| 67 | + } |
| 68 | + |
| 69 | + static func time(forFraction fraction: Double, duration: Double) -> Double { |
| 70 | + clamp(fraction, lower: 0, upper: 1) * max(0, duration) |
| 71 | + } |
| 72 | + |
| 73 | + static func fraction(forTime seconds: Double, duration: Double) -> Double { |
| 74 | + guard duration > 0 else { return 0 } |
| 75 | + return clamp(seconds / duration, lower: 0, upper: 1) |
| 76 | + } |
| 77 | + |
| 78 | + static func fraction(forX x: Double, width: Double) -> Double { |
| 79 | + guard width > 0 else { return 0 } |
| 80 | + return clamp(x / width, lower: 0, upper: 1) |
| 81 | + } |
| 82 | + |
| 83 | + static func playheadFraction( |
| 84 | + forTime seconds: Double, |
| 85 | + start: Double, |
| 86 | + end: Double, |
| 87 | + duration: Double |
| 88 | + ) -> Double { |
| 89 | + fraction(forTime: clampedTime(seconds, start: start, end: end, duration: duration), duration: duration) |
| 90 | + } |
| 91 | + |
| 92 | + static func thumbnailTileWidth(totalWidth: Double, count: Int, spacing: Double) -> Double { |
| 93 | + guard count > 0 else { return 0 } |
| 94 | + let availableWidth = max(0, totalWidth - spacing * Double(max(0, count - 1))) |
| 95 | + return max(1, availableWidth / Double(count)) |
| 96 | + } |
| 97 | + |
| 98 | + static func dragTarget( |
| 99 | + x: Double, |
| 100 | + startX: Double, |
| 101 | + endX: Double, |
| 102 | + playheadX: Double, |
| 103 | + handleHitWidth: Double, |
| 104 | + playheadHitWidth: Double |
| 105 | + ) -> DragTarget? { |
| 106 | + if abs(x - startX) <= handleHitWidth / 2 { |
| 107 | + return .start |
| 108 | + } |
| 109 | + if abs(x - endX) <= handleHitWidth / 2 { |
| 110 | + return .end |
| 111 | + } |
| 112 | + if abs(x - playheadX) <= playheadHitWidth / 2 { |
| 113 | + return .playhead |
| 114 | + } |
| 115 | + return nil |
| 116 | + } |
| 117 | + |
| 118 | + private static func clamp(_ value: Double, lower: Double, upper: Double) -> Double { |
| 119 | + min(max(value, lower), upper) |
| 120 | + } |
| 121 | +} |
0 commit comments