Skip to content

Commit 0dca8bb

Browse files
committed
fix(important): F3 actions didn't work on mobile at all like chunks reload
1 parent de9bfba commit 0dca8bb

File tree

2 files changed

+70
-29
lines changed

2 files changed

+70
-29
lines changed

src/controls.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,11 @@ export const f3Keybinds: Array<{
818818
}
819819
]
820820

821+
export const reloadChunksAction = () => {
822+
const action = f3Keybinds.find(f3Keybind => f3Keybind.key === 'KeyA')
823+
void action!.action()
824+
}
825+
821826
document.addEventListener('keydown', (e) => {
822827
if (!isGameActive(false)) return
823828
if (contro.pressedKeys.has('F3')) {
@@ -987,14 +992,17 @@ export function updateBinds (commands: any) {
987992
}
988993

989994
export const onF3LongPress = async () => {
990-
const select = await showOptionsModal('', f3Keybinds.filter(f3Keybind => {
995+
const actions = f3Keybinds.filter(f3Keybind => {
991996
return f3Keybind.mobileTitle && (f3Keybind.enabled?.() ?? true)
992-
}).map(f3Keybind => {
997+
})
998+
const actionNames = actions.map(f3Keybind => {
993999
return `${f3Keybind.mobileTitle}${f3Keybind.key ? ` (F3+${f3Keybind.key})` : ''}`
994-
}))
1000+
})
1001+
const select = await showOptionsModal('', actionNames)
9951002
if (!select) return
996-
const f3Keybind = f3Keybinds.find(f3Keybind => f3Keybind.mobileTitle === select)
997-
if (f3Keybind) void f3Keybind.action()
1003+
const actionIndex = actionNames.indexOf(select)
1004+
const f3Keybind = actions[actionIndex]!
1005+
void f3Keybind.action()
9981006
}
9991007

10001008
export const handleMobileButtonCustomAction = (action: CustomAction) => {

src/react/OptionsItems.tsx

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { titleCase } from 'title-case'
44
import { useMemo } from 'react'
55
import { disabledSettings, options, qsOptions } from '../optionsStorage'
66
import { hideAllModals, miscUiState } from '../globalState'
7+
import { reloadChunksAction } from '../controls'
78
import Button from './Button'
89
import Slider from './Slider'
910
import Screen from './Screen'
1011
import { showOptionsModal } from './SelectOption'
1112
import PixelartIcon, { pixelartIcons } from './PixelartIcon'
13+
import { reconnectReload } from './AppStatusProvider'
1214

1315
type GeneralItem<T extends string | number | boolean> = {
1416
id?: string
@@ -18,7 +20,8 @@ type GeneralItem<T extends string | number | boolean> = {
1820
tooltip?: string
1921
// description?: string
2022
enableWarning?: string
21-
willHaveNoEffect?: boolean
23+
requiresRestart?: boolean
24+
requiresChunksReload?: boolean
2225
values?: Array<T | [T, string]>
2326
disableIf?: [option: keyof typeof options, value: any]
2427
}
@@ -56,7 +59,14 @@ const useCommonComponentsProps = (item: OptionMeta) => {
5659
}
5760
}
5861

59-
export const OptionButton = ({ item }: { item: Extract<OptionMeta, { type: 'toggle' }> }) => {
62+
const ignoreReloadWarningsCache = new Set<string>()
63+
64+
export const OptionButton = ({ item, onClick, valueText, cacheKey }: {
65+
item: Extract<OptionMeta, { type: 'toggle' }>,
66+
onClick?: () => void,
67+
valueText?: string,
68+
cacheKey?: string,
69+
}) => {
6070
const { disabledBecauseOfSetting } = useCommonComponentsProps(item)
6171

6272
const optionValue = useSnapshot(options)[item.id!]
@@ -84,40 +94,63 @@ export const OptionButton = ({ item }: { item: Extract<OptionMeta, { type: 'togg
8494

8595
return <Button
8696
data-setting={item.id}
87-
label={`${item.text}: ${valuesTitlesMap[optionValue]}`}
88-
// label={`${item.text}:`}
89-
// postLabel={valuesTitlesMap[optionValue]}
97+
label={`${translate(item.text)}: ${translate(valueText ?? valuesTitlesMap[optionValue])}`}
9098
onClick={async (event) => {
9199
if (disabledReason) {
92-
await showOptionsModal(`The option is unavailable. ${disabledReason}`, [])
100+
await showOptionsModal(`${translate('The option is not available')}: ${disabledReason}`, [])
93101
return
94102
}
95103
if (item.enableWarning && !options[item.id!]) {
96104
const result = await showOptionsModal(item.enableWarning, ['Enable'])
97105
if (!result) return
98106
}
99-
const { values } = item
100-
if (values) {
101-
const getOptionValue = (arrItem) => {
102-
if (typeof arrItem === 'string') {
103-
return arrItem
107+
onClick?.()
108+
if (item.id) {
109+
const { values } = item
110+
if (values) {
111+
const getOptionValue = (arrItem) => {
112+
if (typeof arrItem === 'string') {
113+
return arrItem
114+
} else {
115+
return arrItem[0]
116+
}
117+
}
118+
const currentIndex = values.findIndex((value) => {
119+
return getOptionValue(value) === optionValue
120+
})
121+
if (currentIndex === -1) {
122+
options[item.id] = getOptionValue(values[0])
104123
} else {
105-
return arrItem[0]
124+
const nextIndex = event.shiftKey
125+
? (currentIndex - 1 + values.length) % values.length
126+
: (currentIndex + 1) % values.length
127+
options[item.id] = getOptionValue(values[nextIndex])
106128
}
107-
}
108-
const currentIndex = values.findIndex((value) => {
109-
return getOptionValue(value) === optionValue
110-
})
111-
if (currentIndex === -1) {
112-
options[item.id!] = getOptionValue(values[0])
113129
} else {
114-
const nextIndex = event.shiftKey
115-
? (currentIndex - 1 + values.length) % values.length
116-
: (currentIndex + 1) % values.length
117-
options[item.id!] = getOptionValue(values[nextIndex])
130+
options[item.id] = !options[item.id]
131+
}
132+
}
133+
134+
const toCacheKey = cacheKey ?? item.id ?? ''
135+
if (toCacheKey && !ignoreReloadWarningsCache.has(toCacheKey)) {
136+
ignoreReloadWarningsCache.add(toCacheKey)
137+
138+
if (item.requiresRestart) {
139+
const result = await showOptionsModal(translate('The option requires a restart to take effect'), ['Restart', 'I will do it later'], {
140+
cancel: false,
141+
})
142+
if (result) {
143+
reconnectReload()
144+
}
145+
}
146+
if (item.requiresChunksReload) {
147+
const result = await showOptionsModal(translate('The option requires a chunks reload to take effect'), ['Reload', 'I will do it later'], {
148+
cancel: false,
149+
})
150+
if (result) {
151+
reloadChunksAction()
152+
}
118153
}
119-
} else {
120-
options[item.id!] = !options[item.id!]
121154
}
122155
}}
123156
title={disabledReason ? `${disabledReason} | ${item.tooltip}` : item.tooltip}

0 commit comments

Comments
 (0)