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_BUTTON_TOGGLE_BUTTON_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_TOGGLE_BUTTON_EVENT_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "base/utils/noncopyable.h" 21 #include "core/common/recorder/event_recorder.h" 22 #include "core/common/recorder/node_data_cache.h" 23 #include "core/components_ng/base/frame_node.h" 24 #include "core/components_ng/event/event_hub.h" 25 #include "core/components_ng/pattern/button/button_event_hub.h" 26 27 namespace OHOS::Ace::NG { 28 29 using ChangeEvent = std::function<void(const bool)>; 30 31 class ToggleButtonEventHub : public ButtonEventHub { 32 DECLARE_ACE_TYPE(ToggleButtonEventHub, ButtonEventHub); 33 34 public: 35 ToggleButtonEventHub() = default; 36 ~ToggleButtonEventHub() override = default; 37 SetOnChange(ChangeEvent && changeEvent)38 void SetOnChange(ChangeEvent&& changeEvent) 39 { 40 changeEvent_ = std::move(changeEvent); 41 } 42 UpdateChangeEvent(bool select)43 void UpdateChangeEvent(bool select) const 44 { 45 if (onChangeEvent_) { 46 onChangeEvent_(select); 47 } 48 if (changeEvent_) { 49 changeEvent_(select); 50 } 51 if (Recorder::EventRecorder::Get().IsComponentRecordEnable()) { 52 Recorder::EventParamsBuilder builder; 53 auto host = GetFrameNode(); 54 if (host) { 55 auto id = host->GetInspectorIdValue(""); 56 builder.SetId(id) 57 .SetType(host->GetHostTag()) 58 .SetDescription(host->GetAutoEventParamValue("")) 59 .SetHost(host); 60 if (!id.empty()) { 61 Recorder::NodeDataCache::Get().PutBool(host, id, select); 62 } 63 } 64 builder.SetChecked(select); 65 Recorder::EventRecorder::Get().OnChange(std::move(builder)); 66 } 67 } 68 SetOnChangeEvent(ChangeEvent && onChangeEvent)69 void SetOnChangeEvent(ChangeEvent&& onChangeEvent) 70 { 71 onChangeEvent_ = std::move(onChangeEvent); 72 } 73 74 private: 75 ChangeEvent changeEvent_; 76 ChangeEvent onChangeEvent_; 77 78 ACE_DISALLOW_COPY_AND_MOVE(ToggleButtonEventHub); 79 }; 80 } // namespace OHOS::Ace::NG 81 82 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_TOGGLE_BUTTON_EVENT_HUB_H 83