1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/Player/VideoPlayerSizeModifier.swift

65 lines
1.7 KiB
Swift
Raw Normal View History

2021-08-23 00:43:33 +05:30
import Foundation
import SwiftUI
struct VideoPlayerSizeModifier: ViewModifier {
let geometry: GeometryProxy
2021-09-19 02:06:42 +05:30
let aspectRatio: Double?
2022-02-17 01:53:11 +05:30
let fullScreen: Bool
2023-05-21 02:19:10 +05:30
var detailsHiddenInFullScreen = true
2021-08-23 00:43:33 +05:30
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
init(
geometry: GeometryProxy,
2021-09-19 02:06:42 +05:30
aspectRatio: Double? = nil,
2023-05-21 02:19:10 +05:30
fullScreen: Bool = false,
detailsHiddenInFullScreen: Bool = false
2021-08-23 00:43:33 +05:30
) {
self.geometry = geometry
self.aspectRatio = aspectRatio ?? VideoPlayerView.defaultAspectRatio
2022-02-17 01:53:11 +05:30
self.fullScreen = fullScreen
2023-05-21 02:19:10 +05:30
self.detailsHiddenInFullScreen = detailsHiddenInFullScreen
2021-08-23 00:43:33 +05:30
}
func body(content: Content) -> some View {
content
2023-06-08 01:26:56 +05:30
.frame(width: geometry.size.width)
2022-07-09 05:51:04 +05:30
.frame(maxHeight: maxHeight)
2023-05-21 02:19:10 +05:30
.aspectRatio(ratio, contentMode: usedAspectRatioContentMode)
2021-08-23 00:43:33 +05:30
}
2023-06-17 17:39:51 +05:30
var ratio: CGFloat? { // swiftlint:disable:this no_cgfloat
2023-05-21 02:19:10 +05:30
fullScreen ? detailsHiddenInFullScreen ? nil : usedAspectRatio : usedAspectRatio
}
2021-09-19 02:06:42 +05:30
var usedAspectRatio: Double {
2023-05-21 02:19:10 +05:30
guard let aspectRatio, aspectRatio > 0 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 {
2023-11-21 22:28:44 +05:30
#if os(tvOS)
.fit
2021-08-23 00:43:33 +05:30
#else
2023-11-21 22:28:44 +05:30
fullScreen ? .fill : .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 {
2023-05-21 02:19:10 +05:30
if detailsHiddenInFullScreen {
return geometry.size.height
}
2023-06-17 17:39:51 +05:30
return geometry.size.width / usedAspectRatio
2022-07-09 05:51:04 +05:30
}
2023-05-21 02:19:10 +05:30
return max(geometry.size.height - VideoPlayerView.defaultMinimumHeightLeft, 0)
2021-08-23 00:43:33 +05:30
}
}