Only adds a border to video controls when in full screen

This commit is contained in:
Vitor Pamplona
2023-10-15 18:07:40 -04:00
parent 1767cc74a9
commit e1e42ed500
@@ -43,10 +43,12 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.LayoutCoordinates import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInWindow import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalView import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.isFinite import androidx.compose.ui.unit.isFinite
import androidx.compose.ui.unit.isSpecified import androidx.compose.ui.unit.isSpecified
@@ -534,115 +536,134 @@ private fun RenderVideoPlayer(
mutableStateOf(false) mutableStateOf(false)
} }
BoxWithConstraints() { val parentVideoPlaybackSize = remember {
val borders = MaterialTheme.colorScheme.imageModifier mutableStateOf<IntSize>(IntSize.Zero)
}
val myModifier = remember { val videoPlaybackSize = remember {
if (roundedCorner) { mutableStateOf<IntSize>(IntSize.Zero)
borders }
.defaultMinSize(minHeight = 100.dp)
.align(Alignment.Center) BoxWithConstraints(
} else { contentAlignment = Alignment.Center,
Modifier modifier = Modifier.fillMaxSize().onSizeChanged {
.fillMaxWidth() parentVideoPlaybackSize.value = it
.defaultMinSize(minHeight = 100.dp)
.align(Alignment.Center)
}
} }
) {
BoxWithConstraints(
modifier = Modifier.onSizeChanged {
videoPlaybackSize.value = it
}
) {
val borders = MaterialTheme.colorScheme.imageModifier
val factory = remember(controller) { val myModifier = remember {
{ context: Context -> if (roundedCorner) {
PlayerView(context).apply { borders
player = controller .defaultMinSize(minHeight = 100.dp)
layoutParams = FrameLayout.LayoutParams( .align(Alignment.Center)
ViewGroup.LayoutParams.MATCH_PARENT, } else {
ViewGroup.LayoutParams.WRAP_CONTENT Modifier
) .fillMaxWidth()
controllerAutoShow = false .defaultMinSize(minHeight = 100.dp)
thumbData?.thumb?.let { defaultArtwork = it } .align(Alignment.Center)
hideController()
resizeMode =
if (maxHeight.isFinite) AspectRatioFrameLayout.RESIZE_MODE_FIT else AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH
onDialog?.let { innerOnDialog ->
setFullscreenButtonClickListener {
controller.pause()
innerOnDialog(it)
}
}
setControllerVisibilityListener(
PlayerView.ControllerVisibilityListener { visible ->
controllerVisible.value = visible == View.VISIBLE
onControllerVisibilityChanged?.let { callback ->
callback(visible == View.VISIBLE)
}
}
)
} }
} }
}
AndroidView( val factory = remember(controller) {
modifier = myModifier, { context: Context ->
factory = factory PlayerView(context).apply {
) player = controller
layoutParams = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
controllerAutoShow = false
thumbData?.thumb?.let { defaultArtwork = it }
hideController()
resizeMode =
if (maxHeight.isFinite) AspectRatioFrameLayout.RESIZE_MODE_FIT else AspectRatioFrameLayout.RESIZE_MODE_FIXED_WIDTH
onDialog?.let { innerOnDialog ->
setFullscreenButtonClickListener {
controller.pause()
innerOnDialog(it)
}
}
setControllerVisibilityListener(
PlayerView.ControllerVisibilityListener { visible ->
controllerVisible.value = visible == View.VISIBLE
onControllerVisibilityChanged?.let { callback ->
callback(visible == View.VISIBLE)
}
}
)
}
}
}
waveform?.let { AndroidView(
Waveform(it, controller, remember { Modifier.align(Alignment.Center) }) modifier = myModifier,
} factory = factory
)
val startingMuteState = remember(controller) { waveform?.let {
controller.volume < 0.001 Waveform(it, controller, remember { Modifier.align(Alignment.Center) })
} }
MuteButton( val startingMuteState = remember(controller) {
controllerVisible, controller.volume < 0.001
startingMuteState, }
remember {
if (topPaddingForControllers.isSpecified) { val spaceModifier =
Modifier.padding(top = topPaddingForControllers) if (topPaddingForControllers.isSpecified && videoPlaybackSize.value.height > 0) {
val space = (abs(parentVideoPlaybackSize.value.height - videoPlaybackSize.value.height) / 2).dp
if (space > topPaddingForControllers) {
Modifier
} else {
Modifier.padding(top = topPaddingForControllers - space)
}
} else { } else {
Modifier Modifier
} }
}
) { mute: Boolean ->
// makes the new setting the default for new creations.
DefaultMutedSetting.value = mute
// if the user unmutes a video and it's not the current playing, switches to that one. MuteButton(
if (!mute && keepPlayingMutex != null && keepPlayingMutex != controller) { controllerVisible,
keepPlayingMutex?.stop() startingMuteState,
keepPlayingMutex?.release() spaceModifier
keepPlayingMutex = null ) { mute: Boolean ->
} // makes the new setting the default for new creations.
DefaultMutedSetting.value = mute
controller.volume = if (mute) 0f else 1f // if the user unmutes a video and it's not the current playing, switches to that one.
} if (!mute && keepPlayingMutex != null && keepPlayingMutex != controller) {
KeepPlayingButton(
keepPlaying,
controllerVisible,
remember {
if (topPaddingForControllers.isSpecified) {
Modifier.align(Alignment.TopEnd).padding(top = topPaddingForControllers)
} else {
Modifier.align(Alignment.TopEnd)
}
}
) { newKeepPlaying: Boolean ->
// If something else is playing and the user marks this video to keep playing, stops the other one.
if (newKeepPlaying) {
if (keepPlayingMutex != null && keepPlayingMutex != controller) {
keepPlayingMutex?.stop() keepPlayingMutex?.stop()
keepPlayingMutex?.release() keepPlayingMutex?.release()
}
keepPlayingMutex = controller
} else {
if (keepPlayingMutex == controller) {
keepPlayingMutex = null keepPlayingMutex = null
} }
controller.volume = if (mute) 0f else 1f
} }
keepPlaying.value = newKeepPlaying KeepPlayingButton(
keepPlaying,
controllerVisible,
spaceModifier.align(Alignment.TopEnd)
) { newKeepPlaying: Boolean ->
// If something else is playing and the user marks this video to keep playing, stops the other one.
if (newKeepPlaying) {
if (keepPlayingMutex != null && keepPlayingMutex != controller) {
keepPlayingMutex?.stop()
keepPlayingMutex?.release()
}
keepPlayingMutex = controller
} else {
if (keepPlayingMutex == controller) {
keepPlayingMutex = null
}
}
keepPlaying.value = newKeepPlaying
}
} }
} }
} }