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_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_EVENT_HUB_H 18 19 #include <list> 20 #include <utility> 21 22 #include "base/memory/ace_type.h" 23 #include "base/memory/referenced.h" 24 #include "base/utils/noncopyable.h" 25 #include "core/components_ng/event/focus_hub.h" 26 #include "core/components_ng/event/gesture_event_hub.h" 27 #include "core/components_ng/event/input_event_hub.h" 28 #include "core/components_ng/event/state_style_manager.h" 29 30 namespace OHOS::Ace::NG { 31 32 class FrameNode; 33 using OnAreaChangedFunc = 34 std::function<void(const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin)>; 35 36 // The event hub is mainly used to handle common collections of events, such as gesture events, mouse events, etc. 37 class EventHub : public virtual AceType { 38 DECLARE_ACE_TYPE(EventHub, AceType) 39 40 public: 41 EventHub() = default; 42 ~EventHub() override = default; 43 GetOrCreateGestureEventHub()44 const RefPtr<GestureEventHub>& GetOrCreateGestureEventHub() 45 { 46 if (!gestureEventHub_) { 47 gestureEventHub_ = MakeRefPtr<GestureEventHub>(WeakClaim(this)); 48 } 49 return gestureEventHub_; 50 } 51 GetGestureEventHub()52 const RefPtr<GestureEventHub>& GetGestureEventHub() const 53 { 54 return gestureEventHub_; 55 } 56 GetOrCreateInputEventHub()57 const RefPtr<InputEventHub>& GetOrCreateInputEventHub() 58 { 59 if (!inputEventHub_) { 60 inputEventHub_ = MakeRefPtr<InputEventHub>(WeakClaim(this)); 61 } 62 return inputEventHub_; 63 } 64 GetInputEventHub()65 const RefPtr<InputEventHub>& GetInputEventHub() const 66 { 67 return inputEventHub_; 68 } 69 70 const RefPtr<FocusHub>& GetOrCreateFocusHub(FocusType type = FocusType::DISABLE, bool focusable = false, 71 FocusStyleType focusStyleType = FocusStyleType::NONE, 72 const std::unique_ptr<FocusPaintParam>& paintParamsPtr = nullptr) 73 { 74 if (!focusHub_) { 75 focusHub_ = MakeRefPtr<FocusHub>(WeakClaim(this), type, focusable); 76 focusHub_->SetFocusStyleType(focusStyleType); 77 if (paintParamsPtr) { 78 focusHub_->SetFocusPaintParamsPtr(paintParamsPtr); 79 } 80 } 81 return focusHub_; 82 } 83 GetFocusHub()84 const RefPtr<FocusHub>& GetFocusHub() const 85 { 86 return focusHub_; 87 } 88 89 void AttachHost(const WeakPtr<FrameNode>& host); 90 91 RefPtr<FrameNode> GetFrameNode() const; 92 93 GetEventTargetImpl CreateGetEventTargetImpl() const; 94 OnContextAttached()95 void OnContextAttached() 96 { 97 if (gestureEventHub_) { 98 gestureEventHub_->OnContextAttached(); 99 } 100 } 101 SetOnAppear(std::function<void ()> && onAppear)102 void SetOnAppear(std::function<void()>&& onAppear) 103 { 104 onAppear_ = std::move(onAppear); 105 } FireOnAppear()106 void FireOnAppear() 107 { 108 if (onAppear_) { 109 auto pipeline = PipelineBase::GetCurrentContext(); 110 CHECK_NULL_VOID(pipeline); 111 auto taskScheduler = pipeline->GetTaskExecutor(); 112 CHECK_NULL_VOID(taskScheduler); 113 taskScheduler->PostTask( 114 [weak = WeakClaim(this)]() { 115 auto eventHub = weak.Upgrade(); 116 CHECK_NULL_VOID(eventHub); 117 if (eventHub->onAppear_) { 118 eventHub->onAppear_(); 119 } 120 }, 121 TaskExecutor::TaskType::UI); 122 } 123 } 124 SetOnDisappear(std::function<void ()> && onDisappear)125 void SetOnDisappear(std::function<void()>&& onDisappear) 126 { 127 onDisappear_ = std::move(onDisappear); 128 } FireOnDisappear()129 void FireOnDisappear() 130 { 131 if (onDisappear_) { 132 onDisappear_(); 133 } 134 } 135 SetOnAreaChanged(OnAreaChangedFunc && onAreaChanged)136 void SetOnAreaChanged(OnAreaChangedFunc&& onAreaChanged) 137 { 138 onAreaChanged_ = std::move(onAreaChanged); 139 } 140 FireOnAreaChanged(const RectF & oldRect,const OffsetF & oldOrigin,const RectF & rect,const OffsetF & origin)141 void FireOnAreaChanged(const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin) 142 { 143 if (onAreaChanged_) { 144 onAreaChanged_(oldRect, oldOrigin, rect, origin); 145 } 146 } 147 HasOnAreaChanged()148 bool HasOnAreaChanged() const 149 { 150 return static_cast<bool>(onAreaChanged_); 151 } 152 153 using OnDragFunc = std::function<void(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&)>; 154 using OnDragStartFunc = std::function<DragDropInfo(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&)>; SetOnDragStart(OnDragStartFunc && onDragStart)155 void SetOnDragStart(OnDragStartFunc&& onDragStart) 156 { 157 onDragStart_ = std::move(onDragStart); 158 } 159 GetOnDragStart()160 const OnDragStartFunc& GetOnDragStart() const 161 { 162 return onDragStart_; 163 } 164 HasOnDragStart()165 bool HasOnDragStart() const 166 { 167 return static_cast<bool>(onDragStart_); 168 } 169 SetOnDragEnter(OnDragFunc && onDragEnter)170 void SetOnDragEnter(OnDragFunc&& onDragEnter) 171 { 172 onDragEnter_ = std::move(onDragEnter); 173 } 174 FireOnDragEnter(const RefPtr<OHOS::Ace::DragEvent> & info,const std::string & extraParams)175 void FireOnDragEnter(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams) 176 { 177 if (onDragEnter_) { 178 onDragEnter_(info, extraParams); 179 } 180 } 181 SetOnDragLeave(OnDragFunc && onDragLeave)182 void SetOnDragLeave(OnDragFunc&& onDragLeave) 183 { 184 onDragLeave_ = std::move(onDragLeave); 185 } 186 FireOnDragLeave(const RefPtr<OHOS::Ace::DragEvent> & info,const std::string & extraParams)187 void FireOnDragLeave(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams) 188 { 189 if (onDragLeave_) { 190 onDragLeave_(info, extraParams); 191 } 192 } 193 SetOnDragMove(OnDragFunc && onDragMove)194 void SetOnDragMove(OnDragFunc&& onDragMove) 195 { 196 onDragMove_ = std::move(onDragMove); 197 } 198 FireOnDragMove(const RefPtr<OHOS::Ace::DragEvent> & info,const std::string & extraParams)199 void FireOnDragMove(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams) 200 { 201 if (onDragMove_) { 202 onDragMove_(info, extraParams); 203 } 204 } 205 SetOnDrop(OnDragFunc && onDrop)206 void SetOnDrop(OnDragFunc&& onDrop) 207 { 208 onDrop_ = std::move(onDrop); 209 } 210 FireOnDrop(const RefPtr<OHOS::Ace::DragEvent> & info,const std::string & extraParams)211 void FireOnDrop(const RefPtr<OHOS::Ace::DragEvent>& info, const std::string& extraParams) 212 { 213 if (onDrop_) { 214 onDrop_(info, extraParams); 215 } 216 } 217 HasOnDrop()218 bool HasOnDrop() const 219 { 220 return static_cast<bool>(onDrop_); 221 } 222 GetDragExtraParams(const std::string & extraInfo,const Point & point,DragEventType isStart)223 virtual std::string GetDragExtraParams(const std::string& extraInfo, const Point& point, DragEventType isStart) 224 { 225 auto json = JsonUtil::Create(true); 226 if (!extraInfo.empty()) { 227 json->Put("extraInfo", extraInfo.c_str()); 228 } 229 return json->ToString(); 230 } 231 IsEnabled()232 bool IsEnabled() const 233 { 234 return enabled_; 235 } 236 SetEnabled(bool enabled)237 void SetEnabled(bool enabled) 238 { 239 enabled_ = enabled; 240 } 241 // get XTS inspector value ToJsonValue(std::unique_ptr<JsonValue> & json)242 virtual void ToJsonValue(std::unique_ptr<JsonValue>& json) const {} 243 244 void MarkModifyDone(); 245 UpdateCurrentUIState(UIState state)246 void UpdateCurrentUIState(UIState state) 247 { 248 if (stateStyleMgr_) { 249 stateStyleMgr_->UpdateCurrentUIState(state); 250 } 251 } 252 ResetCurrentUIState(UIState state)253 void ResetCurrentUIState(UIState state) 254 { 255 if (stateStyleMgr_) { 256 stateStyleMgr_->ResetCurrentUIState(state); 257 } 258 } 259 GetCurrentUIState()260 UIState GetCurrentUIState() const 261 { 262 return stateStyleMgr_ ? stateStyleMgr_->GetCurrentUIState() : UI_STATE_NORMAL; 263 } 264 265 void AddSupportedState(UIState state); 266 267 void SetSupportedStates(UIState state); 268 269 bool IsCurrentStateOn(UIState state); 270 271 protected: OnModifyDone()272 virtual void OnModifyDone() {} 273 274 private: 275 WeakPtr<FrameNode> host_; 276 RefPtr<GestureEventHub> gestureEventHub_; 277 RefPtr<InputEventHub> inputEventHub_; 278 RefPtr<FocusHub> focusHub_; 279 RefPtr<StateStyleManager> stateStyleMgr_; 280 281 std::function<void()> onAppear_; 282 std::function<void()> onDisappear_; 283 OnAreaChangedFunc onAreaChanged_; 284 285 OnDragStartFunc onDragStart_; 286 OnDragFunc onDragEnter_; 287 OnDragFunc onDragLeave_; 288 OnDragFunc onDragMove_; 289 OnDragFunc onDrop_; 290 291 bool enabled_ { true }; 292 293 ACE_DISALLOW_COPY_AND_MOVE(EventHub); 294 }; 295 296 } // namespace OHOS::Ace::NG 297 298 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_EVENT_HUB_H