• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()>;
26 using ExternalEvent = std::function<void(const std::string&, const uint32_t, const bool)>;
27 
28 class XComponentEventHub : public EventHub {
29     DECLARE_ACE_TYPE(XComponentEventHub, EventHub)
30 
31 public:
32     XComponentEventHub() = default;
33     ~XComponentEventHub() override = default;
34 
SetOnLoad(LoadEvent && loadEvent)35     void SetOnLoad(LoadEvent&& loadEvent)
36     {
37         loadEvent_ = std::move(loadEvent);
38     }
39 
FireLoadEvent(const std::string & xcomponentId)40     void FireLoadEvent(const std::string& xcomponentId) const
41     {
42         if (loadEvent_) {
43             loadEvent_(xcomponentId);
44         }
45     }
46 
SetOnDestroy(DestroyEvent && destroyEvent)47     void SetOnDestroy(DestroyEvent&& destroyEvent)
48     {
49         destroyEvent_ = std::move(destroyEvent);
50     }
51 
FireDestroyEvent()52     void FireDestroyEvent() const
53     {
54         if (destroyEvent_) {
55             destroyEvent_();
56         }
57     }
58 
SetOnSurfaceInitEvent(ExternalEvent && surfaceInitEvent)59     void SetOnSurfaceInitEvent(ExternalEvent&& surfaceInitEvent)
60     {
61         surfaceInitEvent_ = std::move(surfaceInitEvent);
62     }
63 
FireSurfaceInitEvent(const std::string & componentId,const uint32_t nodeId)64     void FireSurfaceInitEvent(const std::string& componentId, const uint32_t nodeId) const
65     {
66         if (surfaceInitEvent_) {
67             surfaceInitEvent_(componentId, nodeId, false);
68         }
69     }
70 
71 private:
72     LoadEvent loadEvent_;
73     DestroyEvent destroyEvent_;
74     ExternalEvent surfaceInitEvent_;
75 };
76 } // namespace OHOS::Ace::NG
77 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H
78