@@ -50,12 +50,13 @@ internal object ShortcutKeySettings : SettingTab {
5050 commandManager.customCommands.forEach { (key, commandName) ->
5151 commands[commandManager.commandsMap[commandName]!! ] = key
5252 }
53+ val refreshState = remember { mutableStateOf(0 ) }
5354 SettingsSection (" 快捷键设置" ) {
55+ val refresh = refreshState.value
5456 commands.forEach { (command, key) ->
5557 SettingsCard (command.displayName){
56- var curKey by remember { mutableStateOf(key) }
5758 val keyCompose = remember { key.split(" " ).map { Key (it.toLong()) }.toMutableStateSet() }
58- val keyDown = remember { mutableStateSetOf<Key >() }
59+ val pressedKeySet = remember { mutableStateSetOf<Key >() }
5960 val preKeyCompose = remember { mutableStateListOf<Key >() }
6061
6162 fun cancel () {
@@ -66,32 +67,28 @@ internal object ShortcutKeySettings : SettingTab {
6667 }
6768 }
6869
69- fun done () {
70+ fun done (keyStr : String ) {
7071 selectKey = " "
71- val keyStr = keyCompose.sortedKeys().joinToString(separator = " " ) {
72- it.keyCode.toString()
73- }
74- if (keyStr.isEmpty() || keyStr in commandManager.commands || keyStr in commandManager.customCommands) {
75- cancel()
76- return
77- }
78- if (curKey !in commandManager.commands && curKey !in commandManager.customCommands) {
72+ if (keyStr.isEmpty()) {
7973 cancel()
8074 return
8175 }
82- val commandTar : String
83- if (curKey in commandManager.customCommands) {
84- commandTar = commandManager.customCommands[curKey] !!
85- commandManager.customCommands - = curKey
76+ if (keyStr in commandManager.commands && commandManager.commands[keyStr] !! .name == command.name) {
77+ if (command.name in commandManager.customCommands.values ) {
78+ commandManager.customCommands - = commandManager.customCommands.filterValues { it == command.name }.keys
79+ }
8680 } else {
87- commandTar = commandManager.commands[curKey] !! .name
81+ commandManager.customCommands[keyStr] = command .name
8882 }
89- commandManager.customCommands[keyStr] = commandTar
90- curKey = keyStr
83+ commands[command] = keyStr
9184 commandManager.saveCustomShortcutKeys()
85+ refreshState.value = refreshState.value xor 1
9286 }
9387
9488 Box {
89+ val sortedKeyString = keyCompose.sortedKeys().joinToString(separator = " " ) {
90+ it.keyCode.toString()
91+ }
9592 CustomOutlinedTextField (
9693 keyCompose.sortedKeys().joinToString(separator = " +" ) {
9794 var cur = it
@@ -101,35 +98,34 @@ internal object ShortcutKeySettings : SettingTab {
10198 }
10299 KeyEvent .getKeyText(cur.nativeKeyCode)
103100 },
104- {
105- EchoInMirror .currentPosition.bpm = it.toDoubleOrNull()?.coerceIn(1.0 , 600.0 ) ? : return @CustomOutlinedTextField
106- },
101+ { },
107102 Modifier .width(256 .dp).height(36 .dp),
108103 singleLine = true ,
109104 textStyle = MaterialTheme .typography.labelLarge.copy(LocalContentColor .current),
110105 colors = TextFieldDefaults .colors(
111106 unfocusedIndicatorColor = MaterialTheme .colorScheme.outlineVariant,
112107 focusedIndicatorColor = MaterialTheme .colorScheme.outlineVariant,
113108 ),
109+ isError = commands.count { it.value == key } > 1 ,
114110 keyboardOptions = KeyboardOptions .Default .copy(keyboardType = KeyboardType .Number ),
115111 paddingValues = TextFieldDefaults .contentPaddingWithLabel(6 .dp, 6 .dp, 3 .dp, 4 .dp)
116112 )
117113 Box (modifier = Modifier .matchParentSize()
118114 .onFocusChanged {
119- if (! it.isFocused && selectKey == curKey ) done()
115+ if (! it.isFocused && selectKey == key ) done(sortedKeyString )
120116 }
121117 .clickableWithIcon {
122- if (selectKey == curKey ) {
123- done()
118+ if (selectKey == key ) {
119+ done(sortedKeyString )
124120 } else {
125- selectKey = curKey
121+ selectKey = key
126122 preKeyCompose.clear()
127- keyDown .clear()
123+ pressedKeySet .clear()
128124 preKeyCompose.addAll(keyCompose)
129125 keyCompose.clear()
130126 }
131127 }.onKeyEvent {
132- if (selectKey != curKey ) return @onKeyEvent false
128+ if (selectKey != key ) return @onKeyEvent false
133129 var pressKey = it.key
134130 when (pressKey) {
135131 Key .MetaLeft , Key .MetaRight -> if (SystemUtils .IS_OS_MAC ) pressKey = Key .CtrlLeft
@@ -143,18 +139,18 @@ internal object ShortcutKeySettings : SettingTab {
143139 Key .Escape -> cancel()
144140 Key .Enter -> return @onKeyEvent false
145141 else -> {
146- keyDown .add(pressKey)
142+ pressedKeySet .add(pressKey)
147143 keyCompose.add(pressKey)
148144 }
149145 }
150146 } else if (it.type == KeyEventType .KeyUp ) {
151- keyDown .remove(pressKey)
152- if (keyDown .isEmpty()) done()
147+ pressedKeySet .remove(pressKey)
148+ if (pressedKeySet .isEmpty()) done(sortedKeyString )
153149 }
154150 true
155- })
151+ }
152+ )
156153 }
157-
158154 }
159155 }
160156 }
0 commit comments