CEFInterfaceTextInputMethodContext.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Engine/Source/Runtime/WebBrowser/Private/CEF/CEFTextInputMethodContext.cpp
  2. #include "CEF/CEFInterfaceTextInputMethodContext.h"
  3. #if WITH_CEF3 && !PLATFORM_LINUX
  4. #include "CEF/CEFWebInterfaceBrowserWindow.h"
  5. #include "CEFInterfaceImeHandler.h"
  6. #include "Framework/Application/SlateApplication.h"
  7. TSharedRef<FCEFInterfaceTextInputMethodContext> FCEFInterfaceTextInputMethodContext::Create(const TSharedRef<FCEFInterfaceImeHandler>& InOwner)
  8. {
  9. return MakeShareable(new FCEFInterfaceTextInputMethodContext(InOwner));
  10. }
  11. FCEFInterfaceTextInputMethodContext::FCEFInterfaceTextInputMethodContext(const TSharedRef<FCEFInterfaceImeHandler>& InOwner)
  12. : Owner(InOwner)
  13. , bIsComposing(false)
  14. , CompositionBeginIndex(0)
  15. , CompositionLength(0)
  16. , SelectionRangeBeginIndex(0)
  17. , SelectionRangeLength(0)
  18. , SelectionCaretPosition(ECaretPosition::Ending)
  19. {
  20. }
  21. void FCEFInterfaceTextInputMethodContext::AbortComposition()
  22. {
  23. bIsComposing = false;
  24. Owner->InternalCefBrowser->GetHost()->ImeCancelComposition();
  25. ResetComposition();
  26. }
  27. bool FCEFInterfaceTextInputMethodContext::UpdateCachedGeometry(const FGeometry& AllottedGeometry)
  28. {
  29. bool bCachedGeometryUpdated = false;
  30. if (CachedGeometry != AllottedGeometry)
  31. {
  32. CachedGeometry = AllottedGeometry;
  33. bCachedGeometryUpdated = true;
  34. }
  35. return bCachedGeometryUpdated;
  36. }
  37. bool FCEFInterfaceTextInputMethodContext::CEFCompositionRangeChanged(const CefRange& SelectionRange, const CefRenderHandler::RectList& CharacterBounds)
  38. {
  39. if (bIsComposing)
  40. {
  41. if (CharacterBounds != CefCompositionBounds)
  42. {
  43. CefCompositionBounds = CharacterBounds;
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. bool FCEFInterfaceTextInputMethodContext::IsComposing()
  50. {
  51. return bIsComposing;
  52. }
  53. bool FCEFInterfaceTextInputMethodContext::IsReadOnly()
  54. {
  55. return false;
  56. }
  57. uint32 FCEFInterfaceTextInputMethodContext::GetTextLength()
  58. {
  59. return CompositionString.Len();
  60. }
  61. void FCEFInterfaceTextInputMethodContext::GetSelectionRange(uint32& BeginIndex, uint32& Length, ECaretPosition& CaretPosition)
  62. {
  63. BeginIndex = SelectionRangeBeginIndex;
  64. Length = SelectionRangeLength;
  65. CaretPosition = SelectionCaretPosition;
  66. }
  67. void FCEFInterfaceTextInputMethodContext::SetSelectionRange(const uint32 BeginIndex, const uint32 Length, const ECaretPosition CaretPosition)
  68. {
  69. SelectionRangeBeginIndex = BeginIndex;
  70. SelectionRangeLength = Length;
  71. SelectionCaretPosition = CaretPosition;
  72. CefString Str = TCHAR_TO_WCHAR(*CompositionString);
  73. std::vector<CefCompositionUnderline> underlines;
  74. Owner->InternalCefBrowser->GetHost()->ImeSetComposition(
  75. Str,
  76. underlines,
  77. CefRange(UINT32_MAX, UINT32_MAX),
  78. CefRange(SelectionRangeBeginIndex, SelectionRangeLength));
  79. }
  80. void FCEFInterfaceTextInputMethodContext::GetTextInRange(const uint32 BeginIndex, const uint32 Length, FString& OutString)
  81. {
  82. OutString = CompositionString.Mid(BeginIndex, Length);
  83. }
  84. void FCEFInterfaceTextInputMethodContext::SetTextInRange(const uint32 BeginIndex, const uint32 Length, const FString& InString)
  85. {
  86. FString NewString;
  87. if (BeginIndex > 0)
  88. {
  89. NewString = CompositionString.Mid(0, BeginIndex);
  90. }
  91. NewString += InString;
  92. if ((int32)(BeginIndex + Length) < CompositionString.Len())
  93. {
  94. NewString += CompositionString.Mid(BeginIndex + Length, CompositionString.Len() - (BeginIndex + Length));
  95. }
  96. CompositionString = NewString;
  97. CefString Str = TCHAR_TO_WCHAR(*CompositionString);
  98. std::vector<CefCompositionUnderline> underlines;
  99. Owner->InternalCefBrowser->GetHost()->ImeSetComposition(
  100. Str,
  101. underlines,
  102. CefRange(UINT32_MAX, UINT32_MAX),
  103. CefRange(0, Str.length()));
  104. }
  105. int32 FCEFInterfaceTextInputMethodContext::GetCharacterIndexFromPoint(const FVector2D& Point)
  106. {
  107. int32 ResultIdx = INDEX_NONE;
  108. const FVector2D LocalPoint = CachedGeometry.AbsoluteToLocal(Point);
  109. CefPoint CefLocalPoint = CefPoint(FMath::RoundToInt(LocalPoint.X), FMath::RoundToInt(LocalPoint.Y));
  110. for (uint32 CharIdx = 0; CharIdx < CefCompositionBounds.size(); CharIdx++)
  111. {
  112. if (CefCompositionBounds[CharIdx].Contains(CefLocalPoint))
  113. {
  114. ResultIdx = CharIdx;
  115. break;
  116. }
  117. }
  118. return ResultIdx;
  119. }
  120. bool FCEFInterfaceTextInputMethodContext::GetTextBounds(const uint32 BeginIndex, const uint32 Length, FVector2D& Position, FVector2D& Size)
  121. {
  122. if (CefCompositionBounds.size() < BeginIndex ||
  123. CefCompositionBounds.size() < BeginIndex + Length)
  124. {
  125. if (CefCompositionBounds.size() > 0)
  126. {
  127. // Fall back to the start of the composition
  128. Position = CachedGeometry.LocalToAbsolute(FVector2D(CefCompositionBounds[0].x, CefCompositionBounds[0].y));
  129. Size = FVector2D(CefCompositionBounds[0].width, CefCompositionBounds[0].height);
  130. return false;
  131. }
  132. else
  133. {
  134. // We don't have any updated composition bounds so we'll just default to the window bounds and say we are clipped.
  135. GetScreenBounds(Position, Size);
  136. return true;
  137. }
  138. }
  139. FVector2D LocalSpaceMin(FLT_MAX, FLT_MAX);
  140. FVector2D LocalSpaceMax(-FLT_MAX, -FLT_MAX);
  141. for (uint32 CharIdx = BeginIndex; CharIdx < BeginIndex + Length; CharIdx++)
  142. {
  143. if (LocalSpaceMin.X > CefCompositionBounds[CharIdx].x)
  144. {
  145. LocalSpaceMin.X = CefCompositionBounds[CharIdx].x;
  146. }
  147. if (LocalSpaceMax.X < CefCompositionBounds[CharIdx].x + CefCompositionBounds[CharIdx].width)
  148. {
  149. LocalSpaceMax.X = CefCompositionBounds[CharIdx].x + CefCompositionBounds[CharIdx].width;
  150. }
  151. if (LocalSpaceMin.Y > CefCompositionBounds[CharIdx].y)
  152. {
  153. LocalSpaceMin.Y = CefCompositionBounds[CharIdx].y;
  154. }
  155. if (LocalSpaceMax.Y < CefCompositionBounds[CharIdx].y + CefCompositionBounds[CharIdx].height)
  156. {
  157. LocalSpaceMax.Y = CefCompositionBounds[CharIdx].y + CefCompositionBounds[CharIdx].height;
  158. }
  159. }
  160. Position = CachedGeometry.LocalToAbsolute(LocalSpaceMin);
  161. Size = LocalSpaceMax - LocalSpaceMin;
  162. return false; // false means "not clipped"
  163. }
  164. void FCEFInterfaceTextInputMethodContext::GetScreenBounds(FVector2D& Position, FVector2D& Size)
  165. {
  166. Position = CachedGeometry.GetAccumulatedRenderTransform().GetTranslation();
  167. Size = TransformVector(CachedGeometry.GetAccumulatedRenderTransform(), CachedGeometry.GetLocalSize());
  168. }
  169. TSharedPtr<FGenericWindow> FCEFInterfaceTextInputMethodContext::GetWindow()
  170. {
  171. if (CachedSlateWindow.IsValid())
  172. {
  173. return CachedSlateWindow.Pin()->GetNativeWindow();
  174. }
  175. const TSharedPtr<SWidget> CachedSlateWidgetPtr = Owner->InternalBrowserSlateWidget.Pin();
  176. if (!CachedSlateWidgetPtr.IsValid())
  177. {
  178. return nullptr;
  179. }
  180. TSharedPtr<SWindow> SlateWindow = FSlateApplication::Get().FindWidgetWindow(CachedSlateWidgetPtr.ToSharedRef());
  181. CachedSlateWindow = SlateWindow;
  182. return SlateWindow.IsValid() ? SlateWindow->GetNativeWindow() : nullptr;
  183. }
  184. void FCEFInterfaceTextInputMethodContext::BeginComposition()
  185. {
  186. if (!bIsComposing)
  187. {
  188. bIsComposing = true;
  189. }
  190. }
  191. void FCEFInterfaceTextInputMethodContext::UpdateCompositionRange(const int32 InBeginIndex, const uint32 InLength)
  192. {
  193. CompositionBeginIndex = InBeginIndex;
  194. CompositionLength = InLength;
  195. }
  196. void FCEFInterfaceTextInputMethodContext::EndComposition()
  197. {
  198. if (bIsComposing)
  199. {
  200. bIsComposing = false;
  201. if (CompositionString.Len() > 0)
  202. {
  203. CefString Result = TCHAR_TO_WCHAR(*CompositionString);
  204. Owner->InternalCefBrowser->GetHost()->ImeCommitText(Result, CefRange(UINT32_MAX, UINT32_MAX), 0);
  205. }
  206. else
  207. {
  208. Owner->InternalCefBrowser->GetHost()->ImeCancelComposition();
  209. }
  210. ResetComposition();
  211. }
  212. }
  213. void FCEFInterfaceTextInputMethodContext::ResetComposition()
  214. {
  215. CompositionString.Empty();
  216. CefCompositionBounds.clear();
  217. CompositionBeginIndex = 0;
  218. CompositionLength = 0;
  219. SelectionRangeBeginIndex = 0;
  220. SelectionRangeLength = 0;
  221. }
  222. #endif