KeyboardManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // Copyright (c) 2024 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. // Only define BaseWebView.cs on supported platforms to avoid IL2CPP linking
  15. // errors on unsupported platforms.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using UnityEngine;
  20. namespace Vuplex.WebView.Internal {
  21. /// <summary>
  22. /// Internal class that implements the KeyboardEnabled setting for WebViewPrefab and CanvasWebViewPrefab.
  23. /// </summary>
  24. class KeyboardManager : MonoBehaviour {
  25. public static KeyboardManager Instance {
  26. get {
  27. // Don't recreate the instance if it's already been destroyed due to the app closing, otherwise it will cause Unity to
  28. // log the error "Some objects were not cleaned up when closing the scene" when stopping the player in the editor.
  29. if (_instance == null && !_destroyed) {
  30. _instance = new GameObject("WebView Keyboard Manager").AddComponent<KeyboardManager>();
  31. DontDestroyOnLoad(_instance.gameObject);
  32. }
  33. return _instance;
  34. }
  35. }
  36. public void AddKeyboard(BaseKeyboard keyboard) {
  37. _keyboards.Add(keyboard);
  38. keyboard.KeyPressed += OnScreenKeyboard_KeyPressed;
  39. keyboard.BaseWebViewPrefab.PointerEntered += OnScreenKeyboard_PointerEntered;
  40. keyboard.BaseWebViewPrefab.PointerExited += OnScreenKeyboard_PointerExited;
  41. }
  42. public void RemoveKeyboard(BaseKeyboard keyboard) {
  43. if (!_keyboards.Contains(keyboard)) {
  44. return;
  45. }
  46. _keyboards.Remove(keyboard);
  47. keyboard.KeyPressed -= OnScreenKeyboard_KeyPressed;
  48. keyboard.BaseWebViewPrefab.PointerEntered -= OnScreenKeyboard_PointerEntered;
  49. keyboard.BaseWebViewPrefab.PointerExited -= OnScreenKeyboard_PointerExited;
  50. }
  51. public void SetKeyboardEnabled(BaseWebViewPrefab webViewPrefab, bool enabled) {
  52. if (enabled) {
  53. _addWebViewPrefab(webViewPrefab);
  54. } else {
  55. _removeWebViewPrefab(webViewPrefab);
  56. }
  57. }
  58. static bool _destroyed;
  59. BaseWebViewPrefab _focusedWebViewPrefab;
  60. BaseWebViewPrefab _hoveredWebViewPrefab;
  61. static KeyboardManager _instance;
  62. HashSet<BaseKeyboard> _keyboards = new HashSet<BaseKeyboard>();
  63. NativeKeyboardListener _nativeKeyboardListener;
  64. bool _pointerIsHoveringOverKeyboard;
  65. HashSet<BaseWebViewPrefab> _webViewPrefabs = new HashSet<BaseWebViewPrefab>();
  66. void Awake() {
  67. _nativeKeyboardListener = NativeKeyboardListener.Instantiate();
  68. _nativeKeyboardListener.transform.parent = transform;
  69. _nativeKeyboardListener.ImeCompositionCancelled += NativeKeyboardListener_ImeCompositionCancelled;
  70. _nativeKeyboardListener.ImeCompositionChanged += NativeKeyboardListener_ImeCompositionChanged;
  71. _nativeKeyboardListener.ImeCompositionFinished += NativeKeyboardListener_ImeCompositionFinished;
  72. _nativeKeyboardListener.KeyDownReceived += NativeKeyboardListener_KeyDownReceived;
  73. _nativeKeyboardListener.KeyUpReceived += NativeKeyboardListener_KeyUpReceived;
  74. }
  75. void OnDestroy() => _destroyed = true;
  76. void WebViewPrefab_Clicked(object sender, ClickedEventArgs eventArgs) {
  77. var webViewPrefab = (BaseWebViewPrefab)sender;
  78. _setFocusedWebViewPrefab(webViewPrefab);
  79. // Also set _hoveredWebViewPrefab here in case the input module doesn't support PointerEntered.
  80. _hoveredWebViewPrefab = webViewPrefab;
  81. }
  82. void _addWebViewPrefab(BaseWebViewPrefab webViewPrefab) {
  83. _webViewPrefabs.Add(webViewPrefab);
  84. // Automatically focus the new prefab.
  85. _setFocusedWebViewPrefab(webViewPrefab);
  86. webViewPrefab.Clicked += WebViewPrefab_Clicked;
  87. webViewPrefab.PointerEntered += WebViewPrefab_PointerEntered;
  88. webViewPrefab.PointerExited += WebViewPrefab_PointerExited;
  89. // Note: BaseWebViewPrefab.cs intentionally calls KeyboardManager.SetKeyboardEnabled() after BaseWebViewPrefab.WebView
  90. // is set but before raising BaseWebViewPrefab.Initialized so that this method can set the FocusChanged handler before
  91. // the application has the chance to call SetFocused().
  92. webViewPrefab.WebView.FocusChanged += WebView_FocusChanged;
  93. var webViewWithIme = webViewPrefab.WebView as IWithIme;
  94. if (webViewWithIme != null) {
  95. webViewWithIme.ImeInputFieldPositionChanged += WebView_ImeInputFieldPositionChanged;
  96. }
  97. }
  98. void NativeKeyboardListener_ImeCompositionCancelled(object sender, EventArgs eventArgs) {
  99. var webViewWithIme = _focusedWebViewPrefab?.WebView as IWithIme;
  100. if (webViewWithIme != null) {
  101. webViewWithIme.CancelImeComposition();
  102. }
  103. }
  104. void NativeKeyboardListener_ImeCompositionChanged(object sender, EventArgs<string> eventArgs) {
  105. var webViewWithIme = _focusedWebViewPrefab?.WebView as IWithIme;
  106. if (webViewWithIme != null) {
  107. webViewWithIme.SetImeComposition(eventArgs.Value);
  108. }
  109. }
  110. void NativeKeyboardListener_ImeCompositionFinished(object sender, EventArgs<string> eventArgs) {
  111. var webViewWithIme = _focusedWebViewPrefab?.WebView as IWithIme;
  112. if (webViewWithIme != null) {
  113. webViewWithIme.FinishImeComposition(eventArgs.Value);
  114. }
  115. }
  116. void NativeKeyboardListener_KeyDownReceived(object sender, KeyboardEventArgs eventArgs) {
  117. var webViewWithKeyDown = _focusedWebViewPrefab?.WebView as IWithKeyDownAndUp;
  118. if (webViewWithKeyDown != null) {
  119. webViewWithKeyDown.KeyDown(eventArgs.Key, eventArgs.Modifiers);
  120. } else {
  121. _focusedWebViewPrefab?.WebView?.SendKey(eventArgs.Key);
  122. }
  123. }
  124. void NativeKeyboardListener_KeyUpReceived(object sender, KeyboardEventArgs eventArgs) {
  125. var webViewWithKeyUp = _focusedWebViewPrefab?.WebView as IWithKeyDownAndUp;
  126. webViewWithKeyUp?.KeyUp(eventArgs.Key, eventArgs.Modifiers);
  127. }
  128. void OnScreenKeyboard_KeyPressed(object sender, EventArgs<string> eventArgs) {
  129. _focusedWebViewPrefab?.WebView?.SendKey(eventArgs.Value);
  130. }
  131. void OnScreenKeyboard_PointerEntered(object sender, EventArgs eventArgs) => _pointerIsHoveringOverKeyboard = true;
  132. void OnScreenKeyboard_PointerExited(object sender, EventArgs eventArgs) => _pointerIsHoveringOverKeyboard = false;
  133. void Update() {
  134. var mouseDown = false;
  135. #if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
  136. mouseDown = Input.GetMouseButtonDown(0);
  137. #endif
  138. if (_hoveredWebViewPrefab == null && !_pointerIsHoveringOverKeyboard && mouseDown) {
  139. // Some area outside of a keyboard-enabled webview or Keyboard was clicked, so unfocus the
  140. // webview in case the object clicked was a Unity Input Field.
  141. _setFocusedWebViewPrefab(null);
  142. }
  143. if (_focusedWebViewPrefab != null && !_focusedWebViewPrefab.gameObject.activeInHierarchy) {
  144. // The focused WebViewPrefab was deactivated, so unfocus it so that we don't continue sending
  145. // keys to the webview while it's invisible.
  146. _setFocusedWebViewPrefab(null);
  147. }
  148. }
  149. void _removeWebViewPrefab(BaseWebViewPrefab webViewPrefab) {
  150. if (!_webViewPrefabs.Contains(webViewPrefab)) {
  151. return;
  152. }
  153. _webViewPrefabs.Remove(webViewPrefab);
  154. if (_focusedWebViewPrefab == webViewPrefab) {
  155. _focusedWebViewPrefab = null;
  156. }
  157. webViewPrefab.Clicked -= WebViewPrefab_Clicked;
  158. webViewPrefab.PointerEntered -= WebViewPrefab_PointerEntered;
  159. webViewPrefab.PointerExited -= WebViewPrefab_PointerExited;
  160. if (webViewPrefab.WebView != null) {
  161. webViewPrefab.WebView.FocusChanged -= WebView_FocusChanged;
  162. }
  163. var webViewWithIme = webViewPrefab.WebView as IWithIme;
  164. if (webViewWithIme != null) {
  165. webViewWithIme.ImeInputFieldPositionChanged -= WebView_ImeInputFieldPositionChanged;
  166. }
  167. }
  168. void _setFocusedWebViewPrefab(BaseWebViewPrefab webViewPrefab) {
  169. var previouslyFocusedPrefab = _focusedWebViewPrefab;
  170. _focusedWebViewPrefab = webViewPrefab;
  171. if (previouslyFocusedPrefab != null && previouslyFocusedPrefab != webViewPrefab) {
  172. // Unfocus the previous webview.
  173. previouslyFocusedPrefab.WebView?.SetFocused(false);
  174. }
  175. }
  176. void WebView_FocusChanged(object sender, EventArgs<bool> eventArgs) {
  177. var prefab = _webViewPrefabs.ToList().Find(p => p.WebView == sender);
  178. if (prefab == null) {
  179. return;
  180. }
  181. var focused = eventArgs.Value;
  182. if (focused) {
  183. _setFocusedWebViewPrefab(prefab);
  184. } else if (prefab == _focusedWebViewPrefab) {
  185. _setFocusedWebViewPrefab(null);
  186. }
  187. }
  188. void WebView_ImeInputFieldPositionChanged(object sender, EventArgs<Vector2Int> eventArgs) {
  189. var prefab = _webViewPrefabs.ToList().Find(p => p.WebView == sender);
  190. if (prefab != null && prefab == _focusedWebViewPrefab) {
  191. #if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
  192. var screenPoint = prefab.BrowserToScreenPoint(eventArgs.Value.x, eventArgs.Value.y);
  193. var screenPointY = screenPoint.y;
  194. switch (Application.platform) {
  195. case RuntimePlatform.WindowsEditor:
  196. // For some reason, in the Windows editor, Input.compositionCursorPos doesn't work correctly
  197. // and the IME popup window is positioned higher than the specified point.
  198. // So, add an extra Y offset when running in the Windows editor.
  199. screenPointY += 60;
  200. break;
  201. case RuntimePlatform.OSXPlayer:
  202. case RuntimePlatform.OSXEditor:
  203. // Unity has a bug on macOS where the Y axis for Input.compositionCursorPos is incorrectly flipped.
  204. screenPointY = Screen.height - screenPointY;
  205. break;
  206. }
  207. Input.compositionCursorPos = new Vector2(screenPoint.x, screenPointY);
  208. #endif
  209. }
  210. }
  211. void WebViewPrefab_PointerEntered(object sender, EventArgs eventArgs) {
  212. _hoveredWebViewPrefab = (BaseWebViewPrefab)sender;
  213. }
  214. void WebViewPrefab_PointerExited(object sender, EventArgs eventArgs) {
  215. var webViewPrefab = (BaseWebViewPrefab)sender;
  216. if (_hoveredWebViewPrefab == webViewPrefab) {
  217. _hoveredWebViewPrefab = null;
  218. }
  219. }
  220. }
  221. }