Files

229 lines
12 KiB
C#
Raw Permalink Normal View History

namespace UCVL.VPNPlus;
internal sealed partial class MainForm
{
private readonly CardPanel _wideMonitor = new() { Visible = false, Embedded = true };
private readonly Label _wideSummaryState = new();
private readonly RoundedButton _wideUpload = new(), _wideDownload = new();
private bool _wideDashboardBuilt;
private TrafficChartSeries _wideSeries = TrafficChartSeries.Both;
private void BuildWideDashboard()
{
_wideMonitor.AccessibleName = "连接状态和流量监控";
_dashboardBody.Controls.Add(_wideMonitor);
ConfigureLabel(_wideSummaryState, "", Brand.Font(8, FontStyle.Bold), Brand.Muted);
_wideSummaryState.AutoEllipsis = true;
_wideSummaryState.TextAlign = ContentAlignment.MiddleRight;
_modeCard.Controls.Add(_wideSummaryState);
_connectionModeCombo.LeadingIcon = DashboardIcon.Settings;
_routeCombo.LeadingIcon = DashboardIcon.Layers;
_manualNodeCombo.LeadingIcon = DashboardIcon.Server;
_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 = _wideSeries == series ? TrafficChartSeries.Both : series;
ApplyWideTrafficSeries(WideDashboardPresentation);
}
private void ApplyWideTrafficSeries(bool wide)
{
_trafficChart.DisplayedSeries = wide ? _wideSeries : TrafficChartSeries.Both;
UpdateTab(_wideUpload, Brand.RoyalBlue, _wideSeries != TrafficChartSeries.Download);
UpdateTab(_wideDownload, TrafficChart.DownloadColor, _wideSeries != TrafficChartSeries.Upload);
static void UpdateTab(RoundedButton button, Color accent, bool visible)
{
button.BackColor = Color.White;
button.ForeColor = visible ? accent : Brand.Muted;
button.AccessibleDescription = visible ? "曲线可见,点击切换" : "曲线已隐藏,点击显示";
}
}
private void SetDashboardPresentation(bool wide)
{
if (!_wideDashboardBuilt) return;
static void Move(Control control, Control parent)
{
if (!ReferenceEquals(control.Parent, parent)) control.Parent = parent;
}
Move(_speedTrendCard, wide ? _wideMonitor : _dashboardBody);
Move(_dailyCard, wide ? _wideMonitor : _dashboardBody);
Move(_nodeTrafficPanel, wide ? _wideMonitor : _dashboardBody);
_wideMonitor.Visible = wide;
_wideSummaryState.Visible = wide;
_wideUpload.Visible = _wideDownload.Visible = wide;
_footerCompanySite.Visible = !wide;
_dailyIcon.Visible = wide;
_usageMetric.InlineTotal = _sessionMetric.InlineTotal = wide;
_statusCard.WideBanner = false;
_connectButton.Capsule = wide;
_nodeCard.GlassRim = _speedTrendCard.GlassRim = _dailyCard.GlassRim = _footer.GlassRim = wide;
_uploadLegend.Visible = _downloadLegend.Visible = !wide;
_historyStatus.Visible = false;
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, _manualNodeCombo.Visible);
_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.Configuration;
_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 ? 120 : 138);
var actionHeight = S(dense ? 34 : 38);
var shieldSize = S(dense ? 36 : 42);
var pad = S(dense ? 14 : 18);
_statusDot.Bounds = new Rectangle(pad, (bannerHeight - shieldSize) / 2, shieldSize, shieldSize);
_connectButton.Bounds = new Rectangle(_statusCard.Width - actionWidth - pad, (bannerHeight - actionHeight) / 2, actionWidth, actionHeight);
var statusLeft = _statusDot.Right + S(12);
var statusWidth = _connectButton.Left - statusLeft - S(12);
_statusTitle.Bounds = new Rectangle(statusLeft, bannerHeight / 2 - S(dense ? 22 : 26), statusWidth, S(dense ? 25 : 29));
_statusDetail.Bounds = new Rectangle(statusLeft, bannerHeight / 2 + S(2), statusWidth, S(20));
var rowHeight = S(dense ? 26 : 31);
var fieldHeight = rowHeight - S(3);
var firstRow = layout.State.Top;
var valueLeft = pad + S(43);
var fieldWidth = layout.State.Left - valueLeft - S(16);
_selectionHeading.Bounds = new Rectangle(pad, firstRow, S(39), fieldHeight);
_routeHeading.Bounds = new Rectangle(pad, firstRow + rowHeight, S(39), fieldHeight);
_routeHeading.Text = "模式";
_selectionHeading.TextAlign = _routeHeading.TextAlign = ContentAlignment.MiddleLeft;
_connectionModeCombo.Bounds = new Rectangle(valueLeft, firstRow, fieldWidth, fieldHeight);
_routeCombo.Bounds = new Rectangle(valueLeft, firstRow + rowHeight, fieldWidth, fieldHeight);
_manualNodeCombo.Bounds = new Rectangle(valueLeft, firstRow + 2 * rowHeight, fieldWidth, fieldHeight);
var headingLeft = S(12);
var headingWidth = S(45);
var stateValueLeft = headingLeft + headingWidth;
var entryWidth = _selectionUiConnected ? S(105) : 0;
var stateWidth = _modeCard.Width - stateValueLeft - S(8);
_nodeHeading.Text = "节点";
_nodeHeading.Bounds = new Rectangle(headingLeft, 0, headingWidth, fieldHeight);
_currentNodeLabel.Bounds = new Rectangle(stateValueLeft, 0, stateWidth - entryWidth, fieldHeight);
_actualModeHeading.Bounds = new Rectangle(headingLeft, rowHeight, headingWidth, fieldHeight);
_routeReadOnly.Bounds = new Rectangle(stateValueLeft, rowHeight, stateWidth, fieldHeight);
_wideSummaryState.Bounds = new Rectangle(_modeCard.Width - entryWidth - S(8), 0, entryWidth, fieldHeight);
_wideSummaryState.Visible = _selectionUiConnected;
if (_selectionUiConnected && !string.IsNullOrWhiteSpace(_currentNodeBaseName))
_currentNodeLabel.Text = _currentNodeBaseName;
using (var metrics = _modeCard.CreateGraphics())
{
int NodeWidth(Font font) => TextRenderer.MeasureText(metrics, _currentNodeLabel.Text, font,
Size.Empty, TextFormatFlags.SingleLine).Width;
if (NodeWidth(_currentNodeLabel.Font) > _currentNodeLabel.Width)
_currentNodeLabel.Font = NodeWidth(_narrowNodeFont) <= _currentNodeLabel.Width ? _narrowNodeFont : _minimumNodeFont;
}
_nodeIcon.Visible = _modeIcon.Visible = false;
_nodeHeading.TextAlign = _actualModeHeading.TextAlign = ContentAlignment.MiddleLeft;
var trendWidth = _speedTrendCard.Width;
var tabTop = S(7);
var tabHeight = S(dense ? 24 : 28);
var rangeWidth = S(128);
var tabWidth = S(135);
_historyRangeCombo.Bounds = new Rectangle(trendWidth - pad - rangeWidth, tabTop, rangeWidth, tabHeight);
_wideDownload.Bounds = new Rectangle(_historyRangeCombo.Left - S(14) - tabWidth, tabTop, tabWidth, tabHeight);
_wideUpload.Bounds = new Rectangle(_wideDownload.Left - S(5) - tabWidth, tabTop, tabWidth, tabHeight);
_trendHeading.Text = "本机代理趋势";
_trendHeading.Bounds = new Rectangle(pad, tabTop, _wideUpload.Left - pad - S(8), tabHeight);
_trendHeading.TextAlign = ContentAlignment.MiddleLeft;
_trafficChart.Font = _wideChartFont;
var chartTop = _wideUpload.Bottom + S(3);
_trafficChart.Bounds = new Rectangle(S(9), chartTop, trendWidth - S(18), _speedTrendCard.Height - chartTop - S(6));
var totalsHeight = _dailyCard.Height;
var titleWidth = S(140);
var iconSize = S(18);
_dailyIcon.Bounds = new Rectangle(pad, (totalsHeight - iconSize) / 2, iconSize, iconSize);
_dailyHeading.Bounds = new Rectangle(_dailyIcon.Right + S(8), 0, titleWidth - _dailyIcon.Right - S(8), totalsHeight);
_dailyHeading.TextAlign = ContentAlignment.MiddleLeft;
var metricWidth = Math.Min(S(178), (_dailyCard.Width - titleWidth - pad) / 2);
_usageMetric.Bounds = new Rectangle(titleWidth, S(1), metricWidth, totalsHeight - S(3));
_sessionMetric.Bounds = new Rectangle(_usageMetric.Right, S(1), metricWidth, totalsHeight - S(3));
var footerHeight = _footer.Height;
_expiryLabel.Bounds = new Rectangle(pad, 0, S(110), footerHeight);
_planLabel.Bounds = new Rectangle(_expiryLabel.Right + S(8), 0, _footer.Width - S(390), footerHeight);
_footerHint.Bounds = new Rectangle(_footer.Width - S(229) - pad, 0, S(229), footerHeight);
_footerHint.Visible = _planLabel.Visible = true;
_footerPoem.Visible = false;
}
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;
_statusDot.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;
_footerCompanySite.Text = tone switch
{
BannerTone.Connected => "网络 · 已连接",
BannerTone.Connecting => "网络 · 正在连接",
BannerTone.Attention => "网络 · 连接异常",
BannerTone.Error => "网络 · 连接失败",
_ => "网络 · 未连接",
};
_footerCompanySite.ForeColor = accent;
}
}