AvaHostUIHandler.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.Ava.Utilities.Configuration;
  9. using Ryujinx.HLE;
  10. using Ryujinx.HLE.HOS.Applets;
  11. using Ryujinx.HLE.HOS.Applets.SoftwareKeyboard;
  12. using Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy.Types;
  13. using Ryujinx.HLE.UI;
  14. using System;
  15. using System.Threading;
  16. namespace Ryujinx.Ava.UI.Applet
  17. {
  18. internal class AvaHostUIHandler : IHostUIHandler
  19. {
  20. private readonly MainWindow _parent;
  21. public IHostUITheme HostUITheme { get; }
  22. public AvaHostUIHandler(MainWindow parent)
  23. {
  24. _parent = parent;
  25. HostUITheme = new AvaloniaHostUITheme(parent);
  26. }
  27. public bool DisplayMessageDialog(ControllerAppletUIArgs args)
  28. {
  29. ManualResetEvent dialogCloseEvent = new(false);
  30. bool okPressed = false;
  31. if (ConfigurationState.Instance.IgnoreApplet)
  32. return false;
  33. Dispatcher.UIThread.InvokeAsync(async () =>
  34. {
  35. var response = await ControllerAppletDialog.ShowControllerAppletDialog(_parent, args);
  36. if (response == UserResult.Ok)
  37. {
  38. okPressed = true;
  39. }
  40. dialogCloseEvent.Set();
  41. });
  42. dialogCloseEvent.WaitOne();
  43. return okPressed;
  44. }
  45. public bool DisplayMessageDialog(string title, string message)
  46. {
  47. ManualResetEvent dialogCloseEvent = new(false);
  48. bool okPressed = false;
  49. Dispatcher.UIThread.InvokeAsync(async () =>
  50. {
  51. try
  52. {
  53. ManualResetEvent deferEvent = new(false);
  54. bool opened = false;
  55. UserResult response = await ContentDialogHelper.ShowDeferredContentDialog(_parent,
  56. title,
  57. message,
  58. string.Empty,
  59. LocaleManager.Instance[LocaleKeys.DialogOpenSettingsWindowLabel],
  60. string.Empty,
  61. LocaleManager.Instance[LocaleKeys.SettingsButtonClose],
  62. (int)Symbol.Important,
  63. deferEvent,
  64. async window =>
  65. {
  66. if (opened)
  67. {
  68. return;
  69. }
  70. opened = true;
  71. _parent.SettingsWindow = new SettingsWindow(_parent.VirtualFileSystem, _parent.ContentManager);
  72. await _parent.SettingsWindow.ShowDialog(window);
  73. _parent.SettingsWindow = null;
  74. opened = false;
  75. });
  76. if (response == UserResult.Ok)
  77. {
  78. okPressed = true;
  79. }
  80. dialogCloseEvent.Set();
  81. }
  82. catch (Exception ex)
  83. {
  84. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogMessageDialogErrorExceptionMessage, ex));
  85. dialogCloseEvent.Set();
  86. }
  87. });
  88. dialogCloseEvent.WaitOne();
  89. return okPressed;
  90. }
  91. public bool DisplayInputDialog(SoftwareKeyboardUIArgs args, out string userText)
  92. {
  93. ManualResetEvent dialogCloseEvent = new(false);
  94. bool okPressed = false;
  95. bool error = false;
  96. string inputText = args.InitialText ?? string.Empty;
  97. Dispatcher.UIThread.InvokeAsync(async () =>
  98. {
  99. try
  100. {
  101. _parent.ViewModel.AppHost.NpadManager.BlockInputUpdates();
  102. (UserResult result, string userInput) = await SwkbdAppletDialog.ShowInputDialog(LocaleManager.Instance[LocaleKeys.SoftwareKeyboard], args);
  103. if (result == UserResult.Ok)
  104. {
  105. inputText = userInput;
  106. okPressed = true;
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. error = true;
  112. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogSoftwareKeyboardErrorExceptionMessage, ex));
  113. }
  114. finally
  115. {
  116. dialogCloseEvent.Set();
  117. }
  118. });
  119. dialogCloseEvent.WaitOne();
  120. _parent.ViewModel.AppHost.NpadManager.UnblockInputUpdates();
  121. userText = error ? null : inputText;
  122. return error || okPressed;
  123. }
  124. public bool DisplayCabinetDialog(out string userText)
  125. {
  126. ManualResetEvent dialogCloseEvent = new(false);
  127. bool okPressed = false;
  128. string inputText = "My Amiibo";
  129. Dispatcher.UIThread.InvokeAsync(async () =>
  130. {
  131. try
  132. {
  133. _parent.ViewModel.AppHost.NpadManager.BlockInputUpdates();
  134. SoftwareKeyboardUIArgs args = new SoftwareKeyboardUIArgs();
  135. args.KeyboardMode = KeyboardMode.Default;
  136. args.InitialText = "Ryujinx";
  137. args.StringLengthMin = 1;
  138. args.StringLengthMax = 25;
  139. (UserResult result, string userInput) = await SwkbdAppletDialog.ShowInputDialog(LocaleManager.Instance[LocaleKeys.CabinetDialog], args);
  140. if (result == UserResult.Ok)
  141. {
  142. inputText = userInput;
  143. okPressed = true;
  144. }
  145. }
  146. finally
  147. {
  148. dialogCloseEvent.Set();
  149. }
  150. });
  151. dialogCloseEvent.WaitOne();
  152. _parent.ViewModel.AppHost.NpadManager.UnblockInputUpdates();
  153. userText = inputText;
  154. return okPressed;
  155. }
  156. public void DisplayCabinetMessageDialog()
  157. {
  158. ManualResetEvent dialogCloseEvent = new(false);
  159. Dispatcher.UIThread.InvokeAsync(async () =>
  160. {
  161. dialogCloseEvent.Set();
  162. await ContentDialogHelper.CreateInfoDialog(LocaleManager.Instance[LocaleKeys.CabinetScanDialog],
  163. string.Empty,
  164. LocaleManager.Instance[LocaleKeys.InputDialogOk],
  165. string.Empty,
  166. LocaleManager.Instance[LocaleKeys.CabinetTitle]);
  167. });
  168. dialogCloseEvent.WaitOne();
  169. }
  170. public void ExecuteProgram(Switch device, ProgramSpecifyKind kind, ulong value)
  171. {
  172. device.Configuration.UserChannelPersistence.ExecuteProgram(kind, value);
  173. _parent.ViewModel.AppHost?.Stop();
  174. }
  175. public bool DisplayErrorAppletDialog(string title, string message, string[] buttons)
  176. {
  177. ManualResetEvent dialogCloseEvent = new(false);
  178. bool showDetails = false;
  179. Dispatcher.UIThread.InvokeAsync(async () =>
  180. {
  181. try
  182. {
  183. ErrorAppletWindow msgDialog = new(_parent, buttons, message)
  184. {
  185. Title = title,
  186. WindowStartupLocation = WindowStartupLocation.CenterScreen,
  187. Width = 400
  188. };
  189. object response = await msgDialog.Run();
  190. if (response != null && buttons is { Length: > 1 } && (int)response != buttons.Length - 1)
  191. {
  192. showDetails = true;
  193. }
  194. dialogCloseEvent.Set();
  195. msgDialog.Close();
  196. }
  197. catch (Exception ex)
  198. {
  199. dialogCloseEvent.Set();
  200. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogErrorAppletErrorExceptionMessage, ex));
  201. }
  202. });
  203. dialogCloseEvent.WaitOne();
  204. return showDetails;
  205. }
  206. public IDynamicTextInputHandler CreateDynamicTextInputHandler() => new AvaloniaDynamicTextInputHandler(_parent);
  207. }
  208. }