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()) {
51         PaintIndicator(context);
52     }
53 }
54 
SetIndicatorOffset(const OffsetF & indicatorOffset)55 void TabBarModifier::SetIndicatorOffset(const OffsetF& indicatorOffset)
56 {
57     if (indicatorLeft_) {
58         indicatorLeft_->Set(indicatorOffset.GetX());
59     }
60     if (indicatorTop_) {
61         indicatorTop_->Set(indicatorOffset.GetY());
62     }
63 }
64 
SetIndicatorColor(const LinearColor & indicatorColor)65 void TabBarModifier::SetIndicatorColor(const LinearColor& indicatorColor)
66 {
67     if (indicatorColor_) {
68         indicatorColor_->Set(indicatorColor);
69     }
70 }
71 
SetIndicatorWidth(float indicatorWidth)72 void TabBarModifier::SetIndicatorWidth(float indicatorWidth)
73 {
74     if (indicatorWidth_) {
75         indicatorWidth_->Set(indicatorWidth);
76     }
77 }
78 
SetIndicatorHeight(float indicatorHeight)79 void TabBarModifier::SetIndicatorHeight(float indicatorHeight)
80 {
81     if (indicatorHeight_) {
82         indicatorHeight_->Set(indicatorHeight);
83     }
84 }
85 
SetIndicatorBorderRadius(float indicatorBorderRadius)86 void TabBarModifier::SetIndicatorBorderRadius(float indicatorBorderRadius)
87 {
88     if (indicatorBorderRadius_) {
89         indicatorBorderRadius_->Set(indicatorBorderRadius);
90     }
91 }
92 
SetIndicatorMarginTop(float indicatorMarginTop)93 void TabBarModifier::SetIndicatorMarginTop(float indicatorMarginTop)
94 {
95     if (indicatorMarginTop_) {
96         indicatorMarginTop_->Set(indicatorMarginTop);
97     }
98 }
99 
SetHasIndicator(bool hasIndicator)100 void TabBarModifier::SetHasIndicator(bool hasIndicator)
101 {
102     if (hasIndicator_) {
103         hasIndicator_->Set(hasIndicator);
104     }
105 }
106 
PaintIndicator(DrawingContext & context)107 void TabBarModifier::PaintIndicator(DrawingContext& context)
108 {
109     auto& canvas = context.canvas;
110     auto pipelineContext = PipelineContext::GetCurrentContext();
111     CHECK_NULL_VOID(pipelineContext);
112     auto tabTheme = pipelineContext->GetTheme<TabTheme>();
113     CHECK_NULL_VOID(tabTheme);
114 
115     RectF indicator;
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