CEFInterfaceImeHandler.cpp
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Engine/Source/Runtime/WebBrowser/Private/CEF/CEFImeHandler.cpp
#include "CEF/CEFInterfaceImeHandler.h"
#if WITH_CEF3 && !PLATFORM_LINUX
#include "CEFInterfaceTextInputMethodContext.h"
FCEFInterfaceImeHandler::FCEFInterfaceImeHandler(CefRefPtr<CefBrowser> Browser)
: InternalCefBrowser(Browser)
, TextInputMethodSystem(nullptr)
{
}
bool FCEFInterfaceImeHandler::OnProcessMessageReceived(CefRefPtr<CefBrowser> Browser, CefProcessId SourceProcess, CefRefPtr<CefProcessMessage> Message)
{
bool Result = false;
FString MessageName = WCHAR_TO_TCHAR(Message->GetName().ToWString().c_str());
if (MessageName == TEXT("UE::IME::FocusChanged"))
{
Result = HandleFocusChangedMessage(Message->GetArgumentList());
}
return Result;
}
void FCEFInterfaceImeHandler::SendProcessMessage(CefRefPtr<CefProcessMessage> Message)
{
if (IsValid() )
{
InternalCefBrowser->SendProcessMessage(PID_RENDERER, Message);
}
}
void FCEFInterfaceImeHandler::BindInputMethodSystem(ITextInputMethodSystem* InTextInputMethodSystem)
{
TextInputMethodSystem = InTextInputMethodSystem;
if (TextInputMethodSystem && TextInputMethodContext.IsValid())
{
TextInputMethodChangeNotifier = TextInputMethodSystem->RegisterContext(TextInputMethodContext.ToSharedRef());
}
}
void FCEFInterfaceImeHandler::UnbindInputMethodSystem()
{
if (TextInputMethodContext.IsValid())
{
DestroyContext();
}
TextInputMethodSystem = nullptr;
}
void FCEFInterfaceImeHandler::InitContext()
{
if (!TextInputMethodSystem)
{
return;
}
// Clean up any existing context
DestroyContext();
TextInputMethodContext = FCEFInterfaceTextInputMethodContext::Create(SharedThis(this));
if (TextInputMethodSystem)
{
TextInputMethodChangeNotifier = TextInputMethodSystem->RegisterContext(TextInputMethodContext.ToSharedRef());
}
if (TextInputMethodChangeNotifier.IsValid())
{
TextInputMethodChangeNotifier->NotifyLayoutChanged(ITextInputMethodChangeNotifier::ELayoutChangeType::Created);
}
TextInputMethodSystem->ActivateContext(TextInputMethodContext.ToSharedRef());
}
void FCEFInterfaceImeHandler::DeactivateContext()
{
if (!TextInputMethodSystem || !TextInputMethodContext.IsValid())
{
return;
}
TSharedRef<FCEFInterfaceTextInputMethodContext> TextInputMethodContextRef = TextInputMethodContext.ToSharedRef();
if (TextInputMethodSystem->IsActiveContext(TextInputMethodContextRef))
{
// Make sure that the composition is aborted to avoid any IME calls to EndComposition from trying to mutate our dying owner widget
if (TextInputMethodContextRef->IsComposing())
{
TextInputMethodContextRef->AbortComposition();
if (TextInputMethodChangeNotifier.IsValid())
{
TextInputMethodChangeNotifier->CancelComposition();
}
}
TextInputMethodSystem->DeactivateContext(TextInputMethodContextRef);
}
}
void FCEFInterfaceImeHandler::DestroyContext()
{
if (!TextInputMethodContext.IsValid())
{
return;
}
if (TextInputMethodSystem)
{
DeactivateContext();
TextInputMethodSystem->UnregisterContext(TextInputMethodContext.ToSharedRef());
}
TextInputMethodChangeNotifier.Reset();
TextInputMethodContext.Reset();
}
bool FCEFInterfaceImeHandler::HandleFocusChangedMessage(CefRefPtr<CefListValue> MessageArguments)
{
if (MessageArguments->GetSize() == 1 &&
MessageArguments->GetType(0) == VTYPE_STRING)
{
if (TextInputMethodContext.IsValid())
{
DestroyContext();
}
return true;
}
else if (MessageArguments->GetSize() == 8 &&
MessageArguments->GetType(0) == VTYPE_STRING &&
MessageArguments->GetType(1) == VTYPE_STRING &&
MessageArguments->GetType(2) == VTYPE_BOOL &&
MessageArguments->GetType(3) == VTYPE_STRING &&
MessageArguments->GetType(4) == VTYPE_INT &&
MessageArguments->GetType(5) == VTYPE_INT &&
MessageArguments->GetType(6) == VTYPE_INT &&
MessageArguments->GetType(7) == VTYPE_INT)
{
FString Type = WCHAR_TO_TCHAR(MessageArguments->GetString(0).ToWString().c_str());
FString Name = WCHAR_TO_TCHAR(MessageArguments->GetString(1).ToWString().c_str());
bool bIsEditable = MessageArguments->GetBool(2);
if (Type == TEXT("DOM_NODE_TYPE_ELEMENT") &&
(Name == TEXT("INPUT") || Name == TEXT("TEXTAREA")) &&
bIsEditable)
{
// @todo: Make use of the bounds of the text box here to act as a fallback for the initial composition window pos.
InitContext();
}
return true;
}
return false;
}
void FCEFInterfaceImeHandler::UnbindCefBrowser()
{
if (TextInputMethodContext.IsValid())
{
DestroyContext();
}
InternalCefBrowser = nullptr;
}
void FCEFInterfaceImeHandler::CacheBrowserSlateInfo(const TSharedRef<SWidget>& Widget)
{
InternalBrowserSlateWidget = Widget;
}
void FCEFInterfaceImeHandler::SetFocus(bool bHasFocus)
{
if (!TextInputMethodSystem || !TextInputMethodContext.IsValid())
{
return;
}
if (bHasFocus)
{
TextInputMethodSystem->ActivateContext(TextInputMethodContext.ToSharedRef());
}
else
{
DeactivateContext();
}
}
void FCEFInterfaceImeHandler::UpdateCachedGeometry(const FGeometry& AllottedGeometry)
{
if (TextInputMethodContext.IsValid() &&
TextInputMethodContext->UpdateCachedGeometry(AllottedGeometry))
{
if (TextInputMethodChangeNotifier.IsValid())
{
TextInputMethodChangeNotifier->NotifyLayoutChanged(ITextInputMethodChangeNotifier::ELayoutChangeType::Changed);
}
}
}
void FCEFInterfaceImeHandler::CEFCompositionRangeChanged(const CefRange& SelectionRange, const CefRenderHandler::RectList& CharacterBounds)
{
if (TextInputMethodContext.IsValid() &&
TextInputMethodContext->CEFCompositionRangeChanged(SelectionRange, CharacterBounds))
{
if (TextInputMethodChangeNotifier.IsValid())
{
TextInputMethodChangeNotifier->NotifyLayoutChanged(ITextInputMethodChangeNotifier::ELayoutChangeType::Changed);
}
}
}
#endif