Skip to content

Code Quality: Improved the way of instancing OpacityIconModel #14603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ public ContextMenuFlyoutItemViewModel Build()
var glyph = command.Glyph;
if (!string.IsNullOrEmpty(glyph.OpacityStyle))
{
viewModel.OpacityIcon = new OpacityIconModel
{
OpacityIconStyle = glyph.OpacityStyle,
};
viewModel.OpacityIcon = new(glyph.OpacityStyle);
}
else
{
Expand Down
20 changes: 14 additions & 6 deletions src/Files.App/Data/Models/OpacityIconModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@

namespace Files.App.Data.Models
{
public struct OpacityIconModel
/// <summary>
/// Represents a model for <see cref="OpacityIcon"/>.
/// </summary>
public class OpacityIconModel
{
public string OpacityIconStyle { get; set; }
public string? OpacityIconStyle { get; set; }

public readonly OpacityIcon ToOpacityIcon()
public bool IsValid
=> !string.IsNullOrWhiteSpace(OpacityIconStyle);

public OpacityIconModel(string styleName)
{
OpacityIconStyle = styleName;
}

public OpacityIcon ToOpacityIcon()
{
return new()
{
Style = (Style)Application.Current.Resources[OpacityIconStyle],
};
}

public readonly bool IsValid
=> !string.IsNullOrEmpty(OpacityIconStyle);
}
}
25 changes: 5 additions & 20 deletions src/Files.App/Helpers/MenuFlyout/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
new ContextMenuFlyoutItemViewModel()
{
Text = "SortBy".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconSort",
},
OpacityIcon = new("ColorIconSort"),
ShowItem = !itemsSelected,
ShowInRecycleBin = true,
ShowInSearchPage = true,
Expand Down Expand Up @@ -355,10 +352,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
},
new ContextMenuFlyoutItemViewModel()
{
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = commands.AddItem.Glyph.OpacityStyle
},
OpacityIcon = new(commands.AddItem.Glyph.OpacityStyle),
Text = commands.AddItem.Label,
Items = GetNewItemItems(commandsViewModel, currentInstanceViewModel.CanCreateFileInPage),
ShowItem = !itemsSelected,
Expand All @@ -385,10 +379,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenWith".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenWith"
},
OpacityIcon = new("ColorIconOpenWith"),
Tag = "OpenWithOverflow",
IsHidden = true,
CollapseLabel = true,
Expand Down Expand Up @@ -512,10 +503,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
{
Text = "Compress".GetLocalizedResource(),
ShowInSearchPage = true,
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconZip",
},
OpacityIcon = new("ColorIconZip"),
Items = new List<ContextMenuFlyoutItemViewModel>
{
new ContextMenuFlyoutItemViewModelBuilder(commands.CompressIntoArchive).Build(),
Expand All @@ -528,10 +516,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
{
Text = "Extract".GetLocalizedResource(),
ShowInSearchPage = true,
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconZip",
},
OpacityIcon = new("ColorIconZip"),
Items = new List<ContextMenuFlyoutItemViewModel>
{
new ContextMenuFlyoutItemViewModelBuilder(commands.DecompressArchive).Build(),
Expand Down
5 changes: 1 addition & 4 deletions src/Files.App/Helpers/MenuFlyout/ShellContextMenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,7 @@ x.Tag is Win32ContextMenuItem menuItem &&
{
await openWithItem.LoadSubMenuAction();

openWithItem.OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenWith",
};
openWithItem.OpacityIcon = new("ColorIconOpenWith");
var (_, openWithItems) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(new List<ContextMenuFlyoutItemViewModel>() { openWithItem });
var placeholder = itemContextMenuFlyout.SecondaryCommands.Where(x => Equals((x as AppBarButton)?.Tag, "OpenWithPlaceholder")).FirstOrDefault() as AppBarButton;
if (placeholder is not null)
Expand Down
25 changes: 5 additions & 20 deletions src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,15 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewTab".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewTab",
},
OpacityIcon = new("ColorIconOpenInNewTab"),
Command = OpenInNewTabCommand,
CommandParameter = item,
ShowItem = userSettingsService.GeneralSettingsService.ShowOpenInNewTab
},
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewWindow".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewWindow",
},
OpacityIcon = new("ColorIconOpenInNewWindow"),
Command = OpenInNewWindowCommand,
CommandParameter = item,
ShowItem = userSettingsService.GeneralSettingsService.ShowOpenInNewWindow
Expand All @@ -188,21 +182,15 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
},
OpacityIcon = new("ColorIconPinToFavorites"),
Command = PinToFavoritesCommand,
CommandParameter = item,
ShowItem = !isPinned
},
new ContextMenuFlyoutItemViewModel()
{
Text = "UnpinFromFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconUnpinFromFavorites",
},
OpacityIcon = new("ColorIconUnpinFromFavorites"),
Command = UnpinFromFavoritesCommand,
CommandParameter = item,
ShowItem = isPinned
Expand All @@ -224,10 +212,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "Properties".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconProperties",
},
OpacityIcon = new("ColorIconProperties"),
Command = OpenPropertiesCommand,
CommandParameter = item
},
Expand Down
30 changes: 6 additions & 24 deletions src/Files.App/UserControls/Widgets/FileTagsWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenWith".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenWith",
},
OpacityIcon = new("ColorIconOpenWith"),
Tag = "OpenWithPlaceholder",
ShowItem = !isFolder
},
Expand All @@ -171,21 +168,15 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewTab".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewTab",
},
OpacityIcon = new("ColorIconOpenInNewTab"),
Command = OpenInNewTabCommand,
CommandParameter = item,
ShowItem = isFolder
},
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewWindow".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewWindow",
},
OpacityIcon = new("ColorIconOpenInNewWindow"),
Command = OpenInNewWindowCommand,
CommandParameter = item,
ShowItem = isFolder
Expand All @@ -208,32 +199,23 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
},
OpacityIcon = new("ColorIconPinToFavorites"),
Command = PinToFavoritesCommand,
CommandParameter = item,
ShowItem = !isPinned && isFolder
},
new ContextMenuFlyoutItemViewModel()
{
Text = "UnpinFromFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconUnpinFromFavorites",
},
OpacityIcon = new("ColorIconUnpinFromFavorites"),
Command = UnpinFromFavoritesCommand,
CommandParameter = item,
ShowItem = isPinned && isFolder
},
new ContextMenuFlyoutItemViewModel()
{
Text = "Properties".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconProperties",
},
OpacityIcon = new("ColorIconProperties"),
Command = OpenPropertiesCommand,
CommandParameter = item,
ShowItem = isFolder
Expand Down
25 changes: 5 additions & 20 deletions src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,15 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewTab".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewTab",
},
OpacityIcon = new("ColorIconOpenInNewTab"),
Command = OpenInNewTabCommand,
CommandParameter = item,
ShowItem = userSettingsService.GeneralSettingsService.ShowOpenInNewTab
},
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewWindow".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewWindow",
},
OpacityIcon = new("ColorIconOpenInNewWindow"),
Command = OpenInNewWindowCommand,
CommandParameter = item,
ShowItem = userSettingsService.GeneralSettingsService.ShowOpenInNewWindow
Expand All @@ -178,32 +172,23 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
},
OpacityIcon = new("ColorIconPinToFavorites"),
Command = PinToFavoritesCommand,
CommandParameter = item,
ShowItem = !isPinned
},
new ContextMenuFlyoutItemViewModel()
{
Text = "UnpinFromFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconUnpinFromFavorites",
},
OpacityIcon = new("ColorIconUnpinFromFavorites"),
Command = UnpinFromFavoritesCommand,
CommandParameter = item,
ShowItem = isPinned
},
new ContextMenuFlyoutItemViewModel()
{
Text = "Properties".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconProperties",
},
OpacityIcon = new("ColorIconProperties"),
Command = OpenPropertiesCommand,
CommandParameter = item
},
Expand Down
10 changes: 2 additions & 8 deletions src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenWith".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenWith",
},
OpacityIcon = new("ColorIconOpenWith"),
Tag = "OpenWithPlaceholder",
},
new ContextMenuFlyoutItemViewModel()
Expand Down Expand Up @@ -193,10 +190,7 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
new ContextMenuFlyoutItemViewModel()
{
Text = "Properties".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconProperties",
},
OpacityIcon = new("ColorIconProperties"),
Command = OpenPropertiesCommand,
CommandParameter = item
},
Expand Down
25 changes: 5 additions & 20 deletions src/Files.App/ViewModels/UserControls/SidebarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,20 +977,14 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewTab".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewTab",
},
OpacityIcon = new("ColorIconOpenInNewTab"),
Command = OpenInNewTabCommand,
ShowItem = options.IsLocationItem && UserSettingsService.GeneralSettingsService.ShowOpenInNewTab
},
new ContextMenuFlyoutItemViewModel()
{
Text = "OpenInNewWindow".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconOpenInNewWindow",
},
OpacityIcon = new("ColorIconOpenInNewWindow"),
Command = OpenInNewWindowCommand,
ShowItem = options.IsLocationItem && UserSettingsService.GeneralSettingsService.ShowOpenInNewTab
},
Expand All @@ -1003,20 +997,14 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
new ContextMenuFlyoutItemViewModel()
{
Text = "PinToFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconPinToFavorites",
},
OpacityIcon = new("ColorIconPinToFavorites"),
Command = PinItemCommand,
ShowItem = isDriveItem && !isDriveItemPinned
},
new ContextMenuFlyoutItemViewModel()
{
Text = "UnpinFromFavorites".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconUnpinFromFavorites",
},
OpacityIcon = new("ColorIconUnpinFromFavorites"),
Command = UnpinItemCommand,
ShowItem = options.ShowUnpinItem || isDriveItemPinned
},
Expand Down Expand Up @@ -1050,10 +1038,7 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
new ContextMenuFlyoutItemViewModel()
{
Text = "Properties".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconProperties",
},
OpacityIcon = new("ColorIconProperties"),
Command = OpenPropertiesCommand,
CommandParameter = menu,
ShowItem = options.ShowProperties
Expand Down