Signed-off-by: cy7717 <chenyu301@huawei.com>
This commit is contained in:
cy7717 2023-07-07 19:21:50 +08:00
parent 9d97d31419
commit 9d18cd6f72
6 changed files with 36 additions and 12 deletions

View File

@ -302,7 +302,7 @@ void JsGetInputMethodController::RegisterListener(
jsCbMap_[type].push_back(std::move(callbackObj));
}
void JsGetInputMethodController::UnRegisterListener(std::string type)
void JsGetInputMethodController::UnRegisterListener(napi_value callback, std::string type)
{
IMSA_HILOGI("UnRegisterListener %{public}s", type.c_str());
std::lock_guard<std::recursive_mutex> lock(mutex_);
@ -310,7 +310,22 @@ void JsGetInputMethodController::UnRegisterListener(std::string type)
IMSA_HILOGE("methodName: %{public}s already unRegistered!", type.c_str());
return;
}
if (callback == nullptr) {
jsCbMap_.erase(type);
IMSA_HILOGE("callback is nullptr");
return;
}
for (auto item = jsCbMap_[type].begin(); item != jsCbMap_[type].end(); item++) {
if ((callback != nullptr)
&& (JsUtils::Equals((*item)->env_, callback, (*item)->callback_, (*item)->threadId_))) {
jsCbMap_[type].erase(item);
break;
}
}
if (jsCbMap_[type].empty()) {
jsCbMap_.erase(type);
}
}
napi_value JsGetInputMethodController::Subscribe(napi_env env, napi_callback_info info)
@ -364,12 +379,16 @@ napi_value JsGetInputMethodController::UnSubscribe(napi_env env, napi_callback_i
IMSA_HILOGE("UnSubscribe failed, type:%{public}s", type.c_str());
return nullptr;
}
// If the type of optional parameter is wrong, make it nullptr
if (JsUtil::GetType(env, argv[1]) != napi_function) {
argv[1] = nullptr;
}
IMSA_HILOGD("UnSubscribe type:%{public}s.", type.c_str());
auto engine = reinterpret_cast<JsGetInputMethodController *>(JsUtils::GetNativeSelf(env, info));
if (engine == nullptr) {
return nullptr;
}
engine->UnRegisterListener(type);
engine->UnRegisterListener(argv[1], type);
napi_value result = nullptr;
napi_get_null(env, &result);

View File

@ -192,7 +192,7 @@ private:
static napi_value CreateSelectMovement(napi_env env, int32_t direction);
static napi_value CreateSendFunctionKey(napi_env env, int32_t functionKey);
void RegisterListener(napi_value callback, std::string type, std::shared_ptr<JSCallbackObject> callbackObj);
void UnRegisterListener(std::string type);
void UnRegisterListener(napi_value callback, std::string type);
static napi_status ParseAttachInput(
napi_env env, size_t argc, napi_value *argv, const std::shared_ptr<AttachContext> &ctxt);
static bool GetValue(napi_env env, napi_value in, CursorInfo &out);

View File

@ -67,7 +67,7 @@ void JsGetInputMethodTextChangedListener::HandleExtendAction(int32_t action)
std::u16string JsGetInputMethodTextChangedListener::GetLeftTextOfCursor(int32_t number)
{
return JsGetInputMethodController::GetInstance()->GetText("getLeftTextOfCursor" ,number);
return JsGetInputMethodController::GetInstance()->GetText("getLeftTextOfCursor", number);
}
std::u16string JsGetInputMethodTextChangedListener::GetRightTextOfCursor(int32_t number)

View File

@ -115,8 +115,8 @@ int32_t InputDataChannelStub::SelectByRangeOnRemote(MessageParcel &data, Message
int32_t start = 0;
int32_t end = 0;
int ret = SendMessage([&data, &start, &end](MessageParcel &parcel) {
return ITypesUtil::Unmarshal(data, start, end) && ITypesUtil::Marshal(parcel, start, end) ? new (std::nothrow)
Message(MessageID::MSG_ID_SELECT_BY_RANGE, &parcel) : nullptr;
return ITypesUtil::Unmarshal(data, start, end) && ITypesUtil::Marshal(parcel, start, end) ?
new (std::nothrow)Message(MessageID::MSG_ID_SELECT_BY_RANGE, &parcel) : nullptr;
});
if (!ITypesUtil::Marshal(reply, ret)) {
IMSA_HILOGE("failed to write reply");
@ -148,8 +148,8 @@ int32_t InputDataChannelStub::HandleExtendActionOnRemote(MessageParcel &data, Me
IMSA_HILOGD("InputDataChannelStub run in");
int32_t action = 0;
auto ret = SendMessage([&data, &action](MessageParcel &parcel) {
return ITypesUtil::Unmarshal(data, action) && ITypesUtil::Marshal(parcel, action) ? new (std::nothrow)
Message(MessageID::MSG_ID_HANDLE_EXTEND_ACTION, &parcel) : nullptr;
return ITypesUtil::Unmarshal(data, action) && ITypesUtil::Marshal(parcel, action) ?
new (std::nothrow)Message(MessageID::MSG_ID_HANDLE_EXTEND_ACTION, &parcel) : nullptr;
});
if (!ITypesUtil::Marshal(reply, ret)) {
IMSA_HILOGE("failed to write reply");
@ -205,8 +205,8 @@ int32_t InputDataChannelStub::GetText(int32_t msgId, MessageParcel &data, Messag
int32_t number = -1;
auto resultHandler = std::make_shared<BlockData<std::u16string>>(MAX_TIMEOUT, u"");
auto ret = SendMessage([&msgId, &data, &number, &resultHandler](MessageParcel &parcel) {
return ITypesUtil::Unmarshal(data, number) && ITypesUtil::Marshal(parcel, number) ? new (std::nothrow)
Message(msgId, &parcel, resultHandler) : nullptr;
return ITypesUtil::Unmarshal(data, number) && ITypesUtil::Marshal(parcel, number) ?
new (std::nothrow)Message(msgId, &parcel, resultHandler) : nullptr;
});
if (ret != ErrorCode::NO_ERROR) {
return ITypesUtil::Marshal(reply, ret) ? ErrorCode::NO_ERROR : ErrorCode::ERROR_EX_PARCELABLE;

View File

@ -774,6 +774,10 @@ void InputMethodController::GetText(const Message *msg)
return;
}
auto number = msg->msgContent_->ReadInt32();
if (number < 0) {
resultHandler->SetValue(text);
return;
}
if (msg->msgId_ == MSG_ID_GET_TEXT_BEFORE_CURSOR) {
text = textListener_->GetLeftTextOfCursor(number);
} else {

View File

@ -30,7 +30,8 @@ public:
MessageParcel *msgContent_ = nullptr; // message content
std::shared_ptr<BlockData<std::u16string>> textResultHandler_{ nullptr };
std::shared_ptr<BlockData<int32_t>> indexResultHandler_{ nullptr };
Message(int32_t msgId, MessageParcel *msgContent, const std::shared_ptr<BlockData<std::u16string>> &textResultHandler);
Message(
int32_t msgId, MessageParcel *msgContent, const std::shared_ptr<BlockData<std::u16string>> &textResultHandler);
Message(int32_t msgId, MessageParcel *msgContent, const std::shared_ptr<BlockData<int32_t>> &indexResultHandler);
Message(int32_t msgId, MessageParcel *msgContent);
explicit Message(const Message &msg);