Files

68 lines
3.6 KiB
C#
Raw Permalink Normal View History

namespace UCVL.VPNPlus;
internal readonly record struct DashboardWideLayout(Rectangle Banner, Rectangle Configuration, Rectangle Monitor,
Rectangle State, Rectangle Trend, Rectangle Servers, Rectangle Totals, Rectangle Footer)
{
// State belongs to the configuration card. The remaining panels belong to the monitor.
internal static DashboardWideLayout Calculate(Size size, int dpi, bool showServers, bool showManualNode = false)
{
int S(double logical) => (int)Math.Round(logical * dpi / 96d);
var dense = size.Height < S(465);
var margin = S(dense ? 13 : 20);
var gap = S(dense ? 6 : 10);
var width = size.Width - 2 * margin;
var banner = new Rectangle(margin, S(3), width, S(dense ? 53 : 62));
var footer = new Rectangle(margin, size.Height - S(dense ? 31 : 40), width, S(dense ? 25 : 31));
var middleTop = banner.Bottom + gap;
var rowHeight = S(dense ? 26 : 31);
var configuration = new Rectangle(margin, middleTop, width, rowHeight * (showManualNode ? 3 : 2) + S(10));
var stateLeft = (int)(width * .48);
var state = new Rectangle(stateLeft, S(5), width - stateLeft - S(12), rowHeight * 2);
var monitorTop = configuration.Bottom + gap;
var monitor = new Rectangle(margin, monitorTop, width, footer.Top - gap - monitorTop);
var totalHeight = S(dense ? 30 : 38);
var totals = new Rectangle(0, monitor.Height - totalHeight, width, totalHeight);
var serverHeight = showServers ? Math.Clamp(monitor.Height / 3, S(dense ? 58 : 80), S(dense ? 105 : 150)) : 0;
var servers = showServers
? new Rectangle(0, totals.Top - gap - serverHeight, width, serverHeight)
: Rectangle.Empty;
var trendBottom = (showServers ? servers.Top : totals.Top) - gap;
var trend = new Rectangle(0, 0, width, trendBottom);
return new(banner, configuration, monitor, state, trend, servers, totals, footer);
}
internal static bool SelfTest()
{
foreach (var dpi in new[] { 96, 120, 144, 192, 240, 288 })
foreach (var width in new[] { 840, 960, 1160, 1440 })
foreach (var height in new[] { 390, 420, 465, 515, 650, 850 })
foreach (var servers in new[] { false, true })
foreach (var manualNode in new[] { false, true })
{
var size = new Size(width * dpi / 96, height * dpi / 96);
var layout = Calculate(size, dpi, servers, manualNode);
Rectangle[] outer = [layout.Banner, layout.Configuration, layout.Monitor, layout.Footer];
Rectangle[] inner = servers
? [layout.Trend, layout.Servers, layout.Totals]
: [layout.Trend, layout.Totals];
if (!Fits(outer, size) || !Fits(inner, layout.Monitor.Size)
|| !new Rectangle(Point.Empty, layout.Configuration.Size).Contains(layout.State)) return false;
if (layout.Trend.Height < 80 * dpi / 96 || layout.Trend.Width != layout.Banner.Width
|| layout.Totals.Height > Math.Ceiling(38 * dpi / 96d)) return false;
}
return true;
static bool Fits(Rectangle[] cards, Size size)
{
var bounds = new Rectangle(Point.Empty, size);
for (var i = 0; i < cards.Length; i++)
{
if (cards[i].IsEmpty || !bounds.Contains(cards[i])) return false;
for (var j = i + 1; j < cards.Length; j++)
if (cards[i].IntersectsWith(cards[j])) return false;
}
return true;
}
}
}