1 /* 2 * Copyright (c) 2025 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_SELECT_OVERLAY_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_OVERLAY_EVENT_HUB_H 18 19 #include "core/components_ng/event/event_hub.h" 20 21 namespace OHOS::Ace::NG { 22 class SelectOverlayEventHub : public EventHub { 23 DECLARE_ACE_TYPE(SelectOverlayEventHub, EventHub); 24 25 public: 26 SelectOverlayEventHub() = default; 27 ~SelectOverlayEventHub() override = default; 28 SetMenuAppearCallback(std::function<void ()> && onAppear)29 void SetMenuAppearCallback(std::function<void()>&& onAppear) 30 { 31 onAppear_ = std::move(onAppear); 32 } 33 FireAppearEvent()34 void FireAppearEvent() 35 { 36 if (onAppear_) { 37 onAppear_(); 38 } 39 } 40 SetMenuDisappearCallback(std::function<void ()> && onDisappear)41 void SetMenuDisappearCallback(std::function<void()>&& onDisappear) 42 { 43 onDisappear_ = std::move(onDisappear); 44 } 45 FireDisappearEvent()46 void FireDisappearEvent() 47 { 48 if (onDisappear_) { 49 onDisappear_(); 50 } 51 } 52 SetMenuShowCallback(std::function<void ()> && onMenuShow)53 void SetMenuShowCallback(std::function<void()>&& onMenuShow) 54 { 55 onMenuShowCallback_ = std::move(onMenuShow); 56 } 57 SetMenuHideCallback(std::function<void ()> && onMenuHide)58 void SetMenuHideCallback(std::function<void()>&& onMenuHide) 59 { 60 onMenuHideCallback_ = std::move(onMenuHide); 61 } 62 FireMenuVisibilityChangeEvent(bool isMenuShow)63 void FireMenuVisibilityChangeEvent(bool isMenuShow) 64 { 65 if (lastMenuIsShow_.has_value() && lastMenuIsShow_.value() == isMenuShow) { 66 return; 67 } 68 if (isMenuShow && onMenuShowCallback_) { 69 onMenuShowCallback_(); 70 } else if (!isMenuShow && onMenuHideCallback_) { 71 onMenuHideCallback_(); 72 } 73 lastMenuIsShow_ = isMenuShow; 74 } 75 private: 76 std::optional<bool> lastMenuIsShow_; 77 std::function<void()> onAppear_; 78 std::function<void()> onDisappear_; 79 std::function<void()> onMenuShowCallback_; 80 std::function<void()> onMenuHideCallback_; 81 ACE_DISALLOW_COPY_AND_MOVE(SelectOverlayEventHub); 82 }; 83 } // namespace OHOS::Ace::NG 84 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_OVERLAY_EVENT_HUB_H 85