@@ -45,16 +45,17 @@ public static bool RaiseKeyDownEvent (Key key)
45
45
46
46
// Invoke any Application-scoped KeyBindings.
47
47
// The first view that handles the key will stop the loop.
48
- foreach ( KeyValuePair < Key , KeyBinding > binding in KeyBindings . Bindings . Where ( b => b . Key == key . KeyCode ) )
48
+ // foreach (KeyValuePair<Key, KeyBinding> binding in KeyBindings.GetBindings (key))
49
+ if ( KeyBindings . TryGet ( key , out KeyBinding binding ) )
49
50
{
50
- if ( binding . Value . BoundView is { } )
51
+ if ( binding . Target is { } )
51
52
{
52
- if ( ! binding . Value . BoundView . Enabled )
53
+ if ( ! binding . Target . Enabled )
53
54
{
54
55
return false ;
55
56
}
56
57
57
- bool ? handled = binding . Value . BoundView ? . InvokeCommands ( binding . Value . Commands , binding . Key , binding . Value ) ;
58
+ bool ? handled = binding . Target ? . InvokeCommands ( binding . Commands , binding ) ;
58
59
59
60
if ( handled != null && ( bool ) handled )
60
61
{
@@ -63,16 +64,17 @@ public static bool RaiseKeyDownEvent (Key key)
63
64
}
64
65
else
65
66
{
66
- if ( ! KeyBindings . TryGet ( key , KeyBindingScope . Application , out KeyBinding appBinding ) )
67
+ // BUGBUG: this seems unneeded.
68
+ if ( ! KeyBindings . TryGet ( key , out KeyBinding keybinding ) )
67
69
{
68
- continue ;
70
+ return false ;
69
71
}
70
72
71
73
bool ? toReturn = null ;
72
74
73
- foreach ( Command command in appBinding . Commands )
75
+ foreach ( Command command in keybinding . Commands )
74
76
{
75
- toReturn = InvokeCommand ( command , key , appBinding ) ;
77
+ toReturn = InvokeCommand ( command , key , keybinding ) ;
76
78
}
77
79
78
80
return toReturn ?? true ;
@@ -81,18 +83,18 @@ public static bool RaiseKeyDownEvent (Key key)
81
83
82
84
return false ;
83
85
84
- static bool ? InvokeCommand ( Command command , Key key , KeyBinding appBinding )
86
+ static bool ? InvokeCommand ( Command command , Key key , KeyBinding binding )
85
87
{
86
- if ( ! CommandImplementations ! . ContainsKey ( command ) )
88
+ if ( ! _commandImplementations ! . ContainsKey ( command ) )
87
89
{
88
90
throw new NotSupportedException (
89
91
@$ "A KeyBinding was set up for the command { command } ({ key } ) but that command is not supported by Application."
90
92
) ;
91
93
}
92
94
93
- if ( CommandImplementations . TryGetValue ( command , out View . CommandImplementation ? implementation ) )
95
+ if ( _commandImplementations . TryGetValue ( command , out View . CommandImplementation ? implementation ) )
94
96
{
95
- var context = new CommandContext ( command , key , appBinding ) ; // Create the context here
97
+ CommandContext < KeyBinding > context = new ( command , binding ) ; // Create the context here
96
98
97
99
return implementation ( context ) ;
98
100
}
@@ -116,7 +118,8 @@ public static bool RaiseKeyDownEvent (Key key)
116
118
public static event EventHandler < Key > ? KeyDown ;
117
119
118
120
/// <summary>
119
- /// Called when the user releases a key (by the <see cref="IConsoleDriver"/>). Raises the cancelable <see cref="KeyUp"/>
121
+ /// Called when the user releases a key (by the <see cref="IConsoleDriver"/>). Raises the cancelable
122
+ /// <see cref="KeyUp"/>
120
123
/// event
121
124
/// then calls <see cref="View.NewKeyUpEvent"/> on all top level views. Called after <see cref="RaiseKeyDownEvent"/>.
122
125
/// </summary>
@@ -155,14 +158,14 @@ public static bool RaiseKeyUpEvent (Key key)
155
158
156
159
#region Application-scoped KeyBindings
157
160
158
- static Application ( ) { AddApplicationKeyBindings ( ) ; }
161
+ static Application ( ) { AddKeyBindings ( ) ; }
159
162
160
163
/// <summary>Gets the Application-scoped key bindings.</summary>
161
- public static KeyBindings KeyBindings { get ; internal set ; } = new ( ) ;
164
+ public static KeyBindings KeyBindings { get ; internal set ; } = new ( null ) ;
162
165
163
- internal static void AddApplicationKeyBindings ( )
166
+ internal static void AddKeyBindings ( )
164
167
{
165
- CommandImplementations = new ( ) ;
168
+ _commandImplementations . Clear ( ) ;
166
169
167
170
// Things this view knows how to do
168
171
AddCommand (
@@ -231,83 +234,40 @@ internal static void AddApplicationKeyBindings ()
231
234
return false ;
232
235
} ) ;
233
236
234
- KeyBindings . Clear ( ) ;
235
-
236
237
// Resources/config.json overrides
238
+ QuitKey = Key . Esc ;
237
239
NextTabKey = Key . Tab ;
238
240
PrevTabKey = Key . Tab . WithShift ;
239
241
NextTabGroupKey = Key . F6 ;
240
242
PrevTabGroupKey = Key . F6 . WithShift ;
241
- QuitKey = Key . Esc ;
242
243
ArrangeKey = Key . F5 . WithCtrl ;
243
244
244
- KeyBindings . Add ( QuitKey , KeyBindingScope . Application , Command . Quit ) ;
245
-
246
- KeyBindings . Add ( Key . CursorRight , KeyBindingScope . Application , Command . NextTabStop ) ;
247
- KeyBindings . Add ( Key . CursorDown , KeyBindingScope . Application , Command . NextTabStop ) ;
248
- KeyBindings . Add ( Key . CursorLeft , KeyBindingScope . Application , Command . PreviousTabStop ) ;
249
- KeyBindings . Add ( Key . CursorUp , KeyBindingScope . Application , Command . PreviousTabStop ) ;
250
- KeyBindings . Add ( NextTabKey , KeyBindingScope . Application , Command . NextTabStop ) ;
251
- KeyBindings . Add ( PrevTabKey , KeyBindingScope . Application , Command . PreviousTabStop ) ;
245
+ // Need to clear after setting the above to ensure actually clear
246
+ // because set_QuitKey etc.. may call Add
247
+ KeyBindings . Clear ( ) ;
252
248
253
- KeyBindings . Add ( NextTabGroupKey , KeyBindingScope . Application , Command . NextTabGroup ) ;
254
- KeyBindings . Add ( PrevTabGroupKey , KeyBindingScope . Application , Command . PreviousTabGroup ) ;
249
+ KeyBindings . Add ( QuitKey , Command . Quit ) ;
250
+ KeyBindings . Add ( NextTabKey , Command . NextTabStop ) ;
251
+ KeyBindings . Add ( PrevTabKey , Command . PreviousTabStop ) ;
252
+ KeyBindings . Add ( NextTabGroupKey , Command . NextTabGroup ) ;
253
+ KeyBindings . Add ( PrevTabGroupKey , Command . PreviousTabGroup ) ;
254
+ KeyBindings . Add ( ArrangeKey , Command . Edit ) ;
255
255
256
- KeyBindings . Add ( ArrangeKey , KeyBindingScope . Application , Command . Edit ) ;
256
+ KeyBindings . Add ( Key . CursorRight , Command . NextTabStop ) ;
257
+ KeyBindings . Add ( Key . CursorDown , Command . NextTabStop ) ;
258
+ KeyBindings . Add ( Key . CursorLeft , Command . PreviousTabStop ) ;
259
+ KeyBindings . Add ( Key . CursorUp , Command . PreviousTabStop ) ;
257
260
258
261
// TODO: Refresh Key should be configurable
259
- KeyBindings . Add ( Key . F5 , KeyBindingScope . Application , Command . Refresh ) ;
262
+ KeyBindings . Add ( Key . F5 , Command . Refresh ) ;
260
263
261
264
// TODO: Suspend Key should be configurable
262
265
if ( Environment . OSVersion . Platform == PlatformID . Unix )
263
266
{
264
- KeyBindings . Add ( Key . Z . WithCtrl , KeyBindingScope . Application , Command . Suspend ) ;
265
- }
266
- }
267
-
268
- /// <summary>
269
- /// Gets the list of Views that have <see cref="KeyBindingScope.Application"/> key bindings.
270
- /// </summary>
271
- /// <remarks>
272
- /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
273
- /// </remarks>
274
- /// <returns>The list of Views that have Application-scoped key bindings.</returns>
275
- internal static List < KeyBinding > GetViewKeyBindings ( )
276
- {
277
- // Get the list of views that do not have Application-scoped key bindings
278
- return KeyBindings . Bindings
279
- . Where ( kv => kv . Value . Scope != KeyBindingScope . Application )
280
- . Select ( kv => kv . Value )
281
- . Distinct ( )
282
- . ToList ( ) ;
283
- }
284
-
285
- private static void ReplaceKey ( Key oldKey , Key newKey )
286
- {
287
- if ( KeyBindings . Bindings . Count == 0 )
288
- {
289
- return ;
290
- }
291
-
292
- if ( newKey == Key . Empty )
293
- {
294
- KeyBindings . Remove ( oldKey ) ;
295
- }
296
- else
297
- {
298
- if ( KeyBindings . TryGet ( oldKey , out KeyBinding binding ) )
299
- {
300
- KeyBindings . Remove ( oldKey ) ;
301
- KeyBindings . Add ( newKey , binding ) ;
302
- }
303
- else
304
- {
305
- KeyBindings . Add ( newKey , binding ) ;
306
- }
267
+ KeyBindings . Add ( Key . Z . WithCtrl , Command . Suspend ) ;
307
268
}
308
269
}
309
270
310
-
311
271
#endregion Application-scoped KeyBindings
312
272
313
273
/// <summary>
@@ -321,16 +281,15 @@ private static void ReplaceKey (Key oldKey, Key newKey)
321
281
/// </summary>
322
282
/// <remarks>
323
283
/// <para>
324
- /// This version of AddCommand is for commands that do not require a <see cref="CommandContext "/>.
284
+ /// This version of AddCommand is for commands that do not require a <see cref="ICommandContext "/>.
325
285
/// </para>
326
286
/// </remarks>
327
287
/// <param name="command">The command.</param>
328
288
/// <param name="f">The function.</param>
329
- private static void AddCommand ( Command command , Func < bool ? > f ) { CommandImplementations ! [ command ] = ctx => f ( ) ; }
289
+ private static void AddCommand ( Command command , Func < bool ? > f ) { _commandImplementations ! [ command ] = ctx => f ( ) ; }
330
290
331
291
/// <summary>
332
292
/// Commands for Application.
333
293
/// </summary>
334
- private static Dictionary < Command , View . CommandImplementation > ? CommandImplementations { get ; set ; }
335
-
294
+ private static readonly Dictionary < Command , View . CommandImplementation > _commandImplementations = new ( ) ;
336
295
}
0 commit comments