1
1
using CommunityToolkit . Mvvm . DependencyInjection ;
2
2
using CommunityToolkit . Mvvm . Input ;
3
3
using CommunityToolkit . WinUI . Helpers ;
4
+ using CommunityToolkit . WinUI . UI ;
4
5
using CommunityToolkit . WinUI . UI . Controls ;
5
6
using Files . App . Commands ;
6
7
using Files . App . DataModels ;
22
23
using Microsoft . UI . Xaml . Navigation ;
23
24
using System ;
24
25
using System . ComponentModel ;
25
- using System . Linq ;
26
26
using System . Runtime . CompilerServices ;
27
27
using System . Threading . Tasks ;
28
28
using System . Windows . Input ;
31
31
using Windows . Graphics ;
32
32
using Windows . Services . Store ;
33
33
using Windows . Storage ;
34
+ using Windows . System ;
34
35
35
36
namespace Files . App . Views
36
37
{
@@ -39,6 +40,8 @@ namespace Files.App.Views
39
40
/// </summary>
40
41
public sealed partial class MainPage : Page , INotifyPropertyChanged
41
42
{
43
+ private VirtualKeyModifiers currentModifiers = VirtualKeyModifiers . None ;
44
+
42
45
public IUserSettingsService UserSettingsService { get ; } = Ioc . Default . GetRequiredService < IUserSettingsService > ( ) ;
43
46
public ICommandManager Commands { get ; } = Ioc . Default . GetRequiredService < ICommandManager > ( ) ;
44
47
@@ -218,6 +221,62 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
218
221
SidebarControl . SidebarItemNewPaneInvoked += SidebarControl_SidebarItemNewPaneInvoked ;
219
222
}
220
223
224
+ protected override async void OnPreviewKeyDown ( KeyRoutedEventArgs e )
225
+ {
226
+ base . OnPreviewKeyDown ( e ) ;
227
+
228
+ switch ( e . Key )
229
+ {
230
+ case VirtualKey . Menu :
231
+ currentModifiers |= VirtualKeyModifiers . Menu ;
232
+ break ;
233
+ case VirtualKey . Control :
234
+ currentModifiers |= VirtualKeyModifiers . Control ;
235
+ break ;
236
+ case VirtualKey . Shift :
237
+ currentModifiers |= VirtualKeyModifiers . Shift ;
238
+ break ;
239
+ default :
240
+ // break for natives hotkeys in textbox (cut/copy/paste/selectAll/cancel)
241
+ bool isTextBox = e . OriginalSource is DependencyObject source && source . FindAscendantOrSelf < TextBox > ( ) is not null ;
242
+ if ( isTextBox )
243
+ {
244
+ if ( currentModifiers is VirtualKeyModifiers . Control &&
245
+ e . Key is VirtualKey . X or VirtualKey . C or VirtualKey . V or VirtualKey . A or VirtualKey . Z )
246
+ {
247
+ break ;
248
+ }
249
+ }
250
+
251
+ // execute command for hotkey
252
+ var hotKey = new HotKey ( e . Key , currentModifiers ) ;
253
+ var command = Commands [ hotKey ] ;
254
+ if ( command . Code is not CommandCodes . None )
255
+ {
256
+ e . Handled = true ;
257
+ await command . ExecuteAsync ( ) ;
258
+ }
259
+ break ;
260
+ }
261
+ }
262
+ protected override void OnPreviewKeyUp ( KeyRoutedEventArgs e )
263
+ {
264
+ base . OnPreviewKeyDown ( e ) ;
265
+
266
+ switch ( e . Key )
267
+ {
268
+ case VirtualKey . Menu :
269
+ currentModifiers &= ~ VirtualKeyModifiers . Menu ;
270
+ break ;
271
+ case VirtualKey . Control :
272
+ currentModifiers &= ~ VirtualKeyModifiers . Control ;
273
+ break ;
274
+ case VirtualKey . Shift :
275
+ currentModifiers &= ~ VirtualKeyModifiers . Shift ;
276
+ break ;
277
+ }
278
+ }
279
+
221
280
private async void SidebarControl_SidebarItemDropped ( object sender , SidebarItemDroppedEventArgs e )
222
281
{
223
282
await SidebarAdaptiveViewModel . FilesystemHelpers . PerformOperationTypeAsync ( e . AcceptedOperation , e . Package , e . ItemPath , false , true ) ;
@@ -320,11 +379,6 @@ private void Page_Loaded(object sender, RoutedEventArgs e)
320
379
FindName ( nameof ( TabControl ) ) ;
321
380
FindName ( nameof ( NavToolbar ) ) ;
322
381
323
- var commands = Commands . Where ( command => ! command . CustomHotKey . IsNone ) ;
324
- foreach ( var command in commands )
325
- KeyboardAccelerators . Add ( new CommandAccelerator ( command ) ) ;
326
- Commands . HotKeyChanged += Commands_HotKeyChanged ;
327
-
328
382
if ( Package . Current . Id . Name != "49306atecsolution.FilesUWP" || UserSettingsService . ApplicationSettingsService . ClickedToReviewApp )
329
383
return ;
330
384
@@ -513,51 +567,5 @@ private void RootGrid_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
513
567
}
514
568
515
569
private void NavToolbar_Loaded ( object sender , RoutedEventArgs e ) => UpdateNavToolbarProperties ( ) ;
516
-
517
- private void Commands_HotKeyChanged ( object ? sender , HotKeyChangedEventArgs e )
518
- {
519
- if ( ! e . OldHotKey . IsNone )
520
- {
521
- var oldAccelerator = KeyboardAccelerators . FirstOrDefault ( IsOldHotKey ) ;
522
- if ( oldAccelerator is CommandAccelerator commandAccelerator )
523
- {
524
- commandAccelerator . Dispose ( ) ;
525
- KeyboardAccelerators . Remove ( commandAccelerator ) ;
526
- }
527
- }
528
-
529
- if ( ! e . NewHotKey . IsNone )
530
- {
531
- var newAccelerator = new CommandAccelerator ( e . Command ) ;
532
- KeyboardAccelerators . Add ( newAccelerator ) ;
533
- }
534
-
535
- bool IsOldHotKey ( KeyboardAccelerator accelerator )
536
- => accelerator is CommandAccelerator commandAccelerator
537
- && accelerator . Key == e . OldHotKey . Key
538
- && accelerator . Modifiers == e . OldHotKey . Modifiers ;
539
- }
540
-
541
- private class CommandAccelerator : KeyboardAccelerator , IDisposable
542
- {
543
- public IRichCommand Command { get ; }
544
-
545
- public CommandAccelerator ( IRichCommand command )
546
- {
547
- Command = command ;
548
-
549
- Key = Command . CustomHotKey . Key ;
550
- Modifiers = Command . CustomHotKey . Modifiers ;
551
- Invoked += CommandAccelerator_Invoked ;
552
- }
553
-
554
- public void Dispose ( ) => Invoked -= CommandAccelerator_Invoked ;
555
-
556
- private async void CommandAccelerator_Invoked ( KeyboardAccelerator sender , KeyboardAcceleratorInvokedEventArgs e )
557
- {
558
- e . Handled = true ;
559
- await Command . ExecuteAsync ( ) ;
560
- }
561
- }
562
570
}
563
571
}
0 commit comments