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