-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChart_TableGridChart.cs
More file actions
416 lines (358 loc) · 13.4 KB
/
Copy pathChart_TableGridChart.cs
File metadata and controls
416 lines (358 loc) · 13.4 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace c2flux
{
public sealed class Chart_TableGridChart : AntdUI.Table
{
private readonly ContextMenuStrip _contextMenu =
new ContextMenuStrip();
private List<EntryChartItem> _rows = new List<EntryChartItem>();
private FileSystemEntry _entry;
private EntryChartItem _contextMenuRow;
private bool _showFiles;
public Chart_TableGridChart()
{
ConfigureTableGridChart();
}
public void SetEntry(FileSystemEntry entry)
{
_entry = entry;
BindEntryRows();
}
public void SetShowFiles(bool showFiles)
{
if (_showFiles == showFiles)
return;
_showFiles = showFiles;
BindEntryRows();
}
public void SetColumnVisible(string columnKey, bool visible)
{
AntdUI.Column column = Columns.FirstOrDefault(
currentColumn => string.Equals(
currentColumn.Key,
columnKey,
StringComparison.Ordinal));
if (column == null)
return;
column.Visible = visible;
if (string.Equals(
columnKey,
nameof(EntryChartItem.SizeBytes),
StringComparison.Ordinal))
{
AntdUI.Column sizeMbColumn = Columns.FirstOrDefault(
currentColumn => string.Equals(
currentColumn.Key,
nameof(EntryChartItem.SizeMb),
StringComparison.Ordinal));
if (sizeMbColumn != null)
sizeMbColumn.Visible = visible;
}
LoadLayout();
Invalidate();
}
public void ApplyEntryGridColumnWidths()
{
LoadLayout();
Invalidate();
}
public void ApplyLocalizedTexts()
{
SetColumnTitle(
nameof(EntryChartItem.Name),
LocalizationService.GetText("Common.Name"));
SetColumnTitle(
nameof(EntryChartItem.Percent),
LocalizationService.GetText("Chart.TableUsage"));
SetColumnTitle(
nameof(EntryChartItem.SizeBytes),
LocalizationService.GetText("Advanced.SizeGb"));
SetColumnTitle(
nameof(EntryChartItem.SizeMb),
LocalizationService.GetText("Advanced.SizeMb"));
SetColumnTitle(
nameof(EntryChartItem.FullPath),
LocalizationService.GetText("Common.Path"));
LoadLayout();
Invalidate();
}
private void ConfigureTableGridChart()
{
Dock = DockStyle.Fill;
FixedHeader = true;
VisibleHeader = true;
EnableHeaderResizing = true;
ColumnDragSort = false;
MultipleRows = false;
LostFocusClearSelection = false;
MouseClickPenetration = true;
ScrollBarAvoidHeader = true;
AutoSizeColumnsMode = AntdUI.ColumnsMode.Fill;
ShowTip = true;
EmptyHeader = true;
EmptyText = string.Empty;
Columns = new AntdUI.ColumnCollection
{
new AntdUI.Column(
nameof(EntryChartItem.Name),
LocalizationService.GetText("Common.Name"))
{
Width = "22%",
MinWidth = "120",
Ellipsis = false,
SortOrder = true,
Render = (value, record, rowIndex) =>
{
string text =
record is EntryChartItem row
? row.Name
: string.Empty;
return AntdThemeService.CreateVisibleTableCellText(
text);
}
},
new AntdUI.Column(
nameof(EntryChartItem.Percent),
LocalizationService.GetText("Chart.TableUsage"),
AntdUI.ColumnAlign.Center)
{
Width =
(AntdThemeService.TableProgressWidth +
(AntdThemeService.TableCellHorizontalPadding * 2))
.ToString(),
SortOrder = true,
Render = (value, record, rowIndex) =>
{
double percent = record is EntryChartItem row
? row.Percent
: 0D;
float progressValue =
(float)Math.Clamp(percent / 100D, 0D, 1D);
return new PercentCellProgress(
progressValue,
$"{percent:0.0} %");
}
},
new AntdUI.Column(
nameof(EntryChartItem.SizeBytes),
LocalizationService.GetText("Advanced.SizeGb"),
AntdUI.ColumnAlign.Right)
{
Width = "auto",
Ellipsis = true,
SortOrder = true,
Render = (value, record, rowIndex) =>
{
long sizeBytes = record is EntryChartItem row
? row.SizeBytes
: 0L;
return (sizeBytes /
(1024D * 1024D * 1024D))
.ToString("N2") + " GB";
}
},
new AntdUI.Column(
nameof(EntryChartItem.SizeMb),
LocalizationService.GetText("Advanced.SizeMb"),
AntdUI.ColumnAlign.Right)
{
Width = "auto",
Ellipsis = true,
SortOrder = true,
Render = (value, record, rowIndex) =>
{
double sizeMb = record is EntryChartItem row
? row.SizeMb
: 0D;
return sizeMb.ToString("N0") + " MB";
}
},
new AntdUI.Column(
nameof(EntryChartItem.FullPath),
LocalizationService.GetText("Common.Path"))
{
Width = "fill",
MinWidth = "180",
Ellipsis = false,
SortOrder = true,
Render = (value, record, rowIndex) =>
{
string text =
record is EntryChartItem row
? row.FullPath
: string.Empty;
return AntdThemeService.CreateVisibleTableCellText(
text);
}
}
};
MouseDown += Chart_TableGridChart_MouseDown;
CellClickBegin += Chart_TableGridChart_CellClickBegin;
ToolStripMenuItem openInExplorerItem =
new ToolStripMenuItem(
LocalizationService.GetText(
"Context.OpenInExplorer"));
openInExplorerItem.Click +=
OpenInExplorerItem_Click;
_contextMenu.Items.Add(openInExplorerItem);
_contextMenu.Opening +=
(sender, e) =>
e.Cancel =
_contextMenuRow == null;
AntdThemeService.ConfigureContextMenu(
_contextMenu);
ContextMenuStrip = _contextMenu;
AntdThemeService.ApplyTable(this);
BindEntryRows();
}
private void Chart_TableGridChart_MouseDown(
object sender,
MouseEventArgs e)
{
_contextMenuRow = null;
}
private void Chart_TableGridChart_CellClickBegin(
object sender,
AntdUI.TableClickBeginEventArgs e)
{
dynamic eventArgs = e;
_contextMenuRow =
eventArgs.Record as EntryChartItem;
}
private void OpenInExplorerItem_Click(
object sender,
EventArgs e)
{
if (_contextMenuRow == null ||
string.IsNullOrWhiteSpace(
_contextMenuRow.FullPath))
{
return;
}
string targetPath = _contextMenuRow.FullPath;
if (!File.Exists(targetPath) &&
!Directory.Exists(targetPath))
{
return;
}
string arguments = File.Exists(targetPath)
? "/select,\"" + targetPath + "\""
: "\"" + targetPath + "\"";
System.Diagnostics.Process.Start(
new System.Diagnostics.ProcessStartInfo
{
FileName = "explorer.exe",
Arguments = arguments,
UseShellExecute = true
});
}
private void BindEntryRows()
{
if (_entry == null)
{
_rows = new List<EntryChartItem>();
DataSource = _rows;
return;
}
List<FileSystemEntry> visibleEntries = _showFiles
? GetLargestFilesRecursive(_entry, 100)
: _entry.Children
.Where(child => child.IsDirectory)
.ToList();
long totalSize = visibleEntries.Sum(child => child.SizeBytes);
_rows = visibleEntries
.Select(child => new EntryChartItem
{
Name = child.Name,
FullPath = child.FullPath,
SizeBytes = child.SizeBytes,
FormattedSize = SizeFormatter.Format(child.SizeBytes),
SizeMb = child.SizeBytes / (1024D * 1024D),
Percent = totalSize <= 0
? 0D
: (double)child.SizeBytes * 100D / totalSize
})
.OrderByDescending(row => row.SizeBytes)
.ToList();
DataSource = _rows;
}
private void SetColumnTitle(string key, string title)
{
AntdUI.Column column = Columns.FirstOrDefault(
currentColumn => string.Equals(
currentColumn.Key,
key,
StringComparison.Ordinal));
if (column != null)
column.Title = title;
}
private static List<FileSystemEntry> GetLargestFilesRecursive(
FileSystemEntry rootEntry,
int maximumFileCount)
{
PriorityQueue<FileSystemEntry, long> largestFiles =
new PriorityQueue<FileSystemEntry, long>();
Stack<FileSystemEntry> pendingEntries =
new Stack<FileSystemEntry>();
pendingEntries.Push(rootEntry);
while (pendingEntries.Count > 0)
{
FileSystemEntry currentEntry = pendingEntries.Pop();
foreach (FileSystemEntry child in currentEntry.Children)
{
if (child.IsDirectory)
{
pendingEntries.Push(child);
continue;
}
largestFiles.Enqueue(child, child.SizeBytes);
if (largestFiles.Count > maximumFileCount)
largestFiles.Dequeue();
}
}
List<FileSystemEntry> result =
new List<FileSystemEntry>(largestFiles.Count);
while (largestFiles.Count > 0)
result.Add(largestFiles.Dequeue());
return result;
}
private sealed class PercentCellProgress : AntdUI.CellProgress
{
private readonly string _text;
public PercentCellProgress(float value, string text)
: base(value)
{
_text = text;
Radius = AntdThemeService.TableProgressRadius;
Back = AntdThemeService.TableProgressBackColor;
Fill = AntdThemeService.TableProgressFillColor;
Size = new Size(
AntdThemeService.TableProgressWidth,
AntdThemeService.TableProgressHeight);
}
public override void Paint(
AntdUI.Canvas g,
Font font,
bool enable,
SolidBrush fore)
{
base.Paint(g, font, enable, fore);
g.String(_text, font, fore, Rect);
}
}
private sealed class EntryChartItem
{
public string Name { get; set; }
public string FullPath { get; set; }
public long SizeBytes { get; set; }
public string FormattedSize { get; set; }
public double SizeMb { get; set; }
public double Percent { get; set; }
}
}
}