yattee/Shared/Player/VideoPlayerSizeModifier.swift
Toni Förster 8900f96ce7
honour the aspect ratio when resizing
The `defaultMinimumHeightLeft? has been adjusted to make the radio of the video view 16:9 in its initial state. Also on macOS when resizing the window, the aspect ratio of the view now correlates with the video.
2023-11-21 13:47:51 +01:00

65 lines
1.7 KiB
Swift

import Foundation
import SwiftUI
struct VideoPlayerSizeModifier: ViewModifier {
let geometry: GeometryProxy
let aspectRatio: Double?
let fullScreen: Bool
var detailsHiddenInFullScreen = true
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
init(
geometry: GeometryProxy,
aspectRatio: Double? = nil,
fullScreen: Bool = false,
detailsHiddenInFullScreen: Bool = false
) {
self.geometry = geometry
self.aspectRatio = aspectRatio ?? VideoPlayerView.defaultAspectRatio
self.fullScreen = fullScreen
self.detailsHiddenInFullScreen = detailsHiddenInFullScreen
}
func body(content: Content) -> some View {
content
.frame(width: geometry.size.width)
.frame(maxHeight: maxHeight)
.aspectRatio(ratio, contentMode: usedAspectRatioContentMode)
}
var ratio: CGFloat? { // swiftlint:disable:this no_cgfloat
fullScreen ? detailsHiddenInFullScreen ? nil : usedAspectRatio : usedAspectRatio
}
var usedAspectRatio: Double {
guard let aspectRatio, aspectRatio > 0 else {
return VideoPlayerView.defaultAspectRatio
}
return aspectRatio
}
var usedAspectRatioContentMode: ContentMode {
#if os(iOS)
fullScreen ? .fill : .fit
#else
.fit
#endif
}
var maxHeight: Double {
guard !fullScreen else {
if detailsHiddenInFullScreen {
return geometry.size.height
}
return geometry.size.width / usedAspectRatio
}
return max(geometry.size.height - VideoPlayerView.defaultMinimumHeightLeft, 0)
}
}