• 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 #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 
SetFocusable(bool focusable)179 void SearchModelImpl::SetFocusable(bool focusable)
180 {
181     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent();
182     CHECK_NULL_VOID(focusableComponent);
183     focusableComponent->SetFocusable(focusable);
184 }
185 
SetFocusNode(bool isFocusNode)186 void SearchModelImpl::SetFocusNode(bool isFocusNode)
187 {
188     auto focusableComponent = ViewStackProcessor::GetInstance()->GetFocusableComponent(false);
189     CHECK_NULL_VOID(focusableComponent);
190     focusableComponent->SetFocusNode(!isFocusNode);
191 }
192 
SetHeight(const Dimension & value)193 void SearchModelImpl::SetHeight(const Dimension& value)
194 {
195     auto stack = ViewStackProcessor::GetInstance();
196     auto searchComponent = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
197     CHECK_NULL_VOID(searchComponent);
198     auto childComponent = searchComponent->GetChild();
199     CHECK_NULL_VOID(childComponent);
200     auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
201     CHECK_NULL_VOID(textFieldComponent);
202     textFieldComponent->SetHeight(value);
203 }
204 
SetBackBorder()205 void SearchModelImpl::SetBackBorder()
206 {
207     auto stack = ViewStackProcessor::GetInstance();
208     auto searchComponent = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
209     CHECK_NULL_VOID(searchComponent);
210     auto childComponent = searchComponent->GetChild();
211     CHECK_NULL_VOID(childComponent);
212     auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
213     CHECK_NULL_VOID(textFieldComponent);
214     auto decoration = textFieldComponent->GetDecoration();
215     CHECK_NULL_VOID(decoration);
216     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
217     auto boxDecoration = box->GetBackDecoration();
218     if (boxDecoration) {
219         decoration->SetBorder(boxDecoration->GetBorder());
220         Border border = {};
221         boxDecoration->SetBorder(border);
222     }
223     textFieldComponent->SetOriginBorder(decoration->GetBorder());
224 }
225 
SetOnSubmit(std::function<void (const std::string &)> && onSubmit)226 void SearchModelImpl::SetOnSubmit(std::function<void(const std::string&)>&& onSubmit)
227 {
228     auto* stack = ViewStackProcessor::GetInstance();
229     auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
230     CHECK_NULL_VOID(component);
231     component->SetOnSubmit(std::move(onSubmit));
232 }
233 
SetOnCopy(std::function<void (const std::string &)> && func)234 void SearchModelImpl::SetOnCopy(std::function<void(const std::string&)>&& func)
235 {
236     auto* stack = ViewStackProcessor::GetInstance();
237     auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
238     CHECK_NULL_VOID(component);
239     auto childComponent = component->GetChild();
240     CHECK_NULL_VOID(childComponent);
241     auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
242     CHECK_NULL_VOID(textFieldComponent);
243     textFieldComponent->SetOnCopy(std::move(func));
244 }
245 
SetOnCut(std::function<void (const std::string &)> && func)246 void SearchModelImpl::SetOnCut(std::function<void(const std::string&)>&& func)
247 {
248     auto* stack = ViewStackProcessor::GetInstance();
249     auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
250     CHECK_NULL_VOID(component);
251     auto childComponent = component->GetChild();
252     CHECK_NULL_VOID(childComponent);
253     auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
254     CHECK_NULL_VOID(textFieldComponent);
255     textFieldComponent->SetOnCut(std::move(func));
256 }
257 
SetOnPaste(std::function<void (const std::string &)> && func)258 void SearchModelImpl::SetOnPaste(std::function<void(const std::string&)>&& func)
259 {
260     auto* stack = ViewStackProcessor::GetInstance();
261     auto component = AceType::DynamicCast<SearchComponent>(stack->GetMainComponent());
262     CHECK_NULL_VOID(component);
263     auto childComponent = component->GetChild();
264     CHECK_NULL_VOID(childComponent);
265     auto textFieldComponent = AceType::DynamicCast<TextFieldComponent>(childComponent);
266     CHECK_NULL_VOID(textFieldComponent);
267     textFieldComponent->SetOnPaste(std::move(func));
268 }
269 
InitializeDefaultValue(const RefPtr<BoxComponent> & boxComponent,const RefPtr<TextFieldComponent> & component,const RefPtr<TextFieldTheme> & theme)270 void SearchModelImpl::InitializeDefaultValue(const RefPtr<BoxComponent>& boxComponent,
271     const RefPtr<TextFieldComponent>& component, const RefPtr<TextFieldTheme>& theme)
272 {
273     component->SetAction(INPUT_TEXTINPUTACTION_VALUE_DEFAULT);
274     component->SetCursorColor(theme->GetCursorColor());
275     component->SetCursorRadius(theme->GetCursorRadius());
276     component->SetPlaceholderColor(theme->GetPlaceholderColor());
277 
278     component->SetFocusBgColor(theme->GetFocusBgColor());
279     component->SetFocusPlaceholderColor(theme->GetFocusPlaceholderColor());
280     component->SetFocusTextColor(theme->GetFocusTextColor());
281     component->SetBgColor(theme->GetBgColor());
282     component->SetTextColor(theme->GetTextColor());
283     component->SetSelectedColor(theme->GetSelectedColor());
284     component->SetHoverColor(theme->GetHoverColor());
285     component->SetPressColor(theme->GetPressColor());
286     component->SetNeedFade(theme->NeedFade());
287     component->SetShowEllipsis(theme->ShowEllipsis());
288 
289     TextStyle textStyle = component->GetTextStyle();
290     textStyle.SetTextColor(theme->GetTextColor());
291     textStyle.SetFontSize(theme->GetFontSize());
292     textStyle.SetFontWeight(theme->GetFontWeight());
293     textStyle.SetFontFamilies(INPUT_FONT_FAMILY_VALUE);
294     component->SetTextStyle(textStyle);
295 
296     component->SetCountTextStyle(theme->GetCountTextStyle());
297     component->SetOverCountStyle(theme->GetOverCountStyle());
298     component->SetCountTextStyleOuter(theme->GetCountTextStyleOuter());
299     component->SetOverCountStyleOuter(theme->GetOverCountStyleOuter());
300 
301     component->SetErrorTextStyle(theme->GetErrorTextStyle());
302     component->SetErrorSpacing(theme->GetErrorSpacing());
303     component->SetErrorIsInner(theme->GetErrorIsInner());
304     component->SetErrorBorderWidth(theme->GetErrorBorderWidth());
305     component->SetErrorBorderColor(theme->GetErrorBorderColor());
306 
307     RefPtr<Decoration> decoration = AceType::MakeRefPtr<Decoration>();
308     decoration->SetPadding(theme->GetPadding());
309     decoration->SetBackgroundColor(theme->GetBgColor());
310     decoration->SetBorderRadius(theme->GetBorderRadius());
311     defaultRadius = theme->GetBorderRadius();
312     const auto& boxDecoration = boxComponent->GetBackDecoration();
313     if (boxDecoration) {
314         decoration->SetImage(boxDecoration->GetImage());
315         decoration->SetGradient(boxDecoration->GetGradient());
316     }
317     component->SetDecoration(decoration);
318 
319     component->SetIconSize(theme->GetIconSize());
320     component->SetIconHotZoneSize(theme->GetIconHotZoneSize());
321 
322     boxComponent->SetPadding(theme->GetPadding());
323     component->SetHeight(theme->GetHeight());
324 }
325 
UpdateDecorationStyle(const RefPtr<BoxComponent> & boxComponent,const RefPtr<TextFieldComponent> & component,const Border & boxBorder,bool hasBoxRadius)326 void SearchModelImpl::UpdateDecorationStyle(const RefPtr<BoxComponent>& boxComponent,
327     const RefPtr<TextFieldComponent>& component, const Border& boxBorder, bool hasBoxRadius)
328 {
329     RefPtr<Decoration> decoration = component->GetDecoration();
330     if (!decoration) {
331         decoration = AceType::MakeRefPtr<Decoration>();
332     }
333     if (hasBoxRadius) {
334         decoration->SetBorder(boxBorder);
335     } else {
336         Border border = decoration->GetBorder();
337         border.SetLeftEdge(boxBorder.Left());
338         border.SetRightEdge(boxBorder.Right());
339         border.SetTopEdge(boxBorder.Top());
340         border.SetBottomEdge(boxBorder.Bottom());
341         border.SetBorderRadius(defaultRadius);
342         decoration->SetBorder(border);
343     }
344     component->SetOriginBorder(decoration->GetBorder());
345 
346     if (!boxComponent) {
347         return;
348     }
349     RefPtr<Decoration> boxDecoration = boxComponent->GetBackDecoration();
350     if (boxDecoration && (boxDecoration->GetImage() || boxDecoration->GetGradient().IsValid())) {
351         // clear box properties except background image and radius.
352         boxDecoration->SetBackgroundColor(Color::TRANSPARENT);
353         Border border;
354         if (!hasBoxRadius) {
355             border.SetBorderRadius(defaultRadius);
356         } else {
357             border.SetTopLeftRadius(boxBorder.TopLeftRadius());
358             border.SetTopRightRadius(boxBorder.TopRightRadius());
359             border.SetBottomLeftRadius(boxBorder.BottomLeftRadius());
360             border.SetBottomRightRadius(boxBorder.BottomRightRadius());
361         }
362         boxDecoration->SetBorder(border);
363     } else {
364         RefPtr<Decoration> backDecoration = AceType::MakeRefPtr<Decoration>();
365         backDecoration->SetBorderRadius(Radius(BOX_HOVER_RADIUS));
366         boxComponent->SetBackDecoration(backDecoration);
367     }
368     boxComponent->SetPadding(Edge());
369 }
370 
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)371 void SearchModelImpl::InitializeComponent(OHOS::Ace::RefPtr<OHOS::Ace::SearchComponent>& searchComponent,
372     OHOS::Ace::RefPtr<OHOS::Ace::TextFieldComponent>& textFieldComponent,
373     const OHOS::Ace::RefPtr<OHOS::Ace::SearchTheme>& searchTheme,
374     const OHOS::Ace::RefPtr<OHOS::Ace::TextFieldTheme>& textFieldTheme)
375 {
376     textFieldComponent->SetTextFieldController(AceType::MakeRefPtr<TextFieldController>());
377     textFieldComponent->SetTextEditController(AceType::MakeRefPtr<TextEditController>());
378     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
379     InitializeDefaultValue(boxComponent, textFieldComponent, textFieldTheme);
380     boxComponent->SetBackDecoration(nullptr);
381     boxComponent->SetPadding(Edge());
382     textFieldComponent->SetIconSize(searchTheme->GetIconSize());
383     textFieldComponent->SetIconHotZoneSize(searchTheme->GetCloseIconHotZoneSize());
384     Edge decorationPadding;
385     Dimension leftPadding = searchTheme->GetLeftPadding();
386     Dimension rightPadding = searchTheme->GetRightPadding();
387     decorationPadding = Edge(rightPadding.Value(), 0.0, leftPadding.Value(), 0.0, leftPadding.Unit());
388     auto textFieldDecoration = textFieldComponent->GetDecoration();
389     if (textFieldDecoration) {
390         textFieldDecoration->SetPadding(decorationPadding);
391         textFieldDecoration->SetBorderRadius(searchTheme->GetBorderRadius());
392         textFieldComponent->SetOriginBorder(textFieldDecoration->GetBorder());
393     }
394     textFieldComponent->SetAction(TextInputAction::SEARCH);
395     textFieldComponent->SetWidthReserved(searchTheme->GetTextFieldWidthReserved());
396     textFieldComponent->SetTextColor(searchTheme->GetTextColor());
397     textFieldComponent->SetFocusTextColor(searchTheme->GetFocusTextColor());
398     textFieldComponent->SetPlaceholderColor(searchTheme->GetPlaceholderColor());
399     textFieldComponent->SetFocusPlaceholderColor(searchTheme->GetFocusPlaceholderColor());
400     textFieldComponent->SetBlockRightShade(searchTheme->GetBlockRightShade());
401 
402     auto textStyle = textFieldComponent->GetTextStyle();
403     searchComponent->SetPlaceHoldStyle(textStyle);
404     searchComponent->SetEditingStyle(textStyle);
405 
406     std::function<void(const std::string&)> submitEvent;
407     searchComponent->SetSubmitEvent(submitEvent);
408     searchComponent->SetChild(textFieldComponent);
409     searchComponent->SetTextEditController(textFieldComponent->GetTextEditController());
410     searchComponent->SetCloseIconSize(searchTheme->GetCloseIconSize());
411     searchComponent->SetCloseIconHotZoneHorizontal(searchTheme->GetCloseIconHotZoneSize());
412     searchComponent->SetHoverColor(textFieldTheme->GetHoverColor());
413     searchComponent->SetPressColor(textFieldTheme->GetPressColor());
414     isPaddingChanged = false;
415 }
416 
PrepareSpecializedComponent(OHOS::Ace::RefPtr<OHOS::Ace::SearchComponent> & searchComponent,OHOS::Ace::RefPtr<OHOS::Ace::TextFieldComponent> & textFieldComponent)417 void SearchModelImpl::PrepareSpecializedComponent(OHOS::Ace::RefPtr<OHOS::Ace::SearchComponent>& searchComponent,
418     OHOS::Ace::RefPtr<OHOS::Ace::TextFieldComponent>& textFieldComponent)
419 {
420     Border boxBorder;
421 
422     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
423 
424     boxComponent->SetMouseAnimationType(HoverAnimationType::BOARD);
425     if (boxComponent->GetBackDecoration()) {
426         boxBorder = boxComponent->GetBackDecoration()->GetBorder();
427     }
428     UpdateDecorationStyle(boxComponent, textFieldComponent, boxBorder, false);
429     if (GreatOrEqual(boxComponent->GetHeightDimension().Value(), 0.0)) {
430         textFieldComponent->SetHeight(boxComponent->GetHeightDimension());
431     }
432     if (isPaddingChanged) {
433         auto padding = textFieldComponent->GetDecoration()->GetPadding();
434         if (searchComponent->GetTextDirection() == TextDirection::RTL) {
435             padding.SetLeft(padding.Left() + searchComponent->GetCloseIconHotZoneHorizontal());
436         } else {
437             padding.SetRight(padding.Right() + searchComponent->GetCloseIconHotZoneHorizontal());
438         }
439         textFieldComponent->GetDecoration()->SetPadding(padding);
440         searchComponent->SetDecoration(textFieldComponent->GetDecoration());
441         isPaddingChanged = false;
442     }
443 }
444 
445 } // namespace OHOS::Ace::Framework
446