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_PATTERNS_NAVROUTER_NAVDESTINATION_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_EVENT_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "bridge/declarative_frontend/engine/js_ref_ptr.h" 21 #include "core/components_ng/event/event_hub.h" 22 #include "core/components_ng/event/gesture_event_hub.h" 23 #include "frameworks/bridge/declarative_frontend/engine/js_types.h" 24 25 namespace OHOS::Ace::NG { 26 using OnStateChangeEvent = std::function<void(bool)>; 27 using namespace Framework; 28 class NavDestinationEventHub : public EventHub { DECLARE_ACE_TYPE(NavDestinationEventHub,EventHub)29 DECLARE_ACE_TYPE(NavDestinationEventHub, EventHub) 30 public: 31 void SetOnStateChange(const OnStateChangeEvent& changeEvent) 32 { 33 onStateChangeEvent_ = changeEvent; 34 } 35 FireChangeEvent(bool isActivated)36 void FireChangeEvent(bool isActivated) 37 { 38 if (isActivated_ != isActivated) { 39 if (onStateChangeEvent_) { 40 onStateChangeEvent_(isActivated); 41 } 42 } 43 isActivated_ = isActivated; 44 } 45 SetOnShown(const std::function<void ()> & onShown)46 void SetOnShown(const std::function<void()>& onShown) 47 { 48 onShownEvent_ = onShown; 49 } 50 FireOnShownEvent()51 void FireOnShownEvent() const 52 { 53 if (onShownEvent_) { 54 auto onShownEvent = onShownEvent_; 55 onShownEvent(); 56 } 57 } 58 SetOnHidden(const std::function<void ()> & onHidden)59 void SetOnHidden(const std::function<void()>& onHidden) 60 { 61 onHiddenEvent_ = onHidden; 62 } 63 FireOnHiddenEvent()64 void FireOnHiddenEvent() const 65 { 66 if (onHiddenEvent_) { 67 onHiddenEvent_(); 68 } 69 } 70 SetOnBackPressed(const std::function<bool ()> & onBackPressed)71 void SetOnBackPressed(const std::function<bool()>& onBackPressed) 72 { 73 onBackPressedEvent_ = onBackPressed; 74 } 75 GetOnBackPressedEvent()76 std::function<bool()> GetOnBackPressedEvent() const 77 { 78 return onBackPressedEvent_; 79 } 80 FireOnBackPressedEvent()81 bool FireOnBackPressedEvent() 82 { 83 if (onBackPressedEvent_) { 84 return onBackPressedEvent_(); 85 } 86 return false; 87 } 88 89 private: 90 OnStateChangeEvent onStateChangeEvent_; 91 std::function<void()> onShownEvent_; 92 std::function<void()> onHiddenEvent_; 93 std::function<bool()> onBackPressedEvent_; 94 95 bool isActivated_ = false; 96 }; 97 } // namespace OHOS::Ace::NG 98 99 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVROUTER_NAVDESTINATION_EVENT_HUB_H 100