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 #include "core/components/declaration/textarea/textarea_declaration.h"
17 
18 #include "base/utils/string_utils.h"
19 #include "frameworks/core/components/text_field/textfield_theme.h"
20 
21 namespace OHOS::Ace {
22 namespace {
23 
24 constexpr uint32_t TEXTAREA_MAXLENGTH_VALUE_DEFAULT = std::numeric_limits<uint32_t>::max();
25 
26 } // namespace
27 
28 using namespace Framework;
29 
30 using TextareaMap = std::unordered_map<std::string, void (*)(const std::string&, TextareaDeclaration&)>;
31 
TextareaDeclaration()32 TextareaDeclaration::TextareaDeclaration()
33 {
34     textFieldDeclaration_ = AceType::MakeRefPtr<TextFieldDeclaration>();
35     textFieldDeclaration_->BindPipelineContext(pipelineContext_);
36     textFieldDeclaration_->Init();
37     textFieldDeclaration_->SetTextInputType(TextInputType::MULTILINE);
38     textFieldDeclaration_->SetTextEditController(AceType::MakeRefPtr<TextEditController>());
39     textFieldDeclaration_->SetTextFieldController(AceType::MakeRefPtr<TextFieldController>());
40 }
41 
InitializeStyle()42 void TextareaDeclaration::InitializeStyle()
43 {
44     auto theme = GetTheme<TextFieldTheme>();
45     if (!theme || !textFieldDeclaration_) {
46         return;
47     }
48 
49     textFieldDeclaration_->SetTextMaxLines(TEXTAREA_MAXLENGTH_VALUE_DEFAULT);
50     textFieldDeclaration_->SetCursorColor(theme->GetCursorColor());
51     textFieldDeclaration_->SetPlaceholderColor(theme->GetPlaceholderColor());
52     textFieldDeclaration_->SetFocusBgColor(theme->GetFocusBgColor());
53     textFieldDeclaration_->SetFocusPlaceholderColor(theme->GetFocusPlaceholderColor());
54     textFieldDeclaration_->SetFocusTextColor(theme->GetFocusTextColor());
55     textFieldDeclaration_->SetBgColor(theme->GetBgColor());
56     textFieldDeclaration_->SetTextColor(theme->GetTextColor());
57     textFieldDeclaration_->SetSelectedColor(theme->GetSelectedColor());
58     textFieldDeclaration_->SetHoverColor(theme->GetHoverColor());
59     textFieldDeclaration_->SetPressColor(theme->GetPressColor());
60     textFieldDeclaration_->GetTextStyle().SetTextColor(theme->GetTextColor());
61     textFieldDeclaration_->GetTextStyle().SetFontSize(theme->GetFontSize());
62     textFieldDeclaration_->GetTextStyle().SetFontWeight(theme->GetFontWeight());
63     textFieldDeclaration_->GetTextStyle().SetFontFamilies({
64         "sans-serif",
65     });
66     textFieldDeclaration_->SetCountTextStyle(theme->GetCountTextStyle());
67     textFieldDeclaration_->SetOverCountStyle(theme->GetOverCountStyle());
68     textFieldDeclaration_->SetCountTextStyleOuter(theme->GetCountTextStyleOuter());
69     textFieldDeclaration_->SetOverCountStyleOuter(theme->GetOverCountStyleOuter());
70 
71     textFieldDeclaration_->SetErrorBorderWidth(theme->GetErrorBorderWidth());
72     textFieldDeclaration_->SetErrorBorderColor(theme->GetErrorBorderColor());
73 
74     RefPtr<Decoration> backDecoration = AceType::MakeRefPtr<Decoration>();
75     backDecoration->SetPadding(theme->GetPadding());
76     backDecoration->SetBackgroundColor(theme->GetBgColor());
77     backDecoration->SetBorderRadius(theme->GetBorderRadius());
78     textFieldDeclaration_->SetDecoration(backDecoration);
79     textFieldDeclaration_->SetIconSize(theme->GetIconSize());
80     textFieldDeclaration_->SetIconHotZoneSize(theme->GetIconHotZoneSize());
81 }
82 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)83 bool TextareaDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
84 {
85     return textFieldDeclaration_->SetSpecializedAttr(attr);
86 }
87 
SetSpecializedStyle(const std::pair<std::string,std::string> & style)88 bool TextareaDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
89 {
90     return textFieldDeclaration_->SetSpecializedStyle(style);
91 }
92 
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)93 bool TextareaDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
94 {
95     return textFieldDeclaration_->SetSpecializedEvent(pageId, eventId, event);
96 }
97 
CallSpecializedMethod(const std::string & method,const std::string & args)98 void TextareaDeclaration::CallSpecializedMethod(const std::string& method, const std::string& args)
99 {
100     textFieldDeclaration_->CallSpecializedMethod(method, args);
101 }
102 
OnRequestFocus(bool shouldFocus)103 void TextareaDeclaration::OnRequestFocus(bool shouldFocus)
104 {
105     textFieldDeclaration_->OnRequestFocus(shouldFocus);
106 }
107 
108 } // namespace OHOS::Ace