• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common/recorder/event_recorder.h"
25 
26 namespace OHOS::Ace::NG {
27 using ChangeAndSubmitEvent = std::function<void(const std::string)>;
28 class SearchEventHub : public EventHub {
29     DECLARE_ACE_TYPE(SearchEventHub, EventHub)
30 
31 public:
32     SearchEventHub() = default;
33 
34     ~SearchEventHub() override = default;
35 
SetOnSubmit(ChangeAndSubmitEvent && submitEvent)36     void SetOnSubmit(ChangeAndSubmitEvent&& submitEvent)
37     {
38         submitEvent_ = std::move(submitEvent);
39     }
40 
SetOnChange(ChangeAndSubmitEvent && changeEvent)41     void SetOnChange(ChangeAndSubmitEvent&& changeEvent)
42     {
43         changeEvent_ = std::move(changeEvent);
44     }
45 
UpdateSubmitEvent(const std::string & value)46     void UpdateSubmitEvent(const std::string& value) const
47     {
48         if (submitEvent_) {
49             submitEvent_(value);
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             }
58             builder.SetEventType(Recorder::EventType::SEARCH_SUBMIT).SetText(value);
59             Recorder::EventRecorder::Get().OnEvent(std::move(builder));
60         }
61     }
62 
63     void UpdateChangeEvent(const std::string& value) const;
64 
SetOnCopy(std::function<void (const std::string &)> && func)65     void SetOnCopy(std::function<void(const std::string&)>&& func)
66     {
67         onCopy_ = std::move(func);
68     }
69 
FireOnCopy(const std::string & value)70     void FireOnCopy(const std::string& value)
71     {
72         if (onCopy_) {
73             onCopy_(value);
74         }
75     }
76 
SetOnCut(std::function<void (const std::string &)> && func)77     void SetOnCut(std::function<void(const std::string&)>&& func)
78     {
79         onCut_ = std::move(func);
80     }
81 
FireOnCut(const std::string & value)82     void FireOnCut(const std::string& value)
83     {
84         if (onCut_) {
85             onCut_(value);
86         }
87     }
88 
SetOnPaste(std::function<void (const std::string &)> && func)89     void SetOnPaste(std::function<void(const std::string&)>&& func)
90     {
91         onPaste_ = std::move(func);
92     }
93 
FireOnPaste(const std::string & value)94     void FireOnPaste(const std::string& value)
95     {
96         if (onPaste_) {
97             onPaste_(value);
98         }
99     }
100 
SetOnPasteWithEvent(std::function<void (const std::string &,NG::TextCommonEvent &)> && func)101     void SetOnPasteWithEvent(std::function<void(const std::string&, NG::TextCommonEvent&)>&& func)
102     {
103         onPasteWithEvent_ = std::move(func);
104     }
105 
FireOnPasteWithEvent(const std::string & value,NG::TextCommonEvent & event)106     void FireOnPasteWithEvent(const std::string& value, NG::TextCommonEvent& event)
107     {
108         if (onPasteWithEvent_) {
109             onPasteWithEvent_(value, event);
110         }
111     }
112 
SetOnChangeEvent(ChangeAndSubmitEvent && onChangeEvent)113     void SetOnChangeEvent(ChangeAndSubmitEvent&& onChangeEvent)
114     {
115         onValueChangeEvent_ = std::move(onChangeEvent);
116     }
117 
CreateGestureEventHub()118     RefPtr<GestureEventHub> CreateGestureEventHub() override
119     {
120         return MakeRefPtr<SearchGestureEventHub>(WeakClaim(this));
121     }
122 
123 private:
124     ChangeAndSubmitEvent submitEvent_;
125     ChangeAndSubmitEvent changeEvent_;
126     ChangeAndSubmitEvent onValueChangeEvent_;
127 
128     std::function<void(const std::string&)> onCopy_;
129     std::function<void(const std::string&)> onCut_;
130     std::function<void(const std::string&)> onPaste_;
131     std::function<void(const std::string&, NG::TextCommonEvent&)> onPasteWithEvent_;
132 
133     ACE_DISALLOW_COPY_AND_MOVE(SearchEventHub);
134 };
135 
136 } // namespace OHOS::Ace::NG
137 
138 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SEARCH_SEARCH_EVENT_HUB_H
139