forked from files-community/Files
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDetailsLayoutPage.xaml.cs
More file actions
1107 lines (945 loc) · 41 KB
/
Copy pathDetailsLayoutPage.xaml.cs
File metadata and controls
1107 lines (945 loc) · 41 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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Files Community
// Licensed under the MIT License.
using CommunityToolkit.WinUI;
using Files.App.Controls;
using Files.App.UserControls.Selection;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation;
using Windows.Storage;
using Windows.System;
using Windows.UI.Core;
using SortDirection = Files.App.Data.Enums.SortDirection;
namespace Files.App.Views.Layouts
{
/// <summary>
/// Represents the browser page of Details View
/// </summary>
public sealed partial class DetailsLayoutPage : BaseGroupableLayoutPage
{
// Constants
private const int TAG_TEXT_BLOCK = 1;
// Fields
private ListedItem? _nextItemToSelect;
/// <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;
// Properties
protected override ListViewBase ListViewBase => FileList;
protected override SemanticZoom RootZoom => RootGridZoom;
public ColumnsViewModel ColumnsViewModel { get; } = new();
private RelayCommand<string>? UpdateSortOptionsCommand { get; set; }
public ScrollViewer? ContentScroller { get; private set; }
private double maxWidthForRenameTextbox;
public double MaxWidthForRenameTextbox
{
get => maxWidthForRenameTextbox;
set
{
if (value != maxWidthForRenameTextbox)
{
maxWidthForRenameTextbox = value;
NotifyPropertyChanged(nameof(MaxWidthForRenameTextbox));
}
}
}
/// <summary>
/// Row height for items in the Details View
/// </summary>
public int RowHeight
{
get => LayoutSizeKindHelper.GetDetailsViewRowHeight((DetailsViewSizeKind)UserSettingsService.LayoutSettingsService.DetailsViewSize);
}
// Constructor
public DetailsLayoutPage() : base()
{
InitializeComponent();
DataContext = this;
var selectionRectangle = RectangleSelection.Create(FileList, SelectionRectangle, FileList_SelectionChanged);
selectionRectangle.SelectionEnded += SelectionRectangle_SelectionEnded;
UpdateSortOptionsCommand = new RelayCommand<string>(x =>
{
if (!Enum.TryParse<SortOption>(x, out var val))
return;
if (FolderSettings.DirectorySortOption == val)
{
FolderSettings.DirectorySortDirection = (SortDirection)(((int)FolderSettings.DirectorySortDirection + 1) % 2);
}
else
{
FolderSettings.DirectorySortOption = val;
FolderSettings.DirectorySortDirection = SortDirection.Ascending;
}
});
}
// Methods
protected override void ItemManipulationModel_ScrollIntoViewInvoked(object? sender, ListedItem e)
{
FileList.ScrollIntoView(e);
ContentScroller?.ChangeView(null, FileList.Items.IndexOf(e) * RowHeight, null, true); // Scroll to index * item height
}
protected override void ItemManipulationModel_ScrollToTopInvoked(object? sender, EventArgs e)
{
ContentScroller?.ChangeView(null, 0, null, true);
}
protected override void ItemManipulationModel_FocusSelectedItemsInvoked(object? sender, EventArgs e)
{
if (SelectedItems?.Any() ?? false)
{
FileList.ScrollIntoView(SelectedItems.Last());
ContentScroller?.ChangeView(null, FileList.Items.IndexOf(SelectedItems.Last()) * RowHeight, null, false);
(FileList.ContainerFromItem(SelectedItems.Last()) as ListViewItem)?.Focus(FocusState.Keyboard);
}
}
protected override void ItemManipulationModel_AddSelectedItemInvoked(object? sender, ListedItem e)
{
if (NextRenameIndex != 0)
{
_nextItemToSelect = e;
FileList.LayoutUpdated += FileList_LayoutUpdated;
}
else if (FileList?.Items.Contains(e) ?? false)
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(FolderLayoutModes.DetailsView);
if (FolderSettings?.ColumnsViewModel is not null)
{
// Don't assign the columns view model directly, instead update each property individually using the Update method.
// This is done to workaround a bug where CsWinRT doesn't properly track the memory of the object so that
// an invalid memory access can occur when the object is moved.
// See https://github.com/microsoft/CsWinRT/issues/1834.
ColumnsViewModel.DateCreatedColumn.Update(FolderSettings.ColumnsViewModel.DateCreatedColumn);
ColumnsViewModel.DateDeletedColumn.Update(FolderSettings.ColumnsViewModel.DateDeletedColumn);
ColumnsViewModel.DateModifiedColumn.Update(FolderSettings.ColumnsViewModel.DateModifiedColumn);
ColumnsViewModel.IconColumn.Update(FolderSettings.ColumnsViewModel.IconColumn);
ColumnsViewModel.ItemTypeColumn.Update(FolderSettings.ColumnsViewModel.ItemTypeColumn);
ColumnsViewModel.NameColumn.Update(FolderSettings.ColumnsViewModel.NameColumn);
ColumnsViewModel.PathColumn.Update(FolderSettings.ColumnsViewModel.PathColumn);
ColumnsViewModel.OriginalPathColumn.Update(FolderSettings.ColumnsViewModel.OriginalPathColumn);
ColumnsViewModel.SizeColumn.Update(FolderSettings.ColumnsViewModel.SizeColumn);
ColumnsViewModel.StatusColumn.Update(FolderSettings.ColumnsViewModel.StatusColumn);
ColumnsViewModel.TagColumn.Update(FolderSettings.ColumnsViewModel.TagColumn);
ColumnsViewModel.GitStatusColumn.Update(FolderSettings.ColumnsViewModel.GitStatusColumn);
ColumnsViewModel.GitLastCommitDateColumn.Update(FolderSettings.ColumnsViewModel.GitLastCommitDateColumn);
ColumnsViewModel.GitLastCommitMessageColumn.Update(FolderSettings.ColumnsViewModel.GitLastCommitMessageColumn);
ColumnsViewModel.GitCommitAuthorColumn.Update(FolderSettings.ColumnsViewModel.GitCommitAuthorColumn);
ColumnsViewModel.GitLastCommitShaColumn.Update(FolderSettings.ColumnsViewModel.GitLastCommitShaColumn);
}
ParentShellPageInstance.ShellViewModel.EnabledGitProperties = GetEnabledGitProperties(ColumnsViewModel);
FolderSettings.LayoutModeChangeRequested += FolderSettings_LayoutModeChangeRequested;
FolderSettings.SortDirectionPreferenceUpdated += FolderSettings_SortDirectionPreferenceUpdated;
FolderSettings.SortOptionPreferenceUpdated += FolderSettings_SortOptionPreferenceUpdated;
ParentShellPageInstance.ShellViewModel.PageTypeUpdated += FilesystemViewModel_PageTypeUpdated;
UserSettingsService.LayoutSettingsService.PropertyChanged += LayoutSettingsService_PropertyChanged;
var parameters = (NavigationArguments)eventArgs.Parameter;
if (parameters.IsLayoutSwitch)
_ = ReloadItemIconsAsync();
FilesystemViewModel_PageTypeUpdated(null, new PageTypeUpdatedEventArgs()
{
IsTypeCloudDrive = InstanceViewModel?.IsPageTypeCloudDrive ?? false,
IsTypeRecycleBin = InstanceViewModel?.IsPageTypeRecycleBin ?? false,
IsTypeGitRepository = InstanceViewModel?.IsGitRepository ?? false,
IsTypeSearchResults = InstanceViewModel?.IsPageTypeSearchResults ?? false
});
RootGrid_SizeChanged(null, null);
SetItemContainerStyle();
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
FolderSettings.LayoutModeChangeRequested -= FolderSettings_LayoutModeChangeRequested;
FolderSettings.SortDirectionPreferenceUpdated -= FolderSettings_SortDirectionPreferenceUpdated;
FolderSettings.SortOptionPreferenceUpdated -= FolderSettings_SortOptionPreferenceUpdated;
ParentShellPageInstance.ShellViewModel.PageTypeUpdated -= FilesystemViewModel_PageTypeUpdated;
UserSettingsService.LayoutSettingsService.PropertyChanged -= LayoutSettingsService_PropertyChanged;
}
private void LayoutSettingsService_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ILayoutSettingsService.DetailsViewSize))
{
// Get current scroll position
var previousOffset = ContentScroller?.VerticalOffset;
NotifyPropertyChanged(nameof(RowHeight));
// Update the container style to match the item size
SetItemContainerStyle();
// Restore correct scroll position
ContentScroller?.ChangeView(null, previousOffset, null);
// Check if icons need to be reloaded
var newIconSize = LayoutSizeKindHelper.GetIconSize(FolderLayoutModes.DetailsView);
if (newIconSize != currentIconSize)
{
currentIconSize = newIconSize;
_ = ReloadItemIconsAsync();
}
}
else
{
var settings = sender as ILayoutSettingsService;
var isDefaultPath = FolderSettings?.IsPathUsingDefaultLayout(ParentShellPageInstance?.ShellViewModel.CurrentFolder?.ItemPath);
if (settings is not null && (isDefaultPath ?? true))
{
switch (e.PropertyName)
{
case nameof(ILayoutSettingsService.ShowFileTagColumn):
ColumnsViewModel.TagColumn.UserCollapsed = !settings.ShowFileTagColumn;
break;
case nameof(ILayoutSettingsService.ShowSizeColumn):
ColumnsViewModel.SizeColumn.UserCollapsed = !settings.ShowSizeColumn;
break;
case nameof(ILayoutSettingsService.ShowTypeColumn):
ColumnsViewModel.ItemTypeColumn.UserCollapsed = !settings.ShowTypeColumn;
break;
case nameof(ILayoutSettingsService.ShowDateCreatedColumn):
ColumnsViewModel.DateCreatedColumn.UserCollapsed = !settings.ShowDateCreatedColumn;
break;
case nameof(ILayoutSettingsService.ShowDateColumn):
ColumnsViewModel.DateModifiedColumn.UserCollapsed = !settings.ShowDateColumn;
break;
}
}
}
}
/// <summary>
/// Sets the item size and spacing
/// </summary>
private void SetItemContainerStyle()
{
if (UserSettingsService.LayoutSettingsService.DetailsViewSize == DetailsViewSizeKind.Compact)
{
// Toggle style to force item size to update
FileList.ItemContainerStyle = RegularItemContainerStyle;
// Set correct style
FileList.ItemContainerStyle = CompactItemContainerStyle;
}
else
{
// Toggle style to force item size to update
FileList.ItemContainerStyle = CompactItemContainerStyle;
// Set correct style
FileList.ItemContainerStyle = RegularItemContainerStyle;
}
// Set the width of the icon column. The value is increased by 4px to account for icon overlays.
ColumnsViewModel.IconColumn.UserLength = new GridLength(LayoutSizeKindHelper.GetIconSize(FolderLayoutModes.DetailsView) + 4);
}
private void FileList_LayoutUpdated(object? sender, object e)
{
FileList.LayoutUpdated -= FileList_LayoutUpdated;
TryStartRenameNextItem(_nextItemToSelect!);
_nextItemToSelect = null;
}
private void FolderSettings_SortOptionPreferenceUpdated(object? sender, SortOption e)
{
UpdateSortIndicator();
}
private void FolderSettings_SortDirectionPreferenceUpdated(object? sender, SortDirection e)
{
UpdateSortIndicator();
}
private void UpdateSortIndicator()
{
NameHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.Name ? FolderSettings.DirectorySortDirection : null;
TagHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.FileTag ? FolderSettings.DirectorySortDirection : null;
PathHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.Path ? FolderSettings.DirectorySortDirection : null;
OriginalPathHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.OriginalFolder ? FolderSettings.DirectorySortDirection : null;
DateDeletedHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.DateDeleted ? FolderSettings.DirectorySortDirection : null;
DateModifiedHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.DateModified ? FolderSettings.DirectorySortDirection : null;
DateCreatedHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.DateCreated ? FolderSettings.DirectorySortDirection : null;
FileTypeHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.FileType ? FolderSettings.DirectorySortDirection : null;
ItemSizeHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.Size ? FolderSettings.DirectorySortDirection : null;
SyncStatusHeader.ColumnSortOption = FolderSettings.DirectorySortOption == SortOption.SyncStatus ? FolderSettings.DirectorySortDirection : null;
}
private void FilesystemViewModel_PageTypeUpdated(object? sender, PageTypeUpdatedEventArgs e)
{
if (e.IsTypeRecycleBin)
{
ColumnsViewModel.OriginalPathColumn.Show();
ColumnsViewModel.DateDeletedColumn.Show();
}
else
{
ColumnsViewModel.OriginalPathColumn.Hide();
ColumnsViewModel.DateDeletedColumn.Hide();
}
if (e.IsTypeCloudDrive)
ColumnsViewModel.StatusColumn.Show();
else
ColumnsViewModel.StatusColumn.Hide();
if (e.IsTypeGitRepository && !e.IsTypeSearchResults)
{
ColumnsViewModel.GitCommitAuthorColumn.Show();
ColumnsViewModel.GitLastCommitDateColumn.Show();
ColumnsViewModel.GitLastCommitMessageColumn.Show();
ColumnsViewModel.GitLastCommitShaColumn.Show();
ColumnsViewModel.GitStatusColumn.Show();
}
else
{
ColumnsViewModel.GitCommitAuthorColumn.Hide();
ColumnsViewModel.GitLastCommitDateColumn.Hide();
ColumnsViewModel.GitLastCommitMessageColumn.Hide();
ColumnsViewModel.GitLastCommitShaColumn.Hide();
ColumnsViewModel.GitStatusColumn.Hide();
}
if (e.IsTypeSearchResults)
ColumnsViewModel.PathColumn.Show();
else
ColumnsViewModel.PathColumn.Hide();
UpdateSortIndicator();
}
private void FolderSettings_LayoutModeChangeRequested(object? sender, LayoutModeEventArgs e)
{
}
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()
{
StartRenameItem("ItemNameTextBox");
if (FileList.ContainerFromItem(RenamingItem) is not ListViewItem listViewItem)
return;
var textBox = listViewItem.FindDescendant("ItemNameTextBox") as TextBox;
if (textBox is null || textBox.FindParent<Grid>() is null)
return;
Grid.SetColumnSpan(textBox.FindParent<Grid>(), 8);
}
private void ItemNameTextBox_BeforeTextChanging(TextBox textBox, TextBoxBeforeTextChangingEventArgs args)
{
if (IsRenamingItem)
{
_ = ValidateItemNameInputTextAsync(textBox, args, (showError) =>
{
FileNameTeachingTip.Visibility = showError ? Visibility.Visible : Visibility.Collapsed;
FileNameTeachingTip.IsOpen = showError;
});
}
}
protected override void EndRename(TextBox textBox)
{
if (textBox is not null && textBox.FindParent<Grid>() is FrameworkElement parent)
Grid.SetColumnSpan(parent, 1);
ListViewItem? listViewItem = FileList.ContainerFromItem(RenamingItem) as ListViewItem;
if (textBox is null || listViewItem is null)
{
// Navigating away, do nothing
}
else
{
TextBlock? textBlock = listViewItem.FindDescendant("ItemName") as TextBlock;
textBox.Visibility = Visibility.Collapsed;
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
listViewItem?.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 = (FrameworkElement)FocusManager.GetFocusedElement(MainWindow.Instance.Content.XamlRoot);
var isHeaderFocused = DependencyObjectHelpers.FindParent<DataGridHeader>(focusedElement) is not null;
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 && !e.KeyStatus.IsMenuKeyDown)
{
e.Handled = true;
if (ctrlPressed && !shiftPressed)
{
var folders = SelectedItems?.Where(file => file.PrimaryItemAttribute == StorageItemTypes.Folder);
if (folders?.Any() ?? false)
{
foreach (ListedItem folder in folders)
await NavigationHelpers.OpenPathInNewTab(folder.ItemPath);
}
}
else if (ctrlPressed && shiftPressed)
{
var selectedFolders = SelectedItems?.Where(item => item.PrimaryItemAttribute == StorageItemTypes.Folder);
if (selectedFolders?.Count() == 1)
{
NavigationHelpers.OpenInSecondaryPane(ParentShellPageInstance, selectedFolders.First());
}
}
else if (!ctrlPressed && !shiftPressed)
{
if (SelectedItems?.Any() ?? false)
{
foreach (var selectedItem in SelectedItems)
await OpenItem(selectedItem);
}
}
}
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.Down)
{
// Focus the first item in the file list if Header header has focus,
// or if there is only one item in the file list (#13774)
if (isHeaderFocused || FileList.Items.Count == 1)
{
var selectIndex = FileList.SelectedIndex < 0 ? 0 : FileList.SelectedIndex;
if (FileList.ContainerFromIndex(selectIndex) is ListViewItem item)
{
// Focus selected list item or first item
item.Focus(FocusState.Programmatic);
if (!IsItemSelected)
FileList.SelectedIndex = 0;
e.Handled = true;
}
}
}
}
protected override bool CanGetItemFromElement(object element)
=> element is ListViewItem;
private async Task ReloadItemIconsAsync()
{
if (ParentShellPageInstance is null)
return;
ParentShellPageInstance.ShellViewModel.CancelExtendedPropertiesLoading();
var filesAndFolders = ParentShellPageInstance.ShellViewModel.FilesAndFolders.ToList();
await Task.WhenAll(filesAndFolders.Select(listedItem =>
{
listedItem.ItemPropertiesInitialized = false;
if (FileList.ContainerFromItem(listedItem) is not null)
return ParentShellPageInstance.ShellViewModel.LoadExtendedItemPropertiesAsync(listedItem);
else
return Task.CompletedTask;
}));
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 = clickedItem?.DataContext as ListedItem;
if (item is null)
{
if (IsRenamingItem && RenamingItem is not null)
{
ListViewItem? listViewItem = FileList.ContainerFromItem(RenamingItem) as ListViewItem;
if (listViewItem is not null)
{
var textBox = listViewItem.FindDescendant("ItemNameTextBox") as TextBox;
if (textBox is not null)
await CommitRenameAsync(textBox);
}
}
else
{
// 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)clickedItem).Name == "ItemName")
{
CheckRenameDoubleClick(clickedItem.DataContext);
}
else if (IsRenamingItem && RenamingItem is not null)
{
ListViewItem? listViewItem = FileList.ContainerFromItem(RenamingItem) as ListViewItem;
if (listViewItem is not null)
{
var textBox = listViewItem.FindDescendant("ItemNameTextBox") as TextBox;
if (textBox is not null)
await CommitRenameAsync(textBox);
}
}
}
}
private async Task OpenItem(ListedItem item)
{
if (!Commands.OpenItem.IsExecutable)
{
// Fallback if the command is not executable. It occurs only when search is performed from the columns view.
var itemType = item.PrimaryItemAttribute == StorageItemTypes.Folder ? FilesystemItemType.Directory : FilesystemItemType.File;
await NavigationHelpers.OpenPath(item.ItemPath, ParentShellPageInstance, itemType);
}
else
{
await Commands.OpenItem.ExecuteAsync();
}
}
private async void FileList_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
// Skip opening selected items if the double tap doesn't capture an item
var originalElement = e.OriginalSource as FrameworkElement;
var dataContext = originalElement?.DataContext;
// Try to get the item from DataContext or from sender (ListView)
ListedItem? item = dataContext as ListedItem;
if (item == null && sender is ListView listView && listView.SelectedItem is ListedItem selectedItem)
item = selectedItem;
if (item != null && item.PrimaryItemAttribute == StorageItemTypes.File && !UserSettingsService.FoldersSettingsService.OpenFilesWithSingleClick.ShouldOpenWithSingleClick(e.PointerDeviceType))
await OpenItem(item);
else if (item != null && item.PrimaryItemAttribute == StorageItemTypes.Folder && !UserSettingsService.FoldersSettingsService.OpenFoldersWithSingleClick.ShouldOpenWithSingleClick(e.PointerDeviceType))
await OpenItem(item);
else if (item == null && UserSettingsService.FoldersSettingsService.DoubleClickToGoUp)
await Commands.NavigateUp.ExecuteAsync();
ResetRenameDoubleClick();
}
private void StackPanel_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 StackPanel);
while (item is not ListViewItem)
item = VisualTreeHelper.GetParent(item);
if (item is ListViewItem itemContainer)
itemContainer.ContextFlyout = ItemContextMenuFlyout;
}
private void Grid_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// This prevents the drag selection rectangle from appearing when resizing the columns
e.Handled = true;
}
private void GridSplitter_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
UpdateColumnLayout();
}
private void GridSplitter_PreviewKeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Left || e.Key == VirtualKey.Right)
{
UpdateColumnLayout();
FolderSettings.ColumnsViewModel = ColumnsViewModel;
}
}
private void UpdateColumnLayout()
{
ColumnsViewModel.IconColumn.UserLength = Column2.Width;
ColumnsViewModel.NameColumn.UserLength = Column3.Width;
// Git
ColumnsViewModel.GitStatusColumn.UserLength = GitStatusColumnDefinition.Width;
ColumnsViewModel.GitLastCommitDateColumn.UserLength = GitLastCommitDateColumnDefinition.Width;
ColumnsViewModel.GitLastCommitMessageColumn.UserLength = GitLastCommitMessageColumnDefinition.Width;
ColumnsViewModel.GitCommitAuthorColumn.UserLength = GitCommitAuthorColumnDefinition.Width;
ColumnsViewModel.GitLastCommitShaColumn.UserLength = GitLastCommitShaColumnDefinition.Width;
ColumnsViewModel.TagColumn.UserLength = Column4.Width;
ColumnsViewModel.PathColumn.UserLength = Column5.Width;
ColumnsViewModel.OriginalPathColumn.UserLength = Column6.Width;
ColumnsViewModel.DateDeletedColumn.UserLength = Column7.Width;
ColumnsViewModel.DateModifiedColumn.UserLength = Column8.Width;
ColumnsViewModel.DateCreatedColumn.UserLength = Column9.Width;
ColumnsViewModel.ItemTypeColumn.UserLength = Column10.Width;
ColumnsViewModel.SizeColumn.UserLength = Column11.Width;
ColumnsViewModel.StatusColumn.UserLength = Column12.Width;
}
private void RootGrid_SizeChanged(object? sender, SizeChangedEventArgs? e)
{
MaxWidthForRenameTextbox = Math.Max(0, RootGrid.ActualWidth - 80);
}
private void GridSplitter_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
this.ChangeCursor(InputSystemCursor.Create(InputSystemCursorShape.SizeWestEast));
}
private void GridSplitter_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
FolderSettings.ColumnsViewModel = ColumnsViewModel;
this.ChangeCursor(InputSystemCursor.Create(InputSystemCursorShape.Arrow));
}
private void GridSplitter_Loaded(object sender, RoutedEventArgs e)
{
(sender as UIElement)?.ChangeCursor(InputSystemCursor.Create(InputSystemCursorShape.SizeWestEast));
}
private void ToggleMenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
FolderSettings.ColumnsViewModel = ColumnsViewModel;
ParentShellPageInstance.ShellViewModel.EnabledGitProperties = GetEnabledGitProperties(ColumnsViewModel);
}
private void GridSplitter_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
var columnToResize = Grid.GetColumn(sender as Files.App.Controls.GridSplitter) / 2 + 1;
ResizeColumnToFit(columnToResize);
e.Handled = true;
}
private void SizeAllColumnsToFit_Click(object sender, RoutedEventArgs e)
{
// If there aren't items, do not make columns fit
if (!FileList.Items.Any())
return;
// For scalability, just count the # of public `ColumnViewModel` properties in ColumnsViewModel
int totalColumnCount = ColumnsViewModel.GetType().GetProperties().Count(prop => prop.PropertyType == typeof(DetailsLayoutColumnItem));
for (int columnIndex = 1; columnIndex <= totalColumnCount; columnIndex++)
ResizeColumnToFit(columnIndex);
}
private void ResizeColumnToFit(int columnToResize)
{
if (!FileList.Items.Any())
return;
var maxItemLength = columnToResize switch
{
1 => 40, // Check all items columns
2 => FileList.Items.Cast<ListedItem>().Select(x => x.Name?.Length ?? 0).Max(), // file name column
4 => FileList.Items.Cast<ListedItem>().Select(x => (x as IGitItem)?.GitLastCommitDateHumanized?.Length ?? 0).Max(), // git
5 => FileList.Items.Cast<ListedItem>().Select(x => (x as IGitItem)?.GitLastCommitMessage?.Length ?? 0).Max(), // git
6 => FileList.Items.Cast<ListedItem>().Select(x => (x as IGitItem)?.GitLastCommitAuthor?.Length ?? 0).Max(), // git
7 => FileList.Items.Cast<ListedItem>().Select(x => (x as IGitItem)?.GitLastCommitSha?.Length ?? 0).Max(), // git
8 => FileList.Items.Cast<ListedItem>().Select(x => x.FileTagsUI?.Sum(x => x?.Name?.Length ?? 0) ?? 0).Max(), // file tag column
9 => FileList.Items.Cast<ListedItem>().Select(x => x.ItemPath?.Length ?? 0).Max(), // path column
10 => FileList.Items.Cast<ListedItem>().Select(x => (x as RecycleBinItem)?.ItemOriginalPath?.Length ?? 0).Max(), // original path column
11 => FileList.Items.Cast<ListedItem>().Select(x => (x as RecycleBinItem)?.ItemDateDeleted?.Length ?? 0).Max(), // date deleted column
12 => FileList.Items.Cast<ListedItem>().Select(x => x.ItemDateModified?.Length ?? 0).Max(), // date modified column
13 => FileList.Items.Cast<ListedItem>().Select(x => x.ItemDateCreated?.Length ?? 0).Max(), // date created column
14 => FileList.Items.Cast<ListedItem>().Select(x => x.ItemType?.Length ?? 0).Max(), // item type column
15 => FileList.Items.Cast<ListedItem>().Select(x => x.FileSize?.Length ?? 0).Max(), // item size column
_ => 20 // cloud status column
};
// if called programmatically, the column could be hidden
// in this case, resizing doesn't need to be done at all
if (maxItemLength == 0)
return;
var columnSizeToFit = MeasureColumnEstimate(columnToResize, 5, maxItemLength);
if (columnSizeToFit > 1)
{
var column = columnToResize switch
{
2 => ColumnsViewModel.NameColumn,
3 => ColumnsViewModel.GitStatusColumn,
4 => ColumnsViewModel.GitLastCommitDateColumn,
5 => ColumnsViewModel.GitLastCommitMessageColumn,
6 => ColumnsViewModel.GitCommitAuthorColumn,
7 => ColumnsViewModel.GitLastCommitShaColumn,
8 => ColumnsViewModel.TagColumn,
9 => ColumnsViewModel.PathColumn,
10 => ColumnsViewModel.OriginalPathColumn,
11 => ColumnsViewModel.DateDeletedColumn,
12 => ColumnsViewModel.DateModifiedColumn,
13 => ColumnsViewModel.DateCreatedColumn,
14 => ColumnsViewModel.ItemTypeColumn,
15 => ColumnsViewModel.SizeColumn,
_ => ColumnsViewModel.StatusColumn
};
if (columnToResize == 2) // file name column
columnSizeToFit += 20;
var minFitLength = Math.Max(columnSizeToFit, column.NormalMinLength);
var maxFitLength = Math.Min(minFitLength + 36, column.NormalMaxLength); // 36 to account for SortIcon & padding
column.UserLength = new GridLength(maxFitLength, GridUnitType.Pixel);
}
FolderSettings.ColumnsViewModel = ColumnsViewModel;
}
private double MeasureColumnEstimate(int columnIndex, int measureItemsCount, int maxItemLength)
{
if (columnIndex == 15) // sync status
return maxItemLength;
if (columnIndex == 8) // file tag
return MeasureTagColumnEstimate(columnIndex);
return MeasureTextColumnEstimate(columnIndex, measureItemsCount, maxItemLength);
}
private double MeasureTagColumnEstimate(int columnIndex)
{
var grids = DependencyObjectHelpers
.FindChildren<Grid>(FileList.ItemsPanelRoot)
.Where(grid => IsCorrectColumn(grid, columnIndex));
// Get the list of stack panels with the most letters
var stackPanels = grids
.Select(DependencyObjectHelpers.FindChildren<StackPanel>)
.OrderByDescending(sps => sps.Select(sp => DependencyObjectHelpers.FindChildren<TextBlock>(sp).Select(tb => tb.Text.Length).Sum()).Sum())
.First()
.ToArray();
var mesuredSize = stackPanels.Select(x =>
{
x.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return x.DesiredSize.Width;
}).Sum();
if (stackPanels.Length >= 2)
mesuredSize += 4 * (stackPanels.Length - 1); // The spacing between the tags
return mesuredSize;
}
private double MeasureTextColumnEstimate(int columnIndex, int measureItemsCount, int maxItemLength)
{
var tbs = DependencyObjectHelpers
.FindChildren<TextBlock>(FileList.ItemsPanelRoot)
.Where(tb => IsCorrectColumn(tb, columnIndex));
// heuristic: usually, text with more letters are wider than shorter text with wider letters
// with this, we can calculate avg width using longest text(s) to avoid overshooting the width
var widthPerLetter = tbs
.OrderByDescending(x => x.Text.Length)
.Where(tb => !string.IsNullOrEmpty(tb.Text))
.Take(measureItemsCount)
.Select(tb =>
{
var sampleTb = new TextBlock { Text = tb.Text, FontSize = tb.FontSize, FontFamily = tb.FontFamily };
sampleTb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
return sampleTb.DesiredSize.Width / Math.Max(1, tb.Text.Length);
});
if (!widthPerLetter.Any())
return 0;
// Take weighted avg between mean and max since width is an estimate
var weightedAvg = (widthPerLetter.Average() + widthPerLetter.Max()) / 2;
return weightedAvg * maxItemLength;
}
private bool IsCorrectColumn(FrameworkElement element, int columnIndex)
{
int columnIndexFromName = element.Name switch
{
"ItemName" => 2,
"ItemGitStatusTextBlock" => 3,
"ItemGitLastCommitDateTextBlock" => 4,
"ItemGitLastCommitMessageTextBlock" => 5,
"ItemGitCommitAuthorTextBlock" => 6,
"ItemGitLastCommitShaTextBlock" => 7,
"ItemTagGrid" => 8,
"ItemPath" => 9,
"ItemOriginalPath" => 10,
"ItemDateDeleted" => 11,
"ItemDateModified" => 12,
"ItemDateCreated" => 13,
"ItemType" => 14,
"ItemSize" => 15,
"ItemStatus" => 16,
_ => -1,
};
return columnIndexFromName != -1 && columnIndexFromName == columnIndex;
}
private void FileList_Loaded(object sender, RoutedEventArgs e)
{
ContentScroller = FileList.FindDescendant<ScrollViewer>(x => x.Name == "ScrollViewer");
const double OffsetCorrection = 88; // HeaderGrid (40) + ListViewHeaderItem (44 + 4 margin)
RootGridZoom.ViewChangeStarted += (_, args) =>
{
var scroller = ContentScroller;
if (args.IsSourceZoomedInView || scroller is null)
return;
void OnZoomScrolled(object? s, ScrollViewerViewChangedEventArgs ve)
{
scroller.ViewChanged -= OnZoomScrolled;
scroller.ChangeView(0, Math.Max(0, scroller.VerticalOffset - OffsetCorrection), null, true);
}
scroller.ViewChanged += OnZoomScrolled;
};
}
private void SetDetailsColumnsAsDefault_Click(object sender, RoutedEventArgs e)
{
LayoutPreferencesManager.SetDefaultLayoutPreferences(ColumnsViewModel);
}
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 ListViewItem);
selectionCheckbox.PointerEntered += SelectionCheckbox_PointerEntered;
selectionCheckbox.PointerExited += SelectionCheckbox_PointerExited;
selectionCheckbox.PointerCanceled += SelectionCheckbox_PointerCanceled;
}
private void SetCheckboxSelectionState(object item, ListViewItem? lviContainer = null)
{
var container = lviContainer ?? FileList.ContainerFromItem(item) as ListViewItem;
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 TagItem_Tapped(object sender, TappedRoutedEventArgs e)
{
var tagName = ((sender as StackPanel)?.Children[TAG_TEXT_BLOCK] as TextBlock)?.Text;
if (tagName is null)
return;
ParentShellPageInstance?.SubmitSearch(FolderSearch.FormatTagQuery(tagName));
}
private void FileTag_PointerEntered(object sender, PointerRoutedEventArgs e)
{
VisualStateManager.GoToState((UserControl)sender, "PointerOver", true);