1 /*
2  * Copyright (C) 2023 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 #include "core/components_ng/pattern/text_field/text_input_ai_checker.h"
16 
17 #include "base/log/dump_log.h"
18 
19 using namespace OHOS::Ace::NG;
20 namespace OHOS::Ace {
NeedAIAnalysis(const std::string & content,const NG::CaretUpdateType targetType,std::chrono::duration<float,std::ratio<1,SECONDS_TO_MILLISECONDS>> timeout)21 bool InputAIChecker::NeedAIAnalysis(const std::string& content, const NG::CaretUpdateType targetType,
22     std::chrono::duration<float, std::ratio<1, SECONDS_TO_MILLISECONDS>> timeout)
23 {
24     if (targetType != CaretUpdateType::DOUBLE_CLICK) {
25         TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "NeedAIAnalysis type is %{public}d,return!", static_cast<int>(targetType));
26         return false;
27     }
28 
29     // empty string check
30     if (content.empty()) {
31         TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "NeedAIAnalysis content empty,return!");
32         return false;
33     }
34 
35     // if user determined want the clicked position
36     TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "NeedAIAnalysis rectfied time:%{public}f", timeout.count());
37     if (targetType == CaretUpdateType::PRESSED && timeout.count() < AIPOS_RECTIFIED_INTERVAL_MS) {
38         return false;
39     }
40 
41     return true;
42 }
IsSingleClickAtBoundary(int32_t position,int32_t textLength)43 bool InputAIChecker::IsSingleClickAtBoundary(int32_t position, int32_t textLength)
44 {
45     bool endBoundary = position == textLength;
46     bool startBoundary = position == 0;
47     if (startBoundary || endBoundary) {
48         TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "boundary start is %{public}d,end %{public}d ", startBoundary, endBoundary);
49         return true;
50     }
51 
52     return false;
53 }
IsMultiClickAtBoundary(const NG::OffsetF & handleOffset,const NG::RectF & textRect)54 bool InputAIChecker::IsMultiClickAtBoundary(const NG::OffsetF& handleOffset, const NG::RectF& textRect)
55 {
56     if (textRect.GetX() - FLOAT_DIFF_COMPARE <= handleOffset.GetX() &&
57         handleOffset.GetX() <= textRect.GetX() + FLOAT_DIFF_COMPARE) {
58         TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "multi font boundary offset is handle: %{public}f,textRect: %{public}f ",
59             handleOffset.GetX(), textRect.GetX());
60         return true;
61     }
62 
63     auto edge = textRect.GetX() + textRect.Width();
64     if (handleOffset.GetX() >= edge - FLOAT_DIFF_COMPARE) {
65         TAG_LOGI(AceLogTag::ACE_TEXTINPUT, "multi tail boundary offset is handle: %{public}f,textRect: %{public}f ",
66             handleOffset.GetX(), edge);
67         return true;
68     }
69     return false;
70 }
71 } // namespace OHOS::Ace
72