diff --git a/core/model/src/main/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventions.kt b/core/model/src/main/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventions.kt index 216d2596..70fe7c5e 100644 --- a/core/model/src/main/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventions.kt +++ b/core/model/src/main/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventions.kt @@ -221,7 +221,7 @@ fun settingsScreenSubtitleLabel(): UiText = uiText("settings_screen_subtitle", "管理媒体源、WebUI 和元数据服务") fun settingsMenuPanelDescriptionAndroidTv(): UiText = - uiText("settings_menu_panel_desc_tv", "按上下切换分类,向右进入当前设置。") + uiText("settings_menu_panel_desc_tv", "按上下选择分类,按确定打开,向右进入设置。") fun settingsOpenLibraryActionLabel(): UiText = uiText("settings_open_library", "打开海报墙") diff --git a/core/model/src/test/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventionsTest.kt b/core/model/src/test/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventionsTest.kt index a4deb79b..1246b685 100644 --- a/core/model/src/test/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventionsTest.kt +++ b/core/model/src/test/kotlin/com/miruplay/tv/model/SettingsSectionDisplayConventionsTest.kt @@ -123,7 +123,7 @@ class SettingsSectionDisplayConventionsTest { assertEquals("设置", settingsScreenTitleLabel().fallback) assertEquals("管理媒体源、WebUI 和元数据服务", settingsScreenSubtitleLabel().fallback) assertEquals("设置菜单", settingsMenuPanelTitle().fallback) - assertEquals("按上下切换分类,向右进入当前设置。", settingsMenuPanelDescriptionAndroidTv().fallback) + assertEquals("按上下选择分类,按确定打开,向右进入设置。", settingsMenuPanelDescriptionAndroidTv().fallback) assertEquals("打开海报墙", settingsOpenLibraryActionLabel().fallback) assertEquals("扫描当前源", settingsScanActiveSourceActionLabel().fallback) assertEquals("打开播放器", settingsOpenPlayerActionLabel().fallback) diff --git a/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvButton.kt b/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvButton.kt index 409ac9c3..2d58abd2 100644 --- a/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvButton.kt +++ b/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvButton.kt @@ -45,6 +45,9 @@ fun TvButton( val contentColor = if (enabled) Color.White else TextSecondary val borderColor = when { + // 不可用的按钮也保留焦点框:TV 上按钮一旦变成不可聚焦, + // 焦点就会被甩到别处(最常见是左侧菜单),下一个方向键就变成"翻页" + !enabled && isFocused -> Color.White.copy(alpha = 0.8f) !enabled -> Color.White.copy(alpha = 0.08f) isFocused -> Color.White secondary -> Color.White.copy(alpha = 0.18f) @@ -52,8 +55,9 @@ fun TvButton( } Button( - onClick = onClick, - enabled = enabled, + // 始终 enabled,用 guard 拦点击:disabled 时仍可聚焦,焦点不会离开当前按钮 + onClick = { if (enabled) onClick() }, + enabled = true, interactionSource = interactionSource, modifier = modifier .defaultMinSize(minWidth = 240.dp) diff --git a/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvTextField.kt b/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvTextField.kt index 9d49f2f0..73a3f499 100644 --- a/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvTextField.kt +++ b/ui-tv/src/main/kotlin/com/miruplay/tv/ui/components/TvTextField.kt @@ -19,6 +19,7 @@ import androidx.compose.ui.input.key.key import androidx.compose.ui.input.key.onPreviewKeyEvent import androidx.compose.ui.input.key.type import androidx.compose.ui.platform.LocalFocusManager +import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.text.TextStyle @@ -37,6 +38,7 @@ fun TvTextField( requestInitialFocus: Boolean = false, ) { val focusManager = LocalFocusManager.current + val keyboardController = LocalSoftwareKeyboardController.current var isFocused by remember { mutableStateOf(false) } val focusRequester = remember { FocusRequester() } LaunchedEffect(requestInitialFocus) { @@ -81,6 +83,12 @@ fun TvTextField( android.view.KeyEvent.KEYCODE_DPAD_UP -> { focusManager.moveFocus(FocusDirection.Up); true } + android.view.KeyEvent.KEYCODE_DPAD_CENTER, + android.view.KeyEvent.KEYCODE_ENTER -> { + // 焦点移过来时不弹键盘,按 OK 才真正开始编辑 + if (isFocused) keyboardController?.show() + false + } else -> false } }, @@ -92,7 +100,12 @@ fun TvTextField( modifier = Modifier .fillMaxWidth() .focusRequester(focusRequester) - .onFocusChanged { isFocused = it.isFocused }, + .onFocusChanged { state -> + isFocused = state.isFocused + // 遥控器只是把焦点移过来(比如从左侧菜单按右键进来)时不要弹输入法, + // 否则整屏被键盘盖住;真正要编辑由 OK 键触发。 + if (state.isFocused) keyboardController?.hide() + }, textStyle = TextStyle( color = TextPrimary, fontSize = 16.sp diff --git a/ui-tv/src/main/kotlin/com/miruplay/tv/ui/settings/AddSourceScreen.kt b/ui-tv/src/main/kotlin/com/miruplay/tv/ui/settings/AddSourceScreen.kt index fb54ff15..37be962d 100644 --- a/ui-tv/src/main/kotlin/com/miruplay/tv/ui/settings/AddSourceScreen.kt +++ b/ui-tv/src/main/kotlin/com/miruplay/tv/ui/settings/AddSourceScreen.kt @@ -89,6 +89,7 @@ import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue @@ -385,6 +386,7 @@ import com.google.zxing.common.BitMatrix import com.google.zxing.qrcode.QRCodeWriter import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import java.text.SimpleDateFormat import java.util.Date import java.util.Locale @@ -1092,6 +1094,26 @@ private fun SettingsHeader(onNavigateBack: () -> Unit) { } } +internal enum class SettingsMenuFocusAction { + NONE, + RESTORE_SELECTION, +} + +/** + * 菜单项拿到焦点时该做什么: + * - 焦点进来只负责高亮,**绝不**因为焦点变化就切分类(页面切换只认 OK 键); + * - 焦点从右侧内容区误入到别的分类项时,把焦点拉回当前选中项,避免光标与页面不一致。 + */ +internal fun settingsMenuFocusAction( + isFocused: Boolean, + enteredFromContent: Boolean, + selected: Boolean, +): SettingsMenuFocusAction = when { + !isFocused -> SettingsMenuFocusAction.NONE + enteredFromContent && !selected -> SettingsMenuFocusAction.RESTORE_SELECTION + else -> SettingsMenuFocusAction.NONE +} + @Composable private fun SettingsMenuPanel( selectedSection: MiruPlaySettingsSection, @@ -1162,7 +1184,14 @@ private fun SettingsMenuPanel( }, ) - SettingsPanel(modifier = modifier) { + // 焦点从右侧内容区误入菜单时不应顺手切页:只有焦点本来就在菜单里(用户按上下浏览)才切换分类, + // 误入到别的分类项时把焦点拉回当前选中项,免得光标停在与页面不一致的地方 + var menuHasFocus by remember { mutableStateOf(false) } + val restoreScope = rememberCoroutineScope() + + SettingsPanel( + modifier = modifier.onFocusChanged { menuHasFocus = it.hasFocus } + ) { Text( text = settingsMenuPanelTitle().resolve(), style = TvTypography.subtitle, @@ -1188,7 +1217,13 @@ private fun SettingsMenuPanel( section = section, summary = section.settingsMenuSummary(menuSummaryInput), selected = section == selectedSection, + switchOnFocus = menuHasFocus, onClick = { onSectionSelected(section) }, + onRestoreSelection = { + restoreScope.launch { + menuFocusRequesters.getValue(selectedSection).requestFocus() + } + }, modifier = Modifier.focusRequester(menuFocusRequesters.getValue(section)) ) } @@ -1201,7 +1236,9 @@ private fun SettingsMenuItem( section: MiruPlaySettingsSection, summary: UiText, selected: Boolean, + switchOnFocus: Boolean, onClick: () -> Unit, + onRestoreSelection: () -> Unit, modifier: Modifier = Modifier ) { val interactionSource = remember { MutableInteractionSource() } @@ -1225,7 +1262,10 @@ private fun SettingsMenuItem( .background(background) .border(if (selected || isFocused) 2.dp else 1.dp, borderColor, RoundedCornerShape(8.dp)) .onFocusChanged { state -> - if (state.isFocused) onClick() + when (settingsMenuFocusAction(state.isFocused, enteredFromContent = !switchOnFocus, selected = selected)) { + SettingsMenuFocusAction.NONE -> Unit + SettingsMenuFocusAction.RESTORE_SELECTION -> onRestoreSelection() + } } .tvFocusableClickable( interactionSource = interactionSource, diff --git a/ui-tv/src/main/res/values-en/strings.xml b/ui-tv/src/main/res/values-en/strings.xml index 9aef00db..497b534c 100644 --- a/ui-tv/src/main/res/values-en/strings.xml +++ b/ui-tv/src/main/res/values-en/strings.xml @@ -138,7 +138,7 @@ Enabled · sweep calibrated Disabled Settings menu - Move up/down to switch sections, right to enter. + Move up/down to choose a section, press OK to open it, right to enter. Settings Manage media sources, WebUI and metadata services Open poster wall diff --git a/ui-tv/src/main/res/values-zh-rHK/strings.xml b/ui-tv/src/main/res/values-zh-rHK/strings.xml index de8daaee..afae023e 100644 --- a/ui-tv/src/main/res/values-zh-rHK/strings.xml +++ b/ui-tv/src/main/res/values-zh-rHK/strings.xml @@ -664,7 +664,7 @@ 未設定 已設定 1 項 已設定 2 項 - 按上下切換分類,向右進入目前設定。 + 按上下選擇分類,按確定開啟,向右進入設定。 設定選單 %1$s · %2$s%3$s · %4$s %1$s diff --git a/ui-tv/src/main/res/values-zh-rTW/strings.xml b/ui-tv/src/main/res/values-zh-rTW/strings.xml index c55f09a3..812c1944 100644 --- a/ui-tv/src/main/res/values-zh-rTW/strings.xml +++ b/ui-tv/src/main/res/values-zh-rTW/strings.xml @@ -138,7 +138,7 @@ 已啟用 · 掃頻校準 未啟用 設定選單 - 按上下切換分類,向右進入目前設定。 + 按上下選擇分類,按確定開啟,向右進入設定。 設定 管理媒體源、WebUI 和後設資料服務 開啟海報牆 diff --git a/ui-tv/src/main/res/values/strings.xml b/ui-tv/src/main/res/values/strings.xml index 23afadd9..dc4f18b9 100644 --- a/ui-tv/src/main/res/values/strings.xml +++ b/ui-tv/src/main/res/values/strings.xml @@ -614,7 +614,7 @@ 未设置 已设置 1 项 已设置 2 项 - 按上下切换分类,向右进入当前设置。 + 按上下选择分类,按确定打开,向右进入设置。 设置菜单 %1$s 未启用 diff --git a/ui-tv/src/test/kotlin/com/miruplay/tv/ui/settings/SettingsMenuFocusActionTest.kt b/ui-tv/src/test/kotlin/com/miruplay/tv/ui/settings/SettingsMenuFocusActionTest.kt new file mode 100644 index 00000000..f1a63fc5 --- /dev/null +++ b/ui-tv/src/test/kotlin/com/miruplay/tv/ui/settings/SettingsMenuFocusActionTest.kt @@ -0,0 +1,39 @@ +package com.miruplay.tv.ui.settings + +import org.junit.Assert.assertEquals +import org.junit.Test + +class SettingsMenuFocusActionTest { + @Test + fun `focus moving inside the menu only highlights and never switches the section`() { + assertEquals( + SettingsMenuFocusAction.NONE, + settingsMenuFocusAction(isFocused = true, enteredFromContent = false, selected = false), + ) + } + + @Test + fun `focus entering the menu from the content pane restores the selected item`() { + // 从右侧内容区误入菜单(例如点完按钮后焦点掉进菜单):不切页,把焦点拉回当前选中项 + assertEquals( + SettingsMenuFocusAction.RESTORE_SELECTION, + settingsMenuFocusAction(isFocused = true, enteredFromContent = true, selected = false), + ) + } + + @Test + fun `focus entering the menu on the selected item does nothing`() { + assertEquals( + SettingsMenuFocusAction.NONE, + settingsMenuFocusAction(isFocused = true, enteredFromContent = true, selected = true), + ) + } + + @Test + fun `losing focus never does anything`() { + assertEquals( + SettingsMenuFocusAction.NONE, + settingsMenuFocusAction(isFocused = false, enteredFromContent = true, selected = false), + ) + } +}