Skip to content

Rich Commands: Rotate image as Rich Commands #11633

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 3 commits into from
Mar 9, 2023
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
48 changes: 48 additions & 0 deletions src/Files.App/Actions/Content/ImageEdition/RotateLeftAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using Files.App.ViewModels;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;

namespace Files.App.Actions.Content.ImageEdition
{
internal class RotateLeftAction : ObservableObject, IAction
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public string Label { get; } = "RotateLeft".GetLocalizedResource();

public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconRotateLeft");

public bool IsExecutable => context.ShellPage.SlimContentPage.SelectedItemsPropertiesViewModel.IsSelectedItemImage;

public RotateLeftAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public async Task ExecuteAsync()
{
foreach (var image in context.SelectedItems)
await BitmapHelper.Rotate(PathNormalization.NormalizePath(image.ItemPath), BitmapRotation.Clockwise270Degrees);

context.ShellPage.SlimContentPage.ItemManipulationModel.RefreshItemsThumbnail();
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
}

public void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.HasSelection))
{
context.ShellPage.SlimContentPage.SelectedItemsPropertiesViewModel.CheckAllFileExtensions(context.SelectedItems.Select(selectedItem => selectedItem?.FileExtension).ToList<string>());
OnPropertyChanged(nameof(IsExecutable));
}
}
}
}
47 changes: 47 additions & 0 deletions src/Files.App/Actions/Content/ImageEdition/RotateRightAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.App.Helpers;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;

namespace Files.App.Actions.Content.ImageEdition
{
internal class RotateRightAction : ObservableObject, IAction
{
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>();

public string Label { get; } = "RotateRight".GetLocalizedResource();

public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconRotateRight");

public bool IsExecutable => context.ShellPage.SlimContentPage.SelectedItemsPropertiesViewModel.IsSelectedItemImage;

public RotateRightAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public async Task ExecuteAsync()
{
foreach (var image in context.SelectedItems)
await BitmapHelper.Rotate(PathNormalization.NormalizePath(image.ItemPath), BitmapRotation.Clockwise90Degrees);

context.ShellPage.SlimContentPage.ItemManipulationModel.RefreshItemsThumbnail();
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
}

public void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.HasSelection))
{
context.ShellPage.SlimContentPage.SelectedItemsPropertiesViewModel.CheckAllFileExtensions(context.SelectedItems.Select(selectedItem => selectedItem?.FileExtension).ToList<string>());
OnPropertyChanged(nameof(IsExecutable));
}
}
}
}
8 changes: 6 additions & 2 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public enum CommandCodes
RunAsAdmin,
RunAsAnotherUser,

// Archives
// Archives
CompressIntoArchive,
CompressIntoSevenZip,
CompressIntoZip
CompressIntoZip,

// Image Edition
RotateLeft,
RotateRight
}
}
7 changes: 6 additions & 1 deletion src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Files.App.Actions;
using Files.App.Actions.Content.Archives;
using Files.App.Actions.Content.Background;
using Files.App.Actions.Content.ImageEdition;
using Files.App.Actions.Favorites;
using Files.App.UserControls;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -61,6 +62,8 @@ internal class CommandManager : ICommandManager
public IRichCommand CompressIntoArchive => commands[CommandCodes.CompressIntoArchive];
public IRichCommand CompressIntoSevenZip => commands[CommandCodes.CompressIntoSevenZip];
public IRichCommand CompressIntoZip => commands[CommandCodes.CompressIntoZip];
public IRichCommand RotateLeft => commands[CommandCodes.RotateLeft];
public IRichCommand RotateRight => commands[CommandCodes.RotateRight];

public CommandManager()
{
Expand Down Expand Up @@ -109,7 +112,9 @@ public CommandManager()
[CommandCodes.RunAsAnotherUser] = new RunAsAnotherUserAction(),
[CommandCodes.CompressIntoArchive] = new CompressIntoArchiveAction(),
[CommandCodes.CompressIntoSevenZip] = new CompressIntoSevenZipAction(),
[CommandCodes.CompressIntoZip] = new CompressIntoZipAction()
[CommandCodes.CompressIntoZip] = new CompressIntoZipAction(),
[CommandCodes.RotateLeft] = new RotateLeftAction(),
[CommandCodes.RotateRight] = new RotateRightAction()
};

