Skip to content

Commit d7c8503

Browse files
committed
Initial IME support
1 parent 61bdbf2 commit d7c8503

File tree

12 files changed

+116
-2
lines changed

12 files changed

+116
-2
lines changed

src/Packages/UnityWebBrowser/Runtime/Core/RawImageUwbClientInputHandler.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ protected override void OnStart()
9393
base.OnStart();
9494
if (inputHandler == null)
9595
throw new NullReferenceException("The input handler is null! You need to assign it in the editor!");
96+
97+
browserClient.OnInputFocus += OnClientInput;
98+
}
99+
100+
private void OnClientInput(bool focused)
101+
{
102+
if (focused)
103+
{
104+
if (!GetMousePosition(out Vector2 pos)) return;
105+
pos.x /= 1.5f;
106+
inputHandler.EnableIme(pos);
107+
return;
108+
}
109+
110+
inputHandler.DisableIme();
96111
}
97112

98113
protected override void OnDestroyed()

src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,13 @@ internal void InvokeOnPopup(WebBrowserPopupInfo popupInfo)
628628
OnPopup?.Invoke(popupInfo);
629629
}
630630

631+
public event OnInputFocus OnInputFocus;
632+
633+
internal void InvokeOnInputFocus(bool focused)
634+
{
635+
OnInputFocus?.Invoke(focused);
636+
}
637+
631638
#endregion
632639

633640
#region Browser Controls

src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserCommunicationsManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ public void Fullscreen(bool fullScreen)
254254
ExecuteOnUnity(() => client.InvokeFullscreen(fullScreen));
255255
}
256256

257+
public void InputFocusChange(bool focused)
258+
{
259+
ExecuteOnUnity(() => client.InvokeOnInputFocus(focused));
260+
}
261+
257262
public void Ready()
258263
{
259264
client.EngineReady().Forget();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// UnityWebBrowser (UWB)
2+
// Copyright (c) 2021-2023 Voltstro-Studios
3+
//
4+
// This project is under the MIT license. See the LICENSE.md file for more details.
5+
6+
namespace VoltstroStudios.UnityWebBrowser.Events
7+
{
8+
public delegate void OnInputFocus(bool focused);
9+
}

src/Packages/UnityWebBrowser/Runtime/Events/OnInputFocus.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Packages/UnityWebBrowser/Runtime/Input/WebBrowserInputHandler.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ public abstract class WebBrowserInputHandler : ScriptableObject
6363
/// </summary>
6464
public abstract void OnStop();
6565

66+
/// <summary>
67+
/// Called when IME needs to be enabled
68+
/// </summary>
69+
public abstract void EnableIme(Vector2 location);
70+
71+
/// <summary>
72+
/// Called when IME is no longer needed
73+
/// </summary>
74+
public abstract void DisableIme();
75+
6676
#endregion
6777
}
6878
}

src/Packages/UnityWebBrowser/Runtime/Input/WebBrowserInputSystemHandler.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public sealed class WebBrowserInputSystemHandler : WebBrowserInputHandler
3131
private string inputBuffer = string.Empty;
3232

3333
private Keyboard keyboard;
34+
private IMECompositionMode compositionMode;
3435

3536
public override float GetScroll()
3637
{
@@ -124,6 +125,33 @@ public override void OnStop()
124125
scrollInput.Disable();
125126
pointPosition.Disable();
126127
}
128+
129+
public override void EnableIme(Vector2 location)
130+
{
131+
//Appears we still have to set UnityEngine.Input.imeCompositionMode?
132+
compositionMode = UnityEngine.Input.imeCompositionMode;
133+
UnityEngine.Input.imeCompositionMode = IMECompositionMode.On;
134+
135+
keyboard.SetIMEEnabled(true);
136+
keyboard.SetIMECursorPosition(location);
137+
}
138+
139+
public override void DisableIme()
140+
{
141+
UnityEngine.Input.imeCompositionMode = compositionMode;
142+
switch (compositionMode)
143+
{
144+
case IMECompositionMode.Auto:
145+
case IMECompositionMode.On:
146+
keyboard.SetIMEEnabled(true);
147+
break;
148+
case IMECompositionMode.Off:
149+
keyboard.SetIMEEnabled(false);
150+
break;
151+
default:
152+
throw new ArgumentOutOfRangeException();
153+
}
154+
}
127155
}
128156
}
129157

src/Packages/UnityWebBrowser/Runtime/Input/WebBrowserOldInputHandler.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public sealed class WebBrowserOldInputHandler : WebBrowserInputHandler
3333
private readonly List<WindowsKey> keysDown = new();
3434
private readonly List<WindowsKey> keysUp = new();
3535

36+
private IMECompositionMode compositionMode;
37+
3638
public override float GetScroll()
3739
{
3840
return UnityEngine.Input.GetAxis(scrollAxisName) * scrollSensitivity;
@@ -107,5 +109,17 @@ public override void OnStart()
107109
public override void OnStop()
108110
{
109111
}
112+
113+
public override void EnableIme(Vector2 location)
114+
{
115+
compositionMode = UnityEngine.Input.imeCompositionMode;
116+
UnityEngine.Input.imeCompositionMode = IMECompositionMode.On;
117+
UnityEngine.Input.compositionCursorPos = location;
118+
}
119+
120+
public override void DisableIme()
121+
{
122+
UnityEngine.Input.imeCompositionMode = compositionMode;
123+
}
110124
}
111125
}

src/UnityWebBrowser.Engine.Cef/Browser/UwbCefClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public UwbCefClient(CefSize size, PopupAction popupAction, EnginePopupManager po
4141

4242
//Setup our handlers
4343
loadHandler = new UwbCefLoadHandler(this);
44-
renderHandler = new UwbCefRenderHandler(size);
44+
renderHandler = new UwbCefRenderHandler(this, size);
4545
lifespanHandler = new UwbCefLifespanHandler(popupAction, popupManager, proxySettings);
4646
lifespanHandler.AfterCreated += cefBrowser =>
4747
{

src/UnityWebBrowser.Engine.Cef/Browser/UwbCefRenderHandler.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Numerics;
88
using System.Runtime.InteropServices;
99
using System.Security;
10+
using UnityWebBrowser.Engine.Cef.Core;
11+
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core;
1012
using Xilium.CefGlue;
1113

1214
namespace UnityWebBrowser.Engine.Cef.Browser;
@@ -19,16 +21,19 @@ public class UwbCefRenderHandler : CefRenderHandler
1921
private readonly object pixelsLock;
2022
private CefSize cefSize;
2123
private byte[] pixels;
24+
25+
private readonly ClientControlsActions clientControls;
2226

2327
/// <summary>
2428
/// Tracked mouse scroll position
2529
/// </summary>
2630
public Vector2 MouseScrollPosition { get; private set; }
2731

28-
public UwbCefRenderHandler(CefSize size)
32+
public UwbCefRenderHandler(UwbCefClient client, CefSize size)
2933
{
3034
pixelsLock = new object();
3135
Resize(size);
36+
clientControls = client.ClientControls;
3237
}
3338

3439
public byte[] Pixels
@@ -114,4 +119,10 @@ protected override void OnImeCompositionRangeChanged(CefBrowser browser, CefRang
114119
CefRectangle[] characterBounds)
115120
{
116121
}
122+
123+
protected override void OnVirtualKeyboardRequested(CefBrowser browser, CefTextInputMode inputMode)
124+
{
125+
CefLoggerWrapper.Debug($"Input mode changed to: {inputMode}");
126+
clientControls.InputFocusChange(inputMode == CefTextInputMode.Default);
127+
}
117128
}

0 commit comments

Comments
 (0)