-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDebugStateService.cs
More file actions
89 lines (73 loc) · 2.81 KB
/
DebugStateService.cs
File metadata and controls
89 lines (73 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Linq;
using CodingWithCalvin.LaunchyBar.Models;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
namespace CodingWithCalvin.LaunchyBar.Services;
/// <summary>
/// Service that monitors debug state and updates the debug item's icon accordingly.
/// </summary>
public sealed class DebugStateService : IDisposable
{
private readonly DTE2? _dte;
private readonly DebuggerEvents? _debuggerEvents;
private readonly IConfigurationService _configurationService;
private bool _disposed;
public DebugStateService(AsyncPackage package, IConfigurationService configurationService)
{
_configurationService = configurationService;
ThreadHelper.ThrowIfNotOnUIThread();
_dte = package.GetService<DTE, DTE2>();
if (_dte != null)
{
_debuggerEvents = _dte.Events.DebuggerEvents;
_debuggerEvents.OnEnterRunMode += OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode += OnEnterBreakMode;
_debuggerEvents.OnEnterDesignMode += OnEnterDesignMode;
// Set initial state
UpdateDebugIcon(_dte.Debugger.CurrentMode != dbgDebugMode.dbgDesignMode);
}
}
private void OnEnterRunMode(dbgEventReason reason)
{
UpdateDebugIcon(true);
}
private void OnEnterBreakMode(dbgEventReason reason, ref dbgExecutionAction executionAction)
{
UpdateDebugIcon(true);
}
private void OnEnterDesignMode(dbgEventReason reason)
{
UpdateDebugIcon(false);
}
private void UpdateDebugIcon(bool isDebugging)
{
var debugItem = _configurationService.Configuration.Items
.FirstOrDefault(i => i.Id == "debug" || i.Target == "Debug.Start");
if (debugItem != null)
{
debugItem.IconPath = isDebugging ? "KnownMonikers.Stop" : "KnownMonikers.Run";
debugItem.Name = isDebugging ? "Stop Debugging" : "Start Debugging";
}
var startWithoutDebuggingItem = _configurationService.Configuration.Items
.FirstOrDefault(i => i.Id == "start-without-debugging" || i.Target == "Debug.StartWithoutDebugging");
if (startWithoutDebuggingItem != null)
{
startWithoutDebuggingItem.IconPath = isDebugging ? "KnownMonikers.Stop" : "KnownMonikers.RunOutline";
startWithoutDebuggingItem.Name = isDebugging ? "Stop Debugging" : "Start Without Debugging";
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
ThreadHelper.ThrowIfNotOnUIThread();
if (_debuggerEvents != null)
{
_debuggerEvents.OnEnterRunMode -= OnEnterRunMode;
_debuggerEvents.OnEnterBreakMode -= OnEnterBreakMode;
_debuggerEvents.OnEnterDesignMode -= OnEnterDesignMode;
}
}
}