Publish Zonghengjia Windows 0.3.13 client source

This commit is contained in:
2026-11-13 10:35:04 +08:00
parent 1590443809
commit 839434a313
55 changed files with 2324 additions and 963 deletions
+363
View File
@@ -0,0 +1,363 @@
using System.Drawing.Drawing2D;
namespace UCVL.VPNPlus;
internal sealed partial class MainForm
{
private readonly CardPanel _wideMonitor = new() { Visible = false };
private readonly TrendSummaryPanel _wideSummary = new() { Visible = false };
private readonly Label _wideSummaryState = new(), _wideSidebarPlan = new(), _wideFooterState = new();
private readonly DashboardGlyph _wideFooterClock = new(DashboardIcon.History);
private readonly DashboardGlyph _wideFooterShield = new(DashboardIcon.Shield);
private readonly RoundedButton _wideUpload = new(), _wideDownload = new();
private readonly DashboardFeature[] _wideFeatures =
[
new(DashboardIcon.Server, "线路调度", "自动分配或手动选择"),
new(DashboardIcon.Shield, "入口验证", "Tailscale 与青岛公网", true),
new(DashboardIcon.Layers, "灵活代理", "以已生效的模式为准", true),
];
private bool _wideDashboardBuilt;
private TrafficChartSeries _wideSeries = TrafficChartSeries.Upload;
private void BuildWideDashboard()
{
_wideMonitor.AccessibleName = "连接状态和流量监控";
_dashboardBody.Controls.Add(_wideMonitor);
ConfigureLabel(_wideSummaryState, "", Brand.Font(10, FontStyle.Bold), Brand.Text);
ConfigureLabel(_wideSidebarPlan, "", Brand.Font(8), Brand.Muted);
ConfigureLabel(_wideFooterState, "", Brand.Font(9.5f, FontStyle.Bold), Brand.Text);
foreach (var label in new[] { _wideSummaryState, _wideSidebarPlan, _wideFooterState })
{
label.AutoEllipsis = true;
label.TextAlign = ContentAlignment.MiddleLeft;
}
_wideSummary.Controls.Add(_wideSummaryState);
_nodeCard.Controls.Add(_wideSummary);
_nodeCard.Controls.Add(_wideSidebarPlan);
_nodeCard.Controls.AddRange(_wideFeatures);
_connectionModeCombo.LeadingIcon = DashboardIcon.Settings;
_routeCombo.LeadingIcon = DashboardIcon.Layers;
_manualNodeCombo.LeadingIcon = DashboardIcon.Server;
_footer.Controls.AddRange([_wideFooterClock, _wideFooterShield, _wideFooterState]);
_modeCard.Controls.AddRange([_nodeIcon, _modeIcon]);
ConfigureSecondaryButton(_wideUpload, "↑ 上传 —", Point.Empty, new(144, 29));
ConfigureSecondaryButton(_wideDownload, "↓ 下载 —", Point.Empty, new(144, 29));
_wideUpload.Capsule = _wideDownload.Capsule = true;
_wideUpload.AccessibleName = "显示上传速度曲线";
_wideDownload.AccessibleName = "显示下载速度曲线";
_wideUpload.Click += (_, _) => SelectWideTrafficSeries(TrafficChartSeries.Upload);
_wideDownload.Click += (_, _) => SelectWideTrafficSeries(TrafficChartSeries.Download);
_speedTrendCard.Controls.AddRange([_wideUpload, _wideDownload]);
_wideDashboardBuilt = true;
}
private void SelectWideTrafficSeries(TrafficChartSeries series)
{
_wideSeries = series;
ApplyWideTrafficSeries(WideDashboardPresentation);
}
private void ApplyWideTrafficSeries(bool wide)
{
_trafficChart.DisplayedSeries = wide ? _wideSeries : TrafficChartSeries.Both;
UpdateTab(_wideUpload, _wideSeries == TrafficChartSeries.Upload);
UpdateTab(_wideDownload, _wideSeries == TrafficChartSeries.Download);
static void UpdateTab(RoundedButton button, bool selected)
{
button.BackColor = selected ? Brand.RoyalBlue : Color.White;
button.ForeColor = selected ? Color.White : Brand.DeepBlue;
button.AccessibleDescription = selected ? "当前显示" : "点击切换";
}
}
private void SetDashboardPresentation(bool wide)
{
if (!_wideDashboardBuilt) return;
static void Move(Control control, Control parent)
{
if (!ReferenceEquals(control.Parent, parent)) control.Parent = parent;
}
Move(_modeCard, wide ? _wideMonitor : _nodeCard);
Move(_speedTrendCard, wide ? _wideMonitor : _dashboardBody);
Move(_dailyCard, wide ? _wideMonitor : _dashboardBody);
Move(_nodeTrafficPanel, wide ? _wideMonitor : _dashboardBody);
Move(_trendHeading, wide ? _wideSummary : _speedTrendCard);
var rangeParent = wide ? _modeCard : _speedTrendCard;
if (_historyRangeCombo.Parent != rangeParent)
{
_historyRangeCombo.DroppedDown = false;
Move(_historyRangeCombo, rangeParent);
}
_wideMonitor.Visible = wide;
_wideSummary.Visible = _wideSidebarPlan.Visible = wide;
_wideUpload.Visible = _wideDownload.Visible = wide;
_wideFooterClock.Visible = _wideFooterShield.Visible = _wideFooterState.Visible = wide;
_footerCompanySite.Visible = !wide;
_dailyIcon.Visible = wide;
foreach (var feature in _wideFeatures) feature.Visible = wide;
_usageMetric.InlineTotal = _sessionMetric.InlineTotal = wide;
_statusCard.WideBanner = wide;
_connectButton.Capsule = wide;
_nodeCard.GlassRim = _wideMonitor.GlassRim = _wideSummary.GlassRim
= _speedTrendCard.GlassRim = _dailyCard.GlassRim = _footer.GlassRim = wide;
_uploadLegend.Visible = _downloadLegend.Visible = !wide;
_historyStatus.Visible = wide;
ApplyWideTrafficSeries(wide);
RefreshConnectionAppearance();
}
private void LayoutWideDashboard(Size viewport)
{
int S(int value) => ScaleLogical(value);
var dense = viewport.Height < S(465);
var layout = DashboardWideLayout.Calculate(viewport, DeviceDpi, _canViewNodeTraffic);
_dashboardBody.Bounds = new Rectangle(Point.Empty, viewport);
if (_welcomeLabel.Parent != _dashboardBody) _welcomeLabel.Parent = _dashboardBody;
if (_logoutButton.Parent != _dashboardBody) _logoutButton.Parent = _dashboardBody;
_welcomeLabel.Visible = _logoutButton.Visible = _accountLabel.Visible = _identityBadge.Visible = false;
_statusCard.Bounds = layout.Banner;
_nodeCard.Bounds = layout.Sidebar;
_wideMonitor.Bounds = layout.Monitor;
_modeCard.Bounds = layout.State;
_speedTrendCard.Bounds = layout.Trend;
_nodeTrafficPanel.Bounds = layout.Servers;
_dailyCard.Bounds = layout.Totals;
_footer.Bounds = layout.Footer;
_trafficCard.Visible = _tailnetTrafficCard.Visible = false;
_tailnetSampleLabel.Visible = false;
var bannerHeight = _statusCard.Height;
var actionWidth = S(dense ? 132 : 158);
var actionHeight = S(dense ? 36 : 42);
var shieldSize = S(dense ? 43 : 52);
_statusDot.Bounds = new Rectangle(S(22), (bannerHeight - shieldSize) / 2, shieldSize, shieldSize);
_connectButton.Bounds = new Rectangle(_statusCard.Width - actionWidth - S(22), (bannerHeight - actionHeight) / 2, actionWidth, actionHeight);
var statusLeft = S(dense ? 77 : 90);
var statusWidth = _connectButton.Left - statusLeft - S(13);
_statusTitle.Bounds = new Rectangle(statusLeft, bannerHeight / 2 - S(dense ? 24 : 30), statusWidth, S(dense ? 26 : 33));
_statusDetail.Bounds = new Rectangle(statusLeft + S(2), bannerHeight / 2 + S(dense ? 1 : 4), statusWidth, S(dense ? 20 : 23));
var sidebarWidth = _nodeCard.Width;
var selectorWidth = sidebarWidth - S(75);
var firstRow = S(dense ? 9 : 15);
var rowStep = S(dense ? 33 : 40);
var rowHeight = S(dense ? 28 : 32);
_selectionHeading.Bounds = new Rectangle(S(15), firstRow, S(47), rowHeight);
_routeHeading.Bounds = new Rectangle(S(15), firstRow + rowStep, S(47), rowHeight);
_routeHeading.Text = "模式";
_selectionHeading.TextAlign = _routeHeading.TextAlign = ContentAlignment.MiddleLeft;
_connectionModeCombo.Bounds = new Rectangle(S(60), firstRow, selectorWidth, rowHeight);
_routeCombo.Bounds = new Rectangle(S(60), firstRow + rowStep, selectorWidth, rowHeight);
_manualNodeCombo.Bounds = new Rectangle(S(60), firstRow + 2 * rowStep, selectorWidth, S(29));
var summaryTop = firstRow + (_manualNodeCombo.Visible ? 3 : 2) * rowStep + S(3);
var remaining = _nodeCard.Height - summaryTop - S(24);
var summaryHeight = Math.Clamp(remaining / 3, S(dense ? 55 : 65), S(115));
_wideSummary.Bounds = new Rectangle(S(10), summaryTop, sidebarWidth - S(20), summaryHeight);
_trendHeading.Text = "本机代理趋势";
_trendHeading.Bounds = new Rectangle(S(13), S(8), _wideSummary.Width - S(26), S(22));
_trendHeading.TextAlign = ContentAlignment.MiddleLeft;
_wideSummaryState.Bounds = new Rectangle(S(13), S(31), _wideSummary.Width - S(26), S(22));
_wideSidebarPlan.Bounds = new Rectangle(S(14), _nodeCard.Height - S(25), sidebarWidth - S(28), S(16));
var featureTop = _wideSummary.Bottom + S(5);
var featureHeight = Math.Min(S(66), (_wideSidebarPlan.Top - featureTop - S(4)) / _wideFeatures.Length);
for (var index = 0; index < _wideFeatures.Length; index++)
{
var feature = _wideFeatures[index];
feature.Visible = featureHeight >= S(30);
if (feature.Visible) feature.Bounds = new Rectangle(S(12), featureTop + index * featureHeight, sidebarWidth - S(24), featureHeight);
}
var stateHeight = _modeCard.Height;
var rangeWidth = S(129);
var statesWidth = _modeCard.Width - rangeWidth - S(23);
using var metrics = _modeCard.CreateGraphics();
int TextWidth(string text, Font font) => TextRenderer.MeasureText(metrics, text, font,
Size.Empty, TextFormatFlags.SingleLine).Width;
var headingLeft = S(34);
var headingWidth = Math.Max(S(40), Math.Max(TextWidth("节点", _nodeHeading.Font),
TextWidth("模式", _actualModeHeading.Font)) + S(2));
var valueInset = headingLeft + headingWidth;
var modeWidth = Math.Clamp(TextWidth(_routeReadOnly.Text, _routeReadOnly.Font) + S(6), S(79), S(146));
var splitLimit = Math.Max(valueInset + S(35), statesWidth - valueInset - modeWidth);
var nodeRoom = splitLimit - valueInset - S(4);
var nodeText = _selectionUiConnected && !string.IsNullOrWhiteSpace(_currentNodeName) ? _currentNodeName : _currentNodeLabel.Text;
var nodeFont = _currentNodeLabel.Font;
if (TextWidth(nodeText, nodeFont) > nodeRoom)
{
// The sidebar already identifies the active transport. Omit its
// duplicate suffix here if it would crowd out the actual name.
if (_selectionUiConnected && _currentNodeUsesTailnet && !string.IsNullOrWhiteSpace(_currentNodeBaseName))
nodeText = _currentNodeBaseName;
if (TextWidth(nodeText, nodeFont) > nodeRoom)
nodeFont = TextWidth(nodeText, _narrowNodeFont) <= nodeRoom ? _narrowNodeFont : _minimumNodeFont;
}
_currentNodeLabel.Text = nodeText;
_currentNodeLabel.Font = nodeFont;
var split = Math.Min(splitLimit, Math.Max(statesWidth / 2, valueInset + TextWidth(nodeText, nodeFont) + S(4)));
_historyRangeCombo.Bounds = new Rectangle(_modeCard.Width - rangeWidth - S(6), (stateHeight - S(31)) / 2, rangeWidth, S(31));
_nodeIcon.Bounds = new Rectangle(S(7), (stateHeight - S(26)) / 2, S(26), S(26));
_nodeHeading.Text = "节点";
_nodeHeading.Bounds = new Rectangle(headingLeft, 0, headingWidth, stateHeight);
_currentNodeLabel.Bounds = new Rectangle(valueInset, 0, Math.Max(S(31), split - valueInset - S(4)), stateHeight);
_modeIcon.Bounds = new Rectangle(split + S(7), (stateHeight - S(25)) / 2, S(25), S(25));
_actualModeHeading.Bounds = new Rectangle(split + headingLeft, 0, headingWidth, stateHeight);
_routeReadOnly.Bounds = new Rectangle(split + valueInset, 0, Math.Max(S(35), statesWidth - split - valueInset), stateHeight);
_nodeIcon.Visible = _modeIcon.Visible = true;
_nodeHeading.TextAlign = _actualModeHeading.TextAlign = ContentAlignment.MiddleLeft;
var trendWidth = _speedTrendCard.Width;
var tabWidth = Math.Min(S(164), (trendWidth - S(30)) / 2);
var tabTop = S(dense ? 6 : 9);
var tabHeight = S(dense ? 26 : 31);
_wideUpload.Bounds = new Rectangle(S(12), tabTop, tabWidth, tabHeight);
_wideDownload.Bounds = new Rectangle(_wideUpload.Right + S(4), tabTop, tabWidth, tabHeight);
_historyStatus.Bounds = new Rectangle(_wideDownload.Right + S(6), tabTop, Math.Max(1, trendWidth - _wideDownload.Right - S(18)), tabHeight);
_historyStatus.Text = "实时代理采样";
_trafficChart.Font = _wideChartFont;
var chartTop = _wideUpload.Bottom + S(3);
_trafficChart.Bounds = new Rectangle(S(10), chartTop, trendWidth - S(20), _speedTrendCard.Height - chartTop - S(7));
var totalsHeight = _dailyCard.Height;
var titleWidth = Math.Min(S(155), _dailyCard.Width / 4);
_dailyIcon.Bounds = new Rectangle(S(12), (totalsHeight - S(28)) / 2, S(28), S(28));
_dailyHeading.Bounds = new Rectangle(S(47), 0, Math.Max(1, titleWidth - S(48)), totalsHeight);
_dailyHeading.TextAlign = ContentAlignment.MiddleLeft;
var metricWidth = (_dailyCard.Width - titleWidth - S(9)) / 2;
_usageMetric.Bounds = new Rectangle(titleWidth, S(3), metricWidth, totalsHeight - S(9));
_sessionMetric.Bounds = new Rectangle(_usageMetric.Right, S(3), metricWidth, totalsHeight - S(9));
var footerHeight = _footer.Height;
_wideFooterClock.Bounds = new Rectangle(S(11), (footerHeight - S(22)) / 2, S(22), S(22));
_expiryLabel.Bounds = new Rectangle(S(40), 0, S(126), footerHeight);
_wideFooterShield.Bounds = new Rectangle(S(171), (footerHeight - S(22)) / 2, S(22), S(22));
_wideFooterState.Bounds = new Rectangle(S(200), 0, Math.Max(1, _footer.Width - S(452)), footerHeight);
_footerHint.Bounds = new Rectangle(_footer.Width - S(243), 0, S(229), footerHeight);
_footerHint.Visible = true;
_planLabel.Visible = _footerPoem.Visible = false;
_wideSidebarPlan.Text = _planLabel.Text;
}
private void RefreshConnectionAppearance()
{
var title = _statusTitle.Text;
var recovering = !title.StartsWith("已恢复", StringComparison.Ordinal)
&& (title.Contains("恢复") || title.Contains("异常") || title.Contains("修复"));
var tone = _selectionUiConnected ? recovering ? BannerTone.Attention : BannerTone.Connected
: title.Contains("失败") || title.Contains("需要修复") || title.Contains("暂无线") ? BannerTone.Error
: recovering ? BannerTone.Attention
: title.StartsWith("正在连接", StringComparison.Ordinal) ? BannerTone.Connecting : BannerTone.Idle;
_statusCard.Tone = tone;
var accent = tone switch
{
BannerTone.Connected => Brand.Success,
BannerTone.Attention => Color.FromArgb(151, 82, 10),
BannerTone.Error => Color.FromArgb(172, 49, 55),
BannerTone.Connecting => Brand.RoyalBlue,
_ => Brand.Muted,
};
_statusTitle.ForeColor = _statusCard.WideBanner ? Color.White : tone == BannerTone.Idle ? Brand.Text : accent;
_statusDetail.ForeColor = _statusCard.WideBanner ? Color.FromArgb(236, 248, 255) : Brand.Muted;
_statusDot.ForeColor = _statusCard.WideBanner ? tone switch
{
BannerTone.Connected => Color.FromArgb(19, 127, 128),
BannerTone.Attention => Color.FromArgb(169, 93, 24),
BannerTone.Error => Color.FromArgb(178, 60, 81),
_ => Color.FromArgb(13, 99, 195),
} : accent;
_statusDot.Featured = _statusCard.WideBanner;
_wideFooterShield.ForeColor = accent;
_statusDot.Glyph = _wideFooterShield.Glyph = tone switch
{
BannerTone.Connected => DashboardIcon.Shield,
BannerTone.Error or BannerTone.Attention => DashboardIcon.ShieldAlert,
_ => DashboardIcon.ShieldPending,
};
_wideSummaryState.ForeColor = accent;
_wideSummaryState.Text = _selectionUiConnected
? (_currentNodeUsesTailnet ? "● Tailscale 入口" : "● 公网入口")
: "● " + title;
_wideFooterState.Text = tone == BannerTone.Connected ? "网络连接 · 已连接" : "网络连接 · " + title;
_footerCompanySite.Text = tone switch
{
BannerTone.Connected => "网络 · 已连接",
BannerTone.Connecting => "网络 · 正在连接",
BannerTone.Attention => "网络 · 连接异常",
BannerTone.Error => "网络 · 连接失败",
_ => "网络 · 未连接",
};
_footerCompanySite.ForeColor = accent;
}
}
internal sealed class TrendSummaryPanel : CardPanel
{
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (Width < 20 || Height < 48) return;
var g = e.Graphics;
var state = g.Save();
var scale = DeviceDpi / 96f;
var inset = Math.Max(1, (int)Math.Round(scale * 2));
using var clip = UiGeometry.RoundedRectangle(new Rectangle(inset, inset, Width - 2 * inset - 1, Height - 2 * inset - 2),
Math.Min((int)Math.Round(12 * scale), (Height - 2 * inset - 2) / 2));
g.SetClip(clip, CombineMode.Intersect);
var y = Height * .84f;
using var wave = new GraphicsPath();
wave.AddBezier(0, y, Width * .35f, Height * .43f, Width * .52f, Height * 1.25f, Width, Height * .66f);
wave.AddLine(Width, Height, 0, Height);
wave.CloseFigure();
using var fill = new LinearGradientBrush(ClientRectangle, Color.FromArgb(36, 117, 194, 244), Color.FromArgb(80, 114, 192, 246), 90);
using var line = new Pen(Color.FromArgb(96, 66, 152, 217), Math.Max(1f, .7f * scale));
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillPath(fill, wave);
g.DrawBezier(line, 0, y, Width * .35f, Height * .43f, Width * .52f, Height * 1.25f, Width, Height * .66f);
g.Restore(state);
}
}
internal sealed class DashboardFeature : Control
{
private readonly DashboardIcon _icon;
private readonly string _title, _detail;
private readonly bool _divider;
internal DashboardFeature(DashboardIcon icon, string title, string detail, bool divider = false)
{
_icon = icon;
_title = title;
_detail = detail;
_divider = divider;
SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
BackColor = Color.Transparent;
AccessibleName = title;
AccessibleDescription = detail;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
var scale = DeviceDpi / 96f;
int S(float value) => (int)Math.Round(value * scale);
var side = Math.Min(S(36), Height - S(6));
var icon = new Rectangle(S(5), (Height - side) / 2, side, side);
g.SmoothingMode = SmoothingMode.AntiAlias;
using var bubble = new SolidBrush(Color.FromArgb(92, 174, 217, 251));
g.FillEllipse(bubble, icon);
DashboardDrawing.Icon(g, _icon, Rectangle.Inflate(icon, -S(7), -S(7)), Brand.DeepBlue);
using var title = Brand.Font(10.5f, FontStyle.Bold);
using var detail = Brand.Font(8.5f);
var x = icon.Right + S(10);
var width = Width - x - S(4);
var middle = Height / 2;
if (_divider && Height >= S(38))
{
using var divider = new Pen(Color.FromArgb(108, 167, 198, 223), Math.Max(1f, .65f * scale));
g.DrawLine(divider, S(6), 0, Width - S(7), 0);
}
var flags = TextFormatFlags.SingleLine | TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter;
TextRenderer.DrawText(g, _title, title, new Rectangle(x, middle - S(16), width, S(18)), Brand.Navy, flags);
TextRenderer.DrawText(g, _detail, detail, new Rectangle(x, middle + S(2), width, S(16)), Brand.Muted, flags);
}
}