SwkbdAppletDialog.axaml.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using Avalonia.Controls;
  2. using Avalonia.Input;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Media;
  5. using FluentAvalonia.UI.Controls;
  6. using Ryujinx.Ava.Common.Locale;
  7. using Ryujinx.Ava.UI.Helpers;
  8. using Ryujinx.HLE.HOS.Applets;
  9. using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
  10. using System;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace Ryujinx.Ava.UI.Controls
  14. {
  15. internal partial class SwkbdAppletDialog : UserControl
  16. {
  17. private Predicate<int> _checkLength = _ => true;
  18. private Predicate<string> _checkInput = _ => true;
  19. private int _inputMax;
  20. private int _inputMin;
  21. private readonly 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. }
  34. public SwkbdAppletDialog()
  35. {
  36. DataContext = this;
  37. InitializeComponent();
  38. }
  39. protected override void OnGotFocus(GotFocusEventArgs e)
  40. {
  41. // FIXME: This does not work. Might be a bug in Avalonia with DialogHost
  42. // Currently focus will be redirected to the overlay window instead.
  43. Input.Focus();
  44. }
  45. public string Message { get; set; } = "";
  46. public string MainText { get; set; } = "";
  47. public string SecondaryText { get; set; } = "";
  48. public static async Task<(UserResult Result, string Input)> ShowInputDialog(string title, SoftwareKeyboardUiArgs args)
  49. {
  50. ContentDialog contentDialog = new();
  51. UserResult result = UserResult.Cancel;
  52. SwkbdAppletDialog content = new(args.HeaderText, args.SubtitleText, args.GuideText, args.InitialText);
  53. string input = string.Empty;
  54. content.SetInputLengthValidation(args.StringLengthMin, args.StringLengthMax);
  55. content.SetInputValidation(args.KeyboardMode);
  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. void Handler(ContentDialog sender, ContentDialogClosedEventArgs 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. private void ApplyValidationInfo(string text)
  76. {
  77. Error.IsVisible = !string.IsNullOrEmpty(text);
  78. Error.Text = text;
  79. }
  80. public void SetInputLengthValidation(int min, int max)
  81. {
  82. _inputMin = Math.Min(min, max);
  83. _inputMax = Math.Max(min, max);
  84. Error.IsVisible = false;
  85. Error.FontStyle = FontStyle.Italic;
  86. string validationInfoText = "";
  87. if (_inputMin <= 0 && _inputMax == int.MaxValue) // Disable.
  88. {
  89. Error.IsVisible = false;
  90. _checkLength = length => true;
  91. }
  92. else if (_inputMin > 0 && _inputMax == int.MaxValue)
  93. {
  94. validationInfoText = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SwkbdMinCharacters, _inputMin);
  95. _checkLength = length => _inputMin <= length;
  96. }
  97. else
  98. {
  99. validationInfoText = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SwkbdMinRangeCharacters, _inputMin, _inputMax);
  100. _checkLength = length => _inputMin <= length && length <= _inputMax;
  101. }
  102. ApplyValidationInfo(validationInfoText);
  103. Message_TextInput(this, new TextInputEventArgs());
  104. }
  105. private void SetInputValidation(KeyboardMode mode)
  106. {
  107. string validationInfoText = Error.Text;
  108. string localeText;
  109. switch (mode)
  110. {
  111. case KeyboardMode.NumbersOnly:
  112. localeText = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SoftwareKeyboardModeNumbersOnly);
  113. validationInfoText = string.IsNullOrEmpty(validationInfoText) ? localeText : string.Join("\n", validationInfoText, localeText);
  114. _checkInput = text => text.All(char.IsDigit);
  115. break;
  116. case KeyboardMode.Alphabet:
  117. localeText = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SoftwareKeyboardModeAlphabet);
  118. validationInfoText = string.IsNullOrEmpty(validationInfoText) ? localeText : string.Join("\n", validationInfoText, localeText);
  119. _checkInput = text => text.All(value => !CJKCharacterValidation.IsCJK(value));
  120. break;
  121. case KeyboardMode.ASCII:
  122. localeText = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SoftwareKeyboardModeASCII);
  123. validationInfoText = string.IsNullOrEmpty(validationInfoText) ? localeText : string.Join("\n", validationInfoText, localeText);
  124. _checkInput = text => text.All(char.IsAscii);
  125. break;
  126. default:
  127. _checkInput = _ => true;
  128. break;
  129. }
  130. ApplyValidationInfo(validationInfoText);
  131. Message_TextInput(this, new TextInputEventArgs());
  132. }
  133. private void Message_TextInput(object sender, TextInputEventArgs e)
  134. {
  135. if (_host != null)
  136. {
  137. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length) && _checkInput(Message);
  138. }
  139. }
  140. private void Message_KeyUp(object sender, KeyEventArgs e)
  141. {
  142. if (e.Key == Key.Enter && _host.IsPrimaryButtonEnabled)
  143. {
  144. _host.Hide(ContentDialogResult.Primary);
  145. }
  146. else
  147. {
  148. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length) && _checkInput(Message);
  149. }
  150. }
  151. }
  152. }