Use LRU cache for icons to improve performance when changing back and forth between frequently used apps, and reduce possibility of an icon randomly not loading
This commit is contained in:
@ -5,45 +5,58 @@ using BarRaider.SdTools;
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using FocusVolumeControl.UI;
|
using FocusVolumeControl.UI;
|
||||||
|
using BitFaster.Caching.Lru;
|
||||||
|
|
||||||
namespace FocusVolumeControl.AudioSessions;
|
namespace FocusVolumeControl.AudioSessions;
|
||||||
|
|
||||||
public sealed class ActiveAudioSessionWrapper : IAudioSession
|
public sealed class ActiveAudioSessionWrapper : IAudioSession
|
||||||
{
|
{
|
||||||
|
static ConcurrentLru<string, string> _iconCache = new ConcurrentLru<string, string>(10);
|
||||||
|
|
||||||
public string DisplayName { get; set; }
|
public string DisplayName { get; set; }
|
||||||
public string ExecutablePath { get; set; }
|
public string ExecutablePath { get; set; }
|
||||||
public string IconPath { get; set; }
|
public string IconPath { get; set; }
|
||||||
private List<IAudioSessionControl2> Sessions { get; } = new List<IAudioSessionControl2>();
|
private List<IAudioSessionControl2> Sessions { get; } = new List<IAudioSessionControl2>();
|
||||||
private IEnumerable<ISimpleAudioVolume> Volume => Sessions.Cast<ISimpleAudioVolume>();
|
private IEnumerable<ISimpleAudioVolume> Volume => Sessions.Cast<ISimpleAudioVolume>();
|
||||||
|
|
||||||
string _icon;
|
string GetIconFromIconPath()
|
||||||
|
{
|
||||||
|
return _iconCache.GetOrAdd(IconPath, (key) =>
|
||||||
|
{
|
||||||
|
var tmp = (Bitmap)Bitmap.FromFile(IconPath);
|
||||||
|
tmp.MakeTransparent();
|
||||||
|
return Tools.ImageToBase64(tmp, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
string GetIconFromExecutablePath()
|
||||||
|
{
|
||||||
|
return _iconCache.GetOrAdd(ExecutablePath, (key) =>
|
||||||
|
{
|
||||||
|
var tmp = IconExtraction.GetIcon(ExecutablePath);
|
||||||
|
//var tmp = Icon.ExtractAssociatedIcon(ExecutablePath);
|
||||||
|
return Tools.ImageToBase64(tmp, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public string GetIcon()
|
public string GetIcon()
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(_icon))
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(IconPath))
|
if (!string.IsNullOrEmpty(IconPath))
|
||||||
{
|
{
|
||||||
var tmp = (Bitmap)Bitmap.FromFile(IconPath);
|
return GetIconFromIconPath();
|
||||||
tmp.MakeTransparent();
|
|
||||||
_icon = Tools.ImageToBase64(tmp, true);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var tmp = IconExtraction.GetIcon(ExecutablePath);
|
return GetIconFromExecutablePath();
|
||||||
//var tmp = Icon.ExtractAssociatedIcon(ExecutablePath);
|
|
||||||
_icon = Tools.ImageToBase64(tmp, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
_icon = "Images/encoderIcon";
|
return "Images/encoderIcon";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return _icon;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool Any()
|
public bool Any()
|
||||||
{
|
{
|
||||||
|
@ -97,6 +97,9 @@
|
|||||||
</Content>
|
</Content>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BitFaster.Caching">
|
||||||
|
<Version>2.2.1</Version>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="IsExternalInit">
|
<PackageReference Include="IsExternalInit">
|
||||||
<Version>1.0.3</Version>
|
<Version>1.0.3</Version>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
Reference in New Issue
Block a user