using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using VibranceHud.Controls; using VibranceHud.Design; using VibranceHud.Games; namespace VibranceHud.Pages; public sealed class ProfilesPage : GlowPage { private readonly AppSettings _settings; private readonly SettingsStore _store; private readonly GameSelection _selection; private readonly IVibranceEngine _engine; private readonly Action? _applyLook; private readonly Func _runningGameId; private readonly Func _gameProfileLookup; private readonly Action _gameProfileSave; private readonly Func> _gameProfileLoadAll; private readonly Action _gameProfileRemove; private readonly Label _title; private readonly Label _subtitle; private CardPanel? _offerCard; private Label? _offerLine; private GlassButton? _offerYes; private GlassButton? _offerNo; private CardPanel? _pickCard; private Label? _pickTitle; private Label? _pickHint; private GameChooser? _picker; private CardPanel? _profileCard; private CardPanel? _listCard; private GameProfileManagerSection? _manager; private int _cardW; private bool _built; private readonly EventHandler _onSelectionChanged; protected override bool LaysOutInLogicalPixels => false; private static int CardPad => S(20); private static int CardGap => S(16); private static int RowTopPad => S(8); private static int RowBottomPad => S(6); private static int TitleToNote => S(2); private static int PickerW => S(260); private static int PickerH => S(32); internal GameProfileManagerSection ProfileManagerForTest => _manager ?? throw new InvalidOperationException("no list built"); internal int RefreshesForTest { get; private set; } private static int TitleHeight => Math.Max(Fonts.LineHeight(FontRole.BodyBold), TextRenderer.MeasureText("Ag", Fonts.BodyBold).Height); private static int NoteLineHeight => Math.Max(Fonts.LineHeight(FontRole.Caption), TextRenderer.MeasureText("Ag", Fonts.Caption).Height); private static int S(int px) { return Tokens.Scale(px); } public ProfilesPage(AppSettings settings, SettingsStore store, GameSelection selection, IVibranceEngine engine, Action? applyLook = null, Func? runningGameId = null, Func? gameProfileLookup = null, Action? gameProfileSave = null, Func>? gameProfileLoadAll = null, Action? gameProfileRemove = null) { _settings = settings; _store = store; _selection = selection; _engine = engine; _applyLook = applyLook; _runningGameId = runningGameId ?? (() => SupportedGames.CurrentlyRunning()?.Id); _gameProfileLookup = gameProfileLookup ?? new Func(GameProfileStore.Get); _gameProfileSave = gameProfileSave ?? new Action(GameProfileStore.Set); _gameProfileLoadAll = gameProfileLoadAll ?? new Func>(GameProfileStore.Load); _gameProfileRemove = gameProfileRemove ?? new Action(GameProfileStore.Remove); AutoScroll = true; Font = Fonts.Body; _title = PageLabel("Profiles", Fonts.Display, Theme.Text); _subtitle = PageLabel("A look for each game — saved here, applied when it opens, put back when it closes.", Fonts.Label, Theme.TextDim); BuildSections(); _onSelectionChanged = (object? _, EventArgs _) => { RefreshProfileCard(); LayoutContent(); }; _selection.Changed += _onSelectionChanged; Resize += (object? _, EventArgs _) => { LayoutContent(); }; ParentChanged += (object? _, EventArgs _) => { if (Parent != null) { Refresh(); } }; _subtitle.TextAlign = ContentAlignment.TopLeft; UseHeaderPanel(_title, _subtitle); _built = true; } protected override void Dispose(bool disposing) { if (disposing) { _selection.Changed -= _onSelectionChanged; } base.Dispose(disposing); } private void BuildSections() { _offerCard = new CardPanel { Visible = false }; Controls.Add(_offerCard); _offerLine = new Label { Text = "", Font = Fonts.Body, ForeColor = Theme.Text, BackColor = Color.Transparent, AutoSize = true, TextAlign = ContentAlignment.TopLeft, UseMnemonic = false }; _offerCard.Controls.Add(_offerLine); _offerYes = new GlassButton { Text = "Yes, set it up", Kind = GlassButtonKind.Primary }; _offerYes.Click += (object? _, EventArgs _) => { AcceptSetupOffer(); }; _offerCard.Controls.Add(_offerYes); _offerNo = new GlassButton { Text = "No thanks", Kind = GlassButtonKind.Secondary }; _offerNo.Click += (object? _, EventArgs _) => { DeclineSetupOffer(); }; _offerCard.Controls.Add(_offerNo); _pickCard = new CardPanel(); _pickTitle = RowTitle(_pickCard, "Pick a game"); _picker = new GameChooser(_selection); _pickCard.Controls.Add(_picker); _pickHint = RowNote(_pickCard, "Choose a game to give it its own colours."); Controls.Add(_pickCard); _listCard = new CardPanel(); Controls.Add(_listCard); _manager = new GameProfileManagerSection(_listCard, Fonts.BodyBold, Fonts.Caption, _gameProfileLoadAll, _gameProfileSave, _gameProfileRemove, (string id) => !string.IsNullOrEmpty(id) && _runningGameId() == id, () => { string text = _runningGameId(); return (text != null) ? SupportedGames.ById(text)?.DisplayName : null; }, SaveCurrentLookForRunningGame); Retitle(_listCard, "SAVED GAME PROFILES", "Saved game profiles"); _manager.Changed += () => { if (_built) { LayoutContent(); Invalidate(invalidateChildren: true); } }; RefreshProfileCard(); } private static void Retitle(Control card, string oldText, string newText) { string value = oldText.Replace(" ", ""); foreach (Label item in card.Controls.OfType