1
1
package com.eimsound.daw.window.dialogs.settings
2
2
3
+ import androidx.compose.foundation.layout.Box
3
4
import androidx.compose.foundation.layout.height
4
5
import androidx.compose.foundation.layout.width
5
6
import androidx.compose.foundation.text.KeyboardOptions
@@ -50,12 +51,13 @@ internal object ShortcutKeySettings : SettingTab {
50
51
commandManager.customCommands.forEach { (key, commandName) ->
51
52
commands[commandManager.commandsMap[commandName]!! ] = key
52
53
}
54
+ val refreshState = remember { mutableStateOf(0 ) }
53
55
SettingsSection (langs.shortcutKey) {
56
+ val refresh = refreshState.value
54
57
commands.forEach { (command, key) ->
55
58
SettingsCard (command.displayName){
56
- var curKey by remember { mutableStateOf(key) }
57
59
val keyCompose = remember { key.split(" " ).map { Key (it.toLong()) }.toMutableStateSet() }
58
- val keyDown = remember { mutableStateSetOf<Key >() }
60
+ val pressedKeySet = remember { mutableStateSetOf<Key >() }
59
61
val preKeyCompose = remember { mutableStateListOf<Key >() }
60
62
61
63
fun cancel () {
@@ -66,59 +68,65 @@ internal object ShortcutKeySettings : SettingTab {
66
68
}
67
69
}
68
70
69
- fun done () {
71
+ fun done (keyStr : String ) {
70
72
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) {
73
+ if (keyStr.isEmpty()) {
79
74
cancel()
80
75
return
81
76
}
82
- val commandTar : String
83
- if (curKey in commandManager.customCommands) {
84
- commandTar = commandManager.customCommands[curKey] !!
85
- commandManager.customCommands - = curKey
77
+ if (keyStr in commandManager.commands && commandManager.commands[keyStr] !! .name == command.name) {
78
+ if (command.name in commandManager.customCommands.values ) {
79
+ commandManager.customCommands - = commandManager.customCommands.filterValues { it == command.name }.keys
80
+ }
86
81
} else {
87
- commandTar = commandManager.commands[curKey] !! .name
82
+ commandManager.customCommands[keyStr] = command .name
88
83
}
89
- commandManager.customCommands[keyStr] = commandTar
90
- curKey = keyStr
84
+ commands[command] = keyStr
91
85
commandManager.saveCustomShortcutKeys()
86
+ refreshState.value = refreshState.value xor 1
92
87
}
93
88
94
- CustomOutlinedTextField (
95
- keyCompose.sortedKeys().joinToString(separator = " +" ) {
96
- var cur = it
97
- when (it) {
98
- Key .MetaLeft , Key .MetaRight -> if (SystemUtils .IS_OS_MAC ) cur = Key .CtrlLeft
99
- Key .CtrlLeft , Key .CtrlRight -> if (SystemUtils .IS_OS_MAC ) cur = Key .MetaLeft
89
+ Box {
90
+ val sortedKeyString = keyCompose.sortedKeys().joinToString(separator = " " ) {
91
+ it.keyCode.toString()
100
92
}
101
- KeyEvent .getKeyText(cur.nativeKeyCode)
102
- },
103
- {
104
- EchoInMirror .currentPosition.bpm = it.toDoubleOrNull()?.coerceIn(1.0 , 600.0 ) ? : return @CustomOutlinedTextField
105
- },
106
- Modifier .width(256 .dp).height(36 .dp)
93
+ CustomOutlinedTextField (
94
+ keyCompose.sortedKeys().joinToString(separator = " +" ) {
95
+ var cur = it
96
+ when (it) {
97
+ Key .MetaLeft , Key .MetaRight -> if (SystemUtils .IS_OS_MAC ) cur = Key .CtrlLeft
98
+ Key .CtrlLeft , Key .CtrlRight -> if (SystemUtils .IS_OS_MAC ) cur = Key .MetaLeft
99
+ }
100
+ KeyEvent .getKeyText(cur.nativeKeyCode)
101
+ },
102
+ { },
103
+ Modifier .width(256 .dp).height(36 .dp),
104
+ singleLine = true ,
105
+ textStyle = MaterialTheme .typography.labelLarge.copy(LocalContentColor .current),
106
+ colors = TextFieldDefaults .colors(
107
+ unfocusedIndicatorColor = MaterialTheme .colorScheme.outlineVariant,
108
+ focusedIndicatorColor = MaterialTheme .colorScheme.outlineVariant,
109
+ ),
110
+ isError = commands.count { it.value == key } > 1 ,
111
+ keyboardOptions = KeyboardOptions .Default .copy(keyboardType = KeyboardType .Number ),
112
+ paddingValues = TextFieldDefaults .contentPaddingWithLabel(6 .dp, 6 .dp, 3 .dp, 4 .dp)
113
+ )
114
+ Box (modifier = Modifier .matchParentSize()
107
115
.onFocusChanged {
108
- if (! it.isFocused && selectKey == curKey ) done()
116
+ if (! it.isFocused && selectKey == key ) done(sortedKeyString )
109
117
}
110
118
.clickableWithIcon {
111
- if (selectKey == curKey ) {
112
- done()
119
+ if (selectKey == key ) {
120
+ done(sortedKeyString )
113
121
} else {
114
- selectKey = curKey
122
+ selectKey = key
115
123
preKeyCompose.clear()
116
- keyDown .clear()
124
+ pressedKeySet .clear()
117
125
preKeyCompose.addAll(keyCompose)
118
126
keyCompose.clear()
119
127
}
120
128
}.onKeyEvent {
121
- if (selectKey != curKey ) return @onKeyEvent false
129
+ if (selectKey != key ) return @onKeyEvent false
122
130
var pressKey = it.key
123
131
when (pressKey) {
124
132
Key .MetaLeft , Key .MetaRight -> if (SystemUtils .IS_OS_MAC ) pressKey = Key .CtrlLeft
@@ -132,25 +140,18 @@ internal object ShortcutKeySettings : SettingTab {
132
140
Key .Escape -> cancel()
133
141
Key .Enter -> return @onKeyEvent false
134
142
else -> {
135
- keyDown .add(pressKey)
143
+ pressedKeySet .add(pressKey)
136
144
keyCompose.add(pressKey)
137
145
}
138
146
}
139
147
} else if (it.type == KeyEventType .KeyUp ) {
140
- keyDown .remove(pressKey)
141
- if (keyDown .isEmpty()) done()
148
+ pressedKeySet .remove(pressKey)
149
+ if (pressedKeySet .isEmpty()) done(sortedKeyString )
142
150
}
143
151
true
144
- },
145
- singleLine = true ,
146
- textStyle = MaterialTheme .typography.labelLarge.copy(LocalContentColor .current),
147
- colors = TextFieldDefaults .colors(
148
- unfocusedIndicatorColor = MaterialTheme .colorScheme.outlineVariant,
149
- focusedIndicatorColor = MaterialTheme .colorScheme.outlineVariant,
150
- ),
151
- keyboardOptions = KeyboardOptions .Default .copy(keyboardType = KeyboardType .Number ),
152
- paddingValues = TextFieldDefaults .contentPaddingWithLabel(6 .dp, 6 .dp, 3 .dp, 4 .dp)
153
- )
152
+ }
153
+ )
154
+ }
154
155
}
155
156
}
156
157
}
0 commit comments