Skip to content

Commit

Permalink
支持按 ESC 退出全屏, 修复快捷键焦点问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed May 19, 2024
1 parent 71b17cb commit dbe09e9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/shared/pages/episode-play/common/EpisodePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ private fun EpisodeVideo(
context.setRequestFullScreen(true)
}
},
onExitFullscreen = {
context.setRequestFullScreen(false)
vm.isFullscreen = false
},
danmakuEnabled = { danmakuEnabled },
setDanmakuEnabled = { vm.launchInBackground { danmaku.setEnabled(it) } },
danmakuEditor = {
Expand Down
4 changes: 3 additions & 1 deletion app/shared/pages/episode-play/common/EpisodeVideo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ internal fun EpisodeVideoImpl(
videoLoadingState: () -> VideoLoadingState,
danmakuConfig: () -> DanmakuConfig,
onClickFullScreen: () -> Unit,
onExitFullscreen: () -> Unit,
danmakuEnabled: () -> Boolean,
setDanmakuEnabled: (enabled: Boolean) -> Unit,
danmakuEditor: @Composable (RowScope.() -> Unit),
Expand Down Expand Up @@ -159,7 +160,8 @@ internal fun EpisodeVideoImpl(
},
onToggleFullscreen = {
onClickFullScreen()
}
},
onExitFullscreen = onExitFullscreen,
)
},
floatingMessage = {
Expand Down
2 changes: 2 additions & 0 deletions app/shared/video-player/common/ui/guesture/GestureLock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ fun LockableVideoGestureHost(
modifier: Modifier = Modifier,
onTogglePauseResume: () -> Unit = {},
onToggleFullscreen: () -> Unit = {},
onExitFullscreen: () -> Unit = {},
) {
if (locked) {
LockedScreenGestureHost(controllerVisible, setControllerVisible, modifier)
Expand All @@ -145,6 +146,7 @@ fun LockableVideoGestureHost(
},
onTogglePauseResume = onTogglePauseResume,
onToggleFullscreen = onToggleFullscreen,
onExitFullscreen = onExitFullscreen,
)
}
}
20 changes: 19 additions & 1 deletion app/shared/video-player/common/ui/guesture/VideoGestureHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.coerceAtLeast
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -358,6 +359,7 @@ enum class GestureFamily(
val keyboardUpDownForVolume: Boolean = true,
val keyboardLeftRightToSeek: Boolean = true,
val mouseHoverForController: Boolean = true,
val escToExitFullscreen: Boolean = true,
) {
TOUCH(
clickToPauseResume = false,
Expand Down Expand Up @@ -392,6 +394,7 @@ fun VideoGestureHost(
onToggleControllerVisibility: (setVisible: Boolean?) -> Unit = {},
onTogglePauseResume: () -> Unit = {},
onToggleFullscreen: () -> Unit = {},
onExitFullscreen: () -> Unit = {},
) {
val onTogglePauseResumeState by rememberUpdatedState(onTogglePauseResume)
val onToggleControllerVisibilityState by rememberUpdatedState(onToggleControllerVisibility)
Expand Down Expand Up @@ -424,9 +427,12 @@ fun VideoGestureHost(

val indicatorTasker = rememberUiMonoTasker()
val focusRequester = remember { FocusRequester() }
val manager = LocalFocusManager.current
val keyboardFocus = remember { FocusRequester() } // focus 了才能用键盘快捷键

Box(
modifier
.focusRequester(keyboardFocus)
.padding(top = 60.dp)
.ifThen(family.swipeToSeek) {
swipeToSeek(seekerState, Orientation.Horizontal)
Expand Down Expand Up @@ -460,20 +466,29 @@ fun VideoGestureHost(
val scope = rememberUiMonoTasker()
onPointerEventMultiplatform(PointerEventType.Move) { events ->
onToggleControllerVisibilityState(true)
keyboardFocus.requestFocus()
scope.launch {
delay(3000)
onToggleControllerVisibilityState(false)
}
}
}
.ifThen(family.escToExitFullscreen) {
onKey(ComposeKey.Escape) {
if (needWorkaroundForFocusManager) {
manager.clearFocus()
}
onExitFullscreen()
}
}
.fillMaxSize()
) {
Box(
Modifier
.ifThen(needWorkaroundForFocusManager) {
onFocusEvent {
if (it.hasFocus) {
focusRequester.requestFocus() // 随便转移走就行
focusRequester.requestFocus()
}
}
}
Expand All @@ -493,6 +508,9 @@ fun VideoGestureHost(
},
onDoubleClick = remember(family, onToggleFullscreen) {
{
if (needWorkaroundForFocusManager) {
manager.clearFocus()
}
if (family.doubleClickToFullscreen) {
onToggleFullscreen()
}
Expand Down

0 comments on commit dbe09e9

Please sign in to comment.