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 void SetKeyboardAppearanceConfig(const KeyboardAppearanceConfig& config); 186 void OnColorModeChange(uint32_t colorMode) override; 187 void UpdatePropertyImpl(const std::string& key, RefPtr<PropertyValueBase> value) override; 188 void UpdatePlaceholderResource(const std::u16string& value); 189 void UpdateTextResource(const std::u16string& value); 190 void UpdateSearchButtonValueResource(const std::string value); 191 void UpdateSearchButtonFontSizeResource(const Dimension& value); 192 void UpdateSearchButtonFontColorResource(const Color& value); 193 void UpdateFontColorResource(const Color& value); 194 void UpdateCaretColorResource(const Color& value); 195 void UpdateCaretWidthResource(const Dimension& value); 196 void UpdatePlaceholderColorResource(const Color& value); 197 void UpdatePlaceholderFontSizeResource(const Dimension& value); 198 void UpdateDecorationColorResource(const Color& value); 199 void UpdateMinFontSizeResource(const Dimension& value); 200 void UpdateMaxFontSizeResource(const Dimension& value); 201 void UpdateLetterSpacingResource(const Dimension& value); 202 void UpdateLineHeightResource(const Dimension& value); 203 void UpdateMinFontScaleResource(const float value); 204 void UpdateMaxFontScaleResource(const float value); 205 void UpdateSelectedBackgroundColorResource(const Color& value); 206 void UpdateTextIndentResource(const Dimension& value); 207 void UpdateInputFilterResource(const std::string& value); 208 void UpdateFontSizeResource(const Dimension& value); 209 void UpdateBorderResource() override; 210 void ProcessTextFieldDefaultStyleAndBehaviors(); 211 void ProcessTextFieldDefaultStyleAndBehaviorsMultiThread(); 212 213 private: 214 void OnModifyDone() override; 215 void OnAfterModifyDone() override; 216 void SetAccessibilityAction(); 217 void SetAccessibilityClearAction(); 218 void SetSearchFieldAccessibilityAction(); 219 void SetSearchButtonAccessibilityAction(); 220 void InitButtonAndImageClickEvent(); 221 void InitCancelButtonClickEvent(); 222 void InitTextFieldClickEvent(); 223 void InitSearchController(); 224 void OnClickButtonAndImage(); 225 void OnClickCancelButton(); 226 void OnClickTextField(); 227 void HandleCaretPosition(int32_t caretPosition); 228 void HandleTextContentRect(Rect& rect); 229 int32_t HandleGetCaretIndex(); 230 NG::OffsetF HandleGetCaretPosition(); 231 int32_t HandleTextContentLines(); 232 void StopEditing(); 233 // Init key event 234 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 235 bool OnKeyEvent(const KeyEvent& event); 236 void PaintFocusState(bool recoverFlag = false); 237 void GetInnerFocusPaintRect(RoundRect& paintRect); 238 void PaintSearchFocusState(); 239 void GetSearchFocusPaintRect(RoundRect& paintRect); 240 void RequestKeyboard(); 241 // Init touch and hover event 242 void InitTextFieldValueChangeEvent(); 243 void InitTextFieldDragEvent(); 244 void InitButtonTouchEvent(RefPtr<TouchEventImpl>& touchEvent, int32_t childId); 245 void InitButtonMouseEvent(RefPtr<InputEvent>& inputEvent, int32_t childId); 246 void HandleBackgroundColor(); 247 void HandleEnabled(); 248 void HandleTouchableAndHitTestMode(); 249 void InitButtonMouseAndTouchEvent(); 250 void SetMouseStyle(MouseFormat format); 251 void OnButtonTouchDown(int32_t childId); 252 void OnButtonTouchUp(int32_t childId); 253 void HandleButtonMouseEvent(bool isHover, int32_t childId); 254 void ClearButtonStyle(int32_t childId); 255 256 void ToJsonValueForTextField(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const; 257 void ToJsonValueForSearchIcon(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const; 258 void ToJsonValueForCancelButton(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const; 259 void ToJsonValueForSearchButtonOption(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const; 260 void ToJsonValueForCursor(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const; 261 void ToJsonValueForCancelButtonStyle( 262 std::unique_ptr<JsonValue>& cancelButtonJson, const CancelButtonStyle& cancelButtonStyle) const; 263 std::string SymbolColorToString(std::vector<Color>& colors) const; 264 265 void AnimateTouchAndHover(RefPtr<RenderContext>& renderContext, float startOpacity, float endOpacity, 266 int32_t duration, const RefPtr<Curve>& curve); 267 void AnimateSearchTouchAndHover(RefPtr<RenderContext>& renderContext, Color& blendColorFrom, Color& blendColorTo, 268 int32_t duration, const RefPtr<Curve>& curve); 269 void InitFocusEvent(const RefPtr<FocusHub>& focusHub); 270 void HandleFocusEvent(bool forwardFocusMovement, bool backwardFocusMovement); 271 void HandleBlurEvent(); 272 void InitClickEvent(); 273 void HandleClickEvent(GestureEvent& info); 274 void UpdateIconChangeEvent(); 275 bool IsEventEnabled(const std::u16string& textValue, int16_t style); 276 void InitAllEvent(); 277 void InitHoverEvent(); 278 void InitTouchEvent(); 279 void InitSearchTheme(); 280 void OnTouchDownOrUp(bool isDown); 281 void HandleHoverEvent(bool isHover); 282 283 void UpdateSearchSymbolIconColor(); 284 void UpdateCancelSymbolIconColor(); 285 286 void CreateOrUpdateSymbol(int32_t index, bool isCreateNode, bool isFromModifier); 287 void CreateOrUpdateImage(int32_t index, bool isCreateNode); 288 void UpdateImageIconProperties(RefPtr<FrameNode>& frameNode, int32_t index); 289 void UpdateImageIconNode(int32_t index); 290 void UpdateSymbolIconNode(int32_t index); 291 void UpdateSymbolIconProperties(RefPtr<FrameNode>& frameNode, int32_t index); 292 293 void CreateOrUpdateSymbol(int32_t index, bool isCreateNode); 294 void CreateOrUpdateImage(int32_t index, const std::string& src, bool isCreateNode, const std::string& bundleName, 295 const std::string& moduleName); 296 void HandleImageLayoutProperty(RefPtr<FrameNode>& frameNode, int32_t index, const std::string& src, 297 const std::string& bundleName, const std::string& moduleName); 298 bool IsSymbolIcon(int32_t index); 299 void UpdateIconNode( 300 int32_t index, const std::string& src, const std::string& bundleName, const std::string& moduleName); 301 void UpdateIconSrc(int32_t index, const std::string& src); 302 void UpdateIconColor(int32_t index, const Color& color); 303 void UpdateIconSize(int32_t index, const Dimension& value); 304 void UpdateDivider(); 305 void UpdateCancelButton(); 306 void UpdateDividerColorMode(); 307 void UpdateCancelButtonColorMode(); 308 void UpdateCancelButtonStatus(const std::u16string& value, int16_t style = -1); 309 Color GetDefaultIconColor(int32_t index); 310 bool IsConsumeEvent(); 311 void HandleFocusChoiceSearch(const RefPtr<TextFieldPattern>& textFieldPattern, bool recoverFlag, 312 const RefPtr<SearchTextFieldPattern>& searchTextFieldPattern); 313 314 bool IsSearchAttached(); 315 RefPtr<SearchTheme> GetTheme() const; 316 317 void OnAttachToMainTree() override; 318 void OnAttachToMainTreeMultiThread(); 319 320 uint32_t GetMaxLength() const; 321 std::string SearchTypeToString() const; 322 void InitMargin(const RefPtr<SearchLayoutProperty>& property); 323 std::string searchButton_; 324 SizeF searchSize_; 325 OffsetF searchOffset_; 326 SizeF buttonSize_; 327 OffsetF buttonOffset_; 328 SizeF cancelButtonSize_; 329 OffsetF cancelButtonOffset_; 330 SizeF textFieldSize_; 331 OffsetF textFieldOffset_; 332 RefPtr<ClickEvent> imageClickListener_; 333 RefPtr<ClickEvent> buttonClickListener_; 334 RefPtr<ClickEvent> cancelButtonClickListener_; 335 RefPtr<ClickEvent> textFieldClickListener_; 336 RefPtr<TextFieldController> searchController_; 337 FocusChoice focusChoice_ = FocusChoice::SEARCH; 338 339 RefPtr<TouchEventImpl> searchTouchListener_; 340 RefPtr<TouchEventImpl> searchButtonTouchListener_; 341 RefPtr<TouchEventImpl> cancelButtonTouchListener_; 342 RefPtr<InputEvent> searchHoverListener_; 343 RefPtr<InputEvent> searchButtonMouseEvent_; 344 RefPtr<InputEvent> cancelButtonMouseEvent_; 345 RefPtr<InputEvent> textFieldHoverEvent_ = nullptr; 346 RefPtr<ClickEvent> clickListener_; 347 348 bool isSearchHover_ = false; 349 bool isSearchPress_ = false; 350 bool isCancelButtonHover_ = false; 351 bool isSearchButtonHover_ = false; 352 bool isSearchButtonEnabled_ = false; 353 bool isFocusPlaceholderColorSet_ = false; 354 bool isFocusBgColorSet_ = false; 355 bool isFocusIconColorSet_ = false; 356 bool isFocusTextColorSet_ = false; 357 bool directionKeysMoveFocusOut_ = false; 358 Color searchNormalColor_; 359 Color transparentColor_ = Color::TRANSPARENT; 360 361 WeakPtr<FrameNode> cancelButtonNode_; 362 WeakPtr<FrameNode> buttonNode_; 363 WeakPtr<FrameNode> textField_; 364 WeakPtr<FrameNode> searchIcon_; 365 WeakPtr<FrameNode> cancelIcon_; 366 WeakPtr<SearchNode> searchNode_; 367 WeakPtr<SearchTheme> searchTheme_; 368 369 // ----- multi thread state variables ----- 370 bool processTextFieldDefaultStyleAndBehaviorsMultiThread_ = false; 371 // ----- multi thread state variables end ----- 372 }; 373 374 } // namespace OHOS::Ace::NG 375 376 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SEARCH_SEARCH_PATTERN_H 377