GtkHostUiHandler.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Gtk;
  2. using Ryujinx.HLE;
  3. using Ryujinx.HLE.HOS.Applets;
  4. using Ryujinx.HLE.HOS.Services.Am.AppletOE.ApplicationProxyService.ApplicationProxy.Types;
  5. using Ryujinx.Ui.Widgets;
  6. using System;
  7. using System.Threading;
  8. namespace Ryujinx.Ui.Applet
  9. {
  10. internal class GtkHostUiHandler : IHostUiHandler
  11. {
  12. private readonly Window _parent;
  13. public GtkHostUiHandler(Window parent)
  14. {
  15. _parent = parent;
  16. }
  17. public bool DisplayMessageDialog(ControllerAppletUiArgs args)
  18. {
  19. string playerCount = args.PlayerCountMin == args.PlayerCountMax ? $"exactly {args.PlayerCountMin}" : $"{args.PlayerCountMin}-{args.PlayerCountMax}";
  20. string message = $"Application requests <b>{playerCount}</b> player(s) with:\n\n"
  21. + $"<tt><b>TYPES:</b> {args.SupportedStyles}</tt>\n\n"
  22. + $"<tt><b>PLAYERS:</b> {string.Join(", ", args.SupportedPlayers)}</tt>\n\n"
  23. + (args.IsDocked ? "Docked mode set. <tt>Handheld</tt> is also invalid.\n\n" : "")
  24. + "<i>Please reconfigure Input now and then press OK.</i>";
  25. return DisplayMessageDialog("Controller Applet", message);
  26. }
  27. public bool DisplayMessageDialog(string title, string message)
  28. {
  29. ManualResetEvent dialogCloseEvent = new ManualResetEvent(false);
  30. bool okPressed = false;
  31. Application.Invoke(delegate
  32. {
  33. MessageDialog msgDialog = null;
  34. try
  35. {
  36. msgDialog = new MessageDialog(_parent, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Ok, null)
  37. {
  38. Title = title,
  39. Text = message,
  40. UseMarkup = true
  41. };
  42. msgDialog.SetDefaultSize(400, 0);
  43. msgDialog.Response += (object o, ResponseArgs args) =>
  44. {
  45. if (args.ResponseId == ResponseType.Ok)
  46. {
  47. okPressed = true;
  48. }
  49. dialogCloseEvent.Set();
  50. msgDialog?.Dispose();
  51. };
  52. msgDialog.Show();
  53. }
  54. catch (Exception ex)
  55. {
  56. GtkDialog.CreateErrorDialog($"Error displaying Message Dialog: {ex}");
  57. dialogCloseEvent.Set();
  58. }
  59. });
  60. dialogCloseEvent.WaitOne();
  61. return okPressed;
  62. }
  63. public bool DisplayInputDialog(SoftwareKeyboardUiArgs args, out string userText)
  64. {
  65. ManualResetEvent dialogCloseEvent = new ManualResetEvent(false);
  66. bool okPressed = false;
  67. bool error = false;
  68. string inputText = args.InitialText ?? "";
  69. Application.Invoke(delegate
  70. {
  71. try
  72. {
  73. var swkbdDialog = new SwkbdAppletDialog(_parent)
  74. {
  75. Title = "Software Keyboard",
  76. Text = args.HeaderText,
  77. SecondaryText = args.SubtitleText
  78. };
  79. swkbdDialog.InputEntry.Text = inputText;
  80. swkbdDialog.InputEntry.PlaceholderText = args.GuideText;
  81. swkbdDialog.OkButton.Label = args.SubmitText;
  82. swkbdDialog.SetInputLengthValidation(args.StringLengthMin, args.StringLengthMax);
  83. if (swkbdDialog.Run() == (int)ResponseType.Ok)
  84. {
  85. inputText = swkbdDialog.InputEntry.Text;
  86. okPressed = true;
  87. }
  88. swkbdDialog.Dispose();
  89. }
  90. catch (Exception ex)
  91. {
  92. error = true;
  93. GtkDialog.CreateErrorDialog($"Error displaying Software Keyboard: {ex}");
  94. }
  95. finally
  96. {
  97. dialogCloseEvent.Set();
  98. }
  99. });
  100. dialogCloseEvent.WaitOne();
  101. userText = error ? null : inputText;
  102. return error || okPressed;
  103. }
  104. public void ExecuteProgram(HLE.Switch device, ProgramSpecifyKind kind, ulong value)
  105. {
  106. device.Configuration.UserChannelPersistence.ExecuteProgram(kind, value);
  107. ((MainWindow)_parent).RendererWidget?.Exit();
  108. }
  109. public bool DisplayErrorAppletDialog(string title, string message, string[] buttons)
  110. {
  111. ManualResetEvent dialogCloseEvent = new ManualResetEvent(false);
  112. bool showDetails = false;
  113. Application.Invoke(delegate
  114. {
  115. try
  116. {
  117. ErrorAppletDialog msgDialog = new ErrorAppletDialog(_parent, DialogFlags.DestroyWithParent, MessageType.Error, buttons)
  118. {
  119. Title = title,
  120. Text = message,
  121. UseMarkup = true,
  122. WindowPosition = WindowPosition.CenterAlways
  123. };
  124. msgDialog.SetDefaultSize(400, 0);
  125. msgDialog.Response += (object o, ResponseArgs args) =>
  126. {
  127. if (buttons != null)
  128. {
  129. if (buttons.Length > 1)
  130. {
  131. if (args.ResponseId != (ResponseType)(buttons.Length - 1))
  132. {
  133. showDetails = true;
  134. }
  135. }
  136. }
  137. dialogCloseEvent.Set();
  138. msgDialog?.Dispose();
  139. };
  140. msgDialog.Show();
  141. }
  142. catch (Exception ex)
  143. {
  144. GtkDialog.CreateErrorDialog($"Error displaying ErrorApplet Dialog: {ex}");
  145. dialogCloseEvent.Set();
  146. }
  147. });
  148. dialogCloseEvent.WaitOne();
  149. return showDetails;
  150. }
  151. }
  152. }