-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_fsd.py
More file actions
474 lines (411 loc) · 18.2 KB
/
Copy pathwrite_fsd.py
File metadata and controls
474 lines (411 loc) · 18.2 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import os
base = r"E:\C#\BetterExplorerEx\BetterExplorer.Controls"
# ── XAML ────────────────────────────────────────────────────────────────
xaml = r'''<?xml version="1.0" encoding="utf-8"?>
<ContentDialog
x:Class="BetterExplorer.Controls.FolderSizeDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Folder / Drive Size"
CloseButtonText="Close"
DefaultButton="Close">
<Grid Width="900" RowSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="460" />
</Grid.RowDefinitions>
<TextBlock x:Name="PathLabel"
Grid.Row="0"
FontSize="12"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
TextWrapping="NoWrap"
TextTrimming="CharacterEllipsis" />
<ProgressBar x:Name="ScanProgress"
Grid.Row="1"
Height="4"
IsIndeterminate="False"
Minimum="0" Maximum="100" Value="0" />
<TextBlock x:Name="StatusText"
Grid.Row="2"
FontSize="11"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="Scanning..." />
<Grid Grid.Row="3" ColumnSpacing="16">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="460" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0"
x:Name="PieCanvasWrapper"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Canvas x:Name="PieCanvas"
Width="420" Height="420"
HorizontalAlignment="Center"
VerticalAlignment="Center"
PointerMoved="PieCanvas_PointerMoved"
PointerExited="PieCanvas_PointerExited" />
<Popup x:Name="SlicePopup"
IsLightDismissEnabled="False"
IsOpen="False">
<Border Background="{ThemeResource AcrylicBackgroundFillColorDefaultBrush}"
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
BorderThickness="1"
CornerRadius="6"
Padding="10,6"
MaxWidth="280">
<StackPanel Spacing="2">
<TextBlock x:Name="TooltipName"
FontSize="13" FontWeight="SemiBold"
TextWrapping="Wrap" />
<TextBlock x:Name="TooltipSize"
FontSize="12"
Foreground="{ThemeResource TextFillColorSecondaryBrush}" />
<TextBlock x:Name="TooltipPercent"
FontSize="11"
Foreground="{ThemeResource TextFillColorTertiaryBrush}" />
</StackPanel>
</Border>
</Popup>
</Grid>
<ScrollViewer Grid.Column="1"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled">
<ItemsControl x:Name="LegendItems">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,3" ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="14" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Width="12" Height="12"
RadiusX="2" RadiusY="2"
VerticalAlignment="Center">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Color}" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Grid.Column="1"
Text="{Binding Name}"
FontSize="12"
TextTrimming="CharacterEllipsis"
VerticalAlignment="Center" />
<TextBlock Grid.Column="2"
Text="{Binding SizeText}"
FontSize="12"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
VerticalAlignment="Center" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Grid>
</ContentDialog>
'''
# ── Code-behind ─────────────────────────────────────────────────────────
cs = r'''using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using Microsoft.UI;
using Windows.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace BetterExplorer.Controls
{
// ── Data models ──────────────────────────────────────────────────────────
internal sealed class FolderSizeEntry
{
public string Name { get; init; } = string.Empty;
public string Path { get; init; } = string.Empty;
public long Bytes { get; set; }
public Color Color { get; init; }
public string SizeText => FormatBytes(Bytes);
// Geometry for hit-testing (start/sweep in radians)
public double StartAngle { get; set; }
public double SweepAngle { get; set; }
private static string FormatBytes(long b)
{
if (b >= 1L << 30) return $"{b / (double)(1L << 30):F2} GB";
if (b >= 1L << 20) return $"{b / (double)(1L << 20):F1} MB";
if (b >= 1L << 10) return $"{b / (double)(1L << 10):F0} KB";
return $"{b} B";
}
}
// ── Dialog ───────────────────────────────────────────────────────────────
public sealed partial class FolderSizeDialog : ContentDialog
{
// Palette – enough for up to 20 slices before wrapping
private static readonly Color[] _palette =
[
Color.FromArgb(255, 70, 130, 180), Color.FromArgb(255, 255, 127, 14),
Color.FromArgb(255, 44, 160, 44), Color.FromArgb(255, 214, 39, 40),
Color.FromArgb(255, 148, 103, 189), Color.FromArgb(255, 140, 86, 75),
Color.FromArgb(255, 227, 119, 194), Color.FromArgb(255, 127, 127, 127),
Color.FromArgb(255, 188, 189, 34), Color.FromArgb(255, 23, 190, 207),
Color.FromArgb(255, 174, 199, 232), Color.FromArgb(255, 255, 187, 120),
Color.FromArgb(255, 152, 223, 138), Color.FromArgb(255, 255, 152, 150),
Color.FromArgb(255, 197, 176, 213), Color.FromArgb(255, 196, 156, 148),
Color.FromArgb(255, 247, 182, 210), Color.FromArgb(255, 199, 199, 199),
Color.FromArgb(255, 219, 219, 141), Color.FromArgb(255, 158, 218, 229),
];
private readonly string _rootPath;
private readonly List<FolderSizeEntry> _entries = [];
private long _totalBytes;
private long _scannedCount;
// Redraw throttle
private long _pendingRedraw;
private CancellationTokenSource _cts = new();
public FolderSizeDialog(string path)
{
InitializeComponent();
_rootPath = path;
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
PathLabel.Text = _rootPath;
_ = ScanAsync(_cts.Token);
}
// ── Scanning ─────────────────────────────────────────────────────────
private async Task ScanAsync(CancellationToken ct)
{
try
{
// Enumerate immediate subdirs
string[] dirs;
try { dirs = Directory.GetDirectories(_rootPath); }
catch { dirs = []; }
long looseFiles = 0;
try
{
foreach (var f in Directory.EnumerateFiles(_rootPath))
looseFiles += new FileInfo(f).Length;
}
catch { /* ignore */ }
// Kick off parallel dir scans
var tasks = new List<Task>();
int idx = 0;
foreach (var dir in dirs)
{
if (ct.IsCancellationRequested) break;
var entry = new FolderSizeEntry
{
Name = System.IO.Path.GetFileName(dir),
Path = dir,
Color = _palette[idx % _palette.Length],
};
_entries.Add(entry);
int capturedIdx = idx++;
tasks.Add(Task.Run(async () =>
{
long size = await Task.Run(() => RecurseDirectory(dir, ct), ct);
entry.Bytes = size;
Interlocked.Add(ref _totalBytes, size);
Interlocked.Increment(ref _scannedCount);
TryIncrementalRedraw((int)((double)_scannedCount / Math.Max(dirs.Length, 1) * 100));
}, ct));
}
await Task.WhenAll(tasks);
ct.ThrowIfCancellationRequested();
// Add loose-files slice
if (looseFiles > 0)
{
_entries.Add(new FolderSizeEntry
{
Name = "Files in root",
Path = _rootPath,
Bytes = looseFiles,
Color = Color.FromArgb(255, 160, 160, 160),
});
Interlocked.Add(ref _totalBytes, looseFiles);
}
// Add free space slice for drives
try
{
var di = new DriveInfo(System.IO.Path.GetPathRoot(_rootPath)!);
if (di.IsReady && di.AvailableFreeSpace > 0)
{
_entries.Add(new FolderSizeEntry
{
Name = "Free space",
Path = string.Empty,
Bytes = di.AvailableFreeSpace,
Color = Color.FromArgb(255, 200, 200, 200),
});
Interlocked.Add(ref _totalBytes, di.AvailableFreeSpace);
}
}
catch { /* not a drive root */ }
// Final draw
DispatcherQueue.TryEnqueue(() =>
{
DrawPieChart();
RebuildLegend();
ScanProgress.Value = 100;
StatusText.Text = $"Total: {FolderSizeEntry_FormatBytes(_totalBytes)}";
});
}
catch (OperationCanceledException) { }
}
private static long RecurseDirectory(string path, CancellationToken ct)
{
long total = 0;
try
{
foreach (var f in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
{
if (ct.IsCancellationRequested) break;
try { total += new FileInfo(f).Length; } catch { }
}
}
catch { }
return total;
}
private static string FolderSizeEntry_FormatBytes(long b)
{
if (b >= 1L << 30) return $"{b / (double)(1L << 30):F2} GB";
if (b >= 1L << 20) return $"{b / (double)(1L << 20):F1} MB";
if (b >= 1L << 10) return $"{b / (double)(1L << 10):F0} KB";
return $"{b} B";
}
// ── Incremental redraw throttle ───────────────────────────────────────
private void TryIncrementalRedraw(int progressPct)
{
if (Interlocked.CompareExchange(ref _pendingRedraw, 1, 0) != 0) return;
DispatcherQueue.TryEnqueue(async () =>
{
DrawPieChart();
RebuildLegend();
ScanProgress.Value = progressPct;
StatusText.Text = $"Scanning… ({progressPct}%)";
await Task.Delay(120);
Interlocked.Exchange(ref _pendingRedraw, 0);
});
}
// ── Pie-chart drawing ─────────────────────────────────────────────────
private void DrawPieChart()
{
PieCanvas.Children.Clear();
double total = _totalBytes;
if (total <= 0) return;
const double cx = 210, cy = 210, r = 200;
double angle = -Math.PI / 2; // start at top
foreach (var entry in _entries)
{
if (entry.Bytes <= 0) continue;
double sweep = entry.Bytes / total * 2 * Math.PI;
entry.StartAngle = angle;
entry.SweepAngle = sweep;
var path = BuildSlice(cx, cy, r, angle, sweep, entry.Color);
PieCanvas.Children.Add(path);
angle += sweep;
}
}
private static Microsoft.UI.Xaml.Shapes.Path BuildSlice(
double cx, double cy, double r,
double startAngle, double sweepAngle,
Color fill)
{
double endAngle = startAngle + sweepAngle;
double x1 = cx + r * Math.Cos(startAngle);
double y1 = cy + r * Math.Sin(startAngle);
double x2 = cx + r * Math.Cos(endAngle);
double y2 = cy + r * Math.Sin(endAngle);
bool largeArc = sweepAngle > Math.PI;
var fig = new PathFigure
{
StartPoint = new Windows.Foundation.Point(cx, cy),
IsClosed = true,
IsFilled = true,
};
fig.Segments.Add(new LineSegment { Point = new Windows.Foundation.Point(x1, y1) });
fig.Segments.Add(new ArcSegment
{
Point = new Windows.Foundation.Point(x2, y2),
Size = new Windows.Foundation.Size(r, r),
SweepDirection = SweepDirection.Clockwise,
IsLargeArc = largeArc,
});
var geo = new PathGeometry();
geo.Figures.Add(fig);
return new Microsoft.UI.Xaml.Shapes.Path
{
Data = geo,
Fill = new SolidColorBrush(fill),
Stroke = new SolidColorBrush(Colors.White),
StrokeThickness = 1.5,
};
}
// ── Legend ────────────────────────────────────────────────────────────
private void RebuildLegend()
{
LegendItems.ItemsSource = null;
LegendItems.ItemsSource = new List<FolderSizeEntry>(_entries);
}
// ── Slice hit-testing (pointer events) ───────────────────────────────
private void PieCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
var pt = e.GetCurrentPoint(PieCanvas).Position;
const double cx = 210, cy = 210, r = 200;
double dx = pt.X - cx;
double dy = pt.Y - cy;
double dist = Math.Sqrt(dx * dx + dy * dy);
if (dist > r || _totalBytes <= 0)
{
HideTooltip();
return;
}
double angle = Math.Atan2(dy, dx);
// Normalise to the same domain used when drawing (starting at -PI/2)
double baseAngle = -Math.PI / 2;
double a = NormaliseAngle(angle - baseAngle);
FolderSizeEntry? hit = null;
foreach (var entry in _entries)
{
if (entry.SweepAngle <= 0) continue;
double s = NormaliseAngle(entry.StartAngle - baseAngle);
double e2 = s + entry.SweepAngle;
if (a >= s && a < e2) { hit = entry; break; }
}
if (hit is null) { HideTooltip(); return; }
double pct = _totalBytes > 0 ? hit.Bytes * 100.0 / _totalBytes : 0;
TooltipName.Text = hit.Name;
TooltipSize.Text = hit.SizeText;
TooltipPercent.Text = $"{pct:F1} %";
SlicePopup.HorizontalOffset = pt.X + 16;
SlicePopup.VerticalOffset = pt.Y - 10;
SlicePopup.IsOpen = true;
}
private void PieCanvas_PointerExited(object sender, PointerRoutedEventArgs e)
=> HideTooltip();
private void HideTooltip() => SlicePopup.IsOpen = false;
private static double NormaliseAngle(double a)
{
const double pi2 = 2 * Math.PI;
while (a < 0) a += pi2;
while (a >= pi2) a -= pi2;
return a;
}
}
}
'''
with open(os.path.join(base, "FolderSizeDialog.xaml"), "w", encoding="utf-8") as f:
f.write(xaml)
with open(os.path.join(base, "FolderSizeDialog.xaml.cs"), "w", encoding="utf-8") as f:
f.write(cs)
print("Written OK")