CEFInterfaceImeHandler.cpp 5.7 KB

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