Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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", "打开海报墙")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ 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)
else -> Color.Transparent
}

Button(
onClick = onClick,
enabled = enabled,
// 始终 enabled,用 guard 拦点击:disabled 时仍可聚焦,焦点不会离开当前按钮
onClick = { if (enabled) onClick() },
enabled = true,
interactionSource = interactionSource,
modifier = modifier
.defaultMinSize(minWidth = 240.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
},
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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))
)
}
Expand All @@ -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() }
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ui-tv/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<string name="settings_menu_audio_dsp_on">Enabled · sweep calibrated</string>
<string name="settings_menu_audio_dsp_off">Disabled</string>
<string name="settings_menu_panel_title">Settings menu</string>
<string name="settings_menu_panel_desc_tv">Move up/down to switch sections, right to enter.</string>
<string name="settings_menu_panel_desc_tv">Move up/down to choose a section, press OK to open it, right to enter.</string>
<string name="settings_screen_title">Settings</string>
<string name="settings_screen_subtitle">Manage media sources, WebUI and metadata services</string>
<string name="settings_open_library">Open poster wall</string>
Expand Down
2 changes: 1 addition & 1 deletion ui-tv/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@
<string name="settings_menu_metadata_tokens_none">未設定</string>
<string name="settings_menu_metadata_tokens_one">已設定 1 項</string>
<string name="settings_menu_metadata_tokens_two">已設定 2 項</string>
<string name="settings_menu_panel_desc_tv">按上下切換分類,向右進入目前設定。</string>
<string name="settings_menu_panel_desc_tv">按上下選擇分類,按確定開啟,向右進入設定。</string>
<string name="settings_menu_panel_title">設定選單</string>
<string name="settings_menu_playback_summary">%1$s · %2$s%3$s · %4$s</string>
<string name="settings_menu_proxy_address">%1$s</string>
Expand Down
2 changes: 1 addition & 1 deletion ui-tv/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<string name="settings_menu_audio_dsp_on">已啟用 · 掃頻校準</string>
<string name="settings_menu_audio_dsp_off">未啟用</string>
<string name="settings_menu_panel_title">設定選單</string>
<string name="settings_menu_panel_desc_tv">按上下切換分類,向右進入目前設定。</string>
<string name="settings_menu_panel_desc_tv">按上下選擇分類,按確定開啟,向右進入設定。</string>
<string name="settings_screen_title">設定</string>
<string name="settings_screen_subtitle">管理媒體源、WebUI 和後設資料服務</string>
<string name="settings_open_library">開啟海報牆</string>
Expand Down
2 changes: 1 addition & 1 deletion ui-tv/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@
<string name="settings_menu_metadata_tokens_none">未设置</string>
<string name="settings_menu_metadata_tokens_one">已设置 1 项</string>
<string name="settings_menu_metadata_tokens_two">已设置 2 项</string>
<string name="settings_menu_panel_desc_tv">按上下切换分类,向右进入当前设置。</string>
<string name="settings_menu_panel_desc_tv">按上下选择分类,按确定打开,向右进入设置。</string>
<string name="settings_menu_panel_title">设置菜单</string>
<string name="settings_menu_proxy_address">%1$s</string>
<string name="settings_menu_proxy_off">未启用</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
)
}
}
Loading