• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_paint_method.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 
35 class SearchPattern : public Pattern {
36     DECLARE_ACE_TYPE(SearchPattern, Pattern);
37 
38 public:
39     SearchPattern() = default;
40     ~SearchPattern() override = default;
41 
IsAtomicNode()42     bool IsAtomicNode() const override
43     {
44         return false;
45     }
46 
47     // search pattern needs softkeyboard, override function.
NeedSoftKeyboard()48     bool NeedSoftKeyboard() const override
49     {
50         return true;
51     }
52 
GetNeedToRequestKeyboardOnFocus()53     bool GetNeedToRequestKeyboardOnFocus()
54     {
55         auto pattern = textField_->GetPattern();
56         CHECK_NULL_RETURN(pattern, false);
57         auto curPattern = DynamicCast<TextFieldPattern>(pattern);
58         return curPattern->GetNeedToRequestKeyboardOnFocus();
59     }
60 
CreateLayoutProperty()61     RefPtr<LayoutProperty> CreateLayoutProperty() override
62     {
63         return MakeRefPtr<SearchLayoutProperty>();
64     }
65 
CreateLayoutAlgorithm()66     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
67     {
68         return MakeRefPtr<SearchLayoutAlgorithm>();
69     }
70 
CreateNodePaintMethod()71     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
72     {
73         auto paintMethod = MakeRefPtr<SearchPaintMethod>(buttonSize_, searchButton_, isSearchButtonEnabled_);
74         return paintMethod;
75     }
76 
CreateEventHub()77     RefPtr<EventHub> CreateEventHub() override
78     {
79         return MakeRefPtr<SearchEventHub>();
80     }
81 
82     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& /*config*/) override;
83 
GetSearchController()84     const RefPtr<TextFieldController>& GetSearchController()
85     {
86         return searchController_;
87     }
88 
SetSearchController(const RefPtr<TextFieldController> & searchController)89     void SetSearchController(const RefPtr<TextFieldController>& searchController)
90     {
91         searchController_ = searchController;
92     }
93 
GetTextFieldOffset()94     const OffsetF& GetTextFieldOffset() const
95     {
96         return textFieldOffset_;
97     }
98 
99     FocusPattern GetFocusPattern() const override;
100 
101     bool HandleInputChildOnFocus() const;
102 
103     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override;
104 
ConvertCopyOptionsToString(CopyOptions copyOptions)105     static std::string ConvertCopyOptionsToString(CopyOptions copyOptions)
106     {
107         std::string result;
108         switch (copyOptions) {
109             case CopyOptions::None:
110                 result = "CopyOptions.None";
111                 break;
112             case CopyOptions::InApp:
113                 result = "CopyOptions.InApp";
114                 break;
115             case CopyOptions::Local:
116                 result = "CopyOptions.Local";
117                 break;
118             case CopyOptions::Distributed:
119                 result = "CopyOptions.Distributed";
120                 break;
121             default:
122                 break;
123         }
124         return result;
125     }
126 
127     enum class FocusChoice { SEARCH = 0, CANCEL_BUTTON, SEARCH_BUTTON };
128 
129     void UpdateChangeEvent(const std::string& value);
130 
SetCancelButtonNode(const RefPtr<FrameNode> & cancelButtonNode)131     void SetCancelButtonNode(const RefPtr<FrameNode>& cancelButtonNode)
132     {
133         cancelButtonNode_ = cancelButtonNode;
134     }
135 
SetButtonNode(const RefPtr<FrameNode> & buttonNode)136     void SetButtonNode(const RefPtr<FrameNode>& buttonNode)
137     {
138         buttonNode_ = buttonNode;
139     }
140 
SetTextFieldNode(const RefPtr<FrameNode> & textField)141     void SetTextFieldNode(const RefPtr<FrameNode>& textField)
142     {
143         textField_ = textField;
144     }
145 
146     void ResetDragOption() override;
147     void OnColorConfigurationUpdate() override;
148 
149 private:
150     void OnModifyDone() override;
151     void OnAfterModifyDone() override;
152     void InitButtonAndImageClickEvent();
153     void InitCancelButtonClickEvent();
154     void InitTextFieldClickEvent();
155     void InitSearchController();
156     void OnClickButtonAndImage();
157     void OnClickCancelButton();
158     void OnClickTextField();
159     void HandleCaretPosition(int32_t caretPosition);
160     int32_t HandleGetCaretIndex();
161     NG::OffsetF HandleGetCaretPosition();
162     void HandleTextContentRect(Rect& rect);
163     int32_t HandleTextContentLines();
164     void StopEditing();
165     // Init key event
166     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
167     bool OnKeyEvent(const KeyEvent& event);
168     void PaintFocusState(bool recoverFlag = false);
169     void GetInnerFocusPaintRect(RoundRect& paintRect);
170     void RequestKeyboard();
171     // Init touch and hover event
172     void InitTextFieldValueChangeEvent();
173     void InitTextFieldDragEvent();
174     void RemoveDragFrameNodeFromManager();
175     void InitButtonTouchEvent(RefPtr<TouchEventImpl>& touchEvent, int32_t childId);
176     void InitButtonMouseEvent(RefPtr<InputEvent>& inputEvent, int32_t childId);
177     void HandleBackgroundColor();
178     void HandleEnabled();
179     void HandleTouchableAndHitTestMode();
180     void InitButtonMouseAndTouchEvent();
181     void SetMouseStyle(MouseFormat format);
182     void OnButtonTouchDown(int32_t childId);
183     void OnButtonTouchUp(int32_t childId);
184     void HandleButtonMouseEvent(bool isHover, int32_t childId);
185     void ClearButtonStyle(int32_t childId);
186 
187     void ToJsonValueForTextField(std::unique_ptr<JsonValue>& json) const;
188     void ToJsonValueForSearchIcon(std::unique_ptr<JsonValue>& json) const;
189     void ToJsonValueForCancelButton(std::unique_ptr<JsonValue>& json) const;
190     void ToJsonValueForSearchButtonOption(std::unique_ptr<JsonValue>& json) const;
191     void ToJsonValueForCursor(std::unique_ptr<JsonValue>& json) const;
192 
193     void AnimateTouchAndHover(RefPtr<RenderContext>& renderContext, float startOpacity, float endOpacity,
194         int32_t duration, const RefPtr<Curve>& curve);
195     void InitFocusEvent(const RefPtr<FocusHub>& focusHub);
196     void HandleFocusEvent(bool forwardFocusMovement, bool backwardFocusMovement);
197     void HandleBlurEvent();
198     void InitClickEvent();
199     void HandleClickEvent(GestureEvent& info);
200     uint32_t GetMaxLength() const;
201     std::string searchButton_;
202     SizeF searchSize_;
203     OffsetF searchOffset_;
204     SizeF buttonSize_;
205     OffsetF buttonOffset_;
206     SizeF cancelButtonSize_;
207     OffsetF cancelButtonOffset_;
208     SizeF textFieldSize_;
209     OffsetF textFieldOffset_;
210     RefPtr<ClickEvent> imageClickListener_;
211     RefPtr<ClickEvent> buttonClickListener_;
212     RefPtr<ClickEvent> cancelButtonClickListener_;
213     RefPtr<ClickEvent> textFieldClickListener_;
214     RefPtr<TextFieldController> searchController_;
215     FocusChoice focusChoice_ = FocusChoice::SEARCH;
216 
217     RefPtr<TouchEventImpl> searchButtonTouchListener_;
218     RefPtr<TouchEventImpl> cancelButtonTouchListener_;
219     RefPtr<InputEvent> searchButtonMouseEvent_;
220     RefPtr<InputEvent> cancelButtonMouseEvent_;
221     RefPtr<InputEvent> textFieldHoverEvent_ = nullptr;
222     RefPtr<ClickEvent> clickListener_;
223 
224     bool isCancelButtonHover_ = false;
225     bool isSearchButtonHover_ = false;
226     bool isSearchButtonEnabled_ = false;
227 
228     RefPtr<FrameNode> cancelButtonNode_;
229     RefPtr<FrameNode> buttonNode_;
230     RefPtr<FrameNode> textField_;
231 };
232 
233 } // namespace OHOS::Ace::NG
234 
235 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SEARCH_SEARCH_PATTERN_H
236