[DebuggerDisplay("Command None")]
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand CompressIntoArchive { get; }
IRichCommand CompressIntoSevenZip { get; }
IRichCommand CompressIntoZip { get; }

IRichCommand RotateLeft { get; }
IRichCommand RotateRight { get; }
}
}
26 changes: 6 additions & 20 deletions src/Files.App/Helpers/ContextFlyoutItemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -692,28 +692,14 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
}.Build(),
}
},
new ContextMenuFlyoutItemViewModel
new ContextMenuFlyoutItemViewModelBuilder(commands.RotateLeft)
{
Text = "RotateLeft".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconRotateLeft"
},
Command = commandsViewModel.RotateImageLeftCommand,
ShowInSearchPage = true,
ShowItem = selectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false
},
new ContextMenuFlyoutItemViewModel
IsVisible = selectedItemsPropertiesViewModel.IsSelectedItemImage
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.RotateRight)
{
Text = "RotateRight".GetLocalizedResource(),
OpacityIcon = new OpacityIconModel()
{
OpacityIconStyle = "ColorIconRotateRight"
},
Command = commandsViewModel.RotateImageRightCommand,
ShowInSearchPage = true,
ShowItem = selectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false
},
IsVisible = selectedItemsPropertiesViewModel.IsSelectedItemImage
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.RunAsAdmin).Build(),
new ContextMenuFlyoutItemViewModelBuilder(commands.RunAsAnotherUser).Build(),
new ContextMenuFlyoutItemViewModel()
Expand Down
18 changes: 0 additions & 18 deletions src/Files.App/Interacts/BaseLayoutCommandImplementationModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,24 +598,6 @@ public async Task InstallInfDriver()
await Win32API.InstallInf(selectedItem.ItemPath);
}

public async Task RotateImageLeft()
{
foreach (var image in SlimContentPage.SelectedItems)
await BitmapHelper.Rotate(PathNormalization.NormalizePath(image.ItemPath), BitmapRotation.Clockwise270Degrees);

SlimContentPage.ItemManipulationModel.RefreshItemsThumbnail();
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
}

public async Task RotateImageRight()
{
foreach (var image in SlimContentPage.SelectedItems)
await BitmapHelper.Rotate(PathNormalization.NormalizePath(image.ItemPath), BitmapRotation.Clockwise90Degrees);

SlimContentPage.ItemManipulationModel.RefreshItemsThumbnail();
App.PreviewPaneViewModel.UpdateSelectedItemPreview();
}

