Skip to content

Commit fc94a5d

Browse files
authored
Code Quality: Rich commands Set As... (#11563)
1 parent 9d17822 commit fc94a5d

12 files changed

+189
-56
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Files.App.Commands;
4+
using Files.App.Contexts;
5+
using Files.App.Extensions;
6+
using Files.App.Helpers;
7+
using System.ComponentModel;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
11+
namespace Files.App.Actions.Content.Background
12+
{
13+
internal class SetAsSlideshowBackgroundAction : ObservableObject, IAction
14+
{
15+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
16+
17+
public string Label { get; } = "SetAsSlideshow".GetLocalizedResource();
18+
19+
public RichGlyph Glyph { get; } = new RichGlyph("\uE91B");
20+
21+
public bool IsExecutable => IsContextPageTypeAdaptedToCommand() && context.SelectedItems.Count > 1;
22+
23+
public SetAsSlideshowBackgroundAction()
24+
{
25+
context.PropertyChanged += Context_PropertyChanged;
26+
}
27+
28+
public async Task ExecuteAsync()
29+
{
30+
if (context.ShellPage is not null)
31+
{
32+
var imagePaths = context.SelectedItems.Select(item => item.ItemPath).ToArray();
33+
WallpaperHelpers.SetSlideshow(imagePaths);
34+
}
35+
}
36+
37+
private bool IsContextPageTypeAdaptedToCommand()
38+
{
39+
return context.PageType is not ContentPageTypes.RecycleBin
40+
and not ContentPageTypes.ZipFolder
41+
and not ContentPageTypes.None;
42+
}
43+
44+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
45+
{
46+
switch (e.PropertyName)
47+
{
48+
case nameof(IContentPageContext.SelectedItems):
49+
if (IsContextPageTypeAdaptedToCommand())
50+
OnPropertyChanged(nameof(IsExecutable));
51+
break;
52+
}
53+
}
54+
}
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Files.App.Commands;
4+
using Files.App.Contexts;
5+
using Files.App.Extensions;
6+
using Files.App.Helpers;
7+
using Files.Shared.Enums;
8+
using System.ComponentModel;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
namespace Files.App.Actions.Content.Background
13+
{
14+
internal class SetAsWallpaperBackgroundAction : ObservableObject, IAction
15+
{
16+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
17+
18+
public string Label { get; } = "SetAsBackground".GetLocalizedResource();
19+
20+
public RichGlyph Glyph { get; } = new RichGlyph("\uE91B");
21+
22+
public bool IsExecutable => IsContextPageTypeAdaptedToCommand() && context.SelectedItems.Count == 1;
23+
24+
public SetAsWallpaperBackgroundAction()
25+
{
26+
context.PropertyChanged += Context_PropertyChanged;
27+
}
28+
29+
public async Task ExecuteAsync()
30+
{
31+
if (context.ShellPage is not null)
32+
WallpaperHelpers.SetAsBackground(WallpaperType.Desktop, context.SelectedItems.FirstOrDefault().ItemPath);
33+
}
34+
35+
private bool IsContextPageTypeAdaptedToCommand()
36+
{
37+
return context.PageType is not ContentPageTypes.RecycleBin
38+
and not ContentPageTypes.ZipFolder
39+
and not ContentPageTypes.None;
40+
}
41+
42+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
43+
{
44+
switch (e.PropertyName)
45+
{
46+
case nameof(IContentPageContext.SelectedItems):
47+
if (IsContextPageTypeAdaptedToCommand())
48+
OnPropertyChanged(nameof(IsExecutable));
49+
break;
50+
}
51+
}
52+
}
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using CommunityToolkit.Mvvm.ComponentModel;
2+
using CommunityToolkit.Mvvm.DependencyInjection;
3+
using Files.App.Contexts;
4+
using Files.App.Extensions;
5+
using Files.App.Helpers;
6+
using Files.Shared.Enums;
7+
using System.ComponentModel;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
11+
namespace Files.App.Actions.Content.Background
12+
{
13+
internal class SetAsLockscreenBackgroundAction : ObservableObject, IAction
14+
{
15+
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();
16+
17+
public string Label { get; } = "BaseLayoutItemContextFlyoutSetAsLockscreenBackground/Text".GetLocalizedResource();
18+
19+
public bool IsExecutable => IsContextPageTypeAdaptedToCommand() && context.SelectedItems.Count == 1;
20+
21+
public SetAsLockscreenBackgroundAction()
22+
{
23+
context.PropertyChanged += Context_PropertyChanged;
24+
}
25+
26+
public async Task ExecuteAsync()
27+
{
28+
if (context.ShellPage is not null)
29+
WallpaperHelpers.SetAsBackground(WallpaperType.LockScreen, context.SelectedItems.FirstOrDefault().ItemPath);
30+
}
31+
32+
private bool IsContextPageTypeAdaptedToCommand()
33+
{
34+
return context.PageType is not ContentPageTypes.RecycleBin
35+
and not ContentPageTypes.ZipFolder
36+
and not ContentPageTypes.None;
37+
}
38+
39+
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
40+
{
41+
switch (e.PropertyName)
42+
{
43+
case nameof(IContentPageContext.SelectedItems):
44+
if (IsContextPageTypeAdaptedToCommand())
45+
OnPropertyChanged(nameof(IsExecutable));
46+
break;
47+
}
48+
}
49+
}
50+
}

src/Files.App/Commands/CommandCodes.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,10 @@ public enum CommandCodes
3636
// Favorites
3737
PinItemToFavorites,
3838
UnpinItemFromFavorites,
39+
40+
// Backgrounds
41+
SetAsWallpaperBackground,
42+
SetAsSlideshowBackground,
43+
SetAsLockscreenBackground,
3944
}
4045
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using CommunityToolkit.Mvvm.ComponentModel;
22
using CommunityToolkit.Mvvm.Input;
33
using Files.App.Actions;
4+
using Files.App.Actions.Content.Background;
45
using Files.App.Actions.Favorites;
56
using Files.App.UserControls;
67
using Microsoft.UI.Xaml.Controls;
@@ -46,6 +47,9 @@ internal class CommandManager : ICommandManager
4647
public IRichCommand UnpinFromStart => commands[CommandCodes.UnpinFromStart];
4748
public IRichCommand PinItemToFavorites => commands[CommandCodes.PinItemToFavorites];
4849
public IRichCommand UnpinItemFromFavorites => commands[CommandCodes.UnpinItemFromFavorites];
50+
public IRichCommand SetAsWallpaperBackground => commands[CommandCodes.SetAsWallpaperBackground];
51+
public IRichCommand SetAsSlideshowBackground => commands[CommandCodes.SetAsSlideshowBackground];
52+
public IRichCommand SetAsLockscreenBackground => commands[CommandCodes.SetAsLockscreenBackground];
4953
public IRichCommand CopyItem => commands[CommandCodes.CopyItem];
5054
public IRichCommand CutItem => commands[CommandCodes.CutItem];
5155
public IRichCommand DeleteItem => commands[CommandCodes.DeleteItem];
@@ -86,6 +90,9 @@ public CommandManager()
8690
[CommandCodes.UnpinFromStart] = new UnpinFromStartAction(),
8791
[CommandCodes.PinItemToFavorites] = new PinItemAction(),
8892
[CommandCodes.UnpinItemFromFavorites] = new UnpinItemAction(),
93+
[CommandCodes.SetAsWallpaperBackground] = new SetAsWallpaperBackgroundAction(),
94+
[CommandCodes.SetAsSlideshowBackground] = new SetAsSlideshowBackgroundAction(),
95+
[CommandCodes.SetAsLockscreenBackground] = new SetAsLockscreenBackgroundAction(),
8996
[CommandCodes.CopyItem] = new CopyItemAction(),
9097
[CommandCodes.CutItem] = new CutItemAction(),
9198
[CommandCodes.DeleteItem] = new DeleteItemAction(),

src/Files.App/Commands/Manager/ICommandManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,9 @@ public interface ICommandManager : IEnumerable<IRichCommand>
3636
IRichCommand UnpinFromStart { get; }
3737
IRichCommand PinItemToFavorites { get; }
3838
IRichCommand UnpinItemFromFavorites { get; }
39+
40+
IRichCommand SetAsWallpaperBackground { get; }
41+
IRichCommand SetAsSlideshowBackground { get; }
42+
IRichCommand SetAsLockscreenBackground { get; }
3943
}
4044
}

