PopupDemo.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (c) 2022 Vuplex Inc. All rights reserved.
  2. //
  3. // Licensed under the Vuplex Commercial Software Library License, you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // https://vuplex.com/commercial-library-license
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. namespace Vuplex.WebView.Demos {
  16. /// <summary>
  17. /// Sets up the PopupDemo scene, which demonstrates how to use
  18. /// the IWithPopups interface with WebViewPrefab.
  19. /// </summary>
  20. /// <remarks>
  21. /// Links: <br/>
  22. /// - WebViewPrefab docs: https://developer.vuplex.com/webview/WebViewPrefab <br/>
  23. /// - IWithPopups: https://developer.vuplex.com/webview/IWithPopups <br/>
  24. /// - How clicking works: https://support.vuplex.com/articles/clicking <br/>
  25. /// - Other examples: https://developer.vuplex.com/webview/overview#examples <br/>
  26. /// </remarks>
  27. class PopupDemo : MonoBehaviour {
  28. WebViewPrefab _focusedPrefab;
  29. HardwareKeyboardListener _hardwareKeyboardListener;
  30. async void Start() {
  31. // Use a desktop User-Agent to request the desktop versions of websites.
  32. // https://developer.vuplex.com/webview/Web#SetUserAgent
  33. Web.SetUserAgent(false);
  34. // Create a 0.6 x 0.3 webview for the main web content.
  35. var mainWebViewPrefab = WebViewPrefab.Instantiate(0.6f, 0.3f);
  36. mainWebViewPrefab.PixelDensity = 2;
  37. mainWebViewPrefab.transform.parent = transform;
  38. mainWebViewPrefab.transform.localPosition = new Vector3(0, 0, 0.4f);
  39. mainWebViewPrefab.transform.localEulerAngles = new Vector3(0, 180, 0);
  40. _focusedPrefab = mainWebViewPrefab;
  41. _setUpKeyboards();
  42. // Wait for the WebViewPrefab to initialize, because the WebViewPrefab.WebView property
  43. // is null until the prefab has initialized.
  44. await mainWebViewPrefab.WaitUntilInitialized();
  45. // The WebViewPrefab has initialized, so now we can use its WebViewPrefab.WebView property.
  46. var webViewWithPopups = mainWebViewPrefab.WebView as IWithPopups;
  47. if (webViewWithPopups == null) {
  48. mainWebViewPrefab.WebView.LoadHtml(NOT_SUPPORTED_HTML);
  49. return;
  50. }
  51. Debug.Log("Loading Pinterest as an example because it uses popups for third party login. Click 'Login', then select Facebook or Google to open a popup for authentication.");
  52. mainWebViewPrefab.WebView.LoadUrl("https://pinterest.com");
  53. webViewWithPopups.SetPopupMode(PopupMode.LoadInNewWebView);
  54. webViewWithPopups.PopupRequested += async (webView, eventArgs) => {
  55. Debug.Log("Popup opened with URL: " + eventArgs.Url);
  56. var popupPrefab = WebViewPrefab.Instantiate(eventArgs.WebView);
  57. popupPrefab.Resolution = mainWebViewPrefab.Resolution;
  58. _focusedPrefab = popupPrefab;
  59. popupPrefab.transform.parent = transform;
  60. // Place the popup in front of the main webview.
  61. popupPrefab.transform.localPosition = new Vector3(0, 0, 0.39f);
  62. popupPrefab.transform.localEulerAngles = new Vector3(0, 180, 0);
  63. await popupPrefab.WaitUntilInitialized();
  64. popupPrefab.WebView.CloseRequested += (popupWebView, closeEventArgs) => {
  65. Debug.Log("Closing the popup");
  66. _focusedPrefab = mainWebViewPrefab;
  67. popupPrefab.Destroy();
  68. };
  69. };
  70. }
  71. void _setUpKeyboards() {
  72. // Send keys from the hardware (USB or Bluetooth) keyboard to the webview.
  73. // Use separate KeyDown() and KeyUp() methods if the webview supports
  74. // it, otherwise just use IWebView.SendKey().
  75. // https://developer.vuplex.com/webview/IWithKeyDownAndUp
  76. _hardwareKeyboardListener = HardwareKeyboardListener.Instantiate();
  77. _hardwareKeyboardListener.KeyDownReceived += (sender, eventArgs) => {
  78. var webViewWithKeyDown = _focusedPrefab.WebView as IWithKeyDownAndUp;
  79. if (webViewWithKeyDown != null) {
  80. webViewWithKeyDown.KeyDown(eventArgs.Value, eventArgs.Modifiers);
  81. } else {
  82. _focusedPrefab.WebView.SendKey(eventArgs.Value);
  83. }
  84. };
  85. _hardwareKeyboardListener.KeyUpReceived += (sender, eventArgs) => {
  86. var webViewWithKeyUp = _focusedPrefab.WebView as IWithKeyDownAndUp;
  87. webViewWithKeyUp?.KeyUp(eventArgs.Value, eventArgs.Modifiers);
  88. };
  89. // Also add an on-screen keyboard under the main webview.
  90. var keyboard = Keyboard.Instantiate();
  91. keyboard.transform.SetParent(_focusedPrefab.transform, false);
  92. keyboard.transform.localPosition = new Vector3(0, -0.31f, 0);
  93. keyboard.transform.localEulerAngles = Vector3.zero;
  94. keyboard.InputReceived += (sender, eventArgs) => {
  95. _focusedPrefab.WebView.SendKey(eventArgs.Value);
  96. };
  97. }
  98. const string NOT_SUPPORTED_HTML = @"
  99. <body>
  100. <style>
  101. body {
  102. font-family: sans-serif;
  103. display: flex;
  104. justify-content: center;
  105. align-items: center;
  106. line-height: 1.25;
  107. }
  108. div {
  109. max-width: 80%;
  110. }
  111. li {
  112. margin: 10px 0;
  113. }
  114. </style>
  115. <div>
  116. <p>
  117. Sorry, but this 3D WebView package doesn't support yet the <a href='https://developer.vuplex.com/webview/IWithPopups'>IWithPopups</a> interface. Current packages that support popups:
  118. </p>
  119. <ul>
  120. <li>
  121. <a href='https://developer.vuplex.com/webview/StandaloneWebView'>3D WebView for Windows and macOS</a>
  122. </li>
  123. <li>
  124. <a href='https://developer.vuplex.com/webview/AndroidWebView'>3D WebView for Android</a>
  125. </li>
  126. <li>
  127. <a href='https://developer.vuplex.com/webview/AndroidGeckoWebView'>3D WebView for Android with Gecko Engine</a>
  128. </li>
  129. </ul>
  130. </div>
  131. </body>
  132. ";
  133. }
  134. }