UniWebViewPopup.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // UniWebViewPopup.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. /// <summary>
  19. /// A lightweight handle to control a popup window created by UniWebView.
  20. /// </summary>
  21. public sealed class UniWebViewPopup {
  22. private readonly string id;
  23. private readonly UniWebView owner;
  24. private bool isAlive = true;
  25. internal UniWebViewPopup(UniWebView owner, string id) {
  26. this.owner = owner;
  27. this.id = id;
  28. }
  29. /// <summary>
  30. /// The identifier of the popup window.
  31. /// </summary>
  32. public string Id => id;
  33. /// <summary>
  34. /// Whether this popup window is still alive.
  35. /// </summary>
  36. public bool IsAlive => isAlive && owner != null;
  37. /// <summary>
  38. /// The owner UniWebView that created this popup window.
  39. /// </summary>
  40. public UniWebView Owner => owner;
  41. /// <summary>
  42. /// Delegate for popup page started event.
  43. /// </summary>
  44. /// <param name="popup">The popup window handle.</param>
  45. /// <param name="url">The url which the popup is about to load.</param>
  46. public delegate void PopupPageStartedDelegate(UniWebViewPopup popup, string url);
  47. /// <summary>
  48. /// Raised when the popup starts loading a url.
  49. /// </summary>
  50. public event PopupPageStartedDelegate OnPageStarted;
  51. /// <summary>
  52. /// Delegate for popup page committed event.
  53. /// </summary>
  54. /// <param name="popup">The popup window handle.</param>
  55. /// <param name="url">The url which the popup has started receiving content for.</param>
  56. public delegate void PopupPageCommittedDelegate(UniWebViewPopup popup, string url);
  57. /// <summary>
  58. /// Raised when the popup receives response from the server and starts receiving web content.
  59. /// </summary>
  60. public event PopupPageCommittedDelegate OnPageCommitted;
  61. /// <summary>
  62. /// Delegate for popup page finished event.
  63. /// </summary>
  64. /// <param name="popup">The popup window handle.</param>
  65. /// <param name="payload">The payload which contains the loading result.</param>
  66. public delegate void PopupPageFinishedDelegate(UniWebViewPopup popup, UniWebViewNativeResultPayload payload);
  67. /// <summary>
  68. /// Raised when the popup finishes loading a url.
  69. /// </summary>
  70. public event PopupPageFinishedDelegate OnPageFinished;
  71. /// <summary>
  72. /// Delegate for popup page loading error received event.
  73. /// </summary>
  74. /// <param name="popup">The popup window handle.</param>
  75. /// <param name="payload">The payload which contains the loading error.</param>
  76. public delegate void PopupPageErrorReceivedDelegate(UniWebViewPopup popup, UniWebViewNativeResultPayload payload);
  77. /// <summary>
  78. /// Raised when an error encountered during the loading process of popup.
  79. /// </summary>
  80. public event PopupPageErrorReceivedDelegate OnPageErrorReceived;
  81. /// <summary>
  82. /// Delegate for popup page progress changed event.
  83. /// </summary>
  84. /// <param name="popup">The popup window handle.</param>
  85. /// <param name="progress">The loading progress of current popup page.</param>
  86. public delegate void PopupPageProgressChangedDelegate(UniWebViewPopup popup, float progress);
  87. /// <summary>
  88. /// Raised when the loading progress value changes in current popup page.
  89. /// </summary>
  90. public event PopupPageProgressChangedDelegate OnPageProgressChanged;
  91. /// <summary>
  92. /// Closes the popup window.
  93. /// </summary>
  94. public void Close() {
  95. if (!EnsureAlive()) {
  96. return;
  97. }
  98. owner.InternalClosePopupWindow(id);
  99. }
  100. /// <summary>
  101. /// Navigates back in the popup window history. If it cannot go back, the popup will be closed.
  102. /// </summary>
  103. public void GoBack() {
  104. if (!EnsureAlive()) {
  105. return;
  106. }
  107. owner.InternalGoBackPopupWindow(id);
  108. }
  109. /// <summary>
  110. /// Navigates forward in the popup window history.
  111. /// </summary>
  112. public void GoForward() {
  113. if (!EnsureAlive()) {
  114. return;
  115. }
  116. owner.InternalGoForwardPopupWindow(id);
  117. }
  118. /// <summary>
  119. /// Evaluates a JavaScript string in the popup window context.
  120. /// </summary>
  121. public void EvaluateJavaScript(string jsString, Action<UniWebViewNativeResultPayload> completionHandler = null) {
  122. if (!EnsureAlive(completionHandler)) {
  123. return;
  124. }
  125. owner.InternalEvaluateJavaScriptInPopupWindow(id, jsString, completionHandler);
  126. }
  127. internal void MarkClosed() {
  128. isAlive = false;
  129. }
  130. internal void InternalOnPageStarted(string url) {
  131. if (OnPageStarted != null && IsAlive) {
  132. OnPageStarted(this, url);
  133. }
  134. }
  135. internal void InternalOnPageCommitted(string url) {
  136. if (OnPageCommitted != null && IsAlive) {
  137. OnPageCommitted(this, url);
  138. }
  139. }
  140. internal void InternalOnPageFinished(UniWebViewNativeResultPayload payload) {
  141. if (OnPageFinished != null && IsAlive) {
  142. OnPageFinished(this, payload);
  143. }
  144. }
  145. internal void InternalOnPageErrorReceived(UniWebViewNativeResultPayload payload) {
  146. if (OnPageErrorReceived != null && IsAlive) {
  147. OnPageErrorReceived(this, payload);
  148. }
  149. }
  150. internal void InternalOnPageProgressChanged(float progress) {
  151. if (OnPageProgressChanged != null && IsAlive) {
  152. OnPageProgressChanged(this, progress);
  153. }
  154. }
  155. private bool EnsureAlive(Action<UniWebViewNativeResultPayload> completionHandler = null) {
  156. if (IsAlive) {
  157. return true;
  158. }
  159. UniWebViewLogger.Instance.Warning("Popup window is already closed or its owner is unavailable.");
  160. if (completionHandler != null) {
  161. completionHandler(CreateInvalidPayload("Popup window is unavailable."));
  162. }
  163. return false;
  164. }
  165. private UniWebViewNativeResultPayload CreateInvalidPayload(string message) {
  166. return new UniWebViewNativeResultPayload {
  167. identifier = string.Empty,
  168. resultCode = "-1",
  169. data = message,
  170. extra = string.Empty
  171. };
  172. }
  173. }