SwkbdAppletDialog.axaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 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. SetInputLengthValidation(0, int.MaxValue); // Disable by default.
  30. }
  31. public SwkbdAppletDialog()
  32. {
  33. DataContext = this;
  34. InitializeComponent();
  35. }
  36. public string Message { get; set; } = "";
  37. public string MainText { get; set; } = "";
  38. public string SecondaryText { get; set; } = "";
  39. public TextBlock Error { get; private set; }
  40. public TextBox Input { get; set; }
  41. private void InitializeComponent()
  42. {
  43. AvaloniaXamlLoader.Load(this);
  44. Error = this.FindControl<TextBlock>("Error");
  45. Input = this.FindControl<TextBox>("Input");
  46. Input.Watermark = _placeholder;
  47. Input.AddHandler(TextInputEvent, Message_TextInput, RoutingStrategies.Tunnel, true);
  48. }
  49. public static async Task<(UserResult Result, string Input)> ShowInputDialog(StyleableWindow window, string title, SoftwareKeyboardUiArgs args)
  50. {
  51. ContentDialog contentDialog = window.ContentDialog;
  52. UserResult result = UserResult.Cancel;
  53. SwkbdAppletDialog content = new SwkbdAppletDialog(args.HeaderText, args.SubtitleText, args.GuideText)
  54. {
  55. Message = args.InitialText ?? ""
  56. };
  57. string input = string.Empty;
  58. content.SetInputLengthValidation(args.StringLengthMin, args.StringLengthMax);
  59. if (contentDialog != null)
  60. {
  61. content._host = contentDialog;
  62. contentDialog.Title = title;
  63. contentDialog.PrimaryButtonText = args.SubmitText;
  64. contentDialog.IsPrimaryButtonEnabled = content._checkLength(content.Message.Length);
  65. contentDialog.SecondaryButtonText = "";
  66. contentDialog.CloseButtonText = LocaleManager.Instance["InputDialogCancel"];
  67. contentDialog.Content = content;
  68. TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs> handler = (sender, eventArgs) =>
  69. {
  70. if (eventArgs.Result == ContentDialogResult.Primary)
  71. {
  72. result = UserResult.Ok;
  73. input = content.Input.Text;
  74. }
  75. };
  76. contentDialog.Closed += handler;
  77. await contentDialog.ShowAsync();
  78. contentDialog.Closed -= handler;
  79. }
  80. return (result, input);
  81. }
  82. public void SetInputLengthValidation(int min, int max)
  83. {
  84. _inputMin = Math.Min(min, max);
  85. _inputMax = Math.Max(min, max);
  86. Error.IsVisible = false;
  87. Error.FontStyle = FontStyle.Italic;
  88. if (_inputMin <= 0 && _inputMax == int.MaxValue) // Disable.
  89. {
  90. Error.IsVisible = false;
  91. _checkLength = length => true;
  92. }
  93. else if (_inputMin > 0 && _inputMax == int.MaxValue)
  94. {
  95. Error.IsVisible = true;
  96. Error.Text = string.Format(LocaleManager.Instance["SwkbdMinCharacters"], _inputMin);
  97. _checkLength = length => _inputMin <= length;
  98. }
  99. else
  100. {
  101. Error.IsVisible = true;
  102. Error.Text = string.Format(LocaleManager.Instance["SwkbdMinRangeCharacters"], _inputMin, _inputMax);
  103. _checkLength = length => _inputMin <= length && length <= _inputMax;
  104. }
  105. Message_TextInput(this, new TextInputEventArgs());
  106. }
  107. private void Message_TextInput(object sender, TextInputEventArgs e)
  108. {
  109. if (_host != null)
  110. {
  111. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
  112. }
  113. }
  114. private void Message_KeyUp(object sender, KeyEventArgs e)
  115. {
  116. if (e.Key == Key.Enter && _host.IsPrimaryButtonEnabled)
  117. {
  118. _host.Hide(ContentDialogResult.Primary);
  119. }
  120. else
  121. {
  122. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
  123. }
  124. }
  125. }
  126. }