using System.Drawing.Drawing2D; namespace UCVL.VPNPlus; internal enum DashboardIcon { Server, Settings, Database, Upload, Download, Document, Chart, History, Shield, Layers, ShieldPending, ShieldAlert } // 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, bool featured = false) { 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.65f) { 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.Layers: g.DrawPolygon(pen, [new PointF(2, 7), new(12, 2), new(22, 7), new(12, 12)]); g.DrawLines(pen, [new PointF(2, 12), new(12, 17), new(22, 12)]); g.DrawLines(pen, [new PointF(2, 17), new(12, 22), new(22, 17)]); break; case DashboardIcon.Shield: case DashboardIcon.ShieldPending: case DashboardIcon.ShieldAlert: using (var shield = new GraphicsPath()) { shield.AddLines([new PointF(12, 1.2f), new(22, 5.3f), new(22, 11.8f)]); shield.AddBezier(22, 11.8f, 22, 17.8f, 15, 21.8f, 12, 23); shield.AddBezier(12, 23, 9, 21.8f, 2, 17.8f, 2, 11.8f); shield.AddLine(2, 11.8f, 2, 5.3f); shield.CloseFigure(); using var fill = new LinearGradientBrush(new Point(2, 2), new Point(22, 24), featured ? Color.FromArgb(125, 107, 215, 255) : Color.FromArgb(176, color), color); g.FillPath(fill, shield); if (featured) { using var halo = new Pen(Color.FromArgb(100, 114, 217, 255), 1.9f) { LineJoin = LineJoin.Round }; using var edge = new Pen(Color.FromArgb(244, 213, 245, 255), .85f) { LineJoin = LineJoin.Round }; g.DrawPath(halo, shield); g.DrawPath(edge, shield); } } using (var mark = new Pen(Color.White, featured ? 1.9f : 2.25f) { StartCap = LineCap.Round, EndCap = LineCap.Round, LineJoin = LineJoin.Round }) { if (icon == DashboardIcon.Shield) g.DrawLines(mark, [new PointF(7, 12), new(10.5f, 15.5f), new(17, 9)]); else if (icon == DashboardIcon.ShieldAlert) { g.DrawLine(mark, 12, 7, 12, 12); g.FillEllipse(Brushes.White, 10.8f, 14.5f, 2.4f, 2.4f); } else for (var x = 7; x <= 17; x += 5) g.FillEllipse(Brushes.White, x - 1.2f, 10.8f, 2.4f, 2.4f); } break; } g.Restore(state); } } internal sealed class DashboardGlyph : Control { private DashboardIcon _glyph; private bool _featured; internal DashboardIcon Glyph { get => _glyph; set { if (_glyph != value) { _glyph = value; Invalidate(); } } } internal bool Featured { get => _featured; set { if (_featured != value) { _featured = value; Invalidate(); } } } 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, Featured); } } 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 bool InlineTotal { get; set; } internal MetricCard(string heading, DashboardIcon glyph) { Heading = heading; Glyph = glyph; AccessibleName = heading; } internal void SetBytes(double? bytes, bool speed = false, string? hint = null) { if (speed) { (ValueText, UnitText) = SpeedDisplay.Parts(bytes); Hint = hint ?? "仅代理流量"; AccessibleDescription = $"{Heading} {ValueText} {UnitText},{Hint}"; Invalidate(); return; } 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; if (InlineTotal) { using var divider = new Pen(Color.FromArgb(145, 148, 188, 218), Math.Max(1, s * .7f)); g.DrawLine(divider, 0, S(12), 0, Height - S(12)); using var heading = Brand.Font(10); using var value = Brand.Font(12, FontStyle.Bold); var inlineFlags = ChartAxis.PaintFlags | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding | TextFormatFlags.EndEllipsis; var headingWidth = S(39); TextRenderer.DrawText(g, Heading.Replace("代理流量", ""), heading, new Rectangle(S(12), 0, headingWidth, Height), Brand.Muted, inlineFlags); TextRenderer.DrawText(g, (ValueText + " " + UnitText).TrimEnd(), value, new Rectangle(S(12) + headingWidth, 0, Math.Max(1, Width - S(18) - headingWidth), Height), Brand.Navy, inlineFlags); return; } if(Height Items { get; } = []; internal DashboardIcon? LeadingIcon { get; set; } private int _selectedIndex = -1; private bool _hovered; private ContextMenuStrip? _popup; private long _itemsRevision; internal event EventHandler? SelectedIndexChanged; internal int SelectedIndex { get => _selectedIndex; set { var next = value >= 0 && value < Items.Count ? value : -1; if (_selectedIndex == next) return; _selectedIndex = next; Text = SelectedItem?.ToString() ?? ""; Invalidate(); AccessibilityNotifyClients(AccessibleEvents.ValueChange, -1); SelectedIndexChanged?.Invoke(this, EventArgs.Empty); } } internal object? SelectedItem { get => _selectedIndex >= 0 && _selectedIndex < Items.Count ? Items[_selectedIndex] : null; set => SelectedIndex = value is null ? -1 : Items.IndexOf(value); } internal bool DroppedDown { get => _popup?.Visible == true; set { if (value) OpenPopup(); else _popup?.Close(); } } internal void BeginUpdate() { // Periodic profile refresh may reorder the authorized nodes. Invalidate // even a queued click from the old popup before its list is changed. _itemsRevision++; DroppedDown = false; } internal void EndUpdate() { if (_selectedIndex >= Items.Count) _selectedIndex = -1; var oldText = Text; Text = SelectedItem?.ToString() ?? ""; if (Text != oldText) AccessibilityNotifyClients(AccessibleEvents.ValueChange, -1); Invalidate(); } internal DashboardCombo() { SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.Selectable, true); BackColor = Color.Transparent; ForeColor = Brand.Text; Cursor = Cursors.Hand; TabStop = true; AccessibleRole = AccessibleRole.ComboBox; } protected override AccessibleObject CreateAccessibilityInstance() => new SelectorAccessibleObject(this); protected override void OnMouseEnter(EventArgs e) { _hovered = true; Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { _hovered = false; Invalidate(); base.OnMouseLeave(e); } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (e.Button != MouseButtons.Left || !Enabled) return; Focus(); DroppedDown = !DroppedDown; } protected override bool IsInputKey(Keys keyData) => keyData is Keys.Up or Keys.Down || base.IsInputKey(keyData); protected override void OnKeyDown(KeyEventArgs e) { if (e.KeyCode is Keys.Enter or Keys.Space or Keys.F4) { DroppedDown = !DroppedDown; e.Handled = true; } else if (e.KeyCode is Keys.Up or Keys.Down) { SelectedIndex = Math.Clamp(SelectedIndex + (e.KeyCode == Keys.Up ? -1 : 1), 0, Math.Max(0, Items.Count - 1)); e.Handled = true; } else if (e.KeyCode == Keys.Escape) { DroppedDown = false; e.Handled = true; } base.OnKeyDown(e); } protected override void OnEnabledChanged(EventArgs e) { if (!Enabled) DroppedDown = false; Invalidate(); base.OnEnabledChanged(e); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (Width < 10 || Height < 10) return; var s = DeviceDpi / 96f; e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; using var path = UiGeometry.RoundedRectangle(new(1, 1, Width - 3, Height - 3), Math.Max(4, (int)((Height >= 28*s ? 9 : 5)*s))); using var fill = new SolidBrush(Color.FromArgb(Enabled ? 208 : 115, 245, 250, 255)); using var border = new Pen(Color.FromArgb(_hovered && Enabled ? 125 : 155, 151, 185, 213), Math.Max(1, s * .65f)); e.Graphics.FillPath(fill, path); e.Graphics.DrawPath(border, path); var pad = (int)(7*s); var arrowX = Width - (int)(10*s); var arrowY = Height / 2; if (LeadingIcon is { } icon && Height >= 26*s) { var side = (int)(19*s); DashboardDrawing.Icon(e.Graphics, icon, new Rectangle(pad + 2, (Height - side)/2, side, side), Enabled ? Brand.RoyalBlue : Brand.Muted); pad += side + (int)(8*s); } TextRenderer.DrawText(e.Graphics, SelectedItem?.ToString() ?? "选择节点", Font, new Rectangle(pad, 0, Math.Max(1, Width-pad-(int)(20*s)), Height), Enabled ? ForeColor : Brand.Muted, ChartAxis.PaintFlags | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding | TextFormatFlags.EndEllipsis); using var arrow = new Pen(Enabled ? Brand.RoyalBlue : Brand.Muted, Math.Max(1, s)); e.Graphics.DrawLines(arrow, [new PointF(arrowX-3*s,arrowY-1*s),new(arrowX,arrowY+2*s),new(arrowX+3*s,arrowY-1*s)]); if (Focused && ShowFocusCues) ControlPaint.DrawFocusRectangle(e.Graphics, Rectangle.Inflate(ClientRectangle, -(int)(4*s), -(int)(4*s))); } private void OpenPopup() { if (!Enabled || Items.Count == 0 || DroppedDown) return; _popup?.Dispose(); _popup = new ContextMenuStrip { BackColor = Color.FromArgb(247, 251, 255), ForeColor = Brand.Text, Font = Font, ShowImageMargin = false, ShowCheckMargin = false, Padding = new Padding(4), Renderer = new ToolStripProfessionalRenderer(new SelectorColors()), AutoSize = true }; for (var index = 0; index < Items.Count; index++) { var selectedItem = Items[index]; var revision = _itemsRevision; var option = new ToolStripMenuItem(selectedItem.ToString()) { AutoSize = false, Size = new Size(Math.Max(Width, (int)(170*DeviceDpi/96f)), (int)(26*DeviceDpi/96f)), Padding = new Padding(8, 2, 8, 2) }; option.Click += (_, _) => { if (!Enabled || revision != _itemsRevision) return; // Match the original item, never whichever item now occupies // its numeric index. Removed/replaced items cannot be chosen. var currentIndex = Items.FindIndex(item => ReferenceEquals(item, selectedItem)); if (currentIndex >= 0) SelectedIndex = currentIndex; }; _popup.Items.Add(option); } _popup.Closed += (_, _) => { Invalidate(); AccessibilityNotifyClients(AccessibleEvents.StateChange, -1); }; _popup.Show(this, new Point(0, Height)); Invalidate(); AccessibilityNotifyClients(AccessibleEvents.StateChange, -1); } protected override void Dispose(bool disposing) { if (disposing) _popup?.Dispose(); base.Dispose(disposing); } private sealed class SelectorAccessibleObject(DashboardCombo owner) : ControlAccessibleObject(owner) { public override AccessibleRole Role => AccessibleRole.ComboBox; public override string? Value { get => owner.SelectedItem?.ToString() ?? ""; set { } } public override string? Description => owner.AccessibleDescription ?? "选择当前可用选项"; public override string? DefaultAction => owner.DroppedDown ? "关闭选项" : "打开选项"; public override AccessibleStates State => base.State | (owner.DroppedDown ? AccessibleStates.Expanded : AccessibleStates.Collapsed) | AccessibleStates.HasPopup; public override void DoDefaultAction() { if (!owner.Enabled) return; owner.Focus(); owner.DroppedDown = !owner.DroppedDown; } } private sealed class SelectorColors : ProfessionalColorTable { public override Color MenuItemSelected => Color.FromArgb(222, 237, 251); public override Color MenuItemBorder => Color.FromArgb(169, 201, 229); public override Color MenuBorder => Color.FromArgb(177, 203, 224); public override Color ToolStripDropDownBackground => Color.FromArgb(247, 251, 255); } }