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 #include "core/components_ng/event/event_hub.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21
22 namespace OHOS::Ace::NG {
23
AttachHost(const WeakPtr<FrameNode> & host)24 void EventHub::AttachHost(const WeakPtr<FrameNode>& host)
25 {
26 host_ = host;
27 }
28
GetFrameNode() const29 RefPtr<FrameNode> EventHub::GetFrameNode() const
30 {
31 return host_.Upgrade();
32 }
33
AddSupportedState(UIState state)34 void EventHub::AddSupportedState(UIState state)
35 {
36 if (!stateStyleMgr_) {
37 stateStyleMgr_ = MakeRefPtr<StateStyleManager>(host_);
38 }
39 stateStyleMgr_->AddSupportedState(state);
40 }
41
SetSupportedStates(UIState state)42 void EventHub::SetSupportedStates(UIState state)
43 {
44 if (!stateStyleMgr_) {
45 stateStyleMgr_ = MakeRefPtr<StateStyleManager>(host_);
46 }
47 stateStyleMgr_->SetSupportedStates(state);
48 }
49
SetCurrentUIState(UIState state,bool flag)50 void EventHub::SetCurrentUIState(UIState state, bool flag)
51 {
52 if (!stateStyleMgr_) {
53 stateStyleMgr_ = MakeRefPtr<StateStyleManager>(host_);
54 }
55 stateStyleMgr_->SetCurrentUIState(state, flag);
56 }
57
IsCurrentStateOn(UIState state)58 bool EventHub::IsCurrentStateOn(UIState state)
59 {
60 if (!stateStyleMgr_) {
61 return false;
62 }
63 return stateStyleMgr_->IsCurrentStateOn(state);
64 }
65
CreateGetEventTargetImpl() const66 GetEventTargetImpl EventHub::CreateGetEventTargetImpl() const
67 {
68 auto impl = [weak = host_]() -> std::optional<EventTarget> {
69 auto host = weak.Upgrade();
70 CHECK_NULL_RETURN(host, std::nullopt);
71 EventTarget eventTarget;
72 eventTarget.id = std::to_string(host->GetId());
73 eventTarget.type = host->GetTag();
74 auto geometryNode = host->GetGeometryNode();
75 auto offset = geometryNode->GetFrameOffset();
76 auto size = geometryNode->GetFrameSize();
77 eventTarget.area.SetOffset(DimensionOffset(offset));
78 eventTarget.area.SetHeight(Dimension(size.Height()));
79 eventTarget.area.SetWidth(Dimension(size.Width()));
80 eventTarget.origin = DimensionOffset(host->GetOffsetRelativeToWindow() - offset);
81 return eventTarget;
82 };
83 return impl;
84 }
85
PostEnabledTask()86 void EventHub::PostEnabledTask()
87 {
88 auto pipeline = PipelineBase::GetCurrentContext();
89 CHECK_NULL_VOID(pipeline);
90 auto taskExecutor = pipeline->GetTaskExecutor();
91 CHECK_NULL_VOID(taskExecutor);
92 taskExecutor->PostTask(
93 [weak = WeakClaim(this)]() {
94 auto eventHub = weak.Upgrade();
95 CHECK_NULL_VOID(eventHub);
96 eventHub->UpdateCurrentUIState(UI_STATE_DISABLED);
97 }, TaskExecutor::TaskType::UI);
98 }
99
MarkModifyDone()100 void EventHub::MarkModifyDone()
101 {
102 if (stateStyleMgr_) {
103 // focused style is managered in focus event hub.
104 if (stateStyleMgr_->HasStateStyle(UI_STATE_PRESSED)) {
105 GetOrCreateGestureEventHub()->AddTouchEvent(stateStyleMgr_->GetPressedListener());
106 }
107 if (stateStyleMgr_->HasStateStyle(UI_STATE_DISABLED)) {
108 if (enabled_) {
109 stateStyleMgr_->ResetCurrentUIState(UI_STATE_DISABLED);
110 } else {
111 PostEnabledTask();
112 }
113 }
114 }
115
116 if (gestureEventHub_) {
117 gestureEventHub_->OnModifyDone();
118 }
119 OnModifyDone();
120 }
121
SetCustomerOnDragFunc(DragFuncType dragFuncType,OnDragFunc && onDragFunc)122 void EventHub::SetCustomerOnDragFunc(DragFuncType dragFuncType, OnDragFunc&& onDragFunc)
123 {
124 switch (dragFuncType) {
125 case DragFuncType::DRAG_ENTER:
126 customerOnDragEnter_ = std::move(onDragFunc);
127 break;
128 case DragFuncType::DRAG_LEAVE:
129 customerOnDragLeave_ = std::move(onDragFunc);
130 break;
131 case DragFuncType::DRAG_MOVE:
132 customerOnDragMove_ = std::move(onDragFunc);
133 break;
134 case DragFuncType::DRAG_DROP:
135 customerOnDrop_ = std::move(onDragFunc);
136 break;
137 default:
138 LOGW("unsuport dragFuncType");
139 break;
140 }
141 }
142
SetCustomerOnDragFunc(DragFuncType dragFuncType,OnNewDragFunc && onDragEnd)143 void EventHub::SetCustomerOnDragFunc(DragFuncType dragFuncType, OnNewDragFunc&& onDragEnd)
144 {
145 if (dragFuncType != DragFuncType::DRAG_END) {
146 return;
147 }
148 customerOnDragEnd_ = std::move(onDragEnd);
149 }
150
FireCustomerOnDragFunc(DragFuncType dragFuncType,const RefPtr<OHOS::Ace::DragEvent> & info,const std::string & extraParams)151 void EventHub::FireCustomerOnDragFunc(DragFuncType dragFuncType, const RefPtr<OHOS::Ace::DragEvent>& info,
152 const std::string& extraParams)
153 {
154 switch (dragFuncType) {
155 case DragFuncType::DRAG_ENTER: {
156 if (customerOnDragEnter_ != nullptr) {
157 auto customerDragEnter = customerOnDragEnter_;
158 customerDragEnter(info, extraParams);
159 }
160 break;
161 }
162 case DragFuncType::DRAG_LEAVE: {
163 if (customerOnDragLeave_ != nullptr) {
164 auto customerOnDragLeave = customerOnDragLeave_;
165 customerOnDragLeave(info, extraParams);
166 }
167 break;
168 }
169 case DragFuncType::DRAG_MOVE: {
170 if (customerOnDragMove_ != nullptr) {
171 auto customerOnDragMove = customerOnDragMove_;
172 customerOnDragMove(info, extraParams);
173 }
174 break;
175 }
176 case DragFuncType::DRAG_DROP: {
177 if (customerOnDrop_ != nullptr) {
178 auto customerOnDrop = customerOnDrop_;
179 customerOnDrop(info, extraParams);
180 }
181 break;
182 }
183 case DragFuncType::DRAG_END: {
184 if (customerOnDragEnd_ != nullptr) {
185 auto customerOnDragEnd = customerOnDragEnd_;
186 customerOnDragEnd(info);
187 }
188 break;
189 }
190 default:
191 LOGW("unsuport DragFuncType");
192 break;
193 }
194 }
195
IsFireOnDrop(const RefPtr<OHOS::Ace::DragEvent> & info)196 bool EventHub::IsFireOnDrop(const RefPtr<OHOS::Ace::DragEvent>& info)
197 {
198 return !HasCustomerOnDrop()
199 || info->GetResult() == DragRet::DRAG_DEFAULT
200 || info->GetResult() == DragRet::ENABLE_DROP
201 || info->GetResult() == DragRet::DISABLE_DROP;
202 return true;
203 }
204
HandleInternalOnDrop(const RefPtr<OHOS::Ace::DragEvent> & info,const std::string & extraParams)205 void EventHub::HandleInternalOnDrop(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams)
206 {
207 if (IsFireOnDrop(info)) {
208 FireOnDrop(info, extraParams);
209 }
210 }
211
AddInnerOnAreaChangedCallback(int32_t id,OnAreaChangedFunc && callback)212 void EventHub::AddInnerOnAreaChangedCallback(int32_t id, OnAreaChangedFunc&& callback)
213 {
214 auto pipeline = PipelineContext::GetCurrentContext();
215 CHECK_NULL_VOID(pipeline);
216 auto frameNode = GetFrameNode();
217 CHECK_NULL_VOID(frameNode);
218 pipeline->AddOnAreaChangeNode(frameNode->GetId());
219 frameNode->InitLastArea();
220 onAreaChangedInnerCallbacks_[id] = std::move(callback);
221 }
222
223 } // namespace OHOS::Ace::NG
224