Skip to content

Commit 2f4bce7

Browse files
Code quality: Refactoring and improvements (#10948)
1 parent be7a63e commit 2f4bce7

16 files changed

+580
-822
lines changed

src/Files.App/BaseLayout.cs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public bool IsMiddleClickToScrollEnabled
9898

9999
protected AddressToolbar? NavToolbar => (App.Window.Content as Frame)?.FindDescendant<AddressToolbar>();
100100

101-
private CollectionViewSource collectionViewSource = new CollectionViewSource()
101+
private CollectionViewSource collectionViewSource = new()
102102
{
103103
IsSourceGrouped = true,
104104
};
@@ -150,10 +150,7 @@ public string JumpString
150150
if (value != string.Empty)
151151
{
152152
ListedItem? jumpedToItem = null;
153-
ListedItem? previouslySelectedItem = null;
154-
155-
if (IsItemSelected)
156-
previouslySelectedItem = SelectedItem;
153+
ListedItem? previouslySelectedItem = IsItemSelected ? SelectedItem : null;
157154

158155
// Select first matching item after currently selected item
159156
if (previouslySelectedItem is not null)
@@ -207,7 +204,7 @@ internal set
207204
// check if the preview pane is open before updating the model
208205
if (PreviewPaneViewModel.IsEnabled)
209206
{
210-
bool isPaneEnabled = ((App.Window.Content as Frame)?.Content as MainPage)?.ShouldPreviewPaneBeActive ?? false;
207+
var isPaneEnabled = ((App.Window.Content as Frame)?.Content as MainPage)?.ShouldPreviewPaneBeActive ?? false;
211208
if (isPaneEnabled)
212209
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
213210
}
@@ -222,7 +219,7 @@ internal set
222219
ResetRenameDoubleClick();
223220
UpdateSelectionSize();
224221
}
225-
else if (selectedItems != null)
222+
else if (selectedItems is not null)
226223
{
227224
IsItemSelected = true;
228225
SelectedItem = selectedItems.First();
@@ -307,15 +304,18 @@ private void JumpTimer_Tick(object sender, object e)
307304

308305
protected IEnumerable<ListedItem>? GetAllItems()
309306
{
310-
var items = CollectionViewSource.IsSourceGrouped ? // add all items from each group to the new list
311-
(CollectionViewSource.Source as BulkConcurrentObservableCollection<GroupedCollection<ListedItem>>)?.SelectMany(g => g) :
312-
CollectionViewSource.Source as IEnumerable<ListedItem>;
307+
var items = CollectionViewSource.IsSourceGrouped
308+
? (CollectionViewSource.Source as BulkConcurrentObservableCollection<GroupedCollection<ListedItem>>)?.SelectMany(g => g) // add all items from each group to the new list
309+
: CollectionViewSource.Source as IEnumerable<ListedItem>;
313310
return items ?? new List<ListedItem>();
314311
}
315312

316313
public virtual void ResetItemOpacity()
317314
{
318-
foreach (var item in GetAllItems())
315+
var items = GetAllItems();
316+
if (items is null)
317+
return;
318+
foreach (var item in items)
319319
{
320320
if (item is not null)
321321
item.Opacity = item.IsHiddenItem ? Constants.UI.DimItemOpacity : 1.0d;
@@ -324,8 +324,7 @@ public virtual void ResetItemOpacity()
324324

325325
protected ListedItem? GetItemFromElement(object element)
326326
{
327-
var item = element as ContentControl;
328-
if (item is null || !CanGetItemFromElement(element))
327+
if (element is not ContentControl item || !CanGetItemFromElement(element))
329328
return null;
330329

331330
return (item.DataContext as ListedItem) ?? (item.Content as ListedItem) ?? (ItemsControl.ItemFromContainer(item) as ListedItem);
@@ -384,14 +383,14 @@ protected override async void OnNavigatedTo(NavigationEventArgs eventArgs)
384383

385384
if (!navigationArguments.IsSearchResultPage)
386385
{
387-
string previousDir = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory;
386+
var previousDir = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory;
388387
await ParentShellPageInstance.FilesystemViewModel.SetWorkingDirectoryAsync(navigationArguments.NavPathParam);
389388

390389
// pathRoot will be empty on recycle bin path
391390
var workingDir = ParentShellPageInstance.FilesystemViewModel.WorkingDirectory ?? string.Empty;
392-
string pathRoot = GetPathRoot(workingDir);
391+
var pathRoot = GetPathRoot(workingDir);
393392

394-
bool isRecycleBin = workingDir.StartsWith(CommonPaths.RecycleBinPath, StringComparison.Ordinal);
393+
var isRecycleBin = workingDir.StartsWith(CommonPaths.RecycleBinPath, StringComparison.Ordinal);
395394
ParentShellPageInstance.InstanceViewModel.IsPageTypeRecycleBin = isRecycleBin;
396395

397396
// Can't go up from recycle bin
@@ -506,9 +505,7 @@ public async void ItemContextFlyout_Opening(object? sender, object e)
506505
try
507506
{
508507
if (!IsItemSelected && ((sender as CommandBarFlyout)?.Target as ListViewItem)?.Content is ListedItem li) // Workaround for item sometimes not getting selected
509-
{
510508
ItemManipulationModel.SetSelectedItem(li);
511-
}
512509
if (IsItemSelected)
513510
await LoadMenuItemsAsync();
514511
}
@@ -529,7 +526,7 @@ public async void BaseContextFlyout_Opening(object? sender, object e)
529526
shellContextMenuItemCancellationToken?.Cancel();
530527
shellContextMenuItemCancellationToken = new CancellationTokenSource();
531528
var shiftPressed = Microsoft.UI.Input.InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down);
532-
var items = ContextFlyoutItemHelper.GetBaseContextCommandsWithoutShellItems(currentInstanceViewModel: InstanceViewModel!, itemViewModel: ParentShellPageInstance!.FilesystemViewModel, commandsViewModel: CommandsViewModel!, shiftPressed: shiftPressed, false);
529+
var items = ContextFlyoutItemHelper.GetBaseContextCommandsWithoutShellItems(currentInstanceViewModel: InstanceViewModel!, itemViewModel: ParentShellPageInstance!.FilesystemViewModel, commandsViewModel: CommandsViewModel!, shiftPressed: shiftPressed);
533530
BaseContextMenuFlyout.PrimaryCommands.Clear();
534531
BaseContextMenuFlyout.SecondaryCommands.Clear();
535532
var (primaryElements, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(items);
@@ -540,7 +537,7 @@ public async void BaseContextFlyout_Opening(object? sender, object e)
540537

541538
if (!InstanceViewModel!.IsPageTypeSearchResults && !InstanceViewModel.IsPageTypeZipFolder)
542539
{
543-
var shellMenuItems = await ContextFlyoutItemHelper.GetBaseContextShellCommandsAsync(currentInstanceViewModel: InstanceViewModel, workingDir: ParentShellPageInstance.FilesystemViewModel.WorkingDirectory, shiftPressed: shiftPressed, showOpenMenu: false, shellContextMenuItemCancellationToken.Token);
540+
var shellMenuItems = await ContextFlyoutItemHelper.GetBaseContextShellCommandsAsync(workingDir: ParentShellPageInstance.FilesystemViewModel.WorkingDirectory, shiftPressed: shiftPressed, showOpenMenu: false, shellContextMenuItemCancellationToken.Token);
544541
if (shellMenuItems.Any())
545542
AddShellItemsToMenu(shellMenuItems, BaseContextMenuFlyout, shiftPressed);
546543
}
@@ -556,7 +553,7 @@ public void UpdateSelectionSize()
556553
var items = (selectedItems?.Any() ?? false) ? selectedItems : GetAllItems();
557554
if (items is null)
558555
return;
559-
bool isSizeKnown = !items.Any(item => string.IsNullOrEmpty(item.FileSize));
556+
var isSizeKnown = !items.Any(item => string.IsNullOrEmpty(item.FileSize));
560557
if (isSizeKnown)
561558
{
562559
long size = items.Sum(item => item.FileSizeBytes);
@@ -579,7 +576,7 @@ private async Task LoadMenuItemsAsync()
579576
shellContextMenuItemCancellationToken = new CancellationTokenSource();
580577
SelectedItemsPropertiesViewModel.CheckAllFileExtensions(SelectedItems!.Select(selectedItem => selectedItem?.FileExtension).ToList()!);
581578
var shiftPressed = Microsoft.UI.Input.InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down);
582-
var items = ContextFlyoutItemHelper.GetItemContextCommandsWithoutShellItems(currentInstanceViewModel: InstanceViewModel!, workingDir: ParentShellPageInstance!.FilesystemViewModel.WorkingDirectory, selectedItems: SelectedItems!, selectedItemsPropertiesViewModel: SelectedItemsPropertiesViewModel, commandsViewModel: CommandsViewModel!, shiftPressed: shiftPressed, showOpenMenu: false);
579+
var items = ContextFlyoutItemHelper.GetItemContextCommandsWithoutShellItems(currentInstanceViewModel: InstanceViewModel!, selectedItems: SelectedItems!, selectedItemsPropertiesViewModel: SelectedItemsPropertiesViewModel, commandsViewModel: CommandsViewModel!, shiftPressed: shiftPressed);
583580
ItemContextMenuFlyout.PrimaryCommands.Clear();
584581
ItemContextMenuFlyout.SecondaryCommands.Clear();
585582
var (primaryElements, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(items);
@@ -593,7 +590,7 @@ private async Task LoadMenuItemsAsync()
593590

594591
if (!InstanceViewModel.IsPageTypeZipFolder)
595592
{
596-
var shellMenuItems = await ContextFlyoutItemHelper.GetItemContextShellCommandsAsync(currentInstanceViewModel: InstanceViewModel, workingDir: ParentShellPageInstance.FilesystemViewModel.WorkingDirectory, selectedItems: SelectedItems!, shiftPressed: shiftPressed, showOpenMenu: false, shellContextMenuItemCancellationToken.Token);
593+
var shellMenuItems = await ContextFlyoutItemHelper.GetItemContextShellCommandsAsync(workingDir: ParentShellPageInstance.FilesystemViewModel.WorkingDirectory, selectedItems: SelectedItems!, shiftPressed: shiftPressed, showOpenMenu: false, shellContextMenuItemCancellationToken.Token);
597594
if (shellMenuItems.Any())
598595
AddShellItemsToMenu(shellMenuItems, ItemContextMenuFlyout, shiftPressed);
599596
}
@@ -708,9 +705,7 @@ private void AddShellItemsToMenu(List<ContextMenuFlyoutItemViewModel> shellMenuI
708705
flyout.Items.Clear();
709706

710707
foreach (var item in openWithSubItems)
711-
{
712708
flyout.Items.Add(item);
713-
}
714709

715710
openWithOverflow.Flyout = flyout;
716711
openWith.Visibility = Visibility.Collapsed;
@@ -1079,12 +1074,11 @@ private void UpdateCollectionViewSource()
10791074

10801075
protected void SemanticZoom_ViewChangeStarted(object sender, SemanticZoomViewChangedEventArgs e)
10811076
{
1082-
if (!e.IsSourceZoomedInView)
1083-
{
1084-
// According to the docs this isn't necessary, but it would crash otherwise
1085-
var destination = e.DestinationItem.Item as GroupedCollection<ListedItem>;
1086-
e.DestinationItem.Item = destination?.FirstOrDefault();
1087-
}
1077+
if (e.IsSourceZoomedInView)
1078+
return;
1079+
// According to the docs this isn't necessary, but it would crash otherwise
1080+
var destination = e.DestinationItem.Item as GroupedCollection<ListedItem>;
1081+
e.DestinationItem.Item = destination?.FirstOrDefault();
10881082
}
10891083

10901084
protected void StackPanel_PointerEntered(object sender, PointerRoutedEventArgs e)
@@ -1110,7 +1104,10 @@ protected void RootPanel_PointerPressed(object sender, PointerRoutedEventArgs e)
11101104

11111105
private void ItemManipulationModel_RefreshItemsOpacityInvoked(object? sender, EventArgs e)
11121106
{
1113-
foreach (ListedItem listedItem in GetAllItems())
1107+
var items = GetAllItems();
1108+
if (items is null)
1109+
return;
1110+
foreach (ListedItem listedItem in items)
11141111
{
11151112
if (listedItem.IsHiddenItem)
11161113
listedItem.Opacity = Constants.UI.DimItemOpacity;

src/Files.App/DataModels/SidebarPinnedModel.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Files.App.DataModels
2222
{
2323
public class SidebarPinnedModel
2424
{
25-
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
25+
private IUserSettingsService userSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
2626
private IQuickAccessService QuickAccessService { get; } = Ioc.Default.GetRequiredService<IQuickAccessService>();
2727

2828
public EventHandler<NotifyCollectionChangedEventArgs>? DataChanged;
@@ -157,13 +157,9 @@ private void AddLocationItemToSidebar(LocationItem locationItem)
157157
/// </summary>
158158
public async Task AddAllItemsToSidebar()
159159
{
160-
if (UserSettingsService.PreferencesSettingsService.ShowFavoritesSection)
161-
{
160+
if (userSettingsService.PreferencesSettingsService.ShowFavoritesSection)
162161
foreach (string path in FavoriteItems)
163-
{
164162
await AddItemToSidebarAsync(path);
165-
}
166-
}
167163
}
168164

169165
/// <summary>

src/Files.App/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public async Task<IStorageHistory> RenameAsync(IStorageItemWithPath source, stri
581581
fsProgress.ReportStatus(FileSystemStatusCode.Success);
582582

583583
var renamedSources = renameResult.Items.Where(x => x.Succeeded && x.Destination is not null && x.Source != x.Destination)
584-
.Where(x => new[] { source }.Select(s => s.Path).Contains(x.Source));
584+
.Where(x => x.Source == source.Path);
585585
if (renamedSources.Any())
586586
{
587587
return new StorageHistory(FileOperationType.Rename, source,

0 commit comments

Comments
 (0)