• 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_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 
63     bool IsNeedInterceptedTouchEventForWeb(float x, float y);
64 
GetDefaultNodeId()65     int32_t GetDefaultNodeId() const
66     {
67         return nodeId_;
68     }
69 
GetLayoutInfo()70     WeakPtr<NG::LayoutInfoInterface> GetLayoutInfo() const
71     {
72         return layoutInfo_;
73     }
74 
SetSelectState(int32_t start,int32_t end,bool showMenu)75     void SetSelectState(int32_t start, int32_t end, bool showMenu)
76     {
77         sInd_ = start;
78         eInd_ = end;
79         showMenu_ = showMenu;
80     }
81 
HasSelectChanged(int32_t start,int32_t end,bool showMenu)82     bool HasSelectChanged(int32_t start, int32_t end, bool showMenu)
83     {
84         return (sInd_ != start || eInd_ != end || showMenu_ != showMenu);
85     }
86 
87 
88 private:
89     StylusDetectorMgr();
90     ~StylusDetectorMgr() = default;
91 
92     bool IsStylusTouchEvent(const TouchEvent& touchEvent) const;
93     bool IsHitCleanNodeResponseArea(
94         const NG::PointF& point, const RefPtr<NG::FrameNode>& frameNode, uint64_t nanoTimestamp);
95 
96     std::unordered_map<int32_t, WeakPtr<NG::FrameNode>> textFieldNodes_;
97     std::unordered_map<int32_t, WeakPtr<NG::LayoutInfoInterface>> textFieldLayoutInfos_;
98 
99     StylusDetectorInstance engine_ = nullptr;
100     bool isRegistered_ = false;
101     int32_t nodeId_ = 0;
102     WeakPtr<NG::LayoutInfoInterface> layoutInfo_;
103     int32_t sInd_ = -1;
104     int32_t eInd_ = -1;
105     bool showMenu_ = false;
106 };
107 } // namespace OHOS::Ace
108 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_STYLUS_STYLUS_DETECTOR_MGR_H