yattee/Shared/Player/VideoPlayerSizeModifier.swift

65 lines
1.7 KiB
Swift
Raw Normal View History

2021-08-22 19:13:33 +00:00
import Foundation
import SwiftUI
struct VideoPlayerSizeModifier: ViewModifier {
let geometry: GeometryProxy
2021-09-18 20:36:42 +00:00
let aspectRatio: Double?
2022-02-16 20:23:11 +00:00
let fullScreen: Bool
2023-05-20 20:49:10 +00:00
var detailsHiddenInFullScreen = true
2021-08-22 19:13:33 +00:00
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
init(
geometry: GeometryProxy,
2021-09-18 20:36:42 +00:00
aspectRatio: Double? = nil,
2023-05-20 20:49:10 +00:00
fullScreen: Bool = false,
detailsHiddenInFullScreen: Bool = false
2021-08-22 19:13:33 +00:00
) {
self.geometry = geometry
self.aspectRatio = aspectRatio ?? VideoPlayerView.defaultAspectRatio
2022-02-16 20:23:11 +00:00
self.fullScreen = fullScreen
2023-05-20 20:49:10 +00:00
self.detailsHiddenInFullScreen = detailsHiddenInFullScreen
2021-08-22 19:13:33 +00:00
}
func body(content: Content) -> some View {
content
2023-06-07 19:56:56 +00:00
.frame(width: geometry.size.width)
2022-07-09 00:21:04 +00:00
.frame(maxHeight: maxHeight)
2023-05-20 20:49:10 +00:00
.aspectRatio(ratio, contentMode: usedAspectRatioContentMode)
2021-08-22 19:13:33 +00:00
}
2023-06-17 12:09:51 +00:00
var ratio: CGFloat? { // swiftlint:disable:this no_cgfloat
2023-05-20 20:49:10 +00:00
fullScreen ? detailsHiddenInFullScreen ? nil : usedAspectRatio : usedAspectRatio
}
2021-09-18 20:36:42 +00:00
var usedAspectRatio: Double {
2023-05-20 20:49:10 +00:00
guard let aspectRatio, aspectRatio > 0 else {
2021-08-22 19:13:33 +00:00
return VideoPlayerView.defaultAspectRatio
}
2022-08-06 13:27:34 +00:00
return aspectRatio
2021-08-22 19:13:33 +00:00
}
var usedAspectRatioContentMode: ContentMode {
#if os(iOS)
2022-08-06 13:27:34 +00:00
fullScreen ? .fill : .fit
2021-08-22 19:13:33 +00:00
#else
.fit
2021-08-22 19:13:33 +00:00
#endif
}
2021-09-18 20:36:42 +00:00
var maxHeight: Double {
2022-07-09 00:21:04 +00:00
guard !fullScreen else {
2023-05-20 20:49:10 +00:00
if detailsHiddenInFullScreen {
return geometry.size.height
}
2023-06-17 12:09:51 +00:00
return geometry.size.width / usedAspectRatio
2022-07-09 00:21:04 +00:00
}
2023-05-20 20:49:10 +00:00
return max(geometry.size.height - VideoPlayerView.defaultMinimumHeightLeft, 0)
2021-08-22 19:13:33 +00:00
}
}