Skip to content

Commit

Permalink
Feature: Added dimensions to tooltip when hovering over image files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5bfa authored Jun 10, 2024
1 parent 65ee0b1 commit 31d6fee
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal abstract class BaseSetAsAction : ObservableObject, IAction
context.ShellPage is not null &&
context.PageType != ContentPageTypes.RecycleBin &&
context.PageType != ContentPageTypes.ZipFolder &&
(context.ShellPage?.SlimContentPage?.SelectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false);
(context.ShellPage?.SlimContentPage?.SelectedItemsPropertiesViewModel?.IsCompatibleToSetAsWindowsWallpaper ?? false);

public BaseSetAsAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal abstract class BaseRotateAction : ObservableObject, IAction

public bool IsExecutable =>
IsContextPageTypeAdaptedToCommand() &&
(context.ShellPage?.SlimContentPage?.SelectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false);
(context.ShellPage?.SlimContentPage?.SelectedItemsPropertiesViewModel?.IsCompatibleToSetAsWindowsWallpaper ?? false);

public BaseRotateAction()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
new ContextMenuFlyoutItemViewModel()
{
Text = "BaseLayoutItemContextFlyoutSetAs/Text".GetLocalizedResource(),
ShowItem = itemsSelected && (selectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false),
ShowItem = itemsSelected && (selectedItemsPropertiesViewModel?.IsCompatibleToSetAsWindowsWallpaper ?? false),
ShowInSearchPage = true,
Items =
[
Expand All @@ -419,13 +419,13 @@ public static List<ContextMenuFlyoutItemViewModel> GetBaseItemMenuItems(
{
IsVisible = !currentInstanceViewModel.IsPageTypeRecycleBin
&& !currentInstanceViewModel.IsPageTypeZipFolder
&& (selectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false)
&& (selectedItemsPropertiesViewModel?.IsCompatibleToSetAsWindowsWallpaper ?? false)
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.RotateRight)
{
IsVisible = !currentInstanceViewModel.IsPageTypeRecycleBin
&& !currentInstanceViewModel.IsPageTypeZipFolder
&& (selectedItemsPropertiesViewModel?.IsSelectedItemImage ?? false)
&& (selectedItemsPropertiesViewModel?.IsCompatibleToSetAsWindowsWallpaper ?? false)
}.Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.RunAsAdmin).Build(),
new ContextMenuFlyoutItemViewModelBuilder(Commands.RunAsAnotherUser).Build(),
Expand Down
38 changes: 38 additions & 0 deletions src/Files.App/Data/Items/ListedItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FluentFTP;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Drawing;
using System.IO;
using System.Text;
using Windows.Storage;
Expand Down Expand Up @@ -45,6 +46,8 @@ public string ItemTooltipText
tooltipBuilder.Append($"{"ToolTipDescriptionDate".GetLocalizedResource()} {ItemDateModified}");
if (!string.IsNullOrWhiteSpace(FileSize))
tooltipBuilder.Append($"{Environment.NewLine}{"SizeLabel".GetLocalizedResource()} {FileSize}");
if (!string.IsNullOrWhiteSpace(DimensionsDisplay))
tooltipBuilder.Append($"{Environment.NewLine}{"PropertyDimensions".GetLocalizedResource()}: {DimensionsDisplay}");
if (SyncStatusUI.LoadSyncStatus)
tooltipBuilder.Append($"{Environment.NewLine}{"syncStatusColumn/Header".GetLocalizedResource()}: {syncStatusUI.SyncStatusString}");

Expand Down Expand Up @@ -326,6 +329,41 @@ public ObservableCollection<FileProperty> ItemProperties
set => SetProperty(ref itemProperties, value);
}

public string DimensionsDisplay
{
get
{
int imageHeight = 0;
int imageWidth = 0;

var isImageFile = FileExtensionHelpers.IsImageFile(FileExtension);
if (isImageFile)
{
try
{
// TODO: Consider to use 'System.Kind' instead.
using FileStream fileStream = new(ItemPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using Image image = Image.FromStream(fileStream, false, false);

if (image is not null)
{
imageHeight = image.Height;
imageWidth = image.Width;
}
}
catch { }
}


return
isImageFile &&
imageWidth > 0 &&
imageHeight > 0
? $"{imageWidth} \u00D7 {imageHeight}"
: string.Empty;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="ListedItem" /> class.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/Data/Models/SelectedItemsPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ public SelectedItemsPropertiesViewModel()
}

private bool isSelectedItemImage = false;
public bool IsSelectedItemImage
public bool IsCompatibleToSetAsWindowsWallpaper
{
get => isSelectedItemImage;
set => SetProperty(ref isSelectedItemImage, value);
Expand All @@ -546,7 +546,7 @@ public bool IsSelectedItemShortcut
public void CheckAllFileExtensions(List<string> itemExtensions)
{
// Checks if all the item extensions are image extensions of some kind.
IsSelectedItemImage = itemExtensions.TrueForAll(itemExtension => FileExtensionHelpers.IsImageFile(itemExtension));
IsCompatibleToSetAsWindowsWallpaper = itemExtensions.TrueForAll(FileExtensionHelpers.IsCompatibleToSetAsWindowsWallpaper);
// Checks if there is only one selected item and if it's a shortcut.
IsSelectedItemShortcut = (itemExtensions.Count == 1) && (itemExtensions.TrueForAll(itemExtension => FileExtensionHelpers.IsShortcutFile(itemExtension)));
}
Expand Down
12 changes: 11 additions & 1 deletion src/Files.Shared/Helpers/FileExtensionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,20 @@ public static bool HasExtension(string? filePathToCheck, params string[] extensi
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is an image; otherwise, <c>false</c>.</returns>
public static bool IsImageFile(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".png", ".bmp", ".jpg", ".jpeg", ".jfif", ".gif", ".tiff", ".tif", ".webp");
}

/// <summary>
/// Checks if the file can be set as wallpaper.
/// </summary>
/// <param name="fileExtensionToCheck">The file extension to check.</param>
/// <returns><c>true</c> if the fileExtensionToCheck is an image; otherwise, <c>false</c>.</returns>
public static bool IsCompatibleToSetAsWindowsWallpaper(string? fileExtensionToCheck)
{
return HasExtension(fileExtensionToCheck, ".png", ".bmp", ".jpg", ".jpeg", ".jfif", ".gif", ".tiff", ".tif");
}

/// <summary>
/// Check if the file extension is an audio file.
/// </summary>
Expand Down

0 comments on commit 31d6fee

Please sign in to comment.