CEFInterfaceTextInputMethodContext.cpp
7.4 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Engine/Source/Runtime/WebBrowser/Private/CEF/CEFTextInputMethodContext.cpp
#include "CEF/CEFInterfaceTextInputMethodContext.h"
#if WITH_CEF3 && !PLATFORM_LINUX
#include "CEF/CEFWebInterfaceBrowserWindow.h"
#include "CEFInterfaceImeHandler.h"
#include "Framework/Application/SlateApplication.h"
TSharedRef<FCEFInterfaceTextInputMethodContext> FCEFInterfaceTextInputMethodContext::Create(const TSharedRef<FCEFInterfaceImeHandler>& InOwner)
{
return MakeShareable(new FCEFInterfaceTextInputMethodContext(InOwner));
}
FCEFInterfaceTextInputMethodContext::FCEFInterfaceTextInputMethodContext(const TSharedRef<FCEFInterfaceImeHandler>& InOwner)
: Owner(InOwner)
, bIsComposing(false)
, CompositionBeginIndex(0)
, CompositionLength(0)
, SelectionRangeBeginIndex(0)
, SelectionRangeLength(0)
, SelectionCaretPosition(ECaretPosition::Ending)
{
}
void FCEFInterfaceTextInputMethodContext::AbortComposition()
{
bIsComposing = false;
Owner->InternalCefBrowser->GetHost()->ImeCancelComposition();
ResetComposition();
}
bool FCEFInterfaceTextInputMethodContext::UpdateCachedGeometry(const FGeometry& AllottedGeometry)
{
bool bCachedGeometryUpdated = false;
if (CachedGeometry != AllottedGeometry)
{
CachedGeometry = AllottedGeometry;
bCachedGeometryUpdated = true;
}
return bCachedGeometryUpdated;
}
bool FCEFInterfaceTextInputMethodContext::CEFCompositionRangeChanged(const CefRange& SelectionRange, const CefRenderHandler::RectList& CharacterBounds)
{
if (bIsComposing)
{
if (CharacterBounds != CefCompositionBounds)
{
CefCompositionBounds = CharacterBounds;
return true;
}
}
return false;
}
bool FCEFInterfaceTextInputMethodContext::IsComposing()
{
return bIsComposing;
}
bool FCEFInterfaceTextInputMethodContext::IsReadOnly()
{
return false;
}
uint32 FCEFInterfaceTextInputMethodContext::GetTextLength()
{
return CompositionString.Len();
}
void FCEFInterfaceTextInputMethodContext::GetSelectionRange(uint32& BeginIndex, uint32& Length, ECaretPosition& CaretPosition)
{
BeginIndex = SelectionRangeBeginIndex;
Length = SelectionRangeLength;
CaretPosition = SelectionCaretPosition;
}
void FCEFInterfaceTextInputMethodContext::SetSelectionRange(const uint32 BeginIndex, const uint32 Length, const ECaretPosition CaretPosition)
{
SelectionRangeBeginIndex = BeginIndex;
SelectionRangeLength = Length;
SelectionCaretPosition = CaretPosition;
CefString Str = TCHAR_TO_WCHAR(*CompositionString);
std::vector<CefCompositionUnderline> underlines;
Owner->InternalCefBrowser->GetHost()->ImeSetComposition(
Str,
underlines,
CefRange(UINT32_MAX, UINT32_MAX),
CefRange(SelectionRangeBeginIndex, SelectionRangeLength));
}
void FCEFInterfaceTextInputMethodContext::GetTextInRange(const uint32 BeginIndex, const uint32 Length, FString& OutString)
{
OutString = CompositionString.Mid(BeginIndex, Length);
}
void FCEFInterfaceTextInputMethodContext::SetTextInRange(const uint32 BeginIndex, const uint32 Length, const FString& InString)
{
FString NewString;
if (BeginIndex > 0)
{
NewString = CompositionString.Mid(0, BeginIndex);
}
NewString += InString;
if ((int32)(BeginIndex + Length) < CompositionString.Len())
{
NewString += CompositionString.Mid(BeginIndex + Length, CompositionString.Len() - (BeginIndex + Length));
}
CompositionString = NewString;
CefString Str = TCHAR_TO_WCHAR(*CompositionString);
std::vector<CefCompositionUnderline> underlines;
Owner->InternalCefBrowser->GetHost()->ImeSetComposition(
Str,
underlines,
CefRange(UINT32_MAX, UINT32_MAX),
CefRange(0, Str.length()));
}
int32 FCEFInterfaceTextInputMethodContext::GetCharacterIndexFromPoint(const FVector2D& Point)
{
int32 ResultIdx = INDEX_NONE;
const FVector2D LocalPoint = CachedGeometry.AbsoluteToLocal(Point);
CefPoint CefLocalPoint = CefPoint(FMath::RoundToInt(LocalPoint.X), FMath::RoundToInt(LocalPoint.Y));
for (uint32 CharIdx = 0; CharIdx < CefCompositionBounds.size(); CharIdx++)
{
if (CefCompositionBounds[CharIdx].Contains(CefLocalPoint))
{
ResultIdx = CharIdx;
break;
}
}
return ResultIdx;
}
bool FCEFInterfaceTextInputMethodContext::GetTextBounds(const uint32 BeginIndex, const uint32 Length, FVector2D& Position, FVector2D& Size)
{
if (CefCompositionBounds.size() < BeginIndex ||
CefCompositionBounds.size() < BeginIndex + Length)
{
if (CefCompositionBounds.size() > 0)
{
// Fall back to the start of the composition
Position = CachedGeometry.LocalToAbsolute(FVector2D(CefCompositionBounds[0].x, CefCompositionBounds[0].y));
Size = FVector2D(CefCompositionBounds[0].width, CefCompositionBounds[0].height);
return false;
}
else
{
// We don't have any updated composition bounds so we'll just default to the window bounds and say we are clipped.
GetScreenBounds(Position, Size);
return true;
}
}
FVector2D LocalSpaceMin(FLT_MAX, FLT_MAX);
FVector2D LocalSpaceMax(-FLT_MAX, -FLT_MAX);
for (uint32 CharIdx = BeginIndex; CharIdx < BeginIndex + Length; CharIdx++)
{
if (LocalSpaceMin.X > CefCompositionBounds[CharIdx].x)
{
LocalSpaceMin.X = CefCompositionBounds[CharIdx].x;
}
if (LocalSpaceMax.X < CefCompositionBounds[CharIdx].x + CefCompositionBounds[CharIdx].width)
{
LocalSpaceMax.X = CefCompositionBounds[CharIdx].x + CefCompositionBounds[CharIdx].width;
}
if (LocalSpaceMin.Y > CefCompositionBounds[CharIdx].y)
{
LocalSpaceMin.Y = CefCompositionBounds[CharIdx].y;
}
if (LocalSpaceMax.Y < CefCompositionBounds[CharIdx].y + CefCompositionBounds[CharIdx].height)
{
LocalSpaceMax.Y = CefCompositionBounds[CharIdx].y + CefCompositionBounds[CharIdx].height;
}
}
Position = CachedGeometry.LocalToAbsolute(LocalSpaceMin);
Size = LocalSpaceMax - LocalSpaceMin;
return false; // false means "not clipped"
}
void FCEFInterfaceTextInputMethodContext::GetScreenBounds(FVector2D& Position, FVector2D& Size)
{
Position = CachedGeometry.GetAccumulatedRenderTransform().GetTranslation();
Size = TransformVector(CachedGeometry.GetAccumulatedRenderTransform(), CachedGeometry.GetLocalSize());
}
TSharedPtr<FGenericWindow> FCEFInterfaceTextInputMethodContext::GetWindow()
{
if (CachedSlateWindow.IsValid())
{
return CachedSlateWindow.Pin()->GetNativeWindow();
}
const TSharedPtr<SWidget> CachedSlateWidgetPtr = Owner->InternalBrowserSlateWidget.Pin();
if (!CachedSlateWidgetPtr.IsValid())
{
return nullptr;
}
TSharedPtr<SWindow> SlateWindow = FSlateApplication::Get().FindWidgetWindow(CachedSlateWidgetPtr.ToSharedRef());
CachedSlateWindow = SlateWindow;
return SlateWindow.IsValid() ? SlateWindow->GetNativeWindow() : nullptr;
}
void FCEFInterfaceTextInputMethodContext::BeginComposition()
{
if (!bIsComposing)
{
bIsComposing = true;
}
}
void FCEFInterfaceTextInputMethodContext::UpdateCompositionRange(const int32 InBeginIndex, const uint32 InLength)
{
CompositionBeginIndex = InBeginIndex;
CompositionLength = InLength;
}
void FCEFInterfaceTextInputMethodContext::EndComposition()
{
if (bIsComposing)
{
bIsComposing = false;
if (CompositionString.Len() > 0)
{
CefString Result = TCHAR_TO_WCHAR(*CompositionString);
Owner->InternalCefBrowser->GetHost()->ImeCommitText(Result, CefRange(UINT32_MAX, UINT32_MAX), 0);
}
else
{
Owner->InternalCefBrowser->GetHost()->ImeCancelComposition();
}
ResetComposition();
}
}
void FCEFInterfaceTextInputMethodContext::ResetComposition()
{
CompositionString.Empty();
CefCompositionBounds.clear();
CompositionBeginIndex = 0;
CompositionLength = 0;
SelectionRangeBeginIndex = 0;
SelectionRangeLength = 0;
}
#endif