public Task InstallFont()
{
foreach (ListedItem selectedItem in SlimContentPage.SelectedItems)
Expand Down
6 changes: 0 additions & 6 deletions src/Files.App/Interacts/BaseLayoutCommandsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ private void InitializeCommands()
DecompressArchiveHereCommand = new AsyncRelayCommand(CommandsModel.DecompressArchiveHere);
DecompressArchiveToChildFolderCommand = new AsyncRelayCommand(CommandsModel.DecompressArchiveToChildFolder);
InstallInfDriver = new AsyncRelayCommand(CommandsModel.InstallInfDriver);
RotateImageLeftCommand = new AsyncRelayCommand(CommandsModel.RotateImageLeft);
RotateImageRightCommand = new AsyncRelayCommand(CommandsModel.RotateImageRight);
InstallFontCommand = new AsyncRelayCommand(CommandsModel.InstallFont);
PlayAllCommand = new AsyncRelayCommand(CommandsModel.PlayAll);
FormatDriveCommand = new RelayCommand<ListedItem>(CommandsModel.FormatDrive);
Expand Down Expand Up @@ -116,10 +114,6 @@ private void InitializeCommands()

public ICommand InstallInfDriver { get; set; }

public ICommand RotateImageLeftCommand { get; private set; }

public ICommand RotateImageRightCommand { get; private set; }

public ICommand InstallFontCommand { get; private set; }

public ICommand PlayAllCommand { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ public interface IBaseLayoutCommandImplementationModel : IDisposable

Task InstallInfDriver();

Task RotateImageLeft();

Task RotateImageRight();

Task InstallFont();

Task PlayAll();
Expand Down
8 changes: 4 additions & 4 deletions src/Files.App/UserControls/InnerNavigationToolbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@
<AppBarButton
x:Name="RotateImageLeftButton"
x:Load="{x:Bind ViewModel.IsImage, Mode=OneWay, FallbackValue=False}"
Command="{x:Bind ViewModel.RotateImageLeftCommand, Mode=OneWay}"
Label="{helpers:ResourceString Name=RotateLeft}"
Command="{x:Bind Commands.RotateLeft, Mode=OneWay}"
Label="{x:Bind Commands.RotateLeft.Label}"
LabelPosition="Default"
ToolTipService.ToolTip="{helpers:ResourceString Name=RotateLeft}">

Expand All @@ -351,8 +351,8 @@
<AppBarButton
x:Name="RotateImageRightButton"
x:Load="{x:Bind ViewModel.IsImage, Mode=OneWay, FallbackValue=False}"
Command="{x:Bind ViewModel.RotateImageRightCommand, Mode=OneWay}"
Label="{helpers:ResourceString Name=RotateRight}"
Command="{x:Bind Commands.RotateRight, Mode=OneWay}"
Label="{x:Bind Commands.RotateRight.Label}"
LabelPosition="Default"
ToolTipService.ToolTip="{helpers:ResourceString Name=RotateRight}">

Expand Down
7 changes: 3 additions & 4 deletions src/Files.App/Views/BaseShellPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public abstract class BaseShellPage : Page, IShellPage, INotifyPropertyChanged

protected readonly IUpdateService updateSettingsService = Ioc.Default.GetRequiredService<IUpdateService>();

protected readonly ICommandManager commands = Ioc.Default.GetRequiredService<ICommandManager>();

public ToolbarViewModel ToolbarViewModel { get; } = new ToolbarViewModel();

public IBaseLayout SlimContentPage => ContentPage;

public ICommandManager commands = Ioc.Default.GetRequiredService<ICommandManager>();
public IFilesystemHelpers FilesystemHelpers { get; protected set; }

public Type CurrentPageType => ItemDisplay.SourcePageType;
Expand Down Expand Up @@ -638,9 +639,7 @@ protected void InitToolbarCommands()
ToolbarViewModel.ExtractCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DecompressArchiveCommand.Execute(null));
ToolbarViewModel.ExtractHereCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DecompressArchiveHereCommand.Execute(null));
ToolbarViewModel.ExtractToCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.DecompressArchiveToChildFolderCommand.Execute(null));
ToolbarViewModel.InstallInfCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.InstallInfDriver.Execute(null));
ToolbarViewModel.RotateImageLeftCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.RotateImageLeftCommand.Execute(null), () => SlimContentPage?.CommandsViewModel.RotateImageLeftCommand.CanExecute(null) == true);
ToolbarViewModel.RotateImageRightCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.RotateImageRightCommand.Execute(null), () => SlimContentPage?.CommandsViewModel.RotateImageRightCommand.CanExecute(null) == true);
ToolbarViewModel.InstallInfCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.InstallInfDriver.Execute(null));;
ToolbarViewModel.InstallFontCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.InstallFontCommand.Execute(null));
ToolbarViewModel.UpdateCommand = new AsyncRelayCommand(async () => await updateSettingsService.DownloadUpdates());
ToolbarViewModel.PlayAllCommand = new RelayCommand(() => SlimContentPage?.CommandsViewModel.PlayAllCommand.Execute(null));
Expand Down