UniWebView.Popup.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // UniWebView.Popup.cs
  3. // Created by Wang Wei (@onevcat) on 2025-12-28.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. using System;
  18. using System.Collections.Generic;
  19. public partial class UniWebView {
  20. private readonly Dictionary<string, UniWebViewPopup> popupWindows
  21. = new Dictionary<string, UniWebViewPopup>();
  22. private readonly HashSet<string> closedPopupWindowIds
  23. = new HashSet<string>();
  24. /// <summary>
  25. /// Gets a popup window handle by its identifier.
  26. /// </summary>
  27. /// <param name="multipleWindowId">The identifier of the popup window.</param>
  28. /// <returns>A <see cref="UniWebViewPopup"/> instance if found, otherwise null.</returns>
  29. public UniWebViewPopup GetPopupWindow(string multipleWindowId) {
  30. if (string.IsNullOrEmpty(multipleWindowId)) {
  31. return null;
  32. }
  33. UniWebViewPopup popup;
  34. if (popupWindows.TryGetValue(multipleWindowId, out popup)) {
  35. return popup;
  36. }
  37. return null;
  38. }
  39. /// <summary>
  40. /// Sets whether the popup page events should be delivered to Unity.
  41. /// </summary>
  42. /// <param name="enabled">
  43. /// Whether to enable popup page events. Default is `false`.
  44. /// </param>
  45. public void SetPopupPageEventEnabled(bool enabled) {
  46. UniWebViewInterface.SetPopupPageEventEnabled(listener.Name, enabled);
  47. }
  48. internal UniWebViewPopup GetOrCreatePopupWindow(string multipleWindowId) {
  49. closedPopupWindowIds.Remove(multipleWindowId);
  50. var existing = GetPopupWindow(multipleWindowId);
  51. if (existing != null) {
  52. return existing;
  53. }
  54. var popup = new UniWebViewPopup(this, multipleWindowId);
  55. popupWindows[multipleWindowId] = popup;
  56. return popup;
  57. }
  58. internal void InternalClosePopupWindow(string popupId) {
  59. UniWebViewInterface.ClosePopupWindow(listener.Name, popupId);
  60. }
  61. internal void InternalGoBackPopupWindow(string popupId) {
  62. UniWebViewInterface.GoBackPopupWindow(listener.Name, popupId);
  63. }
  64. internal void InternalGoForwardPopupWindow(string popupId) {
  65. UniWebViewInterface.GoForwardPopupWindow(listener.Name, popupId);
  66. }
  67. internal void InternalEvaluateJavaScriptInPopupWindow(
  68. string popupId,
  69. string jsString,
  70. Action<UniWebViewNativeResultPayload> completionHandler
  71. ) {
  72. var identifier = Guid.NewGuid().ToString();
  73. UniWebViewInterface.EvaluateJavaScriptInPopupWindow(listener.Name, popupId, jsString, identifier);
  74. if (completionHandler != null) {
  75. payloadActions.Add(identifier, completionHandler);
  76. }
  77. }
  78. internal void InternalCloseAllPopupWindows() {
  79. InternalCloseAllPopupWindows(ListenerNameOrId);
  80. }
  81. internal void InternalCloseAllPopupWindows(string listenerName) {
  82. // Internal-only: used by root destroy to emit C# closed events before native cleanup.
  83. // This path intentionally separates notification (HandlePopupWindowClosed) from native removal,
  84. // so callers must NOT expose it as a general "close all" API.
  85. if (popupWindows.Count > 0) {
  86. var ids = new List<string>(popupWindows.Keys);
  87. foreach (var popupId in ids) {
  88. HandlePopupWindowClosed(popupId);
  89. }
  90. }
  91. UniWebViewInterface.CloseAllPopupWindows(listenerName);
  92. }
  93. private void HandlePopupWindowClosed(string popupId) {
  94. if (closedPopupWindowIds.Contains(popupId)) {
  95. return;
  96. }
  97. closedPopupWindowIds.Add(popupId);
  98. UniWebViewPopup popup;
  99. if (OnMultipleWindowClosed != null) {
  100. OnMultipleWindowClosed(this, popupId);
  101. }
  102. if (popupWindows.TryGetValue(popupId, out popup)) {
  103. popup.MarkClosed();
  104. popupWindows.Remove(popupId);
  105. if (OnPopupWindowClosed != null) {
  106. OnPopupWindowClosed(this, popup);
  107. }
  108. }
  109. }
  110. internal void InternalOnPopupPageStarted(UniWebViewPopupPageEventPayload payload) {
  111. var popup = GetPopupWindowForEvent(payload);
  112. if (popup != null) {
  113. popup.InternalOnPageStarted(payload.url ?? string.Empty);
  114. }
  115. }
  116. internal void InternalOnPopupPageCommitted(UniWebViewPopupPageEventPayload payload) {
  117. var popup = GetPopupWindowForEvent(payload);
  118. if (popup != null) {
  119. popup.InternalOnPageCommitted(payload.url ?? string.Empty);
  120. }
  121. }
  122. internal void InternalOnPopupPageFinished(UniWebViewPopupPageEventPayload payload) {
  123. var popup = GetPopupWindowForEvent(payload);
  124. if (popup != null && payload.payload != null) {
  125. popup.InternalOnPageFinished(payload.payload);
  126. }
  127. }
  128. internal void InternalOnPopupPageErrorReceived(UniWebViewPopupPageEventPayload payload) {
  129. var popup = GetPopupWindowForEvent(payload);
  130. if (popup != null && payload.payload != null) {
  131. popup.InternalOnPageErrorReceived(payload.payload);
  132. }
  133. }
  134. internal void InternalOnPopupPageProgressChanged(UniWebViewPopupPageEventPayload payload) {
  135. var popup = GetPopupWindowForEvent(payload);
  136. if (popup != null) {
  137. popup.InternalOnPageProgressChanged(payload.progress);
  138. }
  139. }
  140. private UniWebViewPopup GetPopupWindowForEvent(UniWebViewPopupPageEventPayload payload) {
  141. if (payload == null || string.IsNullOrEmpty(payload.popupId)) {
  142. return null;
  143. }
  144. if (closedPopupWindowIds.Contains(payload.popupId)) {
  145. return null;
  146. }
  147. return GetOrCreatePopupWindow(payload.popupId);
  148. }
  149. }