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
16 #include "core/components_ng/pattern/text_field/text_field_foreground_modifier.h"
17
18 #include "base/geometry/dimension.h"
19 #include "core/components/common/properties/color.h"
20 #include "core/components_ng/base/modifier.h"
21 #include "core/components_ng/pattern/text_field/text_field_pattern.h"
22 #include "core/components_ng/render/drawing_forward.h"
23
24 namespace OHOS::Ace::NG {
25 namespace {
26 constexpr int32_t OFFESET_VALUE = 2;
27 }
TextFieldForegroundModifier(const WeakPtr<OHOS::Ace::NG::Pattern> & pattern)28 TextFieldForegroundModifier::TextFieldForegroundModifier(const WeakPtr<OHOS::Ace::NG::Pattern>& pattern)
29 : pattern_(pattern)
30 {
31 innerBorderWidth_ = AceType::MakeRefPtr<PropertyFloat>(0.0f);
32 AttachProperty(innerBorderWidth_);
33 }
34
onDraw(DrawingContext & context)35 void TextFieldForegroundModifier::onDraw(DrawingContext& context)
36 {
37 auto textFieldPattern = DynamicCast<TextFieldPattern>(pattern_.Upgrade());
38 CHECK_NULL_VOID(textFieldPattern);
39 auto paintProperty = textFieldPattern->GetPaintProperty<TextFieldPaintProperty>();
40 CHECK_NULL_VOID(paintProperty);
41 CHECK_NULL_VOID(paintProperty->HasInnerBorderColor());
42 CHECK_NULL_VOID(innerBorderWidth_);
43
44 auto host = textFieldPattern->GetHost();
45 CHECK_NULL_VOID(host);
46 auto layoutProperty = textFieldPattern->GetLayoutProperty<TextFieldLayoutProperty>();
47 CHECK_NULL_VOID(layoutProperty);
48 auto renderContext = host->GetRenderContext();
49 CHECK_NULL_VOID(renderContext);
50 auto& canvas = context.canvas;
51 RSPen pen;
52 pen.SetColor(paintProperty->GetInnerBorderColorValue(Color()).GetValue());
53 auto width = innerBorderWidth_->Get();
54 pen.SetWidth(width);
55 pen.SetAntiAlias(true);
56 auto textFrameRect = textFieldPattern->GetFrameRect();
57 auto rsRadius = MakeRRadius(renderContext->GetBorderRadius().value_or(BorderRadiusProperty()), width);
58 RSRoundRect rrect(
59 RSRect(width / OFFESET_VALUE, width / OFFESET_VALUE, textFrameRect.Width() - width / OFFESET_VALUE,
60 textFrameRect.Height() - width / OFFESET_VALUE),
61 rsRadius);
62 canvas.AttachPen(pen);
63 canvas.DrawRoundRect(rrect);
64 canvas.DetachPen();
65 }
66
MakeRRadius(const BorderRadiusProperty & border,float borderWidth) const67 std::vector<RSPoint> TextFieldForegroundModifier::MakeRRadius(
68 const BorderRadiusProperty& border, float borderWidth) const
69 {
70 std::vector<RSPoint> rectRadii;
71 rectRadii.resize(RSRoundRect::CORNER_NUMBER);
72 auto topLeft = static_cast<float>(border.radiusTopLeft.value_or(Dimension()).ConvertToPx()) - borderWidth;
73 rectRadii[RSRoundRect::TOP_LEFT_POS] = RSPoint(topLeft, topLeft);
74 auto topRight = static_cast<float>(border.radiusTopRight.value_or(Dimension()).ConvertToPx()) - borderWidth;
75 rectRadii[RSRoundRect::TOP_RIGHT_POS] = RSPoint(topRight, topRight);
76 auto bottomRight = static_cast<float>(border.radiusBottomRight.value_or(Dimension()).ConvertToPx()) - borderWidth;
77 rectRadii[RSRoundRect::BOTTOM_RIGHT_POS] = RSPoint(bottomRight, bottomRight);
78 auto bottomLeft = static_cast<float>(border.radiusBottomLeft.value_or(Dimension()).ConvertToPx()) - borderWidth;
79 rectRadii[RSRoundRect::BOTTOM_LEFT_POS] = RSPoint(bottomLeft, bottomLeft);
80 return rectRadii;
81 }
82 } // namespace OHOS::Ace::NG