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

73 lines
2.0 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?
let minimumHeightLeft: Double
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,
minimumHeightLeft: Double? = nil
2021-08-23 00:43:33 +05:30
) {
self.geometry = geometry
self.aspectRatio = aspectRatio ?? VideoPlayerView.defaultAspectRatio
self.minimumHeightLeft = minimumHeightLeft ?? VideoPlayerView.defaultMinimumHeightLeft
}
func body(content: Content) -> some View {
content
.frame(maxHeight: maxHeight)
.aspectRatio(usedAspectRatio, contentMode: usedAspectRatioContentMode)
.edgesIgnoringSafeArea(edgesIgnoringSafeArea)
2021-08-23 00:43:33 +05:30
}
2021-09-19 02:06:42 +05:30
var usedAspectRatio: Double {
2021-08-23 00:43:33 +05:30
guard aspectRatio != nil else {
return VideoPlayerView.defaultAspectRatio
}
let ratio = [aspectRatio!, VideoPlayerView.defaultAspectRatio].min()!
let viewRatio = geometry.size.width / geometry.size.height
#if os(iOS)
return verticalSizeClass == .regular ? ratio : viewRatio
#else
return ratio
#endif
}
var usedAspectRatioContentMode: ContentMode {
#if os(iOS)
verticalSizeClass == .regular ? .fit : .fill
#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 {
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
}
var edgesIgnoringSafeArea: Edge.Set {
let empty = Edge.Set()
#if os(iOS)
return verticalSizeClass == .compact ? .all : empty
#else
return empty
#endif
}
}