SwkbdAppletDialog.axaml.cs 4.7 KB

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