diff --git a/README.md b/README.md index 4e7ce28..147c3d0 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ This small extension will play notification sounds when following events occur: - Entering debugger mode (breakpoint hit, etc) - Unit tests finished to run +Ability to toggle when the notifications sound can be found in the Visual Studio Options under 'Ding' + Useful when working with big solutions or when build/test run/hitting a breakpoint takes a lot of time and developer can be distructed on other things while he waits. This is an open source project, join! diff --git a/VsDingExtensionFor2012/VsDingExtensionFor2012.csproj b/VsDingExtensionFor2012/VsDingExtensionFor2012.csproj index 99d32d8..7ea7d51 100644 --- a/VsDingExtensionFor2012/VsDingExtensionFor2012.csproj +++ b/VsDingExtensionFor2012/VsDingExtensionFor2012.csproj @@ -128,6 +128,9 @@ + + OptionsDialog.cs + diff --git a/VsDingExtensionFor2012/source.extension.vsixmanifest b/VsDingExtensionFor2012/source.extension.vsixmanifest index 263c0b0..5fb814c 100644 --- a/VsDingExtensionFor2012/source.extension.vsixmanifest +++ b/VsDingExtensionFor2012/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + Visual Studio Ding extension for VS 2012 This small extension will play notification sounds when following events occur: - Build Complete diff --git a/VsDingExtensionProject/OptionsDialog.cs b/VsDingExtensionProject/OptionsDialog.cs new file mode 100644 index 0000000..d1f1d9a --- /dev/null +++ b/VsDingExtensionProject/OptionsDialog.cs @@ -0,0 +1,39 @@ +using Microsoft.VisualStudio.Shell; +using System; +using System.ComponentModel; +using System.Runtime.InteropServices; + +namespace VitaliiGanzha.VsDingExtension +{ + [ClassInterface(ClassInterfaceType.AutoDual)] + [CLSCompliant(false), ComVisible(true)] + public class OptionsDialog : DialogPage + { + [Category("Beeps")] + [DisplayName("Breakpoint")] + [Description("Beep when a breakpoint is hit")] + public bool BreakpointBeep { get; set; } + + [Category("Beeps")] + [DisplayName("Build")] + [Description("Beep when a build is completed")] + public bool BuildBeep { get; set; } + + [Category("Beeps")] + [DisplayName("Tests")] + [Description("Beep when a test run is completed")] + public bool TestBeep { get; set; } + + [DisplayName("Only when in background")] + [Description("Beep only when Visual Studio does not have focus")] + public bool BeepOnUnfocus { get; set; } + + public OptionsDialog() + { + BreakpointBeep = true; + BuildBeep = true; + TestBeep = true; + BeepOnUnfocus = false; + } + } +} diff --git a/VsDingExtensionProject/VsDingExtensionProject.csproj b/VsDingExtensionProject/VsDingExtensionProject.csproj index 87e8733..8f36d7d 100644 --- a/VsDingExtensionProject/VsDingExtensionProject.csproj +++ b/VsDingExtensionProject/VsDingExtensionProject.csproj @@ -148,6 +148,9 @@ + + Component + True True diff --git a/VsDingExtensionProject/VsDingExtensionProjectPackage.cs b/VsDingExtensionProject/VsDingExtensionProjectPackage.cs index e249c8e..11fd5d0 100644 --- a/VsDingExtensionProject/VsDingExtensionProjectPackage.cs +++ b/VsDingExtensionProject/VsDingExtensionProjectPackage.cs @@ -1,13 +1,16 @@ using System; using System.Diagnostics; using System.Globalization; +using System.IO; using System.Media; using System.Runtime.InteropServices; +using System.Windows.Forms; using EnvDTE; using EnvDTE80; using Microsoft.VisualStudio.ComponentModelHost; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.TestWindow.Extensibility; +using Process = System.Diagnostics.Process; namespace VitaliiGanzha.VsDingExtension { @@ -15,6 +18,7 @@ namespace VitaliiGanzha.VsDingExtension [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] [Guid(GuidList.guidVsDingExtensionProjectPkgString)] [ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")] + [ProvideOptionPage(typeof(OptionsDialog), "Ding", "Options", 0, 0, true)] public sealed class VsDingExtensionProjectPackage : Package { private DTE2 applicationObject; @@ -24,18 +28,33 @@ namespace VitaliiGanzha.VsDingExtension private SoundPlayer buildCompleteSoundPlayer; private SoundPlayer debugSoundPlayer; private SoundPlayer testCompleteSoundPlayer; + 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); public VsDingExtensionProjectPackage() { - Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString())); + Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", ToString())); } #region Package Members + protected override void OnSaveOptions(string key, Stream stream) + { + base.OnSaveOptions(key, stream); + ApplySettings(); + } + protected override void Initialize() { - Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", - this.ToString())); + Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", ToString())); base.Initialize(); buildCompleteSoundPlayer = new SoundPlayer(Resources.build); @@ -43,21 +62,25 @@ namespace VitaliiGanzha.VsDingExtension testCompleteSoundPlayer = new SoundPlayer(Resources.ding); applicationObject = (DTE2)GetService(typeof(DTE)); + ApplySettings(); + buildEvents = applicationObject.Events.BuildEvents; + debugEvents = applicationObject.Events.DebuggerEvents; - this.buildEvents = applicationObject.Events.BuildEvents; - this.debugEvents = applicationObject.Events.DebuggerEvents; - - buildEvents.OnBuildDone += (scope, action) => PlaySafe(buildCompleteSoundPlayer); + buildEvents.OnBuildDone += (scope, action) => + { + if (onBuild) + PlaySafe(buildCompleteSoundPlayer); + }; debugEvents.OnEnterBreakMode += delegate(dbgEventReason reason, ref dbgExecutionAction action) { - if (reason != dbgEventReason.dbgEventReasonStep) + if (reason != dbgEventReason.dbgEventReasonStep && onBreakpoint) { PlaySafe(debugSoundPlayer); } }; var componentModel = - Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel; + GetGlobalService(typeof(SComponentModel)) as IComponentModel; if (componentModel == null) { @@ -71,25 +94,50 @@ namespace VitaliiGanzha.VsDingExtension private void PlaySafe(SoundPlayer soundPlayer) { - try + if (onlyOnUnFocus && !ApplicationIsActivated()) { - soundPlayer.Play(); - } - catch (Exception ex) - { - ActivityLog.LogError(this.GetType().FullName, ex.Message); + try + { + soundPlayer.Play(); + } + catch (Exception ex) + { + ActivityLog.LogError(GetType().FullName, ex.Message); + } } } private void OperationStateOnStateChanged(object sender, OperationStateChangedEventArgs operationStateChangedEventArgs) { - - if (operationStateChangedEventArgs.State.HasFlag(TestOperationStates.TestExecutionFinished)) + if (onTestRunCompleted && operationStateChangedEventArgs.State.HasFlag(TestOperationStates.TestExecutionFinished)) { PlaySafe(testCompleteSoundPlayer); } } #endregion + 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 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; + } + } } diff --git a/VsDingExtensionProject/source.extension.vsixmanifest b/VsDingExtensionProject/source.extension.vsixmanifest index bb228de..3a27c71 100644 --- a/VsDingExtensionProject/source.extension.vsixmanifest +++ b/VsDingExtensionProject/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + Visual Studio Ding extension This small extension will play notification sounds when following events occur: - Build Complete