Skip to content

Commit 59a0085

Browse files
Additional refinement
1 parent 324f3a3 commit 59a0085

File tree

4 files changed

+53
-52
lines changed

4 files changed

+53
-52
lines changed

PlayerUI/Protocols/PUIPlayerViewDelegates.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@
88

99
import Cocoa
1010

11-
public enum PUIPiPExitReason {
12-
case returnButton, exitButton
13-
}
14-
1511
public protocol PUIPlayerViewDelegate: AnyObject {
1612

1713
func playerViewWillEnterPictureInPictureMode(_ playerView: PUIPlayerView)
18-
func playerViewWillExitPictureInPictureMode(_ playerView: PUIPlayerView, reason: PUIPiPExitReason)
14+
func playerWillRestoreUserInterfaceForPictureInPictureStop(_ playerView: PUIPlayerView)
1915
func playerViewDidSelectAddAnnotation(_ playerView: PUIPlayerView, at timestamp: Double)
2016
func playerViewDidSelectToggleFullScreen(_ playerView: PUIPlayerView)
2117
func playerViewDidSelectLike(_ playerView: PUIPlayerView)

PlayerUI/Views/PUIPlayerView.swift

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,7 @@ public final class PUIPlayerView: NSView {
1919

2020
public weak var delegate: PUIPlayerViewDelegate?
2121

22-
public internal(set) var isInPictureInPictureMode: Bool = false {
23-
didSet {
24-
guard isInPictureInPictureMode != oldValue else { return }
25-
26-
pipButton.state = isInPictureInPictureMode ? .on : .off
27-
28-
if isInPictureInPictureMode {
29-
externalStatusController.providerIcon = .PUIPictureInPictureLarge
30-
externalStatusController.providerName = "Picture in Picture"
31-
externalStatusController.providerDescription = "Playing in Picture in Picture"
32-
externalStatusController.view.isHidden = false
33-
} else {
34-
externalStatusController.view.isHidden = true
35-
}
36-
37-
invalidateTouchBar()
38-
}
39-
}
22+
public var isInPictureInPictureMode: Bool { pipController.isPictureInPictureActive }
4023

4124
public weak var appearanceDelegate: PUIPlayerViewAppearanceDelegate? {
4225
didSet {
@@ -96,7 +79,6 @@ public final class PUIPlayerView: NSView {
9679
pipPossibleObservation = pipController.observe(
9780
\AVPictureInPictureController.isPictureInPicturePossible, options: [.initial, .new]
9881
) { [weak self] _, change in
99-
// Update the PiP button's enabled state.
10082
self?.pipButton.isEnabled = change.newValue ?? false
10183
}
10284

@@ -1630,9 +1612,49 @@ extension PUIPlayerView: PUIExternalPlaybackConsumer {
16301612

16311613
// MARK: - PiP delegate
16321614

1633-
extension PUIPlayerView: /*PUIPictureContainerViewControllerDelegate,*/ AVPictureInPictureControllerDelegate {
1615+
extension PUIPlayerView: AVPictureInPictureControllerDelegate {
1616+
1617+
// Start
1618+
1619+
public func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
1620+
delegate?.playerViewWillEnterPictureInPictureMode(self)
1621+
1622+
snapshotPlayer { [weak self] image in
1623+
self?.externalStatusController.snapshot = image
1624+
}
1625+
}
1626+
1627+
public func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
1628+
pipButton.state = .on
1629+
externalStatusController.providerIcon = .PUIPictureInPictureLarge
1630+
externalStatusController.providerName = "Picture in Picture"
1631+
externalStatusController.providerDescription = "Playing in Picture in Picture"
1632+
externalStatusController.view.isHidden = false
1633+
1634+
invalidateTouchBar()
1635+
}
1636+
1637+
public func pictureInPictureController(
1638+
_ pictureInPictureController: AVPictureInPictureController,
1639+
failedToStartPictureInPictureWithError error: Error
1640+
) {
1641+
os_log(.error, log: log, "Failed to start PiP \(error, privacy: .public)")
1642+
}
1643+
1644+
// Stop
1645+
1646+
// Called 1st
1647+
public func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
1648+
1649+
}
1650+
1651+
// Called 2nd, not called when the exit button is pressed
1652+
public func pictureInPictureController(
1653+
_ pictureInPictureController: AVPictureInPictureController,
1654+
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
1655+
) {
1656+
delegate?.playerWillRestoreUserInterfaceForPictureInPictureStop(self)
16341657

1635-
public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void) {
16361658
if !NSApp.isActive {
16371659
NSApp.activate(ignoringOtherApps: true)
16381660
}
@@ -1644,30 +1666,14 @@ extension PUIPlayerView: /*PUIPictureContainerViewControllerDelegate,*/ AVPictur
16441666
window.deminiaturize(nil)
16451667
}
16461668
}
1647-
completionHandler(true)
1648-
}
16491669

1650-
public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) {
1651-
isInPictureInPictureMode = false
1670+
completionHandler(true)
16521671
}
16531672

1673+
// Called Last
16541674
public func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
1655-
isInPictureInPictureMode = false
1656-
}
1657-
1658-
public func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
1659-
delegate?.playerViewWillEnterPictureInPictureMode(self)
1660-
1661-
snapshotPlayer { [weak self] image in
1662-
self?.externalStatusController.snapshot = image
1663-
}
1664-
}
1665-
1666-
public func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
1667-
isInPictureInPictureMode = true
1668-
}
1669-
1670-
public func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
1671-
delegate?.playerViewWillExitPictureInPictureMode(self, reason: .returnButton)
1675+
pipButton.state = .off
1676+
externalStatusController.view.isHidden = true
1677+
invalidateTouchBar()
16721678
}
16731679
}

WWDC/AppCoordinator+Shelf.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ extension AppCoordinator: ShelfViewControllerDelegate {
9292

9393
if currentPlayerController == nil {
9494
currentPlayerController = VideoPlayerViewController(player: playbackViewModel.player, session: viewModel)
95-
currentPlayerController?.playerWillExitPictureInPicture = { [weak self] reason in
96-
guard reason == .returnButton else { return }
95+
currentPlayerController?.playerWillRestoreUserInterfaceForPictureInPictureStop = { [weak self] in
9796
self?.returnToPlayingSessionContext()
9897
}
9998

WWDC/VideoPlayerViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class VideoPlayerViewController: NSViewController {
5050
}
5151
}
5252

53-
var playerWillExitPictureInPicture: ((PUIPiPExitReason) -> Void)?
53+
var playerWillRestoreUserInterfaceForPictureInPictureStop: (() -> Void)?
5454
var playerWillExitFullScreen: (() -> Void)?
5555

5656
init(player: AVPlayer, session: SessionViewModel) {
@@ -336,8 +336,8 @@ extension VideoPlayerViewController: PUIPlayerViewDelegate {
336336
playerView.snapshotPlayer(completion: completion)
337337
}
338338

339-
func playerViewWillExitPictureInPictureMode(_ playerView: PUIPlayerView, reason: PUIPiPExitReason) {
340-
playerWillExitPictureInPicture?(reason)
339+
func playerWillRestoreUserInterfaceForPictureInPictureStop(_ playerView: PUIPlayerView) {
340+
playerWillRestoreUserInterfaceForPictureInPictureStop?()
341341
}
342342

343343
func playerViewWillEnterPictureInPictureMode(_ playerView: PUIPlayerView) {

0 commit comments

Comments
 (0)