AvaHostUiHandler.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using Avalonia.Controls;
  2. using Avalonia.Threading;
  3. using FluentAvalonia.UI.Controls;
  4. using Ryujinx.Ava.Common.Locale;
  5. using Ryujinx.Ava.UI.Controls;
  6. using Ryujinx.Ava.UI.Helpers;
  7. using Ryujinx.Ava.UI.Windows;
  8. using Ryujinx.HLE;
  9. using Ryujinx.HLE.HOS.Applets;
  10. using Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy.Types;
  11. using Ryujinx.HLE.Ui;
  12. using System;
  13. using System.Threading;
  14. namespace Ryujinx.Ava.UI.Applet
  15. {
  16. internal class AvaHostUiHandler : IHostUiHandler
  17. {
  18. private readonly MainWindow _parent;
  19. public IHostUiTheme HostUiTheme { get; }
  20. public AvaHostUiHandler(MainWindow parent)
  21. {
  22. _parent = parent;
  23. HostUiTheme = new AvaloniaHostUiTheme(parent);
  24. }
  25. public bool DisplayMessageDialog(ControllerAppletUiArgs args)
  26. {
  27. string message = LocaleManager.Instance.UpdateAndGetDynamicValue(
  28. args.PlayerCountMin == args.PlayerCountMax ? LocaleKeys.DialogControllerAppletMessage : LocaleKeys.DialogControllerAppletMessagePlayerRange,
  29. args.PlayerCountMin == args.PlayerCountMax ? args.PlayerCountMin.ToString() : $"{args.PlayerCountMin}-{args.PlayerCountMax}",
  30. args.SupportedStyles,
  31. string.Join(", ", args.SupportedPlayers),
  32. args.IsDocked ? LocaleManager.Instance[LocaleKeys.DialogControllerAppletDockModeSet] : "");
  33. return DisplayMessageDialog(LocaleManager.Instance[LocaleKeys.DialogControllerAppletTitle], message);
  34. }
  35. public bool DisplayMessageDialog(string title, string message)
  36. {
  37. ManualResetEvent dialogCloseEvent = new(false);
  38. bool okPressed = false;
  39. Dispatcher.UIThread.InvokeAsync(async () =>
  40. {
  41. try
  42. {
  43. ManualResetEvent deferEvent = new(false);
  44. bool opened = false;
  45. UserResult response = await ContentDialogHelper.ShowDeferredContentDialog(_parent,
  46. title,
  47. message,
  48. "",
  49. LocaleManager.Instance[LocaleKeys.DialogOpenSettingsWindowLabel],
  50. "",
  51. LocaleManager.Instance[LocaleKeys.SettingsButtonClose],
  52. (int)Symbol.Important,
  53. deferEvent,
  54. async (window) =>
  55. {
  56. if (opened)
  57. {
  58. return;
  59. }
  60. opened = true;
  61. _parent.SettingsWindow = new SettingsWindow(_parent.VirtualFileSystem, _parent.ContentManager);
  62. await _parent.SettingsWindow.ShowDialog(window);
  63. opened = false;
  64. });
  65. if (response == UserResult.Ok)
  66. {
  67. okPressed = true;
  68. }
  69. dialogCloseEvent.Set();
  70. }
  71. catch (Exception ex)
  72. {
  73. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogMessageDialogErrorExceptionMessage, ex));
  74. dialogCloseEvent.Set();
  75. }
  76. });
  77. dialogCloseEvent.WaitOne();
  78. return okPressed;
  79. }
  80. public bool DisplayInputDialog(SoftwareKeyboardUiArgs args, out string userText)
  81. {
  82. ManualResetEvent dialogCloseEvent = new(false);
  83. bool okPressed = false;
  84. bool error = false;
  85. string inputText = args.InitialText ?? "";
  86. Dispatcher.UIThread.Post(async () =>
  87. {
  88. try
  89. {
  90. var response = await SwkbdAppletDialog.ShowInputDialog(_parent, LocaleManager.Instance[LocaleKeys.SoftwareKeyboard], args);
  91. if (response.Result == UserResult.Ok)
  92. {
  93. inputText = response.Input;
  94. okPressed = true;
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. error = true;
  100. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogSoftwareKeyboardErrorExceptionMessage, ex));
  101. }
  102. finally
  103. {
  104. dialogCloseEvent.Set();
  105. }
  106. });
  107. dialogCloseEvent.WaitOne();
  108. userText = error ? null : inputText;
  109. return error || okPressed;
  110. }
  111. public void ExecuteProgram(Switch device, ProgramSpecifyKind kind, ulong value)
  112. {
  113. device.Configuration.UserChannelPersistence.ExecuteProgram(kind, value);
  114. if (_parent.ViewModel.AppHost != null)
  115. {
  116. _parent.ViewModel.AppHost.Stop();
  117. }
  118. }
  119. public bool DisplayErrorAppletDialog(string title, string message, string[] buttons)
  120. {
  121. ManualResetEvent dialogCloseEvent = new(false);
  122. bool showDetails = false;
  123. Dispatcher.UIThread.Post(async () =>
  124. {
  125. try
  126. {
  127. ErrorAppletWindow msgDialog = new(_parent, buttons, message)
  128. {
  129. Title = title,
  130. WindowStartupLocation = WindowStartupLocation.CenterScreen,
  131. Width = 400
  132. };
  133. object response = await msgDialog.Run();
  134. if (response != null && buttons != null && buttons.Length > 1 && (int)response != buttons.Length - 1)
  135. {
  136. showDetails = true;
  137. }
  138. dialogCloseEvent.Set();
  139. msgDialog.Close();
  140. }
  141. catch (Exception ex)
  142. {
  143. dialogCloseEvent.Set();
  144. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogErrorAppletErrorExceptionMessage, ex));
  145. }
  146. });
  147. dialogCloseEvent.WaitOne();
  148. return showDetails;
  149. }
  150. public IDynamicTextInputHandler CreateDynamicTextInputHandler()
  151. {
  152. return new AvaloniaDynamicTextInputHandler(_parent);
  153. }
  154. }
  155. }