• 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 
31 namespace OHOS::Ace {
32 constexpr int32_t HOT_AREA_EXPAND_TIME = 2;
33 constexpr Dimension HOT_AREA_ADJUST_SIZE = 20.0_vp;
34 struct Hash {
operatorHash35     size_t operator()(const RefPtr<NG::FrameNode>& node) const
36     {
37         return node->GetId();
38     }
39 };
40 using FrameNodeSet = std::unordered_set<RefPtr<NG::FrameNode>, Hash>;
41 
42 class ACE_EXPORT StylusDetectorMgr final : public StylusDetectorInterface {
43 public:
44     static StylusDetectorMgr* GetInstance();
45 
46     bool IsEnable() override;
47     bool RegisterStylusInteractionListener(
48         const std::string& bundleName, const std::shared_ptr<IStylusDetectorCallback>& callback) override;
49     void UnRegisterStylusInteractionListener(const std::string& bundleName) override;
50     bool Notify(const NotifyInfo& notifyInfo) override;
51 
52     RefPtr<NG::FrameNode> FindHitFrameNode(const TouchEvent& touchEvent, const TouchTestResult& touchTestResult);
53     bool IsNeedInterceptedTouchEvent(
54         const TouchEvent& touchEvent, std::unordered_map<size_t, TouchTestResult> touchTestResults);
55 
56     void AddTextFieldFrameNode(const RefPtr<NG::FrameNode>& textFieldNode);
57     void RemoveTextFieldFrameNode(const int32_t id);
58 
GetDefaultNodeId()59     int32_t GetDefaultNodeId() const
60     {
61         return nodeId_;
62     }
63 
64 private:
65     StylusDetectorMgr();
66     ~StylusDetectorMgr() = default;
67 
68     class StylusDetectorCallBack : public OHOS::Ace::IStylusDetectorCallback {
69     public:
70         explicit StylusDetectorCallBack() = default;
71         virtual ~StylusDetectorCallBack() = default;
72         static void RequestFocus(int32_t nodeId);
73         static void SetText(int32_t nodeId, std::string args);
74         static std::string GetText(int32_t nodeId);
75 
76         static void Redo(int32_t nodeId);
77         static void Undo(int32_t nodeId);
78         void OnDetector(
79             const CommandType& command, std::string args, std::shared_ptr<IAceStylusCallback> callback) override;
80         bool OnDetectorSync(const CommandType& command) override;
81     };
82 
83     bool IsStylusTouchEvent(const TouchEvent& touchEvent) const;
84     bool IsHitCleanNodeResponseArea(
85         const NG::PointF& point, const RefPtr<NG::FrameNode>& frameNode, uint64_t nanoTimestamp);
86 
87     std::unordered_map<int32_t, WeakPtr<NG::FrameNode>> textFieldNodes_;
88 
89     StylusDetectorInstance engine_ = nullptr;
90     bool isRegistered_ = false;
91     int32_t nodeId_ = 0;
92 };
93 } // namespace OHOS::Ace
94 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_STYLUS_STYLUS_DETECTOR_MGR_H