Skip to content

Commit

Permalink
Ok, hear me out, no more private API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
allenhumphreys committed Jun 6, 2023
1 parent 089043d commit deac057
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 99 deletions.
39 changes: 0 additions & 39 deletions PlayerUI/PiP Support/PIP.h

This file was deleted.

2 changes: 0 additions & 2 deletions PlayerUI/PlayerUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ FOUNDATION_EXPORT double PlayerUIVersionNumber;
FOUNDATION_EXPORT const unsigned char PlayerUIVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <PlayerUI/PublicHeader.h>

#import <PlayerUI/PIP.h>
119 changes: 73 additions & 46 deletions PlayerUI/Views/PUIPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Cocoa
import AVFoundation
import os.log
import AVKit

public final class PUIPlayerView: NSView {

Expand Down Expand Up @@ -340,7 +341,7 @@ public final class PUIPlayerView: NSView {
}

fileprivate func updatePlayingState() {
pipController?.setPlaying(isPlaying)
// pipController?.setPlaying(isPlaying)

if isPlaying {
playButton.image = .PUIPause
Expand Down Expand Up @@ -645,7 +646,8 @@ public final class PUIPlayerView: NSView {
let b = PUIButton(frame: .zero)

b.isToggle = true
b.image = .PUIPictureInPicture
b.image = AVPictureInPictureController.pictureInPictureButtonStartImage
b.alternateImage = AVPictureInPictureController.pictureInPictureButtonStopImage
b.target = self
b.action = #selector(togglePip)
b.toolTip = "Toggle picture in picture"
Expand Down Expand Up @@ -1018,7 +1020,7 @@ public final class PUIPlayerView: NSView {

@IBAction public func togglePip(_ sender: NSView?) {
if isInPictureInPictureMode {
exitPictureInPictureMode()
pipController?.stopPictureInPicture()
} else {
enterPictureInPictureMode()
}
Expand Down Expand Up @@ -1244,7 +1246,7 @@ public final class PUIPlayerView: NSView {
}
}

fileprivate var pipController: PIPViewController?
fileprivate var pipController: AVPictureInPictureController?

fileprivate func enterPictureInPictureMode() {
delegate?.playerViewWillEnterPictureInPictureMode(self)
Expand All @@ -1253,21 +1255,14 @@ public final class PUIPlayerView: NSView {
self?.externalStatusController.snapshot = image
}

pipController = PIPViewController()
pipController = AVPictureInPictureController(playerLayer: playerLayer)
pipController?.delegate = self
pipController?.setPlaying(isPlaying)
pipController?.aspectRatio = currentPresentationSize ?? NSSize(width: 640, height: 360)
pipController?.view.layer?.backgroundColor = NSColor.black.cgColor
// pipController?.setPlaying(isPlaying)
// pipController?.aspectRatio = currentPresentationSize ?? NSSize(width: 640, height: 360)
// pipController?.view.layer?.backgroundColor = NSColor.black.cgColor

pipController?.presentAsPicture(inPicture: pictureContainer)

isInPictureInPictureMode = true
}

fileprivate func exitPictureInPictureMode() {
if pictureContainer.presentingViewController == pipController {
pipController?.dismiss(pictureContainer)
}
// pipController?.presentAsPicture(inPicture: pictureContainer)
pipController?.startPictureInPicture()
}

// MARK: - Visibility management
Expand Down Expand Up @@ -1621,51 +1616,83 @@ extension PUIPlayerView: PUIExternalPlaybackConsumer {

// MARK: - PiP delegate

extension PUIPlayerView: PIPViewControllerDelegate, PUIPictureContainerViewControllerDelegate {
extension PUIPlayerView: PUIPictureContainerViewControllerDelegate, AVPictureInPictureControllerDelegate {

// public func pipActionStop(_ pip: PIPViewController) {
// pause(pip)
//
// }
//
// public func pipActionReturn(_ pip: PIPViewController) {
// delegate?.playerViewWillExitPictureInPictureModelegate?.playerViewWillExitPictureInPictureMode(self, reason: .exitButton)de(self, reason: .returnButton)
//
// if !NSApp.isActive {
// NSApp.activate(ignoringOtherApps: true)
// }
//
// if let window = lastKnownWindow {
// window.makeKeyAndOrderFront(pip)
//
// if window.isMiniaturized {
// window.deminiaturize(nil)
// }
// }
// }

// public func pipActionPause(_ pip: PIPViewController) {
// pause(pip)
// }
//
// public func pipActionPlay(_ pip: PIPViewController) {
// play(pip)
// }

public func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, failedToStartPictureInPictureWithError error: Error) {
isInPictureInPictureMode = false
pipController = nil
}

public func pipActionStop(_ pip: PIPViewController) {
pause(pip)
delegate?.playerViewWillExitPictureInPictureMode(self, reason: .exitButton)
public func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
print("Player Rate: \(player?.rate)")
isInPictureInPictureMode = false
pipController = nil
}

public func pipActionReturn(_ pip: PIPViewController) {
public func pictureInPictureControllerDidStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
isInPictureInPictureMode = true
}

// public func pipDidClose(_ pip: PIPViewController) {
// pictureContainer.view.frame = bounds
//
// addSubview(pictureContainer.view, positioned: .below, relativeTo: scrimContainerView)
//
// isInPictureInPictureMode = false
// pipController = nil
// }

public func pictureInPictureControllerWillStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
delegate?.playerViewWillExitPictureInPictureMode(self, reason: .returnButton)
print("Player Rate: \(player?.rate)")

if !NSApp.isActive {
NSApp.activate(ignoringOtherApps: true)
}

if let window = lastKnownWindow {
window.makeKeyAndOrderFront(pip)
window.makeKeyAndOrderFront(pictureInPictureController)

if window.isMiniaturized {
window.deminiaturize(nil)
}
}
}

public func pipActionPause(_ pip: PIPViewController) {
pause(pip)
}

public func pipActionPlay(_ pip: PIPViewController) {
play(pip)
}

public func pipDidClose(_ pip: PIPViewController) {
pictureContainer.view.frame = bounds

addSubview(pictureContainer.view, positioned: .below, relativeTo: scrimContainerView)

isInPictureInPictureMode = false
pipController = nil
}

public func pipWillClose(_ pip: PIPViewController) {
pip.replacementRect = frame
pip.replacementView = self
pip.replacementWindow = lastKnownWindow
}
// public func pipWillClose(_ pip: PIPViewController) {
// pip.replacementRect = frame
// pip.replacementView = self
// pip.replacementWindow = lastKnownWindow
// }

func pictureContainerViewSuperviewDidChange(to superview: NSView?) {
guard let superview = superview else { return }
Expand All @@ -1674,7 +1701,7 @@ extension PUIPlayerView: PIPViewControllerDelegate, PUIPictureContainerViewContr

if superview == self, pipController != nil {
if pictureContainer.presentingViewController == pipController {
pipController?.dismiss(pictureContainer)
pipController?.stopPictureInPicture()
}

pipController = nil
Expand Down
12 changes: 0 additions & 12 deletions WWDC.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@
DDF721CA1ECA12A40054C503 /* Images.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721A91ECA12A40054C503 /* Images.swift */; };
DDF721CB1ECA12A40054C503 /* Speeds.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721AA1ECA12A40054C503 /* Speeds.swift */; };
DDF721CC1ECA12A40054C503 /* PUIExternalPlaybackProviderRegistration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721AC1ECA12A40054C503 /* PUIExternalPlaybackProviderRegistration.swift */; };
DDF721CD1ECA12A40054C503 /* PIP.h in Headers */ = {isa = PBXBuildFile; fileRef = DDF721AE1ECA12A40054C503 /* PIP.h */; settings = {ATTRIBUTES = (Public, ); }; };
DDF721CE1ECA12A40054C503 /* PUIExternalPlaybackConsumer.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721B01ECA12A40054C503 /* PUIExternalPlaybackConsumer.swift */; };
DDF721CF1ECA12A40054C503 /* PUIExternalPlaybackProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721B11ECA12A40054C503 /* PUIExternalPlaybackProvider.swift */; };
DDF721D01ECA12A40054C503 /* PUIPlayerViewDelegates.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDF721B21ECA12A40054C503 /* PUIPlayerViewDelegates.swift */; };
Expand Down Expand Up @@ -420,7 +419,6 @@
DDF721A91ECA12A40054C503 /* Images.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Images.swift; sourceTree = "<group>"; };
DDF721AA1ECA12A40054C503 /* Speeds.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Speeds.swift; sourceTree = "<group>"; };
DDF721AC1ECA12A40054C503 /* PUIExternalPlaybackProviderRegistration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PUIExternalPlaybackProviderRegistration.swift; sourceTree = "<group>"; };
DDF721AE1ECA12A40054C503 /* PIP.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PIP.h; sourceTree = "<group>"; };
DDF721B01ECA12A40054C503 /* PUIExternalPlaybackConsumer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PUIExternalPlaybackConsumer.swift; sourceTree = "<group>"; };
DDF721B11ECA12A40054C503 /* PUIExternalPlaybackProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PUIExternalPlaybackProvider.swift; sourceTree = "<group>"; };
DDF721B21ECA12A40054C503 /* PUIPlayerViewDelegates.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PUIPlayerViewDelegates.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -974,7 +972,6 @@
DDF721A41ECA12A40054C503 /* Controllers */,
DDF721A71ECA12A40054C503 /* Definitions */,
DDF721AB1ECA12A40054C503 /* Models */,
DDF721AD1ECA12A40054C503 /* PiP Support */,
DDF721AF1ECA12A40054C503 /* Protocols */,
DDF721B51ECA12A40054C503 /* Resources */,
DDF721B81ECA12A40054C503 /* Util */,
Expand Down Expand Up @@ -1013,14 +1010,6 @@
path = Models;
sourceTree = "<group>";
};
DDF721AD1ECA12A40054C503 /* PiP Support */ = {
isa = PBXGroup;
children = (
DDF721AE1ECA12A40054C503 /* PIP.h */,
);
path = "PiP Support";
sourceTree = "<group>";
};
DDF721AF1ECA12A40054C503 /* Protocols */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1174,7 +1163,6 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
DDF721CD1ECA12A40054C503 /* PIP.h in Headers */,
DDF7219A1ECA12780054C503 /* PlayerUI.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit deac057

Please sign in to comment.