-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathToolWindowMonitorService.cs
More file actions
106 lines (93 loc) · 3.51 KB
/
ToolWindowMonitorService.cs
File metadata and controls
106 lines (93 loc) · 3.51 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Threading;
using CodingWithCalvin.LaunchyBar.Models;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace CodingWithCalvin.LaunchyBar.Services;
/// <summary>
/// Monitors tool window visibility and updates IsActive on configured launch items.
/// </summary>
public sealed class ToolWindowMonitorService : IDisposable
{
private readonly AsyncPackage _package;
private readonly IConfigurationService _configurationService;
private readonly DispatcherTimer _timer;
private bool _disposed;
private static readonly Dictionary<string, Guid> ToolWindowGuids = new(StringComparer.OrdinalIgnoreCase)
{
{ "View.SolutionExplorer", new Guid(ToolWindowGuids80.SolutionExplorer) },
{ "View.Output", new Guid(ToolWindowGuids80.Outputwindow) },
{ "View.ErrorList", new Guid(ToolWindowGuids80.ErrorList) },
{ "View.TaskList", new Guid(ToolWindowGuids80.TaskList) },
{ "View.Toolbox", new Guid(ToolWindowGuids80.Toolbox) },
{ "View.PropertiesWindow", new Guid(ToolWindowGuids80.PropertiesWindow) },
{ "View.ClassView", new Guid(ToolWindowGuids80.ClassView) },
{ "View.Terminal", new Guid("d212f56b-c48a-434c-a121-1c5d80b59b9f") },
{ "View.GitWindow", new Guid("1c64b9c2-e352-428e-a56d-0ace190b99a6") },
};
public ToolWindowMonitorService(AsyncPackage package, IConfigurationService configurationService)
{
_package = package;
_configurationService = configurationService;
_timer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(500)
};
_timer.Tick += OnTimerTick;
_timer.Start();
}
private void OnTimerTick(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
UpdateActiveStates();
}
private void UpdateActiveStates()
{
ThreadHelper.ThrowIfNotOnUIThread();
var shell = _package.GetService<SVsUIShell, IVsUIShell>();
if (shell == null) return;
// Build a set of visible tool window GUIDs
var visibleGuids = new HashSet<Guid>();
shell.GetToolWindowEnum(out var windowEnum);
if (windowEnum != null)
{
var frames = new IVsWindowFrame[1];
while (windowEnum.Next(1, frames, out var fetched) == 0 && fetched == 1)
{
var frame = frames[0];
if (frame == null) continue;
try
{
frame.GetGuidProperty((int)__VSFPROPID.VSFPROPID_GuidPersistenceSlot, out var persistGuid);
frame.IsOnScreen(out var isOnScreen);
if (isOnScreen != 0)
{
visibleGuids.Add(persistGuid);
}
}
catch
{
// Some frames may throw
}
}
}
// Update IsActive on each configured tool window item
foreach (var item in _configurationService.Configuration.Items
.Where(i => i.Type == LaunchItemType.ToolWindow))
{
if (ToolWindowGuids.TryGetValue(item.Target, out var guid))
{
item.IsActive = visibleGuids.Contains(guid);
}
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
_timer.Stop();
_timer.Tick -= OnTimerTick;
}
}