vsdingextension/VsDingExtensionProject/VsDingExtensionProjectPacka...

145 lines
5.3 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
2014-03-13 22:21:47 +00:00
using System.Globalization;
2015-06-01 01:02:52 +00:00
using System.IO;
2014-03-20 23:28:02 +00:00
using System.Media;
2014-03-13 22:21:47 +00:00
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
2014-03-20 23:28:02 +00:00
using Microsoft.VisualStudio.ComponentModelHost;
2014-03-13 22:21:47 +00:00
using Microsoft.VisualStudio.Shell;
2014-03-20 23:28:02 +00:00
using Microsoft.VisualStudio.TestWindow.Extensibility;
2015-06-01 01:02:52 +00:00
using Process = System.Diagnostics.Process;
2014-03-13 22:21:47 +00:00
namespace VitaliiGanzha.VsDingExtension
2014-03-13 22:21:47 +00:00
{
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[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)]
2014-03-13 22:21:47 +00:00
public sealed class VsDingExtensionProjectPackage : Package
{
private DTE2 applicationObject;
private AddIn addInInstance;
private BuildEvents buildEvents;
private DebuggerEvents debugEvents;
private SoundPlayer buildCompleteSoundPlayer;
private SoundPlayer debugSoundPlayer;
private SoundPlayer testCompleteSoundPlayer;
2015-06-01 01:02:52 +00:00
private bool onlyOnUnFocus;
private bool onBuild;
private bool onTestRunCompleted;
private bool onBreakpoint;
[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
2015-06-01 01:02:52 +00:00
protected override void OnSaveOptions(string key, Stream stream)
{
base.OnSaveOptions(key, stream);
ApplySettings();
}
2014-03-13 22:21:47 +00:00
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
ApplySettings();
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) =>
{
if (onBuild)
PlaySafe(buildCompleteSoundPlayer);
};
debugEvents.OnEnterBreakMode += delegate(dbgEventReason reason, ref dbgExecutionAction action)
{
2015-06-01 01:02:52 +00:00
if (reason != dbgEventReason.dbgEventReasonStep && onBreakpoint)
{
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
private void PlaySafe(SoundPlayer soundPlayer)
{
2015-06-01 01:02:52 +00:00
if (onlyOnUnFocus && !ApplicationIsActivated())
{
return;
}
try
{
soundPlayer.Play();
}
catch (Exception ex)
{
2015-06-01 01:02:52 +00:00
ActivityLog.LogError(GetType().FullName, ex.Message);
}
}
2014-03-20 23:28:02 +00:00
private void OperationStateOnStateChanged(object sender, OperationStateChangedEventArgs operationStateChangedEventArgs)
{
2015-06-01 01:02:52 +00:00
if (onTestRunCompleted && operationStateChangedEventArgs.State.HasFlag(TestOperationStates.TestExecutionFinished))
2014-03-20 23:28:02 +00:00
{
PlaySafe(testCompleteSoundPlayer);
2014-03-20 23:28:02 +00:00
}
}
2014-03-13 22:21:47 +00:00
#endregion
2015-06-01 01:02:52 +00:00
private void ApplySettings()
{
onlyOnUnFocus = (applicationObject.Properties["Ding", "Options"].Item("BeepOnUnfocus").Value as bool?) ?? false;
onBreakpoint = (applicationObject.Properties["Ding", "Options"].Item("BreakpointBeep").Value as bool?) ?? true;
onTestRunCompleted = (applicationObject.Properties["Ding", "Options"].Item("TestBeep").Value as bool?) ?? true;
onBuild = (applicationObject.Properties["Ding", "Options"].Item("BuildBeep").Value as bool?) ?? true;
Debug.WriteLine(string.Format("OnlyOnUnFocus: {0}", onlyOnUnFocus));
}
public static bool ApplicationIsActivated()
{
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);
return activeProcId == procId;
}
2014-03-13 22:21:47 +00:00
}
}