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_NAVIGATION_NAVIGATION_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_EVENT_HUB_H 18 19 #include <optional> 20 21 #include "base/memory/ace_type.h" 22 #include "core/components_ng/event/event_hub.h" 23 #include "core/components_ng/event/gesture_event_hub.h" 24 #include "core/components_ng/pattern/navigation/navigation_declaration.h" 25 26 namespace OHOS::Ace::NG { 27 28 using OnTitleModeChangeEvent = std::function<void(const BaseEventInfo* eventInfo)>; 29 using OnNavBarStateChangeEvent = std::function<void(bool)>; 30 31 class NavigationEventHub : public EventHub { DECLARE_ACE_TYPE(NavigationEventHub,EventHub)32 DECLARE_ACE_TYPE(NavigationEventHub, EventHub) 33 public: 34 void SetOnTitleModeChange(OnTitleModeChangeEvent&& changeEvent) 35 { 36 onTitleModeChangeEvent_ = changeEvent; 37 } 38 FireChangeEvent(const BaseEventInfo * eventInfo)39 void FireChangeEvent(const BaseEventInfo* eventInfo) const 40 { 41 if (onTitleModeChangeEvent_) { 42 onTitleModeChangeEvent_(eventInfo); 43 } 44 } 45 SetOnNavBarStateChange(OnNavBarStateChangeEvent && changeEvent)46 void SetOnNavBarStateChange(OnNavBarStateChangeEvent&& changeEvent) 47 { 48 onNavBarStateChangeEvent_ = changeEvent; 49 } 50 FireNavBarStateChangeEvent(bool isVisible)51 void FireNavBarStateChangeEvent(bool isVisible) 52 { 53 if (isVisible_.has_value()) { 54 if (isVisible_.value() != isVisible) { 55 if (onNavBarStateChangeEvent_) { 56 onNavBarStateChangeEvent_(isVisible); 57 } 58 } 59 } else { 60 if (onNavBarStateChangeEvent_) { 61 onNavBarStateChangeEvent_(isVisible); 62 } 63 } 64 isVisible_ = isVisible; 65 } 66 67 private: 68 OnTitleModeChangeEvent onTitleModeChangeEvent_; 69 OnNavBarStateChangeEvent onNavBarStateChangeEvent_; 70 71 std::optional<bool> isVisible_; 72 }; 73 74 } // namespace OHOS::Ace::NG 75 76 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_NAVIGATION_NAVIGATION_EVENT_HUB_H 77