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 #include "bridge/declarative_frontend/jsview/models/search_model_impl.h"
17
18 #include <utility>
19
20 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
21 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
22 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
23 #include "bridge/declarative_frontend/view_stack_processor.h"
24
25 namespace OHOS::Ace::Framework {
26
27 namespace {
28 const TextInputAction INPUT_TEXTINPUTACTION_VALUE_DEFAULT = TextInputAction::UNSPECIFIED;
29 const std::vector<std::string> INPUT_FONT_FAMILY_VALUE = {
30 "sans-serif",
31 };
32 const std::vector<TextAlign> TEXT_ALIGNS = { TextAlign::START, TextAlign::CENTER, TextAlign::END };
33 Radius defaultRadius;
34 constexpr Dimension BOX_HOVER_RADIUS = 18.0_vp;
35 bool isPaddingChanged;
36 } // namespace
37
Create(const std::optional<std::string> & value,const std::optional<std::string> & placeholder,const std::optional<std::string> & icon)38 RefPtr<TextFieldControllerBase> SearchModelImpl::Create(const std::optional<std::string>& value,
39 const std::optional<std::string>& placeholder, const std::optional<std::string>& icon)
40 {
41 auto searchComponent = AceType::MakeRefPtr<OHOS::Ace::SearchComponent>();
42 ViewStackProcessor::GetInstance()->ClaimElementId(searchComponent);
43 ViewStackProcessor::GetInstance()->Push(searchComponent);
44 auto textFieldComponent = AceType::MakeRefPtr<OHOS::Ace::TextFieldComponent>();
45 auto textFieldTheme = JSViewAbstract::GetTheme<TextFieldTheme>();
46 auto searchTheme = JSViewAbstract::GetTheme<SearchTheme>();
47 InitializeComponent(searchComponent, textFieldComponent, searchTheme, textFieldTheme);
48 PrepareSpecializedComponent(searchComponent, textFieldComponent);
49 if (value.has_value()) {
50 textFieldComponent->SetValue(value.value());
51 }
52 if (placeholder.has_value()) {
53 textFieldComponent->SetPlaceholder(placeholder.value());
54 }
55 if (icon.has_value()) {
56 textFieldComponent->SetIconImage(icon.value());
57 }
58 return textFieldComponent->GetTextFieldController();
59 }
60
SetSearchButton(const std::string & text)61 void SearchModelImpl::SetSearchButton(const std::string& text)
62 {
63 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
64 auto searchComponent = AceType::DynamicCast<SearchComponent>(component);
65 if (!searchComponent) {
66 LOGE("component error");
67 return;
68 }
69 searchComponent->SetSearchText(text);
70 }
71
SetPlaceholderColor(const Color & color)72 void SearchModelImpl::SetPlaceholderColor(const Color& color)
73 {
74 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
75 auto searchComponent = AceType::DynamicCast<SearchComponent>(component);
76 if (!searchComponent) {
77 LOGE("search component error");
78 return;
79 }
80 auto childComponent = searchComponent->GetChild();
81 if (!childComponent) {
82 LOGE("component error");
83 return;
84 }
85 auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
86 if (!textFieldComponent) {
87 LOGE("text component error");
88 return;
89 }
90 textFieldComponent->SetPlaceholderColor(color);
91 textFieldComponent->SetFocusPlaceholderColor(color);
92 }
93
SetPlaceholderFont(const Font & font)94 void SearchModelImpl::SetPlaceholderFont(const Font& font)
95 {
96 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
97 auto searchComponent = AceType::DynamicCast<SearchComponent>(component);
98 if (!searchComponent) {
99 LOGE("search component error");
100 return;
101 }
102 auto childComponent = searchComponent->GetChild();
103 if (!childComponent) {
104 LOGE("component error");
105 return;
106 }
107 auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
108 if (!textFieldComponent) {
109 LOGE("text component error");
110 return;
111 }
112 TextStyle textStyle = searchComponent->GetPlaceHoldStyle();
113 if (font.fontSize && font.fontSize->IsNonNegative()) {
114 textStyle.SetFontSize(font.fontSize.value());
115 }
116 if (font.fontWeight) {
117 textStyle.SetFontWeight(font.fontWeight.value());
118 }
119 if (font.fontStyle) {
120 textStyle.SetFontStyle(font.fontStyle.value());
121 }
122 if (!font.fontFamilies.empty()) {
123 textStyle.SetFontFamilies(font.fontFamilies);
124 }
125 textFieldComponent->SetPlaceHoldStyle(textStyle);
126 }
127
SetTextFont(const Font & font)128 void SearchModelImpl::SetTextFont(const Font& font)
129 {
130 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
131 auto searchComponent = AceType::DynamicCast<SearchComponent>(component);
132 if (!searchComponent) {
133 LOGE("search component error");
134 return;
135 }
136 auto childComponent = searchComponent->GetChild();
137 if (!childComponent) {
138 LOGE("component error");
139 return;
140 }
141 auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
142 if (!textFieldComponent) {
143 LOGE("text component error");
144 return;
145 }
146 TextStyle textStyle = searchComponent->GetEditingStyle();
147 if (font.fontSize && font.fontSize->IsNonNegative()) {
148 textStyle.SetFontSize(font.fontSize.value());
149 }
150 if (font.fontWeight) {
151 textStyle.SetFontWeight(font.fontWeight.value());
152 }
153 if (font.fontStyle) {
154 textStyle.SetFontStyle(font.fontStyle.value());
155 }
156 if (!font.fontFamilies.empty()) {
157 textStyle.SetFontFamilies(font.fontFamilies);
158 }
159 textFieldComponent->SetEditingStyle(textStyle);
160 }
161
SetTextAlign(const TextAlign & textAlign)162 void SearchModelImpl::SetTextAlign(const TextAlign& textAlign)
163 {
164 auto* stack = ViewStackProcessor::GetInstance();
165 auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
166 CHECK_NULL_VOID(component);
167 auto childComponent = component->GetChild();
168 CHECK_NULL_VOID(childComponent);
169 auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
170 CHECK_NULL_VOID(textFieldComponent);
171 textFieldComponent->SetTextAlign(textAlign);
172 }
173
SetCopyOption(const CopyOptions & copyOptions)174 void SearchModelImpl::SetCopyOption(const CopyOptions& copyOptions)
175 {
176 JSViewSetProperty(&TextFieldComponent::SetCopyOption, copyOptions);
177 }
178
SetOnSubmit(std::function<void (const std::string &)> && onSubmit)179 void SearchModelImpl::SetOnSubmit(std::function<void(const std::string&)>&& onSubmit)
180 {
181 auto* stack = ViewStackProcessor::GetInstance();
182 auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
183 CHECK_NULL_VOID(component);
184 component->SetOnSubmit(std::move(onSubmit));
185 }
186
SetOnChange(std::function<void (const std::string &)> && onChange)187 void SearchModelImpl::SetOnChange(std::function<void(const std::string&)>&& onChange)
188 {
189 auto* stack = ViewStackProcessor::GetInstance();
190 auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
191 CHECK_NULL_VOID(component);
192 component->SetOnChange(std::move(onChange));
193 }
194
SetOnCopy(std::function<void (const std::string &)> && func)195 void SearchModelImpl::SetOnCopy(std::function<void(const std::string&)>&& func)
196 {
197 auto* stack = ViewStackProcessor::GetInstance();
198 auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
199 CHECK_NULL_VOID(component);
200 auto childComponent = component->GetChild();
201 CHECK_NULL_VOID(childComponent);
202 auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
203 CHECK_NULL_VOID(textFieldComponent);
204 textFieldComponent->SetOnCopy(std::move(func));
205 }
206
SetOnCut(std::function<void (const std::string &)> && func)207 void SearchModelImpl::SetOnCut(std::function<void(const std::string&)>&& func)
208 {
209 auto* stack = ViewStackProcessor::GetInstance();
210 auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
211 CHECK_NULL_VOID(component);
212 auto childComponent = component->GetChild();
213 CHECK_NULL_VOID(childComponent);
214 auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
215 CHECK_NULL_VOID(textFieldComponent);
216 textFieldComponent->SetOnCut(std::move(func));
217 }
218
SetOnPaste(std::function<void (const std::string &)> && func)219 void SearchModelImpl::SetOnPaste(std::function<void(const std::string&)>&& func)
220 {
221 auto* stack = ViewStackProcessor::GetInstance();
222 auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
223 CHECK_NULL_VOID(component);
224 auto childComponent = component->GetChild();
225 CHECK_NULL_VOID(childComponent);
226 auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
227 CHECK_NULL_VOID(textFieldComponent);
228 textFieldComponent->SetOnPaste(std::move(func));
229 }
230
InitializeDefaultValue(const RefPtr<BoxComponent> & boxComponent,const RefPtr<TextFieldComponent> & component,const RefPtr<TextFieldTheme> & theme)231 void SearchModelImpl::InitializeDefaultValue(const RefPtr<BoxComponent>& boxComponent,
232 const RefPtr<TextFieldComponent>& component, const RefPtr<TextFieldTheme>& theme)
233 {
234 component->SetAction(INPUT_TEXTINPUTACTION_VALUE_DEFAULT);
235 component->SetCursorColor(theme->GetCursorColor());
236 component->SetCursorRadius(theme->GetCursorRadius());
237 component->SetPlaceholderColor(theme->GetPlaceholderColor());
238
239 component->SetFocusBgColor(theme->GetFocusBgColor());
240 component->SetFocusPlaceholderColor(theme->GetFocusPlaceholderColor());
241 component->SetFocusTextColor(theme->GetFocusTextColor());
242 component->SetBgColor(theme->GetBgColor());
243 component->SetTextColor(theme->GetTextColor());
244 component->SetSelectedColor(theme->GetSelectedColor());
245 component->SetHoverColor(theme->GetHoverColor());
246 component->SetPressColor(theme->GetPressColor());
247 component->SetNeedFade(theme->NeedFade());
248 component->SetShowEllipsis(theme->ShowEllipsis());
249
250 TextStyle textStyle = component->GetTextStyle();
251 textStyle.SetTextColor(theme->GetTextColor());
252 textStyle.SetFontSize(theme->GetFontSize());
253 textStyle.SetFontWeight(theme->GetFontWeight());
254 textStyle.SetFontFamilies(INPUT_FONT_FAMILY_VALUE);
255 component->SetTextStyle(textStyle);
256
257 component->SetCountTextStyle(theme->GetCountTextStyle());
258 component->SetOverCountStyle(theme->GetOverCountStyle());
259 component->SetCountTextStyleOuter(theme->GetCountTextStyleOuter());
260 component->SetOverCountStyleOuter(theme->GetOverCountStyleOuter());
261
262 component->SetErrorTextStyle(theme->GetErrorTextStyle());
263 component->SetErrorSpacing(theme->GetErrorSpacing());
264 component->SetErrorIsInner(theme->GetErrorIsInner());
265 component->SetErrorBorderWidth(theme->GetErrorBorderWidth());
266 component->SetErrorBorderColor(theme->GetErrorBorderColor());
267
268 RefPtr<Decoration> decoration = AceType::MakeRefPtr<Decoration>();
269 decoration->SetPadding(theme->GetPadding());
270 decoration->SetBackgroundColor(theme->GetBgColor());
271 decoration->SetBorderRadius(theme->GetBorderRadius());
272 defaultRadius = theme->GetBorderRadius();
273 const auto& boxDecoration = boxComponent->GetBackDecoration();
274 if (boxDecoration) {
275 decoration->SetImage(boxDecoration->GetImage());
276 decoration->SetGradient(boxDecoration->GetGradient());
277 }
278 component->SetDecoration(decoration);
279
280 component->SetIconSize(theme->GetIconSize());
281 component->SetIconHotZoneSize(theme->GetIconHotZoneSize());
282
283 boxComponent->SetPadding(theme->GetPadding());
284 component->SetHeight(theme->GetHeight());
285 }
286
UpdateDecorationStyle(const RefPtr<BoxComponent> & boxComponent,const RefPtr<TextFieldComponent> & component,const Border & boxBorder,bool hasBoxRadius)287 void SearchModelImpl::UpdateDecorationStyle(const RefPtr<BoxComponent>& boxComponent,
288 const RefPtr<TextFieldComponent>& component, const Border& boxBorder, bool hasBoxRadius)
289 {
290 RefPtr<Decoration> decoration = component->GetDecoration();
291 if (!decoration) {
292 decoration = AceType::MakeRefPtr<Decoration>();
293 }
294 if (hasBoxRadius) {
295 decoration->SetBorder(boxBorder);
296 } else {
297 Border border = decoration->GetBorder();
298 border.SetLeftEdge(boxBorder.Left());
299 border.SetRightEdge(boxBorder.Right());
300 border.SetTopEdge(boxBorder.Top());
301 border.SetBottomEdge(boxBorder.Bottom());
302 border.SetBorderRadius(defaultRadius);
303 decoration->SetBorder(border);
304 }
305 component->SetOriginBorder(decoration->GetBorder());
306
307 if (!boxComponent) {
308 return;
309 }
310 RefPtr<Decoration> boxDecoration = boxComponent->GetBackDecoration();
311 if (boxDecoration && (boxDecoration->GetImage() || boxDecoration->GetGradient().IsValid())) {
312 // clear box properties except background image and radius.
313 boxDecoration->SetBackgroundColor(Color::TRANSPARENT);
314 Border border;
315 if (!hasBoxRadius) {
316 border.SetBorderRadius(defaultRadius);
317 } else {
318 border.SetTopLeftRadius(boxBorder.TopLeftRadius());
319 border.SetTopRightRadius(boxBorder.TopRightRadius());
320 border.SetBottomLeftRadius(boxBorder.BottomLeftRadius());
321 border.SetBottomRightRadius(boxBorder.BottomRightRadius());
322 }
323 boxDecoration->SetBorder(border);
324 } else {
325 RefPtr<Decoration> backDecoration = AceType::MakeRefPtr<Decoration>();
326 backDecoration->SetBorderRadius(Radius(BOX_HOVER_RADIUS));
327 boxComponent->SetBackDecoration(backDecoration);
328 }
329 boxComponent->SetPadding(Edge());
330 }
331
InitializeComponent(OHOS::Ace::RefPtr<OHOS::Ace::SearchComponent> & searchComponent,OHOS::Ace::RefPtr<OHOS::Ace::TextFieldComponent> & textFieldComponent,const OHOS::Ace::RefPtr<OHOS::Ace::SearchTheme> & searchTheme,const OHOS::Ace::RefPtr<OHOS::Ace::TextFieldTheme> & textFieldTheme)332 void SearchModelImpl::InitializeComponent(OHOS::Ace::RefPtr<OHOS::Ace::SearchComponent>& searchComponent,
333 OHOS::Ace::RefPtr<OHOS::Ace::TextFieldComponent>& textFieldComponent,
334 const OHOS::Ace::RefPtr<OHOS::Ace::SearchTheme>& searchTheme,
335 const OHOS::Ace::RefPtr<OHOS::Ace::TextFieldTheme>& textFieldTheme)
336 {
337 textFieldComponent->SetTextFieldController(AceType::MakeRefPtr<TextFieldController>());
338 textFieldComponent->SetTextEditController(AceType::MakeRefPtr<TextEditController>());
339 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
340 InitializeDefaultValue(boxComponent, textFieldComponent, textFieldTheme);
341 boxComponent->SetBackDecoration(nullptr);
342 boxComponent->SetPadding(Edge());
343 textFieldComponent->SetIconSize(searchTheme->GetIconSize());
344 textFieldComponent->SetIconHotZoneSize(searchTheme->GetCloseIconHotZoneSize());
345 Edge decorationPadding;
346 Dimension leftPadding = searchTheme->GetLeftPadding();
347 Dimension rightPadding = searchTheme->GetRightPadding();
348 decorationPadding = Edge(rightPadding.Value(), 0.0, leftPadding.Value(), 0.0, leftPadding.Unit());
349 auto textFieldDecoration = textFieldComponent->GetDecoration();
350 if (textFieldDecoration) {
351 textFieldDecoration->SetPadding(decorationPadding);
352 textFieldDecoration->SetBorderRadius(searchTheme->GetBorderRadius());
353 textFieldComponent->SetOriginBorder(textFieldDecoration->GetBorder());
354 }
355 textFieldComponent->SetAction(TextInputAction::SEARCH);
356 textFieldComponent->SetWidthReserved(searchTheme->GetTextFieldWidthReserved());
357 textFieldComponent->SetTextColor(searchTheme->GetTextColor());
358 textFieldComponent->SetFocusTextColor(searchTheme->GetFocusTextColor());
359 textFieldComponent->SetPlaceholderColor(searchTheme->GetPlaceholderColor());
360 textFieldComponent->SetFocusPlaceholderColor(searchTheme->GetFocusPlaceholderColor());
361 textFieldComponent->SetBlockRightShade(searchTheme->GetBlockRightShade());
362
363 auto textStyle = textFieldComponent->GetTextStyle();
364 searchComponent->SetPlaceHoldStyle(textStyle);
365 searchComponent->SetEditingStyle(textStyle);
366
367 std::function<void(const std::string&)> submitEvent;
368 searchComponent->SetSubmitEvent(submitEvent);
369 searchComponent->SetChild(textFieldComponent);
370 searchComponent->SetTextEditController(textFieldComponent->GetTextEditController());
371 searchComponent->SetCloseIconSize(searchTheme->GetCloseIconSize());
372 searchComponent->SetCloseIconHotZoneHorizontal(searchTheme->GetCloseIconHotZoneSize());
373 searchComponent->SetHoverColor(textFieldTheme->GetHoverColor());
374 searchComponent->SetPressColor(textFieldTheme->GetPressColor());
375 isPaddingChanged = false;
376 }
377
PrepareSpecializedComponent(OHOS::Ace::RefPtr<OHOS::Ace::SearchComponent> & searchComponent,OHOS::Ace::RefPtr<OHOS::Ace::TextFieldComponent> & textFieldComponent)378 void SearchModelImpl::PrepareSpecializedComponent(OHOS::Ace::RefPtr<OHOS::Ace::SearchComponent>& searchComponent,
379 OHOS::Ace::RefPtr<OHOS::Ace::TextFieldComponent>& textFieldComponent)
380 {
381 Border boxBorder;
382
383 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
384
385 boxComponent->SetMouseAnimationType(HoverAnimationType::BOARD);
386 if (boxComponent->GetBackDecoration()) {
387 boxBorder = boxComponent->GetBackDecoration()->GetBorder();
388 }
389 UpdateDecorationStyle(boxComponent, textFieldComponent, boxBorder, false);
390 if (GreatOrEqual(boxComponent->GetHeightDimension().Value(), 0.0)) {
391 textFieldComponent->SetHeight(boxComponent->GetHeightDimension());
392 }
393 if (isPaddingChanged) {
394 auto padding = textFieldComponent->GetDecoration()->GetPadding();
395 if (searchComponent->GetTextDirection() == TextDirection::RTL) {
396 padding.SetLeft(padding.Left() + searchComponent->GetCloseIconHotZoneHorizontal());
397 } else {
398 padding.SetRight(padding.Right() + searchComponent->GetCloseIconHotZoneHorizontal());
399 }
400 textFieldComponent->GetDecoration()->SetPadding(padding);
401 searchComponent->SetDecoration(textFieldComponent->GetDecoration());
402 isPaddingChanged = false;
403 }
404 }
405
406 } // namespace OHOS::Ace::Framework
407