-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernControls.cs
More file actions
447 lines (386 loc) · 17.5 KB
/
ModernControls.cs
File metadata and controls
447 lines (386 loc) · 17.5 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
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
namespace FolderSizeScanner;
// ═══════════════════════════════════════════════════════════════════
// ModernButton — flat, rounded, owner-drawn WinUI3-style button
// ═══════════════════════════════════════════════════════════════════
internal class ModernButton : Control
{
private bool _hover;
private bool _pressed;
private bool _isPrimary;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsPrimary
{
get => _isPrimary;
set { _isPrimary = value; Invalidate(); }
}
public ModernButton()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
Cursor = Cursors.Hand;
Font = ModernTheme.FontButton;
Size = new Size(80, 32);
}
protected override void OnMouseEnter(EventArgs e) { _hover = true; Invalidate(); base.OnMouseEnter(e); }
protected override void OnMouseLeave(EventArgs e) { _hover = false; _pressed = false; Invalidate(); base.OnMouseLeave(e); }
protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left) { _pressed = true; Invalidate(); } base.OnMouseDown(e); }
protected override void OnMouseUp(MouseEventArgs e) { _pressed = false; Invalidate(); base.OnMouseUp(e); }
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
var rect = new RectangleF(0.5f, 0.5f, Width - 1f, Height - 1f);
Color bg, fg, border;
if (!Enabled)
{
bg = ModernTheme.BtnDisabledBg;
fg = ModernTheme.BtnDisabledText;
border = ModernTheme.CardBorder;
}
else if (_isPrimary)
{
bg = _pressed ? ModernTheme.AccentPressed : _hover ? ModernTheme.AccentHover : ModernTheme.Accent;
fg = ModernTheme.TextOnAccent;
border = bg;
}
else
{
bg = _pressed ? ModernTheme.BtnPressed : _hover ? ModernTheme.BtnHover : ModernTheme.BtnRest;
fg = ModernTheme.TextPrimary;
border = ModernTheme.BtnBorder;
}
using var path = ModernTheme.RoundedRect(rect, ModernTheme.ButtonRadius);
using (var brush = new SolidBrush(bg))
g.FillPath(brush, path);
using (var pen = new Pen(border, 1f))
g.DrawPath(pen, path);
// Subtle bottom shadow line
if (Enabled && !_pressed)
{
var shadowColor = Color.FromArgb(20, 0, 0, 0);
using var shadowPen = new Pen(shadowColor, 1f);
g.DrawLine(shadowPen, rect.X + ModernTheme.ButtonRadius, rect.Bottom,
rect.Right - ModernTheme.ButtonRadius, rect.Bottom);
}
// Text
var font = (_isPrimary || (Font.Bold)) ? ModernTheme.FontButtonBold : ModernTheme.FontButton;
var textSize = g.MeasureString(Text, font);
var tx = (Width - textSize.Width) / 2f;
var ty = (Height - textSize.Height) / 2f;
using (var brush = new SolidBrush(fg))
g.DrawString(Text, font, brush, tx, ty);
}
}
// ═══════════════════════════════════════════════════════════════════
// ModernProgressBar — pill-shaped with shimmer animation
// ═══════════════════════════════════════════════════════════════════
internal class ModernProgressBar : Control
{
private bool _isMarquee;
private double _targetValue;
private double _displayValue;
private float _shimmerOffset;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsMarquee
{
get => _isMarquee;
set { _isMarquee = value; Invalidate(); }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public double Value
{
get => _targetValue;
set { _targetValue = Math.Clamp(value, 0, 100); Invalidate(); }
}
public ModernProgressBar()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
Height = ModernTheme.ProgressHeight;
}
public void AdvanceAnimation()
{
bool needsRedraw = false;
// Smooth interpolation toward target
if (Math.Abs(_displayValue - _targetValue) > 0.1)
{
_displayValue += (_targetValue - _displayValue) * 0.15;
needsRedraw = true;
}
else if (_displayValue != _targetValue)
{
_displayValue = _targetValue;
needsRedraw = true;
}
// Shimmer
_shimmerOffset += 3f;
if (_shimmerOffset > Width + 200)
_shimmerOffset = -200f;
needsRedraw = true;
if (needsRedraw) Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
int h = Height;
int radius = h / 2;
var trackRect = new RectangleF(0, 0, Width, h);
// Track
using (var trackPath = ModernTheme.RoundedRect(trackRect, radius))
using (var trackBrush = new SolidBrush(ModernTheme.TreeBarTrack))
g.FillPath(trackBrush, trackPath);
if (_isMarquee)
{
// Marquee: gaussian-ish glow sliding across
float glowWidth = Width * 0.3f;
float glowX = _shimmerOffset % (Width + glowWidth * 2) - glowWidth;
var glowRect = new RectangleF(glowX, 0, glowWidth, h);
if (glowRect.Width > 1)
{
using var glowPath = ModernTheme.RoundedRect(
RectangleF.Intersect(glowRect, trackRect), radius);
if (glowRect.Width > 0 && RectangleF.Intersect(glowRect, trackRect).Width > 1)
{
var inter = RectangleF.Intersect(glowRect, trackRect);
if (inter.Width > 1)
{
using var glowBrush = new LinearGradientBrush(
new RectangleF(glowX - 1, 0, glowWidth + 2, h),
Color.Transparent, Color.Transparent, 0f);
var blend = new ColorBlend(3)
{
Colors = [Color.Transparent, Color.FromArgb(140, ModernTheme.Accent), Color.Transparent],
Positions = [0f, 0.5f, 1f]
};
glowBrush.InterpolationColors = blend;
g.SetClip(ModernTheme.RoundedRect(trackRect, radius));
g.FillRectangle(glowBrush, glowX, 0, glowWidth, h);
g.ResetClip();
}
}
}
}
else if (_displayValue > 0)
{
// Determinate fill
float fillW = (float)(_displayValue / 100.0 * Width);
if (fillW < 1) fillW = 1;
var fillRect = new RectangleF(0, 0, fillW, h);
using (var clipPath = ModernTheme.RoundedRect(trackRect, radius))
{
g.SetClip(clipPath);
using (var fillBrush = new SolidBrush(ModernTheme.Accent))
g.FillRectangle(fillBrush, fillRect);
// Shimmer band
float shimmerW = 80f;
float sx = _shimmerOffset % (Width + shimmerW * 2) - shimmerW;
if (sx < fillW)
{
using var shimmerBrush = new LinearGradientBrush(
new RectangleF(sx - 1, 0, shimmerW + 2, h),
Color.Transparent, Color.Transparent, 0f);
var blend = new ColorBlend(3)
{
Colors = [Color.Transparent, Color.FromArgb(60, 255, 255, 255), Color.Transparent],
Positions = [0f, 0.5f, 1f]
};
shimmerBrush.InterpolationColors = blend;
g.FillRectangle(shimmerBrush, sx, 0, shimmerW, h);
}
g.ResetClip();
}
}
}
}
// ═══════════════════════════════════════════════════════════════════
// ModernTreeView — owner-drawn with size bars, hover, chevrons
// ═══════════════════════════════════════════════════════════════════
internal class ModernTreeView : TreeView
{
// P/Invoke for double-buffered treeview
private const int TVM_SETEXTENDEDSTYLE = 0x112C;
private const int TVS_EX_DOUBLEBUFFER = 0x0004;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private TreeNode? _hoverNode;
public ModernTreeView()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
DrawMode = TreeViewDrawMode.OwnerDrawAll;
ItemHeight = 28;
ShowLines = false;
FullRowSelect = true;
HotTracking = false;
Font = ModernTheme.FontTree;
BorderStyle = BorderStyle.None;
BackColor = ModernTheme.CardBg;
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
// Enable double buffering via extended style
SendMessage(Handle, TVM_SETEXTENDEDSTYLE, (IntPtr)TVS_EX_DOUBLEBUFFER, (IntPtr)TVS_EX_DOUBLEBUFFER);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
var node = GetNodeAt(e.X, e.Y);
if (node != _hoverNode)
{
_hoverNode = node;
Invalidate();
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
if (_hoverNode != null)
{
_hoverNode = null;
Invalidate();
}
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
if (e.Node == null) return;
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
var bounds = e.Bounds;
if (bounds.Height == 0) return;
var isSelected = e.Node == SelectedNode;
var isHover = e.Node == _hoverNode && !isSelected;
var isRoot = e.Node.Parent == null;
// ── Full-width row background ───────────────────────────────
var rowRect = new Rectangle(0, bounds.Y, ClientSize.Width, bounds.Height);
Color rowBg = isSelected ? ModernTheme.TreeRowSelected
: isHover ? ModernTheme.TreeRowHover
: ModernTheme.CardBg;
using (var brush = new SolidBrush(rowBg))
g.FillRectangle(brush, rowRect);
// ── Selection indicator (3px accent bar on left) ────────────
if (isSelected)
{
using var accentBrush = new SolidBrush(ModernTheme.Accent);
g.FillRectangle(accentBrush, 0, bounds.Y + 4, 3, bounds.Height - 8);
}
// ── Indent and chevron ──────────────────────────────────────
int indent = 12 + e.Node.Level * 20;
if (e.Node.Nodes.Count > 0)
{
var chevronX = indent - 2;
var chevronY = bounds.Y + bounds.Height / 2;
using var chevronBrush = new SolidBrush(ModernTheme.TextSecondary);
if (e.Node.IsExpanded)
{
// Down chevron (filled triangle)
var pts = new Point[]
{
new(chevronX - 4, chevronY - 2),
new(chevronX + 4, chevronY - 2),
new(chevronX, chevronY + 3)
};
g.FillPolygon(chevronBrush, pts);
}
else
{
// Right chevron (filled triangle)
var pts = new Point[]
{
new(chevronX - 2, chevronY - 4),
new(chevronX + 3, chevronY),
new(chevronX - 2, chevronY + 4)
};
g.FillPolygon(chevronBrush, pts);
}
}
int textX = indent + 10;
// ── Get folder info ─────────────────────────────────────────
var folder = e.Node.Tag as FolderInfo;
if (folder == null)
{
// Fallback: just draw text
using var fb = new SolidBrush(ModernTheme.TextPrimary);
g.DrawString(e.Node.Text, ModernTheme.FontTree, fb, textX, bounds.Y + 5);
return;
}
// Calculate percentage
long rootTotal = GetRootTotal(e.Node);
double pct = rootTotal > 0 ? (double)folder.TotalSize / rootTotal * 100 : 0;
// ── Folder name ─────────────────────────────────────────────
var nameFont = isRoot ? ModernTheme.FontTreeBold : ModernTheme.FontTree;
var nameStr = folder.Name;
var nameSize = g.MeasureString(nameStr, nameFont);
using (var nameBrush = new SolidBrush(ModernTheme.TextPrimary))
g.DrawString(nameStr, nameFont, nameBrush, textX, bounds.Y + (bounds.Height - nameSize.Height) / 2f);
float afterName = textX + nameSize.Width + 8;
// ── Size bar ────────────────────────────────────────────────
float barX = Math.Max(afterName, indent + 200);
float barMaxW = 160f;
float barH = 10f;
float barY = bounds.Y + (bounds.Height - barH) / 2f;
// Clamp bar position so it doesn't overflow
if (barX + barMaxW + 220 > ClientSize.Width)
barMaxW = Math.Max(60, ClientSize.Width - barX - 220);
var trackRect = new RectangleF(barX, barY, barMaxW, barH);
int barRadius = (int)(barH / 2);
// Track
using (var trackPath = ModernTheme.RoundedRect(trackRect, barRadius))
using (var trackBrush = new SolidBrush(ModernTheme.TreeBarTrack))
g.FillPath(trackBrush, trackPath);
// Fill proportional to percentage
float fillW = (float)(pct / 100.0 * barMaxW);
if (fillW > 1 && pct > 0.05)
{
if (fillW < 4) fillW = 4; // minimum visible
var fillRect = new RectangleF(barX, barY, fillW, barH);
using var clipPath = ModernTheme.RoundedRect(trackRect, barRadius);
g.SetClip(clipPath, CombineMode.Intersect);
// Gradient fill
var barColor = ModernTheme.GetBarColor(pct);
var barColorLight = ModernTheme.GetBarColorLight(pct);
using (var fillBrush = new LinearGradientBrush(
new RectangleF(barX, barY - 1, fillW, barH + 2),
barColorLight, barColor, 90f))
{
g.FillRectangle(fillBrush, fillRect);
}
// Top highlight
using (var highlightPen = new Pen(Color.FromArgb(80, 255, 255, 255), 1f))
g.DrawLine(highlightPen, barX + 2, barY + 1, barX + fillW - 2, barY + 1);
g.ResetClip();
}
// ── Size / pct / files text ─────────────────────────────────
float infoX = barX + barMaxW + 10;
var denied = folder.AccessDenied ? " [!]" : "";
var sizeStr = FormatSize(folder.TotalSize);
var infoStr = $"{sizeStr} {pct:N1}% [{folder.FileCount:N0} files]{denied}";
using (var infoBrush = new SolidBrush(ModernTheme.TextSecondary))
g.DrawString(infoStr, ModernTheme.FontDetail, infoBrush, infoX,
bounds.Y + (bounds.Height - g.MeasureString(infoStr, ModernTheme.FontDetail).Height) / 2f);
}
private static long GetRootTotal(TreeNode node)
{
var root = node;
while (root.Parent != null)
root = root.Parent;
return root.Tag is FolderInfo ri ? ri.TotalSize : 0;
}
private static string FormatSize(long bytes)
{
string[] units = ["B", "KB", "MB", "GB", "TB"];
double size = bytes;
int unit = 0;
while (size >= 1024 && unit < units.Length - 1)
{
size /= 1024;
unit++;
}
return unit == 0 ? $"{size:N0} {units[unit]}" : $"{size:N2} {units[unit]}";
}
}