• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_COMPONENTS_NG_EVENT_ON_MOUSE_EVENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_ON_MOUSE_EVENT_H
18 
19 #include "core/event/mouse_event.h"
20 
21 namespace OHOS::Ace::NG {
22 
23 class InputEventHub;
24 class FrameNode;
25 
26 class InputEvent : public virtual AceType {
DECLARE_ACE_TYPE(InputEvent,AceType)27     DECLARE_ACE_TYPE(InputEvent, AceType)
28 public:
29     explicit InputEvent(OnMouseEventFunc&& callback) : onMouseCallback_(std::move(callback)) {}
30 
InputEvent(OnHoverEventFunc && callback)31     explicit InputEvent(OnHoverEventFunc&& callback) : onHoverCallback_(std::move(callback)) {}
32 
InputEvent(OnAxisEventFunc && callback)33     explicit InputEvent(OnAxisEventFunc&& callback) : onAxisCallback_(std::move(callback)) {}
34 
35     ~InputEvent() override = default;
36 
GetOnMouseEventFunc()37     const OnMouseEventFunc& GetOnMouseEventFunc() const
38     {
39         return onMouseCallback_;
40     }
41 
GetOnHoverEventFunc()42     const OnHoverEventFunc& GetOnHoverEventFunc() const
43     {
44         return onHoverCallback_;
45     }
46 
GetOnAxisEventFunc()47     const OnAxisEventFunc& GetOnAxisEventFunc() const
48     {
49         return onAxisCallback_;
50     }
51 
operator()52     void operator()(MouseInfo& info) const
53     {
54         if (onMouseCallback_) {
55             onMouseCallback_(info);
56         }
57     }
58 
operator()59     void operator()(bool state) const
60     {
61         if (onHoverCallback_) {
62             onHoverCallback_(state);
63         }
64     }
65 
operator()66     void operator()(AxisInfo& info) const
67     {
68         if (onAxisCallback_) {
69             onAxisCallback_(info);
70         }
71     }
72 
73 private:
74     OnMouseEventFunc onMouseCallback_;
75     OnHoverEventFunc onHoverCallback_;
76     OnAxisEventFunc onAxisCallback_;
77 };
78 
79 class ACE_EXPORT InputEventActuator : public virtual AceType {
80     DECLARE_ACE_TYPE(InputEventActuator, AceType)
81 public:
82     explicit InputEventActuator(const WeakPtr<InputEventHub>& inputEventHub);
83     ~InputEventActuator() override = default;
84 
ReplaceInputEvent(OnMouseEventFunc && callback)85     void ReplaceInputEvent(OnMouseEventFunc&& callback)
86     {
87         if (userCallback_) {
88             userCallback_.Reset();
89         }
90         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
91     }
ReplaceInputEvent(OnHoverEventFunc && callback)92     void ReplaceInputEvent(OnHoverEventFunc&& callback)
93     {
94         if (userCallback_) {
95             userCallback_.Reset();
96         }
97         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
98     }
ReplaceInputEvent(OnAxisEventFunc && callback)99     void ReplaceInputEvent(OnAxisEventFunc&& callback)
100     {
101         if (userCallback_) {
102             userCallback_.Reset();
103         }
104         userCallback_ = MakeRefPtr<InputEvent>(std::move(callback));
105     }
106 
AddInputEvent(const RefPtr<InputEvent> & inputEvent)107     void AddInputEvent(const RefPtr<InputEvent>& inputEvent)
108     {
109         if (inputEvents_.empty()) {
110             inputEvents_.emplace_back(inputEvent);
111             return;
112         }
113         if (std::find(inputEvents_.begin(), inputEvents_.end(), inputEvent) == inputEvents_.end()) {
114             inputEvents_.emplace_back(inputEvent);
115         }
116     }
117 
RemoveInputEvent(const RefPtr<InputEvent> & inputEvent)118     void RemoveInputEvent(const RefPtr<InputEvent>& inputEvent)
119     {
120         inputEvents_.remove(inputEvent);
121     }
122 
123     void OnCollectMouseEvent(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
124         TouchTestResult& result);
125 
126     void OnCollectHoverEvent(
127         const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result);
128 
129     void OnCollectHoverEffect(const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl,
130         TouchTestResult& result);
131 
132     void OnCollectAxisEvent(
133         const OffsetF& coordinateOffset, const GetEventTargetImpl& getEventTargetImpl, AxisTestResult& onAxisResult);
134 
135 private:
136     WeakPtr<InputEventHub> inputEventHub_;
137     RefPtr<MouseEventTarget> mouseEventTarget_;
138     RefPtr<HoverEventTarget> hoverEventTarget_;
139     RefPtr<HoverEffectTarget> hoverEffectTarget_;
140     RefPtr<AxisEventTarget> axisEventTarget_;
141     std::list<RefPtr<InputEvent>> inputEvents_;
142     RefPtr<InputEvent> userCallback_;
143 };
144 
145 } // namespace OHOS::Ace::NG
146 
147 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_INPUT_EVENT_H
148