1 /*
2  * Copyright (c) 2021-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_TEXT_FIELD_TEXT_FIELD_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_TEXT_FIELD_COMPONENT_H
18 
19 #include "base/memory/referenced.h"
20 #include "base/utils/label_target.h"
21 #include "core/common/ime/text_edit_controller.h"
22 #include "core/common/ime/text_input_action.h"
23 #include "core/common/ime/text_input_proxy.h"
24 #include "core/components/common/layout/constants.h"
25 #include "core/components/common/properties/color.h"
26 #include "core/components/common/properties/decoration.h"
27 #include "core/components/common/properties/input_option.h"
28 #include "core/components/common/properties/text_style.h"
29 #include "core/components/declaration/textfield/textfield_declaration.h"
30 #include "core/components/image/image_component.h"
31 #include "core/components/text_field/text_field_controller.h"
32 #include "core/components/text_overlay/text_overlay_manager.h"
33 #include "core/event/ace_event_handler.h"
34 #include "core/pipeline/base/render_component.h"
35 #include "frameworks/bridge/common/dom/dom_input.h"
36 
37 namespace OHOS::Ace {
38 
39 class RenderNode;
40 class Element;
41 
42 class ACE_EXPORT TextFieldComponent : public RenderComponent, public LabelTarget {
43     DECLARE_ACE_TYPE(TextFieldComponent, RenderComponent, LabelTarget);
44 
45 public:
46     TextFieldComponent();
47     ~TextFieldComponent() override = default;
48 
49     RefPtr<RenderNode> CreateRenderNode() override;
50     RefPtr<Element> CreateElement() override;
51 
52     const std::string& GetValue() const;
53     void SetValue(const std::string& value);
54 
55     const std::string& GetPlaceholder() const;
56     void SetPlaceholder(const std::string& placeholder);
57 
58     const Color& GetPlaceholderColor() const;
59     void SetPlaceholderColor(const Color& placeholderColor);
60 
61     void SetTextMaxLines(uint32_t textMaxLines);
62     TextAlign GetTextAlign() const;
63 
64     void SetTextAlign(TextAlign textAlign);
65     uint32_t GetTextMaxLines() const;
66 
67     const TextStyle& GetTextStyle() const;
68     void SetTextStyle(const TextStyle& textStyle);
69 
70     const TextStyle& GetErrorTextStyle() const;
71     void SetErrorTextStyle(const TextStyle& errorTextStyle);
72 
73     const Dimension& GetErrorSpacing() const;
74     void SetErrorSpacing(const Dimension& errorSpacing);
75 
76     bool GetErrorIsInner() const;
77     void SetErrorIsInner(bool errorIsInner);
78 
79     const Dimension& GetErrorBorderWidth() const;
80     void SetErrorBorderWidth(const Dimension& errorBorderWidth);
81 
82     const Color& GetErrorBorderColor() const;
83     void SetErrorBorderColor(const Color& errorBorderColor);
84 
85     bool NeedFade() const;
86     void SetNeedFade(bool needFade);
87 
88     RefPtr<Decoration> GetDecoration() const;
89     void SetDecoration(const RefPtr<Decoration>& decoration);
90 
91     void SetOriginBorder(const Border& originBorder);
92     const Border& GetOriginBorder() const;
93 
94     bool ShowCursor() const;
95     void SetShowCursor(bool show);
96 
97     bool NeedObscure() const;
98     void SetObscure(bool obscure);
99 
100     bool IsEnabled() const;
101     void SetEnabled(bool enable);
102 
103     TextInputType GetTextInputType() const;
104     void SetTextInputType(TextInputType type);
105 
106     TextInputAction GetAction() const;
107     void SetAction(TextInputAction action);
108 
109     void SetCursorColor(const Color& color);
110     const Color& GetCursorColor();
111 
112     void SetCursorRadius(const Dimension& radius);
113     const Dimension& GetCursorRadius() const;
114 
115     bool IsCursorColorSet() const;
116 
117     const std::string& GetActionLabel() const;
118     void SetActionLabel(const std::string& actionLabel);
119 
120     uint32_t GetMaxLength() const;
121     void SetMaxLength(uint32_t maxLength);
122 
123     bool IsTextLengthLimited() const;
124 
125     const Dimension& GetHeight() const;
126     void SetHeight(const Dimension& height);
127 
128     bool GetAutoFocus() const;
129     void SetAutoFocus(bool autoFocus);
130 
131     bool IsExtend() const;
132     void SetExtend(bool extend);
133 
134     InputStyle GetInputStyle() const;
135     void SetInputStyle(InputStyle style);
136 
137     bool ShowEllipsis() const;
138     void SetShowEllipsis(bool showEllipsis);
139 
140     bool IsSoftKeyboardEnabled() const;
141     void SetSoftKeyboardEnabled(bool softKeyboardEnabled);
142 
143     bool ShowPasswordIcon() const;
144     void SetShowPasswordIcon(bool showEllipsis);
145 
146     const std::optional<Color>& GetImageFill() const;
147     void SetImageFill(const std::optional<Color>& color);
148 
149     const std::string& GetIconImage() const;
150     void SetIconImage(const std::string& iconImage);
151 
152     const std::string& GetShowIconImage() const;
153     void SetShowIconImage(const std::string& showImage);
154 
155     const std::string& GetHideIconImage() const;
156     void SetHideIconImage(const std::string& hideImage);
157 
158     const Dimension& GetIconSize() const;
159     void SetIconSize(const Dimension& iconSize);
160 
161     const Dimension& GetIconHotZoneSize() const;
162     void SetIconHotZoneSize(const Dimension& iconHotZoneSize);
163 
164     RefPtr<TextEditController> GetTextEditController() const;
165     void SetTextEditController(const RefPtr<TextEditController>& controller);
166 
167     RefPtr<TextFieldController> GetTextFieldController() const;
168     void SetTextFieldController(const RefPtr<TextFieldController>& controller);
169 
170     void SetFocusBgColor(const Color& focusBgColor);
171     const Color& GetFocusBgColor();
172 
173     void SetFocusPlaceholderColor(const Color& focusPlaceholderColor);
174     const Color& GetFocusPlaceholderColor();
175 
176     void SetFocusTextColor(const Color& focusTextColor);
177     const Color& GetFocusTextColor();
178 
179     void SetBgColor(const Color& bgColor);
180     const Color& GetBgColor();
181 
182     void SetTextColor(const Color& textColor);
183     const Color& GetTextColor();
184 
185     void SetWidthReserved(const Dimension& widthReserved);
186     const Dimension& GetWidthReserved() const;
187 
188     const Color& GetSelectedColor() const;
189     void SetSelectedColor(const Color& selectedColor);
190 
191     const Color& GetHoverColor() const;
192     void SetHoverColor(const Color& hoverColor);
193 
194     const Color& GetPressColor() const;
195     void SetPressColor(const Color& pressColor);
196 
197     void SetBlockRightShade(bool blockRightShade);
198     bool GetBlockRightShade() const;
199 
200     void SetIsVisible(bool isVisible);
201     bool IsVisible() const;
202 
203     void SetResetToStart(bool resetToStart);
204     bool GetResetToStart() const;
205     bool HasSetResetToStart() const;
206 
207     void SetShowCounter(bool showCounter);
208     bool ShowCounter() const;
209 
210     void SetCountTextStyle(const TextStyle& countTextStyle);
211     const TextStyle& GetCountTextStyle() const;
212 
213     void SetOverCountStyle(const TextStyle& overCountStyle);
214     const TextStyle& GetOverCountStyle() const;
215 
216     void SetCountTextStyleOuter(const TextStyle& countTextStyleOuter);
217     const TextStyle& GetCountTextStyleOuter() const;
218 
219     void SetOverCountStyleOuter(const TextStyle& overCountStyleOuter);
220     const TextStyle& GetOverCountStyleOuter() const;
221 
222     void SetHoverAnimationType(HoverAnimationType animationType);
223     HoverAnimationType GetHoverAnimationType() const;
224 
225     const TextSelection& GetSelection() const;
226     void SetSelectedStart(int32_t selectedStart);
227     void SetSelectedEnd(int32_t selectedEnd);
228 
229     const EventMarker& GetOnTextChange() const;
230     void SetOnTextChange(const EventMarker& onTextChange);
231     void SetOnTextChangeFunction(std::function<void(const std::string&)>&& onTextChangeCallback);
232 
233     const EventMarker& GetOnSelectChange() const;
234     void SetOnSelectChange(const EventMarker& onSelectChange);
235 
236     const EventMarker& GetOnFinishInput() const;
237     void SetOnFinishInput(const EventMarker& onFinishInput);
238 
239     const EventMarker& GetOnTap() const;
240     void SetOnTap(const EventMarker& onTap);
241 
242     const EventMarker& GetOnLongPress() const;
243     void SetOnLongPress(const EventMarker& onLongPress);
244 
245     void SetInputOptions(const std::vector<InputOption>& inputOptions);
246     const std::vector<InputOption>& GetInputOptions() const;
247 
248     const EventMarker& GetOnOptionsClick() const;
249     void SetOnOptionsClick(const EventMarker& onOptionsClick);
250 
251     const EventMarker& GetOnTranslate() const;
252     void SetOnTranslate(const EventMarker& onTranslate);
253 
254     const EventMarker& GetOnShare() const;
255     void SetOnShare(const EventMarker& onShare);
256 
257     const EventMarker& GetOnSearch() const;
258     void SetOnSearch(const EventMarker& onSearch);
259 
260     bool IsValueUpdated() const;
261 
262     void SetPlaceHoldStyle(const TextStyle& style);
263     const TextStyle& GetPlaceHoldStyle() const;
264 
265     void SetEditingStyle(const TextStyle& style);
266     const TextStyle& GetEditingStyle() const;
267 
268     void SetInputFilter(const std::string& inputFilter);
269     const std::string& GetInputFilter() const;
270 
SetOnError(const std::function<void (const std::string &)> & value)271     void SetOnError(const std::function<void(const std::string&)>& value)
272     {
273         OnError_ = value;
274     }
GetOnError()275     const std::function<void(const std::string&)>& GetOnError() const
276     {
277         return OnError_;
278     }
279 
280     ACE_DEFINE_COMPONENT_EVENT(OnChange, void(std::string));
281 
282     ACE_DEFINE_COMPONENT_EVENT(OnEditChanged, void(bool));
283 
284     ACE_DEFINE_COMPONENT_EVENT(OnSubmit, void(int32_t));
285 
286     ACE_DEFINE_COMPONENT_EVENT(OnCopy, void(std::string));
287 
288     ACE_DEFINE_COMPONENT_EVENT(OnCut, void(std::string));
289 
290     ACE_DEFINE_COMPONENT_EVENT(OnPaste, void(std::string));
291 
292     ACE_DEFINE_COMPONENT_EVENT(OnClick, void(const ClickInfo& clickInfo));
293 
294     ACE_DEFINE_COMPONENT_PROP(CopyOption, CopyOptions, CopyOptions::Distributed);
295 
296 private:
297     RefPtr<TextFieldDeclaration> declaration_;
298     std::function<void(const std::string&)> OnError_;
299 };
300 
301 } // namespace OHOS::Ace
302 
303 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_FIELD_TEXT_FIELD_COMPONENT_H
304