Skip to content

Commit 8b9b6bf

Browse files
gumbarros0x5bfahishitetsu
authored
Code Quality: Apply CA1829 and CA1859 across the solution (#14970)
Co-authored-by: 0x5bfa <62196528+0x5bfa@users.noreply.github.com> Co-authored-by: hishitetsu <66369541+hishitetsu@users.noreply.github.com>
1 parent 6c762c4 commit 8b9b6bf

File tree

25 files changed

+63
-58
lines changed

25 files changed

+63
-58
lines changed

src/Files.App/Actions/Navigation/OpenDirectoryInNewTabAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public RichGlyph Glyph
2222
context.ShellPage is not null &&
2323
context.ShellPage.SlimContentPage is not null &&
2424
context.SelectedItems.Count <= 5 &&
25-
context.SelectedItems.Where(x => x.IsFolder == true).Count() == context.SelectedItems.Count &&
25+
context.SelectedItems.Count(x => x.IsFolder) == context.SelectedItems.Count &&
2626
userSettingsService.GeneralSettingsService.ShowOpenInNewTab;
2727

2828
public OpenDirectoryInNewTabAction()

src/Files.App/Actions/Navigation/OpenInNewWindowItemAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public RichGlyph Glyph
2727
context.ShellPage is not null &&
2828
context.ShellPage.SlimContentPage is not null &&
2929
context.SelectedItems.Count <= 5 &&
30-
context.SelectedItems.Where(x => x.IsFolder == true).Count() == context.SelectedItems.Count &&
30+
context.SelectedItems.Count(x => x.IsFolder) == context.SelectedItems.Count &&
3131
userSettingsService.GeneralSettingsService.ShowOpenInNewWindow;
3232

3333
public OpenInNewWindowItemAction()

src/Files.App/Data/Commands/HotKey/HotKey.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.Collections.Immutable;
4+
using System.Collections.Frozen;
55
using System.Runtime.InteropServices;
66
using System.Text;
77
using Forms = System.Windows.Forms;
@@ -11,15 +11,15 @@ namespace Files.App.Data.Commands
1111
[DebuggerDisplay("{Code}")]
1212
public readonly struct HotKey : IEquatable<HotKey>
1313
{
14-
private static readonly IImmutableDictionary<KeyModifiers, string> modifiers = new Dictionary<KeyModifiers, string>()
14+
private static readonly FrozenDictionary<KeyModifiers, string> modifiers = new Dictionary<KeyModifiers, string>()
1515
{
1616
[KeyModifiers.Menu] = GetKeyString("Menu"),
1717
[KeyModifiers.Ctrl] = GetKeyString("Control"),
1818
[KeyModifiers.Shift] = GetKeyString("Shift"),
1919
[KeyModifiers.Win] = GetKeyString("Windows"),
20-
}.ToImmutableDictionary();
20+
}.ToFrozenDictionary();
2121

22-
private static readonly IImmutableDictionary<Keys, string> keys = new Dictionary<Keys, string>()
22+
private static readonly FrozenDictionary<Keys, string> keys = new Dictionary<Keys, string>()
2323
{
2424
[Keys.Enter] = GetKeyString("Enter"),
2525
[Keys.Space] = GetKeyString("Space"),
@@ -151,7 +151,7 @@ namespace Files.App.Data.Commands
151151
[Keys.Mute] = GetKeyString("MediaMute"),
152152
[Keys.VolumeDown] = GetKeyString("MediaVolumeDown"),
153153
[Keys.VolumeUp] = GetKeyString("MediaVolumeUp"),
154-
}.ToImmutableDictionary();
154+
}.ToFrozenDictionary();
155155

156156
public static HotKey None { get; } = new(Keys.None, KeyModifiers.None);
157157

src/Files.App/Data/Commands/Manager/CommandManager.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using System.Collections.Frozen;
5+
using System.Collections.Immutable;
46
using Files.App.Actions;
57
using Microsoft.AppCenter.Analytics;
68
using Microsoft.UI.Xaml;
79
using Microsoft.UI.Xaml.Controls;
810
using Microsoft.UI.Xaml.Input;
9-
using System.Collections.Immutable;
1011

