1 /*
2  * Copyright (c) 2021 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_PICKER_PICKER_COLUMN_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_COLUMN_COMPONENT_H
18 
19 #include "core/accessibility/accessibility_manager.h"
20 #include "core/components/picker/picker_theme.h"
21 #include "core/components/picker/toss_animation_controller.h"
22 #include "core/pipeline/base/component_group.h"
23 #include "core/pipeline/base/constants.h"
24 
25 namespace OHOS::Ace {
26 
27 using ColumnChangeCallback = std::function<void(const std::string&, bool, uint32_t, bool)>;
28 using ColumnFinishCallback = std::function<void(bool)>;
29 
30 class PickerColumnComponent : public ComponentGroup {
31     DECLARE_ACE_TYPE(PickerColumnComponent, ComponentGroup);
32 
33 public:
34     PickerColumnComponent() = default;
35     ~PickerColumnComponent() override = default;
36 
37     RefPtr<RenderNode> CreateRenderNode() override;
38     RefPtr<Element> CreateElement() override;
39 
40     void Initialize();
41 
GetTheme()42     const RefPtr<PickerTheme>& GetTheme() const
43     {
44         return theme_;
45     }
SetTheme(const RefPtr<PickerTheme> & value)46     void SetTheme(const RefPtr<PickerTheme>& value)
47     {
48         theme_ = value;
49     }
50 
ClearOption()51     void ClearOption()
52     {
53         options_.clear();
54     }
55 
AppendOption(const std::string & value)56     void AppendOption(const std::string& value)
57     {
58         options_.emplace_back(value);
59     }
60 
GetOptionCount()61     uint32_t GetOptionCount() const
62     {
63         return options_.size();
64     }
65 
GetOption(uint32_t index)66     std::string GetOption(uint32_t index) const
67     {
68         if (index >= GetOptionCount()) {
69             LOGE("index out of range.");
70             return "";
71         }
72         return options_[index];
73     }
74 
GetCurrentIndex()75     uint32_t GetCurrentIndex() const
76     {
77         return currentIndex_;
78     }
SetCurrentIndex(uint32_t value)79     void SetCurrentIndex(uint32_t value)
80     {
81         currentIndex_ = value;
82     }
83 
GetInDialog()84     bool GetInDialog() const
85     {
86         return inDialog_;
87     }
SetInDialog(bool value)88     void SetInDialog(bool value)
89     {
90         inDialog_ = value;
91     }
92 
GetHasSplitter()93     bool GetHasSplitter() const
94     {
95         return hasSplitter_;
96     }
SetHasSplitter(bool value)97     void SetHasSplitter(bool value)
98     {
99         hasSplitter_ = value;
100     }
101 
GetCurrentText()102     std::string GetCurrentText() const
103     {
104         return GetOption(GetCurrentIndex());
105     }
106 
GetChangeCallback()107     const ColumnChangeCallback& GetChangeCallback() const
108     {
109         return changeCallback_;
110     }
SetChangeCallback(const ColumnChangeCallback & value)111     void SetChangeCallback(const ColumnChangeCallback& value)
112     {
113         changeCallback_ = value;
114     }
115 
GetFinishCallback()116     const ColumnFinishCallback& GetFinishCallback() const
117     {
118         return finishCallback_;
119     }
SetFinishCallback(const ColumnFinishCallback & value)120     void SetFinishCallback(const ColumnFinishCallback& value)
121     {
122         finishCallback_ = value;
123     }
124 
GetColumnTag()125     const std::string& GetColumnTag() const
126     {
127         return columnTag_;
128     }
SetColumnTag(const std::string & value)129     void SetColumnTag(const std::string& value)
130     {
131         columnTag_ = value;
132     }
133 
GetPrefix()134     const std::string& GetPrefix() const
135     {
136         return prefix_;
137     }
SetPrefix(const std::string & value)138     void SetPrefix(const std::string& value)
139     {
140         prefix_ = value;
141     }
142 
GetSuffix()143     const std::string& GetSuffix() const
144     {
145         return suffix_;
146     }
SetSuffix(const std::string & value)147     void SetSuffix(const std::string& value)
148     {
149         suffix_ = value;
150     }
151 
GetWidthRatio()152     uint32_t GetWidthRatio() const
153     {
154         return widthRatio_;
155     }
SetWidthRatio(uint32_t value)156     void SetWidthRatio(uint32_t value)
157     {
158         widthRatio_ = value;
159     }
160 
GetNodeId()161     int32_t GetNodeId() const
162     {
163         return nodeId_;
164     }
SetNodeId(int32_t value)165     void SetNodeId(int32_t value)
166     {
167         nodeId_ = value;
168     }
169 
GetFixHeight()170     const Dimension& GetFixHeight() const
171     {
172         return fixHeight_;
173     }
SetFixHeight(const Dimension & value)174     void SetFixHeight(const Dimension& value)
175     {
176         fixHeight_ = value;
177     }
178 
GetDefaultHeight()179     bool GetDefaultHeight() const
180     {
181         return defaultHeight_;
182     }
183 
SetDefaultHeight(bool value)184     void SetDefaultHeight(bool value)
185     {
186         defaultHeight_ = value;
187     }
188 
HandleChangeCallback(bool isAdd,bool needNotify)189     void HandleChangeCallback(bool isAdd, bool needNotify)
190     {
191         if (changeCallback_) {
192             changeCallback_(GetColumnTag(), isAdd, GetCurrentIndex(), needNotify);
193         } else {
194             LOGE("change callback is null.");
195         }
196     }
197 
HandleFinishCallback(bool success)198     void HandleFinishCallback(bool success)
199     {
200         if (finishCallback_) {
201             finishCallback_(success);
202         } else {
203             LOGE("finish callback is null.");
204         }
205     }
206 
GetRequestFocusCallback()207     const std::function<void()>& GetRequestFocusCallback() const
208     {
209         return requestFocusCallback_;
210     }
SetRequestFocusCallback(const std::function<void ()> & value)211     void SetRequestFocusCallback(const std::function<void()>& value)
212     {
213         requestFocusCallback_ = value;
214     }
215 
HandleRequestFocus()216     void HandleRequestFocus()
217     {
218         if (!requestFocusCallback_) {
219             LOGE("function of request focus is null.");
220             return;
221         }
222 
223         requestFocusCallback_();
224     }
225 
GetToss()226     const RefPtr<TossAnimationController>& GetToss() const
227     {
228         return tossAnimationController_;
229     }
230 
SetNeedVibrate(bool needVibrate)231     void SetNeedVibrate(bool needVibrate)
232     {
233         needVibrate_ = needVibrate;
234     }
235 
GetNeedVibrate()236     bool GetNeedVibrate()
237     {
238         return needVibrate_;
239     }
240 
241 private:
242     uint32_t currentIndex_ = 0;
243 
244     bool inDialog_ = true;
245 
246     bool hasSplitter_ = false;
247 
248     int32_t nodeId_ = -1; // use -1 that is invalidate for default.
249 
250     RefPtr<PickerTheme> theme_;
251 
252     std::vector<std::string> options_;
253 
254     ColumnChangeCallback changeCallback_;
255 
256     ColumnFinishCallback finishCallback_;
257 
258     std::string columnTag_;
259 
260     std::string prefix_;
261     std::string suffix_;
262 
263     Dimension fixHeight_;
264     bool defaultHeight_ = false;
265     uint32_t widthRatio_ = 1; // default column ratio is 1:1
266 
267     std::function<void()> requestFocusCallback_;
268 
269     RefPtr<TossAnimationController> tossAnimationController_ = AceType::MakeRefPtr<TossAnimationController>();
270 
271     bool needVibrate_ = true;
272 };
273 
274 } // namespace OHOS::Ace
275 
276 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_COLUMN_COMPONENT_H
277