174 lines
9.4 KiB
C#
174 lines
9.4 KiB
C#
using System.Drawing.Drawing2D;
|
||
|
||
namespace UCVL.VPNPlus;
|
||
|
||
internal enum DashboardIcon { Server, Settings, Database, Upload, Download, Document, Chart, History, Shield }
|
||
|
||
// Code-drawn icons keep the existing brand assets and remain crisp at high DPI.
|
||
internal static class DashboardDrawing
|
||
{
|
||
internal static void Icon(Graphics g, DashboardIcon icon, Rectangle bounds, Color color)
|
||
{
|
||
var state = g.Save();
|
||
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||
g.TranslateTransform(bounds.Left, bounds.Top);
|
||
g.ScaleTransform(bounds.Width / 24f, bounds.Height / 24f);
|
||
using var pen = new Pen(color, 1.8f) { StartCap = LineCap.Round, EndCap = LineCap.Round, LineJoin = LineJoin.Round };
|
||
switch (icon)
|
||
{
|
||
case DashboardIcon.Server:
|
||
g.DrawRectangle(pen, 2, 3, 20, 7); g.DrawRectangle(pen, 2, 14, 20, 7);
|
||
g.DrawLine(pen, 6, 6, 6, 7); g.DrawLine(pen, 6, 17, 6, 18);
|
||
g.DrawLine(pen, 15, 6.5f, 18, 6.5f); g.DrawLine(pen, 15, 17.5f, 18, 17.5f); break;
|
||
case DashboardIcon.Settings:
|
||
g.DrawEllipse(pen, 5, 5, 14, 14); g.DrawEllipse(pen, 9, 9, 6, 6);
|
||
for (var i = 0; i < 8; i++) { var a = i * Math.PI / 4; g.DrawLine(pen, 12 + (float)Math.Cos(a) * 8, 12 + (float)Math.Sin(a) * 8, 12 + (float)Math.Cos(a) * 10, 12 + (float)Math.Sin(a) * 10); } break;
|
||
case DashboardIcon.Database:
|
||
g.DrawEllipse(pen, 3, 3, 18, 6); g.DrawLine(pen, 3, 6, 3, 18); g.DrawLine(pen, 21, 6, 21, 18);
|
||
g.DrawArc(pen, 3, 9, 18, 6, 0, 180); g.DrawArc(pen, 3, 15, 18, 6, 0, 180); break;
|
||
case DashboardIcon.Upload:
|
||
case DashboardIcon.Download:
|
||
var up = icon == DashboardIcon.Upload;
|
||
g.DrawLine(pen, 12, 3, 12, 16);
|
||
g.DrawLines(pen, up ? [new PointF(7, 8), new(12, 3), new(17, 8)] : [new PointF(7, 11), new(12, 16), new(17, 11)]);
|
||
g.DrawLines(pen, [new PointF(3, 16), new(3, 21), new(21, 21), new(21, 16)]); break;
|
||
case DashboardIcon.Document:
|
||
g.DrawLines(pen, [new PointF(5, 2), new(15, 2), new(20, 7), new(20, 22), new(5, 22), new(5, 2)]);
|
||
g.DrawLines(pen, [new PointF(15, 2), new(15, 7), new(20, 7)]);
|
||
g.DrawLine(pen, 9, 12, 16, 12); g.DrawLine(pen, 9, 16, 16, 16); break;
|
||
case DashboardIcon.Chart:
|
||
for (var i = 0; i < 4; i++) g.DrawLine(pen, 4 + i * 5, 21, 4 + i * 5, 16 - i * 4); break;
|
||
case DashboardIcon.History:
|
||
g.DrawArc(pen, 2, 2, 20, 20, -85, 350); g.DrawLine(pen, 12, 2, 12, 12); g.DrawLine(pen, 12, 12, 22, 12); break;
|
||
case DashboardIcon.Shield:
|
||
using (var shield = new GraphicsPath())
|
||
{
|
||
shield.AddLines([new PointF(12, 1), new(22, 5), new(22, 12)]);
|
||
shield.AddBezier(22, 12, 22, 18, 15, 22, 12, 24);
|
||
shield.AddBezier(12, 24, 9, 22, 2, 18, 2, 12);
|
||
shield.AddLine(2, 12, 2, 5); shield.CloseFigure();
|
||
using var fill = new LinearGradientBrush(new Point(2, 2), new Point(22, 24), Color.FromArgb(100, color), color);
|
||
g.FillPath(fill, shield);
|
||
}
|
||
using (var check = new Pen(Color.White, 2.4f) { StartCap = LineCap.Round, EndCap = LineCap.Round, LineJoin = LineJoin.Round })
|
||
g.DrawLines(check, [new PointF(7, 12), new(10.5f, 15.5f), new(17, 9)]);
|
||
break;
|
||
}
|
||
g.Restore(state);
|
||
}
|
||
}
|
||
|
||
internal sealed class DashboardGlyph : Control
|
||
{
|
||
internal DashboardIcon Glyph { get; set; }
|
||
internal DashboardGlyph(DashboardIcon glyph) { Glyph = glyph; DoubleBuffered = true; SetStyle(ControlStyles.SupportsTransparentBackColor, true); BackColor = Color.Transparent; ForeColor = Brand.RoyalBlue; }
|
||
protected override void OnPaint(PaintEventArgs e)
|
||
{
|
||
base.OnPaint(e);
|
||
var side = Math.Min(Width, Height) - 4;
|
||
if (side > 0) DashboardDrawing.Icon(e.Graphics, Glyph, new((Width - side) / 2, (Height - side) / 2, side, side), ForeColor);
|
||
}
|
||
}
|
||
|
||
internal sealed class BrandHeaderPanel : Panel
|
||
{
|
||
internal BrandHeaderPanel() { DoubleBuffered = true; ResizeRedraw = true; }
|
||
protected override void OnPaintBackground(PaintEventArgs e)
|
||
{
|
||
ScenicTheme.Backdrop(e.Graphics, ClientSize, DeviceDpi, header: true);
|
||
}
|
||
}
|
||
|
||
internal sealed class MetricCard : CardPanel
|
||
{
|
||
internal DashboardIcon Glyph { get; }
|
||
internal string Heading { get; }
|
||
internal string ValueText { get; private set; } = "—";
|
||
internal string UnitText { get; private set; } = "";
|
||
internal string Hint { get; private set; } = "仅代理流量";
|
||
internal MetricCard(string heading, DashboardIcon glyph) { Heading = heading; Glyph = glyph; AccessibleName = heading; }
|
||
internal void SetBytes(double? bytes, bool speed = false, string? hint = null)
|
||
{
|
||
string[] units = ["B", "KB", "MB", "GB", "TB"];
|
||
var value = bytes.HasValue && double.IsFinite(bytes.Value) ? Math.Max(0, bytes.Value) : (double?)null;
|
||
var unit = 0;
|
||
while (value >= 1024 && unit < units.Length - 1) { value /= 1024; unit++; }
|
||
ValueText = value.HasValue ? (value == 0 ? "0" : value.Value.ToString("0.##")) : "—";
|
||
UnitText = value.HasValue ? units[unit] + (speed ? "/s" : "") : "";
|
||
Hint = hint ?? "仅代理流量";
|
||
AccessibleDescription = $"{Heading} {ValueText} {UnitText},{Hint}";
|
||
Invalidate();
|
||
}
|
||
protected override void OnPaint(PaintEventArgs e)
|
||
{
|
||
base.OnPaint(e);
|
||
var s = DeviceDpi / 96f;
|
||
int S(int n) => (int)Math.Round(n * s);
|
||
var g = e.Graphics;
|
||
DashboardDrawing.Icon(g, Glyph, new(S(10), S(18), S(22), S(22)), Glyph == DashboardIcon.Download ? Brand.Cyan : Brand.RoyalBlue);
|
||
var x = S(49); var right = Width - S(12); var available = right - x;
|
||
using var headingFont = Brand.Font(9);
|
||
using var unitFont = Brand.Font(9);
|
||
using var hintFont = Brand.Font(7.8f);
|
||
var paintFlags = TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping;
|
||
TextRenderer.DrawText(g, Heading, headingFont, new Rectangle(x, S(10), available, S(23)), Brand.Text, paintFlags | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
|
||
var flags = TextFormatFlags.NoPadding | TextFormatFlags.SingleLine | paintFlags;
|
||
var unitWidth = TextRenderer.MeasureText(g, UnitText, unitFont, Size.Empty, flags).Width;
|
||
var size = 22f;
|
||
Font valueFont;
|
||
while (true)
|
||
{
|
||
valueFont = Brand.Font(size, FontStyle.Bold);
|
||
if (TextRenderer.MeasureText(g, ValueText, valueFont, Size.Empty, flags).Width + unitWidth + S(5) <= available || size <= 13) break;
|
||
valueFont.Dispose(); size--;
|
||
}
|
||
using (valueFont)
|
||
{
|
||
var valueWidth = TextRenderer.MeasureText(g, ValueText, valueFont, Size.Empty, flags).Width;
|
||
TextRenderer.DrawText(g, ValueText, valueFont, new Rectangle(x, S(31), available - unitWidth - S(4), S(32)), Brand.Navy, flags | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
|
||
TextRenderer.DrawText(g, UnitText, unitFont, new Rectangle(x + valueWidth + S(4), S(40), unitWidth, S(23)), Brand.Muted, flags | TextFormatFlags.VerticalCenter);
|
||
}
|
||
TextRenderer.DrawText(g, Hint, hintFont, new Rectangle(x, S(64), available, S(17)), Brand.Muted, paintFlags | TextFormatFlags.EndEllipsis);
|
||
}
|
||
}
|
||
|
||
internal sealed class BadgeLabel : Label
|
||
{
|
||
internal BadgeLabel() { DoubleBuffered = true; ResizeRedraw = true; }
|
||
protected override void OnPaint(PaintEventArgs e)
|
||
{
|
||
if (Width < 4 || Height < 4) return;
|
||
if (Parent is { } parent)
|
||
{
|
||
var state = e.Graphics.Save();
|
||
e.Graphics.TranslateTransform(-Left, -Top);
|
||
InvokePaintBackground(parent, new PaintEventArgs(e.Graphics, parent.ClientRectangle));
|
||
e.Graphics.Restore(state);
|
||
}
|
||
else e.Graphics.Clear(Brand.Surface);
|
||
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
|
||
using var path = UiGeometry.RoundedRectangle(new(0, 0, Width - 1, Height - 1), (Height - 1) / 2);
|
||
using var fill = new SolidBrush(BackColor);
|
||
e.Graphics.FillPath(fill, path);
|
||
TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor,
|
||
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
|
||
}
|
||
}
|
||
|
||
internal sealed class ReadOnlyModeLabel : Control
|
||
{
|
||
internal ReadOnlyModeLabel() { DoubleBuffered = true; ResizeRedraw = true; BackColor = Color.White; Font = Brand.Font(9.5f); AccessibleName = "当前代理模式(连接中不可修改)"; }
|
||
protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); Invalidate(); }
|
||
protected override void OnPaint(PaintEventArgs e)
|
||
{
|
||
if (Width < 10 || Height < 10) return;
|
||
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
|
||
using var path = UiGeometry.RoundedRectangle(new(1, 1, Width - 3, Height - 3), Math.Max(3, DeviceDpi / 24));
|
||
using var border = new Pen(Color.FromArgb(204, 219, 237));
|
||
e.Graphics.DrawPath(border, path);
|
||
var pad = DeviceDpi / 12;
|
||
TextRenderer.DrawText(e.Graphics, Text, Font, new Rectangle(pad, 0, Width - pad * 2, Height), Brand.Text,
|
||
TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
|
||
}
|
||
}
|