Skip to content
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

Code Quality: Icon improvements #14792

Merged
merged 11 commits into from
Feb 21, 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
15 changes: 10 additions & 5 deletions src/Files.App/Data/Items/DriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// Licensed under the MIT License. See the LICENSE.

using Files.App.Storage.WindowsStorage;
using Files.Core.Storage;
using Files.Core.Storage.Enums;
using Files.Core.Storage.LocatableStorage;
using Files.Core.Storage.NestedStorage;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
Expand Down Expand Up @@ -317,7 +313,16 @@ public int CompareTo(INavigationControlItem other)
public async Task LoadThumbnailAsync()
{
if (!string.IsNullOrEmpty(DeviceID) && !string.Equals(DeviceID, "network-folder"))
IconData ??= await FileThumbnailHelper.LoadIconWithoutOverlayAsync(DeviceID, Constants.ShellIconSizes.Small, false, false, true, true);
{
var result = await FileThumbnailHelper.GetIconAsync(
DeviceID,
Constants.ShellIconSizes.Small,
false,
false,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

IconData ??= result.IconData;
}

if (Root is not null)
{
Expand Down
12 changes: 9 additions & 3 deletions src/Files.App/Data/Items/SidebarLibraryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ public async Task<bool> CheckDefaultSaveFolderAccess()

public async Task LoadLibraryIconAsync()
{
IconData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Path, Constants.ShellIconSizes.Small, false, false, true, true);
var result = await FileThumbnailHelper.GetIconAsync(
Path,
Constants.ShellIconSizes.Small,
false,
false,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

if (IconData is not null)
Icon = await IconData.ToBitmapAsync();
var bitmapImage = await result.IconData.ToBitmapAsync();
if (bitmapImage is not null)
Icon = bitmapImage;
}

public override int GetHashCode() => Path.GetHashCode(System.StringComparison.OrdinalIgnoreCase);
Expand Down
17 changes: 12 additions & 5 deletions src/Files.App/Data/Items/WidgetDriveCardItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ public WidgetDriveCardItem(DriveItem item)

public async Task LoadCardThumbnailAsync()
{
thumbnailData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Item.Path, Constants.ShellIconSizes.Large, true, false, true, true);

// Thumbnail data is valid, set the item icon
if (thumbnailData is not null && thumbnailData.Length > 0)
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
var result = await FileThumbnailHelper.GetIconAsync(
Item.Path,
Constants.ShellIconSizes.Large,
true,
false,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

thumbnailData = result.IconData;

var bitmapImage = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
if (bitmapImage is not null)
Thumbnail = bitmapImage;
}

public int CompareTo(WidgetDriveCardItem? other)
Expand Down
12 changes: 9 additions & 3 deletions src/Files.App/Data/Items/WidgetFolderCardItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ public WidgetFolderCardItem(LocationItem item, string text, bool isPinned)

public async Task LoadCardThumbnailAsync()
{
_thumbnailData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(Path, Constants.ShellIconSizes.Large, true, false, true, true);

if (_thumbnailData is not null && _thumbnailData.Length > 0)
var result = await FileThumbnailHelper.GetIconAsync(
Path,
Constants.ShellIconSizes.Large,
true,
false,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

_thumbnailData = result.IconData;
if (_thumbnailData is not null)
Thumbnail = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => _thumbnailData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}
}
Expand Down
59 changes: 31 additions & 28 deletions src/Files.App/Data/Models/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public void Clipboard_ContentChanged(object sender, object e)
}
}

private int tabStripSelectedIndex = 0;
private int _TabStripSelectedIndex = 0;
public int TabStripSelectedIndex
{
get => tabStripSelectedIndex;
get => _TabStripSelectedIndex;
set
{
SetProperty(ref tabStripSelectedIndex, value);
SetProperty(ref _TabStripSelectedIndex, value);

if (value >= 0 && value < MainPageViewModel.AppInstances.Count)
{
Expand All @@ -44,85 +44,88 @@ public int TabStripSelectedIndex
}
}

private bool isAppElevated = false;
private bool _IsAppElevated = false;
public bool IsAppElevated
{
get => isAppElevated;
set => SetProperty(ref isAppElevated, value);
get => _IsAppElevated;
set => SetProperty(ref _IsAppElevated, value);
}

private bool isPasteEnabled = false;
private bool _IsPasteEnabled = false;
public bool IsPasteEnabled
{
get => isPasteEnabled;
set => SetProperty(ref isPasteEnabled, value);
get => _IsPasteEnabled;
set => SetProperty(ref _IsPasteEnabled, value);
}

private volatile int isMainWindowClosed = 0;
private volatile int _IsMainWindowClosed = 0;
public bool IsMainWindowClosed
{
get => isMainWindowClosed == 1;
get => _IsMainWindowClosed == 1;
set
{
int orig = Interlocked.Exchange(ref isMainWindowClosed, value ? 1 : 0);
if (isMainWindowClosed != orig)
int orig = Interlocked.Exchange(ref _IsMainWindowClosed, value ? 1 : 0);
if (_IsMainWindowClosed != orig)
OnPropertyChanged();
}
}

private int propertiesWindowCount = 0;
private int _PropertiesWindowCount = 0;
public int PropertiesWindowCount
{
get => propertiesWindowCount;
get => _PropertiesWindowCount;
}

public int IncrementPropertiesWindowCount()
{
var result = Interlocked.Increment(ref propertiesWindowCount);
var result = Interlocked.Increment(ref _PropertiesWindowCount);
OnPropertyChanged(nameof(PropertiesWindowCount));
return result;
}

public int DecrementPropertiesWindowCount()
{
var result = Interlocked.Decrement(ref propertiesWindowCount);
var result = Interlocked.Decrement(ref _PropertiesWindowCount);
OnPropertyChanged(nameof(PropertiesWindowCount));
return result;
}

private bool forceProcessTermination = false;
private bool _ForceProcessTermination = false;
public bool ForceProcessTermination
{
get => forceProcessTermination;
set => SetProperty(ref forceProcessTermination, value);
get => _ForceProcessTermination;
set => SetProperty(ref _ForceProcessTermination, value);
}

private string googleDrivePath = string.Empty;
private string _GoogleDrivePath = string.Empty;
/// <summary>
/// Gets or sets a value indicating the path for Google Drive.
/// </summary>
public string GoogleDrivePath
{
get => googleDrivePath;
set => SetProperty(ref googleDrivePath, value);
get => _GoogleDrivePath;
set => SetProperty(ref _GoogleDrivePath, value);
}

private string pCloudDrivePath = string.Empty;
private string _PCloudDrivePath = string.Empty;
/// <summary>
/// Gets or sets a value indicating the path for pCloud Drive.
/// </summary>
public string PCloudDrivePath
{
get => pCloudDrivePath;
set => SetProperty(ref pCloudDrivePath, value);
get => _PCloudDrivePath;
set => SetProperty(ref _PCloudDrivePath, value);
}

/// <summary>
/// Gets or sets a value indicating the AppWindow DPI.
/// TODO update value if the DPI changes
/// </summary>
public float AppWindowDpi
private float _AppWindowDPI = InteropHelpers.GetDpiForWindow(MainWindow.Instance.WindowHandle) / 96f;
public float AppWindowDPI
{
get => InteropHelpers.GetDpiForWindow(MainWindow.Instance.WindowHandle) / 96f;
get => _AppWindowDPI;
set => SetProperty(ref _AppWindowDPI, value);
}
}
}
50 changes: 39 additions & 11 deletions src/Files.App/Data/Models/ItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,12 @@ private async Task LoadItemThumbnailAsync(ListedItem item)
{
var getIconOnly = UserSettingsService.FoldersSettingsService.ShowThumbnails == false || thumbnailSize < 48;
var getThumbnailOnly = !item.IsExecutable && !getIconOnly;
var iconInfo = await FileThumbnailHelper.LoadIconAndOverlayAsync(item.ItemPath, thumbnailSize, false, getThumbnailOnly, getIconOnly);
var iconInfo = await FileThumbnailHelper.GetIconAsync(
item.ItemPath,
thumbnailSize,
false,
getThumbnailOnly,
getIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);

if (!iconInfo.isIconCached)
{
Expand All @@ -962,7 +967,13 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
var cancellationTokenSource = new CancellationTokenSource(3000);
while (!iconInfo.isIconCached)
{
iconInfo = await FileThumbnailHelper.LoadIconAndOverlayAsync(item.ItemPath, thumbnailSize, false, getThumbnailOnly, getIconOnly);
iconInfo = await FileThumbnailHelper.GetIconAsync(
item.ItemPath,
thumbnailSize,
false,
getThumbnailOnly,
getIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);

cancellationTokenSource.Token.ThrowIfCancellationRequested();
await Task.Delay(500);
}
Expand All @@ -984,28 +995,39 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
!item.IsExecutable
)
{
var fileIcon = await FileThumbnailHelper.LoadIconAndOverlayAsync(item.ItemPath, thumbnailSize, false, false, true);
var fileIcon = await FileThumbnailHelper.GetIconAsync(
item.ItemPath,
thumbnailSize,
false,
false,
IconOptions.ReturnIconOnly);

var bitmapImage = await fileIcon.IconData.ToBitmapAsync();
DefaultIcons.TryAdd(item.FileExtension.ToLowerInvariant(), bitmapImage);
}

}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}

