vsdingextension/VsDingExtensionProject/VsDingExtensionProjectPacka...

164 lines
5.5 KiB
C#
Raw Normal View History

2015-06-05 21:48:04 +00:00
namespace VitaliiGanzha.VsDingExtension
2014-03-13 22:21:47 +00:00
{
2015-06-05 21:48:04 +00:00
using System;
using System.Diagnostics;
using System.Globalization;
using System.Media;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TestWindow.Extensibility;
using Process = System.Diagnostics.Process;
2014-03-13 22:21:47 +00:00
[PackageRegistration(UseManagedResourcesOnly = true)]
2015-06-05 22:02:21 +00:00
[InstalledProductRegistration("#110", "#112", "1.1", IconResourceID = 400)]
2014-03-13 22:21:47 +00:00
[Guid(GuidList.guidVsDingExtensionProjectPkgString)]
2014-03-20 23:28:02 +00:00
[ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
2015-06-01 01:02:52 +00:00
[ProvideOptionPage(typeof(OptionsDialog), "Ding", "Options", 0, 0, true)]
2015-06-08 19:05:20 +00:00
public sealed class VsDingExtensionProjectPackage : Package, IDisposable
2014-03-13 22:21:47 +00:00
{
private DTE2 applicationObject;
private AddIn addInInstance;
private BuildEvents buildEvents;
private DebuggerEvents debugEvents;
private SoundPlayer buildCompleteSoundPlayer;
private SoundPlayer debugSoundPlayer;
private SoundPlayer testCompleteSoundPlayer;
2015-06-05 21:48:04 +00:00
private OptionsDialog _options = null;
2015-06-01 01:02:52 +00:00
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
2014-03-13 22:21:47 +00:00
public VsDingExtensionProjectPackage()
{
2015-06-01 01:02:52 +00:00
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", ToString()));
2014-03-13 22:21:47 +00:00
}
#region Package Members
protected override void Initialize()
{
2015-06-01 01:02:52 +00:00
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", ToString()));
2014-03-13 22:21:47 +00:00
base.Initialize();
buildCompleteSoundPlayer = new SoundPlayer(Resources.build);
debugSoundPlayer = new SoundPlayer(Resources.debug);
testCompleteSoundPlayer = new SoundPlayer(Resources.ding);
applicationObject = (DTE2)GetService(typeof(DTE));
2015-06-01 01:02:52 +00:00
buildEvents = applicationObject.Events.BuildEvents;
debugEvents = applicationObject.Events.DebuggerEvents;
2014-03-13 22:21:47 +00:00
2015-06-01 01:02:52 +00:00
buildEvents.OnBuildDone += (scope, action) =>
{
2015-06-05 21:48:04 +00:00
if (Options.BuildBeep)
2015-06-01 01:02:52 +00:00
PlaySafe(buildCompleteSoundPlayer);
};
debugEvents.OnEnterBreakMode += delegate(dbgEventReason reason, ref dbgExecutionAction action)
{
2015-06-05 21:48:04 +00:00
if (reason != dbgEventReason.dbgEventReasonStep && Options.BreakpointBeep)
{
PlaySafe(debugSoundPlayer);
}
};
2014-03-13 22:21:47 +00:00
2014-03-20 23:28:02 +00:00
var componentModel =
2015-06-01 01:02:52 +00:00
GetGlobalService(typeof(SComponentModel)) as IComponentModel;
2014-03-13 22:21:47 +00:00
2014-03-20 23:28:02 +00:00
if (componentModel == null)
{
Debug.WriteLine("componentModel is null");
return;
2014-03-20 23:28:02 +00:00
}
var operationState = componentModel.GetService<IOperationState>();
operationState.StateChanged += OperationStateOnStateChanged;
2014-03-13 22:21:47 +00:00
}
2014-03-20 23:28:02 +00:00
2015-06-05 21:48:04 +00:00
private OptionsDialog Options
{
get
{
if (_options == null)
{
_options = (OptionsDialog)GetDialogPage(typeof(OptionsDialog));
}
return _options;
}
}
private void PlaySafe(SoundPlayer soundPlayer)
{
2015-06-05 21:48:04 +00:00
if (ShouldPlaySound())
2015-06-01 01:02:52 +00:00
{
2015-06-04 18:42:54 +00:00
try
{
soundPlayer.Play();
}
catch (Exception ex)
{
ActivityLog.LogError(GetType().FullName, ex.Message);
}
}
}
2015-06-05 21:48:04 +00:00
private bool ShouldPlaySound()
2014-03-20 23:28:02 +00:00
{
2015-06-05 21:48:04 +00:00
if (!Options.BeepOnUnfocus)
2014-03-20 23:28:02 +00:00
{
2015-06-05 21:48:04 +00:00
return true;
2014-03-20 23:28:02 +00:00
}
2015-06-05 21:48:04 +00:00
return Options.BeepOnUnfocus && !ApplicationIsActivated();
2014-03-20 23:28:02 +00:00
}
2014-03-13 22:21:47 +00:00
2015-06-05 21:48:04 +00:00
private void OperationStateOnStateChanged(object sender, OperationStateChangedEventArgs operationStateChangedEventArgs)
2015-06-01 01:02:52 +00:00
{
2015-06-05 21:48:04 +00:00
if (Options.TestBeep && operationStateChangedEventArgs.State.HasFlag(TestOperationStates.TestExecutionFinished))
{
PlaySafe(testCompleteSoundPlayer);
}
2015-06-01 01:02:52 +00:00
}
2015-06-05 21:48:04 +00:00
#endregion
2015-06-04 18:42:54 +00:00
public bool ApplicationIsActivated()
2015-06-01 01:02:52 +00:00
{
var activatedHandle = GetForegroundWindow();
if (activatedHandle == IntPtr.Zero)
{
return false; // No window is currently activated
}
var procId = Process.GetCurrentProcess().Id;
int activeProcId;
GetWindowThreadProcessId(activatedHandle, out activeProcId);
2015-06-04 18:42:54 +00:00
return activeProcId == procId;
2015-06-01 01:02:52 +00:00
}
2015-06-08 19:05:20 +00:00
public void Dispose()
{
SafeDispose(this.debugSoundPlayer);
SafeDispose(this.buildCompleteSoundPlayer);
SafeDispose(this.testCompleteSoundPlayer);
}
private void SafeDispose(SoundPlayer soundPlayer)
{
try
{
soundPlayer.Dispose();
}
catch (Exception ex)
{
ActivityLog.LogError(this.GetType().FullName, "Error when disposing player: " + ex.Message);
}
}
2014-03-13 22:21:47 +00:00
}
}