UniWebViewEmbeddedToolbar.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using UnityEngine;
  2. /// <summary>
  3. /// Represents the embedded toolbar in a web view.
  4. ///
  5. /// You do not create an instance of this class directly. Instead, use the `EmbeddedToolbar` property in `UniWebView` to
  6. /// get the current embedded toolbar in the web view and interact with it.
  7. ///
  8. /// The embedded toolbar of a web view expands its width to match the web view's frame width. It is displayed at either
  9. /// top or bottom of the web view, based on the setting received through `SetPosition`. By default, the toolbar contains
  10. /// a main title, a back button, a forward button and a done button to close the web view.
  11. ///
  12. /// You can customize the toolbar through the config-based approach: create a `UniWebViewEmbeddedToolbarConfig`, set up
  13. /// items (built-in or custom), colors, title interactions, and apply it via `ApplyConfig`. This gives you full control
  14. /// over the toolbar layout, including adding custom buttons, a reload button, and title interactions like scroll-to-top
  15. /// or copy-URL-on-long-press.
  16. ///
  17. /// For simple customization, convenience methods like `SetDoneButtonText`, `SetBackgroundColor`, etc. are also
  18. /// available. These methods modify the current config internally and apply the changes automatically.
  19. /// </summary>
  20. public class UniWebViewEmbeddedToolbar {
  21. private readonly UniWebViewNativeListener listener;
  22. private UniWebViewEmbeddedToolbarConfig currentConfig;
  23. private string lastAppliedJson;
  24. /// <summary>
  25. /// Gets a cloned copy of the current toolbar config.
  26. ///
  27. /// You can use this to inspect the current state of the toolbar, or as a starting point for building a modified
  28. /// config. Changes to the returned object will not affect the toolbar until you pass it to `ApplyConfig`.
  29. /// </summary>
  30. public UniWebViewEmbeddedToolbarConfig CurrentConfig => CloneConfig(currentConfig);
  31. internal UniWebViewEmbeddedToolbar(UniWebViewNativeListener listener) {
  32. this.listener = listener;
  33. currentConfig = UniWebViewEmbeddedToolbarConfig.Default;
  34. lastAppliedJson = null;
  35. }
  36. /// <summary>
  37. /// Sets the position of the embedded toolbar. You can put the toolbar at either top or bottom of your web view.
  38. /// The default position is `Top`.
  39. /// </summary>
  40. /// <param name="position">The desired position of the toolbar.</param>
  41. public void SetPosition(UniWebViewToolbarPosition position) {
  42. currentConfig.Position = position;
  43. ApplyCurrentConfig();
  44. }
  45. /// <summary>
  46. /// Shows the toolbar.
  47. /// </summary>
  48. public void Show() {
  49. currentConfig.Visible = true;
  50. ApplyCurrentConfig();
  51. }
  52. /// <summary>
  53. /// Hides the toolbar.
  54. /// </summary>
  55. public void Hide() {
  56. currentConfig.Visible = false;
  57. ApplyCurrentConfig();
  58. }
  59. /// <summary>
  60. /// Sets the text of the done button. The default text is "Done".
  61. /// </summary>
  62. /// <param name="text">The desired text to display as the done button.</param>
  63. public void SetDoneButtonText(string text) {
  64. SetBuiltInTitle(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Done, text);
  65. ApplyCurrentConfig();
  66. }
  67. /// <summary>
  68. /// Sets the text of the back button. The default text is "❮" ("\u276E").
  69. /// </summary>
  70. /// <param name="text">The desired text to display as the back button.</param>
  71. public void SetGoBackButtonText(string text) {
  72. SetBuiltInTitle(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Back, text);
  73. ApplyCurrentConfig();
  74. }
  75. /// <summary>
  76. /// Sets the text of the forward button. The default text is "❯" ("\u276F").
  77. /// </summary>
  78. /// <param name="text">The desired text to display as the forward button.</param>
  79. public void SetGoForwardButtonText(string text) {
  80. SetBuiltInTitle(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Forward, text);
  81. ApplyCurrentConfig();
  82. }
  83. /// <summary>
  84. /// Sets the text of toolbar title. The default is empty. The space is limited, setting a long text as title might
  85. /// not fit in the space.
  86. /// </summary>
  87. /// <param name="text">The desired text to display as the title in the toolbar.</param>
  88. public void SetTitleText(string text) {
  89. SetBuiltInTitle(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Title, text);
  90. ApplyCurrentConfig();
  91. }
  92. /// <summary>
  93. /// Sets the background color of the toolbar.
  94. /// </summary>
  95. /// <param name="color">The desired color of toolbar's background.</param>
  96. public void SetBackgroundColor(Color color) {
  97. currentConfig.BackgroundColor = UniWebViewEmbeddedToolbarConfig.ColorValue.FromColor(color);
  98. ApplyCurrentConfig();
  99. }
  100. /// <summary>
  101. /// Sets the buttons color of the toolbar. This color affects the back, forward and done button.
  102. /// </summary>
  103. /// <param name="color">The desired color of toolbar's buttons.</param>
  104. public void SetButtonTextColor(Color color) {
  105. currentConfig.ButtonTextColor = UniWebViewEmbeddedToolbarConfig.ColorValue.FromColor(color);
  106. ApplyCurrentConfig();
  107. }
  108. /// <summary>
  109. /// Sets the text color of the toolbar title.
  110. /// </summary>
  111. /// <param name="color">The desired color of the toolbar's title.</param>
  112. public void SetTitleTextColor(Color color) {
  113. currentConfig.TitleTextColor = UniWebViewEmbeddedToolbarConfig.ColorValue.FromColor(color);
  114. ApplyCurrentConfig();
  115. }
  116. /// <summary>
  117. /// Hides the navigation buttons on the toolbar.
  118. ///
  119. /// When called, the back button and forward button will not be shown.
  120. /// By default, the navigation buttons are shown.
  121. /// </summary>
  122. public void HideNavigationButtons() {
  123. SetBuiltInVisible(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Back, false);
  124. SetBuiltInVisible(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Forward, false);
  125. ApplyCurrentConfig();
  126. }
  127. /// <summary>
  128. /// Shows the navigation buttons on the toolbar.
  129. ///
  130. /// When called, the back button and forward button will be shown.
  131. /// By default, the navigation buttons are shown.
  132. /// </summary>
  133. public void ShowNavigationButtons() {
  134. SetBuiltInVisible(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Back, true);
  135. SetBuiltInVisible(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind.Forward, true);
  136. ApplyCurrentConfig();
  137. }
  138. /// <summary>
  139. /// Sets the maximum height of the toolbar. If the specified height is smaller than the toolbar's standard height,
  140. /// the toolbar will be resized to this height. Otherwise, the standard height will be used.
  141. ///
  142. /// This method only works on iOS and Android. On macOS Unity Editor, the toolbar will always be displayed in the
  143. /// window's title bar and the height is fixed.
  144. /// </summary>
  145. /// <param name="height">The maximum height value.</param>
  146. public void SetMaxHeight(float height) {
  147. currentConfig.MaxHeight = height;
  148. ApplyCurrentConfig();
  149. }
  150. /// <summary>
  151. /// Applies a toolbar config to fully customize the toolbar layout and behavior.
  152. ///
  153. /// This is the recommended way to configure the toolbar. You can control which items appear (back, forward, done,
  154. /// reload, title, or custom buttons), their order and placement (left, center, right sections), colors, and title
  155. /// interactions (tap to scroll to top, long press to copy URL).
  156. ///
  157. /// After calling this method, the toolbar will be re-rendered with the given config. Pass <c>null</c> to reset to
  158. /// the default config.
  159. /// </summary>
  160. /// <param name="config">The toolbar config to apply. Pass <c>null</c> to apply the default config.</param>
  161. public void ApplyConfig(UniWebViewEmbeddedToolbarConfig config) {
  162. currentConfig = CloneConfig(config ?? UniWebViewEmbeddedToolbarConfig.Default);
  163. ApplyCurrentConfigDirectly();
  164. }
  165. private void ApplyCurrentConfig() {
  166. ApplyCurrentConfigDirectly();
  167. }
  168. private void ApplyCurrentConfigDirectly() {
  169. var json = currentConfig.ToJson();
  170. if (json == lastAppliedJson) {
  171. return;
  172. }
  173. var applied = UniWebViewEmbeddedToolbarConfigApplier.ApplyJson(listener.Name, json);
  174. if (!applied) {
  175. UniWebViewLogger.Instance.Debug("Embedded toolbar config not applied. The native side may not support setEmbeddedToolbarConfig yet.");
  176. return;
  177. }
  178. lastAppliedJson = json;
  179. }
  180. private static UniWebViewEmbeddedToolbarConfig CloneConfig(UniWebViewEmbeddedToolbarConfig config) {
  181. if (config == null) {
  182. return UniWebViewEmbeddedToolbarConfig.Default;
  183. }
  184. var copy = UniWebViewEmbeddedToolbarConfig.FromJson(config.ToJson());
  185. return copy ?? UniWebViewEmbeddedToolbarConfig.Default;
  186. }
  187. private void SetBuiltInTitle(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind kind, string title) {
  188. var item = currentConfig.FindFirstBuiltInItem(kind);
  189. if (item == null) {
  190. return;
  191. }
  192. item.Title = title;
  193. }
  194. private void SetBuiltInVisible(UniWebViewEmbeddedToolbarConfig.BuiltInItemKind kind, bool visible) {
  195. var item = currentConfig.FindFirstBuiltInItem(kind);
  196. if (item == null) {
  197. return;
  198. }
  199. item.Visible = visible;
  200. }
  201. }