SwkbdAppletDialog.axaml.cs 4.7 KB

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