316 lines
15 KiB
C#
316 lines
15 KiB
C#
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Imaging;
|
|
|
|
namespace UCVL.VPNPlus;
|
|
|
|
// Decoration only. Text, official logos, controls, counters and plots are never
|
|
// baked into this asset, and none of the theme methods access network state.
|
|
internal static class ScenicTheme
|
|
{
|
|
internal const int HeaderHeight = 84;
|
|
private static readonly Lazy<Image?> Landscape = new(() =>
|
|
{
|
|
// Embedded in the existing EXE payload: an older updater cannot omit
|
|
// this decoration by retaining an old external-assets allowlist.
|
|
using var stream = typeof(ScenicTheme).Assembly.GetManifestResourceStream("UCVL.VPNPlus.assets.landscape-header-v1.png");
|
|
if (stream is null) return null;
|
|
using var source = Image.FromStream(stream);
|
|
return new Bitmap(source);
|
|
});
|
|
internal static bool ArtworkAvailable => Landscape.Value is not null;
|
|
private static readonly Lazy<Image?> LoginScene = new(() =>
|
|
{
|
|
using var stream = typeof(ScenicTheme).Assembly.GetManifestResourceStream("UCVL.VPNPlus.assets.login-cloud-scene-v1.jpg");
|
|
if (stream is null) return null;
|
|
using var source = Image.FromStream(stream);
|
|
return new Bitmap(source);
|
|
});
|
|
internal static bool LoginSceneAvailable => LoginScene.Value is not null;
|
|
|
|
internal static (Rectangle Destination, Rectangle Source) LoginSceneLayout(Size size)
|
|
{
|
|
// The supplied original stays byte-for-byte intact. Exclude only its
|
|
// embedded side letterboxes while drawing; do not stretch the people.
|
|
var source = new Rectangle(352, 0, 1918, 1080);
|
|
if (size.Width <= 0 || size.Height <= 0) return (Rectangle.Empty, source);
|
|
var height = Math.Max(1, Math.Min(size.Height * 2 / 3,
|
|
(int)Math.Round(size.Width * source.Height / (double)source.Width)));
|
|
source.Height = Math.Max(1, Math.Min(source.Height,
|
|
(int)Math.Round(source.Width * height / (double)size.Width)));
|
|
return (new Rectangle(0, size.Height - height, size.Width, height), source);
|
|
}
|
|
|
|
internal static void Art(Graphics g, Rectangle bounds, float opacity = 1)
|
|
{
|
|
if (bounds.Width <= 0 || bounds.Height <= 0 || Landscape.Value is not { } image) return;
|
|
using var attributes = new ImageAttributes();
|
|
attributes.SetColorMatrix(new ColorMatrix { Matrix33 = opacity });
|
|
var interpolation = g.InterpolationMode;
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
g.DrawImage(image, bounds, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
|
|
g.InterpolationMode = interpolation;
|
|
}
|
|
|
|
internal static void Backdrop(Graphics g, Size size, int dpi, bool header = false, bool footer = false,
|
|
bool login = false, bool dashboard = false, int headerArtworkOffset = 0)
|
|
{
|
|
if (size.Width <= 0 || size.Height <= 0) return;
|
|
float scale = dpi / 96f;
|
|
int S(int value) => (int)Math.Round(value * scale);
|
|
g.Clear(Brand.Surface);
|
|
if (footer)
|
|
{
|
|
Art(g, new Rectangle(0, -S(32), size.Width, size.Height + S(72)), .25f);
|
|
return;
|
|
}
|
|
if (dashboard)
|
|
{
|
|
DashboardBackdrop(g, size, dpi, headerArtworkOffset);
|
|
return;
|
|
}
|
|
if (login && Landscape.Value is { } landscape)
|
|
{
|
|
// Only the login canvas gets this centered landscape. Crop out
|
|
// the sky/sun so the title artwork is not repeated. It is painted
|
|
// in parent coordinates, including beneath the translucent card.
|
|
// Center the artwork behind the whole login card, not below its
|
|
// upper fields. The card and scene share parent coordinates.
|
|
var height = Math.Min(size.Height, Math.Max(S(440), size.Width / 3));
|
|
var area = new Rectangle(0, (size.Height - height) / 2, size.Width, height);
|
|
using var attributes = new ImageAttributes();
|
|
attributes.SetColorMatrix(new ColorMatrix { Matrix33 = .57f });
|
|
g.DrawImage(landscape, area, 0, landscape.Height / 3, landscape.Width,
|
|
landscape.Height * 2 / 3, GraphicsUnit.Pixel, attributes);
|
|
var veil = new Rectangle(area.X, area.Y, area.Width, Math.Min(S(64), height));
|
|
using var blend = new LinearGradientBrush(veil, Brand.Surface, Color.FromArgb(0, Brand.Surface), 90f)
|
|
{ WrapMode = WrapMode.TileFlipXY };
|
|
g.FillRectangle(blend, veil);
|
|
var tailHeight = Math.Min(S(88), height);
|
|
var tail = new Rectangle(area.X, area.Bottom - tailHeight, area.Width, tailHeight);
|
|
using var lowerMist = new LinearGradientBrush(tail, Color.FromArgb(0, Brand.Surface), Brand.Surface, 90f)
|
|
{ WrapMode = WrapMode.TileFlipXY };
|
|
g.FillRectangle(lowerMist, tail);
|
|
}
|
|
if (login && LoginScene.Value is { } scene)
|
|
{
|
|
// Decoration belongs only to the lower login canvas. The actual
|
|
// form fields remain separate, opaque controls on the light card.
|
|
var (area, source) = LoginSceneLayout(size);
|
|
using var attributes = new ImageAttributes();
|
|
var interpolation = g.InterpolationMode;
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
void Segment(int y, int height, float alpha)
|
|
{
|
|
attributes.SetColorMatrix(new ColorMatrix { Matrix33 = alpha });
|
|
g.DrawImage(scene, new Rectangle(area.X, area.Y + y, area.Width, height), source.X,
|
|
source.Y + y * source.Height / (float)area.Height, source.Width,
|
|
height * source.Height / (float)area.Height, GraphicsUnit.Pixel, attributes);
|
|
}
|
|
// Fade the illustration itself, preserving the mountains beneath
|
|
// it. An opaque blue veil would leave a hard horizontal seam.
|
|
const float sceneOpacity = .32f;
|
|
var upper = Math.Min(S(112), area.Height / 2);
|
|
var lower = Math.Min(S(48), area.Height / 2);
|
|
var step = Math.Max(1, S(2));
|
|
for (var y = 0; y < upper; y += step)
|
|
{
|
|
var height = Math.Min(step, upper - y);
|
|
Segment(y, height, sceneOpacity * (y + height / 2f) / upper);
|
|
}
|
|
if (area.Height > upper + lower) Segment(upper, area.Height - upper - lower, sceneOpacity);
|
|
for (var y = area.Height - lower; y < area.Height; y += step)
|
|
{
|
|
var height = Math.Min(step, area.Height - y);
|
|
Segment(y, height, sceneOpacity * (area.Height - y - height / 2f) / lower);
|
|
}
|
|
g.InterpolationMode = interpolation;
|
|
}
|
|
// Same image coordinate space above and below the client title area.
|
|
var offset = header ? 0 : headerArtworkOffset > 0 ? headerArtworkOffset : S(HeaderHeight);
|
|
var artHeight = S(HeaderHeight + 72);
|
|
var artwork = new Rectangle(0, -offset, size.Width, artHeight);
|
|
if (login)
|
|
BlendLoginHeader(g, artwork, Math.Max(1, S(58)), Math.Max(1, S(1)));
|
|
else
|
|
{
|
|
Art(g, artwork);
|
|
var fade = new Rectangle(0, artwork.Bottom - S(40), size.Width, S(40) + 1);
|
|
using var mist = new LinearGradientBrush(fade, Color.FromArgb(0, Brand.Surface), Brand.Surface, 90)
|
|
{ WrapMode = WrapMode.TileFlipXY };
|
|
g.FillRectangle(mist, fade);
|
|
}
|
|
if (!header) Clouds(g, new Rectangle(size.Width - S(240), S(8), S(105), S(46)), .3f);
|
|
}
|
|
|
|
private static void BlendLoginHeader(Graphics g, Rectangle artwork, int blendHeight, int step)
|
|
{
|
|
var blendTop = Math.Max(0, artwork.Bottom - blendHeight);
|
|
if (blendTop > 0) DrawBand(0, blendTop, 1f);
|
|
for (var y = blendTop; y < artwork.Bottom; y += step)
|
|
{
|
|
var height = Math.Min(step, artwork.Bottom - y);
|
|
DrawBand(y, height, (artwork.Bottom - y - height / 2f) / blendHeight);
|
|
}
|
|
|
|
void DrawBand(int y, int height, float opacity)
|
|
{
|
|
var state = g.Save();
|
|
g.IntersectClip(new Rectangle(0, y, artwork.Width, height));
|
|
Art(g, artwork, opacity);
|
|
g.Restore(state);
|
|
}
|
|
}
|
|
|
|
private static void DashboardBackdrop(Graphics g, Size size, int dpi, int headerOffset)
|
|
{
|
|
int S(int value) => (int)Math.Round(value * dpi / 96f);
|
|
var bounds = new Rectangle(Point.Empty, size);
|
|
using var sky = new LinearGradientBrush(bounds, Color.FromArgb(211, 235, 251),
|
|
Color.FromArgb(218, 238, 251), 90f)
|
|
{
|
|
WrapMode = WrapMode.TileFlipXY,
|
|
InterpolationColors = new ColorBlend
|
|
{
|
|
Colors = [Color.FromArgb(211, 235, 251), Color.FromArgb(232, 245, 254), Color.FromArgb(218, 238, 251)],
|
|
Positions = [0f, .49f, 1f],
|
|
},
|
|
};
|
|
g.FillRectangle(sky, bounds);
|
|
if (Landscape.Value is not { } landscape) return;
|
|
|
|
// Keep the sun in the header and use the lower mountains as a faint
|
|
// backdrop. Drawing the entire image again duplicates the sun and
|
|
// competes with the chart at compact sizes.
|
|
var scale = Math.Max(size.Width / (double)landscape.Width, size.Height / (landscape.Height * .74));
|
|
var width = (int)Math.Ceiling(landscape.Width * scale);
|
|
var height = (int)Math.Ceiling(landscape.Height * scale);
|
|
Art(g, new Rectangle((size.Width - width) / 2, size.Height - height, width, height), .27f);
|
|
|
|
// Continue the header panorama into the upper edge, fading the image
|
|
// itself into the dashboard scenery so there is no hard divider.
|
|
var panoramaHeight = Math.Max(1, S(HeaderHeight + 72));
|
|
var offset = Math.Clamp(headerOffset > 0 ? headerOffset : S(HeaderHeight), 0, panoramaHeight - 1);
|
|
var transition = Math.Min(Math.Min(size.Height, S(60)), panoramaHeight - offset);
|
|
var step = Math.Max(1, S(2));
|
|
using var attributes = new ImageAttributes();
|
|
var interpolation = g.InterpolationMode;
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
try
|
|
{
|
|
for (var y = 0; y < transition; y += step)
|
|
{
|
|
var bandHeight = Math.Min(step, transition - y);
|
|
var fade = 1f - (y + bandHeight / 2f) / transition;
|
|
var opacity = fade * fade;
|
|
attributes.SetColorMatrix(new ColorMatrix { Matrix33 = opacity });
|
|
g.DrawImage(landscape, new Rectangle(0, y, size.Width, bandHeight), 0,
|
|
(offset + y) * landscape.Height / (float)panoramaHeight, landscape.Width,
|
|
bandHeight * landscape.Height / (float)panoramaHeight, GraphicsUnit.Pixel, attributes);
|
|
}
|
|
}
|
|
finally { g.InterpolationMode = interpolation; }
|
|
}
|
|
|
|
internal static void Clouds(Graphics g, Rectangle area, float opacity)
|
|
{
|
|
if (area.Width <= 0 || area.Height <= 0) return;
|
|
var state = g.Save();
|
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
g.TranslateTransform(area.X, area.Y);
|
|
g.ScaleTransform(area.Width / 140f, area.Height / 60f);
|
|
using var gold = new Pen(Color.FromArgb((int)(255 * opacity), 206, 164, 86), .9f);
|
|
g.DrawBezier(gold, 0, 44, 38, 66, 66, 21, 97, 40);
|
|
g.DrawBezier(gold, 20, 47, 45, 60, 62, 32, 83, 32);
|
|
g.DrawArc(gold, 72, 10, 37, 28, 35, 300);
|
|
g.DrawArc(gold, 80, 15, 23, 18, 25, 285);
|
|
g.DrawBezier(gold, 97, 40, 111, 4, 132, 17, 140, 32);
|
|
g.Restore(state);
|
|
}
|
|
}
|
|
|
|
internal sealed class ScenicPanel : Panel
|
|
{
|
|
private Bitmap? _artBackdrop;
|
|
private int _artBackdropDpi, _artBackdropHeaderOffset, _headerArtworkOffset;
|
|
private bool _artBackdropFooter, _artBackdropLogin, _artBackdropDashboard;
|
|
// Only the account-content subtree is composed. Do not compose the entire
|
|
// top-level window or delay native caption/resize/input handling.
|
|
internal bool ComposeChildren { get; set; }
|
|
internal bool FooterArtwork { get; set; }
|
|
internal bool LoginArtwork { get; set; }
|
|
internal bool DashboardArtwork { get; set; }
|
|
internal int HeaderArtworkOffset
|
|
{
|
|
get => _headerArtworkOffset;
|
|
set
|
|
{
|
|
if (_headerArtworkOffset == value) return;
|
|
_headerArtworkOffset = value;
|
|
if (LoginArtwork || DashboardArtwork) Invalidate(true);
|
|
}
|
|
}
|
|
internal bool ParentArtwork { get; set; }
|
|
protected override CreateParams CreateParams
|
|
{
|
|
get
|
|
{
|
|
var parameters = base.CreateParams;
|
|
if (ComposeChildren) parameters.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
|
|
return parameters;
|
|
}
|
|
}
|
|
internal ScenicPanel() { DoubleBuffered = true; ResizeRedraw = true; BackColor = Brand.Surface; }
|
|
protected override void OnPaintBackground(PaintEventArgs e)
|
|
{
|
|
if (ParentArtwork && Parent is { } parent)
|
|
{
|
|
var state = e.Graphics.Save();
|
|
e.Graphics.TranslateTransform(-Left, -Top);
|
|
InvokePaintBackground(parent, new PaintEventArgs(e.Graphics, parent.ClientRectangle));
|
|
e.Graphics.Restore(state);
|
|
return;
|
|
}
|
|
if (!LoginArtwork && !DashboardArtwork)
|
|
{
|
|
ScenicTheme.Backdrop(e.Graphics, ClientSize, DeviceDpi, footer: FooterArtwork);
|
|
return;
|
|
}
|
|
if (Width <= 0 || Height <= 0) return;
|
|
// Transparent children can ask for the same backdrop many times.
|
|
// Cache only decoration, never the account data or child controls.
|
|
if (_artBackdrop is null || _artBackdrop.Size != ClientSize || _artBackdropDpi != DeviceDpi
|
|
|| _artBackdropFooter != FooterArtwork || _artBackdropLogin != LoginArtwork
|
|
|| _artBackdropDashboard != DashboardArtwork || _artBackdropHeaderOffset != HeaderArtworkOffset)
|
|
{
|
|
Bitmap? next = null;
|
|
try
|
|
{
|
|
next = new Bitmap(Width, Height);
|
|
using (var graphics = Graphics.FromImage(next))
|
|
ScenicTheme.Backdrop(graphics, ClientSize, DeviceDpi, footer: FooterArtwork,
|
|
login: LoginArtwork, dashboard: DashboardArtwork, headerArtworkOffset: HeaderArtworkOffset);
|
|
// Transfer the completed bitmap rather than copying the whole
|
|
// canvas on the UI thread. A failed render leaves the previous
|
|
// cache owned and intact; only the incomplete replacement dies.
|
|
var previous = _artBackdrop;
|
|
_artBackdrop = next;
|
|
next = null;
|
|
_artBackdropDpi = DeviceDpi;
|
|
_artBackdropFooter = FooterArtwork;
|
|
_artBackdropLogin = LoginArtwork;
|
|
_artBackdropDashboard = DashboardArtwork;
|
|
_artBackdropHeaderOffset = HeaderArtworkOffset;
|
|
previous?.Dispose();
|
|
}
|
|
finally { next?.Dispose(); }
|
|
}
|
|
e.Graphics.DrawImageUnscaled(_artBackdrop, Point.Empty);
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing) { _artBackdrop?.Dispose(); _artBackdrop = null; }
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|