123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- // 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->GetMainFrame())
- {
- InternalCefBrowser->GetMainFrame()->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
|