SwkbdAppletDialog.axaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Interactivity;
  5. using Avalonia.Media;
  6. using FluentAvalonia.Core;
  7. using FluentAvalonia.UI.Controls;
  8. using Ryujinx.Ava.Common.Locale;
  9. using Ryujinx.Ava.UI.Helpers;
  10. using Ryujinx.Ava.UI.Windows;
  11. using Ryujinx.HLE.HOS.Applets;
  12. using System;
  13. using System.Threading.Tasks;
  14. namespace Ryujinx.Ava.UI.Controls
  15. {
  16. internal partial class SwkbdAppletDialog : UserControl
  17. {
  18. private Predicate<int> _checkLength;
  19. private int _inputMax;
  20. private int _inputMin;
  21. private string _placeholder;
  22. private ContentDialog _host;
  23. public SwkbdAppletDialog(string mainText, string secondaryText, string placeholder, string message)
  24. {
  25. MainText = mainText;
  26. SecondaryText = secondaryText;
  27. Message = message ?? "";
  28. DataContext = this;
  29. _placeholder = placeholder;
  30. InitializeComponent();
  31. Input.Watermark = _placeholder;
  32. Input.AddHandler(TextInputEvent, Message_TextInput, RoutingStrategies.Tunnel, true);
  33. SetInputLengthValidation(0, int.MaxValue); // Disable by default.
  34. }
  35. public SwkbdAppletDialog()
  36. {
  37. DataContext = this;
  38. InitializeComponent();
  39. }
  40. protected override void OnGotFocus(GotFocusEventArgs e)
  41. {
  42. // FIXME: This does not work. Might be a bug in Avalonia with DialogHost
  43. // Currently focus will be redirected to the overlay window instead.
  44. Input.Focus();
  45. }
  46. public string Message { get; set; } = "";
  47. public string MainText { get; set; } = "";
  48. public string SecondaryText { get; set; } = "";
  49. public static async Task<(UserResult Result, string Input)> ShowInputDialog(StyleableWindow window, string title, SoftwareKeyboardUiArgs args)
  50. {
  51. ContentDialog contentDialog = new ContentDialog();
  52. UserResult result = UserResult.Cancel;
  53. SwkbdAppletDialog content = new SwkbdAppletDialog(args.HeaderText, args.SubtitleText, args.GuideText, args.InitialText);
  54. string input = string.Empty;
  55. content.SetInputLengthValidation(args.StringLengthMin, args.StringLengthMax);
  56. content._host = contentDialog;
  57. contentDialog.Title = title;
  58. contentDialog.PrimaryButtonText = args.SubmitText;
  59. contentDialog.IsPrimaryButtonEnabled = content._checkLength(content.Message.Length);
  60. contentDialog.SecondaryButtonText = "";
  61. contentDialog.CloseButtonText = LocaleManager.Instance[LocaleKeys.InputDialogCancel];
  62. contentDialog.Content = content;
  63. TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs> handler = (sender, eventArgs) =>
  64. {
  65. if (eventArgs.Result == ContentDialogResult.Primary)
  66. {
  67. result = UserResult.Ok;
  68. input = content.Input.Text;
  69. }
  70. };
  71. contentDialog.Closed += handler;
  72. await ContentDialogHelper.ShowAsync(contentDialog);
  73. return (result, input);
  74. }
  75. public void SetInputLengthValidation(int min, int max)
  76. {
  77. _inputMin = Math.Min(min, max);
  78. _inputMax = Math.Max(min, max);
  79. Error.IsVisible = false;
  80. Error.FontStyle = FontStyle.Italic;
  81. if (_inputMin <= 0 && _inputMax == int.MaxValue) // Disable.
  82. {
  83. Error.IsVisible = false;
  84. _checkLength = length => true;
  85. }
  86. else if (_inputMin > 0 && _inputMax == int.MaxValue)
  87. {
  88. Error.IsVisible = true;
  89. Error.Text = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SwkbdMinCharacters, _inputMin);
  90. _checkLength = length => _inputMin <= length;
  91. }
  92. else
  93. {
  94. Error.IsVisible = true;
  95. Error.Text = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SwkbdMinRangeCharacters, _inputMin, _inputMax);
  96. _checkLength = length => _inputMin <= length && length <= _inputMax;
  97. }
  98. Message_TextInput(this, new TextInputEventArgs());
  99. }
  100. private void Message_TextInput(object sender, TextInputEventArgs e)
  101. {
  102. if (_host != null)
  103. {
  104. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
  105. }
  106. }
  107. private void Message_KeyUp(object sender, KeyEventArgs e)
  108. {
  109. if (e.Key == Key.Enter && _host.IsPrimaryButtonEnabled)
  110. {
  111. _host.Hide(ContentDialogResult.Primary);
  112. }
  113. else
  114. {
  115. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
  116. }
  117. }
  118. }
  119. }