forked from ToadsworthLP/desktoptale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowTracker.cs
98 lines (83 loc) · 2.81 KB
/
WindowTracker.cs
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
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace Desktoptale
{
public class WindowTracker
{
private IDictionary<IntPtr, TrackedWindow> trackedWindows;
private IDictionary<IntPtr, int> usages;
private MonitorManager monitorManager;
private bool changed = false;
private IEnumerator<TrackedWindow> windowUpdateEnumerator;
public WindowTracker(MonitorManager monitorManager)
{
this.monitorManager = monitorManager;
trackedWindows = new Dictionary<IntPtr, TrackedWindow>();
usages = new Dictionary<IntPtr, int>();
}
public void Update()
{
if (trackedWindows.Count == 0)
{
windowUpdateEnumerator?.Dispose();
return;
}
if (changed)
{
windowUpdateEnumerator?.Dispose();
windowUpdateEnumerator = null;
changed = false;
}
if (windowUpdateEnumerator == null)
{
windowUpdateEnumerator = trackedWindows.Values.GetEnumerator();
}
bool last = !windowUpdateEnumerator.MoveNext();
if (last)
{
windowUpdateEnumerator.Reset();
windowUpdateEnumerator.MoveNext();
}
TrackedWindow current = windowUpdateEnumerator.Current;
UpdateWindow(current);
}
public TrackedWindow Subscribe(WindowInfo window)
{
if (!trackedWindows.ContainsKey(window.hWnd))
{
trackedWindows.Add(window.hWnd, new TrackedWindow(window));
usages.Add(window.hWnd, 0);
changed = true;
}
usages[window.hWnd]++;
return trackedWindows[window.hWnd];
}
public void Unsubscribe(WindowInfo window)
{
var updatesUses = --usages[window.hWnd];
if (updatesUses <= 0)
{
trackedWindows.Remove(window.hWnd);
usages.Remove(window.hWnd);
changed = true;
}
}
private void UpdateWindow(TrackedWindow window)
{
Rectangle? rect = WindowsUtils.GetWindowRect(window.Window.hWnd);
if (rect.HasValue)
{
Rectangle val = rect.Value;
val.Location = monitorManager.ToMonoGameCoordinates(val.Location.ToVector2()).ToPoint();
window.Bounds = val;
}
else
{
window.Bounds = Rectangle.Empty;
window.NotifyWindowDestroyed();
changed = true;
}
}
}
}