This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
/
OverlayIconManager.cs
227 lines (204 loc) · 5.65 KB
/
OverlayIconManager.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using ICSharpCode.Core;
using ICSharpCode.Core.WinForms;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.Svn
{
/// <summary>
/// Static class managing the retrieval of the Subversion icons on a worker thread.
/// </summary>
public static class OverlayIconManager
{
static Bitmap statusImages;
public static Bitmap StatusImages {
get {
if (statusImages == null) {
statusImages = WinFormsResourceService.GetBitmap("Icons.Svn.StatusImages");
}
return statusImages;
}
}
enum StatusIcon {
Empty = 0,
OK,
Added,
Deleted,
Info,
Empty2,
Exclamation,
PropertiesModified,
Unknown,
Modified
}
static Image[] statusIcons = new Image[10];
static Image GetImage(StatusIcon status)
{
int index = (int)status;
if (statusIcons[index] == null) {
Bitmap statusImages = StatusImages;
Bitmap smallImage = new Bitmap(7, 10);
using (Graphics g = Graphics.FromImage(smallImage)) {
//g.DrawImageUnscaled(statusImages, -index * 7, -3);
Rectangle srcRect = new Rectangle(index * 7, 3, 7, 10);
Rectangle destRect = new Rectangle(0, 0, 7, 10);
g.DrawImage(statusImages, destRect, srcRect, GraphicsUnit.Pixel);
//g.DrawLine(Pens.Black, 0, 0, 7, 10);
}
statusIcons[index] = smallImage;
}
return statusIcons[index];
}
public static Image GetImage(StatusKind status)
{
switch (status) {
case StatusKind.Added:
return GetImage(StatusIcon.Added);
case StatusKind.Deleted:
return GetImage(StatusIcon.Deleted);
case StatusKind.Modified:
case StatusKind.Replaced:
return GetImage(StatusIcon.Modified);
case StatusKind.Normal:
return GetImage(StatusIcon.OK);
case StatusKind.Conflicted:
case StatusKind.Obstructed:
return GetImage(StatusIcon.Exclamation);
default:
return null;
}
}
static Queue<AbstractProjectBrowserTreeNode> queue = new Queue<AbstractProjectBrowserTreeNode>();
static bool threadRunning;
public static void Enqueue(AbstractProjectBrowserTreeNode node)
{
if (subversionDisabled)
return;
lock (queue) {
queue.Enqueue(node);
if (!threadRunning) {
threadRunning = true;
ThreadPool.QueueUserWorkItem(Run);
}
}
}
public static void EnqueueRecursive(AbstractProjectBrowserTreeNode node)
{
if (subversionDisabled)
return;
lock (queue) {
queue.Enqueue(node);
// use breadth-first search
Queue<AbstractProjectBrowserTreeNode> q = new Queue<AbstractProjectBrowserTreeNode>();
q.Enqueue(node);
while (q.Count > 0) {
node = q.Dequeue();
foreach (TreeNode n in node.Nodes) {
node = n as AbstractProjectBrowserTreeNode;
if (node != null) {
q.Enqueue(node);
queue.Enqueue(node);
}
}
}
if (!threadRunning) {
threadRunning = true;
ThreadPool.QueueUserWorkItem(Run);
}
}
}
static readonly object clientLock = new object();
static SvnClientWrapper client;
static bool subversionDisabled;
public static bool SubversionDisabled {
get { return subversionDisabled; }
}
static void Run(object state)
{
LoggingService.Debug("SVN: OverlayIconManager Thread started");
// sleep a tiny bit to give main thread time to add more jobs to the queue
Thread.Sleep(2);
while (true) {
if (ICSharpCode.SharpDevelop.ParserService.LoadSolutionProjectsThreadRunning) {
// Run OverlayIconManager much more slowly while solution is being loaded.
// This prevents the disk from seeking too much
Thread.Sleep(100);
}
AbstractProjectBrowserTreeNode node;
lock (queue) {
if (queue.Count == 0) {
threadRunning = false;
ClearStatusCache();
LoggingService.Debug("SVN: OverlayIconManager Thread finished");
return;
}
node = queue.Dequeue();
}
try {
RunStep(node);
} catch (Exception ex) {
MessageService.ShowException(ex);
}
}
}
public static void ClearStatusCache()
{
lock (clientLock) {
if (client != null) {
client.ClearStatusCache();
}
}
}
public static StatusKind GetStatus(string fileName)
{
lock (clientLock) {
if (subversionDisabled)
return StatusKind.None;
//Console.WriteLine(fileName);
if (client == null) {
try {
client = new SvnClientWrapper();
} catch (Exception ex) {
subversionDisabled = true;
SharpDevelop.Gui.WorkbenchSingleton.SafeThreadAsyncCall(
MessageService.ShowWarning,
"Error initializing Subversion library:\n" + ex.ToString()
);
return StatusKind.None;
}
}
return client.SingleStatus(fileName).TextStatus;
}
}
static void RunStep(AbstractProjectBrowserTreeNode node)
{
if (node.IsDisposed) return;
FileNode fileNode = node as FileNode;
StatusKind status;
if (fileNode != null) {
status = GetStatus(fileNode.FileName);
} else {
DirectoryNode directoryNode = node as DirectoryNode;
if (directoryNode != null) {
status = GetStatus(directoryNode.Directory);
} else {
SolutionNode solNode = node as SolutionNode;
if (solNode != null) {
status = GetStatus(solNode.Solution.Directory);
} else {
return;
}
}
}
SharpDevelop.Gui.WorkbenchSingleton.SafeThreadAsyncCall(
delegate {
node.Overlay = GetImage(status);
});
}
}
}