1 /*
2  * Copyright (c) 2022-2024 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_PATTERN_TEXT_FIELD_TEXT_FIELD_MODEL_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MODEL_NG_H
18 
19 #include "core/components_ng/pattern/text_field/text_content_type.h"
20 #include "core/components_ng/pattern/text_field/text_field_model.h"
21 
22 namespace OHOS::Ace::NG {
23 class ACE_EXPORT TextFieldModelNG : public TextFieldModel {
24 public:
25     TextFieldModelNG() = default;
26     ~TextFieldModelNG() override = default;
27     void CreateNode(
28         const std::optional<std::string>& placeholder, const std::optional<std::string>& value, bool isTextArea);
29     RefPtr<TextFieldControllerBase> CreateTextInput(
30         const std::optional<std::string>& placeholder, const std::optional<std::string>& value) override;
31 
32     RefPtr<TextFieldControllerBase> CreateTextArea(
33         const std::optional<std::string>& placeholder, const std::optional<std::string>& value) override;
34 
35     void RequestKeyboardOnFocus(bool needToRequest) override;
36     void SetWidthAuto(bool isAuto) override;
37     void SetType(TextInputType value) override;
38     void SetContentType(const TextContentType& value) override;
39     void SetPlaceholderColor(const Color& value) override;
40     void SetPlaceholderFont(const Font& value) override;
41     void SetEnterKeyType(TextInputAction value) override;
42     void SetTextAlign(TextAlign value) override;
43     void SetLineBreakStrategy(LineBreakStrategy value) override;
44     void SetCaretColor(const Color& value) override;
45     void SetCaretStyle(const CaretStyle& value) override;
46     void SetCaretPosition(const int32_t& value) override;
47     void SetSelectedBackgroundColor(const Color& value) override;
48     void SetMaxLength(uint32_t value) override;
49     void SetMaxLines(uint32_t value) override;
50     void SetFontSize(const Dimension& value) override;
51     void SetFontWeight(FontWeight value) override;
52     void SetTextColor(const Color& value) override;
53     void SetWordBreak(Ace::WordBreak value) override;
54     void SetFontStyle(Ace::FontStyle value) override;
55     void SetFontFamily(const std::vector<std::string>& value) override;
56     void SetInputFilter(const std::string& value, const std::function<void(const std::string&)>& onError) override;
57     void SetInputStyle(InputStyle value) override;
58     void SetShowPasswordIcon(bool value) override;
59     void SetShowPasswordText(bool value) override;
60     void SetOnEditChanged(std::function<void(bool)>&& func) override;
SetOnSubmit(std::function<void (int32_t)> && func)61     void SetOnSubmit(std::function<void(int32_t)>&& func) override {};
62     void SetOnSubmit(std::function<void(int32_t, NG::TextFieldCommonEvent&)>&& func) override;
63     void SetOnChange(std::function<void(const std::string&, PreviewText&)>&& func) override;
64     void SetOnTextSelectionChange(std::function<void(int32_t, int32_t)>&& func) override;
65     void SetOnSecurityStateChange(std::function<void(bool)>&& func) override;
66     void SetOnContentScroll(std::function<void(float, float)>&& func) override;
67     void SetOnCopy(std::function<void(const std::string&)>&& func) override;
68     void SetOnCut(std::function<void(const std::string&)>&& func) override;
69     void SetOnPaste(std::function<void(const std::string&)>&& func) override;
70     void SetOnPasteWithEvent(std::function<void(const std::string&, NG::TextCommonEvent&)>&& func) override;
71     void SetCopyOption(CopyOptions copyOption) override;
72     static void ProcessDefaultStyleAndBehaviors(const RefPtr<FrameNode>& frameNode);
73     void ResetMaxLength() override;
74     void SetForegroundColor(const Color& value) override;
75     void SetPasswordIcon(const PasswordIcon& passwordIcon) override;
76     void SetShowUnit(std::function<void()>&& unitFunction) override;
77     void SetShowError(const std::string& errorText, bool visible) override;
78     void SetBarState(OHOS::Ace::DisplayMode value) override;
79     void SetMaxViewLines(uint32_t value) override;
80     void SetNormalMaxViewLines(uint32_t value) override;
81 
82     void SetShowUnderline(bool showUnderLine) override;
83     void SetNormalUnderlineColor(const Color& normalColor) override;
84     void SetUserUnderlineColor(UserUnderlineColor userColor) override;
85     void SetShowCounter(bool value) override;
86     void SetCounterType(int32_t value) override;
87     void SetShowCounterBorder(bool value) override;
88     void SetOnChangeEvent(std::function<void(const std::string&)>&& func) override;
89     void SetBackgroundColor(const Color& color, bool tmp) override;
90     void SetHeight(const Dimension& value) override;
91     void SetPadding(const NG::PaddingProperty& newPadding, Edge oldPadding, bool tmp) override;
92     void SetMargin() override;
93     void SetHoverEffect(HoverEffectType hoverEffect) override;
94     void SetSelectionMenuHidden(bool contextMenuHidden) override;
95     void SetCustomKeyboard(const std::function<void()>&& buildFunc, bool supportAvoidance = false) override;
96     void SetPasswordRules(const std::string& passwordRules) override;
97     void SetEnableAutoFill(bool enableAutoFill) override;
98     void SetCleanNodeStyle(CleanNodeStyle cleanNodeStyle) override;
99     void SetCancelIconSize(const CalcDimension& iconSize) override;
100     void SetCanacelIconSrc(
101         const std::string& iconSrc, const std::string& bundleName, const std::string& moduleName) override;
102     void SetCancelIconColor(const Color& iconColor) override;
103     void SetIsShowCancelButton(bool isShowCancelButton) override;
104     void SetSelectAllValue(bool isSetSelectAllValue) override;
105     void SetFontFeature(const FONT_FEATURES_LIST& value) override;
106     void SetLetterSpacing(const Dimension& value) override;
107     void SetLineHeight(const Dimension& value) override;
108     void SetLineSpacing(const Dimension& value) override;
109     void SetAdaptMinFontSize(const Dimension& value) override;
110     void SetAdaptMaxFontSize(const Dimension& value) override;
111     void SetHeightAdaptivePolicy(TextHeightAdaptivePolicy value) override;
112     void SetTextDecoration(Ace::TextDecoration value) override;
113     void SetTextDecorationColor(const Color& value) override;
114     void SetTextDecorationStyle(Ace::TextDecorationStyle value) override;
115     void SetBackBorder() override;
116     void SetOnWillInsertValueEvent(std::function<bool(const InsertValueInfo&)>&& func) override;
117     void SetOnDidInsertValueEvent(std::function<void(const InsertValueInfo&)>&& func) override;
118     void SetOnWillDeleteEvent(std::function<bool(const DeleteValueInfo&)>&& func) override;
119     void SetOnDidDeleteEvent(std::function<void(const DeleteValueInfo&)>&& func) override;
120     void SetSelectionMenuOptions(const NG::OnCreateMenuCallback&& onCreateMenuCallback,
121         const NG::OnMenuItemClickCallback&& onMenuItemClick) override;
122     void SetEnablePreviewText(bool enablePreviewText) override;
123     void SetEnableHapticFeedback(bool state) override;
124 
125     static void SetTextDecoration(FrameNode* frameNode, TextDecoration value);
126     static void SetTextDecorationColor(FrameNode* frameNode, const Color& value);
127     static void SetTextDecorationStyle(FrameNode* frameNode, TextDecorationStyle value);
128     static void SetLetterSpacing(FrameNode* frameNode, const Dimension& value);
129     static void SetLineHeight(FrameNode* frameNode, const Dimension& value);
130     static void SetLineSpacing(FrameNode* frameNode, const Dimension& value);
131     void SetTextOverflow(Ace::TextOverflow value) override;
132     void SetTextIndent(const Dimension& value) override;
133     static void SetTextOverflow(FrameNode* frameNode, Ace::TextOverflow value);
134     static void SetTextIndent(FrameNode* frameNode, const Dimension& value);
135     static RefPtr<FrameNode> CreateFrameNode(int32_t nodeId, const std::optional<std::string>& placeholder,
136         const std::optional<std::string>& value, bool isTextArea);
137     static void SetAdaptMinFontSize(FrameNode* frameNode, const Dimension& value);
138     static void SetAdaptMaxFontSize(FrameNode* frameNode, const Dimension& value);
139     static void SetHeightAdaptivePolicy(FrameNode* frameNode, TextHeightAdaptivePolicy value);
140     static void SetInputStyle(FrameNode* frameNode, InputStyle value);
141     static void SetSelectionMenuHidden(FrameNode* frameNode, bool contextMenuHidden);
142     static bool GetSelectionMenuHidden(FrameNode* frameNode);
143     static void SetPasswordRules(FrameNode* frameNode, const std::string& passwordRules);
144     static void SetEnableAutoFill(FrameNode* frameNode, bool enableAutoFill);
145     static void RequestKeyboardOnFocus(FrameNode* frameNode, bool needToRequest);
146     static void SetBarState(FrameNode* frameNode, OHOS::Ace::DisplayMode value);
147     static void SetPasswordIcon(FrameNode* frameNode, const PasswordIcon& passwordIcon);
148     static void SetSelectedBackgroundColor(FrameNode* frameNode, const Color& value);
149     static void SetMaxViewLines(FrameNode* frameNode, uint32_t value);
150     static void SetNormalMaxViewLines(FrameNode* frameNode, uint32_t value);
151     static void SetType(FrameNode* frameNode, TextInputType value);
152     static void SetContentType(const FrameNode* frameNode, const TextContentType& value);
153     static void SetCopyOption(FrameNode* frameNode, CopyOptions copyOption);
154     static void SetShowPasswordIcon(FrameNode* frameNode, bool value);
155     static void SetShowPassword(FrameNode* frameNode, bool value);
156     static void SetTextAlign(FrameNode* frameNode, TextAlign value);
157     static void SetTextColor(FrameNode* frameNode, const Color& value);
158     static void SetCaretPosition(FrameNode* frameNode, const int32_t& value);
159     static void SetFontStyle(FrameNode* frameNode, Ace::FontStyle value);
160     static void SetMaxLength(FrameNode* frameNode, uint32_t value);
161     static void ResetMaxLength(FrameNode* frameNode);
162     static void SetCaretStyle(FrameNode* frameNode, const CaretStyle& value);
163     static void SetPlaceholderColor(FrameNode* frameNode, const Color& value);
164     static void SetFontWeight(FrameNode* frameNode, FontWeight value);
165     static void SetEnterKeyType(FrameNode* frameNode, TextInputAction value);
166     static void SetShowUnderline(FrameNode* frameNode, bool showUnderLine);
167     static void SetNormalUnderlineColor(FrameNode* frameNode, const Color& normalColor);
168     static void SetUserUnderlineColor(FrameNode* frameNode, UserUnderlineColor userColor);
169     static void SetFontFamily(FrameNode* frameNode, const std::vector<std::string>& value);
170     static void SetMaxLines(FrameNode* frameNode, uint32_t value);
171     static void SetPlaceholderFont(FrameNode* frameNode, const Font& value);
172     static void SetFontSize(FrameNode* frameNode, const Dimension& value);
173     static void SetCaretColor(FrameNode* frameNode, const Color& value);
174     static void SetShowCounter(FrameNode* frameNode, bool value);
175     static void SetCounterType(FrameNode* frameNode, int32_t value);
176     static void SetShowError(FrameNode* frameNode, const std::string& errorText, bool visible);
177     static void SetOnChange(FrameNode* frameNode, std::function<void(const std::string&, PreviewText&)>&& func);
178     static void SetOnContentSizeChange(FrameNode* frameNode, std::function<void(float, float)>&& func);
179     static void SetOnTextSelectionChange(FrameNode* frameNode, std::function<void(int32_t, int32_t)>&& func);
180     static void SetTextFieldText(FrameNode* frameNode, const std::string& value);
181     static void SetTextFieldPlaceHolder(FrameNode* frameNode, const std::string& placeholder);
182     static void StopTextFieldEditing(FrameNode* frameNode);
183     static void SetOnSubmit(FrameNode* frameNode, std::function<void(int32_t, NG::TextFieldCommonEvent&)>&& func);
184     static void SetOnCut(FrameNode* frameNode, std::function<void(const std::string&)>&& func);
185     static void SetOnPasteWithEvent(FrameNode* frameNode,
186         std::function<void(const std::string&, NG::TextCommonEvent&)>&& func);
187     static void SetCleanNodeStyle(FrameNode* frameNode, CleanNodeStyle cleanNodeStyle);
188     static void SetIsShowCancelButton(FrameNode* frameNode, bool isShowCancelButton);
189     static void SetCancelIconSize(FrameNode* frameNode, const CalcDimension& iconSize);
190     static void SetCanacelIconSrc(FrameNode* frameNode, const std::string& iconSrc);
191     static void SetCancelIconColor(FrameNode* frameNode, const Color& iconColor);
192     static void SetBackgroundColor(FrameNode* frameNode, const Color& color);
193     static std::string GetPlaceholderText(FrameNode* frameNode);
194     static std::string GetTextFieldText(FrameNode* frameNode);
195     static Color GetCaretColor(FrameNode* frameNode);
196     static Dimension GetCaretStyle(FrameNode* frameNode);
197     static bool GetShowUnderline(FrameNode* frameNode);
198     static uint32_t GetMaxLength(FrameNode* frameNode);
199     static TextInputAction GetEnterKeyType(FrameNode* frameNode);
200     static Color GetPlaceholderColor(FrameNode* frameNode);
201     static Font GetPlaceholderFont(FrameNode* frameNode);
202     static bool GetRequestKeyboardOnFocus(FrameNode* frameNode);
203     static TextInputType GetType(FrameNode* frameNode);
204     static Color GetSelectedBackgroundColor(FrameNode* frameNode);
205     static bool GetShowPasswordIcon(FrameNode* frameNode);
206     static bool GetShowPassword(FrameNode* frameNode);
207     static bool GetTextFieldEditing(FrameNode* frameNode);
208     static bool GetShowCancelButton(FrameNode* frameNode);
209     static CalcDimension GetCancelIconSize(FrameNode* frameNode);
210     static std::string GetCanacelIconSrc(FrameNode* frameNode);
211     static Color GetCancelIconColor(FrameNode* frameNode);
212     static TextAlign GetTextAlign(FrameNode* frameNode);
213     static Color GetTextColor(FrameNode* frameNode);
214     static Ace::FontStyle GetFontStyle(FrameNode* frameNode);
215     static FontWeight GetFontWeight(FrameNode* frameNode);
216     static Dimension GetFontSize(FrameNode* frameNode);
217     static CleanNodeStyle GetCleanNodeStyle(FrameNode* frameNode);
218     static void SetShowCounterBorder(FrameNode* frameNode, bool value);
219     static bool GetShowCounter(FrameNode* frameNode);
220     static int GetCounterType(FrameNode* frameNode);
221     static bool GetShowCounterBorder(FrameNode* frameNode);
222     static void SetTextSelection(FrameNode* frameNode, int32_t start, int32_t end);
223     static int32_t GetTextSelectionIndex(FrameNode* frameNode, bool isEnd);
224     static void ResetTextInputPadding(FrameNode* frameNode);
225     static void SetFontFeature(FrameNode* frameNode, const FONT_FEATURES_LIST& value);
226     static void SetAutoWidth(FrameNode* frameNode);
227     static void SetWidth(FrameNode* frameNode, const std::string& value);
228     static void ClearWidth(FrameNode* frameNode);
229     static void SetWordBreak(FrameNode* frameNode, Ace::WordBreak value);
230     static void SetLineBreakStrategy(FrameNode* frameNode, LineBreakStrategy value);
231     static void SetSelectAllValue(FrameNode* frameNode, bool isSelectAllValue);
232     static void SetBlurOnSubmit(FrameNode* frameNode, bool blurOnSubmit);
233     static bool GetBlurOnSubmit(FrameNode* frameNode);
234     static void SetOnEditChange(FrameNode* frameNode, std::function<void(bool)>&& func);
235     static void SetInputFilter(FrameNode* frameNode, const std::string& value,
236         const std::function<void(const std::string&)>& onError);
237     static void SetOnContentScroll(FrameNode* frameNode, std::function<void(float, float)>&& func);
238     static void SetOnCopy(FrameNode* frameNode, std::function<void(const std::string&)>&& func);
239     static void SetOnEditChanged(FrameNode* frameNode, std::function<void(bool)>&& func);
240     static void SetCustomKeyboard(FrameNode* frameNode, FrameNode* customKeyboard, bool supportAvoidance = false);
241     static void SetInputFilter(FrameNode* frameNode, const std::string& value);
242     static void SetInputFilterError(FrameNode* frameNode, const std::function<void(const std::string&)>& onError);
243     static Ace::WordBreak GetWordBreak(FrameNode* frameNode);
244     static bool GetEnableAutoFill(FrameNode* frameNode);
245     static TextContentType GetContentType(FrameNode* frameNode);
246     static UserUnderlineColor GetUnderLineColor(FrameNode* frameNode);
247     static std::string GetPasswordRules(FrameNode* frameNode);
248     static bool GetSelectAllValue(FrameNode* frameNode);
249     static std::string GetInputFilter(FrameNode* frameNode);
250     static InputStyle GetInputStyle(FrameNode* frameNode);
251     static RefPtr<TextFieldControllerBase> GetOrCreateController(FrameNode* frameNode);
252     static FONT_FEATURES_LIST GetFontFeature(FrameNode* frameNode);
253     static Dimension GetAdaptMinFontSize(FrameNode* frameNode);
254     static Dimension GetAdaptMaxFontSize(FrameNode* frameNode);
255     static Dimension GetLineHeight(FrameNode* frameNode);
256     static uint32_t GetMaxLines(FrameNode* frameNode);
257     static void SetPadding(FrameNode* frameNode, NG::PaddingProperty& newPadding);
258     static RefPtr<UINode> GetCustomKeyboard(FrameNode* frameNode);
259     static bool GetCustomKeyboardOption(FrameNode* frameNode);
260     static void SetShowKeyBoardOnFocus(FrameNode* frameNode, bool value);
261     static bool GetShowKeyBoardOnFocus(FrameNode* frameNode);
262     static void SetNumberOfLines(FrameNode* frameNode, int32_t value);
263     static int32_t GetNumberOfLines(FrameNode* frameNode);
264     static void ResetNumberOfLines(FrameNode* frameNode);
265     static void SetBorderWidth(FrameNode* frameNode, NG::BorderWidthProperty borderWidth);
266     static void SetBorderRadius(FrameNode* frameNode, NG::BorderRadiusProperty borderRadius);
267     static void SetBorderColor(FrameNode* frameNode, NG::BorderColorProperty borderColors);
268     static void SetBorderStyle(FrameNode* frameNode, NG::BorderStyleProperty borderStyles);
269     static void SetMargin(FrameNode* frameNode, NG::PaddingProperty& margin);
270     static PaddingProperty GetMargin(FrameNode* frameNode);
271     static void SetOnWillInsertValueEvent(FrameNode* frameNode, std::function<bool(const InsertValueInfo&)>&& func);
272     static void SetOnDidInsertValueEvent(FrameNode* frameNode, std::function<void(const InsertValueInfo&)>&& func);
273     static void SetOnWillDeleteEvent(FrameNode* frameNode, std::function<bool(const DeleteValueInfo&)>&& func);
274     static void SetOnDidDeleteEvent(FrameNode* frameNode, std::function<void(const DeleteValueInfo&)>&& func);
275     static void SetEnablePreviewText(FrameNode* frameNode, bool enablePreviewText);
276     static PaddingProperty GetPadding(FrameNode* frameNode);
277     static void SetSelectionMenuOptions(FrameNode* frameNode, const NG::OnCreateMenuCallback&& onCreateMenuCallback,
278         const NG::OnMenuItemClickCallback&& onMenuItemClick);
279     static void SetEnableHapticFeedback(FrameNode* frameNode, bool state);
280     static void SetJSTextEditableController(FrameNode* frameNode, const RefPtr<Referenced>& controller);
281     static RefPtr<Referenced> GetJSTextEditableController(FrameNode* frameNode);
282 
283 private:
284     void AddDragFrameNodeToManager() const;
285     void SetDraggable(bool draggable);
286     void SetTextRectWillChange();
287     void SetDefaultPadding();
288 };
289 
290 } // namespace OHOS::Ace::NG
291 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_MODEL_NG_H
292