[Feature] Show menu faster (load icons in background and update UI later) #196, 1.0.18.2

This commit is contained in:
Markus Hofknecht 2021-09-24 15:37:58 +02:00
parent cc70299972
commit bf7a92f6b9
3 changed files with 38 additions and 18 deletions

View file

@ -60,8 +60,6 @@ namespace SystemTrayMenu.DataClasses
internal int MenuLevel { get; set; }
internal Icon Icon => icon;
internal bool IconLoading { get; set; }
public void Dispose()
@ -82,11 +80,6 @@ namespace SystemTrayMenu.DataClasses
DataRow row = dataTable.Rows.Add();
data.RowIndex = dataTable.Rows.IndexOf(row);
if (icon == null)
{
icon = NotFoundIcon;
}
if (HiddenEntry)
{
row[0] = IconReader.AddIconOverlay(
@ -179,6 +172,11 @@ namespace SystemTrayMenu.DataClasses
}
}
if (icon == null)
{
icon = NotFoundIcon;
}
return isLnkDirectory;
}
@ -242,6 +240,36 @@ namespace SystemTrayMenu.DataClasses
}
}
internal Icon ReadLoadedIcon()
{
bool showOverlay = false;
string fileExtension = Path.GetExtension(TargetFilePath);
if (fileExtension == ".lnk" || fileExtension == ".url")
{
showOverlay = true;
}
string path = TargetFilePath;
if (ContainsMenu)
{
path = TargetFilePathOrig;
}
icon = IconReader.GetFileIconWithCache(
path,
showOverlay,
false,
out bool loading);
IconLoading = loading;
if (!loading && icon == null)
{
icon = NotFoundIcon;
}
return icon;
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)

View file

@ -703,13 +703,6 @@ namespace SystemTrayMenu.UserInterface
else
{
filesCount++;
if (rowData.IconLoading)
{
string resolvedLnkPath = string.Empty;
rowData.ReadIcon(rowData.ContainsMenu, ref resolvedLnkPath);
row.Cells[0].Value = rowData.Icon;
}
}
}
@ -816,9 +809,7 @@ namespace SystemTrayMenu.UserInterface
if (rowData.IconLoading)
{
iconsToUpdate++;
string resolvedLnkPath = string.Empty;
rowData.ReadIcon(rowData.ContainsMenu, ref resolvedLnkPath);
row.Cells[0].Value = rowData.Icon;
row.Cells[0].Value = rowData.ReadLoadedIcon();
}
}

View file

@ -26,6 +26,7 @@ namespace SystemTrayMenu.Utilities
public static class IconReader
{
private static readonly ConcurrentDictionary<string, Icon> DictIconCache = new ConcurrentDictionary<string, Icon>();
private static readonly Icon LoadingIcon = Properties.Resources.Loading;
// private static readonly object ReadIcon = new object();
public enum IconSize
@ -67,7 +68,7 @@ namespace SystemTrayMenu.Utilities
{
if (!DictIconCache.TryGetValue(filePath, out icon))
{
icon = Properties.Resources.Loading;
icon = LoadingIcon;
loading = true;
if (updateIconInBackground)