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

87 lines
5.9 KiB
C#

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);
// One fitted page: local proxy trend, per-server trends, then numeric totals.
// Transport selection changes the local chart caption, never the page geometry.
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 ? 0 : S(22);
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, margin+topOffset, width, S(30));
var configuration = new Rectangle(margin, status.Bottom + gap, width, S(44));
// The right hand actual-state panel is embedded in the same surface.
var actualLeft = width / 2 + S(4);
var actual = new Rectangle(actualLeft, 0, width - actualLeft - S(8), configuration.Height);
var traffic = Rectangle.Empty;
var tailnet = Rectangle.Empty;
var footer = new Rectangle(margin, viewport.Height - margin - S(18), width, S(18));
var daily = new Rectangle(margin, footer.Top - gap - S(22), width, S(22));
var trendTop = configuration.Bottom + gap;
var graphHeight = Math.Max(S(72), daily.Top - gap - trendTop);
var serverHeight = showServerTraffic ? Math.Min(S(100), (int)Math.Round((graphHeight-gap)*.52)) : 0;
var trend = new Rectangle(margin, trendTop, width, graphHeight - (showServerTraffic ? serverHeight+gap : 0));
var servers = showServerTraffic ? new Rectangle(margin, trend.Bottom+gap, width, serverHeight) : Rectangle.Empty;
var headingWidth = S(120);
var speedWidth = (width - headingWidth - S(10)) / 2;
Rectangle Speed(int column) => new(headingWidth + column * speedWidth, 0, speedWidth, S(22));
Rectangle Total(int column) => new(headingWidth + column * speedWidth, 0, speedWidth, 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.SpeedTrendCard,p.DailyCard,p.Footer];
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=p.DailyCard;
if(!new Rectangle(Point.Empty,parent.Size).Contains(p.Metrics[i]))return false;
}
if (p.TrafficCard != Rectangle.Empty || 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(p.SpeedTrendCard.Width!=p.DailyCard.Width || p.DailyCard.Height>Sized(24))return false;
if (p.Metrics[0].Left != p.Metrics[1].Left || p.Metrics[3].Left != p.Metrics[2].Left
|| p.Metrics[0].Width != p.Metrics[1].Width || p.Metrics[3].Width != p.Metrics[2].Width) return false;
if(p.SpeedTrendCard.Height<Sized(45))return false;
if (p.SpeedTrendCard.Top <= p.NodeCard.Bottom || p.DailyCard.Top<=p.SpeedTrendCard.Bottom) return false;
if (showServers && (p.ServerTrafficCard.Top <= p.SpeedTrendCard.Bottom || p.ServerTrafficCard.Bottom >= p.DailyCard.Top)) return false;
int Sized(int n)=>(int)Math.Round(n*Math.Max(1f,dpi/96f));
}
return true;
}
}