@@ -14,8 +14,8 @@ public class ActionsViewModel : ObservableObject
14
14
15
15
// Properties
16
16
17
- public ObservableCollection < ModifiableCommandHotKeyItem > KeyboardShortcuts { get ; } = [ ] ;
18
- public ObservableCollection < ModifiableCommandHotKeyItem > ExcludedKeyboardShortcuts { get ; } = [ ] ;
17
+ public ObservableCollection < ModifiableCommandHotKeyItem > ValidKeyboardShortcuts { get ; } = [ ] ;
18
+ public ObservableCollection < ModifiableCommandHotKeyItem > AllKeyboardShortcuts { get ; } = [ ] ;
19
19
20
20
private bool _IsResetAllConfirmationTeachingTipOpened ;
21
21
public bool IsResetAllConfirmationTeachingTipOpened
@@ -51,29 +51,29 @@ public ModifiableCommandHotKeyItem? SelectedNewShortcutItem
51
51
public ICommand ShowResetAllConfirmationCommand { get ; set ; }
52
52
public ICommand ShowAddNewShortcutGridCommand { get ; set ; }
53
53
public ICommand HideAddNewShortcutGridCommand { get ; set ; }
54
- public ICommand AddNewShortcutGridCommand { get ; set ; }
54
+ public ICommand AddNewShortcutCommand { get ; set ; }
55
55
public ICommand ResetAllCommand { get ; set ; }
56
56
57
57
// Constructor
58
58
59
59
public ActionsViewModel ( )
60
60
{
61
- LoadCommandsCommand = new AsyncRelayCommand ( LoadCommands ) ;
61
+ LoadCommandsCommand = new AsyncRelayCommand ( ExecuteLoadCommandsCommand ) ;
62
62
ShowResetAllConfirmationCommand = new RelayCommand ( ExecuteShowResetAllConfirmationCommand ) ;
63
63
ShowAddNewShortcutGridCommand = new RelayCommand ( ExecuteShowAddNewShortcutGridCommand ) ;
64
64
HideAddNewShortcutGridCommand = new RelayCommand ( ExecuteHideAddNewShortcutGridCommand ) ;
65
- AddNewShortcutGridCommand = new RelayCommand ( ExecuteAddNewShortcutGridCommand ) ;
65
+ AddNewShortcutCommand = new RelayCommand ( ExecuteAddNewShortcutCommand ) ;
66
66
ResetAllCommand = new RelayCommand ( ExecuteResetAllCommand ) ;
67
67
}
68
68
69
69
// Command methods
70
70
71
- private async Task LoadCommands ( )
71
+ private async Task ExecuteLoadCommandsCommand ( )
72
72
{
73
73
await MainWindow . Instance . DispatcherQueue . EnqueueOrInvokeAsync ( ( ) =>
74
74
{
75
- KeyboardShortcuts . Clear ( ) ;
76
- ExcludedKeyboardShortcuts . Clear ( ) ;
75
+ ValidKeyboardShortcuts . Clear ( ) ;
76
+ AllKeyboardShortcuts . Clear ( ) ;
77
77
78
78
foreach ( var command in Commands )
79
79
{
@@ -82,25 +82,20 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
82
82
if ( command is NoneCommand )
83
83
continue ;
84
84
85
- if ( command . HotKeys . IsEmpty )
85
+ AllKeyboardShortcuts . Add ( new ( )
86
86
{
87
- ExcludedKeyboardShortcuts . Add ( new ( )
88
- {
89
- CommandCode = command . Code ,
90
- Label = command . Label ,
91
- Description = command . Description ,
92
- HotKey = new ( ) ,
93
- } ) ;
94
-
95
- continue ;
96
- }
87
+ CommandCode = command . Code ,
88
+ Label = command . Label ,
89
+ Description = command . Description ,
90
+ HotKey = new ( ) ,
91
+ } ) ;
97
92
98
93
foreach ( var hotkey in command . HotKeys )
99
94
{
100
95
if ( ! hotkey . IsVisible )
101
96
continue ;
102
97
103
- KeyboardShortcuts . Add ( new ( )
98
+ ValidKeyboardShortcuts . Add ( new ( )
104
99
{
105
100
CommandCode = command . Code ,
106
101
Label = command . Label ,
@@ -125,7 +120,7 @@ private void ExecuteShowAddNewShortcutGridCommand()
125
120
ShowAddNewShortcutGrid = true ;
126
121
127
122
// Reset edit mode for each item
128
- foreach ( var hotkey in KeyboardShortcuts )
123
+ foreach ( var hotkey in ValidKeyboardShortcuts )
129
124
{
130
125
hotkey . IsEditMode = false ;
131
126
hotkey . HotKeyText = hotkey . HotKey . LocalizedLabel ;
@@ -143,28 +138,57 @@ private void ExecuteHideAddNewShortcutGridCommand()
143
138
SelectedNewShortcutItem = null ;
144
139
}
145
140
146
- private void ExecuteAddNewShortcutGridCommand ( )
141
+ private void ExecuteAddNewShortcutCommand ( )
147
142
{
148
143
if ( SelectedNewShortcutItem is null )
149
144
return ;
150
145
146
+ // Check if this hot key is already taken
147
+ foreach ( var hotkey in ValidKeyboardShortcuts )
148
+ {
149
+ if ( SelectedNewShortcutItem . HotKeyText == hotkey . PreviousHotKey )
150
+ {
151
+ IsAlreadyUsedTeachingTipOpened = true ;
152
+ return ;
153
+ }
154
+ }
155
+
151
156
var actions =
152
157
GeneralSettingsService . Actions is not null
153
158
? new Dictionary < string , string > ( GeneralSettingsService . Actions )
154
159
: [ ] ;
155
160
156
- // Remove existing setting
157
- foreach ( var action in actions )
161
+ // Get raw string keys stored in the user setting
162
+ var storedKeys = actions . GetValueOrDefault ( SelectedNewShortcutItem . CommandCode . ToString ( ) ) ;
163
+
164
+ // Initialize
165
+ var newHotKey = HotKey . Parse ( SelectedNewShortcutItem . HotKeyText ) ;
166
+ var modifiedCollection = HotKeyCollection . Empty ;
167
+
168
+ // The first time to customize
169
+ if ( string . IsNullOrEmpty ( storedKeys ) )
170
+ {
171
+ // Replace with new one
172
+ var modifiableDefaultCollection = SelectedNewShortcutItem . DefaultHotKeyCollection . ToList ( ) ;
173
+ modifiableDefaultCollection . RemoveAll ( x => x . RawLabel == SelectedNewShortcutItem . PreviousHotKey . RawLabel ) ;
174
+ modifiableDefaultCollection . Add ( newHotKey ) ;
175
+ modifiedCollection = new HotKeyCollection ( modifiableDefaultCollection ) ;
176
+ }
177
+ // Stored in the user setting
178
+ else
158
179
{
159
- if ( Enum . TryParse ( action . Key , true , out CommandCodes code ) && code == SelectedNewShortcutItem . CommandCode )
160
- actions . Remove ( action . Key ) ;
180
+ // Replace with new one
181
+ var modifiableCollection = HotKeyCollection . Parse ( storedKeys ) . ToList ( ) ;
182
+ modifiableCollection . RemoveAll ( x => x . RawLabel == SelectedNewShortcutItem . PreviousHotKey . RawLabel || x . RawLabel == $ "!{ SelectedNewShortcutItem . PreviousHotKey . RawLabel } ") ;
183
+ modifiableCollection . Add ( newHotKey ) ;
184
+ modifiedCollection = new HotKeyCollection ( modifiableCollection ) ;
161
185
}
162
186
163
- // Create a new one
164
- actions . Add ( SelectedNewShortcutItem . CommandCode . ToString ( ) , SelectedNewShortcutItem . HotKeyText ) ;
187
+ // Remove previous one and add new one
188
+ actions . Remove ( SelectedNewShortcutItem . CommandCode . ToString ( ) ) ;
189
+ actions . Add ( SelectedNewShortcutItem . CommandCode . ToString ( ) , modifiedCollection . RawLabel ) ;
165
190
166
- // Set
167
- SelectedNewShortcutItem . HotKey = HotKey . Parse ( SelectedNewShortcutItem . HotKeyText ) ;
191
+ // Store
168
192
GeneralSettingsService . Actions = actions ;
169
193
170
194
// Create a clone
@@ -173,7 +197,7 @@ GeneralSettingsService.Actions is not null
173
197
CommandCode = SelectedNewShortcutItem . CommandCode ,
174
198
Label = SelectedNewShortcutItem . Label ,
175
199
Description = SelectedNewShortcutItem . Description ,
176
- HotKey = HotKey . Parse ( SelectedNewShortcutItem . HotKey . RawLabel ) ,
200
+ HotKey = HotKey . Parse ( SelectedNewShortcutItem . HotKeyText ) ,
177
201
DefaultHotKeyCollection = new ( SelectedNewShortcutItem . DefaultHotKeyCollection ) ,
178
202
PreviousHotKey = HotKey . Parse ( SelectedNewShortcutItem . PreviousHotKey . RawLabel ) ,
179
203
} ;
@@ -186,15 +210,15 @@ GeneralSettingsService.Actions is not null
186
210
SelectedNewShortcutItem = null ;
187
211
188
212
// Add to existing list
189
- KeyboardShortcuts . Insert ( 0 , selectedNewItem ) ;
213
+ ValidKeyboardShortcuts . Insert ( 0 , selectedNewItem ) ;
190
214
}
191
215
192
216
private void ExecuteResetAllCommand ( )
193
217
{
194
218
GeneralSettingsService . Actions = null ;
195
219
IsResetAllConfirmationTeachingTipOpened = false ;
196
220
197
- _ = LoadCommands ( ) ;
221
+ _ = ExecuteLoadCommandsCommand ( ) ;
198
222
}
199
223
}
200
224
}
0 commit comments