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).SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue("")); 57 if (!id.empty()) { 58 Recorder::NodeDataCache::Get().PutBool(host, id, select); 59 } 60 } 61 builder.SetChecked(select); 62 Recorder::EventRecorder::Get().OnChange(std::move(builder)); 63 } 64 } 65 SetOnChangeEvent(ChangeEvent && onChangeEvent)66 void SetOnChangeEvent(ChangeEvent&& onChangeEvent) 67 { 68 onChangeEvent_ = std::move(onChangeEvent); 69 } 70 71 private: 72 ChangeEvent changeEvent_; 73 ChangeEvent onChangeEvent_; 74 75 ACE_DISALLOW_COPY_AND_MOVE(ToggleButtonEventHub); 76 }; 77 } // namespace OHOS::Ace::NG 78 79 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_TOGGLE_BUTTON_EVENT_HUB_H 80