2021-08-23 00:43:33 +05:30
|
|
|
import Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct VideoPlayerSizeModifier: ViewModifier {
|
2021-11-07 23:23:00 +05:30
|
|
|
let geometry: GeometryProxy
|
2021-09-19 02:06:42 +05:30
|
|
|
let aspectRatio: Double?
|
|
|
|
let minimumHeightLeft: Double
|
2022-02-17 01:53:11 +05:30
|
|
|
let fullScreen: Bool
|
2021-08-23 00:43:33 +05:30
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.verticalSizeClass) private var verticalSizeClass
|
|
|
|
#endif
|
|
|
|
|
|
|
|
init(
|
2021-11-07 23:23:00 +05:30
|
|
|
geometry: GeometryProxy,
|
2021-09-19 02:06:42 +05:30
|
|
|
aspectRatio: Double? = nil,
|
2022-02-17 01:53:11 +05:30
|
|
|
minimumHeightLeft: Double? = nil,
|
|
|
|
fullScreen: Bool = false
|
2021-08-23 00:43:33 +05:30
|
|
|
) {
|
|
|
|
self.geometry = geometry
|
|
|
|
self.aspectRatio = aspectRatio ?? VideoPlayerView.defaultAspectRatio
|
|
|
|
self.minimumHeightLeft = minimumHeightLeft ?? VideoPlayerView.defaultMinimumHeightLeft
|
2022-02-17 01:53:11 +05:30
|
|
|
self.fullScreen = fullScreen
|
2021-08-23 00:43:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func body(content: Content) -> some View {
|
2021-11-07 23:23:00 +05:30
|
|
|
content
|
2022-07-09 05:51:04 +05:30
|
|
|
.frame(width: geometry.size.width)
|
|
|
|
.frame(maxHeight: maxHeight)
|
|
|
|
#if !os(macOS)
|
|
|
|
.aspectRatio(fullScreen ? nil : usedAspectRatio, contentMode: usedAspectRatioContentMode)
|
|
|
|
#endif
|
2021-08-23 00:43:33 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 02:06:42 +05:30
|
|
|
var usedAspectRatio: Double {
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let aspectRatio, aspectRatio > 0, aspectRatio < VideoPlayerView.defaultAspectRatio else {
|
2021-08-23 00:43:33 +05:30
|
|
|
return VideoPlayerView.defaultAspectRatio
|
|
|
|
}
|
|
|
|
|
2022-08-06 18:57:34 +05:30
|
|
|
return aspectRatio
|
2021-08-23 00:43:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var usedAspectRatioContentMode: ContentMode {
|
|
|
|
#if os(iOS)
|
2022-08-06 18:57:34 +05:30
|
|
|
fullScreen ? .fill : .fit
|
2021-08-23 00:43:33 +05:30
|
|
|
#else
|
2021-11-08 21:59:35 +05:30
|
|
|
.fit
|
2021-08-23 00:43:33 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-19 02:06:42 +05:30
|
|
|
var maxHeight: Double {
|
2022-07-09 05:51:04 +05:30
|
|
|
guard !fullScreen else {
|
|
|
|
return .infinity
|
|
|
|
}
|
|
|
|
|
2021-08-23 00:43:33 +05:30
|
|
|
#if os(iOS)
|
2021-10-17 04:18:58 +05:30
|
|
|
let height = verticalSizeClass == .regular ? geometry.size.height - minimumHeightLeft : .infinity
|
2021-08-23 00:43:33 +05:30
|
|
|
#else
|
2021-10-17 04:18:58 +05:30
|
|
|
let height = geometry.size.height - minimumHeightLeft
|
2021-08-23 00:43:33 +05:30
|
|
|
#endif
|
2021-10-17 04:18:58 +05:30
|
|
|
|
|
|
|
return [height, 0].max()!
|
2021-08-23 00:43:33 +05:30
|
|
|
}
|
|
|
|
}
|