Initial
This commit is contained in:
1 parent
2cff35f215
commit
0f7377a1b5
359 files changed
+253980
-74
No files matched your search
@@ -0,0 +1,338 @@
|
||||
using System.Drawing.Drawing2D;
|
||||
|
||||
namespace EonaCat.XSBOX.Launcher;
|
||||
|
||||
/// <summary>A control that can report the height it needs for a given width.</summary>
|
||||
internal interface IStackItem
|
||||
{
|
||||
int Measure(int width);
|
||||
}
|
||||
|
||||
/// <summary>DPI scaling, cached fonts and text measuring shared by all launcher windows.</summary>
|
||||
internal static class Ui
|
||||
{
|
||||
static float? scale;
|
||||
static ToolTip? tip;
|
||||
static readonly Dictionary<string, Font> fonts = new(StringComparer.Ordinal);
|
||||
|
||||
public const TextFormatFlags WrapFlags = TextFormatFlags.WordBreak | TextFormatFlags.NoPrefix | TextFormatFlags.Left | TextFormatFlags.Top;
|
||||
|
||||
public static ToolTip Tip => tip ??= new ToolTip { AutoPopDelay = 12000, InitialDelay = 450, ReshowDelay = 150, ShowAlways = true };
|
||||
|
||||
/// <summary>Scales a 96-DPI pixel value to the current display.</summary>
|
||||
public static int S(int value)
|
||||
{
|
||||
scale ??= ReadScale();
|
||||
return (int)Math.Round(value * scale.Value);
|
||||
}
|
||||
|
||||
static float ReadScale()
|
||||
{
|
||||
using var g = Graphics.FromHwnd(IntPtr.Zero);
|
||||
return g.DpiX / 96f;
|
||||
}
|
||||
|
||||
/// <summary>Fonts are created once and reused for the lifetime of the app.</summary>
|
||||
public static Font Font(float size, FontStyle style = FontStyle.Regular, string family = "Segoe UI")
|
||||
{
|
||||
var key = $"{family}|{size}|{(int)style}";
|
||||
if (!fonts.TryGetValue(key, out var font)) fonts[key] = font = new Font(family, size, style);
|
||||
return font;
|
||||
}
|
||||
|
||||
public static int MeasureWrapped(string text, Font font, int width) =>
|
||||
string.IsNullOrEmpty(text)
|
||||
? font.Height
|
||||
: TextRenderer.MeasureText(text, font, new Size(Math.Max(width, 1), int.MaxValue), WrapFlags).Height;
|
||||
|
||||
public static int MeasureWidth(string text, Font font) =>
|
||||
TextRenderer.MeasureText(text, font, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.NoPadding).Width;
|
||||
}
|
||||
|
||||
/// <summary>A label that always wraps and always reports the height its text needs.</summary>
|
||||
internal sealed class WrapLabel : Label, IStackItem
|
||||
{
|
||||
int maxLines;
|
||||
|
||||
public WrapLabel()
|
||||
{
|
||||
AutoSize = false;
|
||||
UseMnemonic = false; // '&' in "Display & Stats" must be drawn, not swallowed
|
||||
BackColor = Color.Transparent;
|
||||
Font = Ui.Font(9.5f);
|
||||
}
|
||||
|
||||
/// <summary>When true the label resizes its own height (used for docked labels such as the status bar).</summary>
|
||||
public bool AutoHeight { get; set; }
|
||||
|
||||
/// <summary>With AutoHeight, caps how many lines the label may grow to (0 = unlimited).</summary>
|
||||
public int MaxLines { get => maxLines; set => maxLines = value; }
|
||||
|
||||
public int Measure(int width)
|
||||
{
|
||||
var h = Ui.MeasureWrapped(Text, Font, width - Padding.Horizontal - Ui.S(4)); // small slack: Label draws with slightly different padding than it measures
|
||||
if (maxLines > 0) h = Math.Min(h, Font.Height * maxLines);
|
||||
return h + Padding.Vertical + 2;
|
||||
}
|
||||
|
||||
void FitHeight()
|
||||
{
|
||||
if (!AutoHeight || Width <= 0) return;
|
||||
var h = Measure(Width);
|
||||
if (h != Height) Height = h;
|
||||
}
|
||||
|
||||
protected override void OnTextChanged(EventArgs e)
|
||||
{
|
||||
base.OnTextChanged(e);
|
||||
if (AutoHeight) FitHeight();
|
||||
else Parent?.PerformLayout();
|
||||
}
|
||||
|
||||
protected override void OnSizeChanged(EventArgs e)
|
||||
{
|
||||
base.OnSizeChanged(e);
|
||||
if (AutoHeight) FitHeight();
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class StretchProgressBar : ProgressBar, IStackItem
|
||||
{
|
||||
public int Measure(int width) => Height;
|
||||
}
|
||||
|
||||
internal sealed class FlowRow : FlowLayoutPanel, IStackItem
|
||||
{
|
||||
public FlowRow()
|
||||
{
|
||||
WrapContents = true;
|
||||
BackColor = Color.Transparent;
|
||||
AutoSize = false;
|
||||
}
|
||||
|
||||
public int Measure(int width) => GetPreferredSize(new Size(width, 0)).Height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Vertical layout where every child is as wide as the panel and as tall as its text needs.
|
||||
/// Works as a scrolling page body or, with AutoScroll off, as a card inside another stack.
|
||||
/// Layout is skipped while the panel is hidden, so tabs that were never opened cost nothing.
|
||||
/// </summary>
|
||||
internal sealed class StackPanel : Panel, IStackItem
|
||||
{
|
||||
bool inLayout;
|
||||
|
||||
public StackPanel()
|
||||
{
|
||||
AutoScroll = true;
|
||||
DoubleBuffered = true;
|
||||
ResizeRedraw = true;
|
||||
}
|
||||
|
||||
/// <summary>Limits the content width on very wide windows (0 = no limit).</summary>
|
||||
public int MaxContentWidth { get; set; }
|
||||
|
||||
static Size ChildSize(Control c, int width)
|
||||
{
|
||||
if (c is IStackItem item) return new Size(width, item.Measure(width));
|
||||
return c.AutoSize ? c.GetPreferredSize(Size.Empty) : c.Size;
|
||||
}
|
||||
|
||||
public int ContentHeight(int width)
|
||||
{
|
||||
var total = 0;
|
||||
foreach (Control c in Controls)
|
||||
{
|
||||
var inner = width - c.Margin.Horizontal;
|
||||
total += c.Margin.Top + ChildSize(c, inner).Height + c.Margin.Bottom;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
public int Measure(int width) => Padding.Vertical + ContentHeight(width - Padding.Horizontal);
|
||||
|
||||
protected override void OnVisibleChanged(EventArgs e)
|
||||
{
|
||||
base.OnVisibleChanged(e);
|
||||
if (Visible) PerformLayout();
|
||||
}
|
||||
|
||||
protected override void OnLayout(LayoutEventArgs levent)
|
||||
{
|
||||
base.OnLayout(levent);
|
||||
if (inLayout || !Visible) return;
|
||||
inLayout = true;
|
||||
try
|
||||
{
|
||||
for (var pass = 0; pass < 3; pass++)
|
||||
{
|
||||
var available = ClientSize.Width - Padding.Horizontal;
|
||||
if (available <= 0) return;
|
||||
var width = MaxContentWidth > 0 ? Math.Min(available, MaxContentWidth) : available;
|
||||
var y = Padding.Top + (AutoScroll ? AutoScrollPosition.Y : 0);
|
||||
foreach (Control c in Controls)
|
||||
{
|
||||
if (!c.Visible) continue;
|
||||
var inner = width - c.Margin.Horizontal;
|
||||
var size = ChildSize(c, inner);
|
||||
c.SetBounds(Padding.Left + c.Margin.Left, y + c.Margin.Top, size.Width, size.Height);
|
||||
y += c.Margin.Top + size.Height + c.Margin.Bottom;
|
||||
}
|
||||
if (AutoScroll) AutoScrollMinSize = new Size(0, y - AutoScrollPosition.Y + Padding.Bottom);
|
||||
// A scrollbar appearing narrows the client area: lay out again with the real width.
|
||||
if (ClientSize.Width - Padding.Horizontal == available) break;
|
||||
}
|
||||
}
|
||||
finally { inLayout = false; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One row of the settings pages: title, fully wrapped description and the value picker.</summary>
|
||||
internal sealed class SettingCard : Panel, IStackItem
|
||||
{
|
||||
readonly Label name = new() { AutoSize = false, AutoEllipsis = true, UseMnemonic = false, BackColor = Color.Transparent, ForeColor = Color.White, Font = Ui.Font(10, FontStyle.Bold) };
|
||||
readonly WrapLabel description = new() { ForeColor = Color.FromArgb(194, 187, 198), Font = Ui.Font(8.75f) };
|
||||
readonly int comboWidth;
|
||||
|
||||
public ComboBox Editor { get; }
|
||||
|
||||
public SettingCard(Setting setting)
|
||||
{
|
||||
BackColor = Color.FromArgb(29, 25, 31);
|
||||
Padding = new Padding(Ui.S(14), Ui.S(10), Ui.S(14), Ui.S(10));
|
||||
Margin = new Padding(0, 0, 0, Ui.S(8));
|
||||
name.Text = setting.Label;
|
||||
description.Text = setting.Description;
|
||||
|
||||
Editor = new ComboBox
|
||||
{
|
||||
DropDownStyle = ComboBoxStyle.DropDownList,
|
||||
FlatStyle = FlatStyle.Flat,
|
||||
BackColor = Color.FromArgb(39, 32, 40),
|
||||
ForeColor = Color.White,
|
||||
Font = Ui.Font(9.5f),
|
||||
FormattingEnabled = true,
|
||||
Tag = setting.Key
|
||||
};
|
||||
Editor.Items.AddRange(setting.Values);
|
||||
Editor.Format += (_, e) =>
|
||||
{
|
||||
if (e.ListItem is string option && option.Length == 0) e.Value = "Auto";
|
||||
};
|
||||
Editor.SelectedItem = setting.Default;
|
||||
|
||||
// Size the picker (and its drop-down) to the longest option so values are never clipped.
|
||||
var widest = setting.Values.Select(v => Ui.MeasureWidth(v.Length == 0 ? "Auto" : v, Editor.Font)).DefaultIfEmpty(0).Max();
|
||||
comboWidth = Math.Clamp(widest + Ui.S(48), Ui.S(200), Ui.S(360));
|
||||
Editor.Width = comboWidth;
|
||||
Editor.DropDownWidth = Math.Max(comboWidth, widest + Ui.S(48));
|
||||
|
||||
// Scrolling over a picker should scroll the page, not silently change the setting.
|
||||
Editor.MouseWheel += (_, e) =>
|
||||
{
|
||||
if (e is HandledMouseEventArgs handled) handled.Handled = true;
|
||||
if (Parent is ScrollableControl { AutoScroll: true } host)
|
||||
{
|
||||
var pos = host.AutoScrollPosition;
|
||||
host.AutoScrollPosition = new Point(-pos.X, -pos.Y - e.Delta);
|
||||
}
|
||||
};
|
||||
|
||||
Controls.Add(name);
|
||||
Controls.Add(description);
|
||||
Controls.Add(Editor);
|
||||
}
|
||||
|
||||
int NameHeight => name.Font.Height + Ui.S(2);
|
||||
int TextWidth(int total) => Math.Max(Ui.S(160), total - Padding.Horizontal - comboWidth - Ui.S(20));
|
||||
|
||||
public int Measure(int width)
|
||||
{
|
||||
var text = Padding.Vertical + NameHeight + Ui.S(4) + description.Measure(TextWidth(width));
|
||||
return Math.Max(text, Padding.Vertical + Ui.S(32));
|
||||
}
|
||||
|
||||
protected override void OnLayout(LayoutEventArgs e)
|
||||
{
|
||||
base.OnLayout(e);
|
||||
if (Editor == null) return; // Padding/Margin are set in the constructor before the picker exists
|
||||
var w = ClientSize.Width;
|
||||
if (w <= 0) return;
|
||||
var textW = TextWidth(w);
|
||||
name.SetBounds(Padding.Left, Padding.Top, textW, NameHeight);
|
||||
description.SetBounds(Padding.Left, Padding.Top + NameHeight + Ui.S(4), textW, description.Measure(textW));
|
||||
Editor.Location = new Point(w - Padding.Right - comboWidth, Padding.Top + Ui.S(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A custom-painted game tile: the name wraps to two lines with an ellipsis and never overflows.</summary>
|
||||
internal sealed class GameTile : Control
|
||||
{
|
||||
static readonly Color Normal = Color.FromArgb(29, 24, 29);
|
||||
static readonly Color Hover = Color.FromArgb(42, 33, 42);
|
||||
static readonly Color Chosen = Color.FromArgb(78, 25, 32);
|
||||
|
||||
readonly GameEntry game;
|
||||
readonly Bitmap? icon;
|
||||
readonly bool selected;
|
||||
bool hover;
|
||||
|
||||
public event Action<GameEntry>? GameChosen;
|
||||
|
||||
public GameTile(GameEntry game, Bitmap? icon, bool selected)
|
||||
{
|
||||
this.game = game;
|
||||
this.icon = icon;
|
||||
this.selected = selected;
|
||||
Size = new Size(Ui.S(232), Ui.S(150));
|
||||
Margin = new Padding(Ui.S(8));
|
||||
Cursor = Cursors.Hand;
|
||||
TabStop = true;
|
||||
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.Selectable, true);
|
||||
Ui.Tip.SetToolTip(this, $"{game.Name}\n{game.Directory}\nClick to select this game");
|
||||
AccessibleName = game.Name;
|
||||
}
|
||||
|
||||
protected override void OnMouseEnter(EventArgs e) { hover = true; Invalidate(); base.OnMouseEnter(e); }
|
||||
protected override void OnMouseLeave(EventArgs e) { hover = false; Invalidate(); base.OnMouseLeave(e); }
|
||||
protected override void OnGotFocus(EventArgs e) { Invalidate(); base.OnGotFocus(e); }
|
||||
protected override void OnLostFocus(EventArgs e) { Invalidate(); base.OnLostFocus(e); }
|
||||
protected override void OnClick(EventArgs e) { base.OnClick(e); Focus(); GameChosen?.Invoke(game); }
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyDown(e);
|
||||
if (e.KeyCode is Keys.Enter or Keys.Space) { e.Handled = true; GameChosen?.Invoke(game); }
|
||||
}
|
||||
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
var g = e.Graphics;
|
||||
using (var back = new SolidBrush(selected ? Chosen : hover ? Hover : Normal)) g.FillRectangle(back, ClientRectangle);
|
||||
var borderWidth = selected ? 2 : 1;
|
||||
using (var pen = new Pen(selected ? Color.FromArgb(245, 55, 65) : Color.FromArgb(93, 43, 49), borderWidth))
|
||||
g.DrawRectangle(pen, borderWidth / 2, borderWidth / 2, Width - borderWidth, Height - borderWidth);
|
||||
|
||||
var pad = Ui.S(12);
|
||||
var iconSize = Ui.S(40);
|
||||
if (icon != null)
|
||||
{
|
||||
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
||||
g.DrawImage(icon, new Rectangle((Width - iconSize) / 2, pad, iconSize, iconSize));
|
||||
}
|
||||
|
||||
var nameFont = Ui.Font(9, FontStyle.Bold);
|
||||
var fileFont = Ui.Font(8, FontStyle.Regular);
|
||||
var textWidth = Width - pad * 2;
|
||||
var fileRect = new Rectangle(pad, Height - pad - fileFont.Height, textWidth, fileFont.Height);
|
||||
var nameTop = pad + iconSize + Ui.S(8);
|
||||
var nameRect = new Rectangle(pad, nameTop, textWidth, Math.Max(nameFont.Height, fileRect.Top - Ui.S(4) - nameTop));
|
||||
|
||||
TextRenderer.DrawText(g, game.Name, nameFont, nameRect, Color.White,
|
||||
TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak | TextFormatFlags.EndEllipsis | TextFormatFlags.NoPrefix | TextFormatFlags.Top);
|
||||
TextRenderer.DrawText(g, Path.GetFileName(game.Executable), fileFont, fileRect, Color.FromArgb(170, 164, 174),
|
||||
TextFormatFlags.HorizontalCenter | TextFormatFlags.EndEllipsis | TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);
|
||||
|
||||
if (Focused && ShowFocusCues) ControlPaint.DrawFocusRectangle(g, new Rectangle(3, 3, Width - 6, Height - 6));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user