forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGridLayoutPage.xaml.cs
More file actions
828 lines (686 loc) · 26.8 KB
/
Copy pathGridLayoutPage.xaml.cs
File metadata and controls
828 lines (686 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
// Copyright (c) Files Community
// Licensed under the MIT License.
using CommunityToolkit.WinUI;
using Files.App.UserControls.Selection;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Storage;
using Windows.System;
using Windows.UI.Core;
namespace Files.App.Views.Layouts
{
/// <summary>
/// Represents the browser page of Grid View
/// </summary>
public sealed partial class GridLayoutPage : BaseGroupableLayoutPage
{
// Fields
/// <summary>
/// This reference is used to prevent unnecessary icon reloading by only reloading icons when their
/// size changes, even if the layout size changes (since some layout sizes share the same icon size).
/// </summary>
private uint currentIconSize;
private volatile bool shouldSetVerticalScrollMode;
// Properties
public ScrollViewer? ContentScroller { get; private set; }
protected override ListViewBase ListViewBase => FileList;
protected override SemanticZoom RootZoom => RootGridZoom;
// List View properties
/// <summary>
/// Row height in the List View layout
/// </summary>
public int RowHeightListView =>
LayoutSizeKindHelper.GetListViewRowHeight(LayoutSettingsService.ListViewSize);
/// <summary>
/// Icon Box size in the List View layout. The value is increased by 4px to account for icon overlays.
/// </summary>
public int IconBoxSizeListView =>
(int)(LayoutSizeKindHelper.GetIconSize(FolderLayoutModes.ListView) + 4);
// Grid View properties
/// <summary>
/// Item width in the Grid View layout
/// </summary>
public int ItemWidthGridView =>
LayoutSizeKindHelper.GetGridViewItemWidth(LayoutSettingsService.GridViewSize);
// Cards View properties
/// <summary>
/// Gets the details box width for the Cards View layout based on the card size.
/// </summary>
public int CardsViewDetailsBoxWidth => LayoutSettingsService.CardsViewSize switch
{
CardsViewSizeKind.Small => 196,
CardsViewSizeKind.Medium => 240,
CardsViewSizeKind.Large => 280,
CardsViewSizeKind.ExtraLarge => 320,
_ => 300
};
/// <summary>
/// Gets the details box height for the Cards View layout based on the card size.
/// </summary>
public int CardsViewDetailsBoxHeight => LayoutSettingsService.CardsViewSize switch
{
CardsViewSizeKind.Small => 104,
CardsViewSizeKind.Medium => 144,
CardsViewSizeKind.Large => 144,
CardsViewSizeKind.ExtraLarge => 128,
_ => 128
};
/// <summary>
/// Gets the icon box height for the Cards View layout based on the card size.
/// </summary>
public int CardsViewIconBoxHeight => LayoutSettingsService.CardsViewSize switch
{
CardsViewSizeKind.Small => 104,
CardsViewSizeKind.Medium => 96,
CardsViewSizeKind.Large => 128,
CardsViewSizeKind.ExtraLarge => 160,
_ => 128
};
/// <summary>
/// Gets the icon box width for the Cards View layout based on the card size.
/// </summary>
public int CardsViewIconBoxWidth => LayoutSettingsService.CardsViewSize switch
{
CardsViewSizeKind.Small => 104,
CardsViewSizeKind.Medium => 240,
CardsViewSizeKind.Large => 280,
CardsViewSizeKind.ExtraLarge => 320,
_ => 128
};
/// <summary>
/// Gets the orientation of cards in the Cards View layout.
/// </summary>
public Orientation CardsViewOrientation => UserSettingsService.LayoutSettingsService.CardsViewSize == CardsViewSizeKind.Small
? Orientation.Horizontal
: Orientation.Vertical;
/// <summary>
/// Gets the maximum lines for item names in the Cards View layout.
/// </summary>
public int CardsViewItemNameMaxLines =>
LayoutSettingsService.CardsViewSize == CardsViewSizeKind.ExtraLarge ? 1 : 2;
/// <summary>
/// Gets the visibility for the contextual property string in the Cards View layout.
/// </summary>
public bool CardsViewShowContextualProperty =>
LayoutSettingsService.CardsViewSize != CardsViewSizeKind.Small;
/// <summary>
/// Gets the icon size for items in the Cards View layout.
/// </summary>
public int CardsViewIconSize =>
(int)LayoutSizeKindHelper.GetIconSize(FolderLayoutModes.CardsView);
// Constructor
public GridLayoutPage() : base()
{
InitializeComponent();
DataContext = this;
var selectionRectangle = RectangleSelection.Create(ListViewBase, SelectionRectangle, FileList_SelectionChanged);
selectionRectangle.SelectionEnded += SelectionRectangle_SelectionEnded;
}
// Methods
protected override void ItemManipulationModel_ScrollIntoViewInvoked(object? sender, ListedItem e)
{
FileList.ScrollIntoView(e);
}
protected override void ItemManipulationModel_ScrollToTopInvoked(object? sender, EventArgs e)
{
if (FolderSettings?.LayoutMode is FolderLayoutModes.ListView)
ContentScroller?.ChangeView(0, null, null, true);
else
ContentScroller?.ChangeView(null, 0, null, true);
}
protected override void ItemManipulationModel_FocusSelectedItemsInvoked(object? sender, EventArgs e)
{
if (SelectedItems.Any())
{
FileList.ScrollIntoView(SelectedItems.Last());
(FileList.ContainerFromItem(SelectedItems.Last()) as GridViewItem)?.Focus(FocusState.Keyboard);
}
}
protected override void ItemManipulationModel_AddSelectedItemInvoked(object? sender, ListedItem e)
{
if ((NextRenameIndex != 0 && TryStartRenameNextItem(e)) || (!FileList?.Items.Contains(e) ?? true))
return;
FileList!.SelectedItems.Add(e);
}
protected override void ItemManipulationModel_RemoveSelectedItemInvoked(object? sender, ListedItem e)
{
if (FileList?.Items.Contains(e) ?? false)
FileList.SelectedItems.Remove(e);
}
protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
{
if (eventArgs.Parameter is NavigationArguments navArgs)
navArgs.FocusOnNavigation = true;
base.OnNavigatedTo(eventArgs);
currentIconSize = LayoutSizeKindHelper.GetIconSize(FolderSettings.LayoutMode);
FolderSettings.LayoutModeChangeRequested -= FolderSettings_LayoutModeChangeRequested;
FolderSettings.LayoutModeChangeRequested += FolderSettings_LayoutModeChangeRequested;
UserSettingsService.LayoutSettingsService.PropertyChanged += LayoutSettingsService_PropertyChanged;
// Set ItemTemplate
SetItemTemplate();
SetItemContainerStyle();
FileList.ItemsSource ??= ParentShellPageInstance.ShellViewModel.FilesAndFolders;
var parameters = (NavigationArguments)eventArgs.Parameter;
if (parameters.IsLayoutSwitch)
_ = ReloadItemIconsAsync();
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
if (FolderSettings != null)
FolderSettings.LayoutModeChangeRequested -= FolderSettings_LayoutModeChangeRequested;
UserSettingsService.LayoutSettingsService.PropertyChanged -= LayoutSettingsService_PropertyChanged;
}
private void LayoutSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// Get current scroll position
var previousHorizontalOffset = ContentScroller?.HorizontalOffset;
var previousVerticalOffset = ContentScroller?.VerticalOffset;
if (e.PropertyName == nameof(ILayoutSettingsService.ListViewSize))
{
NotifyPropertyChanged(nameof(RowHeightListView));
NotifyPropertyChanged(nameof(IconBoxSizeListView));
// Update the container style to match the item size
SetItemContainerStyle();
FolderSettings_IconSizeChanged();
}
if (e.PropertyName == nameof(ILayoutSettingsService.CardsViewSize))
{
// Update the container style to match the item size
SetItemContainerStyle();
FolderSettings_IconSizeChanged();
}
if (e.PropertyName == nameof(ILayoutSettingsService.GridViewSize))
{
// Update the container style to match the item size
SetItemContainerStyle();
FolderSettings_IconSizeChanged();
}
// Restore correct scroll position
ContentScroller?.ChangeView(previousHorizontalOffset, previousVerticalOffset, null);
}
private void FolderSettings_LayoutModeChangeRequested(object? sender, LayoutModeEventArgs e)
{
if (FolderSettings.LayoutMode == FolderLayoutModes.ListView
|| FolderSettings.LayoutMode == FolderLayoutModes.CardsView
|| FolderSettings.LayoutMode == FolderLayoutModes.GridView)
{
// Set ItemTemplate
SetItemTemplate();
SetItemContainerStyle();
FolderSettings_IconSizeChanged();
}
}
private void SetItemTemplate()
{
var newFileListStyle = FolderSettings.LayoutMode switch
{
FolderLayoutModes.ListView => (Style)Resources["VerticalLayoutGridView"],
FolderLayoutModes.CardsView => (Style)Resources["HorizontalLayoutGridView"],
_ => (Style)Resources["HorizontalLayoutGridView"]
};
if (FileList.Style != newFileListStyle)
{
var oldSource = FileList.ItemsSource;
FileList.ItemsSource = null;
FileList.Style = newFileListStyle;
FileList.ItemsSource = oldSource;
}
shouldSetVerticalScrollMode = true;
switch (FolderSettings.LayoutMode)
{
case FolderLayoutModes.ListView:
FileList.ItemTemplate = ListViewBrowserTemplate;
break;
case FolderLayoutModes.CardsView:
FileList.ItemTemplate = CardsBrowserTemplate;
break;
default:
FileList.ItemTemplate = GridViewBrowserTemplate;
break;
}
}
private void SetItemContainerStyle()
{
if (FolderSettings?.LayoutMode == FolderLayoutModes.CardsView || FolderSettings?.LayoutMode == FolderLayoutModes.GridView)
{
// Toggle style to force item size to update
FileList.ItemContainerStyle = LocalListItemContainerStyle;
// Set correct style
FileList.ItemContainerStyle = LocalRegularItemContainerStyle;
}
else if (FolderSettings?.LayoutMode == FolderLayoutModes.ListView)
{
if (UserSettingsService.LayoutSettingsService.ListViewSize == ListViewSizeKind.Compact)
{
// Toggle style to force item size to update
FileList.ItemContainerStyle = LocalRegularItemContainerStyle;
// Set correct style
FileList.ItemContainerStyle = LocalCompactListItemContainerStyle;
}
else
{
// Toggle style to force item size to update
FileList.ItemContainerStyle = LocalCompactListItemContainerStyle;
// Set correct style
FileList.ItemContainerStyle = LocalListItemContainerStyle;
}
}
}
private void FileList_Loaded(object sender, RoutedEventArgs e)
{
ContentScroller = FileList.FindDescendant<ScrollViewer>(x => x.Name == "ScrollViewer");
}
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
foreach (var item in e.AddedItems)
SetCheckboxSelectionState(item);
foreach (var item in e.RemovedItems)
SetCheckboxSelectionState(item);
}
override public void StartRenameItem()
{
RenamingItem = SelectedItem;
if (RenamingItem is null || FolderSettings is null)
return;
int extensionLength = RenamingItem.FileExtension?.Length ?? 0;
if (FileList.ContainerFromItem(RenamingItem) is not GridViewItem gridViewItem)
return;
if (gridViewItem.FindDescendant("ItemName") is not TextBlock textBlock)
return;
TextBox? textBox = null;
// Grid View
if (FolderSettings.LayoutMode == FolderLayoutModes.GridView)
{
if (gridViewItem.FindDescendant("EditPopup") is not Popup popup)
return;
textBox = popup.Child as TextBox;
if (textBox is null)
return;
textBox.Text = textBlock.Text;
textBlock.Opacity = 0;
popup.IsOpen = true;
OldItemName = textBlock.Text;
}
// List View
else if (FolderSettings.LayoutMode == FolderLayoutModes.ListView)
{
textBox = gridViewItem.FindDescendant("ListViewTextBoxItemName") as TextBox;
if (textBox is null)
return;
textBox.Text = textBlock.Text;
OldItemName = textBlock.Text;
textBlock.Visibility = Visibility.Collapsed;
textBox.Visibility = Visibility.Visible;
if (textBox.FindParent<Grid>() is null)
{
textBlock.Visibility = Visibility.Visible;
textBox.Visibility = Visibility.Collapsed;
return;
}
}
// Cards View
else
{
textBox = gridViewItem.FindDescendant("TileViewTextBoxItemName") as TextBox;
if (textBox is null)
return;
textBox.Text = textBlock.Text;
OldItemName = textBlock.Text;
textBox.Visibility = Visibility.Visible;
if (textBox.FindParent<Grid>() is null)
{
textBox.Visibility = Visibility.Collapsed;
return;
}
}
textBox.Focus(FocusState.Pointer);
textBox.LostFocus += RenameTextBox_LostFocus;
textBox.KeyDown += RenameTextBox_KeyDown;
int selectedTextLength = RenamingItem.Name.Length;
if (!RenamingItem.IsShortcut && UserSettingsService.FoldersSettingsService.ShowFileExtensions)
selectedTextLength -= extensionLength;
textBox.Select(0, selectedTextLength);
IsRenamingItem = true;
}
private void ItemNameTextBox_BeforeTextChanging(TextBox textBox, TextBoxBeforeTextChangingEventArgs args)
{
if (!IsRenamingItem)
return;
_ = ValidateItemNameInputTextAsync(textBox, args, (showError) =>
{
FileNameTeachingTip.Visibility = showError ? Visibility.Visible : Visibility.Collapsed;
FileNameTeachingTip.IsOpen = showError;
});
}
protected override void EndRename(TextBox textBox)
{
GridViewItem? gridViewItem = FileList.ContainerFromItem(RenamingItem) as GridViewItem;
if (textBox is null || gridViewItem is null)
{
// NOTE: Navigating away, do nothing
}
else if (FolderSettings.LayoutMode == FolderLayoutModes.GridView)
{
Popup? popup = gridViewItem.FindDescendant("EditPopup") as Popup;
TextBlock? textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;
if (popup is not null)
popup.IsOpen = false;
if (textBlock is not null)
textBlock.Opacity = (textBlock.DataContext as ListedItem)!.Opacity;
}
else if (FolderSettings.LayoutMode == FolderLayoutModes.CardsView || FolderSettings.LayoutMode == FolderLayoutModes.ListView)
{
TextBlock? textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;
textBox.Visibility = Visibility.Collapsed;
if (textBlock is not null)
textBlock.Visibility = Visibility.Visible;
}
// Unsubscribe from events
if (textBox is not null)
{
textBox.LostFocus -= RenameTextBox_LostFocus;
textBox.KeyDown -= RenameTextBox_KeyDown;
}
FileNameTeachingTip.IsOpen = false;
IsRenamingItem = false;
// Re-focus selected list item
gridViewItem?.Focus(FocusState.Programmatic);
}
protected override async void FileList_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (ParentShellPageInstance is null || IsRenamingItem)
return;
var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
var focusedElement = FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot) as FrameworkElement;
var isFooterFocused = focusedElement is HyperlinkButton;
if (ctrlPressed && e.Key is VirtualKey.A)
{
e.Handled = true;
var commands = Ioc.Default.GetRequiredService<ICommandManager>();
var hotKey = new HotKey(Keys.A, KeyModifiers.Ctrl);
await commands[hotKey].ExecuteAsync();
}
else if (e.Key == VirtualKey.Enter && !isFooterFocused && !e.KeyStatus.IsMenuKeyDown)
{
e.Handled = true;
if (ctrlPressed && !shiftPressed)
{
var folders = ParentShellPageInstance?.SlimContentPage.SelectedItems?.Where(file => file.PrimaryItemAttribute == StorageItemTypes.Folder);
foreach (ListedItem? folder in folders)
{
if (folder is not null)
await NavigationHelpers.OpenPathInNewTab(folder.ItemPath);
}
}
else if (ctrlPressed && shiftPressed)
{
NavigationHelpers.OpenInSecondaryPane(ParentShellPageInstance, SelectedItems.FirstOrDefault(item => item.PrimaryItemAttribute == StorageItemTypes.Folder));
}
}
else if (e.Key == VirtualKey.Enter && e.KeyStatus.IsMenuKeyDown)
{
FilePropertiesHelpers.OpenPropertiesWindow(ParentShellPageInstance);
e.Handled = true;
}
else if (e.Key == VirtualKey.Space)
{
e.Handled = true;
}
else if (e.KeyStatus.IsMenuKeyDown && (e.Key == VirtualKey.Left || e.Key == VirtualKey.Right || e.Key == VirtualKey.Up))
{
// Unfocus the GridView so keyboard shortcut can be handled
Focus(FocusState.Pointer);
}
else if (e.KeyStatus.IsMenuKeyDown && shiftPressed && e.Key == VirtualKey.Add)
{
// Unfocus the ListView so keyboard shortcut can be handled (alt + shift + "+")
Focus(FocusState.Pointer);
}
else if (e.Key == VirtualKey.Up || e.Key == VirtualKey.Down)
{
// If list has only one item, select it on arrow down/up (#5681)
if (IsItemSelected)
return;
FileList.SelectedIndex = 0;
e.Handled = true;
}
}
protected override bool CanGetItemFromElement(object element)
=> element is GridViewItem;
private void FolderSettings_IconSizeChanged()
{
// Check if icons need to be reloaded
var newIconSize = LayoutSizeKindHelper.GetIconSize(FolderSettings.LayoutMode);
if (newIconSize != currentIconSize)
{
currentIconSize = newIconSize;
_ = ReloadItemIconsAsync();
}
}
private async Task ReloadItemIconsAsync()
{
if (ParentShellPageInstance is null)
return;
ParentShellPageInstance.ShellViewModel.CancelExtendedPropertiesLoading();
var filesAndFolders = ParentShellPageInstance.ShellViewModel.FilesAndFolders.ToList();
foreach (ListedItem listedItem in filesAndFolders)
{
listedItem.ItemPropertiesInitialized = false;
if (FileList.ContainerFromItem(listedItem) is not null)
await ParentShellPageInstance.ShellViewModel.LoadExtendedItemPropertiesAsync(listedItem);
}
if (ParentShellPageInstance.ShellViewModel.EnabledGitProperties is not GitProperties.None)
{
await Task.WhenAll(filesAndFolders.Select(item =>
{
if (item is IGitItem gitItem)
return ParentShellPageInstance.ShellViewModel.LoadGitPropertiesAsync(gitItem);
return Task.CompletedTask;
}));
}
}
private async void FileList_ItemTapped(object sender, TappedRoutedEventArgs e)
{
var clickedItem = e.OriginalSource as FrameworkElement;
var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
var shiftPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
var item = (e.OriginalSource as FrameworkElement)?.DataContext as ListedItem;
if (item is null)
{
// Clear selection when clicking empty area via touch
// https://github.com/files-community/Files/issues/15051
if (e.PointerDeviceType == PointerDeviceType.Touch)
ItemManipulationModel.ClearSelection();
return;
}
// Skip code if the control or shift key is pressed or if the user is using multiselect
if (ctrlPressed ||
shiftPressed ||
clickedItem is Microsoft.UI.Xaml.Shapes.Rectangle)
{
e.Handled = true;
return;
}
// Check if the setting to open items with a single click is turned on
if ((item.PrimaryItemAttribute is StorageItemTypes.File && UserSettingsService.FoldersSettingsService.OpenFilesWithSingleClick.ShouldOpenWithSingleClick(e.PointerDeviceType)) ||
(item.PrimaryItemAttribute is StorageItemTypes.Folder && UserSettingsService.FoldersSettingsService.OpenFoldersWithSingleClick.ShouldOpenWithSingleClick(e.PointerDeviceType)))
{
ResetRenameDoubleClick();
await Commands.OpenItem.ExecuteAsync();
}
else
{
if (clickedItem is TextBlock textBlock && textBlock.Name == "ItemName")
{
CheckRenameDoubleClick(clickedItem?.DataContext);
}
else if (IsRenamingItem)
{
if (FileList.ContainerFromItem(RenamingItem) is GridViewItem gridViewItem)
{
if (FolderSettings.LayoutMode == FolderLayoutModes.GridView)
{
Popup popup = gridViewItem.FindDescendant("EditPopup") as Popup;
var textBox = popup?.Child as TextBox;
if (textBox is not null)
await CommitRenameAsync(textBox);
}
else
{
var textBox = gridViewItem.FindDescendant("TileViewTextBoxItemName") as TextBox;
if (textBox is not null)
await CommitRenameAsync(textBox);
}
}
}
}
}
private async void FileList_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
// Skip opening selected items if the double tap doesn't capture an item
if ((e.OriginalSource as FrameworkElement)?.DataContext is ListedItem item &&
((item.PrimaryItemAttribute == StorageItemTypes.File && !UserSettingsService.FoldersSettingsService.OpenFilesWithSingleClick.ShouldOpenWithSingleClick(e.PointerDeviceType)) ||
(item.PrimaryItemAttribute == StorageItemTypes.Folder && !UserSettingsService.FoldersSettingsService.OpenFoldersWithSingleClick.ShouldOpenWithSingleClick(e.PointerDeviceType))))
await Commands.OpenItem.ExecuteAsync();
else if ((e.OriginalSource as FrameworkElement)?.DataContext is not ListedItem && UserSettingsService.FoldersSettingsService.DoubleClickToGoUp)
await Commands.NavigateUp.ExecuteAsync();
ResetRenameDoubleClick();
}
private void ItemSelected_Checked(object sender, RoutedEventArgs e)
{
if (sender is CheckBox checkBox &&
checkBox.DataContext is ListedItem item &&
!FileList.SelectedItems.Contains(item))
FileList.SelectedItems.Add(item);
}
private void ItemSelected_Unchecked(object sender, RoutedEventArgs e)
{
if (sender is not CheckBox checkBox)
return;
if (checkBox.DataContext is ListedItem item && FileList.SelectedItems.Contains(item))
FileList.SelectedItems.Remove(item);
// Workaround for #17298
checkBox.IsTabStop = false;
checkBox.IsEnabled = false;
checkBox.IsEnabled = true;
checkBox.IsTabStop = true;
FileList.Focus(FocusState.Programmatic);
}
private new void FileList_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
var selectionCheckbox = args.ItemContainer.FindDescendant("SelectionCheckbox")!;
selectionCheckbox.PointerEntered -= SelectionCheckbox_PointerEntered;
selectionCheckbox.PointerExited -= SelectionCheckbox_PointerExited;
selectionCheckbox.PointerCanceled -= SelectionCheckbox_PointerCanceled;
base.FileList_ContainerContentChanging(sender, args);
SetCheckboxSelectionState(args.Item, args.ItemContainer as GridViewItem);
selectionCheckbox.PointerEntered += SelectionCheckbox_PointerEntered;
selectionCheckbox.PointerExited += SelectionCheckbox_PointerExited;
selectionCheckbox.PointerCanceled += SelectionCheckbox_PointerCanceled;
}
private void SetCheckboxSelectionState(object item, GridViewItem? lviContainer = null)
{
var container = lviContainer ?? FileList.ContainerFromItem(item) as GridViewItem;
if (container is not null)
{
var checkbox = container.FindDescendant("SelectionCheckbox") as CheckBox;
if (checkbox is not null)
{
// Temporarily disable events to avoid selecting wrong items
checkbox.Checked -= ItemSelected_Checked;
checkbox.Unchecked -= ItemSelected_Unchecked;
checkbox.IsChecked = FileList.SelectedItems.Contains(item);
checkbox.Checked += ItemSelected_Checked;
checkbox.Unchecked += ItemSelected_Unchecked;
}
UpdateCheckboxVisibility(container, checkbox?.IsPointerOver ?? false);
}
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
// This is the best way I could find to set the context flyout, as doing it in the styles isn't possible
// because you can't use bindings in the setters
DependencyObject item = VisualTreeHelper.GetParent(sender as Grid);
while (item is not GridViewItem)
item = VisualTreeHelper.GetParent(item);
if (item is GridViewItem itemContainer)
itemContainer.ContextFlyout = ItemContextMenuFlyout;
// Set VerticalScrollMode after an item has been loaded (#14785)
if (shouldSetVerticalScrollMode)
{
shouldSetVerticalScrollMode = false;
if (FolderSettings?.LayoutMode is FolderLayoutModes.ListView)
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Disabled);
else
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Enabled);
}
}
private void SelectionCheckbox_PointerEntered(object sender, PointerRoutedEventArgs e)
{
UpdateCheckboxVisibility((sender as FrameworkElement)!.FindAscendant<GridViewItem>()!, true);
}
private void SelectionCheckbox_PointerExited(object sender, PointerRoutedEventArgs e)
{
UpdateCheckboxVisibility((sender as FrameworkElement)!.FindAscendant<GridViewItem>()!, false);
}
private void SelectionCheckbox_PointerCanceled(object sender, PointerRoutedEventArgs e)
{
UpdateCheckboxVisibility((sender as FrameworkElement)!.FindAscendant<GridViewItem>()!, false);
}
// To avoid crashes, disable scrolling when drag-and-drop if grouped. (#14484)
private bool ShouldDisableScrollingWhenDragAndDrop =>
FolderSettings?.LayoutMode is FolderLayoutModes.GridView or FolderLayoutModes.CardsView &&
(ParentShellPageInstance?.ShellViewModel.FilesAndFolders.IsGrouped ?? false);
protected override void FileList_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
if (ShouldDisableScrollingWhenDragAndDrop)
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Disabled);
base.FileList_DragItemsStarting(sender, e);
if (ShouldDisableScrollingWhenDragAndDrop && e.Cancel)
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Enabled);
}
private void ItemsLayout_DragEnter(object sender, DragEventArgs e)
{
if (ShouldDisableScrollingWhenDragAndDrop)
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Disabled);
}
private void ItemsLayout_DragLeave(object sender, DragEventArgs e)
{
if (ShouldDisableScrollingWhenDragAndDrop)
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Enabled);
}
protected override void ItemsLayout_Drop(object sender, DragEventArgs e)
{
if (ShouldDisableScrollingWhenDragAndDrop)
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Enabled);
base.ItemsLayout_Drop(sender, e);
}
protected override void Item_Drop(object sender, DragEventArgs e)
{
if (ShouldDisableScrollingWhenDragAndDrop)
ScrollViewer.SetVerticalScrollMode(FileList, ScrollMode.Enabled);
base.Item_Drop(sender, e);
}
private void UpdateCheckboxVisibility(object sender, bool isPointerOver)
{
if (sender is GridViewItem control && control.FindDescendant<UserControl>() is UserControl userControl)
{
// Handle visual states
// Show checkboxes when items are selected (as long as the setting is enabled)
// Show checkboxes when hovering over the checkbox area (regardless of the setting to hide them)
if (UserSettingsService.FoldersSettingsService.ShowCheckboxesWhenSelectingItems && control.IsSelected
|| isPointerOver)
VisualStateManager.GoToState(userControl, "ShowCheckbox", true);
else
VisualStateManager.GoToState(userControl, "HideCheckbox", true);
}
}
}
}