• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_PROPERTIES_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_PROPERTIES_H
18 
19 #include <set>
20 
21 #include "interfaces/inner_api/ace/ai/data_detector_interface.h"
22 #include "interfaces/inner_api/ace/ai/data_url_analyzer.h"
23 
24 #include "base/memory/ace_type.h"
25 #include "base/thread/cancelable_callback.h"
26 #include "core/components_ng/base/frame_node.h"
27 #include "core/components_ng/property/property.h"
28 #include "core/components_v2/inspector/utils.h"
29 
30 namespace OHOS::AAFwk {
31 class Want;
32 class WantParams;
33 } // namespace OHOS::AAFwk
34 
35 namespace OHOS::Ace {
36 
37 namespace NG {
38 class TextPattern;
39 class RichEditorPattern;
40 }
41 
42 struct AISpan {
43     int32_t start = 0;
44     int32_t end = 0;
45     std::string content = "";
46     TextDataDetectType type = TextDataDetectType::PHONE_NUMBER;
47     bool operator==(const AISpan& span) const
48     {
49         return start == span.start && end == span.end && content == span.content && type == span.type;
50     }
51 };
52 class DataDetectorAdapter : public AceType {
53     DECLARE_ACE_TYPE(DataDetectorAdapter, AceType);
54 
55 public:
56     DataDetectorAdapter() = default;
57     ~DataDetectorAdapter() override = default;
58 
GetHost()59     RefPtr<NG::FrameNode> GetHost() const
60     {
61         return frameNode_.Upgrade();
62     }
SetTextDetectResult(const TextDataDetectResult result)63     void SetTextDetectResult(const TextDataDetectResult result)
64     {
65         textDetectResult_ = result;
66     }
67     void FireFinalResult();
FireOnResult(const std::string & result)68     void FireOnResult(const std::string& result)
69     {
70         if (onResult_) {
71             onResult_(result);
72         }
73     }
74     bool ParseOriText(const std::unique_ptr<JsonValue>& entityJson, std::u16string& text);
75     void PreprocessTextDetect();
76     void InitTextDetect(int32_t startPos, std::string detectText);
77     void HandleTextUrlDetect();
78     void HandleUrlResult(std::vector<UrlEntity> urlEntities);
79     void SetTextDetectTypes(const std::string& types);
80     void ParseAIResult(const TextDataDetectResult& result, int32_t startPos);
81     void ParseAIJson(const std::unique_ptr<JsonValue>& jsonValue, TextDataDetectType type, int32_t startPos);
82     void StartAITask();
CancelAITask()83     void CancelAITask()
84     {
85         if (aiDetectDelayTask_) {
86             aiDetectDelayTask_.Cancel();
87         }
88         aiDetectInitialized_ = false;
89     }
90     bool ShowAIEntityMenu(const AISpan& aiSpan, const NG::RectF& aiRect, const RefPtr<NG::FrameNode>& targetNode,
91         bool isShowCopy = true, bool isShowSelectText = true);
92     void ResponseBestMatchItem(const AISpan& aiSpan);
93     void GetAIEntityMenu();
94     void MarkDirtyNode() const;
95 
96 private:
97     friend class NG::TextPattern;
98     friend class NG::RichEditorPattern;
99 
100     std::function<void()> GetDetectDelayTask(const std::map<int32_t, AISpan>& aiSpanMap);
101     void OnClickAIMenuOption(const AISpan& aiSpan, const std::pair<std::string, FuncVariant>& menuOption,
102         const RefPtr<NG::FrameNode>& targetNode = nullptr);
103 
104     WeakPtr<NG::FrameNode> frameNode_;
105     bool aiDetectInitialized_ = false;
106     bool hasClickedAISpan_ = false;
107     bool pressedByLeftMouse_ = false;
108     bool typeChanged_ = false;
109     bool hasClickedMenuOption_ = false;
110     bool hasUrlType_ = false;
111     uint8_t aiDetectFlag_ = 0;
112     std::vector<NG::RectF> aiSpanRects_;
113     AISpan clickedAISpan_;
114     std::string textDetectTypes_;
115     std::u16string textForAI_;
116     std::u16string lastTextForAI_;
117     std::set<std::string> textDetectTypesSet_;
118     TextDataDetectResult textDetectResult_;
119     std::function<void(const std::string&)> onResult_;
120     std::function<void(const std::string&)> onClickMenu_;
121     std::map<int32_t, AISpan> aiSpanMap_;
122     CancelableCallback<void()> aiDetectDelayTask_;
123     std::unordered_map<int32_t, std::string> entityJson_;
124     TimeStamp startDetectorTimeStamp_;
125     std::vector<std::string> detectTexts_;
126     int32_t mainContainerId_ = -1;
127     std::optional<Color> entityColor_;
128     std::optional<TextDecoration> entityDecorationType_;
129     std::optional<Color> entityDecorationColor_;
130     std::optional<TextDecorationStyle> entityDecorationStyle_;
131     std::string textDetectConfigStr_;
132 };
133 } // namespace OHOS::Ace
134 
135 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_AI_PROPERTIES_H
136