Publish Zonghengjia Windows 0.3.13 client source
This commit is contained in:
+199
-12
@@ -1,12 +1,32 @@
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Drawing2D;
|
||||
|
||||
namespace UCVL.VPNPlus;
|
||||
|
||||
internal enum TrafficChartSeries { Both, Upload, Download }
|
||||
|
||||
// Reads only existing proxy-outbound counters; never the physical adapter.
|
||||
internal sealed class TrafficChart : Control
|
||||
{
|
||||
private const TextFormatFlags LabelFlags = ChartAxis.PaintFlags | TextFormatFlags.NoPadding |
|
||||
TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter;
|
||||
internal static readonly Color DownloadColor = Color.FromArgb(0, 106, 129);
|
||||
private static readonly Color AxisText = Color.FromArgb(36, 65, 107);
|
||||
private static readonly Color GridColor = Color.FromArgb(108, 130, 171, 209);
|
||||
private static readonly Color AxisStroke = Color.FromArgb(136, 111, 159, 205);
|
||||
|
||||
private readonly TrafficChartTimeline _timeline;
|
||||
private TimeSpan _period = TimeSpan.FromMinutes(5);
|
||||
private TrafficChartSeries _displayedSeries;
|
||||
internal TrafficChartSeries DisplayedSeries
|
||||
{
|
||||
get => _displayedSeries;
|
||||
set
|
||||
{
|
||||
if (_displayedSeries == value) return;
|
||||
_displayedSeries = value;
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
internal int PeriodMinutes => (int)_period.TotalMinutes;
|
||||
internal int PointCount => _timeline.PointCount;
|
||||
internal int VisiblePointCount => VisiblePoints().Length;
|
||||
@@ -24,10 +44,12 @@ internal sealed class TrafficChart : Control
|
||||
{
|
||||
_timeline = new TrafficChartTimeline(clock);
|
||||
DoubleBuffered = true;
|
||||
ResizeRedraw = true;
|
||||
SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.AllPaintingInWmPaint, true);
|
||||
SetStyle(ControlStyles.Opaque, false);
|
||||
BackColor = Color.Transparent;
|
||||
Font = Brand.Font(7.3f);
|
||||
AccessibleName = "本机代理流量趋势";
|
||||
}
|
||||
internal void Add(double up, double down, DateTimeOffset? time = null)
|
||||
{
|
||||
@@ -45,28 +67,193 @@ internal sealed class TrafficChart : Control
|
||||
_timeline.AdvanceClock();
|
||||
var points = VisiblePoints();
|
||||
var peak = points.Length == 0 ? 1024 : Math.Max(1024, points.Max(p => Math.Max(p.Up, p.Down)));
|
||||
var chart = ChartAxis.Draw(e.Graphics, ClientRectangle, Font, DeviceDpi, peak, speed: true);
|
||||
if (chart.Plot.Width < 1 || chart.Plot.Height < 1) return;
|
||||
var axis = ChartAxisY.Create(peak, speed: true);
|
||||
var g = e.Graphics;
|
||||
var scale = DeviceDpi / 96f;
|
||||
g.SmoothingMode = SmoothingMode.AntiAlias;
|
||||
var end = _timeline.End;
|
||||
ChartAxis.TimeLabels(e.Graphics, chart.Plot, Font, [end.Subtract(_period).ToString("HH:mm"), end.Subtract(_period / 2).ToString("HH:mm"), end.ToString("HH:mm")]);
|
||||
var plot = DrawAxes(g, ClientRectangle, axis, end, scale);
|
||||
if (plot.IsEmpty) return;
|
||||
|
||||
if (points.Length == 0)
|
||||
{
|
||||
TextRenderer.DrawText(e.Graphics, LastSampleAt.HasValue ? "采样暂不可用" : "等待代理流量采样", Font, Rectangle.Round(chart.Plot), Brand.Muted,
|
||||
ChartAxis.PaintFlags | TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
|
||||
DrawEmptyState(g, plot, scale);
|
||||
return;
|
||||
}
|
||||
|
||||
var clip = g.Save();
|
||||
g.SetClip(RectangleF.Inflate(plot, scale, scale), CombineMode.Intersect);
|
||||
foreach (var segment in points.GroupBy(point => point.Segment))
|
||||
{
|
||||
var samples = segment.ToArray();
|
||||
PointF[] Line(bool up) => samples.Select(p => new PointF(
|
||||
chart.Plot.Left + (float)Math.Clamp(1 - (end - p.Time).TotalSeconds / _period.TotalSeconds, 0, 1) * chart.Plot.Width,
|
||||
chart.Plot.Bottom - (float)((up ? p.Up : p.Down) / chart.Maximum) * chart.Plot.Height)).ToArray();
|
||||
ChartAxis.Curve(e.Graphics, Line(true), Brand.RoyalBlue, DeviceDpi, dots: samples.Length == 1);
|
||||
ChartAxis.Curve(e.Graphics, Line(false), Brand.Cyan, DeviceDpi, dots: samples.Length == 1);
|
||||
plot.Left + (float)Math.Clamp(1 - (end - p.Time).TotalSeconds / _period.TotalSeconds, 0, 1) * plot.Width,
|
||||
plot.Bottom - (float)((up ? p.Up : p.Down) / axis.MaximumBytes) * plot.Height)).ToArray();
|
||||
if (DisplayedSeries != TrafficChartSeries.Download)
|
||||
ChartAxis.Curve(g, Line(true), Brand.RoyalBlue, DeviceDpi, dots: samples.Length == 1);
|
||||
if (DisplayedSeries != TrafficChartSeries.Upload)
|
||||
ChartAxis.Curve(g, Line(false), DownloadColor, DeviceDpi, dots: samples.Length == 1);
|
||||
}
|
||||
g.Restore(clip);
|
||||
|
||||
if (!HasFreshSample)
|
||||
TextRenderer.DrawText(e.Graphics, "采样暂不可用", Font, Rectangle.Round(chart.Plot), Brand.Muted,
|
||||
ChartAxis.PaintFlags | TextFormatFlags.Right | TextFormatFlags.Top | TextFormatFlags.SingleLine);
|
||||
{
|
||||
var inset = Math.Max(2, (int)Math.Round(5 * scale));
|
||||
var height = Measure(g, "采样暂不可用").Height;
|
||||
var badge = Rectangle.FromLTRB((int)plot.Left + inset, (int)plot.Top + inset, (int)plot.Right - inset,
|
||||
(int)plot.Top + inset + height);
|
||||
if (badge.Width > 0 && badge.Bottom <= plot.Bottom)
|
||||
TextRenderer.DrawText(g, "采样暂不可用", Font, badge, AxisText, LabelFlags | TextFormatFlags.Right);
|
||||
}
|
||||
}
|
||||
|
||||
private Size Measure(Graphics g, string text)
|
||||
{
|
||||
var size = TextRenderer.MeasureText(g, text, Font, Size.Empty, LabelFlags);
|
||||
return new Size(size.Width, Math.Max(size.Height, (int)Math.Ceiling(Font.GetHeight(DeviceDpi))));
|
||||
}
|
||||
|
||||
private RectangleF DrawAxes(Graphics g, Rectangle bounds, ChartAxisY axis, DateTimeOffset end, float scale)
|
||||
{
|
||||
if (bounds.Width < 12 || bounds.Height < 10) return RectangleF.Empty;
|
||||
|
||||
var padding = Math.Max(2, (int)Math.Round(7 * scale));
|
||||
var labelHeight = Measure(g, axis.UnitText).Height;
|
||||
var timeGap = labelHeight / 2 + Math.Max(2, padding / 3);
|
||||
var fullYAxis = bounds.Height >= labelHeight * 4 + padding * 3;
|
||||
var showTime = bounds.Height >= labelHeight * 3 + padding;
|
||||
var showCompactLimit = !fullYAxis && bounds.Height >= labelHeight * 2 + padding;
|
||||
var firstY = bounds.Top + (fullYAxis ? labelHeight + labelHeight / 2 + padding : padding / 2);
|
||||
var lastY = bounds.Bottom - (showTime ? labelHeight + timeGap + padding / 2 : padding / 2);
|
||||
var axisWidth = Math.Max(Measure(g, axis.UnitText).Width, Measure(g, axis.Tick(axis.MaximumUnits)).Width);
|
||||
if (showCompactLimit) axisWidth = Math.Max(axisWidth, Measure(g, axis.UpperLimitLabel).Width);
|
||||
var left = bounds.Left + axisWidth + 2 * padding;
|
||||
var plot = RectangleF.FromLTRB(left, firstY, bounds.Right - padding, lastY);
|
||||
if (plot.Width <= 3 || plot.Height <= 3) return RectangleF.Empty;
|
||||
|
||||
using var plotFill = new LinearGradientBrush(plot, Color.FromArgb(172, 248, 253, 255),
|
||||
Color.FromArgb(143, 233, 246, 255), 90f);
|
||||
using var grid = new Pen(GridColor, Math.Max(.75f, .65f * scale)) { DashStyle = DashStyle.Dot };
|
||||
using var outline = new Pen(AxisStroke, Math.Max(1f, .65f * scale));
|
||||
g.FillRectangle(plotFill, plot);
|
||||
g.DrawRectangle(outline, plot.X, plot.Y, plot.Width, plot.Height);
|
||||
|
||||
var unitTop = bounds.Top + Math.Max(1, padding / 3);
|
||||
var labelLeft = bounds.Left + padding;
|
||||
var labelWidth = left - labelLeft - Math.Max(2, padding / 2);
|
||||
TextRenderer.DrawText(g, axis.UnitText, Font, new Rectangle(labelLeft, unitTop, labelWidth, labelHeight),
|
||||
AxisText, LabelFlags);
|
||||
if (fullYAxis)
|
||||
{
|
||||
var quarterTenths = axis.MaximumUnits * 2.5;
|
||||
var regularDivisions = Math.Abs(quarterTenths - Math.Round(quarterTenths)) < .0001 ? 4 : 5;
|
||||
var divisions = plot.Height >= labelHeight * (regularDivisions + 1) ? regularDivisions
|
||||
: plot.Height >= labelHeight * 3 ? 2 : 1;
|
||||
for (var i = 0; i <= divisions; i++)
|
||||
{
|
||||
var y = plot.Bottom - plot.Height * i / divisions;
|
||||
if (i > 0 && i < divisions) g.DrawLine(grid, plot.Left, y, plot.Right, y);
|
||||
var yTop = (int)Math.Round(y) - labelHeight / 2;
|
||||
TextRenderer.DrawText(g, axis.Tick(axis.MaximumUnits * i / divisions), Font,
|
||||
new Rectangle(labelLeft, yTop, labelWidth, labelHeight), AxisText, LabelFlags | TextFormatFlags.Right);
|
||||
}
|
||||
}
|
||||
else if (showCompactLimit && unitTop + 2 * labelHeight <= (showTime ? plot.Bottom + timeGap : bounds.Bottom))
|
||||
{
|
||||
TextRenderer.DrawText(g, axis.UpperLimitLabel, Font,
|
||||
new Rectangle(labelLeft, unitTop + labelHeight, labelWidth, labelHeight), AxisText, LabelFlags);
|
||||
}
|
||||
if (showTime) DrawTimes(g, bounds, plot, end, labelHeight, padding, timeGap, grid);
|
||||
return plot;
|
||||
}
|
||||
|
||||
private void DrawTimes(Graphics g, Rectangle bounds, RectangleF plot, DateTimeOffset end,
|
||||
int labelHeight, int padding, int timeGap, Pen grid)
|
||||
{
|
||||
var format = PeriodMinutes == 1 ? "HH:mm:ss" : "HH:mm";
|
||||
var labelWidth = Measure(g, end.ToString(format)).Width;
|
||||
var capacity = Math.Max(1, (int)(plot.Width / (labelWidth + padding)) + 1);
|
||||
var nominalStep = PeriodMinutes switch { 1 => 10, 5 => 60, 15 => 180, _ => 600 };
|
||||
var nominalSlots = _period.TotalSeconds / nominalStep;
|
||||
var labelTop = (int)Math.Round(plot.Bottom) + timeGap;
|
||||
|
||||
void DrawLabel(DateTimeOffset time, float x)
|
||||
{
|
||||
var text = time.ToString(format);
|
||||
var width = Measure(g, text).Width;
|
||||
var right = Math.Max(bounds.Left + 2, bounds.Right - width - 2);
|
||||
var left = Math.Clamp((int)Math.Round(x - width / 2f), bounds.Left + 2, right);
|
||||
TextRenderer.DrawText(g, text, Font, new Rectangle(left, labelTop, width, labelHeight),
|
||||
AxisText, LabelFlags);
|
||||
}
|
||||
|
||||
if (capacity == 1)
|
||||
{
|
||||
DrawLabel(end, plot.Right - labelWidth / 2f);
|
||||
return;
|
||||
}
|
||||
|
||||
var step = TimeSpan.FromSeconds(nominalStep * Math.Ceiling(nominalSlots / (capacity - 1)));
|
||||
var start = end.Subtract(_period);
|
||||
var initialTick = (step.Ticks - start.Ticks % step.Ticks) % step.Ticks;
|
||||
for (var tick = initialTick; tick <= _period.Ticks; tick += step.Ticks)
|
||||
{
|
||||
var x = plot.Left + (float)(tick / (double)_period.Ticks) * plot.Width;
|
||||
if (tick > 0 && tick < _period.Ticks) g.DrawLine(grid, x, plot.Top, x, plot.Bottom);
|
||||
DrawLabel(start.AddTicks(tick), x);
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawEmptyState(Graphics g, RectangleF plot, float scale)
|
||||
{
|
||||
var message = LastSampleAt.HasValue ? "采样暂不可用" : "等待代理流量采样 …";
|
||||
var text = Measure(g, message);
|
||||
var centerX = plot.Left + plot.Width / 2;
|
||||
var centerY = plot.Top + plot.Height / 2;
|
||||
var iconSize = 35 * scale;
|
||||
var gap = 6 * scale;
|
||||
if (plot.Height >= iconSize + gap + text.Height + 8 * scale && plot.Width >= iconSize)
|
||||
{
|
||||
var top = centerY - (iconSize + gap + text.Height) / 2;
|
||||
DrawSignal(g, new RectangleF(centerX - iconSize / 2, top, iconSize, iconSize));
|
||||
centerY = top + iconSize + gap + text.Height / 2f;
|
||||
}
|
||||
|
||||
var label = Rectangle.FromLTRB((int)plot.Left + 2, (int)(centerY - text.Height / 2f),
|
||||
(int)plot.Right - 2, (int)Math.Ceiling(centerY + text.Height / 2f));
|
||||
if (label.Width > 0)
|
||||
{
|
||||
var maskWidth = Math.Min(label.Width, text.Width + (int)Math.Ceiling(10 * scale));
|
||||
var maskBounds = new Rectangle(label.Left + (label.Width - maskWidth) / 2, label.Top, maskWidth, label.Height);
|
||||
if (maskBounds.Width > 2 && maskBounds.Height > 2)
|
||||
{
|
||||
using var mask = UiGeometry.RoundedRectangle(maskBounds, Math.Min(maskBounds.Width, maskBounds.Height) / 2);
|
||||
using var tint = new SolidBrush(Color.FromArgb(224, 240, 249, 255));
|
||||
g.FillPath(tint, mask);
|
||||
}
|
||||
TextRenderer.DrawText(g, message, Font, label, AxisText,
|
||||
LabelFlags | TextFormatFlags.HorizontalCenter | TextFormatFlags.EndEllipsis);
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawSignal(Graphics g, RectangleF bounds)
|
||||
{
|
||||
var state = g.Save();
|
||||
g.TranslateTransform(bounds.X, bounds.Y);
|
||||
g.ScaleTransform(bounds.Width / 40, bounds.Height / 40);
|
||||
using var outer = new Pen(Color.FromArgb(180, 97, 162, 224), 1.7f)
|
||||
{ StartCap = LineCap.Round, EndCap = LineCap.Round };
|
||||
using var inner = new Pen(Color.FromArgb(202, 54, 114, 182), 1.85f)
|
||||
{ StartCap = LineCap.Round, EndCap = LineCap.Round, LineJoin = LineJoin.Round };
|
||||
using var center = new SolidBrush(Color.FromArgb(60, 126, 194));
|
||||
g.DrawArc(outer, 2, 3, 36, 27, 135, 90);
|
||||
g.DrawArc(outer, 2, 3, 36, 27, 315, 90);
|
||||
g.DrawArc(inner, 8, 8, 24, 17, 135, 90);
|
||||
g.DrawArc(inner, 8, 8, 24, 17, 315, 90);
|
||||
g.FillEllipse(center, 17, 13.5f, 6, 6);
|
||||
g.DrawLines(inner, [new PointF(11, 35), new(20, 22), new(29, 35)]);
|
||||
g.DrawLine(inner, 14, 31, 26, 31);
|
||||
g.Restore(state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user