PictureInPicturePlaybackOwners.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/media/player/PictureInPicturePlaybackOwners.kt
1package eu.friedcutlet.yokan.services.media.player23import eu.friedcutlet.yokan.util.PlatformLock45/** PiP is installation-owned; its eligibility and requests retain the current player capability. */6internal class PictureInPicturePlaybackOwners(7 private val publish: (active: Boolean, eligible: Boolean, admitted: () -> Boolean) -> Unit,8 private val request: (MediaPlayerController?) -> Boolean,9) {10 private val lock = PlatformLock()11 private var current: Owner? = null1213 fun create(admitted: () -> Boolean, controller: MediaPlayerController? = null): Owner = Owner(admitted, controller)14 fun acquire(admitted: () -> Boolean): Owner = create(admitted).also { it.activate() }1516 inner class Owner internal constructor(private val admitted: () -> Boolean, private val controller: MediaPlayerController?) {17 internal var retired = false18 fun activate() = lock.withLock {19 if (retired || current === this) return@withLock20 current?.retired = true21 current = this22 publish(false, false, this::active)23 }24 fun active(): Boolean = lock.withLock { !retired && admitted() && current === this }25 fun update(playing: Boolean) = lock.withLock {26 if (retired || current !== this) return@withLock27 val active = active()28 publish(active, active && playing, this::active)29 }30 // Eligibility rules stay platform-specific (browser PiP can be toggled while paused).31 fun request(): Boolean = lock.withLock { active() && this@PictureInPicturePlaybackOwners.request.invoke(controller) }32 fun close() = lock.withLock {33 retired = true34 if (current === this) {35 current = null36 publish(false, false) { false }37 }38 }39 }40}4142internal val PlayerPictureInPictureOwners = PictureInPicturePlaybackOwners(43 publish = pictureInPicturePlatform::setPlaybackState,44 request = pictureInPicturePlatform::request,45)46