• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "core/components_ng/pattern/search/search_pattern.h"
17 
18 #include <cstdint>
19 #include "base/geometry/dimension.h"
20 #include "base/utils/utf_helper.h"
21 #include "core/components_ng/pattern/divider/divider_layout_property.h"
22 #include "interfaces/inner_api/ui_session/ui_session_manager.h"
23 #include "base/geometry/rect.h"
24 #include "base/utils/system_properties.h"
25 #include "base/utils/utils.h"
26 #include "core/common/recorder/node_data_cache.h"
27 #include "core/components/search/search_theme.h"
28 #include "core/components_ng/base/inspector_filter.h"
29 #include "core/components_ng/base/view_stack_processor.h"
30 #include "core/components_ng/pattern/button/button_pattern.h"
31 #include "core/components_ng/pattern/divider/divider_render_property.h"
32 #include "core/components_ng/pattern/image/image_pattern.h"
33 #include "core/components_ng/pattern/search/search_model.h"
34 #include "core/components_ng/pattern/search/search_text_field.h"
35 #include "core/components_ng/pattern/text/text_layout_property.h"
36 #include "core/components_ng/pattern/text/text_pattern.h"
37 #include "core/components_ng/pattern/text_field/text_field_pattern.h"
38 #include "core/event/touch_event.h"
39 #include "core/components/theme/app_theme.h"
40 
41 namespace OHOS::Ace::NG {
42 
43 namespace {
44 constexpr int32_t TEXTFIELD_INDEX = 0;
45 constexpr float MAX_FONT_SCALE = 2.0f;
46 
InitSearchMaxFontScale(const RefPtr<FrameNode> & frameNode)47 void InitSearchMaxFontScale(const RefPtr<FrameNode>& frameNode)
48 {
49     CHECK_NULL_VOID(frameNode);
50     auto textFieldLayoutProperty = frameNode->GetLayoutProperty<TextFieldLayoutProperty>();
51     CHECK_NULL_VOID(textFieldLayoutProperty);
52     auto pipeline = frameNode->GetContext();
53     CHECK_NULL_VOID(pipeline);
54     auto maxFontScale = MAX_FONT_SCALE;
55     if (textFieldLayoutProperty->HasMaxFontScale()) {
56         maxFontScale = std::min(textFieldLayoutProperty->GetMaxFontScale().value(), maxFontScale);
57     } else if (pipeline->GetMaxAppFontScale()) {
58         maxFontScale = std::min(pipeline->GetMaxAppFontScale(), maxFontScale);
59     }
60     textFieldLayoutProperty->UpdateMaxFontScale(maxFontScale);
61 }
62 } // namespace
63 
OnAttachToMainTreeMultiThread()64 void SearchPattern::OnAttachToMainTreeMultiThread()
65 {
66     if (processTextFieldDefaultStyleAndBehaviorsMultiThread_) {
67         processTextFieldDefaultStyleAndBehaviorsMultiThread_ = false;
68         ProcessTextFieldDefaultStyleAndBehaviorsMultiThread();
69     }
70 }
71 
ProcessTextFieldDefaultStyleAndBehaviors()72 void SearchPattern::ProcessTextFieldDefaultStyleAndBehaviors()
73 {
74     processTextFieldDefaultStyleAndBehaviorsMultiThread_ = true;
75 }
76 
ProcessTextFieldDefaultStyleAndBehaviorsMultiThread()77 void SearchPattern::ProcessTextFieldDefaultStyleAndBehaviorsMultiThread()
78 {
79     auto host = GetHost();
80     CHECK_NULL_VOID(host);
81     auto frameNode = DynamicCast<FrameNode>(host->GetChildAtIndex(TEXTFIELD_INDEX));
82     CHECK_NULL_VOID(frameNode);
83     auto pipeline = frameNode->GetContext();
84     CHECK_NULL_VOID(pipeline);
85     auto pattern = frameNode->GetPattern<TextFieldPattern>();
86     CHECK_NULL_VOID(pattern);
87     auto textFieldTheme = pipeline->GetTheme<TextFieldTheme>();
88     CHECK_NULL_VOID(textFieldTheme);
89     auto renderContext = frameNode->GetRenderContext();
90     CHECK_NULL_VOID(renderContext);
91     auto textFieldPaintProperty = frameNode->GetPaintProperty<TextFieldPaintProperty>();
92     CHECK_NULL_VOID(textFieldPaintProperty);
93     auto colorMode = pipeline->GetColorMode();
94     pattern->SetOriginCursorColor(colorMode == ColorMode::DARK ? Color(0x4DFFFFFF) : Color(0x4D000000));
95     if (pipeline->GetHasPreviewTextOption()) {
96         pattern->SetSupportPreviewText(pipeline->GetSupportPreviewText());
97     }
98     textFieldPaintProperty->UpdateCursorColor(textFieldTheme->GetCursorColor());
99     textFieldPaintProperty->UpdateCursorWidth(textFieldTheme->GetCursorWidth());
100     renderContext->UpdateBackgroundColor(Color::TRANSPARENT);
101     InitSearchMaxFontScale(frameNode);
102 }
103 
104 } // namespace OHOS::Ace::NG
105