Skip to content

Feature: Added support for editing album covers #14485

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
Jan 22, 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
33 changes: 33 additions & 0 deletions src/Files.App/Data/Models/SelectedItemsPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
using Files.App.ViewModels.Properties;
using Files.Shared.Helpers;
using System.Windows.Input;
using TagLib;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Pickers;

namespace Files.App.Data.Models
{
Expand Down Expand Up @@ -480,6 +484,13 @@ public ICommand FormatDriveCommand
get => formatDriveCommand;
set => SetProperty(ref formatDriveCommand, value);
}

private ICommand editAlbumCoverCommand;
public ICommand EditAlbumCoverCommand
{
get => editAlbumCoverCommand;
set => SetProperty(ref editAlbumCoverCommand, value);
}

private bool itemAttributesVisibility = true;
public bool ItemAttributesVisibility
Expand Down Expand Up @@ -511,6 +522,7 @@ public bool IsItemSelected

public SelectedItemsPropertiesViewModel()
{

}

private bool isSelectedItemImage = false;
Expand Down Expand Up @@ -691,5 +703,26 @@ public bool IsUnblockFileSelected
get => isUnblockFileSelected;
set => SetProperty(ref isUnblockFileSelected, value);
}

private bool isAblumCoverModified;
public bool IsAblumCoverModified
{
get => isAblumCoverModified;
set => SetProperty(ref isAblumCoverModified, value);
}

private bool isEditAlbumCoverVisible;
public bool IsEditAlbumCoverVisible
{
get => isEditAlbumCoverVisible;
set => SetProperty(ref isEditAlbumCoverVisible, value);
}

