Skip to content

Commit

Permalink
Add recording devices to TrayIcon. Fixes #19
Browse files Browse the repository at this point in the history
Remove Exception handler when DEBUG release.
  • Loading branch information
Antoine Aflalo committed Sep 2, 2015
1 parent 2bae78b commit df2de5b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
13 changes: 11 additions & 2 deletions SoundSwitch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ private static void Main()
AppModel.Instance.ActiveAudioDeviceLister = new AudioDeviceLister(DeviceState.Active);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#if !DEBUG
Application.ThreadException += Application_ThreadException;
#endif
WindowsAPIAdapter.Start();
//Manage the Closing events send by Windows
//Since this app don't use a Form as "main window" the app doesn't close
Expand All @@ -67,10 +69,14 @@ private static void Main()
}
};
AppLogger.Log.Info("Set Exception Handler");
#if !DEBUG
WindowsAPIAdapter.AddThreadExceptionHandler(Application_ThreadException);
#endif
AppLogger.Log.Info("Set Tray Icon with Main");
#if !DEBUG
try
{
#endif
using (var icon = new TrayIcon())
{
if (AppConfigs.Configuration.FirstRun)
Expand All @@ -82,13 +88,16 @@ private static void Main()
Application.Run();
WindowsAPIAdapter.Stop();
}
#if !DEBUG
}

catch (Exception ex)
{
HandleException(ex);

}
#endif
}

}
}

private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
Expand Down
80 changes: 57 additions & 23 deletions SoundSwitch/Util/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using AudioEndPointControllerWrapper;
Expand All @@ -41,15 +42,17 @@ public sealed class TrayIcon : IDisposable
};

private readonly ToolStripMenuItem _updateMenuItem;
private ICollection<IAudioDevice> _availableAudioDeviceWrappers;
private ICollection<IAudioDevice> _availablePlaybackDevices;
private ICollection<IAudioDevice> _availableRecordingDevices;
private bool _deviceListChanged = true;

public TrayIcon()
{
_updateMenuItem = new ToolStripMenuItem("No Update", Resources.Update, OnUpdateClick) {Enabled = false};
_trayIcon.ContextMenuStrip = _settingsMenu;

_availableAudioDeviceWrappers = AppModel.Instance.AvailablePlaybackDevices;
_availablePlaybackDevices = AppModel.Instance.AvailablePlaybackDevices;
_availableRecordingDevices = AppModel.Instance.AvailableRecordingDevices;

PopulateSettingsMenu();

Expand Down Expand Up @@ -78,7 +81,7 @@ public void Dispose()
_settingsMenu.Dispose();
_trayIcon.Dispose();
_updateMenuItem.Dispose();
GC.SuppressFinalize(_availableAudioDeviceWrappers);
GC.SuppressFinalize(_availablePlaybackDevices);
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -121,13 +124,20 @@ private void SetEventHandlers()
AppModel.Instance.DefaultDeviceChanged +=
(sender, audioChangeEvent) =>
{
ShowAudioChanged(audioChangeEvent.AudioDevice.FriendlyName);
foreach (ToolStripDeviceItem item in _selectionMenu.Items)
var audioDeviceType = audioChangeEvent.AudioDevice.Type;
switch (audioDeviceType)
{
item.Image = item.AudioDevice.FriendlyName == audioChangeEvent.AudioDevice.FriendlyName
? Resources.Check
: null;
case AudioDeviceType.Playback:
ShowPlaybackChanged(audioChangeEvent.AudioDevice.FriendlyName);
break;
case AudioDeviceType.Recording:
ShowRecordingChanged(audioChangeEvent.AudioDevice.FriendlyName);
break;
default:
throw new ArgumentOutOfRangeException();
}

UpdateImageContextMenu(audioDeviceType, audioChangeEvent);
};
AppModel.Instance.SelectedDeviceChanged += (sender, deviceListChanged) => { UpdateAvailableDeviceList(); };
AppModel.Instance.NewVersionReleased += (sender, @event) =>
Expand All @@ -147,22 +157,39 @@ private void SetEventHandlers()
AppModel.Instance.InitializeMain();
}

