-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase playback speed during a long press gesture (#746)
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// Copyright (c) SRG SSR. All rights reserved. | ||
// | ||
// License information is available from the LICENSE file. | ||
// | ||
|
||
import PillarboxPlayer | ||
import SwiftUI | ||
|
||
private struct HighSpeedCapsule: View { | ||
let speed: Float | ||
|
||
var body: some View { | ||
Text("\(speed, specifier: "%g×") \(Image(systemName: "forward.fill"))") | ||
.font(.footnote) | ||
.bold() | ||
.padding(.horizontal, 10) | ||
.padding(.vertical, 5) | ||
.foregroundStyle(.white) | ||
.background(.black.opacity(0.7)) | ||
.clipShape(Capsule()) | ||
} | ||
} | ||
|
||
private struct HighSpeedGestureView<Content>: View where Content: View { | ||
@GestureState private var isLongPressing = false | ||
@State private var timer: Timer? | ||
|
||
let content: () -> Content | ||
let action: (_ finished: Bool) -> Void | ||
|
||
var body: some View { | ||
content() | ||
.simultaneousGesture(longPressGesture()) | ||
.onChange(of: isLongPressing) { isPressing in | ||
timer?.invalidate() | ||
if !isPressing { | ||
action(true) | ||
} | ||
else { | ||
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in | ||
action(false) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private func longPressGesture() -> some Gesture { | ||
LongPressGesture(minimumDuration: .infinity, maximumDistance: .infinity) | ||
.updating($isLongPressing) { value, state, _ in | ||
state = value | ||
} | ||
} | ||
} | ||
|
||
private struct HighSpeedView<Content>: View where Content: View { | ||
private let highSpeed: Float = 2 | ||
@State private var speed: Float? | ||
|
||
private var displaysCapsule: Bool { | ||
speed != nil && speed != highSpeed && player.playbackSpeedRange.contains(highSpeed) && player.playbackState == .playing | ||
} | ||
|
||
@ObservedObject var player: Player | ||
let content: () -> Content | ||
|
||
var body: some View { | ||
HighSpeedGestureView(content: content) { finished in | ||
if !finished { | ||
speed = player.effectivePlaybackSpeed | ||
player.setDesiredPlaybackSpeed(highSpeed) | ||
} | ||
else if let speed { | ||
player.setDesiredPlaybackSpeed(speed) | ||
self.speed = nil | ||
} | ||
} | ||
.overlay(alignment: .top) { | ||
HighSpeedCapsule(speed: highSpeed) | ||
.opacity(displaysCapsule ? 1 : 0) | ||
.animation(.easeInOut(duration: 0.1), value: displaysCapsule) | ||
.padding() | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
func supportsHighSpeed(for player: Player) -> some View { | ||
HighSpeedView(player: player) { | ||
self | ||
} | ||
} | ||
} |