1 /*
2  * Copyright (c) 2022-2023 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_PATTERNS_SEARCH_SEARCH_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SEARCH_SEARCH_PATTERN_H
18 
19 #include "base/geometry/ng/offset_t.h"
20 #include "base/memory/referenced.h"
21 #include "base/mousestyle/mouse_style.h"
22 #include "core/components/text_field/text_field_controller.h"
23 #include "core/components_ng/pattern/image/image_layout_property.h"
24 #include "core/components_ng/pattern/pattern.h"
25 #include "core/components_ng/pattern/search/search_event_hub.h"
26 #include "core/components_ng/pattern/search/search_layout_algorithm.h"
27 #include "core/components_ng/pattern/search/search_layout_property.h"
28 #include "core/components_ng/pattern/search/search_node.h"
29 #include "core/components_ng/pattern/text_field/text_field_controller.h"
30 #include "core/components_ng/pattern/text_field/text_field_layout_property.h"
31 #include "core/components_ng/pattern/text_field/text_field_pattern.h"
32 
33 namespace OHOS::Ace::NG {
34 class InspectorFilter;
35 
36 class SearchPattern : public Pattern {
37     DECLARE_ACE_TYPE(SearchPattern, Pattern);
38 
39 public:
40     SearchPattern() = default;
41     ~SearchPattern() override = default;
42 
IsAtomicNode()43     bool IsAtomicNode() const override
44     {
45         return false;
46     }
47     // search pattern needs softkeyboard, override function.
NeedSoftKeyboard()48     bool NeedSoftKeyboard() const override
49     {
50         return true;
51     }
52 
NeedToRequestKeyboardOnFocus()53     bool NeedToRequestKeyboardOnFocus() const override
54     {
55         auto textField = textField_.Upgrade();
56         CHECK_NULL_RETURN(textField, false);
57         auto pattern = textField->GetPattern();
58         CHECK_NULL_RETURN(pattern, false);
59         auto curPattern = DynamicCast<TextFieldPattern>(pattern);
60         return curPattern->NeedToRequestKeyboardOnFocus();
61     }
62 
CreateLayoutProperty()63     RefPtr<LayoutProperty> CreateLayoutProperty() override
64     {
65         return MakeRefPtr<SearchLayoutProperty>();
66     }
67 
CreateLayoutAlgorithm()68     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
69     {
70         return MakeRefPtr<SearchLayoutAlgorithm>();
71     }
72 
CreateEventHub()73     RefPtr<EventHub> CreateEventHub() override
74     {
75         return MakeRefPtr<SearchEventHub>();
76     }
77 
78     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& /*config*/) override;
79 
GetSearchController()80     const RefPtr<TextFieldController>& GetSearchController()
81     {
82         return searchController_;
83     }
84 
SetSearchController(const RefPtr<TextFieldController> & searchController)85     void SetSearchController(const RefPtr<TextFieldController>& searchController)
86     {
87         searchController_ = searchController;
88     }
89 
GetTextFieldOffset()90     const OffsetF& GetTextFieldOffset() const
91     {
92         return textFieldOffset_;
93     }
94 
95     FocusPattern GetFocusPattern() const override;
96 
97     bool HandleInputChildOnFocus() const;
98 
99     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
100 
ConvertCopyOptionsToString(CopyOptions copyOptions)101     static std::string ConvertCopyOptionsToString(CopyOptions copyOptions)
102     {
103         std::string result;
104         switch (copyOptions) {
105             case CopyOptions::None:
106                 result = "CopyOptions.None";
107                 break;
108             case CopyOptions::InApp:
109                 result = "CopyOptions.InApp";
110                 break;
111             case CopyOptions::Local:
112                 result = "CopyOptions.Local";
113                 break;
114             case CopyOptions::Distributed:
115                 result = "CopyOptions.Distributed";
116                 break;
117             default:
118                 break;
119         }
120         return result;
121     }
122 
123     enum class FocusChoice { SEARCH = 0, CANCEL_BUTTON, SEARCH_BUTTON };
124 
125     void UpdateChangeEvent(const std::string& value, int16_t style = -1);
126 
SetCancelButtonNode(const RefPtr<FrameNode> & cancelButtonNode)127     void SetCancelButtonNode(const RefPtr<FrameNode>& cancelButtonNode)
128     {
129         cancelButtonNode_ = AceType::WeakClaim(AceType::RawPtr(cancelButtonNode));
130     }
131 
SetButtonNode(const RefPtr<FrameNode> & buttonNode)132     void SetButtonNode(const RefPtr<FrameNode>& buttonNode)
133     {
134         buttonNode_ = AceType::WeakClaim(AceType::RawPtr(buttonNode));
135     }
136 
SetTextFieldNode(const RefPtr<FrameNode> & textField)137     void SetTextFieldNode(const RefPtr<FrameNode>& textField)
138     {
139         textField_ = AceType::WeakClaim(AceType::RawPtr(textField));
140     }
141 
SetSearchIconNode(const RefPtr<FrameNode> & searchIcon)142     void SetSearchIconNode(const RefPtr<FrameNode>& searchIcon)
143     {
144         searchIcon_ = AceType::WeakClaim(AceType::RawPtr(searchIcon));
145     }
146 
SetCancelIconNode(const RefPtr<FrameNode> & cancelIcon)147     void SetCancelIconNode(const RefPtr<FrameNode>& cancelIcon)
148     {
149         cancelIcon_ = AceType::WeakClaim(AceType::RawPtr(cancelIcon));
150     }
151 
SetSearchNode(const RefPtr<SearchNode> & searchNode)152     void SetSearchNode(const RefPtr<SearchNode>& searchNode)
153     {
154         searchNode_ = AceType::WeakClaim(AceType::RawPtr(searchNode));
155     }
156 
GetSearchIconNode()157     RefPtr<FrameNode> GetSearchIconNode() const
158     {
159         return searchIcon_.Upgrade();
160     }
161 
GetCancelIconNode()162     RefPtr<FrameNode> GetCancelIconNode() const
163     {
164         return cancelIcon_.Upgrade();
165     }
166 
GetSearchNode()167     RefPtr<SearchNode> GetSearchNode() const
168     {
169         return searchNode_.Upgrade();
170     }
171 
GetIsSearchButtonEnabled()172     bool GetIsSearchButtonEnabled() const
173     {
174         return isSearchButtonEnabled_;
175     }
176 
GetButtonSize()177     const SizeF GetButtonSize() const
178     {
179         return buttonSize_;
180     }
181 
182     void ResetDragOption() override;
183     void OnColorConfigurationUpdate() override;
184 
185     void SetSearchIconSize(const Dimension& value);
186     void SetSearchIconColor(const Color& color);
187     void SetSearchSrcPath(const std::string& src, const std::string& bundleName, const std::string& moduleName);
188     void SetSearchSymbolIcon();
189     void SetSearchImageIcon(IconOptions& iconOptions);
190     void SetCancelSymbolIcon();
191     void SetCancelImageIcon(IconOptions& iconOptions);
192     void SetRightIconSrcPath(const std::string& src);
193     void SetCancelButtonStyle(const CancelButtonStyle& cancelButtonStyle);
194     void SetCancelIconSize(const Dimension& value);
195     void SetCancelIconColor(const Color& color);
196     void InitIconColorSize();
197     void InitSearchIconColorSize();
198     void InitCancelIconColorSize();
199     void CreateSearchIcon(const std::string& src);
200     void CreateCancelIcon();
201     const Dimension ConvertImageIconSizeValue(const Dimension& fontSizeValue);
202 
203 private:
204     void OnModifyDone() override;
205     void OnAfterModifyDone() override;
206     void SetAccessibilityAction();
207     void SetAccessibilityClearAction();
208     void SetSearchFieldAccessibilityAction();
209     void SetSearchButtonAccessibilityAction();
210     void InitButtonAndImageClickEvent();
211     void InitCancelButtonClickEvent();
212     void InitTextFieldClickEvent();
213     void InitSearchController();
214     void OnClickButtonAndImage();
215     void OnClickCancelButton();
216     void OnClickTextField();
217     void HandleCaretPosition(int32_t caretPosition);
218     void HandleTextContentRect(Rect& rect);
219     int32_t HandleGetCaretIndex();
220     NG::OffsetF HandleGetCaretPosition();
221     int32_t HandleTextContentLines();
222     void StopEditing();
223     // Init key event
224     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
225     bool OnKeyEvent(const KeyEvent& event);
226     void PaintFocusState(bool recoverFlag = false);
227     void GetInnerFocusPaintRect(RoundRect& paintRect);
228     void RequestKeyboard();
229     // Init touch and hover event
230     void InitTextFieldValueChangeEvent();
231     void InitTextFieldDragEvent();
232     void RemoveDragFrameNodeFromManager();
233     void InitButtonTouchEvent(RefPtr<TouchEventImpl>& touchEvent, int32_t childId);
234     void InitButtonMouseEvent(RefPtr<InputEvent>& inputEvent, int32_t childId);
235     void HandleBackgroundColor();
236     void HandleEnabled();
237     void HandleTouchableAndHitTestMode();
238     void InitButtonMouseAndTouchEvent();
239     void SetMouseStyle(MouseFormat format);
240     void OnButtonTouchDown(int32_t childId);
241     void OnButtonTouchUp(int32_t childId);
242     void HandleButtonMouseEvent(bool isHover, int32_t childId);
243     void ClearButtonStyle(int32_t childId);
244 
245     void ToJsonValueForTextField(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
246     void ToJsonValueForSearchIcon(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
247     void ToJsonValueForCancelButton(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
248     void ToJsonValueForSearchButtonOption(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
249     void ToJsonValueForCursor(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
250     void ToJsonValueForCancelButtonStyle(
251         std::unique_ptr<JsonValue>& cancelButtonJson, const CancelButtonStyle& cancelButtonStyle) const;
252     std::string SymbolColorToString(std::vector<Color>& colors) const;
253 
254     void AnimateTouchAndHover(RefPtr<RenderContext>& renderContext, float startOpacity, float endOpacity,
255         int32_t duration, const RefPtr<Curve>& curve);
256     void InitFocusEvent(const RefPtr<FocusHub>& focusHub);
257     void HandleFocusEvent(bool forwardFocusMovement, bool backwardFocusMovement);
258     void HandleBlurEvent();
259     void InitClickEvent();
260     void HandleClickEvent(GestureEvent& info);
261     void UpdateIconChangeEvent();
262     bool IsEventEnabled(const std::string& textValue, int16_t style);
263 
264     void UpdateSearchSymbolIconColor();
265     void UpdateCancelSymbolIconColor();
266 
267     void CreateOrUpdateSymbol(int32_t index, bool isCreateNode, bool isFromModifier);
268     void CreateOrUpdateImage(int32_t index, bool isCreateNode);
269     void UpdateImageIconProperties(RefPtr<FrameNode>& frameNode, int32_t index);
270     void UpdateImageIconNode(int32_t index);
271     void UpdateSymbolIconNode(int32_t index);
272     void UpdateSymbolIconProperties(RefPtr<FrameNode>& frameNode, int32_t index);
273 
274     void CreateOrUpdateSymbol(int32_t index, bool isCreateNode);
275     void CreateOrUpdateImage(int32_t index, const std::string& src, bool isCreateNode, const std::string& bundleName,
276         const std::string& moduleName);
277     void HandleImageLayoutProperty(RefPtr<FrameNode>& frameNode, int32_t index, const std::string& src,
278         const std::string& bundleName, const std::string& moduleName);
279     bool IsSymbolIcon(int32_t index);
280     void UpdateIconNode(
281         int32_t index, const std::string& src, const std::string& bundleName, const std::string& moduleName);
282     void UpdateIconSrc(int32_t index, const std::string& src);
283     void UpdateIconColor(int32_t index, const Color& color);
284     void UpdateIconSize(int32_t index, const Dimension& value);
285     const Dimension ConvertImageIconScaleLimit(const Dimension& fontSizeValue);
286     void UpdateDivider();
287     void UpdateCancelButton();
288     void UpdateDividerColorMode();
289     void UpdateCancelButtonColorMode();
290     void UpdateCancelButtonStatus(const std::string& value, int16_t style = -1);
291 
292     bool IsSearchAttached();
293 
294     uint32_t GetMaxLength() const;
295     std::string SearchTypeToString() const;
296     std::string searchButton_;
297     SizeF searchSize_;
298     OffsetF searchOffset_;
299     SizeF buttonSize_;
300     OffsetF buttonOffset_;
301     SizeF cancelButtonSize_;
302     OffsetF cancelButtonOffset_;
303     SizeF textFieldSize_;
304     OffsetF textFieldOffset_;
305     RefPtr<ClickEvent> imageClickListener_;
306     RefPtr<ClickEvent> buttonClickListener_;
307     RefPtr<ClickEvent> cancelButtonClickListener_;
308     RefPtr<ClickEvent> textFieldClickListener_;
309     RefPtr<TextFieldController> searchController_;
310     FocusChoice focusChoice_ = FocusChoice::SEARCH;
311 
312     RefPtr<TouchEventImpl> searchButtonTouchListener_;
313     RefPtr<TouchEventImpl> cancelButtonTouchListener_;
314     RefPtr<InputEvent> searchButtonMouseEvent_;
315     RefPtr<InputEvent> cancelButtonMouseEvent_;
316     RefPtr<InputEvent> textFieldHoverEvent_ = nullptr;
317     RefPtr<ClickEvent> clickListener_;
318 
319     bool isCancelButtonHover_ = false;
320     bool isSearchButtonHover_ = false;
321     bool isSearchButtonEnabled_ = false;
322 
323     WeakPtr<FrameNode> cancelButtonNode_;
324     WeakPtr<FrameNode> buttonNode_;
325     WeakPtr<FrameNode> textField_;
326     WeakPtr<FrameNode> searchIcon_;
327     WeakPtr<FrameNode> cancelIcon_;
328     WeakPtr<SearchNode> searchNode_;
329 };
330 
331 } // namespace OHOS::Ace::NG
332 
333 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SEARCH_SEARCH_PATTERN_H
334