mirror of
https://github.com/yattee/yattee.git
synced 2025-12-16 13:08:14 +00:00
Compare commits
1 Commits
video-deta
...
fix-subtit
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3339e8cb1f |
@@ -23,15 +23,14 @@ final class MPVBackend: PlayerBackend {
|
|||||||
|
|
||||||
var stream: Stream?
|
var stream: Stream?
|
||||||
var video: Video?
|
var video: Video?
|
||||||
var captions: Captions? { didSet {
|
var captions: Captions? {
|
||||||
guard let captions else {
|
didSet {
|
||||||
if client?.areSubtitlesAdded == true {
|
Task {
|
||||||
client?.removeSubs()
|
await handleCaptionsChange()
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
addSubTrack(captions.url)
|
}
|
||||||
}}
|
|
||||||
var currentTime: CMTime?
|
var currentTime: CMTime?
|
||||||
|
|
||||||
var loadedVideo = false
|
var loadedVideo = false
|
||||||
@@ -617,10 +616,14 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addSubTrack(_ url: URL) {
|
func addSubTrack(_ url: URL) {
|
||||||
if client?.areSubtitlesAdded == true {
|
Task {
|
||||||
client?.removeSubs()
|
if let areSubtitlesAdded = client?.areSubtitlesAdded {
|
||||||
|
if await areSubtitlesAdded() {
|
||||||
|
await client?.removeSubs()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await client?.addSubTrack(url)
|
||||||
}
|
}
|
||||||
client?.addSubTrack(url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setVideoToAuto() {
|
func setVideoToAuto() {
|
||||||
@@ -684,6 +687,17 @@ final class MPVBackend: PlayerBackend {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func handleCaptionsChange() async {
|
||||||
|
guard let captions else {
|
||||||
|
if let isSubtitlesAdded = client?.areSubtitlesAdded, await isSubtitlesAdded() {
|
||||||
|
await client?.removeSubs()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
addSubTrack(captions.url)
|
||||||
|
}
|
||||||
|
|
||||||
private func handlePropertyChange(_ name: String, _ property: mpv_event_property) {
|
private func handlePropertyChange(_ name: String, _ property: mpv_event_property) {
|
||||||
switch name {
|
switch name {
|
||||||
case "pause":
|
case "pause":
|
||||||
|
|||||||
@@ -349,21 +349,15 @@ final class MPVClient: ObservableObject {
|
|||||||
return Int(fps.rounded())
|
return Int(fps.rounded())
|
||||||
}
|
}
|
||||||
|
|
||||||
var areSubtitlesAdded: Bool {
|
func areSubtitlesAdded() async -> Bool {
|
||||||
guard !mpv.isNil else { return false }
|
guard !mpv.isNil else { return false }
|
||||||
|
|
||||||
// Retrieve the number of tracks
|
let trackCount = await Task(operation: { getInt("track-list/count") }).value
|
||||||
let trackCount = getInt("track-list/count")
|
|
||||||
guard trackCount > 0 else { return false }
|
guard trackCount > 0 else { return false }
|
||||||
|
|
||||||
for index in 0 ..< trackCount {
|
for index in 0 ..< trackCount {
|
||||||
// Get the type of each track
|
if let trackType = await Task(operation: { getString("track-list/\(index)/type") }).value, trackType == "sub" {
|
||||||
if let trackType = getString("track-list/\(index)/type"), trackType == "sub" {
|
return true
|
||||||
// Check if the subtitle track is currently selected
|
|
||||||
let selected = getInt("track-list/\(index)/selected")
|
|
||||||
if selected == 1 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@@ -539,12 +533,16 @@ final class MPVClient: ObservableObject {
|
|||||||
command("video-add", args: [url.absoluteString])
|
command("video-add", args: [url.absoluteString])
|
||||||
}
|
}
|
||||||
|
|
||||||
func addSubTrack(_ url: URL) {
|
func addSubTrack(_ url: URL) async {
|
||||||
command("sub-add", args: [url.absoluteString])
|
await Task {
|
||||||
|
command("sub-add", args: [url.absoluteString])
|
||||||
|
}.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeSubs() {
|
func removeSubs() async {
|
||||||
command("sub-remove")
|
await Task {
|
||||||
|
command("sub-remove")
|
||||||
|
}.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func setVideoToAuto() {
|
func setVideoToAuto() {
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ struct ControlsOverlay: View {
|
|||||||
@ObservedObject private var player = PlayerModel.shared
|
@ObservedObject private var player = PlayerModel.shared
|
||||||
private var model = PlayerControlsModel.shared
|
private var model = PlayerControlsModel.shared
|
||||||
|
|
||||||
|
@State private var availableCaptions: [Captions] = []
|
||||||
|
@State private var isLoadingCaptions = true
|
||||||
@State private var contentSize: CGSize = .zero
|
@State private var contentSize: CGSize = .zero
|
||||||
|
|
||||||
@Default(.showMPVPlaybackStats) private var showMPVPlaybackStats
|
@Default(.showMPVPlaybackStats) private var showMPVPlaybackStats
|
||||||
@@ -335,7 +337,6 @@ struct ControlsOverlay: View {
|
|||||||
Image(systemName: "text.bubble")
|
Image(systemName: "text.bubble")
|
||||||
if let captions = captionsBinding.wrappedValue,
|
if let captions = captionsBinding.wrappedValue,
|
||||||
let language = LanguageCodes(rawValue: captions.code)
|
let language = LanguageCodes(rawValue: captions.code)
|
||||||
|
|
||||||
{
|
{
|
||||||
Text("\(language.description.capitalized) (\(language.rawValue))")
|
Text("\(language.description.capitalized) (\(language.rawValue))")
|
||||||
.foregroundColor(.accentColor)
|
.foregroundColor(.accentColor)
|
||||||
@@ -380,17 +381,16 @@ struct ControlsOverlay: View {
|
|||||||
.contextMenu {
|
.contextMenu {
|
||||||
Button("Disabled") { captionsBinding.wrappedValue = nil }
|
Button("Disabled") { captionsBinding.wrappedValue = nil }
|
||||||
|
|
||||||
ForEach(player.currentVideo?.captions ?? []) { caption in
|
ForEach(availableCaptions) { caption in
|
||||||
Button(caption.description) { captionsBinding.wrappedValue = caption }
|
Button(caption.description) { captionsBinding.wrappedValue = caption }
|
||||||
}
|
}
|
||||||
Button("Cancel", role: .cancel) {}
|
Button("Cancel", role: .cancel) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ViewBuilder private var captionsPicker: some View {
|
@ViewBuilder private var captionsPicker: some View {
|
||||||
let captions = player.currentVideo?.captions ?? []
|
let captions = availableCaptions
|
||||||
Picker("Captions", selection: captionsBinding) {
|
Picker("Captions", selection: captionsBinding) {
|
||||||
if captions.isEmpty {
|
if captions.isEmpty {
|
||||||
Text("Not available").tag(Captions?.none)
|
Text("Not available").tag(Captions?.none)
|
||||||
@@ -402,6 +402,31 @@ struct ControlsOverlay: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
.disabled(captions.isEmpty)
|
.disabled(captions.isEmpty)
|
||||||
|
.onAppear {
|
||||||
|
loadCaptions()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadCaptions() {
|
||||||
|
isLoadingCaptions = true
|
||||||
|
|
||||||
|
// Fetch captions asynchronously
|
||||||
|
Task {
|
||||||
|
let fetchedCaptions = await fetchCaptions()
|
||||||
|
await MainActor.run {
|
||||||
|
// Update state on the main thread
|
||||||
|
self.availableCaptions = fetchedCaptions
|
||||||
|
self.isLoadingCaptions = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func fetchCaptions() async -> [Captions] {
|
||||||
|
// Access currentVideo from the main actor context
|
||||||
|
await MainActor.run {
|
||||||
|
// Safely access the main actor-isolated currentVideo property
|
||||||
|
player.currentVideo?.captions ?? []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var captionsBinding: Binding<Captions?> {
|
private var captionsBinding: Binding<Captions?> {
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ struct PlayerBackendView: View {
|
|||||||
Color.clear
|
Color.clear
|
||||||
.onAppear { player.playerSize = proxy.size }
|
.onAppear { player.playerSize = proxy.size }
|
||||||
.onChange(of: proxy.size) { _ in player.playerSize = proxy.size }
|
.onChange(of: proxy.size) { _ in player.playerSize = proxy.size }
|
||||||
.onChange(of: player.currentItem?.id) { _ in player.playerSize = proxy.size }
|
.onChange(of: player.controls.presentingOverlays) { _ in player.playerSize = proxy.size }
|
||||||
})
|
})
|
||||||
|
|
||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ extension VideoPlayerView {
|
|||||||
player.seek.gestureStart = time
|
player.seek.gestureStart = time
|
||||||
}
|
}
|
||||||
let timeSeek = (time / player.playerSize.width) * horizontalDrag * seekGestureSpeed
|
let timeSeek = (time / player.playerSize.width) * horizontalDrag * seekGestureSpeed
|
||||||
|
|
||||||
player.seek.gestureSeek = timeSeek
|
player.seek.gestureSeek = timeSeek
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -79,54 +80,6 @@ extension VideoPlayerView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var detailsDragGesture: some Gesture {
|
|
||||||
DragGesture(minimumDistance: 30)
|
|
||||||
.onChanged { value in
|
|
||||||
handleDetailsDragChange(value)
|
|
||||||
}
|
|
||||||
.onEnded { value in
|
|
||||||
handleDetailsDragEnd(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func handleDetailsDragChange(_ value: DragGesture.Value) {
|
|
||||||
let maxOffset = -player.playerSize.height
|
|
||||||
|
|
||||||
// Continuous drag update for smooth movement of VideoDetails
|
|
||||||
if fullScreenDetails {
|
|
||||||
// Allow only downward dragging when in fullscreen
|
|
||||||
if value.translation.height > 0 {
|
|
||||||
detailViewDragOffset = min(value.translation.height, abs(maxOffset))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Allow only upward dragging when not in fullscreen
|
|
||||||
if value.translation.height < 0 {
|
|
||||||
detailViewDragOffset = max(value.translation.height, maxOffset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func handleDetailsDragEnd(_ value: DragGesture.Value) {
|
|
||||||
if value.translation.height < -50, !fullScreenDetails {
|
|
||||||
// Swipe up to enter fullscreen
|
|
||||||
withAnimation(Constants.overlayAnimation) {
|
|
||||||
fullScreenDetails = true
|
|
||||||
detailViewDragOffset = 0
|
|
||||||
}
|
|
||||||
} else if value.translation.height > 50, fullScreenDetails {
|
|
||||||
// Swipe down to exit fullscreen
|
|
||||||
withAnimation(Constants.overlayAnimation) {
|
|
||||||
fullScreenDetails = false
|
|
||||||
detailViewDragOffset = 0
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Reset offset if drag was not significant
|
|
||||||
withAnimation(Constants.overlayAnimation) {
|
|
||||||
detailViewDragOffset = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func onPlayerDragGestureEnded() {
|
func onPlayerDragGestureEnded() {
|
||||||
if horizontalPlayerGestureEnabled, isHorizontalDrag {
|
if horizontalPlayerGestureEnabled, isHorizontalDrag {
|
||||||
isHorizontalDrag = false
|
isHorizontalDrag = false
|
||||||
@@ -155,6 +108,7 @@ extension VideoPlayerView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Function to temporarily disable the toggle gesture after a fullscreen change
|
||||||
private func disableGestureTemporarily() {
|
private func disableGestureTemporarily() {
|
||||||
disableToggleGesture = true
|
disableToggleGesture = true
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ struct VideoDetails: View {
|
|||||||
.frame(maxWidth: .infinity, alignment: .leading)
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
.contentShape(Rectangle())
|
.contentShape(Rectangle())
|
||||||
.padding(.horizontal, 16)
|
.padding(.horizontal, 16)
|
||||||
|
// swiftlint:disable trailing_closure
|
||||||
// TODO: when setting tvOS minimum to 16, the platform modifier can be removed
|
// TODO: when setting tvOS minimum to 16, the platform modifier can be removed
|
||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
.simultaneousGesture( // Simultaneous gesture to prioritize button tap
|
.simultaneousGesture( // Simultaneous gesture to prioritize button tap
|
||||||
@@ -234,7 +234,7 @@ struct VideoDetails: View {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
#endif
|
#endif
|
||||||
|
// swiftlint:enable trailing_closure
|
||||||
if VideoActions().isAnyActionVisible() {
|
if VideoActions().isAnyActionVisible() {
|
||||||
VideoActions(video: player.videoForDisplay)
|
VideoActions(video: player.videoForDisplay)
|
||||||
.padding(.vertical, 5)
|
.padding(.vertical, 5)
|
||||||
|
|||||||
@@ -24,12 +24,13 @@ struct VideoPlayerView: View {
|
|||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
335
|
335
|
||||||
#else
|
#else
|
||||||
140
|
200
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@State private var playerSize: CGSize = .zero { didSet { updateSidebarQueue() } }
|
@State private var playerSize: CGSize = .zero { didSet { updateSidebarQueue() } }
|
||||||
@State private var hoveringPlayer = false
|
@State private var hoveringPlayer = false
|
||||||
|
@State private var fullScreenDetails = false
|
||||||
@State private var sidebarQueue = defaultSidebarQueueValue
|
@State private var sidebarQueue = defaultSidebarQueueValue
|
||||||
|
|
||||||
@Environment(\.colorScheme) private var colorScheme
|
@Environment(\.colorScheme) private var colorScheme
|
||||||
@@ -50,14 +51,12 @@ struct VideoPlayerView: View {
|
|||||||
@State var isHorizontalDrag = false
|
@State var isHorizontalDrag = false
|
||||||
@State var isVerticalDrag = false
|
@State var isVerticalDrag = false
|
||||||
@State var viewDragOffset = Self.hiddenOffset
|
@State var viewDragOffset = Self.hiddenOffset
|
||||||
@State var detailViewDragOffset: Double = 0
|
|
||||||
// swiftlint:enable private_swiftui_state
|
// swiftlint:enable private_swiftui_state
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// swiftlint:disable private_swiftui_state
|
// swiftlint:disable private_swiftui_state
|
||||||
@State var disableToggleGesture = false
|
@State var disableToggleGesture = false
|
||||||
@State var fullScreenDetails = false
|
|
||||||
// swiftlint:enable private_swiftui_state
|
// swiftlint:enable private_swiftui_state
|
||||||
|
|
||||||
@ObservedObject var player = PlayerModel.shared // swiftlint:disable:this swiftui_state_private
|
@ObservedObject var player = PlayerModel.shared // swiftlint:disable:this swiftui_state_private
|
||||||
@@ -308,8 +307,6 @@ struct VideoPlayerView: View {
|
|||||||
#endif
|
#endif
|
||||||
.id(player.currentVideo?.cacheKey)
|
.id(player.currentVideo?.cacheKey)
|
||||||
.transition(.opacity)
|
.transition(.opacity)
|
||||||
.offset(y: detailViewDragOffset)
|
|
||||||
.gesture(detailsDragGesture)
|
|
||||||
} else {
|
} else {
|
||||||
VStack {}
|
VStack {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user