1 /*
2 * Copyright (c) 2023 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/tabs/tab_bar_modifier.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components/tab_bar/tab_theme.h"
20 #include "core/components_ng/base/modifier.h"
21 #include "core/components_ng/base/view_stack_processor.h"
22 #include "core/components_ng/pattern/tabs/tab_bar_paint_property.h"
23 #include "core/components_ng/render/drawing.h"
24 #include "core/components_ng/render/drawing_prop_convertor.h"
25 #include "core/pipeline_ng/pipeline_context.h"
26
27 namespace OHOS::Ace::NG {
TabBarModifier()28 TabBarModifier::TabBarModifier()
29 :indicatorColor_(AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor::BLACK)),
30 indicatorLeft_(AceType::MakeRefPtr<AnimatablePropertyFloat>(-1)),
31 indicatorTop_(AceType::MakeRefPtr<AnimatablePropertyFloat>(-1)),
32 indicatorWidth_(AceType::MakeRefPtr<AnimatablePropertyFloat>(-1)),
33 indicatorHeight_(AceType::MakeRefPtr<AnimatablePropertyFloat>(-1)),
34 indicatorBorderRadius_(AceType::MakeRefPtr<AnimatablePropertyFloat>(-1)),
35 indicatorMarginTop_(AceType::MakeRefPtr<AnimatablePropertyFloat>(-1)),
36 hasIndicator_(AceType::MakeRefPtr<PropertyBool>(true))
37 {
38 AttachProperty(indicatorColor_);
39 AttachProperty(indicatorLeft_);
40 AttachProperty(indicatorTop_);
41 AttachProperty(indicatorWidth_);
42 AttachProperty(indicatorHeight_);
43 AttachProperty(indicatorBorderRadius_);
44 AttachProperty(indicatorMarginTop_);
45 AttachProperty(hasIndicator_);
46 }
47
onDraw(DrawingContext & context)48 void TabBarModifier::onDraw(DrawingContext& context)
49 {
50 if ((!hasIndicator_ || hasIndicator_->Get()) && !NearZero(indicator_.Height())) {
51 PaintIndicator(context, indicator_);
52 }
53 }
54
SetIndicator(const RectF & indicator)55 void TabBarModifier::SetIndicator(const RectF& indicator)
56 {
57 indicator_ = indicator;
58 if (indicatorLeft_) {
59 indicatorLeft_->Set(indicator.GetX());
60 }
61 if (indicatorTop_) {
62 indicatorTop_->Set(indicator.GetY());
63 }
64 }
65
SetIndicatorColor(const LinearColor & indicatorColor)66 void TabBarModifier::SetIndicatorColor(const LinearColor& indicatorColor)
67 {
68 if (indicatorColor_) {
69 indicatorColor_->Set(indicatorColor);
70 }
71 }
72
SetIndicatorWidth(float indicatorWidth)73 void TabBarModifier::SetIndicatorWidth(float indicatorWidth)
74 {
75 if (indicatorWidth_) {
76 indicatorWidth_->Set(indicatorWidth);
77 }
78 }
79
SetIndicatorHeight(float indicatorHeight)80 void TabBarModifier::SetIndicatorHeight(float indicatorHeight)
81 {
82 if (indicatorHeight_) {
83 indicatorHeight_->Set(indicatorHeight);
84 }
85 }
86
SetIndicatorBorderRadius(float indicatorBorderRadius)87 void TabBarModifier::SetIndicatorBorderRadius(float indicatorBorderRadius)
88 {
89 if (indicatorBorderRadius_) {
90 indicatorBorderRadius_->Set(indicatorBorderRadius);
91 }
92 }
93
SetIndicatorMarginTop(float indicatorMarginTop)94 void TabBarModifier::SetIndicatorMarginTop(float indicatorMarginTop)
95 {
96 if (indicatorMarginTop_) {
97 indicatorMarginTop_->Set(indicatorMarginTop);
98 }
99 }
100
SetSelectedMode(SelectedMode selectedMode)101 void TabBarModifier::SetSelectedMode(SelectedMode selectedMode)
102 {
103 if (hasIndicator_) {
104 hasIndicator_->Set(selectedMode == SelectedMode::INDICATOR ? true : false);
105 }
106 }
107
PaintIndicator(DrawingContext & context,RectF indicator)108 void TabBarModifier::PaintIndicator(DrawingContext& context, RectF indicator)
109 {
110 auto& canvas = context.canvas;
111 auto pipelineContext = PipelineContext::GetCurrentContext();
112 CHECK_NULL_VOID(pipelineContext);
113 auto tabTheme = pipelineContext->GetTheme<TabTheme>();
114 CHECK_NULL_VOID(tabTheme);
115
116 if (GreatNotEqual(indicatorHeight_->Get(), 0.0f)) {
117 indicator.SetHeight(indicatorHeight_->Get());
118 } else {
119 indicator.SetHeight(tabTheme->GetSubTabIndicatorHeight().ConvertToPx());
120 }
121 indicator.SetWidth(indicatorWidth_->Get());
122 indicator.SetLeft(indicatorLeft_->Get());
123 if (GreatNotEqual(indicatorMarginTop_->Get(), 0.0f)) {
124 indicator.SetTop(indicatorTop_->Get() + indicatorMarginTop_->Get());
125 } else {
126 indicator.SetTop(indicatorTop_->Get());
127 }
128
129 RSBrush brush;
130 brush.SetAntiAlias(true);
131 brush.SetColor(ToRSColor(indicatorColor_->Get()));
132 brush.SetBlendMode(RSBlendMode::SRC_OVER);
133 canvas.AttachBrush(brush);
134 if (indicatorBorderRadius_->Get() > 0) {
135 RSRoundRect rect(ToRSRect(indicator), indicatorBorderRadius_->Get(), indicatorBorderRadius_->Get());
136 canvas.DrawRoundRect(rect);
137 } else {
138 canvas.DrawRect(ToRSRect(indicator));
139 }
140 canvas.DetachBrush();
141 canvas.Restore();
142 }
143 } // namespace OHOS::Ace::NG
144