Skip to content

Commit

Permalink
Optimize the update of the menu. Only on demand.
Browse files Browse the repository at this point in the history
Update images of the menu in Main thread. See #41
  • Loading branch information
Antoine Aflalo committed Dec 18, 2015
1 parent 83fa139 commit 481bad9
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions SoundSwitch/Util/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public sealed class TrayIcon : IDisposable
private readonly ContextMenuStrip _selectionMenu = new ContextMenuStrip();
private readonly ContextMenuStrip _settingsMenu = new ContextMenuStrip();
private readonly SynchronizationContext _context = SynchronizationContext.Current ?? new SynchronizationContext();
private bool _needToUpdateList = true;
private readonly NotifyIcon _trayIcon = new NotifyIcon
{
Icon = Icon.FromHandle(Resources.SoundSwitch16.GetHicon()),
Expand All @@ -60,6 +61,7 @@ public TrayIcon()
{
if (e.Button == MouseButtons.Left)
{
UpdateDeviceSelectionList();
_trayIcon.ContextMenuStrip = _selectionMenu;
var mi = typeof (NotifyIcon).GetMethod("ShowContextMenu",
BindingFlags.Instance | BindingFlags.NonPublic);
Expand Down Expand Up @@ -136,24 +138,24 @@ private void SetEventHandlers()
default:
throw new ArgumentOutOfRangeException();
}

UpdateImageContextMenu(audioDeviceType, audioChangeEvent);
_context.Send(s => { UpdateImageContextMenu(audioDeviceType, audioChangeEvent); }, null);

};
AppModel.Instance.SelectedDeviceChanged +=
(sender, deviceListChanged) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); };
(sender, @event) => { _needToUpdateList = true; };
AppModel.Instance.NewVersionReleased += (sender, @event) =>
{
_context.Send(s => { NewReleaseAvailable(sender, @event); }, null);
};

AppModel.Instance.DeviceRemoved +=
(sender, @event) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); };
(sender, @event) => { _needToUpdateList = true; };

AppModel.Instance.DeviceAdded +=
(sender, @event) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); };
(sender, @event) => { _needToUpdateList = true; };

AppModel.Instance.DeviceStateChanged +=
(sender, @event) => { _context.Send(s => { UpdateDeviceSelectionList(); }, null); };
(sender, @event) => { _needToUpdateList = true; };

AppModel.Instance.InitializeMain();
}
Expand Down Expand Up @@ -198,6 +200,11 @@ public void UpdateDeviceSelectionList()
{
using (AppLogger.Log.InfoCall())
{
if (!_needToUpdateList)
{
AppLogger.Log.Info("Device list doesn't need update");
return;
}
if (AppModel.Instance.AvailablePlaybackDevices.Count < 0 &&
AppModel.Instance.AvailableRecordingDevices.Count < 0)
{
Expand All @@ -220,6 +227,7 @@ public void UpdateDeviceSelectionList()
_selectionMenu.Items.Add(new ToolStripDeviceItem(DeviceClicked, item));
}
}
_needToUpdateList = false;
}
}

Expand Down

0 comments on commit 481bad9

Please sign in to comment.