private void UpdateImageContextMenu(AudioDeviceType audioDeviceType, AudioChangeEvent audioChangeEvent)
{
foreach (
var toolStripDevItem in
_selectionMenu.Items.OfType<ToolStripDeviceItem>().Where(item => item.AudioDevice.Type == audioDeviceType))
{
toolStripDevItem.Image = toolStripDevItem.AudioDevice.FriendlyName == audioChangeEvent.AudioDevice.FriendlyName
? Resources.Check
: null;
}
}

private void NewReleaseAvailable(object sender, UpdateChecker.NewReleaseEvent newReleaseEvent)
{
_updateMenuItem.Tag = newReleaseEvent.Release;
_updateMenuItem.Text = $"Update Available ({newReleaseEvent.Release.ReleaseVersion})";
_updateMenuItem.Enabled = true;
_trayIcon.ShowBalloonTip(3000, $"Version {newReleaseEvent.Release.ReleaseVersion} is available",
"Right click on the tray icon to download.", ToolTipIcon.Info);
_trayIcon.ShowBalloonTip(3000, $"Version {newReleaseEvent.Release.ReleaseVersion} is available", "Right click on the tray icon to download.", ToolTipIcon.Info);
}

private void UpdateAvailableDeviceList()
{
var audioDevices = AppModel.Instance.AvailablePlaybackDevices;
_deviceListChanged = !_availableAudioDeviceWrappers.Equals(audioDevices);
_deviceListChanged = !_availablePlaybackDevices.Equals(audioDevices);
if (_deviceListChanged)
{
_availablePlaybackDevices = audioDevices;
}
audioDevices = AppModel.Instance.AvailableRecordingDevices;
_deviceListChanged = !_availableRecordingDevices.Equals(audioDevices);
if (_deviceListChanged)
{
_availableAudioDeviceWrappers = audioDevices;
_availableRecordingDevices = audioDevices;
}
}

Expand All @@ -184,18 +211,26 @@ public void UpdateDeviceSelectionList()
return;
}

if (_availableAudioDeviceWrappers.Count < 0)
if (_availablePlaybackDevices.Count < 0 && _availableRecordingDevices.Count < 0)
{
AppLogger.Log.Info("Device list empty");
return;
}

_selectionMenu.Items.Clear();
AppLogger.Log.Info("Set tray icon menu devices");
foreach (var item in _availableAudioDeviceWrappers)
foreach (var item in _availablePlaybackDevices)
{
_selectionMenu.Items.Add(new ToolStripDeviceItem(DeviceClicked, item));
}
if (_availableRecordingDevices.Count > 0)
{
_selectionMenu.Items.Add("-");
foreach (var item in _availableRecordingDevices)
{
_selectionMenu.Items.Add(new ToolStripDeviceItem(DeviceClicked, item));
}
}
_deviceListChanged = false;
}
}
Expand All @@ -212,13 +247,14 @@ private void DeviceClicked(object sender, EventArgs e)
}
}

/// <summary>
/// Notification that audio has changed
/// </summary>
/// <param name="deviceName"></param>
public void ShowAudioChanged(string deviceName)
private void ShowPlaybackChanged(string deviceName)
{
_trayIcon.ShowBalloonTip(500, "SoundSwitch: Playback device changed", deviceName, ToolTipIcon.Info);
}

private void ShowRecordingChanged(string deviceName)
{
_trayIcon.ShowBalloonTip(500, "SoundSwitch: Audio output changed", deviceName, ToolTipIcon.Info);
_trayIcon.ShowBalloonTip(500, "SoundSwitch: Recording device changed", deviceName, ToolTipIcon.Info);
}

/// <summary>
Expand All @@ -227,9 +263,7 @@ public void ShowAudioChanged(string deviceName)
public void ShowNoDevices()
{
AppLogger.Log.Error("No devices available");
_trayIcon.ShowBalloonTip(3000, "SoundSwitch: Configuration needed",
"No devices available to switch to. Open configuration by right-clicking on the SoundSwitch icon. ",
ToolTipIcon.Warning);
_trayIcon.ShowBalloonTip(3000, "SoundSwitch: Configuration needed", "No devices available to switch to. Open configuration by right-clicking on the SoundSwitch icon. ", ToolTipIcon.Warning);
}

/// <summary>
Expand Down

0 comments on commit df2de5b

Please sign in to comment.