yattee/Shared/Player/VideoPlayerSizeModifier.swift

65 lines
1.8 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?
let minimumHeightLeft: Double
2022-02-16 20:23:11 +00:00
let fullScreen: Bool
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,
2022-02-16 20:23:11 +00:00
minimumHeightLeft: Double? = nil,
fullScreen: Bool = false
2021-08-22 19:13:33 +00:00
) {
self.geometry = geometry
self.aspectRatio = aspectRatio ?? VideoPlayerView.defaultAspectRatio
self.minimumHeightLeft = minimumHeightLeft ?? VideoPlayerView.defaultMinimumHeightLeft
2022-02-16 20:23:11 +00:00
self.fullScreen = fullScreen
2021-08-22 19:13:33 +00:00
}
func body(content: Content) -> some View {
content
2022-07-09 00:21:04 +00:00
.frame(width: geometry.size.width)
.frame(maxHeight: maxHeight)
#if !os(macOS)
.aspectRatio(fullScreen ? nil : usedAspectRatio, contentMode: usedAspectRatioContentMode)
#endif
2021-08-22 19:13:33 +00:00
}
2021-09-18 20:36:42 +00:00
var usedAspectRatio: Double {
2022-08-14 17:03:26 +00:00
guard let aspectRatio = aspectRatio, aspectRatio > 0, aspectRatio < VideoPlayerView.defaultAspectRatio 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
2021-11-08 16:29:35 +00:00
.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 {
return .infinity
}
2021-08-22 19:13:33 +00:00
#if os(iOS)
2021-10-16 22:48:58 +00:00
let height = verticalSizeClass == .regular ? geometry.size.height - minimumHeightLeft : .infinity
2021-08-22 19:13:33 +00:00
#else
2021-10-16 22:48:58 +00:00
let height = geometry.size.height - minimumHeightLeft
2021-08-22 19:13:33 +00:00
#endif
2021-10-16 22:48:58 +00:00
return [height, 0].max()!
2021-08-22 19:13:33 +00:00
}
}