GtkHostUiHandler.cs 6.9 KB

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