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_STYLUS_STYLUS_DETECTOR_MGR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_STYLUS_STYLUS_DETECTOR_MGR_H
18 
19 #include <cstdint>
20 #include <unordered_map>
21 #include <unordered_set>
22 
23 #include "interfaces/inner_api/ace/stylus/stylus_detector_interface.h"
24 #include "stylus_detector_loader.h"
25 
26 #include "base/memory/referenced.h"
27 #include "base/utils/macros.h"
28 #include "core/components_ng/base/frame_node.h"
29 #include "core/event/touch_event.h"
30 #include "core/common/ime/text_input_client.h"
31 #include "core/components_ng/pattern/text/text_base.h"
32 #include "core/components_ng/pattern/text/layout_info_interface.h"
33 
34 namespace OHOS::Ace {
35 constexpr int32_t HOT_AREA_EXPAND_TIME = 2;
36 constexpr Dimension HOT_AREA_ADJUST_SIZE = 20.0_vp;
37 struct Hash {
operatorHash38     size_t operator()(const RefPtr<NG::FrameNode>& node) const
39     {
40         return node->GetId();
41     }
42 };
43 using FrameNodeSet = std::unordered_set<RefPtr<NG::FrameNode>, Hash>;
44 
45 class ACE_EXPORT StylusDetectorMgr final : public StylusDetectorInterface {
46 public:
47     static StylusDetectorMgr* GetInstance();
48 
49     bool IsEnable() override;
50     bool RegisterStylusInteractionListener(
51         const std::string& bundleName, const std::shared_ptr<IStylusDetectorCallback>& callback) override;
52     void UnRegisterStylusInteractionListener(const std::string& bundleName) override;
53     bool Notify(const NotifyInfo& notifyInfo) override;
54 
55     RefPtr<NG::FrameNode> FindHitFrameNode(const TouchEvent& touchEvent, const TouchTestResult& touchTestResult);
56     bool IsNeedInterceptedTouchEvent(
57         const TouchEvent& touchEvent, std::unordered_map<size_t, TouchTestResult> touchTestResults);
58 
59     void AddTextFieldFrameNode(const RefPtr<NG::FrameNode>& textFieldNode,
60         const WeakPtr<NG::LayoutInfoInterface>& layoutInfo);
61     void RemoveTextFieldFrameNode(const int32_t id);
62 
GetDefaultNodeId()63     int32_t GetDefaultNodeId() const
64     {
65         return nodeId_;
66     }
67 
GetLayoutInfo()68     WeakPtr<NG::LayoutInfoInterface> GetLayoutInfo() const
69     {
70         return layoutInfo_;
71     }
72 
SetSelectState(int32_t start,int32_t end,bool showMenu)73     void SetSelectState(int32_t start, int32_t end, bool showMenu)
74     {
75         sInd_ = start;
76         eInd_ = end;
77         showMenu_ = showMenu;
78     }
79 
HasSelectChanged(int32_t start,int32_t end,bool showMenu)80     bool HasSelectChanged(int32_t start, int32_t end, bool showMenu)
81     {
82         return (sInd_ != start || eInd_ != end || showMenu_ != showMenu);
83     }
84 
85 
86 private:
87     StylusDetectorMgr();
88     ~StylusDetectorMgr() = default;
89 
90     bool IsStylusTouchEvent(const TouchEvent& touchEvent) const;
91     bool IsHitCleanNodeResponseArea(
92         const NG::PointF& point, const RefPtr<NG::FrameNode>& frameNode, uint64_t nanoTimestamp);
93 
94     std::unordered_map<int32_t, WeakPtr<NG::FrameNode>> textFieldNodes_;
95     std::unordered_map<int32_t, WeakPtr<NG::LayoutInfoInterface>> textFieldLayoutInfos_;
96 
97     StylusDetectorInstance engine_ = nullptr;
98     bool isRegistered_ = false;
99     int32_t nodeId_ = 0;
100     WeakPtr<NG::LayoutInfoInterface> layoutInfo_;
101     int32_t sInd_ = -1;
102     int32_t eInd_ = -1;
103     bool showMenu_ = false;
104 };
105 } // namespace OHOS::Ace
106 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_STYLUS_STYLUS_DETECTOR_MGR_H