Files
ucvl-zonghengjia-open-source/windows/MainForm.TailnetTraffic.cs

83 lines
4.8 KiB
C#

namespace UCVL.VPNPlus;
internal sealed partial class MainForm
{
// Presentation only. These controls never read adapter/counter state and
// never change proxy, account, session totals or route decisions.
private readonly CardPanel _tailnetTrafficCard = new() { MetricBand = true };
private readonly MetricCard _tailnetUploadMetric = new("上传速度", DashboardIcon.Upload) { Embedded = true };
private readonly MetricCard _tailnetDownloadMetric = new("下载速度", DashboardIcon.Download) { Embedded = true };
private readonly TrafficChart _tailnetTrafficChart = new();
private readonly DashboardGlyph _tailnetTrafficIcon = new(DashboardIcon.Chart);
private readonly Label _tailnetTrafficHeading = new(), _tailnetSampleLabel = new();
private readonly Label _tailnetUploadLegend = new(), _tailnetDownloadLegend = new();
private bool _tailnetTrafficVisible;
private void BuildTailnetTrafficCard()
{
ConfigureLabel(_tailnetTrafficHeading, "Tailscale 实时网速", Brand.Font(9.5f, FontStyle.Bold), Brand.Text);
ConfigureLabel(_tailnetSampleLabel, "等待流量采样", Brand.Font(7.5f), Brand.Muted);
_tailnetSampleLabel.TextAlign = ContentAlignment.MiddleRight;
_tailnetSampleLabel.AutoEllipsis = true;
ConfigureLabel(_tailnetUploadLegend, "● 上传 —", Brand.Font(8), Brand.RoyalBlue);
ConfigureLabel(_tailnetDownloadLegend, "● 下载 —", Brand.Font(8), Brand.Cyan);
_tailnetTrafficCard.AccessibleName = "Tailscale 实时网速";
_tailnetTrafficCard.AccessibleDescription = "仅纵横家当前 Tailscale 代理链路;已计入代理总流量,不代表所有 Tailscale 网卡或整机流量。";
_tailnetTrafficCard.Controls.AddRange([_tailnetTrafficIcon, _tailnetTrafficHeading, _tailnetSampleLabel,
_tailnetUploadMetric, _tailnetDownloadMetric, _tailnetTrafficChart, _tailnetUploadLegend, _tailnetDownloadLegend]);
_tailnetTrafficCard.Visible = false;
_dashboardBody.Controls.Add(_tailnetTrafficCard);
ResetTailnetTraffic();
}
internal void SetTailnetTrafficVisibility(bool show)
{
if (_tailnetTrafficVisible == show) return;
_tailnetTrafficVisible = show;
_tailnetTrafficCard.Visible = show;
if (!show) ResetTailnetTraffic();
LayoutDashboard();
}
internal void DisplayTailnetTraffic(double? up, double? down, string sampling)
{
// Root supplies the already-accounted proxy sample only for the active
// Tailnet transport. This presentation never reads an adapter or adds
// bytes to session/backend totals. Missing samples are not fake zeroes.
static double? Clean(double? value) => value.HasValue && double.IsFinite(value.Value)
? Math.Max(0, value.Value) : null;
up = Clean(up); down = Clean(down);
_tailnetUploadMetric.SetBytes(up, speed: true, hint: "已计入代理总流量");
_tailnetDownloadMetric.SetBytes(down, speed: true, hint: "已计入代理总流量");
_tailnetSampleLabel.Text = "仅纵横家链路 · " + sampling;
_tailnetUploadLegend.Text = up.HasValue ? $"● 上传 {FormatBytes((long)up.Value)}/s" : "● 上传 —";
_tailnetDownloadLegend.Text = down.HasValue ? $"● 下载 {FormatBytes((long)down.Value)}/s" : "● 下载 —";
if (up.HasValue && down.HasValue) _tailnetTrafficChart.Add(up.Value, down.Value);
else _tailnetTrafficChart.Reset();
}
internal void ResetTailnetTraffic()
{
_tailnetTrafficChart.Reset();
DisplayTailnetTraffic(null, null, "等待 Tailscale 流量采样");
}
private void LayoutTailnetTrafficCard(Rectangle bounds)
{
_tailnetTrafficCard.Visible = _tailnetTrafficVisible;
if (!_tailnetTrafficVisible) { _tailnetTrafficCard.Bounds = Rectangle.Empty; return; }
_tailnetTrafficCard.Bounds = bounds;
int S(int value) => ScaleLogical(value);
_tailnetTrafficIcon.Bounds = new Rectangle(S(15), S(10), S(20), S(20));
_tailnetTrafficHeading.Bounds = new Rectangle(S(42), S(8), S(132), S(26));
_tailnetSampleLabel.Bounds = new Rectangle(S(178), S(10), Math.Max(1, bounds.Width - S(192)), S(22));
var metricWidth = (bounds.Width - S(28)) / 2;
_tailnetUploadMetric.Bounds = new Rectangle(S(14), S(43), metricWidth - S(2), S(82));
_tailnetDownloadMetric.Bounds = new Rectangle(S(14) + metricWidth, S(43), metricWidth - S(2), S(82));
_tailnetTrafficChart.Bounds = new Rectangle(S(13), S(132), bounds.Width - S(26), S(106));
var legendWidth = (bounds.Width - S(40)) / 2;
_tailnetUploadLegend.Bounds = new Rectangle(S(20), S(248), legendWidth, S(19));
_tailnetDownloadLegend.Bounds = new Rectangle(S(20) + legendWidth, S(248), legendWidth, S(19));
}
}