1 /* 2 * Copyright (c) 2022-2023 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_SEARCH_SEARCH_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SEARCH_SEARCH_EVENT_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/components_ng/base/frame_node.h" 21 #include "core/components_ng/event/event_hub.h" 22 #include "core/components_ng/event/gesture_event_hub.h" 23 #include "core/components_ng/pattern/search/search_gesture_event_hub.h" 24 #include "core/components_ng/pattern/text_field/text_field_event_hub.h" 25 #include "core/common/recorder/event_recorder.h" 26 27 namespace OHOS::Ace::NG { 28 using ChangeAndSubmitEvent = std::function<void(const std::string)>; 29 class SearchEventHub : public EventHub { 30 DECLARE_ACE_TYPE(SearchEventHub, EventHub) 31 32 public: 33 SearchEventHub() = default; 34 35 ~SearchEventHub() override = default; 36 SetOnSubmit(std::function<void (const std::string &,NG::TextFieldCommonEvent &)> && func)37 void SetOnSubmit(std::function<void(const std::string&, NG::TextFieldCommonEvent&)>&& func) 38 { 39 onSubmit_ = std::move(func); 40 } 41 FireOnSubmit(const std::string & value,NG::TextFieldCommonEvent & event)42 void FireOnSubmit(const std::string& value, NG::TextFieldCommonEvent& event) 43 { 44 if (onSubmit_) { 45 event.SetText(value); 46 onSubmit_(value, event); 47 } 48 if (Recorder::EventRecorder::Get().IsComponentRecordEnable()) { 49 Recorder::EventParamsBuilder builder; 50 auto host = GetFrameNode(); 51 if (host) { 52 auto id = host->GetInspectorIdValue(""); 53 builder.SetId(id).SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue("")); 54 } 55 builder.SetEventType(Recorder::EventType::SEARCH_SUBMIT).SetText(value); 56 Recorder::EventRecorder::Get().OnEvent(std::move(builder)); 57 } 58 } 59 SetOnChange(ChangeAndSubmitEvent && changeEvent)60 void SetOnChange(ChangeAndSubmitEvent&& changeEvent) 61 { 62 changeEvent_ = std::move(changeEvent); 63 } 64 65 void UpdateChangeEvent(const std::string& value) const; 66 SetOnCopy(std::function<void (const std::string &)> && func)67 void SetOnCopy(std::function<void(const std::string&)>&& func) 68 { 69 onCopy_ = std::move(func); 70 } 71 FireOnCopy(const std::string & value)72 void FireOnCopy(const std::string& value) 73 { 74 if (onCopy_) { 75 onCopy_(value); 76 } 77 } 78 SetOnCut(std::function<void (const std::string &)> && func)79 void SetOnCut(std::function<void(const std::string&)>&& func) 80 { 81 onCut_ = std::move(func); 82 } 83 FireOnCut(const std::string & value)84 void FireOnCut(const std::string& value) 85 { 86 if (onCut_) { 87 onCut_(value); 88 } 89 } 90 SetOnPaste(std::function<void (const std::string &)> && func)91 void SetOnPaste(std::function<void(const std::string&)>&& func) 92 { 93 onPaste_ = std::move(func); 94 } 95 FireOnPaste(const std::string & value)96 void FireOnPaste(const std::string& value) 97 { 98 if (onPaste_) { 99 onPaste_(value); 100 } 101 } 102 SetOnPasteWithEvent(std::function<void (const std::string &,NG::TextCommonEvent &)> && func)103 void SetOnPasteWithEvent(std::function<void(const std::string&, NG::TextCommonEvent&)>&& func) 104 { 105 onPasteWithEvent_ = std::move(func); 106 } 107 FireOnPasteWithEvent(const std::string & value,NG::TextCommonEvent & event)108 void FireOnPasteWithEvent(const std::string& value, NG::TextCommonEvent& event) 109 { 110 if (onPasteWithEvent_) { 111 onPasteWithEvent_(value, event); 112 } 113 } 114 SetOnChangeEvent(ChangeAndSubmitEvent && onChangeEvent)115 void SetOnChangeEvent(ChangeAndSubmitEvent&& onChangeEvent) 116 { 117 onValueChangeEvent_ = std::move(onChangeEvent); 118 } 119 CreateGestureEventHub()120 RefPtr<GestureEventHub> CreateGestureEventHub() override 121 { 122 return MakeRefPtr<SearchGestureEventHub>(WeakClaim(this)); 123 } 124 125 private: 126 ChangeAndSubmitEvent changeEvent_; 127 ChangeAndSubmitEvent onValueChangeEvent_; 128 129 std::function<void(const std::string&)> onCopy_; 130 std::function<void(const std::string&)> onCut_; 131 std::function<void(const std::string&)> onPaste_; 132 std::function<void(const std::string&, NG::TextCommonEvent&)> onPasteWithEvent_; 133 std::function<void(const std::string&, NG::TextFieldCommonEvent&)> onSubmit_; 134 135 ACE_DISALLOW_COPY_AND_MOVE(SearchEventHub); 136 }; 137 138 } // namespace OHOS::Ace::NG 139 140 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SEARCH_SEARCH_EVENT_HUB_H 141