Files
ucvl-zonghengjia-open-source/windows/DashboardLayout.cs
T

85 lines
5.8 KiB
C#
Raw Normal View History

namespace UCVL.VPNPlus;
internal readonly record struct DashboardGeometry(
Rectangle WelcomeLabel, Rectangle AccountLabel, Rectangle IdentityBadge,
Rectangle LogoutButton, Rectangle StatusCard, Rectangle NodeCard, Rectangle ModeCard,
Rectangle[] Metrics, Rectangle TrafficCard, Rectangle TailnetTrafficCard, Rectangle SpeedTrendCard, Rectangle DailyCard, Rectangle ServerTrafficCard, Rectangle Footer,
int ContentHeight, bool Compact, float VerticalScale);
// All measurements are logical pixels converted exactly once. Every data row
// spans the canvas; the optional transport row borrows height from the plot.
internal static class DashboardLayout
{
internal static DashboardGeometry Calculate(Size viewport, int dpi, bool showTailnet = false, bool showServerTraffic = false, bool preferCompact = false)
{
var scale = Math.Max(1f, dpi / 96f);
int S(int n) => (int)Math.Round(n * scale);
var margin = S(8); var gap = S(4);
var compact = preferCompact || viewport.Width < S(720) || viewport.Height < S(442);
var topOffset = compact ? -S(22) : 0;
var width = Math.Max(1, viewport.Width - margin * 2);
var welcome = new Rectangle(margin, 0, S(120), S(18));
var account = new Rectangle(welcome.Right + gap, 0, Math.Min(S(160), width - S(252)), S(18));
var identity = new Rectangle(account.Right + gap, S(1), S(50), S(16));
var logout = new Rectangle(margin + width - S(64), 0, S(64), S(19));
var status = new Rectangle(margin, S(22)+topOffset, width, S(32));
var configuration = new Rectangle(margin, S(58)+topOffset, width, S(48));
// The right hand actual-state panel is embedded in the same surface.
var actualLeft = (int)Math.Round(width * .52);
var actual = new Rectangle(actualLeft, S(3), width - actualLeft - S(8), S(42));
var traffic = new Rectangle(margin, S(110)+topOffset, width, S(24));
var tailnet = showTailnet ? new Rectangle(margin, traffic.Bottom + gap, width, S(24)) : Rectangle.Empty;
var daily = new Rectangle(margin, (showTailnet ? tailnet.Bottom : traffic.Bottom) + gap, width, S(28));
var footer = new Rectangle(margin, viewport.Height - S(22), width, S(18));
var servers = showServerTraffic ? new Rectangle(margin, footer.Top - gap - S(38), width, S(38)) : Rectangle.Empty;
var trendTop = daily.Bottom + gap;
var trend = new Rectangle(margin, trendTop, width, Math.Max(S(28), (showServerTraffic ? servers.Top : footer.Top) - gap - trendTop));
var headingWidth = S(112);
var speedWidth = (width - headingWidth - S(16)) / 2;
Rectangle Speed(int column) => new(headingWidth + column * speedWidth, 0, speedWidth, traffic.Height - S(2));
var totalWidth = (width - S(76)) / 2;
Rectangle Total(int column) => new(S(66) + column * totalWidth, 0, totalWidth, daily.Height - S(2));
Rectangle[] metrics = [Total(0), Speed(0), Speed(1), Total(1)];
return new(welcome, account, identity, logout, status, configuration, actual, metrics,
traffic, tailnet, trend, daily, servers, footer, viewport.Height, true, 1f);
}
internal static bool SelfTest()
{
foreach (var dpi in new[] { 96, 120, 144, 168, 192, 240, 288, 336, 384 })
foreach (var width in new[] { 480, 600, 620, 739, 760, 780, 900, 1100, 1600 })
foreach (var height in new[] { 312, 350, 418, 560, 720 })
foreach (var showTailnet in new[] { false, true })
foreach (var showServers in new[] { false, true })
{
var viewport = new Size(width * dpi / 96, height * dpi / 96);
var p = Calculate(viewport, dpi, showTailnet, showServers);
Rectangle[] cards = [p.StatusCard,p.NodeCard,p.TrafficCard,p.SpeedTrendCard,p.DailyCard,p.Footer];
if(showTailnet)cards=[..cards,p.TailnetTrafficCard];
if(showServers)cards=[..cards,p.ServerTrafficCard];
if (cards.Any(c => c.Left < 0 || c.Right > viewport.Width || c.Bottom > p.ContentHeight || c.Width <= 0 || c.Height<=0)) return false;
for (var i = 0; i < cards.Length; i++)
for (var j = i + 1; j < cards.Length; j++)
if (cards[i].IntersectsWith(cards[j])) return false;
if (p.WelcomeLabel.IntersectsWith(p.LogoutButton) || p.AccountLabel.IntersectsWith(p.IdentityBadge)) return false;
if (!new Rectangle(Point.Empty, p.NodeCard.Size).Contains(p.ModeCard)) return false;
if (p.Metrics[0].IntersectsWith(p.Metrics[3]) || p.Metrics[1].IntersectsWith(p.Metrics[2])) return false;
for (var i = 0; i < p.Metrics.Length; i++)
{
var parent=i==0||i==3?p.DailyCard:p.TrafficCard;
if(!new Rectangle(Point.Empty,parent.Size).Contains(p.Metrics[i]))return false;
}
if (!showTailnet && p.TailnetTrafficCard != Rectangle.Empty) return false;
if (p.ContentHeight > viewport.Height || p.Footer.Bottom > viewport.Height) return false;
var baseline = Calculate(viewport, dpi, false, showServers);
if (p.Footer != baseline.Footer || p.ContentHeight != baseline.ContentHeight || p.TrafficCard != baseline.TrafficCard) return false;
if (showTailnet && (p.TailnetTrafficCard.Top <= p.TrafficCard.Bottom || p.DailyCard.Top<=p.TailnetTrafficCard.Bottom)) return false;
if(p.TrafficCard.Width!=p.SpeedTrendCard.Width || p.DailyCard.Height>Sized(40))return false;
if(p.SpeedTrendCard.Height<Sized(45))return false;
if (showServers && (p.ServerTrafficCard.Top <= p.SpeedTrendCard.Bottom || p.ServerTrafficCard.Bottom >= p.Footer.Top)) return false;
int Sized(int n)=>(int)Math.Round(n*Math.Max(1f,dpi/96f));
}
return true;
}
}