yattee/Shared/Player/PlayerLayerView.swift

52 lines
1.2 KiB
Swift
Raw Permalink Normal View History

2022-05-20 21:23:14 +00:00
import AVFoundation
import Foundation
2022-08-18 22:40:46 +00:00
#if os(macOS)
import AppKit
#else
import UIKit
#endif
2022-05-20 21:23:14 +00:00
2022-08-18 22:40:46 +00:00
#if os(macOS)
final class PlayerLayerView: NSView {
var player = PlayerModel.shared { didSet {
2022-08-18 22:40:46 +00:00
wantsLayer = true
}}
2022-05-20 21:23:14 +00:00
2022-08-25 17:09:55 +00:00
override init(frame frameRect: CGRect) {
2022-08-18 22:40:46 +00:00
super.init(frame: frameRect)
}
2022-05-20 21:23:14 +00:00
2022-08-18 22:40:46 +00:00
override func makeBackingLayer() -> CALayer {
player.avPlayerBackend.playerLayer
}
2022-05-20 21:23:14 +00:00
2022-08-18 22:40:46 +00:00
required init?(coder: NSCoder) {
super.init(coder: coder)
}
2022-05-20 21:23:14 +00:00
}
2022-08-18 22:40:46 +00:00
#else
final class PlayerLayerView: UIView {
var player: PlayerModel { .shared }
2022-08-18 22:40:46 +00:00
override init(frame: CGRect) {
super.init(frame: frame)
}
private var layerAdded = false
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
2022-05-20 21:23:14 +00:00
2022-08-18 22:40:46 +00:00
override func layoutSubviews() {
if !layerAdded {
layerAdded = true
layer.addSublayer(player.avPlayerBackend.playerLayer)
}
player.avPlayerBackend.playerLayer.frame = bounds
super.layoutSubviews()
}
2022-05-20 21:23:14 +00:00
}
2022-08-18 22:40:46 +00:00
#endif