Skip to content

Commit 1318124

Browse files
author
while
committed
手柄
1 parent 3aec062 commit 1318124

5 files changed

Lines changed: 234 additions & 40 deletions

File tree

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<script setup lang="ts" generic="T">
2+
import { computed, nextTick, ref } from 'vue'
3+
import { navEnabled, useRemoteNav } from '../composables/useRemoteNav'
4+
5+
// 遥控/手柄友好的下拉框:
6+
// - 鼠标模式(navEnabled 为假):直接渲染原生 <select>,行为与体验完全不变。
7+
// - 遥控模式:渲染触发按钮 —— 选中后按「确定」弹出选项列表,上下键在列表内移动,
8+
// 再按「确定」才切换选项(而非原生 select 那样左右键即时改值,不符合遥控操作习惯)。
9+
// 弹层挂 priority=20 的 useRemoteNav(高于模态的 10),打开时独占按键;BACK/确定后回落。
10+
11+
interface Option {
12+
value: T
13+
label: string
14+
}
15+
16+
const props = defineProps<{ options: Option[] }>()
17+
const model = defineModel<T>()
18+
19+
const open = ref(false)
20+
const triggerRef = ref<HTMLElement | null>(null)
21+
const popupRef = ref<HTMLElement | null>(null)
22+
// 弹层固定定位坐标(打开时按触发按钮的矩形计算,避免被面板 overflow 裁剪)。
23+
const popupStyle = ref<Record<string, string>>({})
24+
25+
const currentLabel = computed(() => {
26+
const hit = props.options.find((o) => o.value === model.value)
27+
return hit ? hit.label : (props.options[0]?.label ?? '')
28+
})
29+
30+
function openPopup(): void {
31+
const trigger = triggerRef.value
32+
if (!trigger) return
33+
const r = trigger.getBoundingClientRect()
34+
// 优先向下展开;下方空间不足则向上。宽度不小于触发按钮。
35+
const belowSpace = window.innerHeight - r.bottom
36+
const style: Record<string, string> = {
37+
position: 'fixed',
38+
left: `${r.left}px`,
39+
minWidth: `${r.width}px`,
40+
maxHeight: `${Math.max(belowSpace, r.top) - 16}px`,
41+
}
42+
if (belowSpace >= 220 || belowSpace >= r.top) {
43+
style.top = `${r.bottom + 4}px`
44+
} else {
45+
style.bottom = `${window.innerHeight - r.top + 4}px`
46+
}
47+
popupStyle.value = style
48+
open.value = true
49+
// 弹层渲染后把焦点落到当前选中项(便于上下微调),供遥控/键盘导航起点。
50+
nextTick(() => {
51+
const el =
52+
popupRef.value?.querySelector<HTMLElement>('.rs-option[data-selected="true"]') ??
53+
popupRef.value?.querySelector<HTMLElement>('.rs-option')
54+
el?.focus()
55+
})
56+
}
57+
58+
function closePopup(): void {
59+
if (!open.value) return
60+
open.value = false
61+
// 关闭后焦点回到触发按钮,导航链路不断。
62+
nextTick(() => triggerRef.value?.focus())
63+
}
64+
65+
function toggle(): void {
66+
if (open.value) closePopup()
67+
else openPopup()
68+
}
69+
70+
function pick(opt: Option): void {
71+
model.value = opt.value
72+
closePopup()
73+
}
74+
75+
// 弹层内的焦点导航:上下选、确定切换(见各选项 @click)、BACK 关闭。
76+
useRemoteNav({
77+
container: popupRef,
78+
active: computed(() => navEnabled.value && open.value),
79+
onBack: closePopup,
80+
priority: 20,
81+
})
82+
</script>
83+
84+
<template>
85+
<!-- 鼠标模式:原生下拉,体验不变 -->
86+
<select v-if="!navEnabled" v-model="model" class="remote-select">
87+
<option v-for="opt in options" :key="String(opt.value)" :value="opt.value">
88+
{{ opt.label }}
89+
</option>
90+
</select>
91+
92+
<!-- 遥控模式:触发按钮 + 弹出列表 -->
93+
<button
94+
v-else
95+
ref="triggerRef"
96+
type="button"
97+
class="remote-select rs-trigger"
98+
:aria-expanded="open"
99+
@click="toggle"
100+
>
101+
<span class="rs-value">{{ currentLabel }}</span>
102+
<span class="rs-arrow" aria-hidden="true">▾</span>
103+
</button>
104+
105+
<Teleport v-if="open" to="body">
106+
<div class="rs-backdrop" @click="closePopup" />
107+
<div ref="popupRef" class="rs-popup" role="listbox" :style="popupStyle">
108+
<button
109+
v-for="opt in options"
110+
:key="String(opt.value)"
111+
type="button"
112+
class="rs-option"
113+
role="option"
114+
:data-selected="opt.value === model"
115+
:aria-selected="opt.value === model"
116+
@click="pick(opt)"
117+
>
118+
{{ opt.label }}
119+
</button>
120+
</div>
121+
</Teleport>
122+
</template>
123+
124+
<style scoped>
125+
.remote-select {
126+
height: 34px;
127+
border: 1px solid #434a54;
128+
border-radius: 6px;
129+
background: #171a1f;
130+
color: #eee;
131+
padding: 0 10px;
132+
font-size: 14px;
133+
}
134+
.rs-trigger {
135+
display: inline-flex;
136+
align-items: center;
137+
justify-content: space-between;
138+
gap: 8px;
139+
cursor: pointer;
140+
text-align: left;
141+
}
142+
.rs-value {
143+
overflow: hidden;
144+
text-overflow: ellipsis;
145+
white-space: nowrap;
146+
}
147+
.rs-arrow {
148+
color: #9aa2ad;
149+
font-size: 12px;
150+
flex: none;
151+
}
152+
.rs-backdrop {
153+
position: fixed;
154+
inset: 0;
155+
z-index: 200;
156+
background: transparent;
157+
}
158+
.rs-popup {
159+
position: fixed;
160+
z-index: 201;
161+
display: flex;
162+
flex-direction: column;
163+
padding: 4px;
164+
gap: 2px;
165+
background: #23272e;
166+
border: 1px solid #434a54;
167+
border-radius: 8px;
168+
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
169+
overflow-y: auto;
170+
}
171+
.rs-option {
172+
border: none;
173+
background: transparent;
174+
color: #ddd;
175+
padding: 8px 12px;
176+
border-radius: 5px;
177+
font-size: 14px;
178+
text-align: left;
179+
white-space: nowrap;
180+
cursor: pointer;
181+
}
182+
.rs-option:hover,
183+
.rs-option:focus {
184+
background: #2f6f9f;
185+
color: #fff;
186+
outline: none;
187+
}
188+
.rs-option[data-selected='true'] {
189+
color: #6db3e6;
190+
}
191+
.rs-option[data-selected='true']::before {
192+
content: '✓ ';
193+
}
194+
</style>

