From f3637e242649811b420d63094a95e7bbc8bec6b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Sun, 21 Apr 2024 01:01:55 +0200 Subject: [PATCH 1/2] fix handling and displaying captions fixes #490 It also fixes the caption picker being empty when a caption was selected in the previous watched video. --- Model/Player/Backends/MPVBackend.swift | 4 ++++ Model/Player/PlayerModel.swift | 12 +++++++----- Shared/Player/Controls/ControlsOverlay.swift | 9 ++++----- Shared/Player/PlaybackSettings.swift | 8 ++++---- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index c8457946..8d265ecc 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -264,6 +264,10 @@ final class MPVBackend: PlayerBackend { self.startClientUpdates() + // Captions should only be displayed when selected by the user, + // not when the video starts. So, we remove them. + self.client?.removeSubs() + if !preservingTime, !upgrading, let segment = self.model.sponsorBlock.segments.first, diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 6f3aff7a..28913757 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -176,7 +176,7 @@ final class PlayerModel: ObservableObject { @Default(.resetWatchedStatusOnPlaying) var resetWatchedStatusOnPlaying @Default(.playerRate) var playerRate @Default(.systemControlsSeekDuration) var systemControlsSeekDuration - + #if os(macOS) @Default(.buttonBackwardSeekDuration) private var buttonBackwardSeekDuration @Default(.buttonForwardSeekDuration) private var buttonForwardSeekDuration @@ -192,7 +192,7 @@ final class PlayerModel: ObservableObject { var onPlayStream = [(Stream) -> Void]() var rateToRestore: Float? private var remoteCommandCenterConfigured = false - + #if os(macOS) var keyPressMonitor: Any? #endif @@ -771,10 +771,12 @@ final class PlayerModel: ObservableObject { func handleCurrentItemChange() { if currentItem == nil { - captions = nil FeedModel.shared.calculateUnwatchedFeed() } + // Captions need to be set to nil on item change, to clear the previus values. + captions = nil + #if os(macOS) Windows.player.window?.title = windowTitle #endif @@ -1158,7 +1160,7 @@ final class PlayerModel: ObservableObject { return nil } - + #if os(macOS) private func assignKeyPressMonitor() { keyPressMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { keyEvent -> NSEvent? in @@ -1193,7 +1195,7 @@ final class PlayerModel: ObservableObject { return nil } } - + private func destroyKeyPressMonitor() { if let keyPressMonitor = keyPressMonitor { NSEvent.removeMonitor(keyPressMonitor) diff --git a/Shared/Player/Controls/ControlsOverlay.swift b/Shared/Player/Controls/ControlsOverlay.swift index d2b6bafb..6de44e8a 100644 --- a/Shared/Player/Controls/ControlsOverlay.swift +++ b/Shared/Player/Controls/ControlsOverlay.swift @@ -312,7 +312,6 @@ struct ControlsOverlay: View { .foregroundColor(.primary) } .transaction { t in t.animation = .none } - .buttonStyle(.plain) .foregroundColor(.primary) .frame(width: 240, height: 40) @@ -374,12 +373,12 @@ struct ControlsOverlay: View { let captions = player.currentVideo?.captions ?? [] Picker("Captions", selection: captionsBinding) { if captions.isEmpty { - Text("Not available") + Text("Not available").tag(Captions?.none) } else { Text("Disabled").tag(Captions?.none) - } - ForEach(captions) { caption in - Text(caption.description).tag(Optional(caption)) + ForEach(captions) { caption in + Text(caption.description).tag(Optional(caption)) + } } } .disabled(captions.isEmpty) diff --git a/Shared/Player/PlaybackSettings.swift b/Shared/Player/PlaybackSettings.swift index d8ad9546..5ae0da17 100644 --- a/Shared/Player/PlaybackSettings.swift +++ b/Shared/Player/PlaybackSettings.swift @@ -433,12 +433,12 @@ struct PlaybackSettings: View { let captions = player.currentVideo?.captions ?? [] Picker("Captions".localized(), selection: $player.captions) { if captions.isEmpty { - Text("Not available") + Text("Not available").tag(Captions?.none) } else { Text("Disabled").tag(Captions?.none) - } - ForEach(captions) { caption in - Text(caption.description).tag(Optional(caption)) + ForEach(captions) { caption in + Text(caption.description).tag(Optional(caption)) + } } } .disabled(captions.isEmpty) From 0e7d66849d32a9a5c206b9e5aba7cb06e43f0882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Tue, 23 Apr 2024 11:39:32 +0200 Subject: [PATCH 2/2] whitespace fixes --- Model/Player/PlayerModel.swift | 3 ++- Shared/Settings/Import/ImportSettings.swift | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 28913757..17d28646 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -1190,7 +1190,8 @@ final class PlayerModel: ObservableObject { if !self.controls.isLoadingVideo { self.backend.togglePlay() } - default: return keyEvent + default: + return keyEvent } return nil } diff --git a/Shared/Settings/Import/ImportSettings.swift b/Shared/Settings/Import/ImportSettings.swift index c97879fc..c2dd32ad 100644 --- a/Shared/Settings/Import/ImportSettings.swift +++ b/Shared/Settings/Import/ImportSettings.swift @@ -1,4 +1,3 @@ - import SwiftUI struct ImportSettings: View {