1112
namespace Files.App.Data.Commands
1213
{
1314
internal sealed class CommandManager : ICommandManager
1415
{
1516
private readonly IGeneralSettingsService settings = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
1617

17-
private readonly IImmutableDictionary<CommandCodes, IRichCommand> commands;
18-
private IImmutableDictionary<HotKey, IRichCommand> hotKeys = new Dictionary<HotKey, IRichCommand>().ToImmutableDictionary();
18+
private readonly FrozenDictionary<CommandCodes, IRichCommand> commands;
19+
private ImmutableDictionary<HotKey, IRichCommand> hotKeys = new Dictionary<HotKey, IRichCommand>().ToImmutableDictionary();
1920

2021
public IRichCommand this[CommandCodes code] => commands.TryGetValue(code, out var command) ? command : None;
2122
public IRichCommand this[string code]
@@ -196,16 +197,18 @@ public CommandManager()
196197
.Select(action => new ActionCommand(this, action.Key, action.Value))
197198
.Cast<IRichCommand>()
198199
.Append(new NoneCommand())
199-
.ToImmutableDictionary(command => command.Code);
200+
.ToFrozenDictionary(command => command.Code);
200201

201202
settings.PropertyChanged += Settings_PropertyChanged;
202203
UpdateHotKeys();
203204
}
204205

205206
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
206-
public IEnumerator<IRichCommand> GetEnumerator() => commands.Values.GetEnumerator();
207207

208-
private static IDictionary<CommandCodes, IAction> CreateActions() => new Dictionary<CommandCodes, IAction>
208+
public IEnumerator<IRichCommand> GetEnumerator() =>
209+
(commands.Values as IEnumerable<IRichCommand>).GetEnumerator();
210+
211+
private static Dictionary<CommandCodes, IAction> CreateActions() => new Dictionary<CommandCodes, IAction>
209212
{
210213
[CommandCodes.OpenHelp] = new OpenHelpAction(),
211214
[CommandCodes.ToggleFullScreen] = new ToggleFullScreenAction(),
@@ -362,7 +365,7 @@ public CommandManager()
362365

363366
private void UpdateHotKeys()
364367
{
365-
ISet<HotKey> useds = new HashSet<HotKey>();
368+
var useds = new HashSet<HotKey>();
366369

367370
var customs = new Dictionary<CommandCodes, HotKeyCollection>();
368371
foreach (var custom in settings.Actions)
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4+
using System.Collections.Frozen;
45
using System.Collections.Immutable;
56

67
namespace Files.App.Data.Commands
@@ -9,7 +10,7 @@ internal sealed class ModifiableCommandManager : IModifiableCommandManager
910
{
1011
private static readonly ICommandManager Commands = Ioc.Default.GetRequiredService<ICommandManager>();
1112

12-
private readonly IImmutableDictionary<CommandCodes, IRichCommand> ModifiableCommands;
13+
private readonly FrozenDictionary<CommandCodes, IRichCommand> ModifiableCommands;
1314

1415
public IRichCommand this[CommandCodes code] => ModifiableCommands.TryGetValue(code, out var command) ? command : None;
1516

@@ -19,13 +20,13 @@ internal sealed class ModifiableCommandManager : IModifiableCommandManager
1920

2021
public ModifiableCommandManager()
2122
{
22-
ModifiableCommands = CreateModifiableCommands().ToImmutableDictionary();
23+
ModifiableCommands = CreateModifiableCommands();
2324
}
2425

2526
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
26-
public IEnumerator<IRichCommand> GetEnumerator() => ModifiableCommands.Values.GetEnumerator();
27+
public IEnumerator<IRichCommand> GetEnumerator() => (ModifiableCommands.Values as IEnumerable<IRichCommand>).GetEnumerator();
2728

28-
private static IDictionary<CommandCodes, IRichCommand> CreateModifiableCommands() => new Dictionary<CommandCodes, IRichCommand>
29+
private static FrozenDictionary<CommandCodes, IRichCommand> CreateModifiableCommands() => new Dictionary<CommandCodes, IRichCommand>
2930
{
3031
[CommandCodes.None] = new NoneCommand(),
3132
[CommandCodes.PasteItem] = new ModifiableCommand(Commands.PasteItem, new() {
@@ -34,6 +35,6 @@ public ModifiableCommandManager()
3435
[CommandCodes.DeleteItem] = new ModifiableCommand(Commands.DeleteItem, new() {
3536
{ KeyModifiers.Shift, Commands.DeleteItemPermanently }
3637
}),
37-
};
38+
}.ToFrozenDictionary();
3839
}
3940
}

src/Files.App/Data/Contexts/HomePage/HomePageContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Files.App.Data.Contexts
99
{
1010
internal sealed class HomePageContext : ObservableObject, IHomePageContext
1111
{
12-
private static readonly IImmutableList<WidgetFileTagCardItem> emptyTaggedItems = Enumerable.Empty<WidgetFileTagCardItem>().ToImmutableList();
12+
private static readonly ImmutableList<WidgetFileTagCardItem> emptyTaggedItems = Enumerable.Empty<WidgetFileTagCardItem>().ToImmutableList();
1313

1414
public bool IsAnyItemRightClicked => rightClickedItem is not null;
1515

src/Files.App/Data/Factories/ContentPageContextFlyoutFactory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static List<ContextMenuFlyoutItemViewModel> Filter(List<ContextMenuFlyout
4040
items = items.Where(x => Check(item: x, currentInstanceViewModel: currentInstanceViewModel, selectedItems: selectedItems)).ToList();
4141
items.ForEach(x => x.Items = x.Items?.Where(y => Check(item: y, currentInstanceViewModel: currentInstanceViewModel, selectedItems: selectedItems)).ToList());
4242

43-
var overflow = items.Where(x => x.ID == "ItemOverflow").FirstOrDefault();
43+
var overflow = items.FirstOrDefault(x => x.ID == "ItemOverflow");
4444
if (overflow is not null)
4545
{
4646
if (!shiftPressed && UserSettingsService.GeneralSettingsService.MoveShellExtensionsToSubMenu) // items with ShowOnShift to overflow menu
@@ -664,8 +664,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetNewItemItems(BaseLayoutVie
664664
public static void SwapPlaceholderWithShellOption(CommandBarFlyout contextMenu, string placeholderName, ContextMenuFlyoutItemViewModel? replacingItem, int position)
665665
{
666666
var placeholder = contextMenu.SecondaryCommands
667-
.Where(x => Equals((x as AppBarButton)?.Tag, placeholderName))
668-
.FirstOrDefault() as AppBarButton;
667+
.FirstOrDefault(x => Equals((x as AppBarButton)?.Tag, placeholderName)) as AppBarButton;
669668

670669
if (placeholder is not null)
671670
placeholder.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;

src/Files.App/Data/Factories/PropertiesNavigationViewItemFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public static ObservableCollection<NavigationViewItemButtonStyleItem> Initialize
7474

7575
if (item is List<ListedItem> listedItems)
7676
{
77-
var commonFileExt = listedItems.Select(x => x.FileExtension).Distinct().Count() == 1 ? listedItems.First().FileExtension : null;
77+
var firstFileExtension = listedItems.FirstOrDefault()?.FileExtension;
78+
var commonFileExt = listedItems.All(x => x.FileExtension == firstFileExtension) ? firstFileExtension : null;
7879
var compatibilityItemEnabled = listedItems.All(listedItem => FileExtensionHelpers.IsExecutableFile(listedItem is ShortcutItem sht ? sht.TargetPath : commonFileExt, true));
7980
var onlyFiles = listedItems.All(listedItem => listedItem.PrimaryItemAttribute == StorageItemTypes.File || listedItem.IsArchive);
8081

src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ private static void LoadMenuFlyoutItem(
8080
var menuItems = menuFlyoutItems.TakeWhile(x => x.Type == MenuItemType.MFT_SEPARATOR || ++itemsCount <= itemsBeforeOverflow).ToList();
8181
var overflowItems = menuFlyoutItems.Except(menuItems).ToList();
8282

83-
if (overflowItems.Where(x => x.Type != MenuItemType.MFT_SEPARATOR).Any())
83+
if (overflowItems.Any(x => x.Type != MenuItemType.MFT_SEPARATOR))
8484
{
85-
var moreItem = menuItemsListLocal.Where(x => x.ID == "ItemOverflow").FirstOrDefault();
85+
var moreItem = menuItemsListLocal.FirstOrDefault(x => x.ID == "ItemOverflow");
8686
if (moreItem is null)
8787
{
8888
var menuLayoutSubItem = new ContextMenuFlyoutItemViewModel()
@@ -341,7 +341,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
341341
OpacityIconStyle = "ColorIconOpenWith",
342342
};
343343
var (_, openWithItems) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { openWithItem });
344-
var placeholder = itemContextMenuFlyout.SecondaryCommands.Where(x => Equals((x as AppBarButton)?.Tag, "OpenWithPlaceholder")).FirstOrDefault() as AppBarButton;
344+
var placeholder = itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => Equals((x as AppBarButton)?.Tag, "OpenWithPlaceholder")) as AppBarButton;
345345
if (placeholder is not null)
346346
placeholder.Visibility = Visibility.Collapsed;
347347
itemContextMenuFlyout.SecondaryCommands.Insert(0, openWithItems.FirstOrDefault());
@@ -353,7 +353,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
353353
await sendToItem.LoadSubMenuAction();
354354

355355
var (_, sendToItems) = ContextFlyoutModelToElementHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { sendToItem });
356-
var placeholder = itemContextMenuFlyout.SecondaryCommands.Where(x => Equals((x as AppBarButton)?.Tag, "SendToPlaceholder")).FirstOrDefault() as AppBarButton;
356+
var placeholder = itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => Equals((x as AppBarButton)?.Tag, "SendToPlaceholder")) as AppBarButton;
357357
if (placeholder is not null)
358358
placeholder.Visibility = Visibility.Collapsed;
359359
itemContextMenuFlyout.SecondaryCommands.Insert(1, sendToItems.FirstOrDefault());

src/Files.App/Data/Models/ItemViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ public async Task LoadExtendedItemPropertiesAsync(ListedItem item)
10601060
BaseStorageFile? matchingStorageFile = null;
10611061
if (item.Key is not null && FilesAndFolders.IsGrouped && FilesAndFolders.GetExtendedGroupHeaderInfo is not null)
10621062
{
1063-
gp = FilesAndFolders.GroupedCollection?.ToList().Where(x => x.Model.Key == item.Key).FirstOrDefault();
1063+
gp = FilesAndFolders.GroupedCollection?.ToList().FirstOrDefault(x => x.Model.Key == item.Key);
10641064
loadGroupHeaderInfo = gp is not null && !gp.Model.Initialized && gp.GetExtendedGroupHeaderInfo is not null;
10651065
}
10661066

0 commit comments

Comments
 (0)