SwkbdAppletDialog.axaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.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. var overlay = new ContentDialogOverlayWindow()
  51. {
  52. Height = window.Bounds.Height,
  53. Width = window.Bounds.Width,
  54. Position = window.PointToScreen(new Point())
  55. };
  56. window.PositionChanged += OverlayOnPositionChanged;
  57. void OverlayOnPositionChanged(object sender, PixelPointEventArgs e)
  58. {
  59. overlay.Position = window.PointToScreen(new Point());
  60. }
  61. contentDialog = overlay.ContentDialog;
  62. bool opened = false;
  63. content.SetInputLengthValidation(args.StringLengthMin, args.StringLengthMax);
  64. content._host = contentDialog;
  65. contentDialog.Title = title;
  66. contentDialog.PrimaryButtonText = args.SubmitText;
  67. contentDialog.IsPrimaryButtonEnabled = content._checkLength(content.Message.Length);
  68. contentDialog.SecondaryButtonText = "";
  69. contentDialog.CloseButtonText = LocaleManager.Instance["InputDialogCancel"];
  70. contentDialog.Content = content;
  71. TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs> handler = (sender, eventArgs) =>
  72. {
  73. if (eventArgs.Result == ContentDialogResult.Primary)
  74. {
  75. result = UserResult.Ok;
  76. input = content.Input.Text;
  77. }
  78. };
  79. contentDialog.Closed += handler;
  80. overlay.Opened += OverlayOnActivated;
  81. async void OverlayOnActivated(object sender, EventArgs e)
  82. {
  83. if (opened)
  84. {
  85. return;
  86. }
  87. opened = true;
  88. overlay.Position = window.PointToScreen(new Point());
  89. await contentDialog.ShowAsync();
  90. contentDialog.Closed -= handler;
  91. overlay.Close();
  92. };
  93. await overlay.ShowDialog(window);
  94. return (result, input);
  95. }
  96. public void SetInputLengthValidation(int min, int max)
  97. {
  98. _inputMin = Math.Min(min, max);
  99. _inputMax = Math.Max(min, max);
  100. Error.IsVisible = false;
  101. Error.FontStyle = FontStyle.Italic;
  102. if (_inputMin <= 0 && _inputMax == int.MaxValue) // Disable.
  103. {
  104. Error.IsVisible = false;
  105. _checkLength = length => true;
  106. }
  107. else if (_inputMin > 0 && _inputMax == int.MaxValue)
  108. {
  109. Error.IsVisible = true;
  110. Error.Text = string.Format(LocaleManager.Instance["SwkbdMinCharacters"], _inputMin);
  111. _checkLength = length => _inputMin <= length;
  112. }
  113. else
  114. {
  115. Error.IsVisible = true;
  116. Error.Text = string.Format(LocaleManager.Instance["SwkbdMinRangeCharacters"], _inputMin, _inputMax);
  117. _checkLength = length => _inputMin <= length && length <= _inputMax;
  118. }
  119. Message_TextInput(this, new TextInputEventArgs());
  120. }
  121. private void Message_TextInput(object sender, TextInputEventArgs e)
  122. {
  123. if (_host != null)
  124. {
  125. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
  126. }
  127. }
  128. private void Message_KeyUp(object sender, KeyEventArgs e)
  129. {
  130. if (e.Key == Key.Enter && _host.IsPrimaryButtonEnabled)
  131. {
  132. _host.Hide(ContentDialogResult.Primary);
  133. }
  134. else
  135. {
  136. _host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
  137. }
  138. }
  139. }
  140. }