src/Files.App/Helpers/ContextFlyoutItemHelper.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -678,28 +678,18 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
678678
ShowInSearchPage = true,
679679
Items = new List<ContextMenuFlyoutItemViewModel>()
680680
{
681-
new ContextMenuFlyoutItemViewModel()
681+
new ContextMenuFlyoutItemViewModelBuilder(commands.SetAsWallpaperBackground)
682682
{
683-
Text = "SetAsBackground".GetLocalizedResource(),
684-
Glyph = "\uE91B",
685-
Command = commandsViewModel.SetAsDesktopBackgroundItemCommand,
686-
ShowInSearchPage = true,
687-
ShowItem = selectedItemsPropertiesViewModel?.SelectedItemsCount == 1
688-
},
689-
new ContextMenuFlyoutItemViewModel()
683+
isVisible = selectedItemsPropertiesViewModel?.SelectedItemsCount == 1
684+
}.Build(),
685+
new ContextMenuFlyoutItemViewModelBuilder(commands.SetAsLockscreenBackground)
690686
{
691-
Text = "BaseLayoutItemContextFlyoutSetAsLockscreenBackground/Text".GetLocalizedResource(),
692-
Command = commandsViewModel.SetAsLockscreenBackgroundItemCommand,
693-
ShowInSearchPage = true,
694-
ShowItem = selectedItemsPropertiesViewModel?.SelectedItemsCount == 1
695-
},
696-
new ContextMenuFlyoutItemViewModel()
687+
isVisible = selectedItemsPropertiesViewModel?.SelectedItemsCount == 1
688+
}.Build(),
689+
new ContextMenuFlyoutItemViewModelBuilder(commands.SetAsSlideshowBackground)
697690
{
698-
Text = "SetAsSlideshow".GetLocalizedResource(),
699-
Command = commandsViewModel.SetAsDesktopBackgroundItemCommand,
700-
ShowInSearchPage = true,
701-
ShowItem = selectedItemsPropertiesViewModel?.SelectedItemsCount > 1
702-
},
691+
isVisible = selectedItemsPropertiesViewModel?.SelectedItemsCount > 1
692+
}.Build(),
703693
}
704694
},
705695
new ContextMenuFlyoutItemViewModel

