using Avalonia; using Avalonia.Controls; using Avalonia.Threading; using Avalonia.VisualTree; namespace Quiver.Services; public sealed class GamepadModalDialogNavigation { private static GamepadModalDialogNavigation? _instance; private readonly List _dialogStack = []; private readonly Dictionary> _questionResultCallbacks = new(); private List _dialogControls = []; private int _focusedControlIndex; public static GamepadModalDialogNavigation Instance => _instance ??= new GamepadModalDialogNavigation(); public bool HasActiveDialog => _dialogStack.Count > 0; public Window? ActiveDialog => _dialogStack.Count > 0 ? _dialogStack[^1] : null; public int DialogStackCount => _dialogStack.Count; public void Configure(InputService inputService) { } public static void Attach(Window dialog) => Attach(dialog, setQuestionResult: null); public static void Attach(Window dialog, Action? setQuestionResult) { Instance.PrepareDialog(dialog, setQuestionResult); } public void PrepareDialog(Window dialog, Action? setQuestionResult = null) { if (setQuestionResult != null) _questionResultCallbacks[dialog] = setQuestionResult; else _questionResultCallbacks.Remove(dialog); if (_dialogStack.Contains(dialog)) { BringToTop(dialog); RefreshDialogButtons(); return; } _dialogStack.Add(dialog); RefreshDialogButtons(); void OnDialogOpened(object? sender, EventArgs e) { if (!ReferenceEquals(ActiveDialog, dialog)) return; RefreshDialogButtons(); void OnLayoutUpdated(object? s, EventArgs args) { if (!ReferenceEquals(ActiveDialog, dialog)) return; RefreshDialogButtons(); dialog.LayoutUpdated -= OnLayoutUpdated; } dialog.LayoutUpdated += OnLayoutUpdated; } void OnDialogClosed(object? sender, EventArgs e) { dialog.Opened -= OnDialogOpened; dialog.Closed -= OnDialogClosed; UnregisterModalDialog(dialog); } dialog.Opened += OnDialogOpened; dialog.Closed += OnDialogClosed; } public void RefreshDialogButtons() { var activeDialog = ActiveDialog; if (activeDialog == null) return; _dialogControls = CollectDialogFocusableControls(activeDialog); _focusedControlIndex = GetDefaultFocusIndex(_dialogControls); Dispatcher.UIThread.Post(FocusCurrentControl, DispatcherPriority.Loaded); } public void UnregisterModalDialog(Window dialog) { _questionResultCallbacks.Remove(dialog); var wasTop = ReferenceEquals(ActiveDialog, dialog); if (!_dialogStack.Remove(dialog)) return; if (_dialogStack.Count == 0) { ClearGamepadFocusClasses(_dialogControls); _dialogControls = []; _focusedControlIndex = 0; return; } if (wasTop) RefreshDialogButtons(); } private void BringToTop(Window dialog) { if (!_dialogStack.Remove(dialog)) return; _dialogStack.Add(dialog); } public bool TryHandleNavigation(NavigationDirection direction) { if (ActiveDialog == null) return false; EnsureDialogControls(); if (_dialogControls.Count == 0) return true; if (_dialogControls.Count == 1) return true; var positions = GetControlPositions(_dialogControls, GetControlCenter); _focusedControlIndex = MoveFocusIndex(_focusedControlIndex, direction, positions); FocusCurrentControl(); return true; } public bool TryHandleConfirm() { var activeDialog = ActiveDialog; if (activeDialog == null) return false; EnsureDialogControls(); if (_dialogControls.Count == 0) { InvokeQuestionResult(activeDialog, false); CloseDialogIfStillOpen(); return true; } var control = GetFocusedControl(); if (control == null) return false; if (control is TextBox textBox) { GamepadControlActivation.ActivateTextBox(textBox); return true; } if (control is not Button button) return false; var accepted = IsAffirmativeDialogButtonLabel(GetButtonLabel(button)); InvokeQuestionResult(activeDialog, accepted); ApplyDialogResultHint(activeDialog, button); ActivateAndCloseDialogButton(activeDialog, button); return true; } public bool TryHandleCancel() { var activeDialog = ActiveDialog; if (activeDialog == null) return false; EnsureDialogControls(); if (_dialogControls.Count == 0) { InvokeQuestionResult(activeDialog, false); CloseDialogIfStillOpen(); return true; } var cancelIndex = FindCancelControlIndex(_dialogControls); Control? control = cancelIndex >= 0 ? _dialogControls[cancelIndex] : _dialogControls.Count == 1 ? _dialogControls[0] : GetFocusedControl(); if (control is not Button button) { InvokeQuestionResult(activeDialog, false); CloseDialogIfStillOpen(); return true; } InvokeQuestionResult(activeDialog, false); ApplyDialogResultHint(activeDialog, button); ActivateAndCloseDialogButton(activeDialog, button); return true; } private void InvokeQuestionResult(Window dialog, bool accepted) { if (_questionResultCallbacks.TryGetValue(dialog, out var setResult)) setResult(accepted); } private static void ActivateAndCloseDialogButton(Window dialog, Button button) { GamepadControlActivation.ActivateButton(button); if (dialog.IsVisible) dialog.Close(); } /// /// Stores Yes/No (or equivalent) on before activation/close so /// force-closing a dialog cannot drop the user's choice when Click handlers are skipped. /// internal static void ApplyDialogResultHint(Window dialog, Button button) { var label = GetButtonLabel(button); if (IsAffirmativeDialogButtonLabel(label)) { dialog.Tag = true; return; } if (IsDismissDialogButtonLabel(label)) { dialog.Tag = false; return; } } internal static bool IsAffirmativeDialogButtonLabel(string label) => AffirmativeDialogLabels.Any(preferred => string.Equals(label, preferred, StringComparison.OrdinalIgnoreCase)); internal static bool IsDismissDialogButtonLabel(string label) => DismissDialogLabels.Any(dismiss => string.Equals(label, dismiss, StringComparison.OrdinalIgnoreCase)); private static readonly string[] AffirmativeDialogLabels = [ "ok", "yes", "add", "download anyway", "open settings", "update quiver", "update apps", ]; private static readonly string[] DismissDialogLabels = [ "cancel", "no", "close", "not now", ]; private void CloseDialogIfStillOpen() { var activeDialog = ActiveDialog; if (activeDialog != null && activeDialog.IsVisible) activeDialog.Close(); } private void EnsureDialogControls() { var activeDialog = ActiveDialog; if (activeDialog == null || _dialogControls.Count > 0) return; _dialogControls = CollectDialogFocusableControls(activeDialog); _focusedControlIndex = GetDefaultFocusIndex(_dialogControls); } public static List