mirror of
https://github.com/insidegui/WWDC.git
synced 2026-04-07 19:17:41 +00:00
38 lines
1.3 KiB
Swift
38 lines
1.3 KiB
Swift
import Cocoa
|
|
import AVFoundation
|
|
import ConfUIFoundation
|
|
|
|
@MainActor
|
|
public extension AVPlayer {
|
|
static let fallbackNaturalSize = CGSize(width: 1920, height: 1080)
|
|
|
|
func fittingRect(with bounds: CGRect) -> CGRect {
|
|
let videoSize = currentItem?.tracks.first(where: { $0.assetTrack?.mediaType == .video })?.assetTrack?.naturalSize ?? Self.fallbackNaturalSize
|
|
|
|
let fittingRect = AVMakeRect(aspectRatio: videoSize, insideRect: bounds)
|
|
|
|
UILog("📐 Video size: \(videoSize), fitting size: \(fittingRect.size)")
|
|
|
|
return fittingRect
|
|
}
|
|
|
|
func updateLayout(guide: NSLayoutGuide, container: NSView, constraints: inout [NSLayoutConstraint]) {
|
|
let videoRect = fittingRect(with: container.bounds)
|
|
|
|
if guide.owningView == nil {
|
|
container.addLayoutGuide(guide)
|
|
}
|
|
|
|
NSLayoutConstraint.deactivate(constraints)
|
|
|
|
constraints = [
|
|
guide.widthAnchor.constraint(equalToConstant: videoRect.width),
|
|
guide.heightAnchor.constraint(equalToConstant: videoRect.height),
|
|
guide.centerYAnchor.constraint(equalTo: container.centerYAnchor),
|
|
guide.centerXAnchor.constraint(equalTo: container.centerXAnchor)
|
|
]
|
|
|
|
NSLayoutConstraint.activate(constraints)
|
|
}
|
|
}
|