1 /*
2  * Copyright (c) 2024 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 #include "core/interfaces/native/node/node_container_span_modifier.h"
16 
17 #include "core/components/common/layout/constants.h"
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/base/ui_node.h"
20 #include "core/components_ng/base/view_abstract.h"
21 #include "core/components_ng/pattern/text/span_model_ng.h"
22 #include "core/pipeline/base/element_register.h"
23 
24 namespace OHOS::Ace::NG {
25 namespace {
26 constexpr int NUM_0 = 0;
27 constexpr int NUM_1 = 1;
28 constexpr int NUM_2 = 2;
29 constexpr int NUM_3 = 3;
30 constexpr int DEFAULT_LENGTH = 4;
31 
SetContainerSpanTextBackgroundStyle(ArkUINodeHandle node,ArkUI_Uint32 color,const ArkUI_Float32 * values,const ArkUI_Int32 * units,ArkUI_Int32 length)32 void SetContainerSpanTextBackgroundStyle(
33     ArkUINodeHandle node, ArkUI_Uint32 color, const ArkUI_Float32* values, const ArkUI_Int32* units, ArkUI_Int32 length)
34 {
35     auto* uiNode = reinterpret_cast<UINode*>(node);
36     CHECK_NULL_VOID(uiNode);
37     if (length != DEFAULT_LENGTH) {
38         return;
39     }
40     TextBackgroundStyle font;
41     NG::BorderRadiusProperty borderRadius;
42     borderRadius.radiusTopLeft = Dimension(values[NUM_0], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_0]));
43     borderRadius.radiusTopRight = Dimension(values[NUM_1], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_1]));
44     borderRadius.radiusBottomLeft = Dimension(values[NUM_2], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_2]));
45     borderRadius.radiusBottomRight = Dimension(values[NUM_3], static_cast<OHOS::Ace::DimensionUnit>(units[NUM_3]));
46     font.backgroundColor = Color(color);
47     font.backgroundRadius = borderRadius;
48     font.backgroundRadius->multiValued = true;
49     SpanModelNG::SetTextBackgroundStyleByBaseSpan(uiNode, font);
50 }
51 
ResetContainerSpanTextBackgroundStyle(ArkUINodeHandle node)52 void ResetContainerSpanTextBackgroundStyle(ArkUINodeHandle node)
53 {
54     auto* uiNode = reinterpret_cast<UINode*>(node);
55     CHECK_NULL_VOID(uiNode);
56     TextBackgroundStyle font;
57     NG::BorderRadiusProperty borderRadius;
58     borderRadius.radiusTopLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
59     borderRadius.radiusTopRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
60     borderRadius.radiusBottomLeft = Dimension(0, OHOS::Ace::DimensionUnit::VP);
61     borderRadius.radiusBottomRight = Dimension(0, OHOS::Ace::DimensionUnit::VP);
62     font.backgroundColor = Color(0x00000000);
63     font.backgroundRadius = borderRadius;
64     font.backgroundRadius->multiValued = true;
65     SpanModelNG::SetTextBackgroundStyleByBaseSpan(uiNode, font);
66 }
67 
68 } // namespace
69 
70 namespace NodeModifier {
GetContainerSpanModifier()71 const ArkUIContainerSpanModifier* GetContainerSpanModifier()
72 {
73     static const ArkUIContainerSpanModifier modifier = {
74         SetContainerSpanTextBackgroundStyle,
75         ResetContainerSpanTextBackgroundStyle };
76     return &modifier;
77 }
78 
GetCJUIContainerSpanModifier()79 const CJUIContainerSpanModifier* GetCJUIContainerSpanModifier()
80 {
81     static const CJUIContainerSpanModifier modifier = {
82         SetContainerSpanTextBackgroundStyle,
83         ResetContainerSpanTextBackgroundStyle };
84     return &modifier;
85 }
86 } // namespace NodeModifier
87 } // namespace OHOS::Ace::NG