4
4
using System . Globalization ;
5
5
using System . Linq ;
6
6
using System . Runtime . InteropServices . ComTypes ;
7
- using System . Threading . Tasks ;
8
7
using System . Windows . Forms ;
9
8
using System . Windows . Input ;
10
9
using Microsoft . Vbe . Interop ;
18
17
using Rubberduck . UI . Command . MenuItems ;
19
18
using Infralution . Localization . Wpf ;
20
19
using Rubberduck . Common . Dispatch ;
21
- using Rubberduck . Common . Hotkeys ;
22
- using Rubberduck . UI . Command ;
23
- using Hotkey = Rubberduck . Common . Hotkeys . Hotkey ;
24
20
25
21
namespace Rubberduck
26
22
{
@@ -35,7 +31,6 @@ public class App : IDisposable
35
31
private readonly RubberduckCommandBar _stateBar ;
36
32
private readonly IIndenter _indenter ;
37
33
private readonly IRubberduckHooks _hooks ;
38
- private readonly IEnumerable < ICommand > _appCommands ;
39
34
40
35
private readonly Logger _logger ;
41
36
@@ -47,19 +42,13 @@ public class App : IDisposable
47
42
private readonly IDictionary < VBComponents , Tuple < IConnectionPoint , int > > _componentsEventsConnectionPoints =
48
43
new Dictionary < VBComponents , Tuple < IConnectionPoint , int > > ( ) ;
49
44
50
- private IReadOnlyDictionary < string , RubberduckHotkey > _hotkeyNameMap ;
51
-
52
- private IReadOnlyDictionary < RubberduckHotkey , ICommand > _hotkeyActions ;
53
- private IReadOnlyDictionary < string , ICommand > _secondKeyActions ;
54
-
55
45
public App ( VBE vbe , IMessageBox messageBox ,
56
46
IRubberduckParser parser ,
57
47
IGeneralConfigService configService ,
58
48
IAppMenu appMenus ,
59
49
RubberduckCommandBar stateBar ,
60
50
IIndenter indenter ,
61
- IRubberduckHooks hooks ,
62
- IEnumerable < ICommand > appCommands )
51
+ IRubberduckHooks hooks )
63
52
{
64
53
_vbe = vbe ;
65
54
_messageBox = messageBox ;
@@ -70,10 +59,8 @@ public App(VBE vbe, IMessageBox messageBox,
70
59
_stateBar = stateBar ;
71
60
_indenter = indenter ;
72
61
_hooks = hooks ;
73
- _appCommands = appCommands ;
74
62
_logger = LogManager . GetCurrentClassLogger ( ) ;
75
63
76
- _hooks . MessageReceived += hooks_MessageReceived ;
77
64
_configService . LanguageChanged += ConfigServiceLanguageChanged ;
78
65
_parser . State . StateChanged += Parser_StateChanged ;
79
66
_stateBar . Refresh += _stateBar_Refresh ;
@@ -100,11 +87,11 @@ public void Startup()
100
87
_appMenus . Initialize ( ) ;
101
88
_appMenus . Localize ( ) ;
102
89
103
- //_hooks.AddHook(new LowLevelKeyboardHook(_vbe));
104
- HookHotkeys ( ) ;
90
+ _hooks . HookHotkeys ( ) ;
105
91
_hooks . Attach ( ) ;
106
92
}
107
93
94
+ #region sink handlers. todo: move to another class
108
95
async void sink_ProjectRemoved ( object sender , DispatcherEventArgs < VBProject > e )
109
96
{
110
97
Debug . WriteLine ( string . Format ( "Project '{0}' was removed." , e . Item . Name ) ) ;
@@ -239,90 +226,7 @@ async void sink_ProjectActivated(object sender, DispatcherEventArgs<VBProject> e
239
226
Debug . WriteLine ( string . Format ( "Project '{0}' was activated." , e . Item . Name ) ) ;
240
227
// do something?
241
228
}
242
-
243
- private Keys _firstStepHotKey ;
244
- private bool _isAwaitingTwoStepKey ;
245
- private bool _skipKeyUp ;
246
-
247
- private void hooks_MessageReceived ( object sender , HookEventArgs e )
248
- {
249
- if ( sender is LowLevelKeyboardHook )
250
- {
251
- HandleLowLevelKeyhook ( e ) ;
252
- return ;
253
- }
254
-
255
- var hotKey = sender as IHotkey ;
256
- if ( hotKey != null )
257
- {
258
- HandleHotkey ( hotKey ) ;
259
- }
260
- else
261
- {
262
- AwaitNextKey ( ) ;
263
- }
264
- }
265
-
266
- private void HandleHotkey ( IHotkey hotkey )
267
- {
268
- if ( hotkey . IsTwoStepHotkey )
269
- {
270
- _firstStepHotKey = hotkey . HotkeyInfo . Keys ;
271
- AwaitNextKey ( true , hotkey . HotkeyInfo ) ;
272
- }
273
- else
274
- {
275
- _firstStepHotKey = Keys . None ;
276
- _hotkeyActions [ _hotkeyNameMap [ hotkey . Key ] ] . Execute ( null ) ;
277
- AwaitNextKey ( ) ;
278
- }
279
- }
280
-
281
- private void HandleLowLevelKeyhook ( HookEventArgs e )
282
- {
283
- if ( _skipKeyUp )
284
- {
285
- _skipKeyUp = false ;
286
- return ;
287
- }
288
-
289
- if ( _isAwaitingTwoStepKey )
290
- {
291
- // todo: use _firstStepHotKey and e.Key to run 2-step hotkey action
292
- if ( _firstStepHotKey == Keys . I && e . Key == Keys . M )
293
- {
294
- _indenter . IndentCurrentModule ( ) ;
295
- }
296
-
297
- AwaitNextKey ( ) ;
298
- return ;
299
- }
300
-
301
- var component = _vbe . ActiveCodePane . CodeModule . Parent ;
302
- _parser . ParseComponent ( component ) ;
303
-
304
- AwaitNextKey ( ) ;
305
- return ;
306
- }
307
-
308
- private void AwaitNextKey ( bool eatNextKey = false , HotkeyInfo info = default ( HotkeyInfo ) )
309
- {
310
- _isAwaitingTwoStepKey = eatNextKey ;
311
- foreach ( var hook in _hooks . Hooks . OfType < ILowLevelKeyboardHook > ( ) )
312
- {
313
- hook . EatNextKey = eatNextKey ;
314
- }
315
-
316
- _skipKeyUp = eatNextKey ;
317
- if ( eatNextKey )
318
- {
319
- _stateBar . SetStatusText ( "(" + info + ") was pressed. Waiting for second key..." ) ;
320
- }
321
- else
322
- {
323
- _stateBar . SetStatusText ( _parser . State . Status . ToString ( ) ) ;
324
- }
325
- }
229
+ #endregion
326
230
327
231
private void _stateBar_Refresh ( object sender , EventArgs e )
328
232
{
@@ -335,42 +239,9 @@ private void Parser_StateChanged(object sender, EventArgs e)
335
239
_appMenus . EvaluateCanExecute ( _parser . State ) ;
336
240
}
337
241
338
- private void HookHotkeys ( )
339
- {
340
- var settings = _config . UserSettings . GeneralSettings . HotkeySettings ;
341
- foreach ( var hotkey in settings . Where ( hotkey => hotkey . IsEnabled ) )
342
- {
343
- _hooks . AddHook ( new Hotkey ( ( IntPtr ) _vbe . MainWindow . HWnd , hotkey . ToString ( ) ) ) ;
344
- }
345
- }
346
-
347
242
private void CleanReloadConfig ( )
348
243
{
349
244
LoadConfig ( ) ;
350
- var hotkeys = _config . UserSettings . GeneralSettings . HotkeySettings
351
- . Where ( hotkey => hotkey . IsEnabled ) . ToList ( ) ;
352
-
353
- _hotkeyNameMap = hotkeys
354
- . ToDictionary (
355
- hotkey => hotkey . ToString ( ) ,
356
- hotkey => ( RubberduckHotkey ) Enum . Parse ( typeof ( RubberduckHotkey ) , hotkey . Name ) ) ;
357
-
358
- _hotkeyActions = ( from hotkey in hotkeys
359
- let value = ( RubberduckHotkey ) Enum . Parse ( typeof ( RubberduckHotkey ) , hotkey . Name )
360
- where string . IsNullOrEmpty ( hotkey . Key2 )
361
- select new
362
- {
363
- Hotkey = value ,
364
- Command = _appCommands . OfType < IHotkeyCommand > ( )
365
- . SingleOrDefault ( command => command . Hotkey == ( RubberduckHotkey ) Enum . Parse ( typeof ( RubberduckHotkey ) , hotkey . Name ) )
366
- } )
367
- . ToDictionary ( kvp => kvp . Hotkey , kvp => ( ICommand ) kvp . Command ) ;
368
-
369
- _secondKeyActions = hotkeys
370
- . Where ( hotkey => ! string . IsNullOrEmpty ( hotkey . Key2 ) )
371
- . ToDictionary (
372
- hotkey => hotkey . Key2 ,
373
- hotkey => hotkey . Command ) ;
374
245
}
375
246
376
247
private void ConfigServiceLanguageChanged ( object sender , EventArgs e )
@@ -410,7 +281,6 @@ public void Dispose()
410
281
item . Value . Item1 . Unadvise ( item . Value . Item2 ) ;
411
282
}
412
283
413
- _hooks . MessageReceived -= hooks_MessageReceived ;
414
284
_hooks . Dispose ( ) ;
415
285
}
416
286
}
0 commit comments