if (iconInfo.OverlayData is not null)
var iconOverlay = await FileThumbnailHelper.GetIconOverlayAsync(item.ItemPath, false);
if (iconOverlay is not null)
{
// Assign the icon overlay to the listed item
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
item.IconOverlay = await iconInfo.OverlayData.ToBitmapAsync();
item.IconOverlay = await iconOverlay.ToBitmapAsync();
item.ShieldIcon = await GetShieldIcon();
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}
}
else
{
var getIconOnly = UserSettingsService.FoldersSettingsService.ShowThumbnails == false || thumbnailSize < 48;
var iconInfo = await FileThumbnailHelper.LoadIconAndOverlayAsync(item.ItemPath, thumbnailSize, true, false, getIconOnly);
var iconInfo = await FileThumbnailHelper.GetIconAsync(
item.ItemPath,
thumbnailSize,
true,
false, getIconOnly ? IconOptions.ReturnIconOnly : IconOptions.None);

if (iconInfo.IconData is not null)
{
Expand All @@ -1015,11 +1037,12 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}

if (iconInfo.OverlayData is not null)
var iconOverlay = await FileThumbnailHelper.GetIconOverlayAsync(item.ItemPath, true);
if (iconOverlay is not null)
{
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
item.IconOverlay = await iconInfo.OverlayData.ToBitmapAsync();
item.IconOverlay = await iconOverlay.ToBitmapAsync();
item.ShieldIcon = await GetShieldIcon();
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}
Expand Down Expand Up @@ -1284,10 +1307,15 @@ await SafetyExtensions.IgnoreExceptions(() =>
ImageSource? groupImage = null;
if (item.PrimaryItemAttribute != StorageItemTypes.Folder || item.IsArchive)
{
var headerIconInfo = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(item.ItemPath, Constants.ShellIconSizes.Large, false, false, true, true);
var headerIconInfo = await FileThumbnailHelper.GetIconAsync(
item.ItemPath,
Constants.ShellIconSizes.Large,
false,
false,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

if (headerIconInfo is not null && !item.IsShortcut)
groupImage = await dispatcherQueue.EnqueueOrInvokeAsync(() => headerIconInfo.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
if (headerIconInfo.IconData is not null && !item.IsShortcut)
groupImage = await dispatcherQueue.EnqueueOrInvokeAsync(() => headerIconInfo.IconData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);

// The groupImage is null if loading icon from fulltrust process failed
if (!item.IsShortcut && !item.IsHiddenItem && !FtpHelpers.IsFtpPath(item.ItemPath) && groupImage is null)
Expand Down
19 changes: 12 additions & 7 deletions src/Files.App/Data/Models/SidebarPinnedModel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.App.UserControls.Widgets;
using System.Collections.Specialized;
using System.IO;
using System.Text.Json.Serialization;
using Windows.Storage.FileProperties;

namespace Files.App.Data.Models
{
Expand Down Expand Up @@ -102,11 +100,18 @@ public async Task<LocationItem> CreateLocationItemFromPathAsync(string path)
locationItem.IsInvalid = false;
if (res && res.Result is not null)
{
var iconData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(res.Result.Path, Constants.ShellIconSizes.Small, true, false, true, true);
locationItem.IconData = iconData;

if (locationItem.IconData is not null)
locationItem.Icon = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => locationItem.IconData.ToBitmapAsync());
var result = await FileThumbnailHelper.GetIconAsync(
res.Result.Path,
Constants.ShellIconSizes.Small,
true,
false,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

locationItem.IconData = result.IconData;

var bitmapImage = await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(() => locationItem.IconData.ToBitmapAsync(), Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
if (bitmapImage is not null)
locationItem.Icon = bitmapImage;
}
}
else
Expand Down
12 changes: 9 additions & 3 deletions src/Files.App/Helpers/Navigation/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,15 @@ private static async Task UpdateTabInfoAsync(TabBarItem tabItem, object navigati

if (iconSource.ImageSource is null)
{
var iconData = await FileThumbnailHelper.LoadIconWithoutOverlayAsync(currentPath, Constants.ShellIconSizes.Small, true, false, true, true);
if (iconData is not null)
iconSource.ImageSource = await iconData.ToBitmapAsync();
var result = await FileThumbnailHelper.GetIconAsync(
currentPath,
Constants.ShellIconSizes.Small,
true,
false,
IconOptions.ReturnIconOnly | IconOptions.UseCurrentScale);

if (result.IconData is not null)
iconSource.ImageSource = await result.IconData.ToBitmapAsync();
}

return (tabLocationHeader, iconSource, toolTipText);
Expand Down
Loading