Compare commits

..

1 Commits

Author SHA1 Message Date
Toni Förster
7e02b08933 update SwiftUI-Introspect 2024-09-11 20:55:00 +02:00
7 changed files with 57 additions and 94 deletions

View File

@@ -23,14 +23,15 @@ final class MPVBackend: PlayerBackend {
var stream: Stream?
var video: Video?
var captions: Captions? {
didSet {
Task {
await handleCaptionsChange()
var captions: Captions? { didSet {
guard let captions else {
if client?.areSubtitlesAdded == true {
client?.removeSubs()
}
return
}
}
addSubTrack(captions.url)
}}
var currentTime: CMTime?
var loadedVideo = false
@@ -616,14 +617,10 @@ final class MPVBackend: PlayerBackend {
}
func addSubTrack(_ url: URL) {
Task {
if let areSubtitlesAdded = client?.areSubtitlesAdded {
if await areSubtitlesAdded() {
await client?.removeSubs()
}
}
await client?.addSubTrack(url)
if client?.areSubtitlesAdded == true {
client?.removeSubs()
}
client?.addSubTrack(url)
}
func setVideoToAuto() {
@@ -687,17 +684,6 @@ 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) {
switch name {
case "pause":

View File

@@ -349,15 +349,21 @@ final class MPVClient: ObservableObject {
return Int(fps.rounded())
}
func areSubtitlesAdded() async -> Bool {
var areSubtitlesAdded: Bool {
guard !mpv.isNil else { return false }
let trackCount = await Task(operation: { getInt("track-list/count") }).value
// Retrieve the number of tracks
let trackCount = getInt("track-list/count")
guard trackCount > 0 else { return false }
for index in 0 ..< trackCount {
if let trackType = await Task(operation: { getString("track-list/\(index)/type") }).value, trackType == "sub" {
return true
// Get the type of each track
if let trackType = getString("track-list/\(index)/type"), trackType == "sub" {
// Check if the subtitle track is currently selected
let selected = getInt("track-list/\(index)/selected")
if selected == 1 {
return true
}
}
}
return false
@@ -533,16 +539,12 @@ final class MPVClient: ObservableObject {
command("video-add", args: [url.absoluteString])
}
func addSubTrack(_ url: URL) async {
await Task {
command("sub-add", args: [url.absoluteString])
}.value
func addSubTrack(_ url: URL) {
command("sub-add", args: [url.absoluteString])
}
func removeSubs() async {
await Task {
command("sub-remove")
}.value
func removeSubs() {
command("sub-remove")
}
func setVideoToAuto() {

View File

@@ -15,7 +15,7 @@ struct AppSidebarNavigation: View {
var body: some View {
#if os(iOS)
content.introspect(.viewController, on: .iOS(.v15, .v16, .v17)) { viewController in
content.introspect(.viewController, on: .iOS(.v15, .v16, .v17, .v18)) { viewController in
// workaround for an empty supplementary view on launch
// the supplementary view is determined by the default selection inside the
// primary view, but the primary view is not loaded so its selection is not read

View File

@@ -5,8 +5,6 @@ struct ControlsOverlay: View {
@ObservedObject private var player = PlayerModel.shared
private var model = PlayerControlsModel.shared
@State private var availableCaptions: [Captions] = []
@State private var isLoadingCaptions = true
@State private var contentSize: CGSize = .zero
@Default(.showMPVPlaybackStats) private var showMPVPlaybackStats
@@ -337,6 +335,7 @@ struct ControlsOverlay: View {
Image(systemName: "text.bubble")
if let captions = captionsBinding.wrappedValue,
let language = LanguageCodes(rawValue: captions.code)
{
Text("\(language.description.capitalized) (\(language.rawValue))")
.foregroundColor(.accentColor)
@@ -381,16 +380,17 @@ struct ControlsOverlay: View {
.contextMenu {
Button("Disabled") { captionsBinding.wrappedValue = nil }
ForEach(availableCaptions) { caption in
ForEach(player.currentVideo?.captions ?? []) { caption in
Button(caption.description) { captionsBinding.wrappedValue = caption }
}
Button("Cancel", role: .cancel) {}
}
#endif
}
@ViewBuilder private var captionsPicker: some View {
let captions = availableCaptions
let captions = player.currentVideo?.captions ?? []
Picker("Captions", selection: captionsBinding) {
if captions.isEmpty {
Text("Not available").tag(Captions?.none)
@@ -402,31 +402,6 @@ struct ControlsOverlay: View {
}
}
.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?> {

View File

@@ -9,7 +9,7 @@ struct FocusableSearchTextField: View {
var body: some View {
SearchTextField()
#if os(macOS)
.introspect(.textField, on: .macOS(.v12, .v13, .v14)) { textField in
.introspect(.textField, on: .macOS(.v12, .v13, .v14, .v15)) { textField in
state.textField = textField
}
.onAppear {
@@ -18,7 +18,7 @@ struct FocusableSearchTextField: View {
}
}
#elseif os(iOS)
.introspect(.textField, on: .iOS(.v15, .v16, .v17)) { textField in
.introspect(.textField, on: .iOS(.v15, .v16, .v17, .v18)) { textField in
state.textField = textField
}
.onChange(of: state.focused) { newValue in

View File

@@ -4913,7 +4913,7 @@
repositoryURL = "https://github.com/sindresorhus/Defaults";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 7.0.0;
minimumVersion = 7.3.1;
};
};
372AA40E286D067B0000B1DC /* XCRemoteSwiftPackageReference "Repeat" */ = {
@@ -4928,8 +4928,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/hyperoslo/Cache.git";
requirement = {
branch = master;
kind = branch;
kind = upToNextMajorVersion;
minimumVersion = 7.4.0;
};
};
375B8AAF28B57F4200397B31 /* XCRemoteSwiftPackageReference "KeychainAccess" */ = {
@@ -4944,16 +4944,16 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/pinterest/PINCache";
requirement = {
branch = master;
kind = branch;
kind = upToNextMajorVersion;
minimumVersion = 3.0.4;
};
};
379325D329A265A300181CF1 /* XCRemoteSwiftPackageReference "swift-log" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/yattee/swift-log.git";
repositoryURL = "https://github.com/apple/swift-log.git";
requirement = {
branch = main;
kind = branch;
kind = upToNextMajorVersion;
minimumVersion = 1.6.1;
};
};
3797104728D3D10600D5F53C /* XCRemoteSwiftPackageReference "SDWebImageSwiftUI" */ = {
@@ -4961,7 +4961,7 @@
repositoryURL = "https://github.com/SDWebImage/SDWebImageSwiftUI.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 2.1.0;
minimumVersion = 2.2.7;
};
};
3797757B268922D100DD52A8 /* XCRemoteSwiftPackageReference "siesta" */ = {
@@ -4969,7 +4969,7 @@
repositoryURL = "https://github.com/bustoutsolutions/siesta";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.5.0;
minimumVersion = 1.5.2;
};
};
3799AC0728B03CEC001376F9 /* XCRemoteSwiftPackageReference "ActiveLabel.swift" */ = {
@@ -4985,7 +4985,7 @@
repositoryURL = "https://github.com/Alamofire/Alamofire.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 5.0.0;
minimumVersion = 5.9.1;
};
};
37BD07C52698B27B003EBB87 /* XCRemoteSwiftPackageReference "SwiftUI-Introspect" */ = {
@@ -4993,7 +4993,7 @@
repositoryURL = "https://github.com/siteline/SwiftUI-Introspect.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.1.3;
minimumVersion = 1.3.0;
};
};
37CF8B8228535E4F00B71E37 /* XCRemoteSwiftPackageReference "SDWebImage" */ = {
@@ -5001,7 +5001,7 @@
repositoryURL = "https://github.com/SDWebImage/SDWebImage";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 5.19.1;
minimumVersion = 5.19.7;
};
};
37D4B19B2671817900C925CA /* XCRemoteSwiftPackageReference "SwiftyJSON" */ = {
@@ -5009,7 +5009,7 @@
repositoryURL = "https://github.com/SwiftyJSON/SwiftyJSON.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 5.0.0;
minimumVersion = 5.0.2;
};
};
37EE6DC328A305AD00BFD632 /* XCRemoteSwiftPackageReference "Reachability" */ = {
@@ -5017,7 +5017,7 @@
repositoryURL = "https://github.com/ashleymills/Reachability.swift";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 5.1.0;
minimumVersion = 5.2.3;
};
};
37FB2847272207F000A57617 /* XCRemoteSwiftPackageReference "SDWebImageWebPCoder" */ = {
@@ -5025,7 +5025,7 @@
repositoryURL = "https://github.com/SDWebImage/SDWebImageWebPCoder.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 0.8.4;
minimumVersion = 0.14.6;
};
};
37FB285227220D8400A57617 /* XCRemoteSwiftPackageReference "SDWebImagePINPlugin" */ = {

View File

@@ -1,5 +1,5 @@
{
"originHash" : "515d8e68c4a31658288fb3f94789ee539399b042082c08c39f4c03c27fd8860c",
"originHash" : "173de1b718eb898698eaba0221b46be9781899a652725709c8400d3ddfb01980",
"pins" : [
{
"identity" : "activelabel.swift",
@@ -24,8 +24,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/hyperoslo/Cache.git",
"state" : {
"branch" : "master",
"revision" : "81a0277cbc6b63f4e0cd6f42c4abefa1011bbfa9"
"revision" : "24e47109e31b2031cb26e25cc1b81b607496066c",
"version" : "7.4.0"
}
},
{
@@ -69,8 +69,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pinterest/PINCache",
"state" : {
"branch" : "master",
"revision" : "2fb85948463292c2e824148cf17dc62a4c217a94"
"revision" : "2fb85948463292c2e824148cf17dc62a4c217a94",
"version" : "3.0.4"
}
},
{
@@ -148,10 +148,10 @@
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/yattee/swift-log.git",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"branch" : "main",
"revision" : "3f3dc1390a2f116894887c352792dc8d5fa9e875"
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
}
},
{
@@ -168,8 +168,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/siteline/SwiftUI-Introspect.git",
"state" : {
"revision" : "121c146fe591b1320238d054ae35c81ffa45f45a",
"version" : "0.12.0"
"revision" : "807f73ce09a9b9723f12385e592b4e0aaebd3336",
"version" : "1.3.0"
}
},
{