src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,6 @@ public virtual void RenameItem(RoutedEventArgs e)
8686
itemManipulationModel.StartRenameItem();
8787
}
8888

89-
public virtual void SetAsLockscreenBackgroundItem(RoutedEventArgs e)
90-
{
91-
WallpaperHelpers.SetAsBackground(WallpaperType.LockScreen, SlimContentPage.SelectedItem.ItemPath);
92-
}
93-
94-
public virtual void SetAsDesktopBackgroundItem(RoutedEventArgs e)
95-
{
96-
WallpaperHelpers.SetAsBackground(WallpaperType.Desktop, SlimContentPage.SelectedItem.ItemPath);
97-
}
98-
99-
public virtual void SetAsSlideshowItem(RoutedEventArgs e)
100-
{
101-
var images = (from o in SlimContentPage.SelectedItems select o.ItemPath).ToArray();
102-
WallpaperHelpers.SetSlideshow(images);
103-
}
104-
10589
public virtual async void RunAsAdmin(RoutedEventArgs e)
10690
{
10791
await ContextMenu.InvokeVerb("runas", SlimContentPage.SelectedItem.ItemPath);

src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ public BaseLayoutCommandsViewModel(IBaseLayoutCommandImplementationModel command
2828
private void InitializeCommands()
2929
{
3030
RenameItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RenameItem);
31-
SetAsLockscreenBackgroundItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.SetAsLockscreenBackgroundItem);
32-
SetAsDesktopBackgroundItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.SetAsDesktopBackgroundItem);
33-
SetAsSlideshowItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.SetAsSlideshowItem);
3431
RunAsAdminCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RunAsAdmin);
3532
RunAsAnotherUserCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.RunAsAnotherUser);
3633
OpenItemCommand = new RelayCommand<RoutedEventArgs>(CommandsModel.OpenItem);
@@ -79,12 +76,6 @@ private void InitializeCommands()
7976

