1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "adapter/preview/entrance/editing/text_input_client_mgr.h"
17
18 #include "base/utils/string_utils.h"
19 #include "base/memory/ace_type.h"
20 #include "core/common/ime/constant.h"
21 #include "core/common/ime/text_selection.h"
22 #include "core/common/ime/text_editing_value.h"
23 #include "core/common/ime/text_input_proxy.h"
24 #include "adapter/preview/entrance/editing/text_input_impl.h"
25
26 namespace OHOS::Ace::Platform {
27
TextInputClientMgr()28 TextInputClientMgr::TextInputClientMgr() : clientId_(IME_CLIENT_ID_NONE), currentConnection_(nullptr)
29 {}
30
31 TextInputClientMgr::~TextInputClientMgr() = default;
32
InitTextInputProxy()33 void TextInputClientMgr::InitTextInputProxy()
34 {
35 // Initial the proxy of Input method
36 TextInputProxy::GetInstance().SetDelegate(std::make_unique<TextInputImpl>());
37 }
38
SetClientId(const int32_t clientId)39 void TextInputClientMgr::SetClientId(const int32_t clientId)
40 {
41 clientId_ = clientId;
42 }
43
ResetClientId()44 void TextInputClientMgr::ResetClientId()
45 {
46 SetClientId(IME_CLIENT_ID_NONE);
47 }
48
IsValidClientId() const49 bool TextInputClientMgr::IsValidClientId() const
50 {
51 return clientId_ != IME_CLIENT_ID_NONE;
52 }
53
AddCharacter(const wchar_t wideChar)54 bool TextInputClientMgr::AddCharacter(const wchar_t wideChar)
55 {
56 LOGI("The unicode of inputed character is: %{public}d.", static_cast<int32_t>(wideChar));
57 if (!IsValidClientId()) {
58 return false;
59 }
60 std::wstring appendElement(1, wideChar);
61 auto textEditingValue = std::make_shared<TextEditingValue>();
62 textEditingValue->text = textEditingValue_.GetBeforeSelection() + StringUtils::ToString(appendElement) +
63 textEditingValue_.GetAfterSelection();
64 textEditingValue->UpdateSelection(std::max(textEditingValue_.selection.GetStart(), 0) + appendElement.length());
65 SetTextEditingValue(*textEditingValue);
66 return UpdateEditingValue(textEditingValue);
67 }
68
SetTextEditingValue(const TextEditingValue & textEditingValue)69 void TextInputClientMgr::SetTextEditingValue(const TextEditingValue& textEditingValue)
70 {
71 textEditingValue_ = textEditingValue;
72 }
73
SetCurrentConnection(const RefPtr<TextInputConnection> & currentConnection)74 void TextInputClientMgr::SetCurrentConnection(const RefPtr<TextInputConnection>& currentConnection)
75 {
76 currentConnection_ = currentConnection;
77 }
78
IsCurrentConnection(const TextInputConnection * connection) const79 bool TextInputClientMgr::IsCurrentConnection(const TextInputConnection* connection) const
80 {
81 return currentConnection_ == connection;
82 }
83
UpdateEditingValue(const std::shared_ptr<TextEditingValue> & value,bool needFireChangeEvent)84 bool TextInputClientMgr::UpdateEditingValue(const std::shared_ptr<TextEditingValue>& value, bool needFireChangeEvent)
85 {
86 #if defined(PREVIEW)
87 return false;
88 #else
89 if (!currentConnection_ || currentConnection_->GetClientId() != clientId_) {
90 return false;
91 }
92 auto weak = AceType::WeakClaim(AceType::RawPtr(currentConnection_));
93 currentConnection_->GetTaskExecutor()->PostTask(
94 [weak, value, needFireChangeEvent]() {
95 auto currentConnection = weak.Upgrade();
96 if (currentConnection == nullptr) {
97 LOGE("currentConnection is nullptr");
98 return;
99 }
100 auto client = currentConnection->GetClient();
101 if (client) {
102 client->UpdateEditingValue(value, needFireChangeEvent);
103 }
104 },
105 TaskExecutor::TaskType::UI, "ArkUIUpdateTextEditingValue");
106 return true;
107 #endif
108 }
109
PerformAction(const TextInputAction action)110 bool TextInputClientMgr::PerformAction(const TextInputAction action)
111 {
112 if (!currentConnection_ || currentConnection_->GetClientId() != clientId_) {
113 return false;
114 }
115 auto weak = AceType::WeakClaim(AceType::RawPtr(currentConnection_));
116 currentConnection_->GetTaskExecutor()->PostTask(
117 [weak, action]() {
118 auto currentConnection = weak.Upgrade();
119 if (currentConnection == nullptr) {
120 LOGE("currentConnection is nullptr");
121 return;
122 }
123 auto client = currentConnection->GetClient();
124 if (client) {
125 client->PerformAction(action);
126 }
127 },
128 TaskExecutor::TaskType::UI, "ArkUIPerformTextInputAction");
129 return true;
130 }
131
132 } // namespace OHOS::Ace::Platform
133