private Picture modifiedAlbumCover;
public Picture ModifiedAlbumCover
{
get => modifiedAlbumCover;
set => SetProperty(ref modifiedAlbumCover, value);
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/Files.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<PackageReference Include="CommunityToolkit.WinUI.Notifications" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Behaviors" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls" Version="7.1.2" />
<PackageReference Include="TagLibSharp" Version="2.3.0" />
<PackageReference Include="Tulpep.ActiveDirectoryObjectPicker" Version="3.0.11" />
<PackageReference Include="Vanara.PInvoke.DwmApi" Version="3.4.17" />
<PackageReference Include="Vanara.Windows.Extensions" Version="3.4.17" />
Expand Down
31 changes: 31 additions & 0 deletions src/Files.App/Helpers/MediaFileHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using TagLib;

namespace Files.App.Helpers
{
internal class MediaFileHelper
{
/// <summary>
/// Changes the album cover for a given <paramref name="filePath"/>.
/// </summary>
/// <param name="filePath">The file path to change the album cover.</param>
/// <param name="albumCover">The album cover to use.</param>
public static bool ChangeAlbumCover(string filePath, Picture albumCover)
{
try
{
File mediaFile = File.Create(filePath);
IPicture[] pictures = [albumCover];
mediaFile.Tag.Pictures = pictures;
mediaFile.Save();
mediaFile.Dispose();

return true;
}
catch (Exception)
{
return false;
}

}
}
}
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -3686,4 +3686,7 @@
<data name="NewWindowDescription" xml:space="preserve">
<value>Open new window</value>
</data>
<data name="ChangeAlbumCover" xml:space="preserve">
<value>Change album cover</value>
</data>
</root>
32 changes: 32 additions & 0 deletions src/Files.App/ViewModels/Properties/BasePropertiesPage.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright(c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Shared.Helpers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using TagLib;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.Storage.Pickers;

namespace Files.App.ViewModels.Properties
{
Expand Down Expand Up @@ -53,7 +57,13 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Selection only contains files
if (items.All(item => item.PrimaryItemAttribute == StorageItemTypes.File || item.IsArchive))
{
BaseProperties = new CombinedFileProperties(ViewModel, np.CancellationTokenSource, DispatcherQueue, items, AppInstance);

ViewModel.IsEditAlbumCoverVisible =
items.All(item => FileExtensionHelpers.IsVideoFile(item.FileExtension)) ||
items.All(item => FileExtensionHelpers.IsAudioFile(item.FileExtension));
}
// Selection includes folders
else
BaseProperties = new CombinedProperties(ViewModel, np.CancellationTokenSource, DispatcherQueue, items, AppInstance);
Expand All @@ -69,6 +79,28 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
BaseProperties = new FolderProperties(ViewModel, np.CancellationTokenSource, DispatcherQueue, item, AppInstance);
}

ViewModel.EditAlbumCoverCommand = new RelayCommand(async () =>
{
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.FileTypeFilter.Add(".jpg");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");

var parentWindowId = np.Window.AppWindow.Id;
var handle = Microsoft.UI.Win32Interop.GetWindowFromWindowId(parentWindowId);
WinRT.Interop.InitializeWithWindow.Initialize(filePicker, handle);

StorageFile file = await filePicker.PickSingleFileAsync();

if (file is not null)
{
ViewModel.IsAblumCoverModified = true;
ViewModel.ModifiedAlbumCover = new Picture(file.Path);
ViewModel.IconData = await FileThumbnailHelper.LoadIconFromPathAsync(file.Path, 80, ThumbnailMode.DocumentsView, ThumbnailOptions.ResizeThumbnail, false);
}
});

base.OnNavigatedTo(e);
}

Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/ViewModels/Properties/Items/FileProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public override void GetBaseProperties()
ViewModel.CustomIconSource = Item.CustomIconSource;
ViewModel.LoadFileIcon = Item.LoadFileIcon;
ViewModel.IsDownloadedFile = NativeFileOperationsHelper.ReadStringFromFile($"{Item.ItemPath}:Zone.Identifier") is not null;
ViewModel.IsEditAlbumCoverVisible =
FileExtensionHelpers.IsVideoFile(Item.FileExtension) ||
FileExtensionHelpers.IsAudioFile(Item.FileExtension);

if (!Item.IsShortcut)
return;
Expand Down
69 changes: 61 additions & 8 deletions src/Files.App/Views/Properties/GeneralPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
x:Class="Files.App.Views.Properties.GeneralPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:controls="using:Files.App.UserControls.Settings"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="using:Files.App.Helpers"
Expand Down Expand Up @@ -64,19 +66,70 @@
</Grid.RowDefinitions>

<!-- File Icon -->
<uc:FileIcon
x:Name="Icon"
<Grid
Grid.Row="0"
Grid.Column="0"
Width="40"
Height="40"
Width="56"
Height="56"
Margin="{StaticResource PropertyNameMargin}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
AutomationProperties.AccessibilityView="Raw"
FileIconImageData="{x:Bind ViewModel.IconData, Mode=OneWay}"
ItemSize="40"
ViewModel="{x:Bind ViewModel}" />
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="1"
CornerRadius="4">

<uc:FileIcon
x:Name="Icon"
Width="48"
Height="48"
HorizontalAlignment="Center"
VerticalAlignment="Center"
AutomationProperties.AccessibilityView="Raw"
FileIconImageData="{x:Bind ViewModel.IconData, Mode=OneWay}"
ItemSize="48"
ViewModel="{x:Bind ViewModel}" />

<!-- Edit Album Cover Button -->
<Button
x:Name="EditAlbumCoverButton"
Grid.Row="0"
Grid.Column="0"
Width="56"
Height="56"
Padding="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
x:Load="{x:Bind ViewModel.IsEditAlbumCoverVisible, Mode=OneWay}"
AutomationProperties.Name="{helpers:ResourceString Name=ChangeAlbumCover}"
Background="Transparent"
BorderThickness="0"
Command="{x:Bind ViewModel.EditAlbumCoverCommand, Mode=OneWay}"
Opacity="0"
ToolTipService.ToolTip="{helpers:ResourceString Name=ChangeAlbumCover}">
<Button.Resources>
<SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Transparent" />
</Button.Resources>
<Border
Width="32"
Height="32"
Background="{ThemeResource AcrylicInAppFillColorDefaultBrush}"
CornerRadius="30">
<FontIcon
FontSize="12"
Foreground="{ThemeResource TextFillColorPrimary}"
Glyph="&#xE70F;" />
</Border>

<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="PointerEntered">
<Core:ChangePropertyAction PropertyName="Opacity" Value="1.0" />
</Core:EventTriggerBehavior>
<Core:EventTriggerBehavior EventName="PointerExited">
<Core:ChangePropertyAction PropertyName="Opacity" Value="0" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
</Grid>

<!-- Name & Folder/File Count -->
<StackPanel
Expand Down
23 changes: 21 additions & 2 deletions src/Files.App/Views/Properties/GeneralPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the MIT License. See the LICENSE.

using Files.App.ViewModels.Properties;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
using System.IO;
using System.Text.RegularExpressions;
using Windows.Storage;
using Microsoft.UI.Dispatching;

namespace Files.App.Views.Properties
{
Expand All @@ -16,7 +16,6 @@ public sealed partial class GeneralPage : BasePropertiesPage
private readonly Regex letterRegex = new(@"\s*\(\w:\)$");

private readonly DispatcherQueueTimer _updateDateDisplayTimer;

public GeneralPage()
{
InitializeComponent();
Expand Down Expand Up @@ -135,6 +134,16 @@ async Task<bool> SaveCombinedAsync(IList<ListedItem> fileOrFolders)
await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
UIFilesystemHelpers.SetHiddenAttributeItem(fileOrFolder, ViewModel.IsHidden, itemMM)
);

if (ViewModel.IsAblumCoverModified)
{
MediaFileHelper.ChangeAlbumCover(fileOrFolder.ItemPath, ViewModel.ModifiedAlbumCover);

await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
{
AppInstance?.FilesystemViewModel?.RefreshItems(null);
});
}
}
}
return true;
Expand All @@ -154,6 +163,16 @@ await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
if (ViewModel.IsUnblockFileSelected)
NativeFileOperationsHelper.DeleteFileFromApp($"{item.ItemPath}:Zone.Identifier");

if (ViewModel.IsAblumCoverModified)
{
MediaFileHelper.ChangeAlbumCover(item.ItemPath, ViewModel.ModifiedAlbumCover);

await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() =>
{
AppInstance?.FilesystemViewModel?.RefreshItems(null);
});
}

if (!GetNewName(out var newName))
return true;

Expand Down