8077
public ICommand RenameItemCommand { get; private set; }
8178

82-
public ICommand SetAsLockscreenBackgroundItemCommand { get; private set; }
83-
84-
public ICommand SetAsDesktopBackgroundItemCommand { get; private set; }
85-
86-
public ICommand SetAsSlideshowItemCommand { get; private set; }
87-
8879
public ICommand RunAsAdminCommand { get; private set; }
8980

9081
public ICommand RunAsAnotherUserCommand { get; private set; }

src/Files.App/Interacts/IBaseLayoutCommandImplementationModel.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable
1111
{
1212
void RenameItem(RoutedEventArgs e);
1313

14-
void SetAsLockscreenBackgroundItem(RoutedEventArgs e);
15-
16-
void SetAsDesktopBackgroundItem(RoutedEventArgs e);
17-
18-
void SetAsSlideshowItem(RoutedEventArgs e);
19-
2014
void RunAsAdmin(RoutedEventArgs e);
2115

2216
void RunAsAnotherUser(RoutedEventArgs e);

src/Files.App/ViewModels/ToolbarViewModel.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using CommunityToolkit.Mvvm.Input;
44
using CommunityToolkit.WinUI;
55
using CommunityToolkit.WinUI.UI;
6+
using Files.App.Commands;
67
using Files.App.Extensions;
78
using Files.App.Filesystem;
89
using Files.App.Filesystem.StorageItems;
@@ -45,6 +46,8 @@ public class ToolbarViewModel : ObservableObject, IAddressToolbar, IDisposable
4546

4647
public IUpdateService UpdateService { get; } = Ioc.Default.GetService<IUpdateService>()!;
4748

49+
private static readonly ICommandManager commands = Ioc.Default.GetRequiredService<ICommandManager>();
50+
4851
public delegate void ToolbarPathItemInvokedEventHandler(object sender, PathNavigationEventArgs e);
4952

5053
public delegate void ToolbarFlyoutOpenedEventHandler(object sender, ToolbarFlyoutOpenedEventArgs e);
@@ -965,11 +968,11 @@ private void SearchRegion_Escaped(object? sender, ISearchBox searchBox)
965968

966969
public ICommand? RunWithPowerShellCommand { get; set; }
967970

968-
public ICommand? SetAsBackgroundCommand { get; set; }
971+
public IRichCommand SetAsBackgroundCommand => commands.SetAsWallpaperBackground;
969972

970-
public ICommand? SetAsLockscreenBackgroundCommand { get; set; }
973+
public IRichCommand SetAsLockscreenBackgroundCommand => commands.SetAsLockscreenBackground;
971974

972-
public ICommand? SetAsSlideshowCommand { get; set; }
975+
public IRichCommand SetAsSlideshowCommand => commands.SetAsSlideshowBackground;
973976

974977
public ICommand? InstallInfCommand { get; set; }
975978

src/Files.App/Views/BaseShellPage.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,6 @@ protected void InitToolbarCommands()
635635
ToolbarViewModel.Share = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShareItemCommand.Execute(null));
636636
ToolbarViewModel.RunWithPowerShellCommand = new RelayCommand(async () => await Win32Helpers.InvokeWin32ComponentAsync("powershell", this, PathNormalization.NormalizePath(SlimContentPage?.SelectedItem.ItemPath)));
637637
ToolbarViewModel.PropertiesCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.ShowPropertiesCommand.Execute(null));
638-
ToolbarViewModel.SetAsBackgroundCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.SetAsDesktopBackgroundItemCommand.Execute(null));
639-
ToolbarViewModel.SetAsLockscreenBackgroundCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.SetAsLockscreenBackgroundItemCommand.Execute(null));
640-
ToolbarViewModel.SetAsSlideshowCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.SetAsSlideshowItemCommand.Execute(null));
641638
ToolbarViewModel.ExtractCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DecompressArchiveCommand.Execute(null));
642639
ToolbarViewModel.ExtractHereCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DecompressArchiveHereCommand.Execute(null));
643640
ToolbarViewModel.ExtractToCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DecompressArchiveToChildFolderCommand.Execute(null));

0 commit comments

Comments
 (0)