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_BRIDGE_COMMON_ACCESSIBILITY_JS_THIRD_ACCESSIBILITY_HOVER_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_ACCESSIBILITY_JS_THIRD_ACCESSIBILITY_HOVER_NG_H
18 
19 #include <vector>
20 
21 #include "accessibility_config.h"
22 #include "accessibility_element_operator.h"
23 #include "accessibility_event_info.h"
24 #include "accessibility_state_event.h"
25 #include "base/geometry/ng/point_t.h"
26 #include "base/memory/ace_type.h"
27 #include "base/utils/type_definition.h"
28 #include "core/accessibility/accessibility_manager.h"
29 #include "core/accessibility/accessibility_utils.h"
30 #include "core/event/ace_events.h"
31 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h"
32 
33 namespace OHOS::Ace {
34 struct MouseEvent;
35 struct TouchEvent;
36 
37 namespace Framework {
38 class FrameNode;
39 class JsAccessibilityManager;
40 struct ActionParam;
41 class JsThirdProviderInteractionOperation;
42 
43 using namespace OHOS::Accessibility;
44 
45 struct AccessibilityHoverForThirdState {
46     SourceType source = SourceType::NONE;
47     std::vector<int64_t> nodesHovering;
48     TimeStamp time;
49     bool idle = true;
50 };
51 
52 struct AccessibilityHoverForThirdConfig {
53     int64_t hostElementId = -1;
54     NG::PointF point;
55     SourceType sourceType = SourceType::NONE;
56     NG::AccessibilityHoverEventType eventType = NG::AccessibilityHoverEventType::ENTER;
57     TimeStamp time = std::chrono::high_resolution_clock::now();
58     RefPtr<NG::FrameNode> hostNode;
59     RefPtr<NG::PipelineContext> context;
60 };
61 
62 using AccessibilityHoverTestPathForThird = std::vector<int64_t>;
63 
64 class AccessibilityHoverManagerForThirdNG : public AceType {
65     DECLARE_ACE_TYPE(AccessibilityHoverManagerForThirdNG, AceType);
66 
67 public:
68     void HandleAccessibilityHoverForThird(
69         const AccessibilityHoverForThirdConfig& config);
70     bool GetElementInfoForThird(
71         int64_t elementId,
72         AccessibilityElementInfo& info,
73         int64_t hostElementId);
74 
75     bool ActThirdAccessibilityFocus(
76         int64_t elementId,
77         const AccessibilityElementInfo& nodeInfo,
78         const RefPtr<NG::FrameNode>& hostNode,
79         const RefPtr<NG::PipelineContext>& context,
80         bool isNeedClear);
81     void RegisterJsThirdProviderInteractionOperation(
82         int64_t hostElementId,
83         const std::shared_ptr<JsThirdProviderInteractionOperation>& jsThirdProviderOperator);
84     void DeregisterJsThirdProviderInteractionOperation(int64_t hostElementId);
GetJsThirdProviderInteractionOperation(int64_t hostElementId)85     std::weak_ptr<JsThirdProviderInteractionOperation> &GetJsThirdProviderInteractionOperation(
86         int64_t hostElementId)
87     {
88         return jsThirdProviderOperator_[hostElementId];
89     }
90 
91     bool OnDumpChildInfoForThirdRecursive(
92         int64_t hostElementId,
93         const std::vector<std::string>& params,
94         std::vector<std::string>& info,
95         const WeakPtr<JsAccessibilityManager>& jsAccessibilityManager);
96     bool ClearThirdAccessibilityFocus(const RefPtr<NG::FrameNode>& hostNode);
97 
98 private:
99     void ResetHoverForThirdState();
100     AccessibilityHoverTestPathForThird HoverPathForThird(
101         const int64_t hostElementId,
102         const NG::PointF& point,
103         AccessibilityElementInfo& rootInfo,
104         NG::OffsetF hostOffset);
105     bool HoverPathForThirdRecursive(
106         const int64_t hostElementId,
107         const NG::PointF& hoverPoint,
108         const AccessibilityElementInfo& nodeInfo,
109         AccessibilityHoverTestPathForThird& path,
110         NG::OffsetF hostOffset);
111     std::pair<bool, bool> GetSearchStrategyForThird(
112         const AccessibilityElementInfo& nodeInfo);
113     bool IsAccessibilityFocusable(const AccessibilityElementInfo& nodeInfo);
114     bool HasAccessibilityTextOrDescription(const AccessibilityElementInfo& nodeInfo);
115     void UpdateSearchStrategyByHitTestModeStr(
116         std::string& hitTestMode,
117         bool& shouldSearchSelf,
118         bool& shouldSearchChildren);
119     void DumpPropertyForThird(
120         int64_t elementId,
121         const WeakPtr<JsAccessibilityManager>& jsAccessibilityManager,
122         const std::shared_ptr<JsThirdProviderInteractionOperation>& jsThirdProviderOperator);
123 
124     AccessibilityHoverForThirdState hoverForThirdState_;
125     std::unordered_map<int64_t, std::weak_ptr<JsThirdProviderInteractionOperation>>
126         jsThirdProviderOperator_;
127 };
128 } // namespace NG
129 } // namespace OHOS::Ace
130 
131 #endif
132