Skip to content

Commit ea893ea

Browse files
committed
Misc. changes, additions and fixes
1 parent 80b6da7 commit ea893ea

10 files changed

+152
-96
lines changed

Classes/ControlsEx/FontListEx.Designer.cs

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Classes/Handlers/AudioHandler.cs

+34-24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class AudioHandler
1616
private readonly Dictionary<IDisposable, CoreAudioDevice> audioDeviceSubscriptions = new Dictionary<IDisposable, CoreAudioDevice>();
1717
public CoreAudioController AudioController { get => audioController; set => audioController = value; }
1818
public int FriendlyVolume { get => audioController.DefaultPlaybackDevice != null ? (int)Math.Round(audioController.DefaultPlaybackDevice.Volume) : -1; }
19+
public List<CoreAudioDevice> AudioDevices { get => AudioController.GetPlaybackDevicesAsync(DeviceState.Active).Result.ToList(); }
20+
1921
public bool IsAudioAvailable()
2022
{
2123
if (AudioController == null)
@@ -56,7 +58,7 @@ public async Task AdjustVolume(MouseWheelDirection direction, ActionTriggerType
5658
if (type == ActionTriggerType.PreciseVolumeScroll)
5759
newVolume += Settings.Default.PreciseVolumeStep;
5860
if (audioController.DefaultPlaybackDevice.IsMuted)
59-
await Task.Run(() => Globals.AudioHandler.SetDeviceMute(Globals.AudioHandler.AudioController.DefaultPlaybackDevice, false));
61+
await Globals.AudioHandler.SetDeviceMute(Globals.AudioHandler.AudioController.DefaultPlaybackDevice, false);
6062
break;
6163
case MouseWheelDirection.Down:
6264
if (type == ActionTriggerType.RegularVolumeScroll)
@@ -71,57 +73,57 @@ public async Task AdjustVolume(MouseWheelDirection direction, ActionTriggerType
7173
if (newVolume < 0)
7274
newVolume = 0;
7375

74-
await Task.Run(() => SetDeviceVolume(audioController.DefaultPlaybackDevice, newVolume));
76+
await SetDeviceVolume(audioController.DefaultPlaybackDevice, newVolume);
7577
if (newVolume == 0)
76-
await Task.Run(() => Globals.AudioHandler.SetDeviceMute(Globals.AudioHandler.AudioController.DefaultPlaybackDevice, true));
78+
await Globals.AudioHandler.SetDeviceMute(Globals.AudioHandler.AudioController.DefaultPlaybackDevice, true);
7779
}
7880

7981

8082

8183
public async Task ChangePlaybackDevice(MouseWheelDirection direction)
8284
{
83-
List<CoreAudioDevice> audioDevices = (await Task.Run(() => AudioController.GetPlaybackDevicesAsync(DeviceState.Active))).ToList();
84-
if (audioDevices.Count != 0)
85+
if (AudioDevices.Count != 0)
8586
{
86-
int curDeviceIndex = audioDevices.IndexOf(AudioController.DefaultPlaybackDevice);
87+
int curDeviceIndex = AudioDevices.IndexOf(AudioController.DefaultPlaybackDevice);
8788
CoreAudioDevice newAudioDevice = null;
8889
switch (direction)
8990
{
9091
case MouseWheelDirection.Up:
91-
if (curDeviceIndex != audioDevices.Count)
92-
newAudioDevice = audioDevices.ElementAt(curDeviceIndex + 1);
92+
if (curDeviceIndex != AudioDevices.Count)
93+
newAudioDevice = AudioDevices.ElementAt(curDeviceIndex + 1);
9394
break;
9495
case MouseWheelDirection.Down:
9596
if (curDeviceIndex != 0)
96-
newAudioDevice = audioDevices.ElementAt(curDeviceIndex - 1);
97+
newAudioDevice = AudioDevices.ElementAt(curDeviceIndex - 1);
9798
break;
9899
}
99100
if(newAudioDevice != null)
100-
await Task.Run(() => SetPlaybackDevice(newAudioDevice));
101+
await SetPlaybackDevice(newAudioDevice);
101102
}
102103
}
103104

104105
public async void DeviceStateChanged(DeviceChangedArgs value)
105106
{
107+
Console.WriteLine($"{DateTime.Now.ToString("H:mm:ss.FFF")}: {value.Device.FullName} triggered {value.ChangedType}");
108+
CoreAudioDevice audioDevice = null;
106109
if (value.Device.InterfaceName != "Unknown" && value.Device.IsPlaybackDevice)
107110
{
108-
CoreAudioDevice audioDevice = audioController.GetDevice(value.Device.Id, DeviceState.All);
109-
Console.WriteLine(value.ChangedType);
110111
switch (value.ChangedType)
111112
{
112-
//case DeviceChangedType.DeviceRemoved:
113-
// foreach (var item in audioDeviceSubscriptions.Where(x => x.Value.Id == audioDevice.Id).ToList())
114-
// {
115-
// audioDeviceSubscriptions.Remove(item.Key);
116-
// item.Key.Dispose();
117-
// }
118-
// if (!Globals.AudioAvailable && Globals.InputAvailable)
119-
// Globals.MainForm.SetAppAppearances(ActionTriggerType.AudioDisabled);
120-
121-
// break;
122-
case DeviceChangedType.StateChanged:
113+
case DeviceChangedType.DeviceRemoved:
114+
foreach (var item in audioDeviceSubscriptions.Where(x => x.Value.Id == value.Device.Id).ToList())
115+
{
116+
audioDeviceSubscriptions.Remove(item.Key);
117+
item.Key.Dispose();
118+
}
119+
if (!Globals.AudioAvailable && Globals.InputAvailable)
120+
Globals.MainForm.SetAppAppearances(ActionTriggerType.AudioDisabled);
121+
122+
break;
123123
case DeviceChangedType.DeviceAdded:
124-
if (audioDevice != null && !audioDeviceSubscriptions.ContainsValue(audioDevice))
124+
Console.WriteLine("State: " + value.Device.State);
125+
audioDevice = audioController.GetDevice(value.Device.Id, DeviceState.All);
126+
if (audioDevice != null && !audioDeviceSubscriptions.ContainsValue(audioDevice) && value.Device.State == DeviceState.Active)
125127
{
126128
audioDeviceSubscriptions.Add(audioDevice.PeakValueChanged.Subscribe(DeviceStateChanged), audioDevice);
127129
audioDeviceSubscriptions.Add(audioDevice.VolumeChanged.Subscribe(DeviceStateChanged), audioDevice);
@@ -132,22 +134,30 @@ public async void DeviceStateChanged(DeviceChangedArgs value)
132134
Globals.MainForm.SetAppAppearances(ActionTriggerType.AudioEnabled);
133135
break;
134136
case DeviceChangedType.VolumeChanged:
137+
Console.WriteLine("Volume: " + value.Device.Volume);
138+
135139
Globals.MainForm.SetAppAppearances(ActionTriggerType.InternalEvent);
136140
if (Globals.VolumeSliderControlForm != null)
137141
Globals.VolumeSliderControlForm.UpdateVolumeState();
138142
break;
139143
case DeviceChangedType.MuteChanged:
144+
Console.WriteLine("Mute: " + value.Device.IsMuted);
145+
140146
Globals.MainForm.SetAppAppearances(ActionTriggerType.InternalEvent);
141147
if (Globals.VolumeSliderControlForm != null)
142148
Globals.VolumeSliderControlForm.UpdateVolumeState();
143149
break;
144150
case DeviceChangedType.DefaultChanged:
151+
Console.WriteLine("Default: " + value.Device.IsPlaybackDevice);
152+
145153
if (Globals.AudioAvailable && Globals.InputAvailable)
146154
Globals.MainForm.SetAppAppearances(ActionTriggerType.InternalEvent);
147155
if (Globals.VolumeSliderControlForm != null)
148156
Globals.VolumeSliderControlForm.UpdateDeviceState();
149157
break;
150158
case DeviceChangedType.PeakValueChanged:
159+
Console.WriteLine("PeakValue: " + value.Device.PeakValue);
160+
audioDevice = audioController.GetDevice(value.Device.Id, DeviceState.All);
151161
if (Globals.VolumeSliderControlForm != null && !AudioController.DefaultPlaybackDevice.IsMuted)
152162
{
153163
audioDevice.PeakValueTimer.PeakValueInterval = 10;

Classes/Handlers/InputHandler.cs

+14-16
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,26 @@ private async Task ProcessMouseWheelScrollActionQueue()
7878

7979
private async Task HandleMouseWheelScroll(MouseWheelDirection direction)
8080
{
81-
Task task = null;
82-
8381
if (!isAltDown && !isCtrlDown && !isShiftDown)
8482
{
8583
// Regular volume
8684
bool reachedThreshold = Globals.AudioHandler.FriendlyVolume < Settings.Default.PreciseVolumeThreshold;
87-
task = Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
88-
await task.ContinueWith(result => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
85+
await Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
86+
await Task.Run(() => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
8987
}
9088
else if (isAltDown)
9189
{
9290
// Precise Volume
9391
if (Settings.Default.AltHotkeyEnabled)
9492
{
95-
task = Globals.AudioHandler.AdjustVolume(direction, ActionTriggerType.PreciseVolumeScroll);
96-
await task.ContinueWith(result => Globals.MainForm.SetAppAppearances(ActionTriggerType.PreciseVolumeScroll));
93+
await Globals.AudioHandler.AdjustVolume(direction, ActionTriggerType.PreciseVolumeScroll);
94+
await Task.Run(() => Globals.MainForm.SetAppAppearances(ActionTriggerType.PreciseVolumeScroll));
9795
}
9896
else
9997
{
10098
bool reachedThreshold = Globals.AudioHandler.FriendlyVolume < Settings.Default.PreciseVolumeThreshold;
101-
task = Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
102-
await task.ContinueWith(result => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
99+
await Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
100+
await Task.Run(() => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
103101
}
104102
}
105103
else if (isCtrlDown)
@@ -108,29 +106,29 @@ private async Task HandleMouseWheelScroll(MouseWheelDirection direction)
108106
if (Settings.Default.CtrlHotkeyEnabled)
109107
{
110108
bool doMute = direction == MouseWheelDirection.Down;
111-
task = Globals.AudioHandler.SetDeviceMute(Globals.AudioHandler.AudioController.DefaultPlaybackDevice, doMute);
112-
await task.ContinueWith(result => Globals.MainForm.SetAppAppearances(ActionTriggerType.MuteToggleScroll));
109+
await Globals.AudioHandler.SetDeviceMute(Globals.AudioHandler.AudioController.DefaultPlaybackDevice, doMute);
110+
await Task.Run(() => Globals.MainForm.SetAppAppearances(ActionTriggerType.MuteToggleScroll));
113111
}
114112
else
115113
{
116114
bool reachedThreshold = Globals.AudioHandler.FriendlyVolume < Settings.Default.PreciseVolumeThreshold;
117-
task = Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
118-
await task.ContinueWith(result => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
115+
await Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
116+
await Task.Run(() => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
119117
}
120118
}
121119
else if (isShiftDown)
122120
{
123121
// Device switch
124122
if (Settings.Default.ShiftHotkeyEnabled)
125123
{
126-
task = Globals.AudioHandler.ChangePlaybackDevice(direction);
127-
await task.ContinueWith(result => Globals.MainForm.SetAppAppearances(ActionTriggerType.DeviceSwitchScroll));
124+
await Globals.AudioHandler.ChangePlaybackDevice(direction);
125+
await Task.Run(() => Globals.MainForm.SetAppAppearances(ActionTriggerType.DeviceSwitchScroll));
128126
}
129127
else
130128
{
131129
bool reachedThreshold = Globals.AudioHandler.FriendlyVolume < Settings.Default.PreciseVolumeThreshold;
132-
task = Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
133-
await task.ContinueWith(result => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
130+
await Globals.AudioHandler.AdjustVolume(direction, reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll);
131+
await Task.Run(() => Globals.MainForm.SetAppAppearances(reachedThreshold ? ActionTriggerType.PreciseVolumeScroll : ActionTriggerType.RegularVolumeScroll));
134132
}
135133
}
136134
else

Classes/Helpers/Utils.cs

+15-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Drawing.Drawing2D;
66
using System.Drawing.Imaging;
77
using System.Drawing.Text;
8+
using System.IO;
89
using System.Linq;
910
using System.Runtime.InteropServices;
1011
using System.Security.Principal;
@@ -130,8 +131,6 @@ public static Bitmap GetUacShield()
130131
}
131132
}
132133

133-
134-
135134
public static Size CalculateMinimumBarSize(Label label, string text)
136135
{
137136
return label.CreateGraphics().MeasureString(text, label.Font).ToSize();
@@ -151,13 +150,20 @@ public static void InvokeIfRequired(Control control, MethodInvoker action)
151150

152151
public static Icon GetIconFromResource(string resPath)
153152
{
154-
string[] iconInfo = resPath.Split(',');
155-
ExtractIconEx(iconInfo[0], int.Parse(iconInfo[1]), out IntPtr hIcon, IntPtr.Zero, 1);
156-
using (Icon tmpIcon = Icon.FromHandle(hIcon))
153+
try
157154
{
158-
Icon newIcon = (Icon)tmpIcon.Clone();
159-
DestroyIcon(tmpIcon.Handle);
160-
return newIcon;
155+
string[] iconInfo = resPath.Split(',');
156+
ExtractIconEx(iconInfo[0], int.Parse(iconInfo[1]), out IntPtr hIcon, IntPtr.Zero, 1);
157+
using (Icon tmpIcon = Icon.FromHandle(hIcon))
158+
{
159+
Icon newIcon = (Icon)tmpIcon.Clone();
160+
DestroyIcon(tmpIcon.Handle);
161+
return newIcon;
162+
}
163+
}
164+
catch
165+
{
166+
return Icon.ExtractAssociatedIcon(Path.Combine(Environment.GetEnvironmentVariable("windir"),@"System32\sndvol.exe"));
161167
}
162168
}
163169

@@ -196,7 +202,7 @@ private static Font FindBestFitFont(Graphics g, string text, Font font, Size pro
196202
if (size.Height <= proposedSize.Height + iconPadding && size.Width <= proposedSize.Width + iconPadding)
197203
{ return font; }
198204

199-
font = new Font(font.Name, font.Size *0.99F, font.Style);
205+
font = new Font(font.Name, font.Size * 0.99F, font.Style);
200206
}
201207
}
202208
catch { return oldFont; }

Forms/AudioPlaybackDevicesForm.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private async Task ProcessUpdateDeviceQueue()
7070
public async Task LoadAudioPlaybackDevicesList()
7171
{
7272
List<ListViewItem> deviceListViewItem = new List<ListViewItem>();
73-
List<CoreAudioDevice> audioDevices = (await Task.Run(() => Globals.AudioHandler.AudioController.GetPlaybackDevicesAsync(DeviceState.Active))).ToList();
73+
List<CoreAudioDevice> audioDevices = (await Globals.AudioHandler.AudioController.GetPlaybackDevicesAsync(DeviceState.Active)).ToList();
7474
if (audioDevices.Count != 0)
7575
{
7676
foreach (CoreAudioDevice d in audioDevices)
@@ -112,7 +112,7 @@ private async void SetDefaultButton_Click(object sender, EventArgs e)
112112

113113
public async Task RefreshOnDeviceActivity()
114114
{
115-
List<CoreAudioDevice> audioDevices = (await Task.Run(() => Globals.AudioHandler.AudioController.GetPlaybackDevicesAsync(DeviceState.Active))).ToList();
115+
List<CoreAudioDevice> audioDevices = (await Globals.AudioHandler.AudioController.GetPlaybackDevicesAsync(DeviceState.Active)).ToList();
116116
Utils.InvokeIfRequired(this, () =>
117117
{
118118
RefreshButton.Enabled = false;

Forms/ConfigurationForm.Designer.cs

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)