Morph the expanded player into the mini player on drag-to-dismiss

Releasing the dismiss drag now shrinks the player window onto its video and
flies it into the mini player's preview instead of sliding it off screen and
showing the mini player afterwards. The mini player stays mounted under the
expanded player and reports its preview frame, and the window is masked down
to the video while its root view is transformed onto that frame (with the
safe area the transform drops added back, so the content doesn't jump).

The handoff of the shared MPV view is made seamless: a freshly made player
container waits for its first layout before taking a view that is on screen
elsewhere, sizes and redraws it there, and the window hides in that same
frame. Resizing the render view draws the current frame into the recreated
framebuffer right away instead of leaving it blank until the next tick.

Falls back to the slide when no preview target is available (rotated
session, video preview hidden, different window size).
This commit is contained in:
Arkadiusz Fal
2026-10-09 23:59:51 +02:00
parent cf8d477351
commit 7b1a24a5b0
7 changed files with 377 additions and 27 deletions

View File

@@ -52,6 +52,13 @@ struct MPVRenderViewRepresentable: UIViewRepresentable {
}
}
extension Notification.Name {
/// Posted (synchronously, object: the player view) when a container adopts
/// the shared player view — lets the expanded player hide its window in the
/// same pass the mini player takes the video over.
static let mpvPlayerViewDidChangeContainer = Notification.Name("mpvPlayerViewDidChangeContainer")
}
/// Container view that properly manages player view swapping
private class MPVContainerView: UIView {
private weak var currentPlayerView: UIView?
@@ -81,7 +88,7 @@ private class MPVContainerView: UIView {
if container !== self && container.window != nil {
MPVLogging.log("MPVContainerView deinit: transferring player view to surviving container",
details: "from:\(containerID.uuidString.prefix(8)) to:\(container.containerID.uuidString.prefix(8))")
container.setPlayerView(playerView)
container.setPlayerView(playerView, force: true)
return
}
}
@@ -99,7 +106,19 @@ private class MPVContainerView: UIView {
}
}
func setPlayerView(_ playerView: UIView) {
/// A player view waiting for this container's first layout (see setPlayerView)
private weak var pendingPlayerView: UIView?
override func layoutSubviews() {
super.layoutSubviews()
if let playerView = pendingPlayerView, !bounds.isEmpty {
pendingPlayerView = nil
setPlayerView(playerView, force: true)
}
}
/// - Parameter force: adopt now even while this container is still unsized.
func setPlayerView(_ playerView: UIView, force: Bool = false) {
// Skip only if same view AND actually our subview
// (weak ref can point to a view that's been stolen by another container)
if playerView === currentPlayerView && playerView.superview === self {
@@ -107,6 +126,17 @@ private class MPVContainerView: UIView {
return
}
// A freshly made container is zero-sized until SwiftUI lays it out.
// Taking a view that's on screen elsewhere now would show it unsized
// (blank) there and leave a black hole where it was, so wait for the
// first layout, where it is sized and redrawn in the same frame.
if !force && bounds.isEmpty && playerView.window != nil {
pendingPlayerView = playerView
setNeedsLayout()
return
}
pendingPlayerView = nil
// Check if player view is in another container
let isInAnotherContainer = playerView.superview is MPVContainerView && playerView.superview !== self
@@ -128,8 +158,12 @@ private class MPVContainerView: UIView {
playerView.leadingAnchor.constraint(equalTo: leadingAnchor),
playerView.trailingAnchor.constraint(equalTo: trailingAnchor)
])
currentPlayerView = playerView
// Size it (and re-render its framebuffer) before anyone reacts to the move
if !bounds.isEmpty {
layoutIfNeeded()
}
NotificationCenter.default.post(name: .mpvPlayerViewDidChangeContainer, object: playerView)
}
}