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