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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SEARCH_SEARCH_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SEARCH_SEARCH_DECLARATION_H
18 
19 #include "core/common/ime/text_edit_controller.h"
20 #include "core/components/common/properties/input_option.h"
21 #include "core/components/declaration/common/declaration.h"
22 #include "core/components/declaration/textfield/textfield_declaration.h"
23 #include "frameworks/bridge/common/dom/dom_type.h"
24 
25 namespace OHOS::Ace {
26 
27 struct SearchAttribute : Attribute {
28     Dimension closeIconSize;
29     Dimension closeIconHotZoneHorizontal;
30     std::string searchText;
31     std::string closeIconSrc;
32     Dimension searchHeight;
33     Dimension searchWidth;
34 };
35 
36 struct SearchStyle : Style {
37     RefPtr<Decoration> decoration;
38     Color hoverColor;
39     Color pressColor;
40     TextStyle editingStyle;
41     TextStyle placeHoldStyle;
42 };
43 
44 struct SearchEvent : Event {
45     std::function<void(const std::string&)> submitEvent;
46     EventMarker changeEventId;
47     EventMarker submitEventId;
48 };
49 
50 struct SearchMethod : Method {};
51 
52 class SearchDeclaration : public Declaration {
53     DECLARE_ACE_TYPE(SearchDeclaration, Declaration);
54 
55 public:
56     SearchDeclaration();
57     ~SearchDeclaration() override = default;
58 
59     void InitializeStyle() override;
60     void OnRequestFocus(bool shouldFocus) override;
61 
SetCloseIconSize(const Dimension & closeIconSize)62     void SetCloseIconSize(const Dimension& closeIconSize)
63     {
64         auto& attribute = MaybeResetAttribute<SearchAttribute>(AttributeTag::SPECIALIZED_ATTR);
65         attribute.closeIconSize = closeIconSize;
66     }
67 
GetCloseIconSize()68     const Dimension& GetCloseIconSize() const
69     {
70         auto& attribute = static_cast<SearchAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
71         return attribute.closeIconSize;
72     }
73 
SetCloseIconHotZoneHorizontal(const Dimension & closeIconHotZoneHorizontal)74     void SetCloseIconHotZoneHorizontal(const Dimension& closeIconHotZoneHorizontal)
75     {
76         auto& attribute = MaybeResetAttribute<SearchAttribute>(AttributeTag::SPECIALIZED_ATTR);
77         attribute.closeIconHotZoneHorizontal = closeIconHotZoneHorizontal;
78     }
79 
GetCloseIconHotZoneHorizontal()80     const Dimension& GetCloseIconHotZoneHorizontal() const
81     {
82         auto& attribute = static_cast<SearchAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
83         return attribute.closeIconHotZoneHorizontal;
84     }
85 
GetSearchText()86     const std::string& GetSearchText() const
87     {
88         auto& attribute = static_cast<SearchAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
89         return attribute.searchText;
90     }
91 
SetSearchText(const std::string & searchText)92     void SetSearchText(const std::string& searchText)
93     {
94         auto& attribute = MaybeResetAttribute<SearchAttribute>(AttributeTag::SPECIALIZED_ATTR);
95         attribute.searchText = searchText;
96     }
97 
GetCloseIconSrc()98     const std::string& GetCloseIconSrc() const
99     {
100         auto& attribute = static_cast<SearchAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR));
101         return attribute.closeIconSrc;
102     }
103 
SetCloseIconSrc(const std::string & closeIconSrc)104     void SetCloseIconSrc(const std::string& closeIconSrc)
105     {
106         auto& attribute = MaybeResetAttribute<SearchAttribute>(AttributeTag::SPECIALIZED_ATTR);
107         attribute.closeIconSrc = closeIconSrc;
108     }
109 
SetChangeEventId(const EventMarker & changeEventId)110     void SetChangeEventId(const EventMarker& changeEventId)
111     {
112         auto& event = MaybeResetEvent<SearchEvent>(EventTag::SPECIALIZED_EVENT);
113         event.changeEventId = changeEventId;
114     }
115 
GetChangeEventId()116     const EventMarker& GetChangeEventId() const
117     {
118         auto& event = static_cast<SearchEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
119         return event.changeEventId;
120     }
121 
SetSubmitEventId(const EventMarker & submitEventId)122     void SetSubmitEventId(const EventMarker& submitEventId)
123     {
124         auto& event = MaybeResetEvent<SearchEvent>(EventTag::SPECIALIZED_EVENT);
125         event.submitEventId = submitEventId;
126     }
127 
GetSubmitEventId()128     const EventMarker& GetSubmitEventId() const
129     {
130         auto& event = static_cast<SearchEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
131         return event.submitEventId;
132     }
133 
SetSubmitEvent(const std::function<void (const std::string &)> & submitEvent)134     void SetSubmitEvent(const std::function<void(const std::string&)>& submitEvent)
135     {
136         auto& event = MaybeResetEvent<SearchEvent>(EventTag::SPECIALIZED_EVENT);
137         event.submitEvent = submitEvent;
138     }
139 
GetSubmitEvent()140     const std::function<void(const std::string&)>& GetSubmitEvent() const
141     {
142         auto& event = static_cast<SearchEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT));
143         return event.submitEvent;
144     }
145 
GetDecoration()146     RefPtr<Decoration> GetDecoration() const
147     {
148         auto& style = static_cast<SearchStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
149         return style.decoration;
150     }
151 
SetDecoration(const RefPtr<Decoration> & decoration)152     void SetDecoration(const RefPtr<Decoration>& decoration)
153     {
154         auto& style = MaybeResetStyle<SearchStyle>(StyleTag::SPECIALIZED_STYLE);
155         style.decoration = decoration;
156     }
157 
GetHoverColor()158     const Color& GetHoverColor() const
159     {
160         auto& style = static_cast<SearchStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
161         return style.hoverColor;
162     }
163 
SetHoverColor(const Color & hoverColor)164     void SetHoverColor(const Color& hoverColor)
165     {
166         auto& style = MaybeResetStyle<SearchStyle>(StyleTag::SPECIALIZED_STYLE);
167         style.hoverColor = hoverColor;
168     }
169 
GetPressColor()170     const Color& GetPressColor() const
171     {
172         auto& style = static_cast<SearchStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
173         return style.pressColor;
174     }
175 
SetPressColor(const Color & pressColor)176     void SetPressColor(const Color& pressColor)
177     {
178         auto& style = MaybeResetStyle<SearchStyle>(StyleTag::SPECIALIZED_STYLE);
179         style.pressColor = pressColor;
180     }
181 
GetTextFieldDeclaration()182     const RefPtr<TextFieldDeclaration>& GetTextFieldDeclaration() const
183     {
184         return textFieldDeclaration_;
185     }
186 
SetTextFieldDeclaration(const RefPtr<TextFieldDeclaration> & textFieldDeclaration)187     void SetTextFieldDeclaration(const RefPtr<TextFieldDeclaration>& textFieldDeclaration)
188     {
189         textFieldDeclaration_ = textFieldDeclaration;
190     }
191 
GetTextEditController()192     const RefPtr<TextEditController>& GetTextEditController() const
193     {
194         return textEditController_;
195     }
196 
SetTextEditController(const RefPtr<TextEditController> & controller)197     void SetTextEditController(const RefPtr<TextEditController>& controller)
198     {
199         textEditController_ = controller;
200     }
201 
SetPlaceHoldStyle(const TextStyle & textstyle)202     void SetPlaceHoldStyle(const TextStyle& textstyle)
203     {
204         auto& style = MaybeResetStyle<SearchStyle>(StyleTag::SPECIALIZED_STYLE);
205         style.placeHoldStyle = textstyle;
206     }
207 
SetEditingStyle(const TextStyle & textstyle)208     void SetEditingStyle(const TextStyle& textstyle)
209     {
210         auto& style = MaybeResetStyle<SearchStyle>(StyleTag::SPECIALIZED_STYLE);
211         style.editingStyle = textstyle;
212     }
213 
GetPlaceHoldStyle()214     const TextStyle& GetPlaceHoldStyle() const
215     {
216         auto& style = static_cast<SearchStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
217         return style.placeHoldStyle;
218     }
219 
GetEditingStyle()220     const TextStyle& GetEditingStyle() const
221     {
222         auto& style = static_cast<SearchStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE));
223         return style.editingStyle;
224     }
225 
226 protected:
227     void InitSpecialized() override;
228     bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override;
229     bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override;
230     bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override;
231     void CallSpecializedMethod(const std::string& method, const std::string& args) override;
232 
233 private:
234     bool hasBoxRadius_ = false;
235     bool isPaddingChanged_ = true;
236     RefPtr<TextEditController> textEditController_;
237     RefPtr<TextFieldDeclaration> textFieldDeclaration_;
238 };
239 
240 } // namespace OHOS::Ace
241 
242 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_SEARCH_SEARCH_DECLARATION_H
243