Compare commits

..

No commits in common. "6de76da8ad70016dcbd0d36f90f3f2b5d55cc676" and "d1df235af05700c2bfdd953bd0caf75f42a1302c" have entirely different histories.

3 changed files with 19 additions and 47 deletions

View File

@ -5,57 +5,44 @@ 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 GetIconFromIconPath() string _icon;
{
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()
{ {
try if (string.IsNullOrEmpty(_icon))
{ {
if (!string.IsNullOrEmpty(IconPath)) try
{ {
return GetIconFromIconPath(); if(!string.IsNullOrEmpty(IconPath))
{
var tmp = (Bitmap)Bitmap.FromFile(IconPath);
tmp.MakeTransparent();
_icon = Tools.ImageToBase64(tmp, true);
}
else
{
var tmp = IconExtraction.GetIcon(ExecutablePath);
//var tmp = Icon.ExtractAssociatedIcon(ExecutablePath);
_icon = Tools.ImageToBase64(tmp, true);
}
} }
else catch
{ {
return GetIconFromExecutablePath(); _icon = "Images/encoderIcon";
} }
} }
catch return _icon;
{
return "Images/encoderIcon";
}
} }
public bool Any() public bool Any()

View File

@ -97,9 +97,6 @@
</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>

View File

@ -39,24 +39,12 @@ namespace FocusVolumeControl
public event Action WindowChanged; public event Action WindowChanged;
CancellationTokenSource? _cancellationTokenSource = null; private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
private async void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{ {
try try
{ {
//debounce the window changed events by 100 ms because if you click mouse over an application on the start bar
//and then click on the preview window, it will quickly go from current -> fallback -> new app
//which can often result in it getting stuck on the fallback app
_cancellationTokenSource?.Cancel();
_cancellationTokenSource = new CancellationTokenSource();
await Task.Delay(100, _cancellationTokenSource.Token);
WindowChanged?.Invoke(); WindowChanged?.Invoke();
} }
catch (TaskCanceledException)
{
//ignored
}
catch (Exception ex) catch (Exception ex)
{ {
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in EventHandler:\n {ex}"); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in EventHandler:\n {ex}");