BaseKeyboard.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using UnityEngine;
  18. namespace Vuplex.WebView {
  19. public abstract class BaseKeyboard : MonoBehaviour {
  20. /// <summary>
  21. /// Indicates that the user clicked a key on the keyboard.
  22. /// </summary>
  23. public event EventHandler<EventArgs<string>> InputReceived;
  24. /// <summary>
  25. /// Indicates that the keyboard finished initializing.
  26. /// </summary>
  27. public event EventHandler Initialized;
  28. [SerializeField]
  29. [HideInInspector]
  30. protected BaseWebViewPrefab _webViewPrefab;
  31. protected static readonly WebViewOptions _webViewOptions = new WebViewOptions {
  32. clickWithoutStealingFocus = true,
  33. disableVideo = true,
  34. // If both Android plugins are installed, prefer the original Chromium
  35. // plugin for the keyboard, since the Gecko plugin doesn't support
  36. // transparent backgrounds.
  37. preferredPlugins = new WebPluginType[] { WebPluginType.Android }
  38. };
  39. protected void _init() {
  40. _webViewPrefab.Initialized += (sender, e) => {
  41. var pluginType = _webViewPrefab.WebView.PluginType;
  42. if (pluginType == WebPluginType.AndroidGecko) {
  43. // On Android Gecko, hovering steals focus.
  44. _webViewPrefab.HoveringEnabled = false;
  45. }
  46. _webViewPrefab.WebView.MessageEmitted += WebView_MessageEmitted;
  47. // Android Gecko and Hololens don't support transparent webviews, so set the cutout
  48. // rect to the entire view so that the shader makes its black background
  49. // pixels transparent.
  50. if (pluginType == WebPluginType.AndroidGecko || pluginType == WebPluginType.UniversalWindowsPlatform) {
  51. _webViewPrefab.SetCutoutRect(new Rect(0, 0, 1, 1));
  52. }
  53. _webViewPrefab.WebView.LoadHtml(KeyboardUi.Html);
  54. };
  55. }
  56. void WebView_MessageEmitted(object sender, EventArgs<string> e) {
  57. var serializedMessage = e.Value;
  58. var messageType = JsonUtility.FromJson<BridgeMessage>(serializedMessage).type;
  59. switch (messageType) {
  60. case "keyboard.inputReceived":
  61. var input = StringBridgeMessage.ParseValue(serializedMessage);
  62. if (InputReceived != null) {
  63. InputReceived(this, new EventArgs<string>(input));
  64. }
  65. break;
  66. case "keyboard.initialized":
  67. _sendKeyboardLanguageMessage();
  68. if (Initialized != null) {
  69. Initialized(this, EventArgs.Empty);
  70. }
  71. break;
  72. }
  73. }
  74. string _getKeyboardLanguage() {
  75. switch (Application.systemLanguage) {
  76. case SystemLanguage.Danish:
  77. return "da";
  78. case SystemLanguage.French:
  79. return "fr";
  80. case SystemLanguage.German:
  81. return "de";
  82. case SystemLanguage.Norwegian:
  83. return "no";
  84. case SystemLanguage.Russian:
  85. return "ru";
  86. case SystemLanguage.Spanish:
  87. return "es";
  88. case SystemLanguage.Swedish:
  89. return "sv";
  90. default:
  91. return "en";
  92. }
  93. }
  94. /// <summary>
  95. /// Initializes the keyboard language based on the system language.
  96. /// </summary>
  97. void _sendKeyboardLanguageMessage() {
  98. var message = new StringBridgeMessage {
  99. type = "keyboard.setLanguage",
  100. value = _getKeyboardLanguage()
  101. };
  102. var serializedMessage = JsonUtility.ToJson(message);
  103. _webViewPrefab.WebView.PostMessage(serializedMessage);
  104. }
  105. }
  106. }