‎packages/player/src/components/RomStorePanel.vue‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type CachedRom,
2020
} from '../store/romLibrary'
2121
import { navEnabled, useRemoteNav } from '../composables/useRemoteNav'
22+
import RemoteSelect from './RemoteSelect.vue'
2223
2324
type StoreTab = 'downloaded' | 'online'
2425
@@ -82,6 +83,10 @@ function sortList<T>(
8283
8384
const cachedKeys = computed(() => new Set(cachedRoms.value.map((rom) => rom.key)))
8485
const genres = computed(() => listGenres(catalog.value.games))
86+
const genreOptions = computed(() => [
87+
{ value: '', label: '全部类型' },
88+
...genres.value.map((item) => ({ value: item, label: item })),
89+
])
8590
const matchedGames = computed(() =>
8691
searchRoms(catalog.value.games, query.value, genre.value, false, cachedKeys.value),
8792
)
@@ -399,15 +404,8 @@ function formatError(err: unknown): string {
399404

400405
<div class="store-tools">
401406
<input v-model="query" class="search" type="search" placeholder="搜索游戏、厂商、标签" />
402-
<select v-model="genre" class="select">
403-
<option value="">全部类型</option>
404-
<option v-for="item in genres" :key="item" :value="item">{{ item }}</option>
405-
</select>
406-
<select v-model="sort" class="select">
407-
<option v-for="item in SORT_OPTIONS" :key="item.value" :value="item.value">
408-
{{ item.label }}
409-
</option>
410-
</select>
407+
<RemoteSelect v-model="genre" class="select" :options="genreOptions" />
408+
<RemoteSelect v-model="sort" class="select" :options="SORT_OPTIONS" />
411409
</div>
412410

413411
<div class="store-summary">

‎packages/player/src/components/SettingsModal.vue‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
type TouchPadMode,
1919
} from '../emulator/settings'
2020
import { navEnabled, useRemoteNav } from '../composables/useRemoteNav'
21+
import RemoteSelect from './RemoteSelect.vue'
2122
2223
const open = defineModel<boolean>('open', { default: false })
2324
@@ -48,6 +49,14 @@ function gamepadName(g: { index: number; id: string }): string {
4849
return `#${g.index} ${name}`
4950
}
5051
52+
// RemoteSelect 需要 {value, label} 形式的选项。
53+
const gamepadOptions = computed(() => [
54+
{ value: null as number | null, label: '未绑定(仅键盘)' },
55+
...gamepads.value.map((g) => ({ value: g.index as number | null, label: gamepadName(g) })),
56+
])
57+
const speedOptions = SPEED_OPTIONS.map((s) => ({ value: s, label: `${s}×` }))
58+
const turboOptions = TURBO_HZ_OPTIONS.map((h) => ({ value: h, label: `${h} 次/秒` }))
59+
5160
const TABS: { key: Tab; label: string }[] = [
5261
{ key: 'keys', label: '按键' },
5362
{ key: 'display', label: '显示' },
@@ -251,12 +260,7 @@ onBeforeUnmount(() => {
251260
<!-- 手柄分配:玩家选择使用哪个实体手柄;手柄映射按型号共享 -->
252261
<label class="pad-assign">
253262
<span>使用手柄</span>
254-
<select v-model="currentPlayer.gamepadIndex">
255-
<option :value="null">未绑定(仅键盘)</option>
256-
<option v-for="g in gamepads" :key="g.index" :value="g.index">
257-
{{ gamepadName(g) }}
258-
</option>
259-
</select>
263+
<RemoteSelect v-model="currentPlayer.gamepadIndex" :options="gamepadOptions" />
260264
</label>
261265

262266
<p class="tip">
@@ -301,9 +305,7 @@ onBeforeUnmount(() => {
301305
</label>
302306
<label class="row">
303307
<span>宽高比</span>
304-
<select v-model="settings.display.aspect">
305-
<option v-for="a in ASPECTS" :key="a.value" :value="a.value">{{ a.label }}</option>
306-
</select>
308+
<RemoteSelect v-model="settings.display.aspect" :options="ASPECTS" />
307309
</label>
308310
<label class="row">
309311
<span>整数倍缩放</span>
@@ -331,21 +333,15 @@ onBeforeUnmount(() => {
331333
<div v-else class="form">
332334
<label class="row">
333335
<span>触屏手柄</span>
334-
<select v-model="settings.misc.touchPad">
335-
<option v-for="m in TOUCH_MODES" :key="m.value" :value="m.value">{{ m.label }}</option>
336-
</select>
336+
<RemoteSelect v-model="settings.misc.touchPad" :options="TOUCH_MODES" />
337337
</label>
338338
<label class="row">
339339
<span>运行速度</span>
340-
<select v-model.number="settings.misc.speed">
341-
<option v-for="s in SPEED_OPTIONS" :key="s" :value="s">{{ s }}×</option>
342-
</select>
340+
<RemoteSelect v-model="settings.misc.speed" :options="speedOptions" />
343341
</label>
344342
<label class="row">
345343
<span>连发频率</span>
346-
<select v-model.number="settings.misc.turboHz">
347-
<option v-for="h in TURBO_HZ_OPTIONS" :key="h" :value="h">{{ h }} 次/秒</option>
348-
</select>
344+
<RemoteSelect v-model="settings.misc.turboHz" :options="turboOptions" />
349345
</label>
350346
<label class="row">
351347
<span>显示按下的按键<br /><small class="hint-text">调试用,状态栏实时显示键盘/手柄/遥控按键</small></span>
@@ -449,7 +445,8 @@ onBeforeUnmount(() => {
449445
gap: 8px;
450446
padding: 8px 0;
451447
}
452-
.pad-assign select {
448+
.pad-assign select,
449+
.pad-assign .remote-select {
453450
min-width: 200px;
454451
max-width: 240px;
455452
}
@@ -500,6 +497,7 @@ onBeforeUnmount(() => {
500497
color: #ffb3aa;
501498
}
502499
.row select,
500+
.row .remote-select,
503501
.row input[type='range'] {
504502
min-width: 160px;
505503
}

‎packages/player/src/composables/useRemoteNav.ts‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,7 @@ function adjustControl(el: HTMLElement, dir: 'left' | 'right'): boolean {
156156
}
157157
return true
158158
}
159-
if (el instanceof HTMLSelectElement) {
160-
const n = el.options.length
161-
if (n > 0) {
162-
const next = Math.min(n - 1, Math.max(0, el.selectedIndex + delta))
163-
if (next !== el.selectedIndex) {
164-
el.selectedIndex = next
165-
el.dispatchEvent(new Event('change', { bubbles: true }))
166-
}
167-
}
168-
return true
169-
}
159+
// 下拉框不再用左右键调值:改由 RemoteSelect(确定弹出 → 上下选 → 确定切换)承载。
170160
return false
171161
}
172162

@@ -328,7 +318,7 @@ export function useRemoteNav(opts: RemoteNavOptions): RemoteNavApi {
328318
const container = opts.container.value
329319
if (!container) return
330320
const active = document.activeElement
331-
// 焦点在 select/range 上时,左右键交给控件调值。
321+
// 焦点在 range 滑块上时,左右键交给控件调值。
332322
if ((dir === 'left' || dir === 'right') && active instanceof HTMLElement) {
333323
if (container.contains(active) && adjustControl(active, dir)) return
334324
}

‎packages/player/src/store/romCatalog.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,23 @@ export async function loadRomCatalog(url = DEFAULT_CATALOG_URL): Promise<RomCata
4949
throw new Error('ROM 目录缺少必要字段')
5050
}
5151
}
52+
// 远程目录可能存在 sha256 相同的重复条目(同一 ROM 内容多次收录)。
53+
// sha256 同时是 romKey / 缓存键,重复会导致 v-for key 冲突且无法区分,
54+
// 故按 sha256 去重,保留首次出现的条目。
55+
catalog.games = dedupeBySha256(catalog.games)
5256
return catalog
5357
}
5458

59+
function dedupeBySha256(games: RomEntry[]): RomEntry[] {
60+
const seen = new Set<string>()
61+
return games.filter((game) => {
62+
const key = game.sha256!
63+
if (seen.has(key)) return false
64+
seen.add(key)
65+
return true
66+
})
67+
}
68+
5569
export function listGenres(games: RomEntry[]): string[] {
5670
return Array.from(new Set(games.map((game) => game.genre).filter(Boolean) as string[])).sort(
5771
(a, b) => a.localeCompare(b, 'zh-Hans-CN'),

0 commit comments

Comments
 (0)