Files
ucvl-zonghengjia-open-source/windows/MainForm.Compact.cs
T

207 lines
11 KiB
C#
Raw Normal View History

namespace UCVL.VPNPlus;
internal sealed partial class MainForm
{
// A compact logical canvas; native DPI scaling still happens exactly once.
private bool WideDashboardPresentation => ClientSize.Width >= ScaleLogical(840) && ClientSize.Height >= ScaleLogical(480);
private bool CompactPresentation => !WideDashboardPresentation;
private readonly Font _compactStatusFont=Brand.Font(9.5f,FontStyle.Bold);
private readonly Font _wideStatusFont=Brand.Font(17f,FontStyle.Bold);
private readonly Font _denseStatusFont=Brand.Font(15f,FontStyle.Bold);
private readonly Font _wideChartFont=Brand.Font(9f);
private readonly Font _compactChartFont=Brand.Font(7.5f);
private readonly Font _compactFieldFont=Brand.Font(7.5f, FontStyle.Bold);
private readonly Font _compactStatusDetailFont=Brand.Font(7.5f);
private readonly Font _compactRowFont=Brand.Font(8f,FontStyle.Bold);
private readonly Font _narrowNodeFont=Brand.Font(9.5f, FontStyle.Bold);
private readonly Font _minimumNodeFont=Brand.Font(8.5f, FontStyle.Bold);
private readonly Dictionary<Control, (Font Original, Font? Compact)> _presentationFonts = new();
private bool _presentationFontsDisposed;
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if(!disposing || _presentationFontsDisposed)return;
_presentationFontsDisposed=true;
foreach(var fonts in _presentationFonts.Values)fonts.Compact?.Dispose();
_presentationFonts.Clear();
_compactStatusFont.Dispose();
_wideStatusFont.Dispose();
_denseStatusFont.Dispose();
_wideChartFont.Dispose();
_compactChartFont.Dispose();
_compactFieldFont.Dispose();
_compactStatusDetailFont.Dispose();
_compactRowFont.Dispose();
_narrowNodeFont.Dispose();
_minimumNodeFont.Dispose();
}
private void SetStatusTitleFont(bool wide, bool dense)
{
var selected = wide ? dense ? _denseStatusFont : _wideStatusFont : _compactStatusFont;
if (ReferenceEquals(_statusTitle.Font, selected)) return;
var previous=_statusTitle.Font;
_statusTitle.Font = selected;
if (!ReferenceEquals(previous, _compactStatusFont) && !ReferenceEquals(previous, _wideStatusFont)
&& !ReferenceEquals(previous, _denseStatusFont))
previous.Dispose();
}
private void ApplyCompactTypography()
{
void Visit(Control parent)
{
foreach (Control control in parent.Controls)
{
if (control is CaptionButton || ReferenceEquals(control,_statusTitle)) continue;
if (!_presentationFonts.TryGetValue(control, out var fonts))
{
var original = control.Font;
fonts = (original, new Font(original.FontFamily, Math.Max(7.5f, original.SizeInPoints * .78f), original.Style));
_presentationFonts.Add(control, fonts);
}
control.Font = CompactPresentation ? fonts.Compact! : fonts.Original;
Visit(control);
}
}
Visit(this);
}
private void LayoutCompactHeader()
{
int S(int n) => ScaleLogical(n);
_brandHeader.Height=S(48);
foreach(Control control in _brandHeader.Controls)
{
if(control.Tag is not Rectangle)continue;
if(control is PictureBox)control.Bounds=new(S(8),S(6),S(34),S(36));
else if(control is Label label && label.Text=="纵横家")control.Bounds=new(S(48),S(6),S(176),S(26));
else control.Bounds=new(S(49),S(31),S(180),S(14));
}
_headerPoem.Visible=false;
LayoutWindowChrome();
if (_welcomeLabel.Parent == _brandHeader)
{
_welcomeLabel.Visible=false;
_logoutButton.Visible=_dashboardView.Visible;
}
}
private void LayoutCompactLogin()
{
int S(int n)=>ScaleLogical(n);
_loginView.Size=new(S(400),S(280));
foreach(Control control in _loginView.Controls)
if(control.Tag is Rectangle r)
control.Bounds=new(S((int)Math.Round(r.X*400d/440)),S((int)Math.Round(r.Y*.72)),
S((int)Math.Round(r.Width*400d/440)),S(Math.Max(14,(int)Math.Round(r.Height*.72))));
// Text boxes and the primary action keep comfortable compact hit areas.
_loginText.Parent!.Bounds=new(S(25),S(65),S(350),S(29));
_passwordText.Parent!.Bounds=new(S(25),S(117),S(350),S(29));
_loginButton.Bounds=new(S(25),S(160),S(350),S(31));
var flags=TextFormatFlags.SingleLine;
var prompt=TextRenderer.MeasureText(_registerPrompt.Text,_registerPrompt.Font,Size.Empty,flags);
var link=TextRenderer.MeasureText(_registerLink.Text,_registerLink.Font,Size.Empty,flags);
var rowWidth=prompt.Width+link.Width+S(16);
var left=(_loginView.Width-rowWidth)/2;
_registerPrompt.Bounds=new(left,S(199),prompt.Width+S(4),S(20));
_registerLink.Bounds=new(_registerPrompt.Right+S(8),S(199),link.Width+S(4),S(20));
foreach(Control control in _loginView.Controls)
{
if(control is Label label && label.Text.StartsWith("浏览器完成注册"))control.Bounds=new(S(20),S(226),S(360),S(18));
}
_loginError.Bounds=new(S(20),S(248),S(360),S(24));
_loginView.Left=Math.Max(S(8),(_content.Width-_loginView.Width)/2);
_loginView.Top=Math.Max(S(8),(_content.Height-_loginView.Height)/2);
}
private void LayoutCompactDashboard(DashboardGeometry layout)
{
int S(int n)=>ScaleLogical(n);
Rectangle R(int x,int y,int w,int h)=>new(S(x),S(y),S(w),S(h));
if (CompactPresentation)
{
_welcomeLabel.Parent=_brandHeader;
_logoutButton.Parent=_brandHeader;
_welcomeLabel.Visible=false;
_logoutButton.Visible=_dashboardView.Visible;
LayoutCompactDashboardActions();
_accountLabel.Visible=_identityBadge.Visible=false;
}
else
{
_welcomeLabel.Parent=_dashboardBody;
_logoutButton.Parent=_dashboardBody;
_welcomeLabel.Bounds=layout.WelcomeLabel;
_logoutButton.Bounds=layout.LogoutButton;
_welcomeLabel.Visible=_logoutButton.Visible=_accountLabel.Visible=_identityBadge.Visible=true;
}
_statusDot.Bounds=R(10,4,22,22);
_connectButton.Bounds=new(_statusCard.Width-S(80),S(3),S(70),S(24));
_statusTitle.Bounds=new(S(40),0,_statusCard.Width-S(130),S(16));
_statusDetail.Bounds=new(S(40),S(16),_statusCard.Width-S(130),S(13));
_statusDetail.Font = _compactStatusDetailFont;
_nodeIcon.Visible=_modeIcon.Visible=false;
_scheduleBadge.Visible=false;
var leftWidth=_modeCard.Left-S(8);
_selectionHeading.Bounds=R(10,2,34,20);
_routeHeading.Bounds=R(10,22,34,20);
_routeHeading.Text="模式";
var fieldLeft=S(44); var fieldWidth=leftWidth-fieldLeft;
var splitWidth=Math.Max(S(84),(fieldWidth-S(4))/2);
_connectionModeCombo.Bounds=new(fieldLeft,S(2),_manualNodeCombo.Visible?splitWidth:fieldWidth,S(20));
_manualNodeCombo.Bounds=new(_connectionModeCombo.Right+S(4),S(2),Math.Max(S(60),fieldWidth-splitWidth-S(4)),S(20));
_routeCombo.Bounds=new(fieldLeft,S(22),fieldWidth,S(20));
_nodeHeading.Text="节点";
_nodeHeading.Bounds=R(10,2,34,20);
_currentNodeLabel.Bounds=new(fieldLeft,S(2),_modeCard.Width-fieldLeft-S(2),S(20));
_actualModeHeading.Bounds=R(10,22,34,20);
_routeReadOnly.Bounds=new(fieldLeft,S(22),_modeCard.Width-fieldLeft-S(2),S(20));
foreach(var label in new[]{_selectionHeading,_routeHeading,_nodeHeading,_actualModeHeading,_currentNodeLabel})
{ label.TextAlign=ContentAlignment.MiddleLeft; label.Padding=Padding.Empty; }
_selectionHeading.Font = _routeHeading.Font = _nodeHeading.Font = _actualModeHeading.Font = _compactFieldFont;
_trafficCard.Visible=_tailnetTrafficCard.Visible=false;
_trafficIcon.Visible=_dailyIcon.Visible=_tailnetTrafficIcon.Visible=false;
_trafficHeading.Bounds=new(S(10),0,S(106),_trafficCard.Height-S(2));
_dailyHeading.Bounds=new(S(10),0,S(106),_dailyCard.Height-S(2));
_tailnetTrafficHeading.Text="Tailscale 速度";
_tailnetTrafficHeading.Bounds=new(S(10),0,S(106),_tailnetTrafficCard.Height-S(2));
foreach(var heading in new[]{_trafficHeading,_tailnetTrafficHeading,_dailyHeading})
{ heading.Font=_compactRowFont; heading.TextAlign=ContentAlignment.MiddleLeft; }
if(_tailnetTrafficVisible)
{
_tailnetUploadMetric.Bounds=_uploadMetric.Bounds;
_tailnetDownloadMetric.Bounds=_downloadMetric.Bounds;
}
_trendHeading.Text = !_selectionUiConnected ? "本机代理趋势" :
_currentNodeUsesTailnet ? "代理趋势 · Tailscale" : "代理趋势 · 公网入口";
var headerTop = S(2);
var headerHeight = S(20);
var inset = S(10);
var headingWidth = S(116);
var rangeWidth = S(78);
var rangeLeft = _speedTrendCard.Width - rangeWidth - inset;
_trendHeading.Bounds = new(inset, headerTop, headingWidth, headerHeight);
_trendHeading.TextAlign=ContentAlignment.MiddleLeft;
_historyRangeCombo.Bounds = new(rangeLeft, headerTop, rangeWidth, headerHeight);
var legendLeft = _trendHeading.Right + S(4);
var legendWidth = Math.Max(1, (rangeLeft - S(5) - legendLeft) / 2);
_uploadLegend.Bounds = new(legendLeft, headerTop, legendWidth, headerHeight);
_downloadLegend.Bounds = new(_uploadLegend.Right, headerTop, legendWidth, headerHeight);
_uploadLegend.TextAlign=_downloadLegend.TextAlign=ContentAlignment.MiddleLeft;
_uploadLegend.Visible=_downloadLegend.Visible=true;
_trafficChart.Font=_compactChartFont;
_trafficChart.Bounds = new(S(8), S(23), _speedTrendCard.Width - S(16), Math.Max(1, _speedTrendCard.Height - S(27)));
_historyStatus.Visible=false;
_sampleLabel.Visible=false;
_planLabel.Visible=false;
var footerHeight = Math.Max(S(15), _footer.Height - S(1));
_expiryLabel.Bounds=new(S(10),0,S(96),footerHeight);
_footerCompanySite.Bounds=new(S(107),0,S(145),footerHeight);
_footerHint.Bounds=new(_footer.Width-S(197),0,S(187),footerHeight);
_footerPoem.Visible=false;
_footerHint.Visible=true;
_dashboardBody.Size=_dashboardView.ClientSize;
_dashboardBody.Location=Point.Empty;
LayoutCompactDashboardActions();
}
}