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_SWITCH_SWITCH_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_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 26 namespace OHOS::Ace::NG { 27 28 using ChangeEvent = std::function<void(const bool)>; 29 30 class SwitchEventHub : public EventHub { 31 DECLARE_ACE_TYPE(SwitchEventHub, EventHub) 32 33 public: 34 SwitchEventHub() = default; 35 ~SwitchEventHub() override = default; 36 SetOnChange(ChangeEvent && changeEvent)37 void SetOnChange(ChangeEvent&& changeEvent) 38 { 39 changeEvent_ = std::move(changeEvent); 40 } 41 UpdateChangeEvent(bool isOn)42 void UpdateChangeEvent(bool isOn) const 43 { 44 auto task = [changeEvent = changeEvent_, onChangeEvent = onChangeEvent_, isOn]() { 45 if (onChangeEvent) { 46 onChangeEvent(isOn); 47 } 48 if (changeEvent) { 49 changeEvent(isOn); 50 } 51 }; 52 auto context = PipelineBase::GetCurrentContext(); 53 CHECK_NULL_VOID(context); 54 context->PostAsyncEvent(task, "ArkUIToggleUpdateChangeEvent", TaskExecutor::TaskType::UI); 55 56 if (Recorder::EventRecorder::Get().IsComponentRecordEnable()) { 57 Recorder::EventParamsBuilder builder; 58 auto host = GetFrameNode(); 59 if (host) { 60 auto id = host->GetInspectorIdValue(""); 61 builder.SetId(id).SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue("")); 62 if (!id.empty()) { 63 Recorder::NodeDataCache::Get().PutBool(host, id, isOn); 64 } 65 } 66 builder.SetChecked(isOn); 67 Recorder::EventRecorder::Get().OnChange(std::move(builder)); 68 } 69 } 70 SetOnChangeEvent(ChangeEvent && onChangeEvent)71 void SetOnChangeEvent(ChangeEvent&& onChangeEvent) 72 { 73 onChangeEvent_ = std::move(onChangeEvent); 74 } 75 76 private: 77 ChangeEvent changeEvent_; 78 ChangeEvent onChangeEvent_; 79 80 ACE_DISALLOW_COPY_AND_MOVE(SwitchEventHub); 81 }; 82 } // namespace OHOS::Ace::NG 83 84 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWITCH_SWITCH_EVENT_HUB_H 85