1 /* 2 * Copyright (c) 2022-2023 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_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/components_ng/event/event_hub.h" 21 #include "core/components_ng/event/gesture_event_hub.h" 22 23 namespace OHOS::Ace::NG { 24 using LoadEvent = std::function<void(const std::string&)>; 25 using DestroyEvent = std::function<void(const std::string&)>; 26 using ExternalEvent = std::function<void(const std::string&, const uint32_t, const bool)>; 27 using DetachEvent = std::function<void(const std::string&)>; 28 using SurfaceCreatedEvent = std::function<void(const std::string&, const std::string&)>; 29 using SurfaceChangedEvent = std::function<void(const std::string&, const RectF&)>; 30 using SurfaceDestroyedEvent = std::function<void(const std::string&, const std::string&)>; 31 32 class XComponentEventHub : public EventHub { 33 DECLARE_ACE_TYPE(XComponentEventHub, EventHub) 34 35 public: 36 XComponentEventHub() = default; 37 ~XComponentEventHub() override = default; 38 SetOnLoad(LoadEvent && loadEvent)39 void SetOnLoad(LoadEvent&& loadEvent) 40 { 41 loadEvent_ = std::move(loadEvent); 42 } 43 FireLoadEvent(const std::string & xcomponentId)44 void FireLoadEvent(const std::string& xcomponentId) const 45 { 46 CHECK_NULL_VOID(loadEvent_); 47 loadEvent_(xcomponentId); 48 } 49 SetOnDestroy(DestroyEvent && destroyEvent)50 void SetOnDestroy(DestroyEvent&& destroyEvent) 51 { 52 destroyEvent_ = std::move(destroyEvent); 53 } 54 FireDestroyEvent(const std::string & xcomponentId)55 void FireDestroyEvent(const std::string& xcomponentId) const 56 { 57 CHECK_NULL_VOID(destroyEvent_); 58 destroyEvent_(xcomponentId); 59 } 60 SetOnSurfaceInitEvent(ExternalEvent && surfaceInitEvent)61 void SetOnSurfaceInitEvent(ExternalEvent&& surfaceInitEvent) 62 { 63 surfaceInitEvent_ = std::move(surfaceInitEvent); 64 } 65 FireSurfaceInitEvent(const std::string & componentId,const uint32_t nodeId)66 void FireSurfaceInitEvent(const std::string& componentId, const uint32_t nodeId) const 67 { 68 CHECK_NULL_VOID(surfaceInitEvent_); 69 surfaceInitEvent_(componentId, nodeId, false); 70 } 71 SetDetachEvent(DetachEvent && detachEvent)72 void SetDetachEvent(DetachEvent&& detachEvent) 73 { 74 detachEvent_ = std::move(detachEvent); 75 } 76 FireDetachEvent(const std::string & componentId)77 void FireDetachEvent(const std::string& componentId) 78 { 79 CHECK_NULL_VOID(detachEvent_); 80 detachEvent_(componentId); 81 } 82 SetControllerCreatedEvent(SurfaceCreatedEvent && controllerCreatedEvent)83 void SetControllerCreatedEvent(SurfaceCreatedEvent&& controllerCreatedEvent) 84 { 85 controllerCreatedEvent_ = std::move(controllerCreatedEvent); 86 } 87 FireControllerCreatedEvent(const std::string & surfaceId,const std::string & xcomponentId)88 void FireControllerCreatedEvent(const std::string& surfaceId, const std::string& xcomponentId) const 89 { 90 CHECK_NULL_VOID(controllerCreatedEvent_); 91 controllerCreatedEvent_(surfaceId, xcomponentId); 92 } 93 SetControllerChangedEvent(SurfaceChangedEvent && controllerChangedEvent)94 void SetControllerChangedEvent(SurfaceChangedEvent&& controllerChangedEvent) 95 { 96 controllerChangedEvent_ = std::move(controllerChangedEvent); 97 } 98 FireControllerChangedEvent(const std::string & surfaceId,const RectF & rect)99 void FireControllerChangedEvent(const std::string& surfaceId, const RectF& rect) const 100 { 101 CHECK_NULL_VOID(controllerChangedEvent_); 102 controllerChangedEvent_(surfaceId, rect); 103 } 104 SetControllerDestroyedEvent(SurfaceDestroyedEvent && controllerDestroyedEvent)105 void SetControllerDestroyedEvent(SurfaceDestroyedEvent&& controllerDestroyedEvent) 106 { 107 controllerDestroyedEvent_ = std::move(controllerDestroyedEvent); 108 } 109 FireControllerDestroyedEvent(const std::string & surfaceId,const std::string & xcomponentId)110 void FireControllerDestroyedEvent(const std::string& surfaceId, const std::string& xcomponentId) const 111 { 112 CHECK_NULL_VOID(controllerDestroyedEvent_); 113 controllerDestroyedEvent_(surfaceId, xcomponentId); 114 } 115 116 private: 117 LoadEvent loadEvent_; 118 DestroyEvent destroyEvent_; 119 ExternalEvent surfaceInitEvent_; 120 DetachEvent detachEvent_; 121 SurfaceCreatedEvent controllerCreatedEvent_; 122 SurfaceChangedEvent controllerChangedEvent_; 123 SurfaceDestroyedEvent controllerDestroyedEvent_; 124 }; 125 } // namespace OHOS::Ace::NG 126 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 127