• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CHECKBOX_CHECKBOX_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_EVENT_HUB_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/event/event_hub.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/gesture_event_hub.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 using ChangeEvent = std::function<void(const bool)>;
29 
30 class CheckBoxEventHub : public EventHub {
31     DECLARE_ACE_TYPE(CheckBoxEventHub, EventHub)
32 
33 public:
34     CheckBoxEventHub() = default;
35 
36     ~CheckBoxEventHub() 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 (selectChangeEvent_) {
46             selectChangeEvent_(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().PutMultiple(host, id, name_, select);
62                 }
63             }
64             builder.SetChecked(select).SetText(name_);
65             Recorder::EventRecorder::Get().OnChange(std::move(builder));
66         }
67     }
68 
GetName()69     const std::string& GetName()
70     {
71         return name_;
72     }
73 
GetGroupName()74     const std::string& GetGroupName()
75     {
76         return groupname_;
77     }
78 
SetName(const std::string & name)79     void SetName(const std::string& name)
80     {
81         name_ = name;
82     }
83 
SetGroupName(const std::string & groupname)84     void SetGroupName(const std::string& groupname)
85     {
86         groupname_ = groupname;
87     }
88 
SetChangeEvent(ChangeEvent && changeEvent)89     void SetChangeEvent(ChangeEvent&& changeEvent)
90     {
91         selectChangeEvent_ = std::move(changeEvent);
92     }
93 
94 private:
95     ChangeEvent changeEvent_;
96     std::string name_;
97     std::string groupname_;
98     ChangeEvent selectChangeEvent_;
99 
100     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxEventHub);
101 };
102 
103 } // namespace OHOS::Ace::NG
104 
105 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_EVENT_HUB_H
106