Files

128 lines
8.7 KiB
C#
Raw Permalink 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);
// Logical pixels converted once, independent of WinForms autoscaling.
internal static class DashboardLayout
{
internal static DashboardGeometry Calculate(Size viewport, int dpi, bool showTailnet = false, bool showServerTraffic = false, bool preferCompact = false)
{
if (preferCompact || viewport.Width < 720f * dpi / 96f || viewport.Height < 442f * dpi / 96f) return CalculateCompact(viewport, dpi, showTailnet, showServerTraffic);
var scale = Math.Max(1f, dpi / 96f);
int S(int n) => (int)Math.Round(n * scale);
// Tailnet is a subdivision of the existing speed area, not another
// dashboard row. Fit the complete canvas even on a small/high-DPI screen.
var verticalScale = Math.Min(1f, Math.Max(1, viewport.Height) / ((showServerTraffic ? 718f : 614f) * scale));
int Y(int n) => Math.Max(1, (int)Math.Floor(n * scale * verticalScale));
var margin = S(20);
var gap = Y(10);
var columnGap = S(10);
var width = Math.Max(1, viewport.Width - margin * 2);
var compact = width < S(720);
var half = (width - columnGap) / 2;
var welcome = new Rectangle(margin, Y(8), width - S(132), Y(30));
var account = new Rectangle(margin, Y(38), Math.Min(S(275), width - S(230)), Y(24));
var identity = new Rectangle(account.Right + S(10), Y(38), S(92), Y(23));
var logout = new Rectangle(margin + width - S(104), Y(16), S(104), Y(34));
var status = new Rectangle(margin, Y(72), width, Y(76));
var node = new Rectangle(margin, status.Bottom + gap, half, Y(72));
var mode = new Rectangle(node.Right + columnGap, node.Top, width - half - columnGap, node.Height);
var analyticsTop = mode.Bottom + gap;
var analyticsHeight = Y(276);
var trendHeight = Y(120);
var figuresHeight = analyticsHeight - trendHeight - gap;
var leftHeight = showTailnet ? (figuresHeight - gap) / 2 : figuresHeight;
var traffic = new Rectangle(margin, analyticsTop, half, leftHeight);
var tailnet = showTailnet ? new Rectangle(traffic.Left, traffic.Bottom + gap, half, figuresHeight - leftHeight - gap) : Rectangle.Empty;
var trend = new Rectangle(margin, analyticsTop + figuresHeight + gap, half, trendHeight);
var daily = new Rectangle(traffic.Right + columnGap, analyticsTop, width - half - columnGap, analyticsHeight);
Rectangle Metric(Rectangle parent, int column, bool speed) => new(
S(10) + column * ((parent.Width - S(20)) / 2),
speed ? Math.Max(Y(38), (parent.Height - Y(82)) / 2) : Y(40),
(parent.Width - S(20)) / 2 - S(1),
speed ? Math.Min(Y(82), parent.Height - Y(44)) : Y(82));
// Relative to their matching chart card: totals together; speeds together.
Rectangle Total(int row) => new(S(14), Y(40 + row * 112), daily.Width - S(28), Y(98));
Rectangle[] metrics = [Total(0), Metric(traffic, 0, true), Metric(traffic, 1, true), Total(1)];
var serverTraffic = showServerTraffic ? new Rectangle(margin, daily.Bottom + gap, width, Y(94)) : Rectangle.Empty;
var footer = new Rectangle(margin, (showServerTraffic ? serverTraffic.Bottom : daily.Bottom) + gap, width, Y(76));
return new(welcome, account, identity, logout, status, node, mode, metrics, traffic,
tailnet, trend, daily, serverTraffic, footer, Math.Min(viewport.Height, footer.Bottom + Y(12)), compact, verticalScale);
}
private static DashboardGeometry CalculateCompact(Size viewport, int dpi, bool showTailnet, bool showServers)
{
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 width = Math.Max(S(400), viewport.Width - margin * 2);
var leftWidth = (int)Math.Round((width-gap)*.61);
var welcome = new Rectangle(margin, S(1), width-S(90), S(15));
var account = new Rectangle(margin, S(16), S(175), S(12));
var identity = new Rectangle(account.Right+gap, S(16), S(58), S(12));
var logout = new Rectangle(margin+width-S(70), S(3), S(70), S(23));
var status = new Rectangle(margin, S(30), width, S(40));
var node = new Rectangle(margin, S(74), leftWidth, S(42));
var mode = new Rectangle(node.Right+gap,node.Top,width-leftWidth-gap,node.Height);
var footer = new Rectangle(margin, viewport.Height-S(26), width, S(22));
var server = showServers ? new Rectangle(margin,footer.Top-gap-S(34),width,S(34)) : Rectangle.Empty;
var bottom = (showServers?server.Top:footer.Top)-gap;
var analyticsTop=S(120);
var height=Math.Max(S(112),bottom-analyticsTop);
var statsHeight=showTailnet?S(30):S(60);
var traffic=new Rectangle(margin,analyticsTop,leftWidth,statsHeight);
var tailnet=showTailnet?new Rectangle(margin,traffic.Bottom+gap,leftWidth,S(30)):Rectangle.Empty;
var trendTop=(showTailnet?tailnet.Bottom:traffic.Bottom)+gap;
var trend=new Rectangle(margin,trendTop,leftWidth,analyticsTop+height-trendTop);
var daily=new Rectangle(mode.Left,analyticsTop,mode.Width,height);
var totalHeight=(daily.Height-S(22))/2;
Rectangle Total(int row)=>new(S(5),S(20)+row*totalHeight,daily.Width-S(10),totalHeight-S(2));
var metricWidth=(traffic.Width-S(8))/2;
var metricTop=showTailnet?S(11):S(15);
var metricHeight=traffic.Height-metricTop-S(1);
Rectangle Speed(int col)=>new(S(4)+col*metricWidth,metricTop,metricWidth-S(1),metricHeight);
Rectangle[] metrics=[Total(0),Speed(0),Speed(1),Total(1)];
return new(welcome,account,identity,logout,status,node,mode,metrics,traffic,tailnet,trend,daily,server,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.ModeCard,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 (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(p.Metrics[i].Left<0||p.Metrics[i].Top<0||p.Metrics[i].Right>parent.Width||p.Metrics[i].Bottom>parent.Height)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.DailyCard != baseline.DailyCard || p.Footer != baseline.Footer || p.ContentHeight != baseline.ContentHeight) return false;
if (showTailnet && (p.TailnetTrafficCard.Top <= p.TrafficCard.Bottom || p.SpeedTrendCard.Top<=p.TailnetTrafficCard.Bottom)) return false;
if(p.SpeedTrendCard.Bottom!=p.DailyCard.Bottom)return false;
if (showServers && (p.ServerTrafficCard.Top <= p.DailyCard.Bottom || p.ServerTrafficCard.Bottom >= p.Footer.Top)) return false;
}
return true;
}
}